|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + "ydbcp/cmd/integration/common" |
| 8 | + "ydbcp/internal/types" |
| 9 | + pb "ydbcp/pkg/proto/ydbcp/v1alpha1" |
| 10 | + |
| 11 | + "google.golang.org/grpc" |
| 12 | + "google.golang.org/grpc/codes" |
| 13 | + "google.golang.org/grpc/status" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + containerID = "abcde" |
| 18 | + databaseName = "/local" |
| 19 | + ydbcpEndpoint = "0.0.0.0:50051" |
| 20 | + databaseEndpoint = "grpcs://local-ydb:2135" |
| 21 | + testKmsKeyID = "test-kms-key-id-123" |
| 22 | +) |
| 23 | + |
| 24 | +type encryptedBackupScenario struct { |
| 25 | + name string |
| 26 | + request *pb.MakeBackupRequest |
| 27 | +} |
| 28 | + |
| 29 | +type negativeEncryptedBackupScenario struct { |
| 30 | + name string |
| 31 | + request *pb.MakeBackupRequest |
| 32 | + expectedStatus codes.Code |
| 33 | +} |
| 34 | + |
| 35 | +func newEncryptedBackupRequest(rootPath string, sourcePaths []string, kmsKeyID string) *pb.MakeBackupRequest { |
| 36 | + encryptionSettings := &pb.EncryptionSettings{ |
| 37 | + Algorithm: pb.EncryptionSettings_AES_256_GCM, |
| 38 | + } |
| 39 | + |
| 40 | + if len(kmsKeyID) > 0 { |
| 41 | + encryptionSettings.KeyEncryptionKey = &pb.EncryptionSettings_KmsKey_{ |
| 42 | + KmsKey: &pb.EncryptionSettings_KmsKey{ |
| 43 | + KeyId: kmsKeyID, |
| 44 | + }, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return &pb.MakeBackupRequest{ |
| 49 | + ContainerId: containerID, |
| 50 | + DatabaseName: databaseName, |
| 51 | + DatabaseEndpoint: databaseEndpoint, |
| 52 | + RootPath: rootPath, |
| 53 | + SourcePaths: sourcePaths, |
| 54 | + EncryptionSettings: encryptionSettings, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func runEncryptedBackupScenario(ctx context.Context, backupClient pb.BackupServiceClient, opClient pb.OperationServiceClient, scenario encryptedBackupScenario) { |
| 59 | + log.Printf("Running scenario: %s", scenario.name) |
| 60 | + |
| 61 | + tbwr, err := backupClient.MakeBackup(ctx, scenario.request) |
| 62 | + if err != nil { |
| 63 | + log.Panicf("scenario %s: failed to make backup: %v", scenario.name, err) |
| 64 | + } |
| 65 | + |
| 66 | + op, err := opClient.GetOperation( |
| 67 | + ctx, &pb.GetOperationRequest{ |
| 68 | + Id: tbwr.Id, |
| 69 | + }, |
| 70 | + ) |
| 71 | + if err != nil { |
| 72 | + log.Panicf("scenario %s: failed to get operation: %v", scenario.name, err) |
| 73 | + } |
| 74 | + |
| 75 | + if op.EncryptionSettings == nil { |
| 76 | + log.Panicf("scenario %s: encryption settings are nil", scenario.name) |
| 77 | + } |
| 78 | + |
| 79 | + if op.EncryptionSettings.GetKmsKey() == nil { |
| 80 | + log.Panicf("scenario %s: KMS key is nil", scenario.name) |
| 81 | + } |
| 82 | + |
| 83 | + if op.EncryptionSettings.GetKmsKey().GetKeyId() != testKmsKeyID { |
| 84 | + log.Panicf("scenario %s: KMS key ID mismatch, expected %s, got %s", |
| 85 | + scenario.name, testKmsKeyID, op.EncryptionSettings.GetKmsKey().GetKeyId()) |
| 86 | + } |
| 87 | + |
| 88 | + if op.EncryptionSettings.GetAlgorithm() != pb.EncryptionSettings_AES_256_GCM { |
| 89 | + log.Panicf("scenario %s: encryption algorithm is not AES_256_GCM", scenario.name) |
| 90 | + } |
| 91 | + |
| 92 | + // Wait for operation handler |
| 93 | + time.Sleep(time.Second * 3) |
| 94 | + |
| 95 | + ops, err := opClient.ListOperations( |
| 96 | + ctx, &pb.ListOperationsRequest{ |
| 97 | + ContainerId: containerID, |
| 98 | + DatabaseNameMask: databaseName, |
| 99 | + OperationTypes: []string{types.OperationTypeTB.String()}, |
| 100 | + }, |
| 101 | + ) |
| 102 | + if err != nil { |
| 103 | + log.Panicf("failed to list operations: %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + var tbOperation *pb.Operation |
| 107 | + for _, op := range ops.Operations { |
| 108 | + if op.GetParentOperationId() == tbwr.Id { |
| 109 | + tbOperation = op |
| 110 | + break |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if tbOperation == nil { |
| 115 | + log.Panicf("scenario %s: TB operation not found", scenario.name) |
| 116 | + } |
| 117 | + |
| 118 | + // Wait for backup to complete |
| 119 | + done := false |
| 120 | + var backup *pb.Backup |
| 121 | + for range 30 { |
| 122 | + backup, err = backupClient.GetBackup( |
| 123 | + ctx, |
| 124 | + &pb.GetBackupRequest{Id: tbOperation.BackupId}, |
| 125 | + ) |
| 126 | + if err != nil { |
| 127 | + log.Panicf("scenario %s: failed to get backup: %v", scenario.name, err) |
| 128 | + } |
| 129 | + if backup.GetStatus().String() == types.BackupStateAvailable { |
| 130 | + done = true |
| 131 | + break |
| 132 | + } |
| 133 | + time.Sleep(time.Second) |
| 134 | + } |
| 135 | + if !done { |
| 136 | + log.Panicf("scenario %s: failed to complete backup in 30 seconds", scenario.name) |
| 137 | + } |
| 138 | + |
| 139 | + // Verify the backup has encryption settings |
| 140 | + if backup.EncryptionSettings == nil { |
| 141 | + log.Panicf("scenario %s: backup should have encryption settings", scenario.name) |
| 142 | + } |
| 143 | + |
| 144 | + if backup.EncryptionSettings.GetKmsKey() == nil { |
| 145 | + log.Panicf("scenario %s: backup should have KMS key", scenario.name) |
| 146 | + } |
| 147 | + |
| 148 | + if backup.EncryptionSettings.GetKmsKey().GetKeyId() != testKmsKeyID { |
| 149 | + log.Panicf("scenario %s: KMS key ID mismatch, expected %s, got %s", |
| 150 | + scenario.name, testKmsKeyID, backup.EncryptionSettings.GetKmsKey().GetKeyId()) |
| 151 | + } |
| 152 | + |
| 153 | + if backup.EncryptionSettings.GetAlgorithm() != pb.EncryptionSettings_AES_256_GCM { |
| 154 | + log.Panicf("scenario %s: encryption algorithm is not AES_256_GCM", scenario.name) |
| 155 | + } |
| 156 | + |
| 157 | + log.Printf("scenario %s: passed", scenario.name) |
| 158 | +} |
| 159 | + |
| 160 | +func runNegativeEncryptedBackupScenario(ctx context.Context, backupClient pb.BackupServiceClient, opClient pb.OperationServiceClient, scenario negativeEncryptedBackupScenario) { |
| 161 | + log.Printf("Running negative scenario: %s", scenario.name) |
| 162 | + |
| 163 | + _, err := backupClient.MakeBackup(ctx, scenario.request) |
| 164 | + if err != nil { |
| 165 | + st, ok := status.FromError(err) |
| 166 | + if !ok { |
| 167 | + log.Panicf("scenario %s: MakeBackup failed but couldn't extract status: %v", scenario.name, err) |
| 168 | + } |
| 169 | + if st.Code() != scenario.expectedStatus { |
| 170 | + log.Panicf("scenario %s: expected status code %v, got %v: %v", scenario.name, scenario.expectedStatus, st.Code(), err) |
| 171 | + } |
| 172 | + log.Printf("scenario %s: passed", scenario.name) |
| 173 | + } else { |
| 174 | + log.Panicf("scenario %s: MakeBackup should fail with status code %v, but it succeeded", scenario.name, scenario.expectedStatus) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func main() { |
| 179 | + conn := common.CreateGRPCClient(ydbcpEndpoint) |
| 180 | + defer func(conn *grpc.ClientConn) { |
| 181 | + err := conn.Close() |
| 182 | + if err != nil { |
| 183 | + log.Panicln("failed to close connection") |
| 184 | + } |
| 185 | + }(conn) |
| 186 | + backupClient := pb.NewBackupServiceClient(conn) |
| 187 | + opClient := pb.NewOperationServiceClient(conn) |
| 188 | + |
| 189 | + ctx := context.Background() |
| 190 | + |
| 191 | + // Positive scenarios |
| 192 | + positiveScenarios := []encryptedBackupScenario{ |
| 193 | + { |
| 194 | + name: "full encrypted backup", |
| 195 | + request: newEncryptedBackupRequest("", nil, testKmsKeyID), |
| 196 | + }, |
| 197 | + { |
| 198 | + name: "partial encrypted backup", |
| 199 | + request: newEncryptedBackupRequest("", []string{"kv_test"}, testKmsKeyID), |
| 200 | + }, |
| 201 | + { |
| 202 | + name: "full encrypted backup with root path", |
| 203 | + request: newEncryptedBackupRequest("stocks", nil, testKmsKeyID), |
| 204 | + }, |
| 205 | + { |
| 206 | + name: "partial encrypted backup with root path", |
| 207 | + request: newEncryptedBackupRequest("stocks", []string{"orders"}, testKmsKeyID), |
| 208 | + }, |
| 209 | + } |
| 210 | + |
| 211 | + for _, scenario := range positiveScenarios { |
| 212 | + runEncryptedBackupScenario(ctx, backupClient, opClient, scenario) |
| 213 | + time.Sleep(time.Second) |
| 214 | + } |
| 215 | + |
| 216 | + // TODO: add tests for restore encrypted backup |
| 217 | + |
| 218 | + // Negative scenarios |
| 219 | + negativeScenarios := []negativeEncryptedBackupScenario{ |
| 220 | + { |
| 221 | + name: "encrypted backup with empty kms key id", |
| 222 | + request: newEncryptedBackupRequest("", nil, ""), |
| 223 | + expectedStatus: codes.InvalidArgument, |
| 224 | + }, |
| 225 | + } |
| 226 | + |
| 227 | + for _, scenario := range negativeScenarios { |
| 228 | + runNegativeEncryptedBackupScenario(ctx, backupClient, opClient, scenario) |
| 229 | + } |
| 230 | +} |
0 commit comments