The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
TL;DR
The internal encryption package protects secret values and routing policy with per-write data keys and a replaceable key provider. The internal attest package tells the Secret Server which runner a request came from.
Scope
This Go module runs Secret Server with database initialization, a migration command, authenticated secret transfer and metadata endpoints, and an authenticated fetch endpoint. See the mdeploy runbook for GCP deployment and verification. The existing API encryption service and stored values remain unchanged.
Server skeleton
Set SECRET_SERVER_ROLE to write, fetch, or all; set ENVIRONMENT,
DATABASE_URL, SECRET_MASTER_KEY_ID, SECRET_MASTER_KEY_BASE64, and
SECRET_SERVER_AUTH_MODE. Google mode requires SECRET_SERVER_AUDIENCE and,
for write or all roles, SECRET_SERVER_ALLOWED_CALLERS. Dev mode requires
SECRET_SERVER_DEV_TOKEN and is limited to local and dev environments.
Production uses Google authentication. Both production roles load the same numbered
Secret Manager version as SECRET_MASTER_KEY_BASE64 and verify the database canary before serving.
The write route group validates Google ID tokens and permits only verified emails
on the allowlist. The fetch route checks Runner proof in fetch and all roles.
The fetch endpoint redeems credentials. The write endpoints transfer secrets
and return secret metadata.
/health is liveness and /ready checks database connectivity. Startup also
performs an encryption round trip.
Startup applies migrations and verifies the durable encrypted canary before the
server listens. A failed migration or key check prevents startup and readiness.
Startup and the standalone migrate command each allow up to two minutes,
including any wait for the migration lock.
The write role exposes POST /internal/v1/boxes/{box_id}/secrets for encrypted
transfers and GET /internal/v1/secrets/{secret_id} for metadata. A successful
transfer returns secret IDs without values. Identical retries return the same IDs;
a changed value or policy returns secret_conflict.
The write role serves the following operations under /internal/v1/organizations/{org_id}/secrets.
| Method and path relative to the collection | Result |
|---|---|
| POST collection | Create a named credential. |
| GET collection | Return id, name, hosts, headers (stored templates), and placeholder, ordered by ID. Use limit (1–1001) and an optional UUID cursor after. |
POST /resolve |
Resolve names to IDs and routing policy. |
HEAD /{secret_id} |
Confirm organization ownership with 204 or return 404. |
DELETE /{secret_id} |
Remove the organization's record. Repeat delivery returns 204. |
The hosted API verifies Box references before sending a delete.
The secret store serializes transfers for each box with a transaction-scoped lock. It checks the organization before names, and rolls back the batch if retry comparison fails. Metadata reads do not retrieve encrypted values.
sequenceDiagram
participant A as Transfer A
participant DB as PostgreSQL
participant B as Transfer B for the same box
A->>DB: Begin and acquire the box lock
B->>DB: Begin and wait for the box lock
alt A succeeds
A->>DB: Commit the batch and release the lock
else A fails
A->>DB: Roll back the batch and release the lock
end
DB-->>B: Grant the box lock
B->>DB: Check the organization, then the existing names
If token validator initialization fails, startup closes the database pool and returns an error before opening the listener.
The server caps the PostgreSQL pool at 10 connections and honors a lower
pool_max_conns in DATABASE_URL. Without that option, the pgx default
applies up to the cap.
SHUTDOWN_TIMEOUT_SEC bounds HTTP draining and database cleanup together.
On a shutdown signal or a permanent listener error, the server stops accepting
connections and drains active requests.
If the deadline expires, the server closes any remaining HTTP connections and stops waiting for the database pool. Pool cleanup can continue until the process exits. A permanent listener error remains part of the returned error even if cleanup times out.
sequenceDiagram
participant Run as Secret Server
participant HTTP as HTTP server
participant DB as PostgreSQL pool
Note over Run: Shutdown signal or listener error starts one deadline
Run->>HTTP: Stop accepting connections and drain active requests
alt Drain completes
HTTP-->>Run: Active requests finished
else Drain fails or the deadline expires
HTTP-->>Run: Return shutdown error
Run->>HTTP: Force-close remaining connections
end
Run->>DB: Start closing the pool
alt Pool closes before the shared deadline
DB-->>Run: Pool close completes
else Shared deadline expires
Note over Run,DB: Run stops waiting and pool cleanup can continue
end
Request logging and OpenTelemetry integration remain open under #2570; the service logs transfer and metadata outcomes without request payloads. Configuration uses explicit environment parsing and validation to keep errors limited to variable names and avoid exposing secret values; it does not use envconfig or validator.
For a local run, supply a persistent development key, database URL, and dev token
through the environment, then run make run:apps:secret-server. The role defaults
to write; set SECRET_SERVER_ROLE=fetch to select fetch. The local target defaults
to the dev attestor. For fetch and all, set
SECRET_SERVER_INTROSPECTION_URL to the full credential introspection URL.
Do not commit or print the key or token.
The local database must use the same key across restarts.
To exercise startup and readiness against a disposable PostgreSQL database, set
SECRET_TEST_DATABASE_URL and run make test:apps:secret-server. Integration
tests are skipped by the general test target when this variable is absent.
The dedicated test:apps:secret-server:integration and
coverage:apps:secret-server:integration targets fail without it. The
configured user must be able to create and drop temporary databases.
Design: envelope encryption and KeyProvider. Routing metadata follows the headers map contract.
Server configuration and write authentication
The service configuration and write authentication middleware are available for the server runtime. Google mode validates token signature, audience, expiry, issuer, verified email, and the caller allowlist. Dev mode uses an exact bearer token and is permitted only in local and dev environments.
Google-mode bearer tokens with signatures shorter than 32 bytes are rejected
before validation; signatures and claims still require verification. PORT must
resolve to a nonzero TCP port so the service does not silently select one.
Calling the package
The server reads SECRET_MASTER_KEY_ID and
SECRET_MASTER_KEY_BASE64, then passes both strings to NewEnvKeyProvider.
The package does not read process environment itself.
provider, err := encryption.NewEnvKeyProvider(keyID, encodedKey)
if err != nil {
return err
}
cipher, err := encryption.NewCipher(provider)
if err != nil {
return err
}
identity := encryption.SecretContext{
OrganizationID: organizationID,
SecretID: secretID,
Version: 1,
Hosts: []string{"api.example.com"},
Headers: map[string]string{"Authorization": "Bearer {value}"},
}
envelope, err := cipher.Encrypt(ctx, identity, value)
The provider requires a canonical standard Base64 encoding of exactly 32 random
key bytes. IDs start with env:, followed by ASCII letters, digits, ., _, or
-, with a maximum total length of 128 bytes. There is no default key.
Construct the provider and cipher before serving requests; their zero values are
not configured instances. Configure one persistent master key per environment.
Encrypt creates a new 32-byte DEK on every call, even for identical inputs.
Decrypt unwraps the saved DEK and authenticates the value before returning it.
An authorized service caller supplies the context for both operations.
value + identity + policy -- DEK --> value ciphertext
DEK + identity + key ID -- master key --> wrapped DEK
Map Envelope.Ciphertext to ciphertext, WrappedDEK.Ciphertext to
encrypted_dek, and WrappedDEK.KeyID to key_id. Ciphertext fields are binary;
the service owns database transactions and transport encoding.
Authenticated context and format
IDs use standard dashed UUID text, accepting either letter case. Versions range from 1 through 2147483647. UUIDs become 16-byte values; versions use uint32 big-endian.
Hosts must be nonempty with no empty entries. ASCII lowercasing, bytewise sorting, and deduplication produce the canonical host set; wildcard syntax is preserved. No DNS lookup, whitespace trimming, trailing-dot removal, or IDNA conversion occurs.
Headers must be an explicit nonempty map with valid HTTP token names. Names are ASCII-lowercased and sorted bytewise. Duplicate canonical names fail even with identical templates. Templates remain byte-exact, including whitespace and case. The transport must reject exact duplicate object keys before converting to a map.
identity = org_uuid[16] || secret_uuid[16] || version_u32be
string(s) = byte_length_u32be || bytes(s)
hosts = count_u32be || string(host_1) || ... || string(host_n)
headers = count_u32be || string(name_1) || string(template_1) || ...
value_aad = "boxlite.secret.value.v1\0" || identity || hosts || headers
dek_aad = "boxlite.secret.dek.v1\0" || identity
env_wrap_aad = dek_aad || string(key_id)
sealed = 0x01 || nonce[12] || encrypted_bytes || tag[16]
Value ciphertext is plaintext length plus 29 bytes. Environment-wrapped DEKs are exactly 61 bytes. Unknown format versions and malformed records fail without fallback. Changing an authenticated destination, header membership, or template causes decryption to fail. Equivalent case/order changes and repeated hosts remain valid.
The service owns secret-value size limits, host policy, prohibited header names,
template syntax, and rendered-value validation. Resolve omitted headers to
{"Authorization": "Bearer {value}"} before calling this package. Never invent
defaults while reading encrypted records: reconstruct the original effective policy.
Return exactly the authenticated metadata to the runner, without rereading it.
AAD does not provide authorization or prevent rollback of an entire historical record.
Errors and key lifecycle
Use errors.Is with ErrInvalidInput, ErrUnknownKey, ErrAuthentication,
ErrProviderUnavailable, context.Canceled, and context.DeadlineExceeded.
Unexpected provider errors are masked, so upstream messages cannot leak values or keys.
Failures return no partial plaintext. There is no plaintext compatibility fallback.
Providers must support concurrent calls and return independently owned buffers. Callers must not concurrently mutate inputs. Temporary DEKs are cleared after use, without a guarantee that every Go runtime copy is erased.
Each DEK encrypts one value. The environment master key wraps many DEKs; Go's
random-nonce GCM limit is 2^32 encryptions per key across all uses. There is no MVP
usage counter. DEK generation uses Go 1.25's
crypto/rand.Read: it fills the buffer
or terminates the process on entropy failure; it cannot return a provider error.
Master-key rotation is not supported yet. Once encrypted data exists, do not change
SECRET_MASTER_KEY_ID or SECRET_MASTER_KEY_BASE64: changing the ID makes existing
versions fail with ErrUnknownKey; changing only the key material makes them fail
with ErrAuthentication. EnvKeyProvider holds only one key, and the package does
not expose a rewrap operation.
A future change will add a rewrap API or a keyring provider that routes by key ID. The envelope format allows rewrapping a DEK without changing the value ciphertext, but KMS, provider routing, and migration jobs are not implemented here.
Database migrations
Set DATABASE_URL to the separate PostgreSQL database and run
secret-server migrate. The command applies embedded goose migrations under a
PostgreSQL session lock, with a two-minute deadline. Run it with an account that
can create schema objects. Migrations are forward-only; deployed application
versions must tolerate compatible schema changes. The database must use UTF-8
server encoding. SIGINT and SIGTERM cancel a running migration.
Before serving requests, the service calls database.Initialize.
It applies migrations and verifies a durable encrypted canary under a separate
transaction advisory lock. If the canary is absent but secrets exist, it first
decrypts an existing secret using AAD version 1.
A wrong key or corrupt ciphertext prevents startup.
Canary enrollment uses a read-committed transaction even when the database has a different default isolation level.
Runner attestation
Design: runner attestation with gcp_iit.
A runner VM requests an instance identity token from its metadata server with
audience=<Secret Server URL>&format=full and sends it in
X-Boxlite-Runner-Identity. The API cannot mint such a token, so the
runner_id the fetch route gets cannot be forged by a compromised API.
selector, err := attest.New(attest.Config{
Attestors: []string{"gcp_iit"},
Environment: "staging",
GCPIIT: attest.GCPIITConfig{
Audience: secretServerURL,
Project: "boxlite-staging",
ServiceAccount: runnerServiceAccount,
},
Registry: `{"gcp_iit": {"4257000000000000001": "rnr_a"}}`,
}, googleVerifier, logger)
runnerID, err := selector.Verify(ctx, r.Header)
googleVerifier comes from attest.NewGoogleTokenVerifier(ctx, nil).
In fetch and all roles, startup requires SECRET_SERVER_ATTESTORS, a
comma-separated list of enabled attestors. The gcp_iit attestor uses
SECRET_SERVER_AUDIENCE, SECRET_SERVER_GCP_PROJECT,
SECRET_SERVER_RUNNER_SERVICE_ACCOUNT, and SECRET_SERVER_RUNNER_REGISTRY.
Invalid attestation configuration prevents startup before the listener opens.
Verify result |
Meaning | Fetch route answer |
|---|---|---|
runnerID, nil |
Proof valid | Continue to credential introspection |
errors.Is(err, attest.ErrInvalidProof) |
Missing, ambiguous, or rejected proof | 401 attestation_failed |
| any other error | Google signing keys unreachable | 503; the runner retries |
Selection. The selector calls the one enabled attestor whose header is present. No proof header, or headers for two attestors, is an invalid proof.
gcp_iit checks. The
idtokenlibrary checks signature,aud, andexp. This package checksiss,iatat most 60 seconds ahead,emailandemail_verified, the Compute Engineproject_id, and thatinstance_idis in the registry'sgcp_iitsection.Registry. JSON
{mode: {machine_id: runner_id}}. GCP mdeploy generates the instance-ID to Runner-UUID map on full deployment. Withgcp_iitenabled, an unset or blank registry fails startup. An explicit{}allows startup and logs a warning that no GCP runners can authenticate.Registry entries. The registry and each mode's machine map must be JSON objects. JSON
null, unknown modes, and empty IDs fail startup. Entries for a disabled attestor log a warning.dev attestor. Reads
X-Boxlite-Dev-Runnerand is refused unlessENVIRONMENTislocalordev. It rejects runner IDs registered under another mode.Errors. The fetch route returns a fixed problem code without token or verifier error details.
Known limitations: a captured token can be replayed until it expires, about an hour; the zone is not checked.
Verification
Run from the repository root with Go 1.25.4 or newer:
make test:apps:secret-server
make test:apps:secret-server:integration
make coverage:apps:secret-server:integration
make lint:apps:secret-server
make fmt:apps:secret-server
make test:apps:secret-server GOTEST_FILTER='-fuzz=FuzzDecryptEnvelope -fuzztime=5s -parallel=2'
The unit test target runs with the race detector and GOWORK=off. The integration
target requires PostgreSQL and permission to create a temporary database. Set
SECRET_TEST_DATABASE_URL before running either dedicated integration target.
Tests create uniquely named databases and drop them afterward.
The coverage target runs the same tests and writes target/coverage/secret-server-integration.out.
Database and command tests share the temporary database setup and cleanup helper.
It also checks that SIGTERM cancels the migration command while it waits for a lock.
CI runs the same integration target against a PostgreSQL 17 service on Ubuntu,
including on draft PRs when Go-related files change. This checks database behavior;
the infra-local BoxLite VM still needs separate local verification.
The Nx test target delegates to the same make target for workspace test discovery.
Nx caching is disabled for this target because the workspace default inputs exclude
Go test files. Every invocation reaches Go, whose own test cache tracks test changes.
The Go CI job selects Secret Server source and test changes and runs this package
through make coverage:go with the race detector and GOWORK=off. It uploads
target/coverage/secret-server.out to Codecov alongside the other Go reports.
Tests cover real environment-key encryption, tampering, policy canonicalization,
key selection, provider errors, cancellation, and concurrent use. An internal test
checks the envelope format's rewrap compatibility; it does not expose rotation to callers.
Boundary tests reject malformed UUID hex and non-AES-256 keys, and cancel at
explicit context checkpoints after cryptographic operations to verify that no
result is released. These checkpoints avoid timing-dependent sleeps.
Fuzzing covers malformed envelope decoding; it is not a cryptographic proof.
On Linux, CI preserves the full-suite report when it adds nested runtime coverage. Database rollback and migration lock cleanup have bounded waits; a connection with a failed migration unlock is discarded.
The test helper verifies the database name before tests run, even if the URL includes
database query parameters. Canary enrollment reads back the persisted ciphertext.
The test database URL also accepts pgxpool options such as pool_max_conns.
Go reports statement coverage; Codecov also distinguishes partially covered source lines, so a passing statement percentage does not imply passing patch coverage. The uint32 framing overflow guards remain in place; tests do not allocate multi-GiB inputs merely to exercise those guards.
Attestation tests sign tokens with a locally generated RSA key and serve it as
a fake Google key set to the real idtoken adapter, so no network is needed.
Verifying real Google tokens is planned for staging.
This is a new contract with no equivalent on the base branch. Full-revert defect proof is not applicable; positive, negative, and boundary tests exercise the new code.
Fetching authorized secrets
The internal/fetch handler implements
credential redemption.
It depends on the single-value database foundation in #2612, including the
existing attestation and encryption packages. Migration 2 extends access-log
results while preserving version-1 data. There is no secret version in grants,
responses or audit rows; decryption always uses AAD version 1.
For fetch and all, startup constructs the handler after database
initialization, using the configured cipher and Runner verifier. Set the full
internal endpoint, for example:
export SECRET_SERVER_INTROSPECTION_URL=https://api-internal/internal/v1/credentials/introspect
For fetch-enabled roles, startup rejects a missing URL or a URL without an
endpoint path before opening the database pool. The runtime
mounts POST /internal/v1/secrets/fetch separately from write-caller authentication.
The write role leaves fetch disabled and does not require this URL.
A runner posts {"credential":"bxsc_..."} with its attestation proof header.
The handler verifies identity before contacting the API, then sends
{"token":"bxsc_...","runner_id":"..."}. The API owns atomic consumption
and runner binding (BUI-1385); the client makes one request with a three-second
limit and never follows redirects, retries or caches authorization.
The MVP API route has no caller authentication and must remain internal-only.
Introspection requires HTTPS, except for literal loopback IPs used by local tests.
Hostnames such as localhost do not qualify for the plaintext exception.
proof -> consuming introspection -> check all organizations and box scopes
-> decrypt all values -> audit -> complete response
An active reply must contain credential, organization, runner and box IDs, an unexpired RFC3339 expiry and unique secret grants. The returned runner must match the verified identity. Missing or disabled rows reject the entire request; box-scoped secrets must match both organization and box. Decryption reconstructs AAD from stored IDs, hosts and headers. A late failure returns no partial values.
Success is {"box_id":"...","secrets":[{"name":"...","value":"...", "hosts":["..."],"headers":{"Authorization":"Bearer {value}"}, "placeholder":"..."}]}. Empty grants return secrets: [].
Every response carries Cache-Control: no-store; errors use RFC 9457 with code.
| Failure | HTTP / code | Audit result |
|---|---|---|
| Invalid proof | 401 / attestation_failed |
attestation_failed |
| Verifier outage | 503 / attestation_unavailable |
attestation_unavailable |
| Inactive credential | 401 / credential_invalid |
credential_invalid |
| Runner mismatch | 401 / credential_invalid |
runner_mismatch |
| Organization or box mismatch | 401 / credential_invalid |
scope_mismatch |
| Missing or disabled secret | 409 / secret_unavailable |
not_found / disabled |
| API failure or malformed answer | 503 / api_unavailable |
api_unavailable |
| Storage failure or resource limit | 503 / store_unavailable |
store_unavailable |
| Decryption failure | 503 / decrypt_failed |
decrypt_failed |
Disabled errors include the authorized name; a deleted secret has no name to return. Inactive reasons are known only to the API. Attestation failures never consume credentials; failures after successful introspection leave them consumed.
Resource bounds are 4 KiB request bodies, 64 KiB API replies, 64 grants, 65,565 bytes of ciphertext (64 KiB plaintext plus 29 bytes of envelope overhead), 64 KiB wrapped keys, 2 MiB per serialized stored row, 12 MiB of serialized rows per grant set, 8 MiB success JSON and 16 concurrent fetch operations. The row budget accommodates the write endpoint's 1 MiB JSON request after PostgreSQL reformats metadata and hex encodes the envelope. A single SQL statement checks row and grant-set sizes before returning payloads.
Audit attempts use a separate 100 ms context for the complete outcome, with at
most 16 concurrent audit operations. Each batch uses one atomic SQL insert, so
a failed row cannot leave a partially persisted batch. Success records one row per secret; empty
success and failure record one row. Unknown identifiers remain null.
Audit failures preserve the response outcome, emit a fixed error log and increment
Handler.AuditFailures() for telemetry collection. Credentials, proofs, values,
templates, placeholders and raw upstream errors are never logged.
Tests use a consuming HTTP fake for the API and real envelope encryption. They cover identity ordering, replay, concurrent consumption, ownership, late failures, redirect refusal, timeout, limits and audit failures. The PostgreSQL integration target includes real fetch queries, audit constraints and schema upgrade preservation; staging integration with the real API remains pending. It also combines the production local/dev attestation selector with HTTP and PostgreSQL to check allowed organization/box access, rejected foreign ownership, and missing-proof rejection before API contact. This does not exercise real GCP tokens or replace staging identity verification.
Credential API contract integration
make test:apps:secret-server:credential-api runs the actual service, controller
and migration as merged by API PR #2769 at commit
bc78b36e5dc241bb3d2d7f8522f4f6f546833b40, with the Secret Server command on a loopback listener and real
PostgreSQL. The harness reads these files from Git into a temporary directory;
it does not change the API checkout or implement a second credential service.
Prerequisites: the merged commit must be available locally (git fetch origin main), Node.js, installed apps dependencies (make _ensure-apps-deps),
Go, and a disposable PostgreSQL instance allowing database creation.
SECRET_TEST_DATABASE_URL=postgresql://boxlite:[email protected]:25432/boxlite \
make test:apps:secret-server:credential-api
NODE_PATH may point to an existing apps dependency installation; otherwise the
target uses this worktree's apps/node_modules. The harness creates a temporary
API schema and a separate secret database, then removes both. Its loopback-only
issuance fixture invokes the real service; it is not a production API endpoint.
The test verifies that missing proof and wrong Runner identity leave the credential usable. It also checks redemption of a 64 KiB value with a 920 KiB placeholder, replay rejection, and persisted audit outcomes. A missing or skipped Go test fails the harness. It covers Secret Server startup and the API contract in an isolated Nest application. Full API startup, ingress, deployment, and GCP attestation require separate validation.