Skip to content

fix: allow opt-in roles without group mapping - #533

Merged
uniqueg merged 4 commits into
devfrom
fix/opt-in-unmapped
Oct 26, 2026
Merged

uniqueg merged 4 commits into
devfrom
fix/opt-in-unmapped

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Summary

Opt-in roles (security.access_control.opt_in_roles) no longer need a group mapping to them. This lets an app make a role opt-in in its base config (e.g., opt_in_roles: [admin] for an admin mode) and leave group_mapping to its deployments. Until groups are mapped to such a role, no user can opt in to it. Foca.create_app() warns of these roles on startup.

Closes #530

  • Validation:
    • validate_opt_in_roles drops the check that opt-in roles must be values of group_mapping.
    • Configured rules (policies) that grant opt-in roles are still refused, also for unmapped roles.
    • validate_mapped_roles now checks opt_in_roles directly: no resource_roles, and no user:, group: or client: prefixes. These checks used to apply indirectly, through the mapping.
  • Startup warning: check_opt_in_roles() warns of opt-in roles that no group is mapped to. The warning notes that service clients mapped to them always have them.
  • Docs:
    • docs/access-control.md covers the base-config use case and the restrictions. It asks deployers to check the warning, as a misspelled opt-in role is now only warned of.
    • The startup checks in docs/configuration/access-control.md and docs/deployment.md list the new warning.
    • The attribute docs are updated.

Motivation: elixir-cloud-aai/cloud-registry#240, whose Docker Compose and test configs have no admin mapping.

Verification

  • coverage run -m pytest on MongoDB 7.0.34: 2967 passed, coverage 100%.
    • One unrelated timing-flaky failure, tests/plugins/test_plugin_backends.py::test_cache (30.00003 vs 30), passed 3 times on rerun.
    • After the last, docs-only commit, tests/test_foca.py and tests/models/test_config.py were rerun: 280 passed.
  • ruff check, ruff format --check, mypy, ast-grep scan, semgrep scan --config .semgrep --error and mkdocs build --strict: clean.
  • Reviews: two rounds with the adversarial and quality reviewer subagents.
    • Round 1 found one high and one medium finding, both fixed in 2cd6efb. With the mapping check gone, resource roles and subject-prefixed names were accepted as opt-in roles. A group: opt-in role would have applied without opt-in.
    • Round 1 also found stale docs of the startup checks and the definition "roles of group_mapping"; both fixed.
    • Round 2 found no high or medium findings. Its lows are fixed in 663f881.

Declined review findings

  • Strict mode for unmapped opt-in roles: a typo in opt_in_roles used to stop the app and is now only warned of. A strict option, like require_admin, could turn the warning into an error once the mapping is final. Declined for now, as it would add a config option before the release. The docs now tell deployers to check the warning. Apps can also enforce the mapping themselves, as require_admin does for admins.
  • Excluding roles that only service clients have from the warning: kept. The warning says that service clients always have these roles. A role that is opt-in only for clients makes no sense, as client roles always apply.

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Allow opt-in roles without group mappings while preserving safeguards against unintended role application.

New Features:

  • Allow applications to declare opt-in roles before deployments map groups to them, enabling shared base configurations.

Bug Fixes:

  • Prevent unmapped opt-in roles from being treated as available while warning at startup when no group can opt in to them.
  • Continue rejecting policies and role definitions that could cause opt-in roles to apply without explicit opt-in.

Enhancements:

  • Extend opt-in role validation to enforce resource-role and subject-prefix restrictions independently of group mappings.

Documentation:

  • Document unmapped opt-in roles, their startup warning, validation restrictions, and deployment guidance.

Tests:

  • Add coverage for unmapped opt-in role validation, enforcement, permissions, and startup warnings.

uniqueg and others added 3 commits October 26, 2026 23:07
An app can make a role opt-in in its base config before its
deployments map groups to it: AccessControlConfig no longer refuses
opt-in roles that no group is mapped to. Nobody can opt in to them,
and Foca.create_app() warns of them on startup
(check_opt_in_roles()). Configured rules that grant opt-in roles are
still refused.

Closes #530

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Opt-in roles that no group is mapped to are no longer checked via
the mapping: refuse resource roles and subject prefixes in
opt_in_roles directly. The startup warning notes that service
clients mapped to the roles always have them. Add tests for
requests asking for unmapped roles and for the permissions API,
and update the docs of the startup checks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tell deployers to check the startup warning of unmapped opt-in
roles, as a misspelled role is only warned of. Tidy the paragraph,
a validator docstring and a test string.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg uniqueg added this to the v0.18.0 milestone Oct 26, 2026
@sourcery-ai

sourcery-ai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR lets applications declare opt-in roles in a base configuration without requiring immediate group mappings. Validation still blocks policy grants, resource roles, and subject-prefixed roles that could bypass opt-in, while startup logging warns when users cannot yet select configured roles; documentation and tests cover the new behavior.

Sequence diagram for unmapped opt-in role startup check

sequenceDiagram
    participant App as Foca.create_app()
    participant Check as check_opt_in_roles()
    participant Config as AccessControlConfig
    participant Logger as Logger
    App->>Check: check_opt_in_roles(access_control_config, cors_config)
    Check->>Config: Read opt_in_roles and group_mapping
    Check->>Logger: Log opt-in roles
    alt opt-in role has no mapped group
        Check->>Logger: warning: no user can opt in until groups are mapped
    end
    alt CORS is enabled
        Check->>Logger: Warn if opt_in_header is not allowed
    end
Loading

File-Level Changes

Change Details Files
Allow opt-in roles to be declared before any group is mapped to them, while preserving safeguards against roles that could apply without explicit opt-in.
  • Removed the requirement that every opt-in role appear in group_mapping.
  • Continued rejecting policy rules that grant opt-in roles, including unmapped ones.
  • Applied resource-role and subject-prefix restrictions directly to opt-in roles.
  • Added tests covering unmapped-role validation, enforcement, and permission API behavior.
foca/models/config.py
tests/models/test_config.py
tests/security/access_control/test_enforcement.py
Warn at application startup when configured opt-in roles cannot currently be selected by users.
  • Detect opt-in roles absent from group_mapping and emit a warning identifying them.
  • Clarify that mapped service clients still receive these roles automatically.
  • Add coverage for partial, empty, and absent group mappings and app startup behavior.
foca/security/access_control/register_access_control.py
tests/test_foca.py
Document the base-configuration workflow, restrictions, and startup warning for unmapped opt-in roles.
  • Explain that deployments may add group mappings later and should review startup warnings for typos.
  • Update configuration, deployment, and model attribute documentation.
  • Describe existing validation and CORS startup checks alongside the new warning.
docs/access-control.md
docs/configuration/access-control.md
docs/deployment.md
foca/models/config.py

Assessment against linked issues

Issue Objective Addressed Explanation
#530 Allow opt-in roles even when group_mapping is absent or does not map to them, so base and deployment configurations can load. ✅
#530 Warn at startup and name opt-in roles with no group mapping, while continuing to reject configured policies that grant opt-in roles and other unsafe opt-in role settings. ✅
#530 Update the access-control and configuration documentation and relevant docstrings to explain unmapped opt-in roles and their restrictions. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b3f5d0a) to head (7996e83).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #533   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           55        55           
  Lines         6667      6667           
=========================================
  Hits          6667      6667           
Flag Coverage Δ
test_unit 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review October 26, 2026 23:00

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. If the opt-in role is misspelled while a group is mapped to the intended role, the intended role can apply without being requested; the startup warning is the signal to catch that mismatch. Reverting restores the prior configuration behavior, but it cannot undo access or data exposure that occurred before the revert.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@uniqueg
uniqueg merged commit 6214325 into dev Oct 26, 2026
30 checks passed
@uniqueg
uniqueg deleted the fix/opt-in-unmapped branch October 26, 2026 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: allow opt-in roles without group mapping

1 participant