Skip to content

feat: approve service types in moderated mode - #244

Merged
uniqueg merged 4 commits into
devfrom
feat/service-type-approval
Oct 25, 2026
Merged

uniqueg merged 4 commits into
devfrom
feat/service-type-approval

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Summary

This adds a third mode for custom.access_control.service_types: moderated. It follows the verification pattern.

  • Pending types:
    • Any authenticated user can add types, explicitly or through a service with a new type. Types added by users other than admins are stored as pending, together with their creator.
    • Admins' types are approved at once.
    • A pending type is stored before the service that adds it, so the service is never served as approved, not even for a moment.
  • Pending services: a service whose type is pending is pending too. This isn't stored with the service but derived from its type, so approving the type also approves its services.
  • Visibility:
    • GET /services, GET /services/{serviceId} (404), GET /services/types and the verification reads hide pending items from everyone except admins and their creators. For services, creators means users with a role on them, also through groups.
    • These reads are public by default and have no user. So they show pending items to nobody unless login for reads is configured.
    • Duplicate 409s don't name a pending service to users who can't see it.
  • New endpoints:
    • GET /approvals (login required) lists the pending service_types and services the caller may see.
    • PUT /services/types/approval (admins only, no policy for others) approves a type. Approving an approved type is a no-op. There is no unapprove; admins remove types instead.
  • While pending:
    • Owners and maintainers can't update a pending service (409). The one exception is an update to an approved type, so that a change of type can be undone.
    • Owners can delete pending services.
    • Only admins can remove types, also pending ones.
  • GET /me: reports service_types_pending.
  • Removal race: DELETE /services/types restores a removed type exactly as it was, also if a registration stored it as pending in the meantime.
  • Docs:
    • api.md: new "Pending approvals" section; endpoint table.
    • configuration.md, access-control.md, upgrading.md: list-style policy overlays must add list-approvals.
    • OpenAPI descriptions.
  • CI smoke test: GET /approvals without login returns 401.

Closes #238

Decisions

  • Pending state of services is derived, not stored. Approval has no second write to keep in sync, and services become visible as soon as their type is approved.
  • Public reads hide pending items from everyone. FOCA authenticates only operations that require login, so these reads have no user. Creators see their pending items in GET /approvals, or in the reads when the deployment requires login for reads. No FOCA change is needed.
  • PUT /services/types/approval takes the type in the body, like POST /services/types. GET /approvals follows GET /verifications.
  • Admins using a pending type don't approve it; approval is explicit. This is documented.
  • Not breaking: the default mode stays open.

Verification

  • pre-commit run --all-files: all passed.
  • pytest (mongomock): 1221 passed, 228 skipped (the Helm chart tests; Helm isn't installed locally). Coverage is 100%.
  • pytest against MongoDB 7.0.34: all passed.
  • mkdocs build --strict: passed.
  • Review: two rounds with the adversarial and quality reviewers. Findings were fixed except those listed below.
  • Sourcery: 5 threads. One fixed in 45e855c (400 instead of 500 for lone surrogates in updates of pending services); one documented (switch to moderated only once all instances support it); three narrow races declined, with reasons given in the threads.
  • CI: all checks passed on 45e855c.

Declined review findings

  • Rolling back a pending type when its registration fails (quality, medium): the type is stored first so that a service is never served as approved. A leftover pending type gives users nothing that POST /services/types doesn't, and admins remove it like other unwanted types. This is documented.
  • Short-circuit or cap for the pending-types query on reads (quality): one query on the small service_types collection; the services type fields are indexed (feat: register and control service types #241).
  • Making the pending check part of the update's write (quality, medium): an approval during the update makes the update legal anyway. It is documented as best effort.
  • Splitting test_moderated_services; moving getApprovals to another module; a separate field to tell the two 409s apart (quality, low): the tests read as one scenario, the module docstring covers the endpoints, and detail differs.

Open questions

None.

🤖 Generated with Claude Code

uniqueg and others added 3 commits October 25, 2026 23:11
New mode moderated for custom.access_control.service_types: any
authenticated user may add types, but the types of users other than
admins are stored as pending until an admin approves them (PUT
/services/types/approval); admins' types are approved at once. There is
no way back; admins remove types instead.

Services with a pending type are pending too (derived from the type, so
approving the type approves them). Pending types and services are only
visible to admins and their creators (for services: users with a role
on them): GET /services, GET /services/{serviceId} and GET
/services/types hide them from others, and GET /approvals lists those
the user may see. Only admins may update pending services (409); owners
may delete them. GET /me tells whether the user's new types are pending
(service_types_pending).

Closes #238

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Hide verifications of pending services like the services. Store new
types as pending before the service, so that it is never served as
approved. Let users update pending services to an approved type, e.g.,
to undo a change of the type. Restore removed types as they were, also
if a registration stored them as pending meanwhile. Add tests and docs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Say in the conflict detail, docstrings, API description and docs that
pending services can be updated to an approved type, that pending types
are stored first, and that admins don't approve types by using them.

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

sourcery-ai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Introduces a moderated service-type mode in which non-admin registrations create pending types and derived pending services, adds authenticated approval workflows, consistently filters pending resources by caller visibility, and updates permissions, documentation, API definitions, CI, and tests.

Sequence diagram for moderated service registration and approval

sequenceDiagram
    actor User
    actor Admin
    participant API
    participant Types
    participant Services

    User->>API: POST /services
    API->>Types: store_pending_type
    Types-->>API: pending type stored
    API->>Services: register service
    Services-->>API: service stored
    API-->>User: 201 Created

    User->>API: GET /approvals
    API->>Types: getApprovals
    Types-->>API: pending types and visible services
    API-->>User: 200 Approvals

    Admin->>API: PUT /services/types/approval
    API->>Types: putServiceTypeApproval
    Types-->>API: remove pending marker
    API-->>Admin: 200 Approved type

    User->>API: GET /services
    API->>Types: visible_services
    Types-->>API: approved services including newly approved type
    API-->>User: 200 Services
Loading

Sequence diagram for pending-resource visibility and update protection

sequenceDiagram
    actor User
    participant API
    participant Registry

    User->>API: GET /services/{serviceId}
    API->>Registry: visible_services
    alt user has a role on pending service
        Registry-->>API: service visible
        API-->>User: 200 Service
    else public or unauthorized read
        Registry-->>API: service filtered
        API-->>User: 404 Not Found
    end

    User->>API: PUT /services/{serviceId}
    API->>Registry: _is_pending
    alt pending service and new type is not approved
        Registry-->>API: PendingService
        API-->>User: 409 Conflict
    else approved type
        Registry-->>API: update service
        API-->>User: 200 Service
    end
Loading

File-Level Changes

Change Details Files
Add moderated service-type registration with durable pending state and administrator approval.
  • Accept moderated as a service-type access-control mode.
  • Record non-admin-created types as pending with their creator, while admin-created types are immediately approved.
  • Store a pending type before registering a service that introduces it and preserve pending state during removal races.
  • Add approval and pending-state helpers, including idempotent admin approval and derived pending-service detection.
cloud_registry/service_models/custom_config.py
cloud_registry/ga4gh/registry/permissions.py
cloud_registry/ga4gh/registry/service_types.py
cloud_registry/ga4gh/registry/server.py
Expose approval workflows and enforce pending-item permissions across registry operations.
  • Add authenticated GET /approvals and admin-only PUT /services/types/approval endpoints.
  • Expose service_types_pending through GET /me.
  • Prevent non-admin updates to pending services except changes back to an approved type; retain owner deletion and admin type removal.
  • Hide unauthorized pending services, types, duplicate details, and verification records from reads; allow visibility to admins and users with applicable ownership or roles, including groups.
cloud_registry/ga4gh/registry/service_types.py
cloud_registry/ga4gh/registry/server.py
cloud_registry/ga4gh/registry/verification.py
cloud_registry/ga4gh/registry/permissions.py
cloud_registry/api/additions.openapi.yaml
Document and validate the moderated access-control behavior across configuration, API contracts, CI, and tests.
  • Update default policy overlays and upgrade guidance with list-approvals.
  • Document configuration, visibility, approval, lifecycle, and concurrency semantics.
  • Extend OpenAPI schemas and descriptions for approvals and pending fields.
  • Add CI authentication smoke coverage and comprehensive moderated-mode tests.
cloud_registry/config.yaml
cloud_registry/api/additions.openapi.yaml
docs/api.md
docs/access-control.md
docs/configuration.md
docs/upgrading.md
.github/workflows/ci.yml
tests/ga4gh/registry/test_service_types.py
tests/service_models/test_custom_config.py
tests/test_access_control.py
tests/test_service_types.py

Assessment against linked issues

Issue Objective Addressed Explanation
#238 Add moderated service-type registration: non-admin-created types remain pending, admin-created types are approved immediately, and admins can approve pending types. ✅
#238 Keep pending types and services hidden from unauthorized users, expose visible pending items through an authenticated approvals listing, and report whether the caller's new types will be pending. ✅
#238 While a service is pending, prevent its owners from editing it while still allowing them to delete it. ❌ The PR blocks updates to pending services except when the update changes the service to an approved type. The issue says owners cannot edit pending services, without this exception, so this acceptance criterion is not fully met.

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

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d6bcf73) to head (45e855c).

Additional details and impacted files
@@            Coverage Diff             @@
##               dev      #244    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           17        17            
  Lines         1502      1603   +101     
==========================================
+ Hits          1502      1603   +101     

☔ 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 25, 2026 22:47

@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 5 issues

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

## Individual Comments

### Comment 1
<location path="cloud_registry/ga4gh/registry/permissions.py" line_range="275" />
<code_context>
+        approves them.
+    """
+    mode = get_custom_config().access_control.service_types
+    return mode == "moderated" and not is_admin()


</code_context>
<issue_to_address>
**Services bypass moderated approval**

When a moderated rollout still sends registrations to old workers, old workers skip `service_types_pending()` and store types without the pending flag, so services become visible without admin approval and the types are absent from the approvals list.

Stop sending registrations to old workers until they have been upgraded to apply moderated-mode handling.

Also at `cloud_registry/ga4gh/registry/service_types.py:230`.
</issue_to_address>

### Comment 2
<location path="cloud_registry/ga4gh/registry/service_types.py" line_range="295" />
<code_context>
+    """
+    pending = pending_services()
+    if pending is None:
+        return {}
+    own = resource_filter(services_path())
+    if not own:
</code_context>
<issue_to_address>
**Pending services briefly become public**

When a public read checks for pending types just before a moderated registration stores its pending type, then queries services after that registration inserts the service, `visible_services()` returns an unrestricted filter when its initial `pending_services()` query finds no pending type. A concurrent registration can then store the type as pending and insert its service before the caller queries services, so public service and verification reads return that pending service to unauthenticated callers.

Avoid treating an empty pending-type lookup as a durable all-visible decision across a later service query, for example by rechecking pending types or using a consistency strategy that prevents this interleaving from exposing the new service.
</issue_to_address>

### Comment 3
<location path="cloud_registry/ga4gh/registry/server.py" line_range="273" />
<code_context>
+        return problem(exc)
     try:
         check_service_type(service.data.get("type"))
+        store_pending_type(service.data.get("type"))
         service.register_metadata()
     except InvalidService as exc:
</code_context>
<issue_to_address>
**Pending services become publicly visible**

When a type-removal request deletes a pending type between registration's pre-insert pending-type write and its post-insert type write, `visible_services()` sees no pending type while registration inserts the service, so public reads expose it; a process stop in that gap also lets startup backfill the type as approved.

Keep the type marked pending throughout registration and removal so it cannot be absent while the service is inserted.

Also at `cloud_registry/ga4gh/registry/server.py:302`.
</issue_to_address>

### Comment 4
<location path="cloud_registry/ga4gh/registry/server.py" line_range="262" />
<code_context>
+        and _is_pending(service_id)
+        # updates to an approved type end the review, e.g., to undo a change
+        # of the type
+        and not is_approved(type_document(service.data.get("type")))
+    ):
+        exc = PendingService()
</code_context>
<issue_to_address>
**Malformed type crashes pending update**

When a non-admin updates a pending service with a type containing an invalid Unicode surrogate, `type_document()` preserves an invalid surrogate in the submitted type, and `is_approved()` queries MongoDB with that value before `check_service_type()` can validate it. PyMongo rejects the query value, so updating a pending service with malformed type strings fails with an internal error instead of the normal `400` response.

Validate the submitted type’s strings before querying whether it is approved, and return the normal invalid-service response for invalid input.
</issue_to_address>

### Comment 5
<location path="cloud_registry/ga4gh/registry/service_types.py" line_range="245" />
<code_context>
+    fields = {CREATED_BY: removed.get(CREATED_BY)}
+    pending = removed.get(PENDING) is True
+    update: dict[str, Any] = (
+        {"$set": {**fields, PENDING: True}}
+        if pending
+        else {"$set": fields, "$unset": {PENDING: ""}}
</code_context>
<issue_to_address>
**Approved types become pending again**

When a pending type is removed, re-added and approved while its delete request is still in progress, and the delete handler then finds a service, `_restore_type()` overwrites the concurrently approved row with the removed snapshot, undoing the approval; the delete returns `409` and services remain hidden until an admin approves the type again.

Restore the removed snapshot only when the current row has not been approved since it was recreated.

Also at `cloud_registry/ga4gh/registry/service_types.py:249-250`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 5 findings to address first, and if the pending-record visibility filter or admin-only approval check is wrong, users could see service metadata and verification details that were meant to stay restricted, or approve types without authorization. Reverting the code would not retract information already exposed or undo approvals that already changed stored state.

Blocking findings: cloud_registry/ga4gh/registry/permissions.py:275, cloud_registry/ga4gh/registry/service_types.py:295, cloud_registry/ga4gh/registry/server.py:273, cloud_registry/ga4gh/registry/server.py:262, cloud_registry/ga4gh/registry/service_types.py:245


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

Comment thread cloud_registry/ga4gh/registry/server.py Resolved
Comment thread cloud_registry/ga4gh/registry/server.py Resolved
Updates of pending services checked whether their type is approved
before its strings were validated, so strings that MongoDB can't store
(e.g., lone surrogates) failed with 500; they now get 400. Note that
deployments switch to moderated only once all instances support it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit de519d6 into dev Oct 25, 2026
19 checks passed
@uniqueg
uniqueg deleted the feat/service-type-approval branch October 25, 2026 23:01
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: approve new service types

1 participant