Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option forwarded_allow_ips #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/fastapi_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def _run(
command: str,
app: Union[str, None] = None,
proxy_headers: bool = False,
forwarded_allow_ips: Union[str, None] = None,
) -> None:
try:
use_uvicorn_app = get_import_string(path=path, app_name=app)
Expand Down Expand Up @@ -97,6 +98,7 @@ def _run(
workers=workers,
root_path=root_path,
proxy_headers=proxy_headers,
forwarded_allow_ips=forwarded_allow_ips,
)


Expand Down Expand Up @@ -145,6 +147,12 @@ def dev(
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
),
] = True,
forwarded_allow_ips: Annotated[
Union[str, None],
typer.Option(
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
),
] = None,
) -> Any:
"""
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
Expand Down Expand Up @@ -180,6 +188,7 @@ def dev(
app=app,
command="dev",
proxy_headers=proxy_headers,
forwarded_allow_ips=forwarded_allow_ips,
)


Expand Down Expand Up @@ -234,6 +243,12 @@ def run(
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
),
] = True,
forwarded_allow_ips: Annotated[
Union[str, None],
typer.Option(
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
),
] = None,
) -> Any:
"""
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
Expand Down Expand Up @@ -270,6 +285,7 @@ def run(
app=app,
command="run",
proxy_headers=proxy_headers,
forwarded_allow_ips=forwarded_allow_ips,
)


Expand Down
33 changes: 33 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_dev() -> None:
"workers": None,
"root_path": "",
"proxy_headers": True,
"forwarded_allow_ips": None,
}
assert "Using import string single_file_app:app" in result.output
assert (
Expand Down Expand Up @@ -71,6 +72,7 @@ def test_dev_args() -> None:
"workers": None,
"root_path": "/api",
"proxy_headers": False,
"forwarded_allow_ips": None,
}
assert "Using import string single_file_app:api" in result.output
assert (
Expand All @@ -97,6 +99,36 @@ def test_run() -> None:
"workers": None,
"root_path": "",
"proxy_headers": True,
"forwarded_allow_ips": None,
}
assert "Using import string single_file_app:app" in result.output
assert (
"╭─────────── FastAPI CLI - Production mode ───────────╮" in result.output
)
assert "│ Serving at: http://0.0.0.0:8000" in result.output
assert "│ API docs: http://0.0.0.0:8000/docs" in result.output
assert "│ Running in production mode, for development use:" in result.output
assert "│ fastapi dev" in result.output


def test_run_trust_proxy() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app, ["run", "single_file_app.py", "--forwarded-allow-ips", "*"]
)
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:app",
"host": "0.0.0.0",
"port": 8000,
"reload": False,
"workers": None,
"root_path": "",
"proxy_headers": True,
"forwarded_allow_ips": "*",
}
assert "Using import string single_file_app:app" in result.output
assert (
Expand Down Expand Up @@ -141,6 +173,7 @@ def test_run_args() -> None:
"workers": 2,
"root_path": "/api",
"proxy_headers": False,
"forwarded_allow_ips": None,
}
assert "Using import string single_file_app:api" in result.output
assert (
Expand Down
Loading