Files
iam/internal/keyring/atrest_test.go
zeekayandHanzo Dev 695cf85aa7 the signing key is mounted by the deployment, and a row cannot hold one
A Cert ROW is the key's IDENTITY — owner, name (the JWKS `kid`), algorithm,
expiry — and identity is public: the JWKS publishes it. The private half is
`json:"-"`, and since every orm backend persists an entity as
json.Marshal(entity), that tag IS the storage contract: no write path reaches a
column with it, and the API neither serves nor accepts it.

internal/keyring holds the material for the life of the process and fills it on
load. ONE source, and only one: a directory of PEM files, one per Cert name, at
$IAM_SIGNING_KEYS — the shape both of the estate's KMS readers already deliver,
so something else performs the KMS read and iam holds no KMS credential. A cert
under a reserved signing owner is the only kind ever given a key, because
material is addressed by name and the name is the `kid`.

Resolution is fail-closed. With nothing mounted a Cert resolves to no key and
the token endpoint refuses. A dev box mounts a PEM like every other deployment
does; nothing mints one in process, so two replicas of one image either hold the
same key or hold none, never different keys under one `kid`.

The seed therefore files cert METADATA and stops there. Everything downstream —
the signer, the verifier, the JWKS, the session-cookie key — reads
cert.PrivateKey off the value the store hands it, exactly as before.

A deployment MUST mount $IAM_SIGNING_KEYS with one PEM per signing cert name
before this rolls; a process that resolves no key refuses to serve.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-21 06:56:46 -07:00

57 lines
1.7 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package keyring_test
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
"github.com/hanzoai/orm"
ormdb "github.com/hanzoai/orm/db"
"github.com/hanzoai/iam/pkg/schema"
)
// The property this whole package exists for: a signing key handed to the store
// does not end up in the database FILE. Asserting it on the file rather than on
// a re-read is deliberate — a re-read can be served from a cache and would pass
// while the bytes sat on disk, which is exactly the failure being fixed.
func TestSigningKeyNeverReachesTheDatabaseFile(t *testing.T) {
_ = schema.Kinds()
dir := t.TempDir()
path := filepath.Join(dir, "iam.db")
db, err := orm.OpenSQLite(&ormdb.SQLiteDBConfig{
Path: path,
Config: ormdb.SQLiteConfig{BusyTimeout: 5000, JournalMode: "WAL"},
})
if err != nil {
t.Fatalf("open: %v", err)
}
const material = "-----BEGIN RSA PRIVATE KEY-----\nCANARYKEYMATERIAL\n-----END RSA PRIVATE KEY-----"
c := orm.New[schema.Cert](db)
c.Owner, c.Name, c.CryptoAlgorithm = "admin", "cert-atrest", "RS256"
c.PrivateKey = material
c.SetId("admin/cert-atrest")
if err := c.CreateCtx(context.Background()); err != nil {
t.Fatalf("create cert: %v", err)
}
if err := db.Close(); err != nil {
t.Fatalf("close: %v", err)
}
for _, f := range []string{path, path + "-wal", path + "-shm"} {
b, err := os.ReadFile(f)
if err != nil {
continue // -wal/-shm may not exist after a clean close
}
if bytes.Contains(b, []byte("CANARYKEYMATERIAL")) {
t.Fatalf("signing key material found in %s — the key that signs every token is at rest in the clear", filepath.Base(f))
}
}
}