Islandr

Self-hosted WireGuard management platform. Peer lifecycle, RBAC access control, nftables enforcement.

26. External API facade: API keys, separate resource surface, hand-written OpenAPI spec

Date: 2026-08-22

Status

Accepted

Context

Every existing /api/v1/... endpoint is authenticated by SessionFilter/Auth against a session cookie (islandr_session) — designed for one specific client (the Vue SPA) that can complete an interactive login. That's the right model for the UI, but it has no path for a script, CI pipeline, or monitoring scraper (e.g. a Prometheus server, Issue #71) that needs to call Islandr without a browser session.

There's a second, independent problem beyond auth: the internal /api/v1 resources are shaped for whatever the SPA currently needs and are free to change as the SPA evolves (a field renamed, a response reshaped, an endpoint split) — that's normal, healthy internal API evolution. An external consumer (a Terraform provider, a shell script in someone's CI, a homegrown dashboard) needs the opposite guarantee: a contract that doesn't shift under them just because the admin console's UI changed. Bolting Bearer-token auth onto the existing internal resources would accidentally promise that stability without ever deciding to.

Decision

A separate, versioned external API facade under /api/external/v1/..., authenticated by API keys (Authorization: Bearer islandr_<random>) instead of session cookies — parallel to, not layered onto, the internal SPA-facing API.

API keys

  • Admin-generated in the Admin Console (new "API Keys" page), each with a label and a creation timestamp.
  • The raw key is shown exactly once, at creation — same one-time-secret posture as a peer's private key or a webhook's HMAC secret (CLAUDE.md's one-time-secret pattern). Only a SHA-256 hash is stored; a short prefix (e.g. the first 8 characters) is kept alongside in plaintext so the admin can tell keys apart in the list without ever re-exposing the secret. SHA-256 (not a slow KDF like PBKDF2) is the right hash here: the key itself is a high-entropy random token generated by Islandr, not a human-chosen password — there's nothing for a fast hash to make brute-forceable that isn't already infeasible from the token's own entropy.
  • Revocable, not deletable — a revoked key's row (and its audit trail) stays; ApiKeyAuthFilter simply stops accepting it. Mirrors how Session revocation already works.
  • v1 scope is deliberately simple: a key acts with the same privileges as the admin who created it (full admin-equivalent) — no granular per-resource/per-action scoping yet, even though issue #15's rough scope mentions "scope/role per key" as an eventual goal. Shipping a correct, simple all-or-nothing model first and refining it later beats a half-designed scoping system now.

Facade resources are their own classes, not the internal ones with a second auth path

PeerExternalResource, UserExternalResource, etc. under /api/external/v1/ are new, deliberately narrow JAX-RS resource classes. They call into the same underlying services the internal resources already use (PeerService, UserService, ...) — no duplicated business logic — but they own their own request/response DTOs and their own URL shape, so the internal API can keep evolving for the SPA without silently breaking an external integration, and the external surface can stay small (only what's actually meant to be automated) rather than accidentally exposing every internal implementation detail.

The OpenAPI spec is hand-written, not runtime-generated

docs/api/openapi.yml describes the external facade only (never the internal SPA API) and is maintained by hand — or by an offline tool a maintainer runs locally and commits the output of — but never by a runtime annotation-processing extension bundled into the shipped application. Concretely: no quarkus-smallrye-openapi (or equivalent) dependency in build.gradle.kts.

Reasoning: a Prometheus-style /metrics endpoint or a CI script consulting the spec never needs Islandr itself to generate that spec at request time — the spec is a documentation/client-generation artifact consumed by humans and tools (e.g. openapi-generator) outside the running instance, not something the running instance must produce for anyone who asks. Pulling in a runtime OpenAPI extension purely to render a document nobody queries at runtime would add build-time annotation processing and binary size to every native-image build, for every user, to serve a use case (spec authoring) that happens at development time for one person (the maintainer). This is the same "hand-rolled over library, no dependency for something used rarely/offline" reasoning as the ACME client (ADR-0019) and DNS resolver (ADR-0023).

Endpoint scope (v1)

Deliberately starts small: read-mostly access to the resources #15 and #71 actually need (peers list/create, users list) rather than mirroring every internal resource on day one. Each additional facade resource is an incremental, reviewable addition to the same pattern — not a big-bang full-CRUD surface.

Implementation update (2026-08-23): sites (list), resources (list), and roles (list) added, read-only — the precondition ADR-0027 named for a useful MCP tool surface (an assistant can now see sites/resources/roles, though ACL grants themselves are still internal-only). Same pattern as peers/users: new narrow resource classes (SiteExternalResource, ResourceExternalResource, RoleExternalResource) delegating to the existing SiteService/ResourceService/RoleService, no duplicated logic.

Alternatives considered (Pugh Matrix)

Baseline: separate facade + API-key auth + hand-written OpenAPI (the decision). +1 better, 0 equal, −1 worse.

Criterion (weight) Separate facade, hand-written spec (baseline) Bolt Bearer-auth onto internal /api/v1 Separate facade, runtime-generated OpenAPI (quarkus-smallrye-openapi) Separate facade, no formal spec (prose docs only) OAuth2/JWT instead of static API keys
External contract stays stable across internal/SPA changes (5) 0 −1 0 0 0
Native-image build size / build-time cost (4) 0 0 −1 0 −1
Spec stays accurate, not stale (3) 0 0 +1 −1 0
Implementation effort (2) 0 +1 +1 +1 −1
Fits self-hosted-script/CI use case without extra moving parts (3) 0 0 0 0 −1
Weighted total 0 −5 −1 −3 −9

Notes:

  • Bolt Bearer-auth onto internal /api/v1 — least code, but every future SPA-driven reshape of an internal endpoint becomes a silent breaking change for anyone automating against it. The −5 is almost entirely that one criterion; this is the mistake this ADR exists to avoid.
  • Runtime-generated OpenAPI — objectively wins on "stays in sync with the code" (+1), which a hand-written file can't match without discipline. Loses on the build-size/native-image criterion this project already weighs heavily elsewhere (ADR-0012's Go proxy, ADR-0019's hand-rolled ACME client) — an annotation-processing extension shipped in every binary to serve a document read by a handful of humans/tools isn't a good trade for a project this size. If the hand-written spec provably drifts in practice, this is the fallback to revisit.
  • No formal spec — cheapest, but a prose-only description of an API meant for scripts/Terraform-style consumption defeats the point of having a stable external contract at all; nothing machine-readable to generate a client from, and prose drifts just as easily as YAML with less structure forcing a maintainer to notice.
  • OAuth2/JWT — the heaviest option: token issuance, refresh, and (for JWT) a JWKS endpoint or shared-secret rotation story, for a self-hosted single-tenant instance whose actual consumers are CI scripts and cron jobs. Static, revocable API keys are the established, simpler pattern for exactly this (GitHub PATs, Stripe API keys) — OAuth2's added complexity buys safety properties (short-lived tokens, delegated consent) that don't apply when the "delegator" and the "script" are the same admin.

Consequences

  • New package de.chriscohnen.islandr.apikey: ApiKey entity, ApiKeyService (generate/list/revoke), ApiKeyAuthFilter (parallel to SessionFilter, populates the same AuthContext shape so downstream code doesn't need to know which auth path fired), ApiKeyResource (admin-console CRUD, session-authenticated like every other admin page).
  • New /api/external/v1/... resource classes, additive to (never replacing) the internal /api/v1 ones.
  • docs/api/openapi.yml becomes a maintained artifact — checked in, versioned, kept honest by (eventually) a contract test that validates real facade responses against it, not by a runtime generator. Implementation update (2026-08-23): the canonical file actually lives at src/main/resources/META-INF/resources/api/openapi.yml, served as a plain static file (the same mechanism that already serves index.html/CSS/JS — no runtime OpenAPI extension involved) at GET /api/openapi.yml, reachable without authentication and regardless of the externalApiEnabled toggle below (it's documentation, not a facade call). docs/api/openapi.yml is a symlink to that file, so there is still exactly one copy — avoiding the R-185 drift risk between "the docs copy" and "the served copy" by construction, not by discipline.
  • Implementation update (2026-08-23) — opt-out toggle: Settings.externalApiEnabled (default true) lets an admin turn the whole facade off — every /api/external/v1/* route then 404s (not 401/403 — same "don't confirm this exists" posture ADR-0023's DNS resolver already applies) regardless of an otherwise-valid API key. Enforced by a single ExternalApiToggleFilter. This is explicit hardening on top of API-key auth, not a replacement for it — the facade was already access-controlled; this just lets an operator who never intends to use it remove the surface area entirely. Exposed in the Settings page, next to the other global feature toggles (gravatarEnabled, selfServicePeerCreation, ...). The DTO field is a nullable Boolean, not a primitive boolean — a lesson from a real bug this surfaced: several pre-existing tests reconstruct a full SettingsDto.UpdateRequest from a stale/partial object, and a primitive boolean silently defaults to false on any JSON body that omits the field, which would have quietly disabled the facade for the rest of a test JVM's run. null meaning "leave unchanged" (the same convention already used for wgMtu in this DTO) closes that hole structurally instead of relying on every caller remembering the new field.
  • Admin Console gains an "API Keys" page: create (one-time reveal), list (label + prefix + created/last-used + revoke), same UX family as the Webhooks secret-reveal flow (ADR-adjacent to issue #68's implementation).
  • R-184 — A leaked API key is full-admin-equivalent (v1 has no scoping) — broader blast radius than a scoped key would allow. Mitigation: one-time reveal (never re-displayed), instant revocation, per-key audit trail (created/used/revoked events) so a leak is both hard to originate silently and cheap to shut down once noticed. Scoped keys are a tracked future refinement, not a blocker for v1.
  • R-185 — The hand-written OpenAPI spec can drift from the actual facade implementation if a maintainer changes an endpoint and forgets to update openapi.yml. Mitigation: keep the facade resource surface intentionally small (per-endpoint additions are reviewable) and treat "update the spec" as part of the same change, not a follow-up; a contract test against the spec is a natural next hardening step once the facade has more than a couple of endpoints.
  • T-019 — Static, long-lived API keys are a phishable/leakable credential like any bearer token (STRIDE: Spoofing). Mitigation ties to R-184: one-time reveal, hashed at rest (SHA-256, never the raw key), revocable, audit-logged on creation/use/revocation.

References

  • Issue #15 — API key management for automation (this ADR's implementation target)
  • Issue #71 — Prometheus /metrics exporter, the first concrete consumer of the API-key auth path
  • ADR-0011 — process privilege model (SHA-256-not-PBKDF2 reasoning parallels the proxy's own trust-boundary reasoning)
  • ADR-0019 / ADR-0023 — hand-rolled-over-library precedent this ADR's OpenAPI decision follows