Karyo
Documentation/ Decision records

ADR 0039: Scope is declared per endpoint, and raw ambient scope is a registered opt-in

Karyo 2.1.1rendered at build time
On this page

Status: Accepted. Refines the request-scope mechanics of ADR 0038; it changes which scope a request resolves, not what the scope rules are. ADR 0050 removes the legacy Business Unit accessors and scope API; Facility scope helpers replace them.

Context

ADR 0038 made operational scope a pair of grant-backed sets, read and write, narrowed by a selected Business Unit. The rule is shared: more than 90 production files read it through TenantContext (operationalBusinessUnitId, readScope(), writeScope(), clientId, principalKind). Changing the rule therefore changes every consumer at once, including those in modules the change never opened.

The review of the change that introduced grant scope found such consumers one review round at a time. The full test suite was green on the half-migrated state each time. One of the defects came from inference: the filter chose write authority for every POST, PUT, PATCH and DELETE, so a POST that only previews a wave rule resolved against write authority. Another came from silence: a context nothing had set answered owner 0 instead of failing.

The model Karyo is moving to has three tiers: Company, Business Unit and Facility. Each later step of that move changes the shared rule again.

Decision

  • Every JAX-RS method declares its scope kind with @DeclaredScope(ScopeKind.X). The kinds are OPERATIONAL_READ, OPERATIONAL_WRITE, ENTITY_OWNED, CONFIGURATION, ADMINISTRATION, IDENTITY, PUBLIC and SCHEDULED. The two operational kinds are declared on the method; a non-operational kind may be declared once on the resource class as the default for its methods. TenantFilter sets operationalWrite only for OPERATIONAL_WRITE, and refuses a Karyo method that declares none (a 500 problem, scope-kind-undeclared), including for an anonymous caller. The HTTP verb is no longer read.
  • The raw ambient scope is an opt-in that the compiler enforces. clientId, principalKind, readScope(), writeScope(), affiliationScope(), operationalBusinessUnitId(s), affiliationOrOperationalBusinessUnitId, requireBodyBusinessUnit and the raw selection and grant fields carry @RawScopeAccess, a Kotlin @RequiresOptIn(level = ERROR) marker. A production file that uses one says @file:OptIn(com.karyo.security.RawScopeAccess::class) and is listed, with the accessors it uses, in config/scope-consumers.json. Test sources opt in through the compiler options of the shared Kotlin convention, because fixtures arrange raw scope on purpose.
  • A context nothing has set throws. TenantContext.clientId and principalKind raise UnprimedTenantContextException until the filter, ownerScoped or a fixture sets one of them. actorSnapshot() still works unprimed, because it names an actor and grants no scope.
  • The final model has types. CompanyId, BusinessUnitId and FacilityId are positive-only value classes, and FacilityScope and the typed TenantScope factories exist. Nothing resolves a Facility grant yet, so no request populates a FacilityScope.
  • Registered facts are tests, not conventions. config/scope-consumers.json also lists every background job and the scope it runs under, the classification of every JPA entity that is not a TenantEntity, the count of client_id predicates in query strings per file, and the SPI port methods that take no scope parameter. The guard tests under services/karyo-app/src/test/kotlin/com/karyo/security/guards fail on a new item, on a listed item that no longer exists, and on a count that grew. The console and floor app have source tests in the same spirit (scope-purity.test.ts). Later amended: the two source-scan guards for the raw-scope file list and the per-file predicate count were deleted; review keeps those two lists, and the isolation tests prove the behaviour (identity and tenancy).

Consequences

  • A new REST method that declares no kind fails RestScopeDeclarationTest and is refused at runtime with a 500, and a new consumer of the raw scope does not compile until it opts in and a reviewer has accepted the registry line. A missed consumer fails before review, not during it.
  • The registry only shrinks. The lists of opt-in files, owner predicates and unscoped port methods are the inventory that later steps work through; each step removes lines.
  • Behaviour is as before, with two intended exceptions. An ambient scope read on an unprimed context now throws where it used to answer owner 0. Write authority now follows the declared kind: every method that was a mutating verb in an operational resource declares OPERATIONAL_WRITE, and the four methods that carried @ReadBusinessUnitContext (now removed) declare OPERATIONAL_READ. Resources of the other kinds (administration, configuration, identity, public, entity-owned) no longer set operationalWrite; a search of the code behind them found no read of operationalBusinessUnitId.
  • The guards check that a declaration exists, not that it is right. A method declared OPERATIONAL_WRITE that should be entity-owned compiles and passes. Review and behavioural tests remain the check for the choice of kind.
  • Request-body references to another unit's rows, SQL views and migrations are outside the scans.

Alternatives considered

  • Keep inferring authority from the HTTP verb. Rejected: it resolved a read-only POST against write authority, and a new read-by-POST endpoint would do the same.
  • Opt in per declaration instead of per file. Rejected: a file-level annotation lets the registry test find every site with one pattern and keeps the change to one line per file. The cost is that a second use in an already-registered file passes the compiler; the registry's uses list is the check for that.
  • Opt in for a whole module in its build file. Rejected: it hides every use. The registry test fails if a module build file names the marker.
  • Leave an unprimed context answering owner 0. Rejected: the answer looks like a valid tenant and was documented as a trap in several places.
  • Declare kinds with one class-level annotation per resource. Rejected for the operational kinds, because a class mixes reads and writes; allowed for non-operational kinds, where a class has one kind.

Evidence

  • libs/karyo-security/src/main/kotlin/com/karyo/security/ScopeKind.kt - the kinds, @DeclaredScope and the lookup that refuses an operational class-level default
  • libs/karyo-security/src/main/kotlin/com/karyo/security/RawScopeAccess.kt - the opt-in marker
  • libs/karyo-security/src/main/kotlin/com/karyo/security/TenantContext.kt - priming and the raw fields; OrganizationIds.kt and FacilityScope.kt - the typed ids and scope
  • services/karyo-app/src/main/kotlin/com/karyo/app/auth/TenantFilter.kt - declaredScopeKind and populateOrganizationScope
  • config/scope-consumers.json - the registry
  • services/karyo-app/src/test/kotlin/com/karyo/security/guards - guards G1 to G8
  • frontend/web/src/test/scope-purity.test.ts and frontend/mobile/src/lib/scope-purity.test.ts - guard G9

ADR 0038, ADR 0014, Identity and tenancy.