@@ -2,6 +2,7 @@ package backup_operations
22
33import  (
44	"context" 
5+ 	"crypto/rand" 
56	"errors" 
67	"fmt" 
78	"github.com/jonboulle/clockwork" 
@@ -35,6 +36,7 @@ type MakeBackupInternalRequest struct {
3536	ScheduleID            * string 
3637	Ttl                   * time.Duration 
3738	ParentOperationID     * string 
39+ 	EncryptionSettings    * pb.EncryptionSettings 
3840}
3941
4042func  FromBackupSchedule (schedule  * types.BackupSchedule ) MakeBackupInternalRequest  {
@@ -62,6 +64,7 @@ func FromTBWROperation(tbwr *types.TakeBackupWithRetryOperation) MakeBackupInter
6264		ScheduleID :           tbwr .ScheduleID ,
6365		Ttl :                  tbwr .Ttl ,
6466		ParentOperationID :    & tbwr .ID ,
67+ 		EncryptionSettings :   tbwr .EncryptionSettings ,
6568	}
6669}
6770
@@ -274,6 +277,34 @@ func IsEmptyBackup(backup *types.Backup) bool {
274277	return  backup .Size  ==  0  &&  backup .S3Endpoint  ==  "" 
275278}
276279
280+ func  GetEncryptionParams (settings  * pb.EncryptionSettings ) ([]byte , string , error ) {
281+ 	var  algorithm  string 
282+ 	var  length  int 
283+ 
284+ 	switch  settings .Algorithm  {
285+ 	case  pb .EncryptionSettings_UNSPECIFIED :
286+ 	case  pb .EncryptionSettings_AES_128_GCM :
287+ 		algorithm  =  "AES-128-GCM" 
288+ 		length  =  16 
289+ 		break 
290+ 	case  pb .EncryptionSettings_AES_256_GCM :
291+ 		algorithm  =  "AES-256-GCM" 
292+ 		length  =  32 
293+ 		break 
294+ 	case  pb .EncryptionSettings_CHACHA20_POLY1305 :
295+ 		algorithm  =  "ChaCha20-Poly1305" 
296+ 		length  =  32 
297+ 		break 
298+ 	}
299+ 
300+ 	dek  :=  make ([]byte , length )
301+ 	_ , err  :=  rand .Read (dek )
302+ 	if  err  !=  nil  {
303+ 		return  nil , "" , err 
304+ 	}
305+ 	return  dek , algorithm , nil 
306+ }
307+ 
277308func  MakeBackup (
278309	ctx  context.Context ,
279310	clientConn  client.ClientConnector ,
@@ -350,6 +381,18 @@ func MakeBackup(
350381		S3ForcePathStyle :  s3 .S3ForcePathStyle ,
351382	}
352383
384+ 	if  req .EncryptionSettings  !=  nil  &&  featureFlags .EnableBackupEncryption  {
385+ 		dek , algorithm , err  :=  GetEncryptionParams (req .EncryptionSettings )
386+ 		if  err  !=  nil  {
387+ 			return  nil , nil , err 
388+ 		}
389+ 
390+ 		s3Settings .EncryptionKey  =  dek 
391+ 		s3Settings .EncryptionAlgorithm  =  algorithm 
392+ 		// TODO: encrypt the DEK using the specified KEK 
393+ 		// TODO: stores the encrypted DEK in S3 
394+ 	}
395+ 
353396	clientOperationID , err  :=  clientConn .ExportToS3 (ctx , client , s3Settings , featureFlags )
354397	if  err  !=  nil  {
355398		xlog .Error (ctx , "can't start export operation" , zap .Error (err ))
@@ -379,9 +422,10 @@ func MakeBackup(
379422			CreatedAt : now ,
380423			Creator :   subject ,
381424		},
382- 		ScheduleID :  req .ScheduleID ,
383- 		ExpireAt :    expireAt ,
384- 		SourcePaths : pathsForExport ,
425+ 		ScheduleID :         req .ScheduleID ,
426+ 		ExpireAt :           expireAt ,
427+ 		SourcePaths :        pathsForExport ,
428+ 		EncryptionSettings : req .EncryptionSettings ,
385429	}
386430
387431	op  :=  & types.TakeBackupOperation {
@@ -399,9 +443,10 @@ func MakeBackup(
399443			CreatedAt : now ,
400444			Creator :   subject ,
401445		},
402- 		YdbOperationId :    clientOperationID ,
403- 		UpdatedAt :         now ,
404- 		ParentOperationID : req .ParentOperationID ,
446+ 		YdbOperationId :     clientOperationID ,
447+ 		UpdatedAt :          now ,
448+ 		ParentOperationID :  req .ParentOperationID ,
449+ 		EncryptionSettings : req .EncryptionSettings ,
405450	}
406451
407452	return  backup , op , nil 
0 commit comments