refactor(scripts): drop the SQLite task-database backup from the installer and watchdog
The pre-update PostgreSQL snapshot is the only backup; the watchdog restores binaries and never the database.
This commit is contained in:
42 files changed
+14
-6999
No files matched your search
@@ -0,0 +1,156 @@
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// bindingSecretAAD is the additional-authenticated-data domain separator. It
|
||||
// binds a ciphertext to the binding-secret payload context so it cannot be
|
||||
// relocated to a different field/column and still authenticate. It is
|
||||
// deliberately NOT row-bound: binding secrets are random HMAC keys and are
|
||||
// never used as lookup keys, so cross-row relocation is not a meaningful
|
||||
// attack here (see docs/prds/binding-secret-encryption.md).
|
||||
var bindingSecretAAD = []byte("arcie-binding-secret")
|
||||
|
||||
// bindingEnvelopeMarker and bindingEnvelopeVersion identify a sealed binding
|
||||
// secret and the format version. A future cipher or KDF change bumps the
|
||||
// version so old rows stay readable while new writes use the new format.
|
||||
const (
|
||||
bindingEnvelopeMarker = "arcie-binding"
|
||||
bindingEnvelopeVersion = "v1"
|
||||
// bindingNonceLen is GCM's recommended 12-byte nonce.
|
||||
bindingNonceLen = 12
|
||||
)
|
||||
|
||||
// bindingKeyFingerprintLen is the number of hex characters of the
|
||||
// SHA-256(material) fingerprint embedded in the envelope (8 bytes, far more
|
||||
// than enough to distinguish a handful of rotation keys).
|
||||
const bindingKeyFingerprintLen = 16
|
||||
|
||||
// BindingCipher encrypts and decrypts binding secrets at rest. The store
|
||||
// calls it transparently on write and read; a nil cipher leaves secrets as
|
||||
// plaintext (legacy behaviour). Implementations must be safe for concurrent
|
||||
// use.
|
||||
type BindingCipher interface {
|
||||
// Encrypt returns the sealed envelope for a plaintext secret.
|
||||
Encrypt(plaintext string) (string, error)
|
||||
// Decrypt returns the plaintext secret for a sealed envelope.
|
||||
Decrypt(envelope string) (string, error)
|
||||
}
|
||||
|
||||
// bindingCipher is the AES-256-GCM BindingCipher. Keys are derived as
|
||||
// SHA-256(resolved material) -- 32 bytes of high-entropy input re-shaped to a
|
||||
// 256-bit key, so no passphrase-stretching KDF (Argon2id) is required. The
|
||||
// active key seals new writes; previous keys are retained for decryption only
|
||||
// during rotation.
|
||||
type bindingCipher struct {
|
||||
activeKey []byte
|
||||
activeFingerprint string
|
||||
keys map[string][]byte // fingerprint(hex[:16]) → 32-byte AES key
|
||||
}
|
||||
|
||||
// NewBindingCipher builds a bindingCipher over the active key material and any
|
||||
// previous (decrypt-only) material. Key identity is intrinsic to the material
|
||||
// (the SHA-256 fingerprint), so rotation needs no manual key IDs. It is a pure
|
||||
// constructor: it never reads the secret registry, which the composition root
|
||||
// owns.
|
||||
func NewBindingCipher(active string, previous []string) (*bindingCipher, error) {
|
||||
if active == "" {
|
||||
return nil, errors.New("store: binding cipher requires a non-empty active key")
|
||||
}
|
||||
fp := bindingKeyFingerprint(active)
|
||||
c := &bindingCipher{
|
||||
activeKey: deriveBindingKey(active),
|
||||
activeFingerprint: fp,
|
||||
keys: make(map[string][]byte, 1+len(previous)),
|
||||
}
|
||||
c.keys[fp] = c.activeKey
|
||||
for _, material := range previous {
|
||||
if material == "" {
|
||||
continue
|
||||
}
|
||||
c.keys[bindingKeyFingerprint(material)] = deriveBindingKey(material)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Encrypt seals plaintext with the active key and returns the versioned
|
||||
// envelope: "arcie-binding:v1:<fingerprint>:<base64url(nonce‖ct‖tag)>".
|
||||
func (c *bindingCipher) Encrypt(plaintext string) (string, error) {
|
||||
block, err := aes.NewCipher(c.activeKey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("store: binding cipher: %w", err)
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("store: binding cipher: %w", err)
|
||||
}
|
||||
nonce := make([]byte, bindingNonceLen)
|
||||
if _, err := rand.Read(nonce); err != nil {
|
||||
return "", fmt.Errorf("store: binding cipher: nonce: %w", err)
|
||||
}
|
||||
sealed := gcm.Seal(nil, nonce, []byte(plaintext), bindingSecretAAD)
|
||||
payload := make([]byte, 0, bindingNonceLen+len(sealed))
|
||||
payload = append(payload, nonce...)
|
||||
payload = append(payload, sealed...)
|
||||
return fmt.Sprintf("%s:%s:%s:%s", bindingEnvelopeMarker, bindingEnvelopeVersion,
|
||||
c.activeFingerprint, base64.RawURLEncoding.EncodeToString(payload)), nil
|
||||
}
|
||||
|
||||
// Decrypt parses the envelope, looks up the key by its embedded fingerprint,
|
||||
// and returns the plaintext. A fingerprint not present in the keyring (an old
|
||||
// key dropped from config) fails rather than returning garbage.
|
||||
func (c *bindingCipher) Decrypt(envelope string) (string, error) {
|
||||
parts := strings.SplitN(envelope, ":", 4)
|
||||
if len(parts) != 4 || parts[0] != bindingEnvelopeMarker || parts[1] != bindingEnvelopeVersion {
|
||||
return "", errors.New("store: binding cipher: unrecognised envelope")
|
||||
}
|
||||
key, ok := c.keys[parts[2]]
|
||||
if !ok {
|
||||
return "", errors.New("store: binding cipher: key not in keyring (rotated out?)")
|
||||
}
|
||||
raw, err := base64.RawURLEncoding.DecodeString(parts[3])
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("store: binding cipher: decode payload: %w", err)
|
||||
}
|
||||
if len(raw) < bindingNonceLen {
|
||||
return "", errors.New("store: binding cipher: payload too short")
|
||||
}
|
||||
nonce, sealed := raw[:bindingNonceLen], raw[bindingNonceLen:]
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("store: binding cipher: %w", err)
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("store: binding cipher: %w", err)
|
||||
}
|
||||
plaintext, err := gcm.Open(nil, nonce, sealed, bindingSecretAAD)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("store: binding cipher: authenticate: %w", err)
|
||||
}
|
||||
return string(plaintext), nil
|
||||
}
|
||||
|
||||
// deriveBindingKey reduces 32-byte high-entropy material to a 256-bit AES key.
|
||||
// SHA-256 is sufficient because the input is already high-entropy; it is not a
|
||||
// passphrase-stretcher.
|
||||
func deriveBindingKey(material string) []byte {
|
||||
sum := sha256.Sum256([]byte(material))
|
||||
return sum[:]
|
||||
}
|
||||
|
||||
// bindingKeyFingerprint returns a short stable identifier for key material,
|
||||
// embedded in the envelope so a rotated row can find the key that sealed it.
|
||||
func bindingKeyFingerprint(material string) string {
|
||||
sum := sha256.Sum256([]byte(material))
|
||||
return hex.EncodeToString(sum[:])[:bindingKeyFingerprintLen]
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package edastore
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// testBindingKey is high-entropy 32-byte-style material for cipher tests. It
|
||||
// must be split across the fingerprint and key derivation consistently.
|
||||
const testBindingKey = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
|
||||
func TestBindingCipherRoundTrip(t *testing.T) {
|
||||
c, err := NewBindingCipher(testBindingKey, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewBindingCipher: %v", err)
|
||||
}
|
||||
plain := "sentry-hmac-secret-0123456789abcdef"
|
||||
env, err := c.Encrypt(plain)
|
||||
if err != nil {
|
||||
t.Fatalf("Encrypt: %v", err)
|
||||
}
|
||||
if env == plain {
|
||||
t.Fatal("Encrypt returned the plaintext")
|
||||
}
|
||||
if !strings.HasPrefix(env, "arcie-binding:v1:") {
|
||||
t.Fatalf("envelope prefix = %q, want arcie-binding:v1:", env)
|
||||
}
|
||||
got, err := c.Decrypt(env)
|
||||
if err != nil {
|
||||
t.Fatalf("Decrypt: %v", err)
|
||||
}
|
||||
if got != plain {
|
||||
t.Fatalf("round-trip = %q, want %q", got, plain)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingCipherWrongKeyFails(t *testing.T) {
|
||||
c, _ := NewBindingCipher(testBindingKey, nil)
|
||||
env, err := c.Encrypt("secret-0123456789abcdef")
|
||||
if err != nil {
|
||||
t.Fatalf("Encrypt: %v", err)
|
||||
}
|
||||
c2, _ := NewBindingCipher("a-completely-different-key-0000000000000000", nil)
|
||||
if _, err := c2.Decrypt(env); err == nil {
|
||||
t.Fatal("Decrypt with the wrong key succeeded, want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingCipherTamperedCiphertextFails(t *testing.T) {
|
||||
c, _ := NewBindingCipher(testBindingKey, nil)
|
||||
env, err := c.Encrypt("secret-0123456789abcdef")
|
||||
if err != nil {
|
||||
t.Fatalf("Encrypt: %v", err)
|
||||
}
|
||||
tampered := tamperEnvelope(t, env)
|
||||
if _, err := c.Decrypt(tampered); err == nil {
|
||||
t.Fatal("Decrypt of tampered envelope succeeded, want authentication error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingCipherUnrecognisedEnvelopeFails(t *testing.T) {
|
||||
c, _ := NewBindingCipher(testBindingKey, nil)
|
||||
for _, bad := range []string{"", "not-an-envelope", "arcie-binding:v2:deadbeef:AAAA"} {
|
||||
if _, err := c.Decrypt(bad); err == nil {
|
||||
t.Errorf("Decrypt(%q) succeeded, want error", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingCipherKeyNotInKeyringFails(t *testing.T) {
|
||||
c, _ := NewBindingCipher("key-0-material-0000000000000000", nil)
|
||||
env, err := c.Encrypt("secret-0123456789abcdef")
|
||||
if err != nil {
|
||||
t.Fatalf("Encrypt: %v", err)
|
||||
}
|
||||
// A keyring without the sealing key cannot decrypt the row.
|
||||
c2, _ := NewBindingCipher(testBindingKey, nil)
|
||||
if _, err := c2.Decrypt(env); err == nil {
|
||||
t.Fatal("Decrypt via a keyring missing the sealing key succeeded, want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingCipherRotationKeepsOldRowsReadable(t *testing.T) {
|
||||
oldKey := "old-key-material-000000000000000000000000"
|
||||
cOld, _ := NewBindingCipher(oldKey, nil)
|
||||
env, err := cOld.Encrypt("secret-0123456789abcdef")
|
||||
if err != nil {
|
||||
t.Fatalf("Encrypt (old): %v", err)
|
||||
}
|
||||
|
||||
// Rotate: active becomes a new key, old is retained for reads.
|
||||
cNew, err := NewBindingCipher(testBindingKey, []string{oldKey})
|
||||
if err != nil {
|
||||
t.Fatalf("NewBindingCipher (rotated): %v", err)
|
||||
}
|
||||
got, err := cNew.Decrypt(env)
|
||||
if err != nil {
|
||||
t.Fatalf("Decrypt old row under rotated keyring: %v", err)
|
||||
}
|
||||
if got != "secret-0123456789abcdef" {
|
||||
t.Fatalf("rotated decrypt = %q", got)
|
||||
}
|
||||
|
||||
// Once the old key is dropped, the old row is unreadable.
|
||||
cDropped, _ := NewBindingCipher(testBindingKey, nil)
|
||||
if _, err := cDropped.Decrypt(env); err == nil {
|
||||
t.Fatal("Decrypt with rotated-out keyring succeeded, want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingCipherDerivesDistinctKeys(t *testing.T) {
|
||||
a, _ := NewBindingCipher("material-A", nil)
|
||||
b, _ := NewBindingCipher("material-B", nil)
|
||||
if a.activeFingerprint == b.activeFingerprint {
|
||||
t.Fatal("distinct materials produced the same fingerprint")
|
||||
}
|
||||
if string(a.activeKey) == string(b.activeKey) {
|
||||
t.Fatal("distinct materials produced the same derived key")
|
||||
}
|
||||
}
|
||||
|
||||
// openWithCipher builds a store whose binding secrets are encrypted at rest.
|
||||
func openWithCipher(t *testing.T, cipher BindingCipher) *Store {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
s, err := Open(Config{
|
||||
DBPath: filepath.Join(dir, "eda.sqlite"),
|
||||
DataDir: filepath.Join(dir, "pb_data"),
|
||||
Cipher: cipher,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Open() error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
return s
|
||||
}
|
||||
|
||||
func sampleBinding() bindingInput {
|
||||
return bindingInput{Name: "n", Source: "sentry", Workflow: "implement", Secret: "supersecretvalue0123"}
|
||||
}
|
||||
|
||||
// TestBindingSecretIsEncryptedAtRest pins that the plaintext secret never
|
||||
// reaches the database file. Reading the column directly is the point: a
|
||||
// round-trip through the store would decrypt and prove nothing.
|
||||
func TestBindingSecretIsEncryptedAtRest(t *testing.T) {
|
||||
cipher, err := NewBindingCipher(testBindingKey, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewBindingCipher() error = %v", err)
|
||||
}
|
||||
s := openWithCipher(t, cipher)
|
||||
in := sampleBinding()
|
||||
id := insertBinding(t, s, in)
|
||||
|
||||
var stored string
|
||||
if err := s.App().DB().NewQuery("SELECT secret FROM " + CollBindings + " WHERE id = {:id}").
|
||||
Bind(map[string]any{"id": id}).Row(&stored); err != nil {
|
||||
t.Fatalf("read raw secret: %v", err)
|
||||
}
|
||||
if stored == in.Secret {
|
||||
t.Fatal("secret is stored as plaintext; it must be encrypted at rest")
|
||||
}
|
||||
if !strings.Contains(stored, ":") {
|
||||
t.Errorf("stored secret %q does not look like a cipher envelope", stored)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBindingSecretDecryptsOnRead pins the other half: what was encrypted
|
||||
// comes back as the original plaintext.
|
||||
func TestBindingSecretDecryptsOnRead(t *testing.T) {
|
||||
cipher, _ := NewBindingCipher(testBindingKey, nil)
|
||||
s := openWithCipher(t, cipher)
|
||||
in := sampleBinding()
|
||||
id := insertBinding(t, s, in)
|
||||
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatalf("GetBinding() error = %v", err)
|
||||
}
|
||||
if got.Secret != in.Secret {
|
||||
t.Errorf("Secret = %q, want %q", got.Secret, in.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNoCipherKeepsPlaintext pins the behaviour that predates encryption: a
|
||||
// deployment with no configured key still works, storing plaintext.
|
||||
func TestNoCipherKeepsPlaintext(t *testing.T) {
|
||||
s := openWithCipher(t, nil)
|
||||
in := sampleBinding()
|
||||
id := insertBinding(t, s, in)
|
||||
|
||||
var stored string
|
||||
if err := s.App().DB().NewQuery("SELECT secret FROM " + CollBindings + " WHERE id = {:id}").
|
||||
Bind(map[string]any{"id": id}).Row(&stored); err != nil {
|
||||
t.Fatalf("read raw secret: %v", err)
|
||||
}
|
||||
if stored != in.Secret {
|
||||
t.Errorf("stored secret = %q, want the plaintext %q with no cipher configured", stored, in.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRotatedKeyStillReadsOldRows pins key rotation: a secret written under
|
||||
// the previous key must stay readable once a new active key is installed,
|
||||
// otherwise a rotation silently bricks every armed binding.
|
||||
func TestRotatedKeyStillReadsOldRows(t *testing.T) {
|
||||
const oldKey = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"
|
||||
oldCipher, _ := NewBindingCipher(oldKey, nil)
|
||||
s := openWithCipher(t, oldCipher)
|
||||
in := sampleBinding()
|
||||
id := insertBinding(t, s, in)
|
||||
|
||||
rotated, err := NewBindingCipher(testBindingKey, []string{oldKey})
|
||||
if err != nil {
|
||||
t.Fatalf("NewBindingCipher(rotated) error = %v", err)
|
||||
}
|
||||
s.cipher = rotated
|
||||
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatalf("GetBinding() after rotation error = %v", err)
|
||||
}
|
||||
if got.Secret != in.Secret {
|
||||
t.Errorf("Secret after rotation = %q, want %q: the previous key must still decrypt", got.Secret, in.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
// tamperEnvelope flips one byte inside the ciphertext region so Decrypt must
|
||||
// fail authentication rather than return altered plaintext.
|
||||
func tamperEnvelope(t *testing.T, envelope string) string {
|
||||
t.Helper()
|
||||
parts := strings.SplitN(envelope, ":", 4)
|
||||
if len(parts) != 4 {
|
||||
t.Fatalf("unexpected envelope: %q", envelope)
|
||||
}
|
||||
raw, err := base64.RawURLEncoding.DecodeString(parts[3])
|
||||
if err != nil {
|
||||
t.Fatalf("decode payload: %v", err)
|
||||
}
|
||||
// Flip a byte in the ciphertext region (not the nonce).
|
||||
idx := 12
|
||||
if len(raw) <= idx {
|
||||
t.Fatalf("payload too short to tamper: %d bytes", len(raw))
|
||||
}
|
||||
raw[idx] ^= 0x01
|
||||
parts[3] = base64.RawURLEncoding.EncodeToString(raw)
|
||||
return strings.Join(parts, ":")
|
||||
}
|
||||
Reference in new issue
Block a user