@@ -53,6 +53,8 @@ const (
53
53
postgresExporterServiceProjectIDAnnotationName string = pg .ProjectIDLabelName
54
54
storageEncryptionKeyName string = "storage-encryption-key"
55
55
storageEncryptionKeyFinalizerName string = "postgres.database.fits.cloud/secret-finalizer"
56
+ walGEncryptionSecretNamePostfix string = "-walg-encryption"
57
+ walGEncryptionSecretKeyName string = "key"
56
58
)
57
59
58
60
// requeue defines in how many seconds a requeue should happen
@@ -80,6 +82,8 @@ type PostgresReconciler struct {
80
82
PatroniLoopWait uint32
81
83
PatroniRetryTimeout uint32
82
84
EnableRandomStorageEncryptionSecret bool
85
+ EnableWalGEncryption bool
86
+ PostgresletFullname string
83
87
}
84
88
85
89
// Reconcile is the entry point for postgres reconciliation.
@@ -403,6 +407,10 @@ func (r *PostgresReconciler) ensureZalandoDependencies(ctx context.Context, p *p
403
407
return fmt .Errorf ("error while updating backup config: %w" , err )
404
408
}
405
409
410
+ if err := r .updatePodEnvironmentSecret (ctx , p ); err != nil {
411
+ return fmt .Errorf ("error while updating backup config secret: %w" , err )
412
+ }
413
+
406
414
return nil
407
415
}
408
416
@@ -433,19 +441,12 @@ func (r *PostgresReconciler) updatePodEnvironmentConfigMap(ctx context.Context,
433
441
434
442
// use the rest as provided in the secret
435
443
bucketName := backupConfig .S3BucketName
436
- awsAccessKeyID := backupConfig .S3AccessKey
437
- awsSecretAccessKey := backupConfig .S3SecretKey
438
444
backupSchedule := backupConfig .Schedule
439
445
backupNumToRetain := backupConfig .Retention
440
446
441
- // s3 server side encryption SSE is enabled if the key is given
442
- // TODO our s3 needs a small change to make this work
447
+ // s3 server side encryption SSE is disabled
448
+ // we use client side encryption
443
449
walgDisableSSE := "true"
444
- walgSSE := ""
445
- if backupConfig .S3EncryptionKey != nil {
446
- walgDisableSSE = "false"
447
- walgSSE = * backupConfig .S3EncryptionKey
448
- }
449
450
450
451
// create updated content for pod environment configmap
451
452
data := map [string ]string {
@@ -457,12 +458,9 @@ func (r *PostgresReconciler) updatePodEnvironmentConfigMap(ctx context.Context,
457
458
"WALE_BACKUP_THRESHOLD_PERCENTAGE" : "100" ,
458
459
"AWS_ENDPOINT" : awsEndpoint ,
459
460
"WALE_S3_ENDPOINT" : walES3Endpoint , // same as above, but slightly modified
460
- "AWS_ACCESS_KEY_ID" : awsAccessKeyID ,
461
- "AWS_SECRET_ACCESS_KEY" : awsSecretAccessKey ,
462
461
"AWS_S3_FORCE_PATH_STYLE" : "true" ,
463
462
"AWS_REGION" : region , // now we can use AWS S3
464
- "WALG_DISABLE_S3_SSE" : walgDisableSSE , // disable server side encryption if key is nil
465
- "WALG_S3_SSE" : walgSSE , // server side encryption key
463
+ "WALG_DISABLE_S3_SSE" : walgDisableSSE , // server side encryption
466
464
"BACKUP_SCHEDULE" : backupSchedule ,
467
465
"BACKUP_NUM_TO_RETAIN" : backupNumToRetain ,
468
466
}
@@ -491,6 +489,62 @@ func (r *PostgresReconciler) updatePodEnvironmentConfigMap(ctx context.Context,
491
489
return nil
492
490
}
493
491
492
+ func (r * PostgresReconciler ) updatePodEnvironmentSecret (ctx context.Context , p * pg.Postgres ) error {
493
+ log := r .Log .WithValues ("postgres" , p .Name )
494
+ if p .Spec .BackupSecretRef == "" {
495
+ log .Info ("No configured backupSecretRef found, skipping configuration of postgres backup" )
496
+ return nil
497
+ }
498
+
499
+ backupConfig , err := r .getBackupConfig (ctx , p .Namespace , p .Spec .BackupSecretRef )
500
+ if err != nil {
501
+ return err
502
+ }
503
+
504
+ awsAccessKeyID := backupConfig .S3AccessKey
505
+ awsSecretAccessKey := backupConfig .S3SecretKey
506
+
507
+ // create updated content for pod environment configmap
508
+ data := map [string ][]byte {
509
+ "AWS_ACCESS_KEY_ID" : []byte (awsAccessKeyID ),
510
+ "AWS_SECRET_ACCESS_KEY" : []byte (awsSecretAccessKey ),
511
+ }
512
+
513
+ // libsodium client side encryption key
514
+ if r .EnableWalGEncryption {
515
+ s , err := r .getWalGEncryptionSecret (ctx )
516
+ if err != nil {
517
+ return err
518
+ }
519
+ k , exists := s .Data [walGEncryptionSecretKeyName ]
520
+ if ! exists {
521
+ return fmt .Errorf ("could not find key %v in secret %v/%v-%v" , walGEncryptionSecretKeyName , r .PostgresletNamespace , r .PostgresletFullname , walGEncryptionSecretNamePostfix )
522
+ }
523
+ // libsodium keys are fixed-size keys of 32 bytes, see https://github.com/wal-g/wal-g#encryption
524
+ if len (k ) != 32 {
525
+ return fmt .Errorf ("wal_g encryption key must be exactly 32 bytes, got %v" , len (k ))
526
+ }
527
+ data ["WALG_LIBSODIUM_KEY" ] = k
528
+ }
529
+
530
+ var s * corev1.Secret
531
+ ns := types.NamespacedName {
532
+ Name : operatormanager .PodEnvCMName ,
533
+ Namespace : p .ToPeripheralResourceNamespace (),
534
+ }
535
+
536
+ if s , err = r .CreateOrGetPodEnvironmentSecret (ctx , ns .Namespace ); err != nil {
537
+ return fmt .Errorf ("error while accessing the pod environment secret %v: %w" , ns .Namespace , err )
538
+ }
539
+
540
+ s .Data = data
541
+ if err := r .SvcClient .Update (ctx , s ); err != nil {
542
+ return fmt .Errorf ("error while updating the pod environment secret in service cluster: %w" , err )
543
+ }
544
+
545
+ return nil
546
+ }
547
+
494
548
func (r * PostgresReconciler ) isManagedByUs (obj * pg.Postgres ) bool {
495
549
if obj .Spec .PartitionID != r .PartitionID {
496
550
return false
@@ -1206,6 +1260,24 @@ func (r *PostgresReconciler) deleteExporterSidecarService(ctx context.Context, n
1206
1260
return nil
1207
1261
}
1208
1262
1263
+ func (r * PostgresReconciler ) getWalGEncryptionSecret (ctx context.Context ) (* corev1.Secret , error ) {
1264
+
1265
+ ns := r .PostgresletNamespace
1266
+ name := r .PostgresletFullname + walGEncryptionSecretNamePostfix
1267
+
1268
+ // fetch secret
1269
+ s := & corev1.Secret {}
1270
+ nn := types.NamespacedName {
1271
+ Name : name ,
1272
+ Namespace : ns ,
1273
+ }
1274
+ if err := r .SvcClient .Get (ctx , nn , s ); err != nil {
1275
+ return nil , fmt .Errorf ("error while getting the backup secret from service cluster: %w" , err )
1276
+ }
1277
+
1278
+ return s , nil
1279
+ }
1280
+
1209
1281
func (r * PostgresReconciler ) ensureStorageEncryptionSecret (ctx context.Context , instance * pg.Postgres ) error {
1210
1282
1211
1283
if ! r .EnableRandomStorageEncryptionSecret {
0 commit comments