Repository navigation
feat: register and control service types - #241
Conversation
Service types are stored in their own collection (service_types), unique as written, with the user who added them (created_by, not served). GET /services/types lists the stored types. POST /services/types adds one; DELETE /services/types (type named by query parameters, as Connexion passes no DELETE bodies) removes one, for admins only in every mode, and is refused with 409 while services have the type. Registering or updating a service stores its type. The new option custom.access_control.service_types sets who may add types: open (the default, the previous behaviour) lets any authenticated user add them; controlled lets only admins add them, and services of other users must have a stored type (400, naming it). GET /me tells clients whether they may add types (may_add_service_types). On startup, the app stores the types of registered services (idempotent), so nothing disappears on upgrade. The smoke test stores the type of the service it inserts. Closes #235 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Store types re-added by registrations without the user if they may not add types, simplify the removal of types, and document that overlays must keep the add-service-types rule and that rolling updates need a restart to store all types. Test the mode setting of service types. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Test that types not stored are not found, and that types that services have can't be removed, also if they aren't stored; tidy docstrings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThis PR introduces a persistent, independently managed service-type registry, integrates type storage and validation into service registration and updates, adds open versus admin-controlled access modes with Sequence diagram for controlled service registrationsequenceDiagram
actor User
participant API
participant Access as AccessControl
participant Types as ServiceTypeRegistry
participant Services as ServicesCollection
User->>API: POST /services
API->>Access: may_add_service_types()
Access-->>API: false
API->>Types: check_service_type(type)
Types->>Types: service_type_exists(type)
alt type not registered
Types-->>API: UnregisteredServiceType
API-->>User: 400 Bad Request
else type registered
Types-->>API: allowed
API->>Services: register service
API->>Types: store_service_type(type)
API-->>User: 201 Created
end
Sequence diagram for protected service-type removalsequenceDiagram
actor Admin
participant API
participant Types as ServiceTypeRegistry
participant Services as ServicesCollection
Admin->>API: DELETE /services/types?group=...&artifact=...&version=...
API->>Types: _type_in_use(type)
alt service uses type
Types->>Services: count matching services
Services-->>Types: in use
Types-->>API: Conflict
API-->>Admin: 409 Conflict
else type unused
Types->>Types: find_one_and_delete(type)
Types->>Services: _type_in_use(type)
alt registration raced with removal
Types->>Types: store_service_type(type)
Types-->>API: Conflict
API-->>Admin: 409 Conflict
else no service uses type
API-->>Admin: removed type
end
end
Entity relationship diagram for service typeserDiagram
SERVICES {
string service_id
object type
}
SERVICE_TYPES {
string group
string artifact
string version
string created_by
}
SERVICES }o--|| SERVICE_TYPES : uses
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 #241 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 16 17 +1
Lines 1375 1480 +105
==========================================
+ Hits 1375 1480 +105 ☔ 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="cloud_registry/ga4gh/registry/server.py" line_range="261-264" />
<code_context>
return problem(exc.hidden())
+ # also if the type was removed concurrently, as a service has it now;
+ # without the user if they may not add types (only then)
+ service_type = type_document(service.data["type"])
+ if service_type is not None:
+ created_by = get_current_subject() if may_add_service_types() else None
+ store_service_type(service_type, created_by=created_by)
if service.verification_removed:
logger.info(
</code_context>
<issue_to_address>
**Failed writes leave partial service state**
When the service write succeeds but the subsequent service-type write or restore fails, `store_service_type` propagates the database error after `register_metadata` commits the service, so `_register_service` returns an error before type and owner setup completes. The service remains persisted without the expected type or owner assignment, and callers see a failed request despite the persisted change.
Make service persistence, service-type storage or restoration, and owner setup atomic, or roll back or complete the changes before returning an error.
Also at `cloud_registry/ga4gh/registry/service_types.py:321`.
</issue_to_address>
### Comment 2
<location path="cloud_registry/ga4gh/registry/service_types.py" line_range="314-322" />
<code_context>
+ cloud_registry.exceptions.NotFound: The type is not stored.
+ """
+ document = {"group": group, "artifact": artifact, "version": version}
+ if _type_in_use(document):
+ return problem(Conflict(IN_USE))
+ removed = get_collection("service_types").find_one_and_delete(dict(document))
+ if removed is None:
</code_context>
<issue_to_address>
**Deleted types remain in use**
When a registration passes its type check, then writes its service after `deleteServiceType`'s second usage check, `deleteServiceType` finishes its usage check and returns success before the registration writes its service and `store_service_type` recreates the type, so the caller sees a successful removal even though the type remains in use.
Coordinate registration and deletion so the type cannot be recreated after the deletion check and before registration completes.
Also at `cloud_registry/ga4gh/registry/server.py:235-264`.
</issue_to_address>
### Comment 3
<location path="cloud_registry/ga4gh/registry/service_types.py" line_range="139" />
<code_context>
+ written).
+ """
+ query = {f"type.{field}": value for field, value in service_type.items()}
+ return get_collection("services").count_documents(query, limit=1) > 0
+
+
</code_context>
<issue_to_address>
**Type removal scans all services**
When the registry has many services and an admin removes a type, `_type_in_use` filters on the three nested `type` fields, but the services collection has no index on them. MongoDB therefore scans the services collection for each removal check, making the admin delete request increasingly slow as the registry grows.
Add a compound index on `type.group`, `type.artifact`, and `type.version` to the services collection.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 3 findings to address first, and a registration or explicit type addition writes a persistent, publicly listed service-type record; reverting the code does not remove those records. They are generally repairable by an admin, though types used by services require those services to be changed or deleted first.
Blocking findings: cloud_registry/ga4gh/registry/server.py:264, cloud_registry/ga4gh/registry/service_types.py:322, cloud_registry/ga4gh/registry/service_types.py:139
Removals of service types check whether a service has the type; index the type fields of services so that this doesn't scan all services. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Service types get their own collection (
service_types, unique ongroup,artifactandversionas written), and deployments configure who may add them.GET /services/typeslists the stored types, sorted.POST /services/typesadds a type (201;409if it is registered). The user who added it is stored with it (created_by), but not served.DELETE /services/types?group=...&artifact=...&version=...removes a type: admins only, in every mode (no policy for other users). It is refused with409("Service type is in use. If you really want to revoke it, edit or delete all services that depend on it.") while a service has the type, also if a service gets it during the removal (then the type is stored again).custom.access_control.service_types:open(default, the previous behaviour): any authenticated user may add types.controlled: only admins may add types; services of other users must have a registered type (400, naming it).GET /mereturnsmay_add_service_types, so that UIs can offer adding types.api.md(Service types),configuration.md(Service types),access-control.md(Current user),upgrading.md(Service types: restart after a rolling update; list-style policy overlays must addadd-service-types).POST /services/typesneeds a login.Closes #235
Decisions
DELETEhandlers, and RFC 9110 givesDELETEbodies no defined meaning.GET /services/types.created_byis not served: it is internal (e.g., for audits); types added on startup have none. A registration in progress while an admin removes its type stores the type again; it is then stored without the user if they may not add types (documented in the module).openkeeps the previous behaviour; requests that worked before still work.Verification
pre-commit run --all-files: all passed.pytest(mongomock): 1180 passed, 228 skipped (Helm chart tests; Helm isn't installed locally), 100% coverage.pytestagainst MongoDB 7.0.34: all passed.mkdocs build --strict: passed.Declined review findings
add_service_keys.may_add_service_typesfrom the casbin policy (adversarial, low): the mode option is the supported control; the docs and the config comment say to keep the policy.404as a problem response instead of raisingNotFound(quality, low): raisingNotFoundis the repo's convention (e.g.,verification.py).IN_USE; frozenset inadd_service_types(quality, low): readability only.upgrading.md(quality): kept, as this targets 0.5.0.Open questions
None.
🤖 Generated with Claude Code
Summary by Sourcery
Introduce persistent service-type management with configurable registration access and safeguards for services that depend on registered types.
New Features:
may_add_service_typesinGET /me.Bug Fixes:
Enhancements:
CI:
Documentation:
Tests: