|
| 1 | +package sharded |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + stdlog "log" |
| 8 | + |
| 9 | + "go.mongodb.org/mongo-driver/v2/bson" |
| 10 | + |
| 11 | + "github.com/percona/percona-backup-mongodb/pbm/defs" |
| 12 | + "github.com/percona/percona-backup-mongodb/pbm/encrypt" |
| 13 | +) |
| 14 | + |
| 15 | +// pbmEncryptedMagic is the 4-byte marker every PBM-encrypted stream begins |
| 16 | +// with. Data objects on storage must carry it when encryption is enabled. |
| 17 | +var pbmEncryptedMagic = []byte("PBME") |
| 18 | + |
| 19 | +// maxObjectScanBytes caps how large an object we pull off storage to inspect. |
| 20 | +// The big ballast dump is skipped. The small per-collection dumps are enough to |
| 21 | +// prove the storage data is encrypted. |
| 22 | +const maxObjectScanBytes = 8 << 20 // 8 Mib |
| 23 | + |
| 24 | +func (c *Cluster) EncryptedBackupAndRestore(confFile string, expected encrypt.EncryptionType) { |
| 25 | + c.ApplyConfig(context.TODO(), confFile) |
| 26 | + |
| 27 | + c.SetBallastData(1e5) |
| 28 | + checkData := c.DataChecker() |
| 29 | + |
| 30 | + bcpName := c.LogicalBackup() |
| 31 | + c.BackupWaitDone(context.TODO(), bcpName) |
| 32 | + |
| 33 | + c.assertBackupEncryption(context.TODO(), bcpName, expected) |
| 34 | + c.assertStorageEncrypted(context.TODO(), bcpName) |
| 35 | + |
| 36 | + c.DeleteBallast() |
| 37 | + |
| 38 | + stdlog.Println("resync backup list") |
| 39 | + if err := c.mongopbm.StoreResync(context.TODO()); err != nil { |
| 40 | + stdlog.Fatalln("Error: resync backup lists:", err) |
| 41 | + } |
| 42 | + |
| 43 | + c.LogicalRestore(context.TODO(), bcpName) |
| 44 | + checkData() |
| 45 | +} |
| 46 | + |
| 47 | +func (c *Cluster) EncryptedRestoreWrongPassphraseFails(correctConf, wrongConf string) { |
| 48 | + c.ApplyConfig(context.TODO(), correctConf) |
| 49 | + |
| 50 | + c.SetBallastData(1e5) |
| 51 | + checkData := c.DataChecker() |
| 52 | + |
| 53 | + bcpName := c.LogicalBackup() |
| 54 | + c.BackupWaitDone(context.TODO(), bcpName) |
| 55 | + |
| 56 | + c.DeleteBallast() |
| 57 | + |
| 58 | + c.ApplyConfig(context.TODO(), wrongConf) |
| 59 | + |
| 60 | + stdlog.Println("restoring with the wrong passphrase (expecting failure)") |
| 61 | + if _, err := c.pbm.Restore(bcpName, []string{}); err != nil { |
| 62 | + stdlog.Fatalln("starting restore with wrong passphrase:", err) |
| 63 | + } |
| 64 | + |
| 65 | + if err := c.pbm.CheckRestore(bcpName, defs.WaitBackupStart*6); err == nil { |
| 66 | + stdlog.Fatalln("Error: restore with wrong passphrase succeeded, expected decryption failure") |
| 67 | + } else { |
| 68 | + stdlog.Printf("restore failed as expected with wrong passphrase: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + c.clearRestoreHistory(context.TODO()) |
| 72 | + |
| 73 | + stdlog.Println("healing cluster with the correct passphrase") |
| 74 | + c.ApplyConfig(context.TODO(), correctConf) |
| 75 | + c.LogicalRestore(context.TODO(), bcpName) |
| 76 | + checkData() |
| 77 | +} |
| 78 | + |
| 79 | +func (c *Cluster) clearRestoreHistory(ctx context.Context) { |
| 80 | + _, err := c.mongopbm.Conn().MongoClient(). |
| 81 | + Database(defs.DB). |
| 82 | + Collection(defs.RestoresCollection). |
| 83 | + DeleteMany(ctx, bson.M{}) |
| 84 | + if err != nil { |
| 85 | + stdlog.Fatalln("clear restore history:", err) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func (c *Cluster) assertBackupEncryption(ctx context.Context, bcpName string, expected encrypt.EncryptionType) { |
| 90 | + meta, err := c.mongopbm.GetBackupMeta(ctx, bcpName) |
| 91 | + if err != nil { |
| 92 | + stdlog.Fatalln("get backup meta:", err) |
| 93 | + } |
| 94 | + if meta.Encryption != expected { |
| 95 | + stdlog.Fatalf("backup %s encryption mismatch: got %q, want %q", bcpName, meta.Encryption, expected) |
| 96 | + } |
| 97 | + stdlog.Printf("backup %s reports encryption %q", bcpName, meta.Encryption) |
| 98 | +} |
| 99 | + |
| 100 | +func (c *Cluster) assertStorageEncrypted(ctx context.Context, bcpName string) { |
| 101 | + stg, err := c.mongopbm.Storage(ctx) |
| 102 | + if err != nil { |
| 103 | + stdlog.Fatalln("get storage:", err) |
| 104 | + } |
| 105 | + |
| 106 | + files, err := stg.List(bcpName, "") |
| 107 | + if err != nil { |
| 108 | + stdlog.Fatalln("list backup files:", err) |
| 109 | + } |
| 110 | + if len(files) == 0 { |
| 111 | + stdlog.Fatalf("no files found on storage for backup %s", bcpName) |
| 112 | + } |
| 113 | + |
| 114 | + encrypted, checked, skipped := 0, 0, 0 |
| 115 | + for _, f := range files { |
| 116 | + if f.Size <= 0 || f.Size > maxObjectScanBytes { |
| 117 | + skipped++ |
| 118 | + continue |
| 119 | + } |
| 120 | + name := bcpName + "/" + f.Name |
| 121 | + r, err := stg.SourceReader(name) |
| 122 | + if err != nil { |
| 123 | + stdlog.Fatalf("read %s: %v", name, err) |
| 124 | + } |
| 125 | + // Partial reads can stall |
| 126 | + data, err := io.ReadAll(r) |
| 127 | + r.Close() |
| 128 | + if err != nil { |
| 129 | + stdlog.Fatalf("read %s: %v", name, err) |
| 130 | + } |
| 131 | + checked++ |
| 132 | + if bytes.HasPrefix(data, pbmEncryptedMagic) { |
| 133 | + encrypted++ |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if encrypted == 0 { |
| 138 | + stdlog.Fatalf("no encrypted data objects found for backup %s (checked %d, skipped %d large or empty of %d)", |
| 139 | + bcpName, checked, skipped, len(files)) |
| 140 | + } |
| 141 | + stdlog.Printf("%d/%d checked objects of backup %s carry the PBM encryption header (%d large or empty skipped)", |
| 142 | + encrypted, checked, bcpName, skipped) |
| 143 | +} |
0 commit comments