|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +// Integration tests for the PGP backend. Skipped when gpg is not on |
| 4 | +// PATH. Uses a fresh GNUPGHOME so generated keys never touch the |
| 5 | +// caller's keyring. |
| 6 | +package pgp_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "context" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/dcadolph/cipher" |
| 18 | + "github.com/dcadolph/cipher/pgp" |
| 19 | +) |
| 20 | + |
| 21 | +// TestPGPIntegrationRoundTrip generates a throwaway RSA key in an |
| 22 | +// isolated GNUPGHOME, encrypts a plaintext through cipher and the |
| 23 | +// pgp provider, then decrypts the bytes and checks they match. |
| 24 | +func TestPGPIntegrationRoundTrip(t *testing.T) { |
| 25 | + if _, err := exec.LookPath("gpg"); err != nil { |
| 26 | + t.Skip("gpg not on PATH") |
| 27 | + } |
| 28 | + gnupgHome := t.TempDir() |
| 29 | + t.Setenv("GNUPGHOME", gnupgHome) |
| 30 | + |
| 31 | + fp := generateKey(t, gnupgHome) |
| 32 | + |
| 33 | + kp, err := pgp.NewProvider(fp) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("pgp.NewProvider: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + plain := []byte("api_key: sk-pgp-integration-2026\n") |
| 39 | + enc := cipher.NewEncoder(kp) |
| 40 | + ciphertext, err := enc.Encode(context.Background(), "secrets.yaml", plain) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("encode: %v", err) |
| 43 | + } |
| 44 | + if bytes.Contains(ciphertext, []byte("sk-pgp-integration-2026")) { |
| 45 | + t.Fatal("plaintext leaked into ciphertext") |
| 46 | + } |
| 47 | + |
| 48 | + dec := cipher.NewDecoder() |
| 49 | + round, err := dec.Decode(context.Background(), "secrets.yaml", ciphertext) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("decode: %v", err) |
| 52 | + } |
| 53 | + if !bytes.Equal(plain, round) { |
| 54 | + t.Errorf("round trip mismatch\n want: %q\n got: %q", plain, round) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// generateKey writes a gpg batch script then asks gpg to create a |
| 59 | +// 2048 bit RSA key with no passphrase. Returns the primary key |
| 60 | +// fingerprint. 2048 is enough for tests and finishes in a few |
| 61 | +// seconds even on slow CI runners. |
| 62 | +func generateKey(t *testing.T, gnupgHome string) string { |
| 63 | + t.Helper() |
| 64 | + batch := filepath.Join(gnupgHome, "batch") |
| 65 | + script := []byte(`%no-protection |
| 66 | +Key-Type: RSA |
| 67 | +Key-Length: 2048 |
| 68 | +Name-Real: Cipher Test |
| 69 | +Name-Email: cipher-test@example.com |
| 70 | +Expire-Date: 0 |
| 71 | +%commit |
| 72 | +`) |
| 73 | + if err := os.WriteFile(batch, script, 0o600); err != nil { |
| 74 | + t.Fatalf("write batch: %v", err) |
| 75 | + } |
| 76 | + if out, err := exec.Command("gpg", "--batch", "--gen-key", batch).CombinedOutput(); err != nil { |
| 77 | + t.Fatalf("gen-key: %v\n%s", err, out) |
| 78 | + } |
| 79 | + out, err := exec.Command( |
| 80 | + "gpg", "--list-keys", "--with-colons", "cipher-test@example.com", |
| 81 | + ).Output() |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("list-keys: %v", err) |
| 84 | + } |
| 85 | + for _, line := range strings.Split(string(out), "\n") { |
| 86 | + if !strings.HasPrefix(line, "fpr:") { |
| 87 | + continue |
| 88 | + } |
| 89 | + parts := strings.Split(line, ":") |
| 90 | + if len(parts) >= 10 && parts[9] != "" { |
| 91 | + return parts[9] |
| 92 | + } |
| 93 | + } |
| 94 | + t.Fatal("no fingerprint in gpg --list-keys output") |
| 95 | + return "" |
| 96 | +} |
0 commit comments