Files
archie/internal/app/archied/binding_cipher.go
T
sam 087f161836 feat(store): encrypt playbook binding secrets at rest
binding.Binding.Secret was persisted as plaintext in the bindings.secret
column, so anyone with the SQLite file saw sender-side HMAC secrets. Seal
it with AES-256-GCM transparently inside the store: encrypt-on-write,
decrypt-on-read.

- cipher: AES-256-GCM, 12-byte random nonce, SHA-256-normalised 32-byte
  key resolved through the secret registry ([bindings].encryption_key).
- envelope 'arcie-binding:v1:<fingerprint>:<b64(nonce||ct||tag)>' so a
  future cipher/KDF change is a version bump and old rows stay readable.
- rotation via previous_encryption_keys; key identity is the material's
  SHA-256 fingerprint embedded in each row.
- legacy plaintext rows written before a key was configured stay readable;
  encryption is not retroactive.
- nil cipher (no key configured) preserves the pre-t2db.7 plaintext path.

Deferred: captured_events.body redaction upgrade (separate concern, see
docs/prds/binding-secret-encryption.md).

Refs archie-core-t2db.7
2026-09-02 23:54:06 +10:00

41 lines
1.3 KiB
Go

package archied
import (
"fmt"
"strings"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/secret"
"github.com/samcharles93/archie-core/internal/store"
)
// bindingCipherFromConfig resolves the binding at-rest encryption key from
// config through the secret registry and returns a store cipher. An unset
// encryption_key returns a nil cipher, keeping the legacy plaintext path (the
// store's default). See docs/prds/binding-secret-encryption.md.
func bindingCipherFromConfig(cfg config.Config, secrets *secret.Registry) (store.BindingCipher, error) {
if cfg.Bindings.EncryptionKey == (secret.SecretRef{}) {
return nil, nil
}
active, err := cfg.Bindings.EncryptionKey.Resolve(secrets)
if err != nil {
return nil, fmt.Errorf("resolve bindings encryption_key: %w", err)
}
active = strings.TrimSpace(active)
if active == "" {
return nil, fmt.Errorf("bindings encryption_key resolved empty")
}
previous := make([]string, 0, len(cfg.Bindings.PreviousEncryptionKeys))
for _, ref := range cfg.Bindings.PreviousEncryptionKeys {
v, err := ref.Resolve(secrets)
if err != nil {
return nil, fmt.Errorf("resolve bindings previous_encryption_key: %w", err)
}
if v = strings.TrimSpace(v); v != "" {
previous = append(previous, v)
}
}
return store.NewBindingCipher(active, previous)
}