ADR 0039: Scope is declared per endpoint, and raw ambient scope is a registered opt-in
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 areOPERATIONAL_READ,OPERATIONAL_WRITE,ENTITY_OWNED,CONFIGURATION,ADMINISTRATION,IDENTITY,PUBLICandSCHEDULED. 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.TenantFiltersetsoperationalWriteonly forOPERATIONAL_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,requireBodyBusinessUnitand 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, inconfig/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.clientIdandprincipalKindraiseUnprimedTenantContextExceptionuntil the filter,ownerScopedor 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,BusinessUnitIdandFacilityIdare positive-only value classes, andFacilityScopeand the typedTenantScopefactories exist. Nothing resolves a Facility grant yet, so no request populates aFacilityScope. - Registered facts are tests, not conventions.
config/scope-consumers.jsonalso lists every background job and the scope it runs under, the classification of every JPA entity that is not aTenantEntity, the count ofclient_idpredicates in query strings per file, and the SPI port methods that take no scope parameter. The guard tests underservices/karyo-app/src/test/kotlin/com/karyo/security/guardsfail 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
RestScopeDeclarationTestand 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) declareOPERATIONAL_READ. Resources of the other kinds (administration, configuration, identity, public, entity-owned) no longer setoperationalWrite; a search of the code behind them found no read ofoperationalBusinessUnitId. - The guards check that a declaration exists, not that it is right. A method declared
OPERATIONAL_WRITEthat 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
POSTagainst write authority, and a new read-by-POSTendpoint 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
useslist 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,@DeclaredScopeand the lookup that refuses an operational class-level defaultlibs/karyo-security/src/main/kotlin/com/karyo/security/RawScopeAccess.kt- the opt-in markerlibs/karyo-security/src/main/kotlin/com/karyo/security/TenantContext.kt- priming and the raw fields;OrganizationIds.ktandFacilityScope.kt- the typed ids and scopeservices/karyo-app/src/main/kotlin/com/karyo/app/auth/TenantFilter.kt-declaredScopeKindandpopulateOrganizationScopeconfig/scope-consumers.json- the registryservices/karyo-app/src/test/kotlin/com/karyo/security/guards- guards G1 to G8frontend/web/src/test/scope-purity.test.tsandfrontend/mobile/src/lib/scope-purity.test.ts- guard G9