Encrypt backups to backup_recipients with age
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3b05ec0538
commit
12efb7d085
10 files changed
+204
-12
No files matched your search
@@ -3,6 +3,7 @@ package cli
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"crypto/ed25519"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -10,9 +11,84 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
agecrypt "filippo.io/age"
|
||||
"golang.org/x/crypto/ssh"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
func TestABackupIsEncryptedToItsRecipients(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
etc := filepath.Join(tmp, "etc")
|
||||
_ = os.MkdirAll(filepath.Join(etc, "pki"), 0o700)
|
||||
_ = os.WriteFile(filepath.Join(etc, "pki", "ca-key.pem"), []byte("THE-CA-KEY"), 0o600)
|
||||
|
||||
id, err := agecrypt.GenerateX25519Identity()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
to, err := config.ParseBackupRecipients([]string{id.Recipient().String()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := filepath.Join(tmp, "backups")
|
||||
path, err := writeBackup(t.Context(), "", etc, out, to)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasSuffix(path, ".tar.gz.age") {
|
||||
t.Errorf("named %s", filepath.Base(path))
|
||||
}
|
||||
raw, _ := os.ReadFile(path)
|
||||
if strings.Contains(string(raw), "THE-CA-KEY") {
|
||||
t.Fatal("the CA key is in the archive in the clear")
|
||||
}
|
||||
|
||||
f, _ := os.Open(path)
|
||||
defer f.Close()
|
||||
plain, err := agecrypt.Decrypt(f, id)
|
||||
if err != nil {
|
||||
t.Fatalf("the recipient cannot open it: %v", err)
|
||||
}
|
||||
gz, err := gzip.NewReader(plain)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tr := tar.NewReader(gz)
|
||||
found := false
|
||||
for {
|
||||
h, err := tr.Next()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
found = found || h.Name == "etc/llmgw/pki/ca-key.pem"
|
||||
}
|
||||
if !found {
|
||||
t.Error("the opened archive lacks the CA key")
|
||||
}
|
||||
// Seen and pruned like any other.
|
||||
if NewestBackup(out).IsZero() {
|
||||
t.Error("the control plane would not see an encrypted archive")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupRecipientsMustBeKeys(t *testing.T) {
|
||||
if _, err := config.ParseBackupRecipients([]string{"not a key"}); err == nil {
|
||||
t.Error("nonsense was taken as a recipient")
|
||||
}
|
||||
pub, _, _ := ed25519.GenerateKey(nil)
|
||||
sp, err := ssh.NewPublicKey(pub)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := config.ParseBackupRecipients([]string{
|
||||
strings.TrimSpace(string(ssh.MarshalAuthorizedKey(sp))) + " ops@example",
|
||||
}); err != nil {
|
||||
t.Errorf("an SSH key was refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestABackupHoldsTheStoreAndTheConfiguration(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
dbPath := filepath.Join(tmp, "store.db")
|
||||
@@ -32,7 +108,7 @@ func TestABackupHoldsTheStoreAndTheConfiguration(t *testing.T) {
|
||||
_ = os.WriteFile(filepath.Join(etc, "pki", "ca-key.pem"), []byte("KEY"), 0o600)
|
||||
|
||||
out := filepath.Join(tmp, "backups")
|
||||
path, err := writeBackup(t.Context(), dbPath, etc, out)
|
||||
path, err := writeBackup(t.Context(), dbPath, etc, out, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -129,7 +205,7 @@ func TestNoScratchCopyOfTheStoreIsLeftBehind(t *testing.T) {
|
||||
}
|
||||
_ = db.Close()
|
||||
out := filepath.Join(tmp, "backups")
|
||||
if _, err := writeBackup(t.Context(), dbPath, t.TempDir(), out); err != nil {
|
||||
if _, err := writeBackup(t.Context(), dbPath, t.TempDir(), out, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, _ := os.ReadDir(out)
|
||||
|
||||
Reference in new issue
Block a user