-
Notifications
You must be signed in to change notification settings - Fork 6
fix: make opa local setup more resilient #331
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,22 @@ async def dispatch(self, request: Request, call_next) -> Response: | |
| return await call_next(request) | ||
|
|
||
|
|
||
| def preflight_embedded_auth_policy_wasm(auth_config) -> None: | ||
| """Ensure local embedded auth PDP has a loadable policy.wasm before serving traffic.""" | ||
| if not auth_config.enabled or auth_config.policy_decision_point_provider != "embedded": | ||
| return | ||
|
|
||
| try: | ||
| from nmp.core.auth.app.embedded_pdp.policy_wasm import ensure_embedded_policy_wasm | ||
| except ImportError as exc: | ||
| raise RuntimeError( | ||
| "Auth is enabled with the embedded PDP, but the nmp-auth package is not installed. " | ||
| "Install nmp-auth or set auth.policy_decision_point_provider='opa'." | ||
| ) from exc | ||
|
Comment on lines
+80
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Narrow the import fallback to true missing-module cases. Catching Proposed fix- try:
- from nmp.core.auth.app.embedded_pdp.policy_wasm import ensure_embedded_policy_wasm
- except ImportError as exc:
+ try:
+ from nmp.core.auth.app.embedded_pdp.policy_wasm import ensure_embedded_policy_wasm
+ except ModuleNotFoundError as exc:
+ if exc.name != "nmp.core.auth.app.embedded_pdp.policy_wasm":
+ raise
raise RuntimeError(
"Auth is enabled with the embedded PDP, but the nmp-auth package is not installed. "
"Install nmp-auth or set auth.policy_decision_point_provider='opa'."
) from exc🤖 Prompt for AI Agents |
||
|
|
||
| ensure_embedded_policy_wasm(auto_build=getattr(auth_config, "embedded_pdp_auto_build_wasm", True)) | ||
|
|
||
|
|
||
| def create_platform_openapi_app() -> FastAPI: | ||
| """Create the platform app used for aggregate OpenAPI generation.""" | ||
| services = [] | ||
|
|
@@ -196,13 +212,15 @@ async def root_handler() -> Response: | |
|
|
||
| def run_server(services: list[Service] | None = None, host: str = "0.0.0.0", port: int = 8080) -> None: | ||
| """Run the platform API server.""" | ||
| preflight_embedded_auth_policy_wasm(get_auth_config()) | ||
| app = create_app(services or []) | ||
| setup_fastapi_instrumentations(app) | ||
| uvicorn.run(app, host=host, port=port, log_config=None) | ||
|
|
||
|
|
||
| def run_server_with_reload(app_factory: str, host: str = "0.0.0.0", port: int = 8080) -> None: | ||
| """Run the platform API server with uvicorn reload enabled.""" | ||
| preflight_embedded_auth_policy_wasm(get_auth_config()) | ||
| reload_dirs = [ | ||
| "packages/nmp_platform/src", | ||
| "services/core", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from nmp.platform_runner import run | ||
| from rich.console import Console | ||
|
|
||
|
|
||
| class PolicyWasmError(Exception): | ||
| pass | ||
|
|
||
|
|
||
| PolicyWasmError.__module__ = "nmp.core.auth.app.embedded_pdp.policy_wasm" | ||
|
|
||
|
|
||
| def test_policy_wasm_error_is_expected_startup_error(): | ||
| assert run._is_policy_wasm_error(PolicyWasmError("boom")) | ||
| assert not run._is_policy_wasm_error(RuntimeError("boom")) | ||
|
|
||
|
|
||
| def test_policy_wasm_error_renders_as_panel(tmp_path, monkeypatch): | ||
| stderr = tmp_path / "stderr.txt" | ||
| console = Console(file=stderr.open("w"), force_terminal=False, width=100) | ||
| monkeypatch.setattr(run, "error_console", console) | ||
|
|
||
| run._display_policy_wasm_error( | ||
| PolicyWasmError( | ||
| "Failed to build embedded auth PDP policy.wasm.\n\n" | ||
| "Command:\n" | ||
| " script/build_policy_wasm.sh\n\n" | ||
| "Offline options:\n" | ||
| " OPA_BIN=/path/to/opa ./script/build_policy_wasm.sh" | ||
| ) | ||
| ) | ||
|
|
||
| output = stderr.read_text() | ||
| assert "Embedded Auth Policy WASM Startup Failed" in output | ||
| assert "script/build_policy_wasm.sh" in output | ||
| assert "OPA_BIN=/path/to/opa" in output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Config example contradicts the documented/default behavior.
This snippet sets
embedded_pdp_auto_build_wasm: false, but the section describes auto-build behavior for source checkouts and the code default istrue.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents