feat(archie): add age sealing key rotation (#2154)
This commit is contained in:
6 files changed
+638
No files matched your search
@@ -0,0 +1,159 @@
|
||||
package archie
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"filippo.io/age"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/buildinfo"
|
||||
"github.com/samcharles93/archie-core/internal/platform/pg"
|
||||
"github.com/samcharles93/archie-core/internal/platform/seal"
|
||||
"github.com/samcharles93/archie-core/modules/connectors"
|
||||
"github.com/samcharles93/archie-core/modules/events"
|
||||
)
|
||||
|
||||
const rekeyLockKey = "archie.rekey.owner"
|
||||
|
||||
// RekeyConfig holds the old and new age identities and optional backup
|
||||
// recipients used by `archie rekey`.
|
||||
type RekeyConfig struct {
|
||||
OldIdentity string
|
||||
NewIdentity string
|
||||
NewBackups []string
|
||||
}
|
||||
|
||||
// LoadRekeyConfig resolves old and new age identities from flags or
|
||||
// ARCHIE_OLD_AGE_IDENTITY[_FILE] and ARCHIE_NEW_AGE_IDENTITY[_FILE], and
|
||||
// validates that both are non-empty, valid, and distinct.
|
||||
func LoadRekeyConfig(oldIdentity, newIdentity string, newBackups []string) (RekeyConfig, error) {
|
||||
oldVal := strings.TrimSpace(oldIdentity)
|
||||
if envOld, ok, err := envOrFile("ARCHIE_OLD_AGE_IDENTITY", "ARCHIE_OLD_AGE_IDENTITY_FILE"); err != nil {
|
||||
return RekeyConfig{}, err
|
||||
} else if ok {
|
||||
oldVal = envOld
|
||||
}
|
||||
|
||||
newVal := strings.TrimSpace(newIdentity)
|
||||
if envNew, ok, err := envOrFile("ARCHIE_NEW_AGE_IDENTITY", "ARCHIE_NEW_AGE_IDENTITY_FILE"); err != nil {
|
||||
return RekeyConfig{}, err
|
||||
} else if ok {
|
||||
newVal = envNew
|
||||
}
|
||||
|
||||
backups := make([]string, len(newBackups))
|
||||
for i, b := range newBackups {
|
||||
backups[i] = strings.TrimSpace(b)
|
||||
}
|
||||
|
||||
rcfg := RekeyConfig{
|
||||
OldIdentity: oldVal,
|
||||
NewIdentity: newVal,
|
||||
NewBackups: backups,
|
||||
}
|
||||
if _, _, err := rcfg.sealers(); err != nil {
|
||||
return RekeyConfig{}, err
|
||||
}
|
||||
return rcfg, nil
|
||||
}
|
||||
|
||||
func (c RekeyConfig) sealers() (oldSealer, newSealer *seal.Sealer, err error) {
|
||||
if c.OldIdentity == "" {
|
||||
return nil, nil, errors.New("old age identity is required")
|
||||
}
|
||||
if c.NewIdentity == "" {
|
||||
return nil, nil, errors.New("new age identity is required")
|
||||
}
|
||||
oldID, err := age.ParseX25519Identity(c.OldIdentity)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("old age identity: %w", err)
|
||||
}
|
||||
newID, err := age.ParseX25519Identity(c.NewIdentity)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("new age identity: %w", err)
|
||||
}
|
||||
if oldID.String() == newID.String() {
|
||||
return nil, nil, errors.New("old and new age identities must be distinct")
|
||||
}
|
||||
oldSealer, err = seal.New(oldID.String(), nil)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("old sealer: %w", err)
|
||||
}
|
||||
newSealer, err = seal.New(newID.String(), c.NewBackups)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("new sealer: %w", err)
|
||||
}
|
||||
return oldSealer, newSealer, nil
|
||||
}
|
||||
|
||||
// Rekey rotates age sealing identities atomically across connectors credentials
|
||||
// and event HMAC secrets under a Postgres advisory lock.
|
||||
func Rekey(ctx context.Context, configPath string, rcfg RekeyConfig) error {
|
||||
log := slog.New(slog.NewJSONHandler(os.Stdout, nil)).With("service", "archie", "version", buildinfo.Version)
|
||||
oldSealer, newSealer, err := rcfg.sealers()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dbURL, err := LoadRuntimeDBConfig(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := pg.Open(ctx, dbURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
if err := pg.VerifySchema(ctx, db); err != nil {
|
||||
return err
|
||||
}
|
||||
release, err := lockRekey(ctx, db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer release()
|
||||
|
||||
tx, err := db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin rekey transaction: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
creds, err := connectors.RekeyCredentials(ctx, tx, oldSealer, newSealer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sources, err := events.RekeySources(ctx, tx, oldSealer, newSealer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("commit rekey transaction: %w", err)
|
||||
}
|
||||
log.InfoContext(ctx, "age sealing keys rotated",
|
||||
"credentials_rekeyed", creds,
|
||||
"sources_rekeyed", sources,
|
||||
"backup_recipients", len(rcfg.NewBackups),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
func lockRekey(ctx context.Context, db *sqlx.DB) (func(), error) {
|
||||
conn, err := db.Conn(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lock rekey: %w", err)
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx, "SELECT pg_advisory_lock(hashtextextended($1, 0))", rekeyLockKey); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("lock rekey: %w", err)
|
||||
}
|
||||
return func() {
|
||||
_, _ = conn.ExecContext(context.WithoutCancel(ctx),
|
||||
"SELECT pg_advisory_unlock(hashtextextended($1, 0))", rekeyLockKey)
|
||||
_ = conn.Close()
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package archie
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"filippo.io/age"
|
||||
)
|
||||
|
||||
func genAgeIdentity(t *testing.T) *age.X25519Identity {
|
||||
t.Helper()
|
||||
id, err := age.GenerateX25519Identity()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// TestLoadRekeyConfigValidationAndOverrides guards `archie rekey` key parsing,
|
||||
// *_FILE secret loading, ambiguity refusal, and identical-key rejection.
|
||||
func TestLoadRekeyConfigValidationAndOverrides(t *testing.T) {
|
||||
oldID := genAgeIdentity(t)
|
||||
newID := genAgeIdentity(t)
|
||||
backupID := genAgeIdentity(t)
|
||||
|
||||
dir := t.TempDir()
|
||||
oldFile := filepath.Join(dir, "old_age_key")
|
||||
newFile := filepath.Join(dir, "new_age_key")
|
||||
if err := os.WriteFile(oldFile, []byte(oldID.String()+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(newFile, []byte(newID.String()+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Run("flags", func(t *testing.T) {
|
||||
cfg, err := LoadRekeyConfig(oldID.String(), newID.String(), []string{backupID.Recipient().String()})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadRekeyConfig: %v", err)
|
||||
}
|
||||
if cfg.OldIdentity != oldID.String() || cfg.NewIdentity != newID.String() || len(cfg.NewBackups) != 1 {
|
||||
t.Fatalf("unexpected RekeyConfig: %+v", cfg)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("env file overrides", func(t *testing.T) {
|
||||
t.Setenv("ARCHIE_OLD_AGE_IDENTITY_FILE", oldFile)
|
||||
t.Setenv("ARCHIE_NEW_AGE_IDENTITY_FILE", newFile)
|
||||
cfg, err := LoadRekeyConfig("", "", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadRekeyConfig from *_FILE: %v", err)
|
||||
}
|
||||
if cfg.OldIdentity != oldID.String() || cfg.NewIdentity != newID.String() {
|
||||
t.Fatalf("unexpected RekeyConfig from files: %+v", cfg)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("both env and file rejected", func(t *testing.T) {
|
||||
t.Setenv("ARCHIE_OLD_AGE_IDENTITY", oldID.String())
|
||||
t.Setenv("ARCHIE_OLD_AGE_IDENTITY_FILE", oldFile)
|
||||
t.Setenv("ARCHIE_NEW_AGE_IDENTITY", newID.String())
|
||||
if _, err := LoadRekeyConfig("", "", nil); err == nil || !strings.Contains(err.Error(), "not both") {
|
||||
t.Fatalf("expected ambiguity error, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
oldKey string
|
||||
newKey string
|
||||
backups []string
|
||||
wantSub string
|
||||
}{
|
||||
{name: "empty old", oldKey: "", newKey: newID.String(), wantSub: "old age identity is required"},
|
||||
{name: "empty new", oldKey: oldID.String(), newKey: "", wantSub: "new age identity is required"},
|
||||
{name: "invalid old", oldKey: "not-an-age-key", newKey: newID.String(), wantSub: "old age identity"},
|
||||
{name: "invalid new", oldKey: oldID.String(), newKey: "not-an-age-key", wantSub: "new age identity"},
|
||||
{name: "invalid backup", oldKey: oldID.String(), newKey: newID.String(), backups: []string{"bad-recipient"}, wantSub: "backup recipient"},
|
||||
{name: "identical keys", oldKey: oldID.String(), newKey: oldID.String(), wantSub: "must be distinct"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := LoadRekeyConfig(tc.oldKey, tc.newKey, tc.backups)
|
||||
if err == nil || !strings.Contains(err.Error(), tc.wantSub) {
|
||||
t.Fatalf("LoadRekeyConfig(%q, %q) = %v, want substring %q", tc.oldKey, tc.newKey, err, tc.wantSub)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRekeyRejectsInvalidConfigBeforeOpeningDB(t *testing.T) {
|
||||
id := genAgeIdentity(t)
|
||||
if err := Rekey(t.Context(), "", RekeyConfig{OldIdentity: id.String(), NewIdentity: id.String()}); err == nil {
|
||||
t.Fatal("expected error for identical identities")
|
||||
}
|
||||
other := genAgeIdentity(t)
|
||||
if err := Rekey(t.Context(), "", RekeyConfig{OldIdentity: id.String(), NewIdentity: other.String()}); err == nil {
|
||||
t.Fatal("expected error when database.url is missing")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user