Repository navigation
fix: group-subject warning, listing media type - #639
Conversation
Warn about dropped group subjects only if a rule of `policies` has a group as its subject; otherwise log it at INFO, so that apps that only map groups to roles don't warn on every start. Declare the configured `exceptions.media_type` for the errors of the plugin listing's operations when a specification with them is registered, as the permissions API already does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Rules with group subjects can also be stored, e.g., written via the permissions API; warn about those as well, rather than only logging at INFO. Identify the plugin listing's operations by their IDs, as apps can override their controllers. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Stored rules with group subjects can still apply via role inheritance, so only say that they don't apply via group claims. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Reviewer's GuideThe PR makes access-control startup warnings conditional on actual configured or stored group-subject rules, while gracefully handling unavailable persistence checks. It also shares OpenAPI media-type utilities and uses them to ensure plugin listing errors advertise the application’s configured exception media type, with corresponding documentation and coverage. Sequence diagram for conditional group-subject startup diagnosticssequenceDiagram
participant App as Application startup
participant Register as register_casbin_enforcer
participant Config as Access control config
participant Adapter as Casbin adapter
participant MongoDB as Stored policy database
participant Logger
App->>Register: register_casbin_enforcer()
Register->>Config: _has_group_subject()
alt Configured group-subject rule exists
Register->>Logger: logger.log(WARNING)
else No configured group-subject rule
Register->>Adapter: has_stored_group_subjects()
Adapter->>MongoDB: count_documents(query, limit=1)
alt Stored group-subject rule exists
MongoDB-->>Adapter: stored = true
Adapter-->>Register: true
Register->>Logger: logger.warning()
else No stored rule or database check fails
MongoDB-->>Adapter: result or PyMongoError
Adapter-->>Register: false or exception handled
Register->>Logger: logger.log(INFO) or logger.warning()
end
end
Register-->>App: Continue startup
Sequence diagram for plugin listing error media type registrationsequenceDiagram
participant App as Connexion application
participant Register as register_openapi
participant Config as FOCA config
participant Spec as Plugin listing specification
App->>Register: register_openapi()
Register->>Config: _get_error_media_type(app)
Config-->>Register: exceptions.media_type or default
Register->>Spec: _iter_operations(spec_parsed)
loop Plugin listing operations
Register->>Register: _set_error_media_type(operation, media_type)
end
Register-->>App: Register merged specification with configured error media type
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 #639 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 56 56
Lines 7090 7119 +29
=========================================
+ Hits 7090 7119 +29
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 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/api/register_openapi.py" line_range="172" />
<code_context>
+ for status, response in operation.get("responses", {}).items():
+ content = response.get("content", {})
+ if str(status)[0] in "45" and DEFAULT_ERROR_MEDIA_TYPE in content:
+ content[media_type] = content.pop(DEFAULT_ERROR_MEDIA_TYPE)
+
+
</code_context>
<issue_to_address>
**Existing error schema is overwritten**
When a merged listing response already contains both `application/problem+json` and the configured media type, `content[media_type]` replaces any schema already declared for the configured media type with the `application/problem+json` schema, so the served listing advertises the wrong schema or fails response validation.
Preserve an existing configured-media-type entry instead of unconditionally replacing it.
</issue_to_address>
### Comment 2
<location path="foca/api/register_openapi.py" line_range="433-439" />
<code_context>
spec_parsed: dict[str, Any] = ConfigParser.merge_yaml(*list_specs)
logger.debug("Parsed spec: %s", list_specs)
+ # Declare the media type that the plugin listing's errors have; the
+ # app's patches may still change it
+ media_type = _get_error_media_type(app)
+ for operation_object in _iter_operations(spec_parsed):
+ if operation_object.get("operationId") in _PLUGIN_LISTING_OPERATIONS:
+ _set_error_media_type(operation_object, media_type)
+
# Add/replace root objects
if spec.append is not None:
</code_context>
<issue_to_address>
**Appended errors advertise stale media types**
When the specification adds or replaces plugin-listing paths through append, `register_openapi` rewrites media types before applying `spec.append`, so appended plugin-listing responses keep `application/problem+json` and the served document disagrees with the configured runtime media type.
Apply the error media-type rewrite after `spec.append` has been applied.
</issue_to_address>
### Comment 3
<location path="foca/security/access_control/register_access_control.py" line_range="295-299" />
<code_context>
+ Whether the subject of a rule of `policies`, which follows the policy
+ type (empty rules fail validation later), has the group prefix.
+ """
+ return any(
+ subject.startswith(GROUP_PREFIX)
+ for rule in access_control_config.policies or []
+ for subject in rule[1:2]
+ )
+
+
</code_context>
<issue_to_address>
**Dropped groups get info-only logs**
When a supported custom Casbin model places the subject outside the first policy value, the configured-rule check assumes the subject is the first policy value, and the stored-rule query only checks `v0`, so both checks miss group-subject rules and startup logs INFO instead of the intended warning.
Use the model's configured subject field for both the configured-rule check and the stored-rule query.
Also at `foca/security/access_control/foca_casbin_adapter/adapter.py:751-764`.
</issue_to_address>Linked issues
#577 fix: group-subject warning, listing media type · 3 of 3 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Avoid a warning when group subjects are unused, but warn when configured policies use them. | ✅ Met | The configured-policy check logs at INFO when no rule has a group subject and at WARNING when one does. |
| 2 | Also detect stored rules with group subjects when deciding whether to warn. | ✅ Met | Startup checks non-configured stored rules and warns when they have group subjects; a failed check logs a warning and does not stop startup. |
| 3 | Declare the configured error media type in the served plugin listing spec, with tests covering both media types. | ✅ Met | The listing operations’ error responses are patched during registration, and the parametrized test covers application/json and application/problem+json. |
Sourcery assessment
Approval pending. 3 findings to address first.
Blocking findings: foca/api/register_openapi.py:172, foca/api/register_openapi.py:439, foca/security/access_control/register_access_control.py:299
Also cover operations of `append`, and keep an entry for the configured media type rather than replacing it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Closes #577
Summary
group_mappingset andkeep_group_subjectsnot, FOCA now warns only if a rule actually has a group as its subject, inpoliciesor stored (e.g., written via the permissions API). Otherwise the message is logged atINFO, so apps like proTES that only map groups to roles don't warn on every start.exceptions.media_type, as the permissions API already does. The media type helpers moved tofoca/api/register_openapi.pyand are shared.docs/configuration/access-control.md,docs/deployment.md,docs/plugins.md.Verification
ruff check,ruff format --check,mypy,ast-grep scan,mkdocs build --strict: passcoverage run -m pytest -m "not mongodb": 3132 passed, 1 skipped, 100% coverage. Tests markedmongodbdidn't run locally (they need the shared port 12345); CI runs them.Declined review findings
has_stored_group_subjects()filters onv0withoutptype, so it can't use the filter indexes and may scan on large collections. It's one query per start, bounded by the 5 s timeout, and a timeout only logs that the hint couldn't be determined. Listing all policy types of a model isn't worth it for a hint._get_error_media_typewithout FOCA config and for_set_error_media_typedirectly: both paths are covered through the permissions API and listing tests (100% coverage).🤖 Generated with Claude Code
https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Summary by Sourcery
Prevent misleading group-subject warnings and ensure plugin listing errors advertise the application's configured media type.
Bug Fixes:
Enhancements:
Documentation:
Tests: