Skip to main content
Version: 0.2.0

The reference server

server/ in tresor's repository is a small implementation of the duckdb-secrets/1 protocol in Go. It exists for two reasons: tresor's end-to-end tests run against it next to a real Keycloak, and a company writing its own service can read it to see the protocol working end to end. It is not meant for production: one process, one encrypted file, no high availability. The production service is tresor-server, released separately (its docs).

What it implements​

  • Discovery and whoami.

  • Secrets: list, read, create/replace with preconditions (If-None-Match: *, If-Match), delete, annotate.

  • Grants.

  • Variables (optional in the protocol, capabilities.variables): the secrets' rules in a namespace of their own. It resolves no references, so none of its variables is sensitive.

  • Token verification against any number of OIDC issuers:

    • the signature against the issuer's JWKS;
    • iss (verbatim: an issuer ending in /, like Auth0 or Entra v1, works), expiry, and the audience;
    • asymmetric algorithms only.
  • Tracing: the request log names the caller's trace_id and parent_span_id from a well-formed traceparent. It does not export spans; a production service would.

  • Delegation: grant exchange, the Delegation header, and actor policy (tresor specs/009: under a grant a server uses its own grants for the user, and passes management through only for admins).

Dynamic secrets: any parameter of any secret type may be generated per caller, a token for them or a claim of their token (generated parameters, below). Issuers must be https unless they are on loopback: their signing keys are fetched from them.

Computed parameters (capabilities.mint): SQL Server's backup keys (2017 to 2025) unwrapped with RSA keys held in a secret of the service's own type tresor_unwrap_key, never served (computed parameters, below).

Run it​

cd server
go build -o ref-server ./cmd/ref-server
REF_SERVER_KEY=$(openssl rand -base64 32) ./ref-server -config server.yaml
listen: 127.0.0.1:8443
public_url: http://127.0.0.1:8443 # what discovery calls `api`
# tls: {cert: server.crt, key: server.key} # required unless listening on loopback
store:
path: data/secrets.enc # AES-256-GCM; omit for memory only
key_env: REF_SERVER_KEY # 32 bytes, base64
issuers:
- issuer: https://login.corp.example/realms/main
audience: duckdb-secrets # the token's `aud` must contain it
client_id: duckdb # the public client people log in with
scopes: [openid]
human_flows: [authorization_code, device_code]
service_flows: [client_credentials, private_key_jwt] # also: federated, managed_identity
# audience_parameter: true # Auth0: clients send audience=<audience> (tresor specs/013)
roles_claim: realm_access.roles # Keycloak; Entra/Okta: roles
groups_claim: groups
service: {claim: client_id} # what marks a client-credentials token (see below)
policy:
admins: [role:secrets_admin, client:etl] # the only principals that manage secrets and grant use
actors: # servers that may act for users
- {principal: client:acl-node, verbs: [use]} # its own grants, for its users' statements
- {principal: client:ops-node, issuer: https://login.corp.example/realms/main,
verbs: [use, create, update, delete, annotate, grant]} # admins may manage through it
# cors_origins: [https://app.corp.example] # web pages whose DuckDB-wasm calls the API (tresor specs/020)

Browsers. cors_origins lists the origins of the web applications that run tresor in DuckDB-wasm (https://host[:port], or http on loopback for a dev server; no wildcard). The server answers their preflights and marks their responses; any other origin gets no CORS header, so browsers refuse it. No credentials mode is involved: the page sends a bearer token, never a cookie.

Who may do what​

  • Principals. Every caller is subject:<issuer>|<sub>, which is its identity. Its roles become role:<name> and its groups group:<name>.
  • Services. A token is a service's only by its issuer's service rule: claim is present, and equals equals when that is set. The service then also gets client:<name>, taken from client_claim (default azp). No claim marks a service by convention. RFC 9068 puts client_id into every access token, a person's included. Without a rule, every caller is a person.
    • Keycloak: {claim: client_id}. The service_account scope sets it on client-credentials tokens only.
    • Entra: {claim: idtyp, equals: app}.
  • Verbs on a secret (tresor specs/009). use comes only from a grant to one of the caller's roles or groups; grants name role: or group: principals, with use only. Admins (policy.admins) hold the management verbs on every secret, and create, but an admin role implies no use: an admin uses a secret when one of its roles is granted it. Users create and grant nothing.
  • client: names are shared. They carry no issuer, so with several issuers a client:etl in a policy matches the etl of each.
  • Invisible secrets. A secret you hold no verb on answers 404, exactly like one that does not exist.

Delegation​

  • Actors. A server listed in policy.actors, optionally pinned to the issuer of its token, exchanges a person's token for a grant. The grant lives in memory only (8 h at most, and at most 100 000 grants), is bound to that server, and is never logged.
  • Revocation. Admins revoke by actor or subject (DELETE /v1/delegations?actor=…), and users revoke their own grants.
  • Under a grant:
    • use is the server's own (its roles' grants), for the grant's user: nothing beyond what an admin granted the server;
    • a management verb (or create) passes only for a user who is an admin, and only if the actor's verbs list it;
    • everything else is 403 actor_not_allowed.

Generated parameters​

The protocol lets a service generate a parameter on each read (dynamic secrets) and leaves how it is written to the service. The reference server and tresor-server share the whole-value syntax below. tresor-server reads more (interpolation, typed values, its own kinds), so a store moved there escapes what it must keep literal (below).

An administrator writes a generator as the whole value of a VARCHAR parameter (a JSON string) of a secret of any type, with the provider the consuming extension knows:

{"type": "mssql", "provider": "access_token", "scope": ["mssql://sql.corp.example"],
"params": {"host": "sql.corp.example", "access_token": "mint+exchange://https://database.windows.net/?scope=user_impersonation"}}
ValueGeneratesReference server
mint+exchange://<audience>[?scope=<scope>]a token for the caller at the identity provider, for <audience> (RFC 8693, below; tresor-server also by Entra On-Behalf-Of: token exchange)yes
mint+claim://<claim>the text of a claim of the caller's verified token: a string as it is, a number or a boolean as its JSON textyes
mint+login://…tresor-server's own (generated values)no
mint+rule://…tresor-server's own (generated values)no
  • The audience is everything up to the first ?, percent-decoded (%3F for a ? in it). The query is scope=<scope> and nothing else, form-encoded (read+write is read write).
  • The claim is a top-level claim, named exactly. A claim that is missing, empty, a list or an object is refused at the read (403 mint_refused), and so is one shaped like a JWT. The served value is the claim, never the token it came from. Claims that name the caller's IdP session or bind a token (sid, session_state, jti, nonce, at_hash, c_hash, s_hash) are refused at PUT. Prefer claims the user cannot change (sub, an object id) to editable ones (preferred_username, where the IdP lets users edit it) for anything that grants access.
  • For whom. For the effective user: the direct caller, or under a delegation grant the grant's user, never the server.
  • What is served. The secret is dynamic; the descriptor's provider is the stored one; each generated key is added to redact_keys; expires_at is the earliest expiry: a minted token's, a claim's token's (under a grant, the grant's).
  • Failures: 403 mint_refused for a lasting refusal, 503 for an outage, never an empty value.
  • Only administrators write a generator, as they write any secret. At PUT a generator must be well formed and of a known kind, and an exchange for this service's own audience is refused; the refusal names the parameter, never its value.
  • A literal beginning with mint+ is written with the + doubled: a stored mint++x is served as mint+x, and mint+++x as mint++x. Exactly one + after mint is a generator; two or more are a literal, with one dropped. The prefix is lowercase: MINT+x is a literal as it is.
  • Whole values only, here. tresor-server also generates a part of a string (${…} inside a value, including values inside MAP, STRUCT and LIST); a literal ${ there is written $${. The reference server reads no ${…} and no $${: it serves such a value as written. A typed value ({"type": …, "value": …}) is always a literal here, never unescaped (a typed mint++x is served as mint++x). Moving a store to tresor-server, escape every literal ${ as $${.
  • Material is a resolved copy. An administrator does not read a stored generator back: GET serves what it generated, and an escaped literal unescaped. Never write material back with PUT: a served literal mint+claim://sub written back is a generator.

A token for the caller​

mint+exchange://<audience> mints a token for the caller. The older form still works as an alias: a secret with provider: token_exchange and the downstream audience (and scope) parameters, for an http (bearer_token) or quack (token) secret, is served as if that parameter were mint+exchange://<audience>?scope=<scope>, with the consumer's provider config (the protocol never serves a name for the service's mechanism):

{"type": "quack", "provider": "token_exchange", "scope": ["quack:corp.duck"], "params": {"audience": "acl-node"}}

On every read, the service mints a token for the caller at the identity provider (RFC 8693, as its own client), and serves it as a dynamic secret:

  • a caller reading directly gets a token exchanged from the one it called with;

  • under a delegation grant, the grant's user gets a token, never the server. At the grant's exchange the service exchanges the user's token for each audience the server may use, with a refresh token kept with the grant in memory, and renews from it while the user's IdP session lives.

  • Failures: a lasting refusal is 403 mint_refused, an outage 503.

  • An outage at the grant's exchange does not spoil the session. The grant keeps the user's token for this service until that token expires (minutes), and mints from it later, as it does for a secret granted to the server after the session opened.

  • A minted token is checked: its aud must name the audience asked for, and never this service itself. At PUT, an audience equal to this service's own is refused.

  • Refresh tokens are dropped, not revoked at the IdP (RFC 7009), when a grant ends. They stay valid at the IdP until the user's SSO session ends, and renewing keeps that session from going idle.

The issuer names the service's client, whose secret comes from the environment:

issuers:
- issuer: https://idp.example/realms/corp
exchange: {client_id: duckdb-secrets, client_secret_env: TRESOR_EXCHANGE_SECRET}

Keycloak (standard token exchange):

  • the service's client: standard.token.exchange.enabled, standard.token.exchange.enableRefreshRequestedTokenType: SAME_SESSION, and an audience mapper per downstream audience;
  • the node's client: the same refresh attribute, because tresor's exchange asks for a refresh token so that Keycloak binds the token to the user's session. The test realm has both.

Upgrading from before tresor specs/009​

  • policy.create is gone. A config that still has it is refused with that message; list the principals who may create in policy.admins.
  • Old grants are ignored. A stored grant to a subject: or a client:, or of any verb but use, gives nothing now, and the server logs each at start. Re-grant use to a role or a group. A service account (a node) is granted through a role of its token.
  • Delegation rules are dropped when a stored file is loaded.
  • An actor's verbs changed meaning. use is now the server's own grants, used for its users. Any other verb lets admins manage through the server. Keep them to what you mean to allow; the test configuration lists every verb for its node, which is not a production default.
  • Admin status is taken at the grant's exchange. A demoted admin keeps managing through a node until the grant expires (8 h at most) or is revoked (DELETE /v1/delegations?subject=…).

Computed parameters​

The protocol's computed parameters: a parameter computed by POST /v1/secrets/{name}/mint from the caller's input. The reference server implements unwrap for SQL Server's backup and TDE keys (2017 to 2025), with RSA private keys held in a secret of the service's own type tresor_unwrap_key. Like generators, how such a parameter is written is the service's own; the reference server and tresor-server share it. An administrator writes the keys once, then the secrets that use them:

PUT /v1/secrets/sql_backup
{"type": "tresor_unwrap_key", "provider": "config",
"params": {"keys": "-----BEGIN PRIVATE KEY-----\n…\n-----END PRIVATE KEY-----\n-----BEGIN PRIVATE KEY-----\n…"},
"redact_keys": ["keys"]}

PUT /v1/secrets/crm_backups
{"type": "mssql_backup_key", "provider": "config", "scope": ["s3://backups/crm"],
"params": {"certificates": {"type": "VARCHAR[]", "value": ["MIID…", "MIIE…"]},
"thumbprints": {"type": "VARCHAR[]", "value": ["9C82…", "0F77…"]},
"private_key": "mint+unwrap://key/sql_backup"},
"redact_keys": []}

Through tresor the same is CREATE PERSISTENT SECRET sql_backup IN corp (TYPE tresor_unwrap_key, KEYS '<the PEMs, one after another>'): the PEMs then pass through the administrator's DuckDB once, as any secret they create.

  • tresor_unwrap_key (the type compares in any case) holds one parameter, keys: a VARCHAR (one PEM, or several one after another, as tresor sends it) or a VARCHAR[], each item a PEM (PRIVATE KEY or RSA PRIVATE KEY, unencrypted; one item may hold several) or base64 DER, PKCS#8 or PKCS#1. Every key is RSA of 1024 to 4096 bits. A key that is not is refused at PUT (422 invalid_secret), and the detail names the problem and the key's place in the list, never the key. A rotation is one more key in the list.
  • It is the service's own (protocol): never listed (not by ?type= either), a read of its material is 404 as for a missing secret, its mint is 404, and a grant on it is 422 invalid_secret. An administrator writes, replaces, annotates and drops it as any other. A replace never changes a secret's type into or out of tresor_unwrap_key: drop it and write it again.
  • mint+unwrap://key/<name>, as the value of a VARCHAR parameter of any name (a JSON string, or a typed {"type": "VARCHAR", "value": …}), makes it a computed parameter: a backup key's private_key, beside its certificate(s). unwrap is the one operation the reference server serves; another mint+<operation>://… is a generator of an unknown kind, refused at PUT. A key management service's reference (mint+unwrap://azkv/…, aws/…, gcp/…, vault/…, tresor-server's) is refused at PUT.
  • The reserved form. A served VARCHAR reading mint+<op>://… (<op> of [a-z0-9_-]) is what a client takes for a computed parameter, so a PUT is refused (422 invalid_secret, naming the parameter, never its value) when a parameter would be served so without being a reference to unwrap: an escaped literal (mint++<op>://…, as for generators), or a typed VARCHAR of another operation ({"type": "VARCHAR", "value": "mint+sign://…"}). A bare generator (mint+claim://…) is resolved at read, never served as written, and a typed mint++… is never unescaped. Two parameter names that differ only in case are refused too.
  • A write that references a key secret is refused (422 invalid_secret) when there is no tresor_unwrap_key of that name the writer can see, or when the writer does not hold update on it (an administrator through a server whose actor policy does not pass update). Without that, anyone allowed to create a secret could compute with a key they cannot manage.
  • The certificates a secret lists (certificates, and certificate: base64 DER or PEM, a VARCHAR or a VARCHAR[]) choose the keys: of the key secret's keys, only one whose public key is a certificate's is ever used, and the thumbprint a mint names is matched against the SHA-1 of each certificate's DER (stored thumbprints / thumbprint are the consumer's, not read). The service checks the values of its own types only, so none of this is checked at PUT: a secret with no certificate, one that does not parse, or one no key serves is written, and its unwrap is 422 invalid_input, at the same cost as any. A key secret dropped later, or one whose keys no longer load, makes the mint 500 service_error.
  • The material serves the parameter as stored ("private_key": "mint+unwrap://key/sql_backup"), never resolved, with the redact keys as written; generated parameters are resolved as always. An operation does not make a secret dynamic. A descriptor carries no parameters.
  • The mint is {"param": "private_key", "operation": "unwrap", "input": …, "context": …}. It needs use (under a delegation grant the server's own), as a material read: 404 for a secret the caller cannot see, and for a param (compared in any case) whose value is not the reference mint+<operation>://… to the request's operation - another operation, or a parameter that is not computed (a body without its param or operation is 422 invalid_input); 403 no_verb (actor_not_allowed under a grant) without use. The caller needs nothing on the key secret: the service reads it itself.
  • unwrap: the input (at most 64 KiB of any operation's; 128 to 512 bytes, the key's modulus) is reversed from CryptoAPI's little-endian order and decrypted with RSA-OAEP, SHA-256 for both the hash and MGF1, no label (SQL Server 2025); then with PKCS#1 v1.5 (2017-2022) by Go's rsa.DecryptPKCS1v15SessionKey with a fresh 44-byte random decoy (implicit rejection: a padding failure yields a value that fails the blob's check like any wrong blob). context.padding (oaep-sha256 | pkcs1v15) accepts only that padding's result; another value is invalid_input. With context.thumbprint (40 hex digits, any case) by that certificate's key alone; without one by each of the secret's keys, the blob's check telling the right one (tresor-server refuses that when the secret lists more than one certificate: send the thumbprint). An input of another size than the key's modulus does not compute. The result must be a PLAINTEXTKEYBLOB - bType 0x08, bVersion 0x02, reserved 0, aiKeyAlg 0x660E / 0x660F / 0x6610 / 0x6603 with dwKeySize 16 / 24 / 32 / 24, the key's bytes present and zeros after them up to 44 bytes, checked in constant time - and is answered as decrypted: 200 {"value": "<base64>"}.
  • One failure. Every input or context that does not compute is the same 422 invalid_input, byte for byte. It costs the same work: both paddings, OAEP and v1.5, for every key it would have tried (a valid OAEP that is not a blob and a v1.5 padding failure alike), with a decoy when the input or the thumbprint is no good.
  • No cache_ttl: a client computes each use with a request, so a revoked grant fails the next mint. No rate limit: the reference server never answers 429 (tresor-server does).
  • The audit is the server's log: a mint line with the secret, the caller (and the actor under a grant), the param, the operation as sent, the input's SHA-256, the context (the thumbprint as sent when it is one, context_padding), the outcome (ok, invalid_input, denied, not_found, error), on ok the key's size (key_bits) and the padding that decrypted it, and, for the operator, the step that failed. Never the input or the value. These are the server's own outcomes, not the protocol's list: it never rate-limits, error is an operation it cannot serve (500), and a secret the caller cannot see answers 404 with no line (a mint against a tresor_unwrap_key has one, not_found).
  • FIPS. The server refuses to start under GODEBUG=fips140=only, which refuses PKCS#1 v1.5 and RSA keys under 2048 bits.

Migrating from specs/048's configuration (specs/050): unwrap_keys and public_params are gone, and a configuration that still has either is refused at start, naming the key. Write each former unwrap_keys entry as a tresor_unwrap_key secret of the same name (its PEM files' keys in keys), so mint+unwrap://key/<name> references keep working. Descriptors no longer carry public: a consumer chooses by scope and checks the material of each candidate. The material no longer has operations: a computed parameter holds its reference (private_key = 'mint+unwrap://…', no unwrap parameter), and the mint request names its operation.

Identity provider setup (Keycloak)​

server/testdata/keycloak/realm-tresor.json is the realm the tests import. What matters for a real deployment:

  • A public client for people with the standard flow, PKCE S256 and the redirect URI http://127.0.0.1/*. Keycloak accepts any port on a loopback redirect, as RFC 8252 requires. Enable the device grant if people log in over SSH.
  • An audience mapper that puts the service's audience into access tokens, on the people's client and on every service client.
  • Service clients with service accounts. A client created in the admin console gets the service_account scope, which adds the client_id claim. The imported test realm adds the same mapper by hand.

Conformance​

tresor's repository carries the conformance suite: sqllogictests in test/sql/conformance/, driven against a service URL by environment variables. The conformance client logs in with client credentials; it must be an administrator holding at least one role (an administrator gets use only through a role's grant), allowed to create secrets named conformance_*. Build tresor, then:

TRESOR_CONFORMANCE_HOST=secrets.corp.example \
TRESOR_CONFORMANCE_INSECURE=false \
TRESOR_CONFORMANCE_ISSUER=https://login.corp.example/realms/main \
TRESOR_CONFORMANCE_CLIENT_ID=conformance TRESOR_CONFORMANCE_CLIENT_SECRET=… \
build/release/test/unittest --skip-error-messages '' 'test/sql/conformance/*'

Set TRESOR_CONFORMANCE_PERSON=1 and a BROWSER that can complete your identity provider's login to include the person flow. To include the secrets case, seed a secret the conformance client may use and name it: TRESOR_CONFORMANCE_SECRET, _SECRET_TYPE, _SECRET_PATH (covered by its scope), _SECRET_KEY and _SECRET_VALUE (a VARCHAR parameter). The optional parts run only when asked:

  • TRESOR_CONFORMANCE_VARIABLES=1: variables, for a service that advertises capabilities.variables.
  • TRESOR_CONFORMANCE_GENERATORS=1: generated parameters in the shared mint+ syntax, with TRESOR_CONFORMANCE_ECHO (an http API that answers whom a bearer token was minted for, like test/keycloak/echo.py), _EXCHANGE_AUDIENCE (an audience the service may exchange the client's token for), _EXCHANGE_USER (the user the exchanged token names), _CLAIM and _CLAIM_VALUE (a claim of the client's token and its value).
  • TRESOR_CONFORMANCE_MINT=1: computed parameters, for a service that advertises capabilities.mint, on mssql-extension's public test vectors (test/data/mint/, test-only keys). scripts/ci/test_keycloak.sh seeds the keys first, a tresor_unwrap_key secret conformance_backup_keys holding both certificates' keys, then three mssql_backup_key secrets through the protocol, whose unwrap is TRESOR_CONFORMANCE_MINT_REF (default mint+unwrap://key/conformance_backup_keys, the same for both servers): _MINT_SECRET (sample_enc_cert), _MINT_PAIR (sample_enc_cert and sample_tde_cert), both granted to the client's role, and _MINT_NOUSE (sample_enc_cert, granted to nobody). The vectors, in hex: _MINT_THUMBPRINT, _MINT_INPUT (as the backup stores it), _MINT_VALUE (the PLAINTEXTKEYBLOB) for sample_enc_cert, and _MINT_THUMBPRINT2, _MINT_INPUT2, _MINT_VALUE2 for sample_tde_cert.

scripts/ci/test_keycloak.sh runs the whole suite against the reference server and Keycloak in docker.

Entra ID​

Setting the server up behind Microsoft Entra ID is its own page: Microsoft Entra ID. It covers the app registrations, the issuer entry and the live check (scripts/dev/entra_live.sh).