Karyo
Documentation/ Decision records

ADR 0034: Karyo authenticates to Loci with a client-credentials token

Karyo 2.1.1rendered at build time
On this page

Status: Accepted. Supersedes in part ADR 0031 (the advice call sent with no credentials).

Context

Loci validates bearer tokens from any standard OIDC provider. On Atlas that provider is Authentik. Karyo's server-side calls to Loci must therefore carry a machine token from the same provider, obtained with the OAuth client-credentials grant. ADR 0037 changes inbound Karyo identity independently; outbound Loci credentials are never rebound to a Karyo principal.

Two constraints bind the change:

  • An unset token URL means Karyo sends no credential, which only an unauthenticated Loci on a private network accepts. A library client that requires a URL would fail boot when that URL is unset, which the SRCFG00040 rule forbids.
  • The token is fetched server-side. A goods-owner administrator must not be able to redirect Karyo's credential, so the token settings are environment-only and never catalogue keys.

Decision

  • The grant is standard client_credentials with client_secret_post. LociTokenSource posts grant_type, client_id and client_secret as a form, and scope only when one is set. Nothing in it is specific to a provider. For Authentik the secret is base64(<service account>:<app password>) and the scope is profile; for a confidential client the secret is that client's own and the scope stays empty.
  • The token is cached until 30 seconds before it expires. The client pins HTTP/1.1 and follows no redirects, as the advice call does. LociClient sends Authorization: Bearer when a token is supplied and omits the header when the token URL is unset.
  • A rejected token is retried once. SlottingService invalidates the cache on a 401 and calls Loci exactly once more. A second 401, a 403, or any other failure is returned as it is. Both 401 and 403 are 502 slotting-engine-unavailable. The messages name the variables or the loci-recommend role, and never the URL, the client id or the secret.
  • Boot and deploy refuse an inert or weak credential. A token URL requires a non-blank client id and secret and must be http(s) with a host. A client id, secret or scope without a token URL is refused. The deploy validator also requires the secret to meet the 32-character credential policy and to be distinct from OIDC_SECRET, KEYCLOAK_ADMIN_CLIENT_SECRET, DB_PASSWORD and the bootstrap password. All four unset is valid.

Consequences

  • A Loci that requires authentication, given wrong variables, is a 502 whose detail names KARYO_LOCI_TOKEN_URL, KARYO_LOCI_CLIENT_ID and KARYO_LOCI_CLIENT_SECRET, shown on the Slotting page without the URL. A token that lacks loci-recommend names that role.
  • An installation that leaves the four variables unset behaves as before: no credential is sent.
  • The token URL, client id, secret and scope are not runtime catalogue keys. Writing them on the system-properties screen does nothing; changing them requires a restart. The base URL remains the one Loci catalogue key.
  • The bearer travels to Loci over whatever network the base URL names. Where that hop is not TLS, the token is still short-lived and scoped to Loci's audience. A TLS hop is an infrastructure choice, not this decision.

Alternatives considered

  • A Quarkus OIDC client, as Karyo uses for its own Keycloak admin client. Rejected: that client fails boot when its URL is unset, which the SRCFG00040 rule forbids, and LociClient already pins HTTP/1.1 on the JDK client.
  • Two Authentik-specific variables instead of one secret. Rejected: Authentik documents base64(<user>:<app password>) as the standard-form client secret, so one client id and one secret work for any provider.
  • A catalogue key for the token URL. Rejected: the fetch is server-side, and a goods-owner administrator must not be able to point Karyo's credential at another endpoint.

Evidence

  • services/slotting-service/karyo-slotting-core/src/main/kotlin/com/karyo/slotting/loci/LociTokenSource.kt, LociTokenSource.bearer
    • LociTokenSource.bearer; LociTokenSource.invalidate; LociTokenSource.REFRESH_MARGIN_SECONDS, the 30-second margin
  • services/slotting-service/karyo-slotting-core/src/main/kotlin/com/karyo/slotting/loci/LociClient.kt, LociClient.recommend
    • LociClient.recommend, the bearer header; LociClient.statusFailure, the 401 message
  • services/slotting-service/karyo-slotting-core/src/main/kotlin/com/karyo/slotting/service/SlottingService.kt, SlottingService.recommend
    • the one retry on 401
  • services/slotting-service/karyo-slotting-core/src/main/kotlin/com/karyo/slotting/exception/SlottingException.kt, SlottingException
    • EngineUnavailable.status
  • services/slotting-service/karyo-slotting-core/src/main/kotlin/com/karyo/slotting/service/SlottingConfig.kt, SlottingConfig.lociTokenScope
    • the four keys and the boot rules
  • services/karyo-app/src/main/resources/application.yaml, karyo.slotting.loci - the environment mapping, empty when unset
  • services/auth-service/karyo-auth-core/src/main/kotlin/com/karyo/auth/config/SystemPropertyCatalog.kt, SystemPropertyCatalog
    • the base URL remains the only Loci catalogue key
  • scripts/deploy-server.sh, validate_env (KARYO_LOCI_* checks) - the deploy rules; scripts/.env.prod.example, KARYO_LOCI_TOKEN_URL, KARYO_LOCI_CLIENT_ID, KARYO_LOCI_CLIENT_SECRET, KARYO_LOCI_TOKEN_SCOPE