Skip to content

feat: add admin mode - #263

Merged
uniqueg merged 17 commits into
devfrom
feat/admin-mode
Oct 27, 2026
Merged

uniqueg merged 17 commits into
devfrom
feat/admin-mode

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Summary

Admin mode: admins act as admins only in requests that ask for it in the header X-Request-Roles: admin (FOCA's opt-in roles, elixir-cloud-aai/foca#502). Without the header, admins have the permissions of other users. GET /me tells clients whether the user may switch to admin mode (admin_available).

Builds on #261 (#226, FOCA 0.18), now merged; dev is merged in, so the diff shows only this PR. The merge conditions hold:

Closes #240

What changes

  • Config: security.access_control.opt_in_roles: [admin] in the base config.
    • The Docker Compose config (config.dev.yaml), which maps no admin group, sets opt_in_roles: [].
    • Deployments can opt out with opt_in_roles: [].
  • GET /me:
    • admin is true on requests in admin mode.
    • The new required field admin_available is true for members of admin groups, and whenever admin is true (e.g., without the opt-in, or with stored admin rules).
  • Duplicate conflicts: may_read_service() passes the opt-in header on to its synthetic request context. Admins in admin mode learn the registered service in a 409; admins without the header get the hidden 409, like other users.
  • FOCA pin: FOCA 0.18.0 (released), which includes fix: allow opt-in roles without group mapping foca#533 (#530): it allows opt-in roles that no group is mapped to. Without it, configs without an admin mapping fail to load.
  • Docs:
    • Admin mode for clients and scripts (header, Vary, CORS), in docs/access-control.md#admin-mode.
    • The API overview and the admin examples now send the header.
    • "Making users admins" in docs/operations.md: the permissions API refuses rules that grant admin.
    • Upgrade notes in docs/upgrading.md (From 0.5.0, item 4: the header, the GET /me semantics, configured rules refused, stored rules still applying with a warning) and in the chart README.

Verification

  • After merging dev (release pins, FOCA 0.18.0 from PyPI): full suite on MongoDB 7.0.34 passed, coverage 100%; scripts/check_release_pins.py passes.
  • Tests:
    • Full suite on MongoDB 7.0.34, with Helm installed locally, so the chart tests ran too: 1603 passed, coverage 100%.
    • After the last round of fixes (docs, spec descriptions, dev config, test constants), the full suite on mongomock: 1601 passed, 2 skipped, coverage 100%.
    • ruff, mypy, mkdocs build --strict and pre-commit run --all-files are clean.
  • New tests:
    • test_admin_mode covers admin operations, parametrized: an admin without the header gets the same status as a user without roles; with the header, the request succeeds.
    • test_admin_mode_vary, test_admin_mode_permissions_api and test_admin_mode_off (opt-out).
    • test_me covers admin_available.
    • test_admin_via_permissions_api: granting admin via the permissions API is refused.
    • The duplicate tests check the hidden 409 for admins without the header.
  • Reviews: two rounds with the adversarial and quality reviewer subagents.
    • Round 1 found one medium bug: /me could report admin: true with admin_available: false (opt-out, stored rules). It is fixed.
    • Round 1's mediums on tests and docs are fixed.
    • Round 2 found no high or medium findings; its lows are fixed.

Declined review findings

  • Default roles in make_headers() stays admin mode: the test helpers ask for admin mode by default. Most existing tests are about admin rights as such, and the no-header paths have their own tests (see above). The default is documented in the helper's summary.
  • Tests of FOCA's CORS warning: not added. The warning is FOCA's and is tested there; the docs only refer to it.

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Introduce opt-in admin mode so administrators must explicitly request elevated permissions while preserving configurable support for always-active admin access.

New Features:

  • Add opt-in admin mode, allowing administrators to exercise admin permissions only when requests include the configured roles header.
  • Expose admin_available through GET /me so clients can determine whether an administrator can switch to admin mode.

Bug Fixes:

  • Ensure duplicate-service conflict responses respect whether the request is operating in admin mode.

Enhancements:

  • Prevent permissions API rules from granting the admin role while admin mode is enabled.
  • Support deployments that disable admin mode to preserve always-active administrator permissions.

Deployment:

  • Document configuration and upgrade behavior for admin mode, including Helm deployment guidance.

Documentation:

  • Document admin-mode request headers, client and CORS requirements, API behavior, operations, and upgrade implications.

Tests:

  • Add coverage for admin-mode authorization, response variation, /me fields, opt-out behavior, permissions API restrictions, and duplicate conflict visibility.

uniqueg and others added 15 commits October 26, 2026 17:09
Validation details come from FOCA (exceptions.validation_details), and
problem() builds on FOCA's problem_response(), adding only the codes of
conflicts. Tests use locked_enforcer() instead of the removed
get_enforcer().

Refs #226

Co-Authored-By: Claude Opus 5.5 <[email protected]>
FOCA checks the group settings, requires admins
(security.access_control.require_admin, moved from
custom.access_control.require_admin), warns of placeholders in the
service info and refuses service clients
(require_no_service_clients()); the app's own copies are removed. The
chart reads require_admin from its new place and rejects the old key.

Refs #226

Co-Authored-By: Claude Opus 5.5 <[email protected]>
server.public_url replaces custom.endpoints.service; the app refuses to
start without it, and FOCA's get_public_url() builds the Location
header. The chart sets it from hostName or the new value publicUrl and
rejects the old key. FOCA's get_custom_config() replaces the app's.

Refs #226

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The image loads foca.gunicorn_config (same settings);
cloud_registry.gunicorn_config is removed. Tests keep the checks of
the image command and its health check port.

Refs #226

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Refs #226

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Chart docs name the required image version, tests render publicUrl,
docstrings and the dev overlay are clearer.

Refs #226

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Admins act as admins only in requests that ask for the role in the
header X-Request-Roles (FOCA opt-in roles, opt_in_roles: [admin] in
the base config). GET /me reports whether the user may switch to
admin mode (admin_available). The duplicate check passes the header
on to the read check, so admins in admin mode learn the registered
service. Document the header for clients and scripts, and the
upgrade (stored rules granting admin).

Closes #240

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pin FOCA and its development image to 6214325, which allows opt-in
roles that no group is mapped to (elixir-cloud-aai/foca#530), so
that configs without an admin mapping still load with admin mode.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GET /me reports admin_available whenever admin is true, e.g., without
the opt-in. Test each admin operation with and without admin mode,
the permissions API, Vary and the opt-out; clarify the upgrade notes
(GET /me, configured admin rules), the config comment and the docs on
making users admins.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Docker Compose config turns admin mode off, as it maps no admin
group. Describe when admin is true without the header, and when
configured rules count as admins; tidy a docstring and test constants.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Adds FOCA-backed, request-level admin mode: admins must opt in with X-Request-Roles: admin for admin permissions, while /me, configuration, duplicate checks, API schemas, documentation, and tests consistently expose and enforce the new semantics, with an opt-out preserving legacy behavior.

Sequence diagram for request-level admin mode

sequenceDiagram
    actor Admin
    participant API
    participant FOCA
    Admin->>API: Request with Authorization
    API->>FOCA: Evaluate request roles
    alt X-Request-Roles: admin
        FOCA-->>API: Admin permissions
        API-->>Admin: Admin operation succeeds
    else Header omitted
        FOCA-->>API: Regular-user permissions
        API-->>Admin: Admin operation denied or hidden
    end
Loading

Sequence diagram for GET /me admin status

sequenceDiagram
    actor Client
    participant API
    participant FOCA
    Client->>API: GET /me
    API->>FOCA: is_admin()
    API->>FOCA: get_available_opt_in_roles()
    FOCA-->>API: Request admin status and availability
    API-->>Client: admin and admin_available
Loading

Sequence diagram for duplicate service visibility in admin mode

sequenceDiagram
    participant API
    participant Permissions
    participant FOCA
    API->>Permissions: may_read_service(serviceId)
    Permissions->>Permissions: Create synthetic GET request
    Permissions->>FOCA: Evaluate with X-Request-Roles
    alt Admin mode header present
        FOCA-->>Permissions: Service is readable
        Permissions-->>API: Registered duplicate visible
    else Header omitted
        FOCA-->>Permissions: Service is not readable
        Permissions-->>API: Hidden duplicate conflict
    end
Loading

File-Level Changes

Change Details Files
Introduces opt-in admin mode, making admin privileges apply only when requests explicitly request the admin role.
  • Enables admin as the default opt-in role and supports disabling the behavior with opt_in_roles: [].
  • Adds admin and admin_available semantics to GET /me and exposes them in the OpenAPI schema.
  • Propagates opt-in role headers through synthetic service-read checks so duplicate conflict visibility matches the original request.
  • Rejects permissions API rules that grant admin, while preserving documented behavior for legacy stored rules and deployments without admin mode.
cloud_registry/config.yaml
config.dev.yaml
cloud_registry/ga4gh/registry/permissions.py
cloud_registry/api/additions.openapi.yaml
Updates client, deployment, and operational documentation for the new request-level admin activation model.
  • Documents the X-Request-Roles: admin header, cache Vary behavior, CORS requirements, and the opt-out configuration.
  • Updates API examples, upgrade guidance, chart notes, and administrator instructions.
  • Clarifies that users must be placed in admin-mapped groups rather than granted admin through the permissions API.
docs/access-control.md
docs/api.md
docs/operations.md
docs/upgrading.md
deployment/README.md
Expands test fixtures and coverage around active versus available admin privileges and compatibility behavior.
  • Makes request helpers configurable for admin mode, with existing tests defaulting to the admin header.
  • Tests admin and non-admin behavior across operations, /me, permissions API access, response Vary, and opt-out deployments.
  • Verifies hidden duplicate 409 responses for admins without the header and confirms the header does not grant privileges to regular users.
tests/conftest.py
tests/test_access_control.py

Assessment against linked issues

Issue Objective Addressed Explanation
#240 Enable opt-in admin roles by default so admin permissions apply only to requests that explicitly request the admin role, while other group roles continue to apply normally. ✅
#240 Make GET /me report whether the current request is in admin mode and whether the caller can switch to admin mode. ✅
#240 Document how API clients and scripts opt in to admin mode. ✅

Possibly linked issues

  • feat: admin mode #240: The PR implements the issue’s requested admin opt-in configuration, request-scoped permissions, GET /me fields, and client documentation.

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 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (6e1d43c) to head (8349ba3).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #263   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           19        19           
  Lines         1862      1866    +4     
=========================================
+ Hits          1862      1866    +4     

☔ 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.

# Conflicts:
#	Dockerfile
#	cloud_registry/config.yaml
#	cloud_registry/ga4gh/registry/permissions.py
#	config.dev.yaml
#	deployment/README.md
#	docs/upgrading.md
#	pyproject.toml
#	tests/test_image_command.py
@uniqueg
uniqueg marked this pull request as ready for review October 27, 2026 10:06

@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 found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="cloud_registry/config.yaml" line_range="112-113" />
<code_context>
+        # groups can then be admins: configured rules granting `admin` are
+        # refused on startup. `[]` lets admins act as admins on every
+        # request; see "Admin mode" in `docs/access-control.md`
+        opt_in_roles:
+            - admin
         # Managed on startup (removed rules are deleted); cannot be changed
         # via the permissions API. Named, so that overlays can change, add or
</code_context>
<issue_to_address>
**Legacy grants bypass admin mode**

When old and new replicas overlap during a rolling upgrade and an administrator grants `admin` through the old permissions API, the old permissions API stores an `admin` grant while old and new replicas share the database; new replicas then load that rule, which still grants admin rights without the opt-in header, so the user retains ambient admin access after the rollout.

Drain old replicas before the final audit and revocation of stored admin grants, or otherwise prevent legacy replicas from writing admin grants during the upgrade.
</issue_to_address>

### Comment 2
<location path="cloud_registry/config.yaml" line_range="113" />
<code_context>
       required:
         - user_id
         - admin
+        - admin_available
         - may_add_service_types
</code_context>
<issue_to_address>
**Old replicas bypass admin mode**

When an upgrade rolls out new replicas while old replicas still serve requests, requests routed to old replicas are authorized under the previous always-admin behavior, while new replicas require the opt-in header. Admin operations without the header therefore succeed or fail depending on which replica handles them, so the new mode is not consistently enforced during a rolling upgrade.

Document or enforce a rollout that stops old replicas serving traffic before relying on admin mode being enforced.
</issue_to_address>

### Comment 3
<location path="cloud_registry/config.yaml" line_range="112-113" />
<code_context>
+        # groups can then be admins: configured rules granting `admin` are
+        # refused on startup. `[]` lets admins act as admins on every
+        # request; see "Admin mode" in `docs/access-control.md`
+        opt_in_roles:
+            - admin
         # Managed on startup (removed rules are deleted); cannot be changed
         # via the permissions API. Named, so that overlays can change, add or
</code_context>
<issue_to_address>
**Chart accepts rejected admin policies**

When a deployment sets `require_admin: false` and grants `admin` in `security.access_control.policies`, the Helm chart still documents that `require_admin: false` allows policies to grant `admin`, and its validation does not reject that configuration. FOCA now refuses configured policies granting `admin`, so deployments following the chart guidance render successfully but fail at app startup.

Update the chart documentation and validation so configurations with configured `admin` policies are rejected or replaced with group mappings before deployment.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 3 findings to address first, and the default now grants admin permissions only on requests carrying X-Request-Roles: admin, changing the authorization boundary for every deployment using the default config. If that policy is wrong, admin actions such as changing permissions or deleting services can still succeed when opted in, and those effects are not undone by reverting the change.

Blocking findings: cloud_registry/config.yaml:113, cloud_registry/config.yaml:113, cloud_registry/config.yaml:113


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

Comment thread cloud_registry/config.yaml Resolved
Comment thread cloud_registry/config.yaml Resolved
Comment thread cloud_registry/config.yaml Resolved
@uniqueg
uniqueg merged commit 6b907fa into dev Oct 27, 2026
19 checks passed
@uniqueg
uniqueg deleted the feat/admin-mode branch October 27, 2026 10:25
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.

feat: admin mode

1 participant