Skip to content

Commit 10759bd

Browse files
cgrardfrezbo
authored andcommitted
feat: add support for encryption as optional in BackupSnapshot
This commit modifies the `BackupSnapshot` function in the `service` package to add support for encryption of the etcd snapshot before uploading it to S3. The `BackupSnapshot` function now takes an additional boolean parameter `encrypt` which determines whether encryption should be enabled or not. If encryption is enabled, the etcd snapshot is encrypted using the provided public key before uploading it to S3. If encryption is disabled, the etcd snapshot is uploaded as is. This change allows users to choose whether they want to encrypt their etcd snapshots or not. Signed-off-by: Cedric Grard <[email protected]> Signed-off-by: Noel Georgi <[email protected]>
1 parent db9891a commit 10759bd

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

cmd/talos-backup/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func run() error {
3333
return fmt.Errorf("failed to create talos client: %w", err)
3434
}
3535

36-
return service.BackupEncryptedSnapshot(ctx, serviceConfig, talosConfig, talosClient)
36+
return service.BackupSnapshot(ctx, serviceConfig, talosConfig, talosClient, serviceConfig.DisableEncryption)
3737
}
3838

3939
func main() {

cmd/talos-backup/service/service.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"github.com/siderolabs/talos-backup/pkg/util"
2020
)
2121

22-
// BackupEncryptedSnapshot takes a snapshot of etcd, encrypts it and uploads it to S3.
23-
func BackupEncryptedSnapshot(ctx context.Context, serviceConfig *config.ServiceConfig, talosConfig *talosconfig.Config, talosClient *talosclient.Client) error {
22+
// BackupSnapshot takes a snapshot of etcd, encrypts it or not and uploads it to S3.
23+
func BackupSnapshot(ctx context.Context, serviceConfig *config.ServiceConfig, talosConfig *talosconfig.Config, talosClient *talosclient.Client, disableEncryption bool) error {
2424
clusterName := serviceConfig.ClusterName
2525
if clusterName == "" {
2626
clusterName = talosConfig.Context
@@ -33,12 +33,16 @@ func BackupEncryptedSnapshot(ctx context.Context, serviceConfig *config.ServiceC
3333

3434
defer util.CleanupFile(snapshotPath)
3535

36-
encryptedFileName, err := encryption.EncryptFile(snapshotPath, serviceConfig.AgeX25519PublicKey)
37-
if err != nil {
38-
return fmt.Errorf("failed to encrypt etcd snapshot: %w", err)
39-
}
36+
if !disableEncryption {
37+
encryptedFileName, encryptionErr := encryption.EncryptFile(snapshotPath, serviceConfig.AgeX25519PublicKey)
38+
if encryptionErr != nil {
39+
return fmt.Errorf("failed to encrypt etcd snapshot: %w", encryptionErr)
40+
}
41+
42+
defer util.CleanupFile(encryptedFileName)
4043

41-
defer util.CleanupFile(encryptedFileName)
44+
snapshotPath = encryptedFileName
45+
}
4246

4347
client, err := s3.CreateClientWithCustomEndpoint(ctx, serviceConfig)
4448
if err != nil {
@@ -54,9 +58,15 @@ func BackupEncryptedSnapshot(ctx context.Context, serviceConfig *config.ServiceC
5458
s3Prefix = clusterName
5559
}
5660

57-
err = s3.PushSnapshot(ctx, s3Info, client, s3Prefix, encryptedFileName)
61+
err = s3.PushSnapshot(ctx, s3Info, client, s3Prefix, snapshotPath)
5862
if err != nil {
59-
return fmt.Errorf("failed to push encrypted snapshot: %w", err)
63+
snapshotType := "snapshot"
64+
65+
if !disableEncryption {
66+
snapshotType = "encrypted snapshot"
67+
}
68+
69+
return fmt.Errorf("failed to push %s: %w", snapshotType, err)
6070
}
6171

6272
return nil

internal/integration/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func cleanup(pool *dockertest.Pool, resources ...*dockertest.Resource) error {
376376
func (suite *integrationTestSuite) TestBackupEncryptedSnapshot() {
377377
// when
378378
suite.Require().Nil(
379-
service.BackupEncryptedSnapshot(suite.ctx, &suite.serviceConfig, suite.talosConfig, suite.talosClient),
379+
service.BackupSnapshot(suite.ctx, &suite.serviceConfig, suite.talosConfig, suite.talosClient, false),
380380
)
381381

382382
// then

pkg/config/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type ServiceConfig struct {
1818
ClusterName string `yaml:"clusterName"`
1919
AgeX25519PublicKey string `yaml:"ageX25519PublicKey"`
2020
UsePathStyle bool `yaml:"usePathStyle"`
21+
DisableEncryption bool `yaml:"disableEncryption"`
2122
}
2223

2324
const (
@@ -27,6 +28,7 @@ const (
2728
s3PrefixEnvVar = "S3_PREFIX"
2829
clusterNameEnvVar = "CLUSTER_NAME"
2930
usePathStyleEnvVar = "USE_PATH_STYLE"
31+
disableEncryptionEnvVar = "DISABLE_ENCRYPTION"
3032
ageX25519PublicKeyEnvVar = "AGE_X25519_PUBLIC_KEY"
3133
)
3234

@@ -39,6 +41,7 @@ func GetServiceConfig() *ServiceConfig {
3941
S3Prefix: os.Getenv(s3PrefixEnvVar),
4042
ClusterName: os.Getenv(clusterNameEnvVar),
4143
UsePathStyle: os.Getenv(usePathStyleEnvVar) == "false",
44+
DisableEncryption: os.Getenv(disableEncryptionEnvVar) == "true",
4245
AgeX25519PublicKey: os.Getenv(ageX25519PublicKeyEnvVar),
4346
}
4447
}

0 commit comments

Comments
 (0)