Repository navigation
feat: add startup checks of the config - #466
Conversation
Move the generic startup checks of cloud-registry into FOCA: - With access control enabled, log the groups mapped to the role `admin`, and warn if no group or configured rule grants it (roles of stored rules still apply); the new option `security.access_control.require_admin` (default: false) makes this an error. Warn if `group_mapping` is set without `claim_groups` or `keep_group_subjects`, and if `claim_groups` has surrounding whitespace. - Add `foca.security.auth.require_no_service_clients()`, for apps whose endpoints don't support service clients; FOCA doesn't run it. - Warn if the service info has values in reserved example domains (RFC 2606), as in templates. The `log` fixture moves to `tests/conftest.py`, for the tests of `Foca.create_app()`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Document the checks of access control settings and `require_admin`, placeholder service info, `require_no_service_clients()`, a list of startup checks in the deployment guide, the option in the config template, and the new warnings in the migration notes for 0.18. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Count service clients mapped to `admin` (`client_mapping`), count groups mapped to it only with a group claim set, and configured rules for groups only if group claim values are used as group subjects, so that `require_admin` neither refuses valid nor passes ineffective configurations. Simplify the placeholder check, test the group check messages by table and document the changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rules granting `admin` to roles or service clients count whether or not any request can have them. Also mention `claim_groups` in the template comment of `require_admin`, and reflow edited paragraphs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideAdds backward-compatible startup diagnostics for access-control misconfiguration and placeholder service metadata, with opt-in failure when no admin grant exists and a separate API for rejecting service clients; documentation, templates, and comprehensive tests are updated accordingly. Sequence diagram for FOCA startup checkssequenceDiagram
participant App as Application
participant Foca
participant AccessControl as register_access_control()
participant ServiceInfo as check_service_info()
participant Logger
App->>Foca: create_app()
Foca->>AccessControl: register_access_control()
AccessControl->>AccessControl: _check_group_config()
AccessControl->>AccessControl: _check_admin_role()
alt admin grant found
AccessControl->>Logger: log mapped admins
else no admin grant
alt require_admin enabled
AccessControl-->>Foca: raise ValueError
else default behavior
AccessControl->>Logger: log warning
end
end
Foca->>ServiceInfo: check_service_info()
ServiceInfo->>ServiceInfo: _placeholder_fields()
alt placeholder fields found
ServiceInfo->>Logger: log warning with field names
end
Foca-->>App: configured application
Sequence diagram for rejecting unsupported service clientssequenceDiagram
participant App as Application
participant Foca
participant Auth as require_no_service_clients()
App->>Foca: create_app()
Foca-->>App: configured application
App->>Auth: require_no_service_clients()
alt service_clients or on_behalf_of_header configured
Auth-->>App: raise RuntimeError
else neither configured
Auth-->>App: return
end
Flow diagram for access-control admin startup validationflowchart TD
A[Access control enabled] --> B["_check_group_config()"]
B --> C["_check_admin_role()"]
C --> D{Admin mapping or applicable policy rule?}
D -->|Yes| E[Continue startup]
D -->|No| F{require_admin enabled?}
F -->|Yes| G[Raise ValueError]
F -->|No| H[Log warning]
H --> E
B --> I[Warn about invalid group settings]
I --> E
File-Level Changes
Assessment against linked issues
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 #466 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 29 29
Lines 3378 3446 +68
=========================================
+ Hits 3378 3446 +68
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:
|
There was a problem hiding this comment.
Hey - I've found 4 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="245" />
<code_context>
+ `require_admin` is set.
+ """
+ conf = access_control_config
+ has_groups = bool(conf.claim_groups)
+ keeps_group_subjects = has_groups and (
+ conf.group_mapping is None or conf.keep_group_subjects
</code_context>
<issue_to_address>
**Startup accepts missing admin access**
When `claim_groups` has surrounding whitespace, a group maps to `admin`, `require_admin` is true, and no other admin grant applies, `bool(conf.claim_groups)` treats the padded name as configured, so `_check_admin_role` counts the mapping even though tokens using the unpadded claim provide no group admin; startup succeeds without an effective admin grant.
Normalize or reject whitespace-padded `claim_groups` before checking whether group mappings provide an admin grant.
</issue_to_address>
### Comment 2
<location path="foca/security/auth.py" line_range="272" />
<code_context>
+ current app context, if any, otherwise the most recently
+ created one (cf. :py:func:`foca.config.access.get_config`).
+ """
+ auth = get_config().security.auth
+ if auth.service_clients or auth.on_behalf_of_header is not None:
+ raise RuntimeError(
</code_context>
<issue_to_address>
**Startup checks the wrong app**
When multiple FOCA apps have different service-client settings and the helper runs outside the target app's context after another app was created, `require_no_service_clients()` calls `get_config()` without an app argument, so it checks the most recently created app instead of the target app; unsupported settings can pass for one app or spuriously reject another.
Make `require_no_service_clients()` read the configuration belonging to the app whose startup check is running.
</issue_to_address>
### Comment 3
<location path="foca/security/access_control/register_access_control.py" line_range="202" />
<code_context>
+ "have no groups: remove the whitespace.",
+ claim_groups,
+ )
+ if conf.group_mapping and not claim_groups:
+ logger.warning(
+ "Groups are mapped to roles "
</code_context>
<issue_to_address>
**Empty mapping hides missing group claim**
When `group_mapping` is `{}`, `claim_groups` is unset, and `keep_group_subjects` is true, `_check_group_config` skips its missing-claim warning for an empty `group_mapping`. With `keep_group_subjects: true`, group subjects then cannot be obtained because no `claim_groups` is configured, so group-based rules silently do not apply.
Check `conf.group_mapping is not None` rather than its truthiness when deciding whether to warn about a missing group claim.
</issue_to_address>
### Comment 4
<location path="foca/security/access_control/register_access_control.py" line_range="257" />
<code_context>
+ mapped = sorted(
+ key for key, role in (mapping or {}).items() if role == ADMIN_ROLE
+ )
+ if mapped and applies:
+ found = True
+ logger.info(
</code_context>
<issue_to_address>
**Configured admin groups go unlogged**
When `group_mapping` maps one or more values to `admin` but `claim_groups` is unset, `applies` is false when `claim_groups` is unset, so `_check_admin_role` skips logging group values mapped to `admin`. The startup warning identifies the missing claim setting but omits the configured group names, contrary to the documented promise to log those mappings.
Log configured admin group values regardless of whether the mapping currently applies, and separately indicate whether they are effective.
</issue_to_address>Sourcery assessment
Approval pending. 3 findings to address first.
Blocking findings: foca/security/access_control/register_access_control.py:245, foca/security/auth.py:272, foca/security/access_control/register_access_control.py:202
- Don't count groups mapped to `admin` if the group claim has surrounding whitespace, but log mapped groups that don't apply. - Warn about a missing group claim also with an empty `group_mapping`. - `require_no_service_clients()` takes an optional app to check, for processes with several FOCA apps. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ecks # Conflicts: # docs/migration-0.18.md
Summary
Moves the generic startup checks of cloud-registry into FOCA, run by
Foca.create_app():register_access_control(), only if access control is enabled, before anything is registered or stored):INFO) the groups and service clients mapped toadmin;claim_groupsset) or service client (client_mapping) is mapped toadminand no configuredgrule (policies) that applies grants it (rules forgroup:subjects count only if group claim values are used as group subjects), saying that roles granted via stored rules still apply. Service clients count as FOCA treats them as admins (is_admin()); only direct grants are checked;security.access_control.require_admin(defaultfalse; requiresenabled, like the other options): refuses to start (ValueError) instead of warning;group_mappingis set withoutclaim_groups, or withoutkeep_group_subjects, and ifclaim_groupshas surrounding whitespace.foca.security.auth.require_no_service_clients(app=None)(checks the given Flask app, or the current FOCA app; raisesRuntimeErrorifsecurity.auth.service_clientsoron_behalf_of_headeris set); not run by FOCA.check_service_info(), on the complete service info including runtime fields): warns, naming the fields, ifidis in a reserved example domain in reverse DNS notation (example.,com.example,net.example,org.example), or the host oforganization.url,contactUrlordocumentationUrlis one (example.com|net|org,*.example, subdomains; the address domain formailto:).docs/configuration/access-control.md, placeholders indocs/configuration/service-info.md,require_no_service_clients()indocs/authentication.mdand the API reference, a list of startup checks indocs/deployment.md, the option intemplates/config.yaml, and the new warnings and option indocs/migration-0.18.md.Backwards compatible: only new log messages; the only new error is opt-in. Messages name the setting to change and contain no claim values other than configured group names (escaped).
The
logfixture (capturing logs afterFoca()configures logging) moves fromtests/api/test_service_info.pytotests/conftest.py, for the newcreate_app()tests.Verification
pre-commit run --all-files: passedruff check,ruff format --check,mypy: passedcoverage run -m pytest(with MongoDB 7.0 on port 12345) +coverage report: 2324 passed (after mergingdev), 100% coveragemkdocs build --strict: passedrequire_no_service_clients(), emptygroup_mapping, logging unapplied admin groups).Declined review findings
group_mapping-without-keep_group_subjectswarning fires for apps that only map roles (low): issue feat: add startup checks of the config #459 asks for exactly this warning; it names the setting and only adds a log line. Lowering it toINFO, or warning only if rules forgroup:subjects are configured, can follow if it turns out noisy.adminto a role nobody can get, or to aclient:subject that is not a service client, still count. Checking that would need a fixpoint over thegrules and the auth config; documented instead that only direct grants are checked.Open questions
group_mappingwithoutclaim_groups, and a group claim with surrounding whitespace, become errors (as in cloud-registry), e.g., from FOCA 1.0? This PR keeps them warnings.Follow-up in cloud-registry (separate issue): set
security.access_control.require_admin: true, callrequire_no_service_clients(), and deletecheck_admin_groups(),check_no_service_clients(),warn_placeholder_service_info(), the placeholder constants andcustom.access_control.Closes #459
🤖 Generated with Claude Code