Skip to content

Commit

Permalink
fix(CLI): #2613 - SSL certificate feature breaks default no-tls setup…
Browse files Browse the repository at this point in the history
…s when using reload or multiple workers (#2616)

* Fix 2613

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* fix(CLI): #2613 - SSL certificate feature breaks default no-tls setups when using reload or multiple workers (Sourcery refactored) (#2617)

'Refactored by Sourcery'

Co-authored-by: Sourcery AI <>

---------

Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
  • Loading branch information
provinzkraut and sourcery-ai[bot] committed Nov 4, 2023
1 parent 1f790fb commit f8bfa4a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
66 changes: 47 additions & 19 deletions litestar/cli/commands/core.py
Expand Up @@ -51,6 +51,42 @@ def _convert_uvicorn_args(args: dict[str, Any]) -> list[str]:
return process_args


def _run_uvicorn_in_subprocess(
*,
env: LitestarEnv,
host: str | None,
port: int | None,
workers: int | None,
reload: bool,
reload_dirs: tuple[str, ...] | None,
fd: int | None,
uds: str | None,
certfile_path: str | None,
keyfile_path: str | None,
) -> None:
process_args: dict[str, Any] = {
"reload": reload,
"host": host,
"port": port,
"workers": workers,
"factory": env.is_app_factory,
}
if fd is not None:
process_args["fd"] = fd
if uds is not None:
process_args["uds"] = uds
if reload_dirs:
process_args["reload-dir"] = reload_dirs
if certfile_path is not None:
process_args["ssl-certfile"] = certfile_path
if keyfile_path is not None:
process_args["ssl-keyfile"] = keyfile_path
subprocess.run(
[sys.executable, "-m", "uvicorn", env.app_path, *_convert_uvicorn_args(process_args)], # noqa: S603
check=True,
)


@command(name="version")
@option("-s", "--short", help="Exclude release level and serial information", is_flag=True, default=False)
def version_command(short: bool) -> None:
Expand Down Expand Up @@ -193,25 +229,17 @@ def run_command(
" with the --reload or --workers options[/]"
)

process_args = {
"reload": reload,
"host": host,
"port": port,
"workers": workers,
"factory": env.is_app_factory,
"ssl-certfile": certfile_path,
"ssl-keyfile": keyfile_path,
}
if fd is not None:
process_args["fd"] = fd
if uds is not None:
process_args["uds"] = uds
if reload_dirs:
process_args["reload-dir"] = reload_dirs

subprocess.run(
[sys.executable, "-m", "uvicorn", env.app_path, *_convert_uvicorn_args(process_args)], # noqa: S603
check=True,
_run_uvicorn_in_subprocess(
env=env,
host=host,
port=port,
workers=workers,
reload=reload,
reload_dirs=reload_dirs,
fd=fd,
uds=uds,
certfile_path=certfile_path,
keyfile_path=keyfile_path,
)


Expand Down
2 changes: 0 additions & 2 deletions tests/unit/test_cli/test_core_commands.py
Expand Up @@ -142,8 +142,6 @@ def test_run_command(
f"{path.stem}:app",
f"--host={host}",
f"--port={port}",
"--ssl-certfile=None",
"--ssl-keyfile=None",
]
if fd is not None:
expected_args.append(f"--fd={fd}")
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/test_cli/test_ssl.py
Expand Up @@ -154,10 +154,10 @@ def test_one_file_not_found(
args = ["--app", app_path, "run"]

if ssl_certfile is not None:
args.extend(["--ssl-certfile", str(ssl_certfile)])
args.extend(["--ssl-certfile", ssl_certfile])

if ssl_keyfile is not None:
args.extend(["--ssl-keyfile", str(ssl_keyfile)])
args.extend(["--ssl-keyfile", ssl_keyfile])

if create_self_signed_cert:
args.append("--create-self-signed-cert")
Expand Down Expand Up @@ -321,9 +321,12 @@ def test_arguments_passed(
"--host=127.0.0.1",
"--port=8000",
"--workers=2",
f"--ssl-certfile={None if ssl_certfile is None else str(project_path / ssl_certfile)}",
f"--ssl-keyfile={None if ssl_keyfile is None else str(project_path / ssl_keyfile)}",
]
if ssl_certfile is not None:
expected_args.append(f"--ssl-certfile={project_path / ssl_certfile}")
if ssl_keyfile is not None:
expected_args.append(f"--ssl-keyfile={project_path / ssl_keyfile}")

mock_subprocess_run.assert_called_once()
assert sorted(mock_subprocess_run.call_args_list[0].args[0]) == sorted(expected_args)

Expand Down

0 comments on commit f8bfa4a

Please sign in to comment.