-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run scenarios flask application as gunicorn app. (#2)
- Loading branch information
Showing
7 changed files
with
176 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import tempfile | ||
from pathlib import Path | ||
|
||
from gunicorn.app.base import BaseApplication | ||
from OpenSSL import crypto | ||
from werkzeug.serving import generate_adhoc_ssl_pair | ||
|
||
|
||
class GunicornServer(BaseApplication): | ||
def __init__(self, app, **kwargs): | ||
self.options = kwargs | ||
self.application = app | ||
super().__init__() | ||
|
||
def load_config(self): | ||
if self.options.get("ssl"): | ||
cert_path, pkey_path = self.generate_devel_ssl_pair() | ||
self.options["certfile"] = str(cert_path) | ||
self.options["keyfile"] = str(pkey_path) | ||
|
||
config = { | ||
key: value for key, value in self.options.items() | ||
if key in self.cfg.settings and value is not None | ||
} | ||
for key, value in config.items(): | ||
self.cfg.set(key.lower(), value) | ||
|
||
def load(self): | ||
return self.application | ||
|
||
@staticmethod | ||
def generate_devel_ssl_pair() -> (Path, Path): | ||
cert_path = Path(tempfile.gettempdir()) / "threat9-test-bed.crt" | ||
pkey_path = Path(tempfile.gettempdir()) / "threat9-test-bed.key" | ||
|
||
if not cert_path.exists() or not pkey_path.exists(): | ||
cert, pkey = generate_adhoc_ssl_pair() | ||
with open(cert_path, 'wb') as f: | ||
f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) | ||
with open(pkey_path, 'wb') as f: | ||
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey)) | ||
|
||
return cert_path, pkey_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import logging | ||
|
||
from ..http_server import http_server | ||
from ..app import app | ||
from ..scenarios import HttpScenario | ||
from .base_http_service import BaseHttpService | ||
from .base_http_service import GunicornBasedHttpService | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class HttpScenarioService(BaseHttpService): | ||
class HttpScenarioService(GunicornBasedHttpService): | ||
def __init__(self, host: str, port: int, scenario: HttpScenario): | ||
http_server.config.update(SCENARIO=scenario) | ||
super().__init__(host, port, http_server) | ||
app.config.update(SCENARIO=scenario) | ||
super().__init__(host, port, app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters