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_idandparent_span_idfrom a well-formedtraceparent. It does not export spans; a production service would. -
Delegation: grant exchange, the
Delegationheader, 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 becomerole:<name>and its groupsgroup:<name>. - Services. A token is a service's only by its issuer's
servicerule:claimis present, and equalsequalswhen that is set. The service then also getsclient:<name>, taken fromclient_claim(defaultazp). No claim marks a service by convention. RFC 9068 putsclient_idinto every access token, a person's included. Without a rule, every caller is a person.- Keycloak:
{claim: client_id}. Theservice_accountscope sets it on client-credentials tokens only. - Entra:
{claim: idtyp, equals: app}.
- Keycloak:
- Verbs on a secret (tresor specs/009).
usecomes only from a grant to one of the caller's roles or groups; grants namerole:orgroup:principals, withuseonly. Admins (policy.admins) hold the management verbs on every secret, and create, but an admin role implies nouse: 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 aclient:etlin a policy matches theetlof 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:
useis 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'sverbslist 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"}}
| Value | Generates | Reference 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 text | yes |
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 (%3Ffor a?in it). The query isscope=<scope>and nothing else, form-encoded (read+writeisread 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'sprovideris the stored one; each generated key is added toredact_keys;expires_atis the earliest expiry: a minted token's, a claim's token's (under a grant, the grant's). - Failures:
403 mint_refusedfor a lasting refusal,503for 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 storedmint++xis served asmint+x, andmint+++xasmint++x. Exactly one+aftermintis a generator; two or more are a literal, with one dropped. The prefix is lowercase:MINT+xis a literal as it is. - Whole values only, here. tresor-server also generates a part of a string (
${…}inside a value, including values insideMAP,STRUCTandLIST); 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 typedmint++xis served asmint++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:
GETserves what it generated, and an escaped literal unescaped. Never write material back withPUT: a served literalmint+claim://subwritten 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 outage503. -
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
audmust 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.createis gone. A config that still has it is refused with that message; list the principals who may create inpolicy.admins.- Old grants are ignored. A stored grant to a
subject:or aclient:, or of any verb butuse, gives nothing now, and the server logs each at start. Re-grantuseto 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
verbschanged meaning.useis 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: aVARCHAR(one PEM, or several one after another, as tresor sends it) or aVARCHAR[], each item a PEM (PRIVATE KEYorRSA 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 is404as for a missing secret, its mint is404, and a grant on it is422 invalid_secret. An administrator writes, replaces, annotates and drops it as any other. A replace never changes a secret's type into or out oftresor_unwrap_key: drop it and write it again. mint+unwrap://key/<name>, as the value of aVARCHARparameter of any name (a JSON string, or a typed{"type": "VARCHAR", "value": …}), makes it a computed parameter: a backup key'sprivate_key, beside itscertificate(s).unwrapis the one operation the reference server serves; anothermint+<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
VARCHARreadingmint+<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 tounwrap: an escaped literal (mint++<op>://…, as for generators), or a typedVARCHARof another operation ({"type": "VARCHAR", "value": "mint+sign://…"}). A bare generator (mint+claim://…) is resolved at read, never served as written, and a typedmint++…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 notresor_unwrap_keyof that name the writer can see, or when the writer does not holdupdateon it (an administrator through a server whose actor policy does not passupdate). Without that, anyone allowed to create a secret could compute with a key they cannot manage. - The certificates a secret lists (
certificates, andcertificate: base64 DER or PEM, aVARCHARor aVARCHAR[]) 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 (storedthumbprints/thumbprintare 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 is422 invalid_input, at the same cost as any. A key secret dropped later, or one whose keys no longer load, makes the mint500 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 secretdynamic. A descriptor carries no parameters. - The mint is
{"param": "private_key", "operation": "unwrap", "input": …, "context": …}. It needsuse(under a delegation grant the server's own), as a material read:404for a secret the caller cannot see, and for aparam(compared in any case) whose value is not the referencemint+<operation>://…to the request'soperation- another operation, or a parameter that is not computed (a body without itsparamoroperationis422 invalid_input);403 no_verb(actor_not_allowedunder a grant) withoutuse. 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'srsa.DecryptPKCS1v15SessionKeywith 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 isinvalid_input. Withcontext.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 -bType0x08,bVersion0x02, reserved 0,aiKeyAlg0x660E / 0x660F / 0x6610 / 0x6603 withdwKeySize16 / 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 answers429(tresor-server does). - The audit is the server's log: a
mintline 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), onokthe 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,erroris an operation it cannot serve (500), and a secret the caller cannot see answers404with no line (a mint against atresor_unwrap_keyhas 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
S256and the redirect URIhttp://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
audienceinto 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_accountscope, which adds theclient_idclaim. 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 advertisescapabilities.variables.TRESOR_CONFORMANCE_GENERATORS=1: generated parameters in the sharedmint+syntax, withTRESOR_CONFORMANCE_ECHO(an http API that answers whom a bearer token was minted for, liketest/keycloak/echo.py),_EXCHANGE_AUDIENCE(an audience the service may exchange the client's token for),_EXCHANGE_USER(the user the exchanged token names),_CLAIMand_CLAIM_VALUE(a claim of the client's token and its value).TRESOR_CONFORMANCE_MINT=1: computed parameters, for a service that advertisescapabilities.mint, on mssql-extension's public test vectors (test/data/mint/, test-only keys).scripts/ci/test_keycloak.shseeds the keys first, atresor_unwrap_keysecretconformance_backup_keysholding both certificates' keys, then threemssql_backup_keysecrets through the protocol, whoseunwrapisTRESOR_CONFORMANCE_MINT_REF(defaultmint+unwrap://key/conformance_backup_keys, the same for both servers):_MINT_SECRET(sample_enc_cert),_MINT_PAIR(sample_enc_certandsample_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) forsample_enc_cert, and_MINT_THUMBPRINT2,_MINT_INPUT2,_MINT_VALUE2forsample_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).