-
Notifications
You must be signed in to change notification settings - Fork 274
/
encryption_config_generator.go
68 lines (59 loc) 路 1.95 KB
/
encryption_config_generator.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
67
68
package common
import (
"fmt"
"strings"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
config "k8s.io/apiserver/pkg/apis/config/v1"
"sigs.k8s.io/yaml"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)
const (
encryptionConfigurationKind = "EncryptionConfiguration"
encryptionProviderVersion = "v1"
encryptionProviderNamePrefix = "aws-encryption-provider"
)
var identityProvider = config.ProviderConfiguration{
Identity: &config.IdentityConfiguration{},
}
// GenerateKMSEncryptionConfiguration takes a list of the EtcdEncryption configs and generates the corresponding Kubernetes EncryptionConfig.
func GenerateKMSEncryptionConfiguration(confs *[]v1alpha1.EtcdEncryption) (string, error) {
if confs == nil || len(*confs) == 0 {
return "", nil
}
encryptionConf := &config.EncryptionConfiguration{
TypeMeta: v1.TypeMeta{
APIVersion: config.SchemeGroupVersion.Identifier(),
Kind: encryptionConfigurationKind,
},
}
resourceConfigs := make([]config.ResourceConfiguration, 0, len(*confs))
for _, conf := range *confs {
providers := []config.ProviderConfiguration{}
for _, provider := range conf.Providers {
provider := config.ProviderConfiguration{
KMS: &config.KMSConfiguration{
APIVersion: encryptionProviderVersion,
Name: provider.KMS.Name,
Endpoint: provider.KMS.SocketListenAddress,
CacheSize: provider.KMS.CacheSize,
Timeout: &v1.Duration{
Duration: provider.KMS.Timeout.Duration,
},
},
}
providers = append(providers, provider)
}
providers = append(providers, identityProvider)
resourceConfig := config.ResourceConfiguration{
Resources: conf.Resources,
Providers: providers,
}
resourceConfigs = append(resourceConfigs, resourceConfig)
}
encryptionConf.Resources = resourceConfigs
marshaledConf, err := yaml.Marshal(encryptionConf)
if err != nil {
return "", fmt.Errorf("marshaling encryption config: %v", err)
}
return strings.Trim(string(marshaledConf), "\n"), nil
}