Skip to content

Commit 04f18db

Browse files
authored
fix: aws registry encryptionConfiguration to be applied (#827)
Modified the CreateRepositoryInput structure for ECR registry according to the configuration. Added the a default value for the EncryptionType and validation for the KmsKey key. fixes #598
1 parent e817e04 commit 04f18db

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

pkg/config/config.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ type ImageScanningConfiguration struct {
9797
}
9898

9999
type EncryptionConfiguration struct {
100-
EncryptionType string `yaml:"encryptionType"`
100+
EncryptionType string `yaml:"encryptionType" validate:"oneof=KMS AES256"`
101101
KmsKey string `yaml:"kmsKey"`
102102
}
103103

@@ -140,6 +140,9 @@ func CheckRegistryConfiguration(r Registry) error {
140140
if r.AWS.AccountID == "" {
141141
return errorWithType(`requires a field "accountdId"`)
142142
}
143+
if r.AWS.ECROptions.EncryptionConfiguration.EncryptionType == "KMS" && r.AWS.ECROptions.EncryptionConfiguration.KmsKey == "" {
144+
return errorWithType(`requires a field "kmsKey" if encryptionType is set to "KMS"`)
145+
}
143146
case types.RegistryGCP:
144147
if r.GCP.Location == "" {
145148
return errorWithType(`requires a field "location"`)
@@ -160,4 +163,5 @@ func SetViperDefaults(v *viper.Viper) {
160163
v.SetDefault("Target.Type", "aws")
161164
v.SetDefault("Target.AWS.ECROptions.ImageScanningConfiguration.ImageScanOnPush", true)
162165
v.SetDefault("Target.AWS.ECROptions.ImageTagMutability", "MUTABLE")
166+
v.SetDefault("Target.AWS.ECROptions.EncryptionConfiguration.EncryptionType", "AES256")
163167
}

pkg/config/config_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func TestConfigParses(t *testing.T) {
2929
ImageScanningConfiguration: ImageScanningConfiguration{
3030
ImageScanOnPush: true,
3131
},
32+
EncryptionConfiguration: EncryptionConfiguration{
33+
EncryptionType: "AES256",
34+
},
3235
},
3336
},
3437
},
@@ -51,6 +54,9 @@ source:
5154
ImageScanningConfiguration: ImageScanningConfiguration{
5255
ImageScanOnPush: true,
5356
},
57+
EncryptionConfiguration: EncryptionConfiguration{
58+
EncryptionType: "AES256",
59+
},
5460
},
5561
},
5662
},
@@ -90,6 +96,9 @@ target:
9096
ImageScanningConfiguration: ImageScanningConfiguration{
9197
ImageScanOnPush: true,
9298
},
99+
EncryptionConfiguration: EncryptionConfiguration{
100+
EncryptionType: "AES256",
101+
},
93102
Tags: []Tag{
94103
{
95104
Key: "CreatedBy",
@@ -128,6 +137,9 @@ source:
128137
ImageScanningConfiguration: ImageScanningConfiguration{
129138
ImageScanOnPush: true,
130139
},
140+
EncryptionConfiguration: EncryptionConfiguration{
141+
EncryptionType: "AES256",
142+
},
131143
},
132144
},
133145
},
@@ -176,6 +188,9 @@ target:
176188
ImageScanningConfiguration: ImageScanningConfiguration{
177189
ImageScanOnPush: true,
178190
},
191+
EncryptionConfiguration: EncryptionConfiguration{
192+
EncryptionType: "AES256",
193+
},
179194
ImageTagMutability: "MUTABLE",
180195
Tags: []Tag{
181196
{

pkg/registry/ecr.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,17 @@ func (e *ECRClient) CreateRepository(ctx context.Context, name string) error {
104104

105105
log.Ctx(ctx).Debug().Str("repository", name).Msg("create repository")
106106

107+
encryptionConfiguration := &ecr.EncryptionConfiguration{
108+
EncryptionType: aws.String(e.options.EncryptionConfiguration.EncryptionType),
109+
}
110+
111+
if e.options.EncryptionConfiguration.EncryptionType == "KMS" {
112+
encryptionConfiguration.KmsKey = aws.String(e.options.EncryptionConfiguration.KmsKey)
113+
}
114+
107115
_, err := e.client.CreateRepositoryWithContext(ctx, &ecr.CreateRepositoryInput{
108-
RepositoryName: aws.String(name),
116+
RepositoryName: aws.String(name),
117+
EncryptionConfiguration: encryptionConfiguration,
109118
ImageScanningConfiguration: &ecr.ImageScanningConfiguration{
110119
ScanOnPush: aws.Bool(e.options.ImageScanningConfiguration.ImageScanOnPush),
111120
},
@@ -326,6 +335,7 @@ func NewMockECRClient(ecrClient ecriface.ECRAPI, region string, ecrDomain string
326335
options: config.ECROptions{
327336
ImageTagMutability: "MUTABLE",
328337
ImageScanningConfiguration: config.ImageScanningConfiguration{ImageScanOnPush: true},
338+
EncryptionConfiguration: config.EncryptionConfiguration{EncryptionType: "AES256"},
329339
Tags: []config.Tag{{Key: "CreatedBy", Value: "k8s-image-swapper"}, {Key: "AnotherTag", Value: "another-tag"}},
330340
},
331341
}

pkg/webhook/image_swapper_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ func TestImageSwapper_Mutate(t *testing.T) {
245245
ImageScanningConfiguration: &ecr.ImageScanningConfiguration{
246246
ScanOnPush: aws.Bool(true),
247247
},
248+
EncryptionConfiguration: &ecr.EncryptionConfiguration{
249+
EncryptionType: aws.String("AES256"),
250+
},
248251
ImageTagMutability: aws.String("MUTABLE"),
249252
RepositoryName: aws.String(expectedRepository),
250253
RegistryId: aws.String("123456789"),
@@ -307,6 +310,9 @@ func TestImageSwapper_MutateWithImagePullSecrets(t *testing.T) {
307310
ImageScanningConfiguration: &ecr.ImageScanningConfiguration{
308311
ScanOnPush: aws.Bool(true),
309312
},
313+
EncryptionConfiguration: &ecr.EncryptionConfiguration{
314+
EncryptionType: aws.String("AES256"),
315+
},
310316
ImageTagMutability: aws.String("MUTABLE"),
311317
RegistryId: aws.String("123456789"),
312318
RepositoryName: aws.String("docker.io/library/nginx"),

0 commit comments

Comments
 (0)