-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
sse_config.go
66 lines (54 loc) · 1.74 KB
/
sse_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package aws
import (
"encoding/base64"
"encoding/json"
"github.com/pkg/errors"
bucket_s3 "github.com/grafana/loki/pkg/storage/bucket/s3"
)
const (
sseKMSType = "aws:kms"
sseS3Type = "AES256"
)
// SSEParsedConfig configures server side encryption (SSE)
// struct used internally to configure AWS S3
type SSEParsedConfig struct {
ServerSideEncryption string
KMSKeyID *string
KMSEncryptionContext *string
}
// NewSSEParsedConfig creates a struct to configure server side encryption (SSE)
func NewSSEParsedConfig(cfg bucket_s3.SSEConfig) (*SSEParsedConfig, error) {
switch cfg.Type {
case bucket_s3.SSES3:
return &SSEParsedConfig{
ServerSideEncryption: sseS3Type,
}, nil
case bucket_s3.SSEKMS:
if cfg.KMSKeyID == "" {
return nil, errors.New("KMS key id must be passed when SSE-KMS encryption is selected")
}
parsedKMSEncryptionContext, err := parseKMSEncryptionContext(cfg.KMSEncryptionContext)
if err != nil {
return nil, errors.Wrap(err, "failed to parse KMS encryption context")
}
return &SSEParsedConfig{
ServerSideEncryption: sseKMSType,
KMSKeyID: &cfg.KMSKeyID,
KMSEncryptionContext: parsedKMSEncryptionContext,
}, nil
default:
return nil, errors.New("SSE type is empty or invalid")
}
}
func parseKMSEncryptionContext(kmsEncryptionContext string) (*string, error) {
if kmsEncryptionContext == "" {
return nil, nil
}
// validates if kmsEncryptionContext is a valid JSON
jsonKMSEncryptionContext, err := json.Marshal(json.RawMessage(kmsEncryptionContext))
if err != nil {
return nil, errors.Wrap(err, "failed to marshal KMS encryption context")
}
parsedKMSEncryptionContext := base64.StdEncoding.EncodeToString(jsonKMSEncryptionContext)
return &parsedKMSEncryptionContext, nil
}