Repository navigation
feat(access-control): add API switch, base path - #678
Conversation
Operators who manage roles through the configuration only can take the permissions API off the internet with api_enabled, or move it with api_base_path, without supplying a whole specification. A custom specification wins, with a warning. Closes #611 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Warn about literal rules that match the permissions API and rules for its default path after a move, and about a base path under an API of the app; refuse the same base path, naming the setting. Say what happens to stored rules, and warn about self-service without the API. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Check the objects of `p` rules only, so that wildcards such as `*` are found, and leave out rules of admins and invalid patterns. Warn about overlaps of base paths both ways, and not about rules for the default path if the API moved below it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…pi-switch # Conflicts: # foca/security/access_control/register_access_control.py
Reviewer's GuideThe pull request introduces an access-control API switch and configurable base path, retaining policy enforcement when management endpoints are disabled. It also adds path-conflict validation, policy safety diagnostics, custom-specification precedence handling, documentation, and comprehensive tests. Sequence diagram for configurable access-control API registrationsequenceDiagram
participant Startup
participant register_permission_specs
participant Connexion
participant AccessControlAPI
Startup->>register_permission_specs: register_permission_specs()
alt api_enabled is false
register_permission_specs-->>Startup: Return app without API registration
else api_enabled is true and api_specs is unset
register_permission_specs->>_check_api_base_path: _check_api_base_path(app, api_base_path)
_check_api_base_path-->>register_permission_specs: Validate path and detect conflicts
register_permission_specs->>Connexion: add_api(specification, base_path)
Connexion-->>AccessControlAPI: Register permissions API
else api_specs is set
register_permission_specs->>Connexion: add_api(specification)
Connexion-->>AccessControlAPI: Use specification-defined base path
end
Flow diagram for access-control API configuration outcomesflowchart TD
Config[AccessControlConfig] --> Enabled{api_enabled?}
Enabled -->|No| Enforce[Enforce configured and stored policies]
Enabled -->|Yes| Spec{api_specs set?}
Spec -->|Yes| Custom[Register custom specification path]
Spec -->|No| Validate[Validate api_base_path]
Validate --> Conflict{Exact app API base path?}
Conflict -->|Yes| Reject[Raise configuration error]
Conflict -->|No| Register[Register default API at api_base_path]
Validate --> Overlap[Warn on overlapping app API paths]
Overlap --> Register
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #678 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 58 58
Lines 7538 7592 +54
=========================================
+ Hits 7538 7592 +54
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Hey - I've found 6 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/security/access_control/register_access_control.py" line_range="574" />
<code_context>
specification: Path | dict[str, Any]
+ base_path: dict[str, str] = {}
if access_control_config.api_specs is None:
+ _check_api_base_path(app.app, access_control_config.api_base_path)
+ base_path["base_path"] = access_control_config.api_base_path
spec_path = str(files(ACCESS_CONTROL_BASE_PATH).joinpath(DEFAULT_API_SPEC_PATH))
</code_context>
<issue_to_address>
**Overlapping routes go unchecked**
When a custom specification resolves to a base path that equals or overlaps an app API's base path, `register_permission_specs` skips `_check_api_base_path` for custom specifications, so the overlap warning or refusal never runs and the routes can shadow one another or have policy rules grant access across them.
Resolve the custom specification's base path and pass it to `_check_api_base_path` as well as checking the default specification's path.
Also at `foca/security/access_control/register_access_control.py:585-586`, `foca/security/access_control/register_access_control.py:588`.
</issue_to_address>
### Comment 2
<location path="foca/security/access_control/register_access_control.py" line_range="644-645" />
<code_context>
+ "'security.access_control.api_base_path' must differ from the "
+ f"base paths of the app's APIs: '{base_path}'"
+ )
+ if (base_path and api_base_path.startswith(f"{base_path}/")) or (
+ base_path.startswith(f"{api_base_path}/")
+ ):
+ logger.warning(
</code_context>
<issue_to_address>
**Root API route collisions go unnoticed**
When an app API has a root base path and declares a path and method also registered by the permissions API, `_check_api_base_path` skips the overlap check when an app API has an empty root base path, without checking its registered paths. If that API declares a route also used by the permissions API, the conflict is not warned about and one route can shadow the other.
Check the registered operation paths for collisions when either API has a root base path, and warn or refuse overlapping routes.
</issue_to_address>
### Comment 3
<location path="foca/models/config.py" line_range="944" />
<code_context>
"""
enabled: bool = False
+ api_enabled: bool = True
+ api_base_path: str = "/admin/access-control"
api_specs: str | None = None
</code_context>
<issue_to_address>
**Disabled API settings prevent startup**
When `api_enabled` is false, `api_swagger_ui` is true, and `api_serve_spec` is explicitly false, `AccessControlConfig.validate_swagger_ui` raises before `register_permission_specs` can skip registration, so the app does not start even though the API and its settings are inactive.
Skip the Swagger UI validation when `api_enabled` is false.
Also at `docs/access-control.md:215`.
</issue_to_address>
### Comment 4
<location path="foca/security/access_control/register_access_control.py" line_range="752" />
<code_context>
+ not access_control_config.api_enabled
+ or access_control_config.api_specs is not None
+ or access_control_config.api_base_path == default
+ or access_control_config.api_base_path.startswith(f"{default}/")
+ ):
+ return
</code_context>
<issue_to_address>
**Stale policy warning is suppressed**
When the API moves below the default path and a configured policy targets only an old endpoint, `_warn_default_base_path` returns for every base path below the default, so it never checks the policy and operators miss that the rule no longer applies to the moved API.
Allow the stale-policy check to run for base paths below the default.
</issue_to_address>
### Comment 5
<location path="foca/security/access_control/register_access_control.py" line_range="756" />
<code_context>
+ ):
+ return
+ for rule in access_control_config.policies or []:
+ if any(value.startswith(f"{default}/") for value in rule[1:]):
+ logger.warning(
+ "Policy rule %r names the default path of the permissions API, "
</code_context>
<issue_to_address>
**Stale policy warnings are inaccurate**
When the API moves and a policy has a path-like subject or action, or its object matches a former API path through a wildcard, `_warn_default_base_path` scans every policy value with a literal prefix test, so it warns for subjects or actions that do not target the API and misses wildcard object matches; operators receive misleading diagnostics or miss stale-rule warnings.
Check the policy object against the former API paths using path-pattern matching rather than scanning every value with a literal prefix test.
</issue_to_address>
### Comment 6
<location path="foca/models/config.py" line_range="944-945" />
<code_context>
"""
enabled: bool = False
+ api_enabled: bool = True
+ api_base_path: str = "/admin/access-control"
api_specs: str | None = None
api_swagger_ui: bool = False
</code_context>
<issue_to_address>
**API settings are silently ignored**
When `security.access_control.enabled` is false and either new setting is explicitly configured, `SecurityConfig.validate_access_control` omits the new API settings from its disabled-access-control checks, and `Foca.create_app` skips registration when `enabled` is false. An explicitly configured `api_enabled: false` or non-default `api_base_path` is therefore accepted but has no effect.
Have the disabled-access-control validator reject explicitly configured API settings that differ from their defaults.
</issue_to_address>Linked issues
#611 feat(access-control): add API switch and base path · 6 of 6 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Disable the permissions API without disabling policy enforcement | ✅ Met | Registration returns early when api_enabled is false; tests check that the API routes return 404 and policies still allow or deny requests. |
| 2 | Add api_base_path, defaulting to /admin/access-control | ✅ Met | The config field defaults to /admin/access-control and is passed to Connexion for the default specification; tests cover the default and a custom path. |
| 3 | Let an explicit api_specs specification win, with a warning | ✅ Met | A custom specification takes precedence, and an explicitly set api_base_path triggers a warning; the behavior is documented and tested. |
| 4 | Test disabled routes, unchanged enforcement, custom base path, defaults, and 100% coverage | ✅ Met | The added tests cover these cases, and the PR description reports pytest with 100% coverage. |
| 5 | Update the documentation | ✅ Met | The access-control guide and configuration reference describe the switch, base path, and custom-spec precedence. |
| 6 | Keep existing behavior by default | ✅ Met | api_enabled defaults to true and api_base_path defaults to the existing API path. |
Sourcery assessment
Needs a human reviewer. 6 findings to address first, and moving the permissions API beneath a path covered by a broad policy can let users use that API to change permissions, potentially granting themselves admin access. Reverting the path can close the exposure, but it does not undo permission rules already written through the API.
Blocking findings: foca/security/access_control/register_access_control.py:574, foca/security/access_control/register_access_control.py:645, foca/models/config.py:944, foca/security/access_control/register_access_control.py:752, foca/security/access_control/register_access_control.py:756, and 1 more
Warn about rules for the former path of the API by their objects only, also for wildcards and below the default. Refuse API settings without access control, and don't check those of a disabled API. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Closes #611.
security.access_control.api_enabled(defaulttrue): withfalse, the permissions API, its Swagger UI and specification aren't registered, and policies are still enforced.security.access_control.api_base_path(default/admin/access-control) moves the default API; a customapi_specswins, with a warning.api_specsorself_servicewithout the API.policiesof typepother than those of admins that match the API; they can be deliberate, so they aren't refused, unlike rules with{base_path}.policiesfor the default path after a move.docs/access-control.md,docs/configuration/access-control.md.Verification
dev.{base_path}refusal before and after the refactor on 1620 combinations: same outcomes.Declined review findings
**base_pathdict style (low): kept, so that custom specifications keep Connexion's handling of the base path.policiesdoesn't remove them.🤖 Generated with Claude Code
Summary by Sourcery
Add configurable registration and routing controls for the access-control management API while preserving policy enforcement and warning about potentially affected configurations.
New Features:
Enhancements:
Documentation:
Tests: