Skip to content

Commit ad9183d

Browse files
committed
Merge branch 'main' of github.com:sanic-org/sanic into current-release
2 parents da23f85 + d70636b commit ad9183d

3 files changed

Lines changed: 50 additions & 18 deletions

File tree

sanic/worker/loader.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@
55

66
from importlib import import_module
77
from pathlib import Path
8-
from typing import (
9-
TYPE_CHECKING,
10-
Any,
11-
Callable,
12-
Dict,
13-
Optional,
14-
Type,
15-
Union,
16-
cast,
17-
)
8+
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union, cast
189

19-
from sanic.http.tls.creators import CertCreator, MkcertCreator, TrustmeCreator
10+
from sanic.http.tls.context import process_to_context
11+
from sanic.http.tls.creators import MkcertCreator, TrustmeCreator
2012

2113

2214
if TYPE_CHECKING:
@@ -106,21 +98,30 @@ def load(self) -> SanicApp:
10698

10799

108100
class CertLoader:
109-
_creator_class: Type[CertCreator]
101+
_creators = {
102+
"mkcert": MkcertCreator,
103+
"trustme": TrustmeCreator,
104+
}
110105

111106
def __init__(self, ssl_data: Dict[str, Union[str, os.PathLike]]):
112-
creator_name = ssl_data.get("creator")
113-
if creator_name not in ("mkcert", "trustme"):
107+
self._ssl_data = ssl_data
108+
109+
creator_name = cast(str, ssl_data.get("creator"))
110+
111+
self._creator_class = self._creators.get(creator_name)
112+
if not creator_name:
113+
return
114+
115+
if not self._creator_class:
114116
raise RuntimeError(f"Unknown certificate creator: {creator_name}")
115-
elif creator_name == "mkcert":
116-
self._creator_class = MkcertCreator
117-
elif creator_name == "trustme":
118-
self._creator_class = TrustmeCreator
119117

120118
self._key = ssl_data["key"]
121119
self._cert = ssl_data["cert"]
122120
self._localhost = cast(str, ssl_data["localhost"])
123121

124122
def load(self, app: SanicApp):
123+
if not self._creator_class:
124+
return process_to_context(self._ssl_data)
125+
125126
creator = self._creator_class(app, self._key, self._cert)
126127
return creator.generate_cert(self._localhost)

tests/test_tls.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import subprocess
55

66
from contextlib import contextmanager
7+
from multiprocessing import Event
78
from pathlib import Path
89
from unittest.mock import Mock, patch
910
from urllib.parse import urlparse
@@ -636,3 +637,29 @@ def test_sanic_ssl_context_create():
636637

637638
assert sanic_context is context
638639
assert isinstance(sanic_context, SanicSSLContext)
640+
641+
642+
def test_ssl_in_multiprocess_mode(app: Sanic, caplog):
643+
644+
ssl_dict = {"cert": localhost_cert, "key": localhost_key}
645+
event = Event()
646+
647+
@app.main_process_start
648+
async def main_start(app: Sanic):
649+
app.shared_ctx.event = event
650+
651+
@app.after_server_start
652+
async def shutdown(app):
653+
app.shared_ctx.event.set()
654+
app.stop()
655+
656+
assert not event.is_set()
657+
with caplog.at_level(logging.INFO):
658+
app.run(ssl=ssl_dict)
659+
assert event.is_set()
660+
661+
assert (
662+
"sanic.root",
663+
logging.INFO,
664+
"Goin' Fast @ https://127.0.0.1:8000",
665+
) in caplog.record_tuples

tests/worker/test_loader.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def test_input_is_module():
8686
@patch("sanic.worker.loader.TrustmeCreator")
8787
@patch("sanic.worker.loader.MkcertCreator")
8888
def test_cert_loader(MkcertCreator: Mock, TrustmeCreator: Mock, creator: str):
89+
CertLoader._creators = {
90+
"mkcert": MkcertCreator,
91+
"trustme": TrustmeCreator,
92+
}
8993
MkcertCreator.return_value = MkcertCreator
9094
TrustmeCreator.return_value = TrustmeCreator
9195
data = {

0 commit comments

Comments
 (0)