Skip to content

Commit 6533da4

Browse files
committed
add support for server side s3 managed encryption
1 parent eba901c commit 6533da4

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ Possible JSON properties:
8585
- `unprotected_metrics`: optional, disable HTTP basic auth protection for Prometheus metrics endpoint
8686
- `s3.service_label`: optional, defines which service label backman will look for to find the S3-compatible object storage
8787
- `s3.bucket_name`: optional, bucket to use on S3 storage, backman will use service-instance/binding-name if not configured
88-
- `s3.encryption_key`: optional, defines the key which will be used to encrypt and decrypt backups as they are stored on the S3 can also be passed as an environment variable with the name `BACKMAN_ENCRYPTION_KEY`
88+
- `s3.encryption_key`: optional, defines the key which will be used to encrypt and decrypt backups as they are stored on the S3 can also be passed as an environment variable with the name `BACKMAN_ENCRYPTION_KEY`. This is done at the client-side
89+
- `s3.server_side_encryption`: optional, use s3 managed server side encryption (only possible value for now is "S3")
8990
- `services.<service-instance>.schedule`: optional, defines cron schedule for running backups
9091
- `services.<service-instance>.timeout`: optional, backman will abort a running backup/restore if timeout is exceeded
9192
- `services.<service-instance>.retention.days`: optional, specifies how long backman will keep backups on S3 at maximum for this service instance

config/config.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"encoding/json"
55
"errors"
6+
"github.com/minio/minio-go/v6/pkg/encrypt"
67
"io/ioutil"
78
"log"
89
"os"
@@ -34,6 +35,7 @@ type S3Config struct {
3435
ServiceName string `json:"service_name"`
3536
BucketName string `json:"bucket_name"`
3637
EncryptionKey string `json:"encryption_key"`
38+
ServerSideEncryption string `json:"server_side_encryption"`
3739
}
3840

3941
type ServiceConfig struct {
@@ -143,6 +145,12 @@ func Get() *Config {
143145
if len(envConfig.S3.EncryptionKey) > 0 {
144146
config.S3.EncryptionKey = envConfig.S3.EncryptionKey
145147
}
148+
if len(envConfig.S3.ServerSideEncryption) > 0 {
149+
if envConfig.S3.ServerSideEncryption != encrypt.S3 {
150+
log.Fatalln("only S3 mananged encryption(SSE-S3) is supported for now")
151+
}
152+
config.S3.ServerSideEncryption = envConfig.S3.ServerSideEncryption
153+
}
146154
for serviceName, serviceConfig := range envConfig.Services {
147155
mergedServiceConfig := config.Services[serviceName]
148156
if len(serviceConfig.Schedule) > 0 {
@@ -192,4 +200,4 @@ func Get() *Config {
192200
}
193201
})
194202
return &config
195-
}
203+
}

s3/objects.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/md5"
66
"encoding/hex"
7+
"github.com/minio/minio-go/v6/pkg/encrypt"
78
"io"
89
"io/ioutil"
910
"sort"
@@ -57,7 +58,12 @@ func (s *Client) UploadWithContext(ctx context.Context, object string, reader io
5758
}
5859
}
5960

60-
n, err := s.Client.PutObjectWithContext(ctx, s.BucketName, object, uploadReader, size, minio.PutObjectOptions{ContentType: "application/gzip"})
61+
putOptions := minio.PutObjectOptions{ContentType: "application/gzip"}
62+
if len(config.Get().S3.ServerSideEncryption) != 0 {
63+
putOptions.ServerSideEncryption = encrypt.NewSSE()
64+
}
65+
66+
n, err := s.Client.PutObjectWithContext(ctx, s.BucketName, object, uploadReader, size, putOptions)
6167
if err != nil {
6268
return err
6369
}
@@ -80,6 +86,10 @@ func (s *Client) Download(object string) (io.Reader, error) {
8086

8187
func (s *Client) DownloadWithContext(ctx context.Context, object string) (io.ReadCloser, error) {
8288
log.Debugf("download S3 object [%s]", object)
89+
getOptions := minio.GetObjectOptions{}
90+
if len(config.Get().S3.ServerSideEncryption) != 0 {
91+
getOptions.ServerSideEncryption = encrypt.NewSSE()
92+
}
8393
reader, err := s.Client.GetObjectWithContext(ctx, s.BucketName, object, minio.GetObjectOptions{})
8494
if err != nil {
8595
return nil, err

0 commit comments

Comments
 (0)