@@ -2,6 +2,7 @@ package backup_operations
22
33import (
44 "context"
5+ "crypto/rand"
56 "errors"
67 "fmt"
78 "github.com/jonboulle/clockwork"
@@ -36,6 +37,7 @@ type MakeBackupInternalRequest struct {
3637 ScheduleID * string
3738 Ttl * time.Duration
3839 ParentOperationID * string
40+ EncryptionSettings * pb.EncryptionSettings
3941}
4042
4143func FromBackupSchedule (schedule * types.BackupSchedule ) MakeBackupInternalRequest {
@@ -65,6 +67,7 @@ func FromTBWROperation(tbwr *types.TakeBackupWithRetryOperation) MakeBackupInter
6567 ScheduleID : tbwr .ScheduleID ,
6668 Ttl : tbwr .Ttl ,
6769 ParentOperationID : & tbwr .ID ,
70+ EncryptionSettings : tbwr .EncryptionSettings ,
6871 }
6972}
7073
@@ -282,6 +285,34 @@ func IsEmptyBackup(backup *types.Backup) bool {
282285 return backup .Size == 0 && backup .S3Endpoint == ""
283286}
284287
288+ func GetEncryptionParams (settings * pb.EncryptionSettings ) ([]byte , string , error ) {
289+ var algorithm string
290+ var length int
291+
292+ switch settings .Algorithm {
293+ case pb .EncryptionSettings_UNSPECIFIED :
294+ case pb .EncryptionSettings_AES_128_GCM :
295+ algorithm = "AES-128-GCM"
296+ length = 16
297+ break
298+ case pb .EncryptionSettings_AES_256_GCM :
299+ algorithm = "AES-256-GCM"
300+ length = 32
301+ break
302+ case pb .EncryptionSettings_CHACHA20_POLY1305 :
303+ algorithm = "ChaCha20-Poly1305"
304+ length = 32
305+ break
306+ }
307+
308+ dek := make ([]byte , length )
309+ _ , err := rand .Read (dek )
310+ if err != nil {
311+ return nil , "" , err
312+ }
313+ return dek , algorithm , nil
314+ }
315+
285316func MakeBackup (
286317 ctx context.Context ,
287318 clientConn client.ClientConnector ,
@@ -359,6 +390,18 @@ func MakeBackup(
359390 S3ForcePathStyle : s3 .S3ForcePathStyle ,
360391 }
361392
393+ if req .EncryptionSettings != nil && featureFlags .EnableBackupEncryption {
394+ dek , algorithm , err := GetEncryptionParams (req .EncryptionSettings )
395+ if err != nil {
396+ return nil , nil , err
397+ }
398+
399+ s3Settings .EncryptionKey = dek
400+ s3Settings .EncryptionAlgorithm = algorithm
401+ // TODO: encrypt the DEK using the specified KEK
402+ // TODO: stores the encrypted DEK in S3
403+ }
404+
362405 clientOperationID , err := clientConn .ExportToS3 (ctx , client , s3Settings , featureFlags )
363406 if err != nil {
364407 xlog .Error (ctx , "can't start export operation" , zap .Error (err ))
@@ -388,9 +431,10 @@ func MakeBackup(
388431 CreatedAt : now ,
389432 Creator : subject ,
390433 },
391- ScheduleID : req .ScheduleID ,
392- ExpireAt : expireAt ,
393- SourcePaths : pathsForExport ,
434+ ScheduleID : req .ScheduleID ,
435+ ExpireAt : expireAt ,
436+ SourcePaths : pathsForExport ,
437+ EncryptionSettings : req .EncryptionSettings ,
394438 }
395439
396440 op := & types.TakeBackupOperation {
@@ -409,9 +453,10 @@ func MakeBackup(
409453 CreatedAt : now ,
410454 Creator : subject ,
411455 },
412- YdbOperationId : clientOperationID ,
413- UpdatedAt : now ,
414- ParentOperationID : req .ParentOperationID ,
456+ YdbOperationId : clientOperationID ,
457+ UpdatedAt : now ,
458+ ParentOperationID : req .ParentOperationID ,
459+ EncryptionSettings : req .EncryptionSettings ,
415460 }
416461
417462 return backup , op , nil
0 commit comments