Skip to content

Commit a488d4e

Browse files
authored
Support Map of SecretKeyReferences (#604)
Issue #, if available: Description of changes: - Add Test case for mapping string to secrets references - Update CleanGoType to update value type of map to SecretKeyReference if is_secret set. - Add tests validating SetSDK() for map of secrets references By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent e675923 commit a488d4e

File tree

4 files changed

+406
-0
lines changed

4 files changed

+406
-0
lines changed

pkg/generate/code/set_sdk_test.go

Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package code_test
1515

1616
import (
17+
"fmt"
1718
"testing"
1819

1920
"github.com/stretchr/testify/assert"
@@ -4326,3 +4327,328 @@ func TestSetSDK_WAFv2_RuleGroup_Create(t *testing.T) {
43264327
code.SetSDK(crd.Config(), crd, model.OpTypeCreate, "r.ko", "res", 1),
43274328
)
43284329
}
4330+
4331+
func TestSetSDK_Lambda_Function_EnvironmentVariable_MapOfSecrets_Create(t *testing.T) {
4332+
assert := assert.New(t)
4333+
require := require.New(t)
4334+
4335+
lambdaModel := testutil.NewModelForServiceWithOptions(t, "lambda", &testutil.TestingModelOptions{
4336+
GeneratorConfigFile: "generator-environment-vars-secret.yaml",
4337+
})
4338+
4339+
crd := testutil.GetCRDByName(t, lambdaModel, "Function")
4340+
require.NotNil(crd)
4341+
4342+
expected := `
4343+
if r.ko.Spec.Code != nil {
4344+
f0 := &svcsdktypes.FunctionCode{}
4345+
if r.ko.Spec.Code.ImageURI != nil {
4346+
f0.ImageUri = r.ko.Spec.Code.ImageURI
4347+
}
4348+
if r.ko.Spec.Code.S3Bucket != nil {
4349+
f0.S3Bucket = r.ko.Spec.Code.S3Bucket
4350+
}
4351+
if r.ko.Spec.Code.S3Key != nil {
4352+
f0.S3Key = r.ko.Spec.Code.S3Key
4353+
}
4354+
if r.ko.Spec.Code.S3ObjectVersion != nil {
4355+
f0.S3ObjectVersion = r.ko.Spec.Code.S3ObjectVersion
4356+
}
4357+
if r.ko.Spec.Code.ZipFile != nil {
4358+
f0.ZipFile = r.ko.Spec.Code.ZipFile
4359+
}
4360+
res.Code = f0
4361+
}
4362+
if r.ko.Spec.CodeSigningConfigARN != nil {
4363+
res.CodeSigningConfigArn = r.ko.Spec.CodeSigningConfigARN
4364+
}
4365+
if r.ko.Spec.DeadLetterConfig != nil {
4366+
f2 := &svcsdktypes.DeadLetterConfig{}
4367+
if r.ko.Spec.DeadLetterConfig.TargetARN != nil {
4368+
f2.TargetArn = r.ko.Spec.DeadLetterConfig.TargetARN
4369+
}
4370+
res.DeadLetterConfig = f2
4371+
}
4372+
if r.ko.Spec.Description != nil {
4373+
res.Description = r.ko.Spec.Description
4374+
}
4375+
if r.ko.Spec.Environment != nil {
4376+
f4 := &svcsdktypes.Environment{}
4377+
if r.ko.Spec.Environment.Variables != nil {
4378+
f4f0 := map[string]string{}
4379+
for f4f0key, f4f0valiter := range r.ko.Spec.Environment.Variables {
4380+
var f4f0val string
4381+
if f4f0valiter != nil {
4382+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, f4f0valiter)
4383+
if err != nil {
4384+
return nil, ackrequeue.Needed(err)
4385+
}
4386+
if tmpSecret != "" {
4387+
f4f0val = tmpSecret
4388+
}
4389+
}
4390+
f4f0[f4f0key] = f4f0val
4391+
}
4392+
f4.Variables = f4f0
4393+
}
4394+
res.Environment = f4
4395+
}
4396+
if r.ko.Spec.FileSystemConfigs != nil {
4397+
f5 := []svcsdktypes.FileSystemConfig{}
4398+
for _, f5iter := range r.ko.Spec.FileSystemConfigs {
4399+
f5elem := &svcsdktypes.FileSystemConfig{}
4400+
if f5iter.ARN != nil {
4401+
f5elem.Arn = f5iter.ARN
4402+
}
4403+
if f5iter.LocalMountPath != nil {
4404+
f5elem.LocalMountPath = f5iter.LocalMountPath
4405+
}
4406+
f5 = append(f5, *f5elem)
4407+
}
4408+
res.FileSystemConfigs = f5
4409+
}
4410+
if r.ko.Spec.FunctionName != nil {
4411+
res.FunctionName = r.ko.Spec.FunctionName
4412+
}
4413+
if r.ko.Spec.Handler != nil {
4414+
res.Handler = r.ko.Spec.Handler
4415+
}
4416+
if r.ko.Spec.ImageConfig != nil {
4417+
f8 := &svcsdktypes.ImageConfig{}
4418+
if r.ko.Spec.ImageConfig.Command != nil {
4419+
f8.Command = aws.ToStringSlice(r.ko.Spec.ImageConfig.Command)
4420+
}
4421+
if r.ko.Spec.ImageConfig.EntryPoint != nil {
4422+
f8.EntryPoint = aws.ToStringSlice(r.ko.Spec.ImageConfig.EntryPoint)
4423+
}
4424+
if r.ko.Spec.ImageConfig.WorkingDirectory != nil {
4425+
f8.WorkingDirectory = r.ko.Spec.ImageConfig.WorkingDirectory
4426+
}
4427+
res.ImageConfig = f8
4428+
}
4429+
if r.ko.Spec.KMSKeyARN != nil {
4430+
res.KMSKeyArn = r.ko.Spec.KMSKeyARN
4431+
}
4432+
if r.ko.Spec.Layers != nil {
4433+
res.Layers = aws.ToStringSlice(r.ko.Spec.Layers)
4434+
}
4435+
if r.ko.Spec.MemorySize != nil {
4436+
memorySizeCopy0 := *r.ko.Spec.MemorySize
4437+
if memorySizeCopy0 > math.MaxInt32 || memorySizeCopy0 < math.MinInt32 {
4438+
return nil, fmt.Errorf("error: field MemorySize is of type int32")
4439+
}
4440+
memorySizeCopy := int32(memorySizeCopy0)
4441+
res.MemorySize = &memorySizeCopy
4442+
}
4443+
if r.ko.Spec.PackageType != nil {
4444+
res.PackageType = svcsdktypes.PackageType(*r.ko.Spec.PackageType)
4445+
}
4446+
if r.ko.Spec.Publish != nil {
4447+
res.Publish = *r.ko.Spec.Publish
4448+
}
4449+
if r.ko.Spec.Role != nil {
4450+
res.Role = r.ko.Spec.Role
4451+
}
4452+
if r.ko.Spec.Runtime != nil {
4453+
res.Runtime = svcsdktypes.Runtime(*r.ko.Spec.Runtime)
4454+
}
4455+
if r.ko.Spec.Tags != nil {
4456+
res.Tags = aws.ToStringMap(r.ko.Spec.Tags)
4457+
}
4458+
if r.ko.Spec.Timeout != nil {
4459+
timeoutCopy0 := *r.ko.Spec.Timeout
4460+
if timeoutCopy0 > math.MaxInt32 || timeoutCopy0 < math.MinInt32 {
4461+
return nil, fmt.Errorf("error: field Timeout is of type int32")
4462+
}
4463+
timeoutCopy := int32(timeoutCopy0)
4464+
res.Timeout = &timeoutCopy
4465+
}
4466+
if r.ko.Spec.TracingConfig != nil {
4467+
f18 := &svcsdktypes.TracingConfig{}
4468+
if r.ko.Spec.TracingConfig.Mode != nil {
4469+
f18.Mode = svcsdktypes.TracingMode(*r.ko.Spec.TracingConfig.Mode)
4470+
}
4471+
res.TracingConfig = f18
4472+
}
4473+
if r.ko.Spec.VPCConfig != nil {
4474+
f19 := &svcsdktypes.VpcConfig{}
4475+
if r.ko.Spec.VPCConfig.SecurityGroupIDs != nil {
4476+
f19.SecurityGroupIds = aws.ToStringSlice(r.ko.Spec.VPCConfig.SecurityGroupIDs)
4477+
}
4478+
if r.ko.Spec.VPCConfig.SubnetIDs != nil {
4479+
f19.SubnetIds = aws.ToStringSlice(r.ko.Spec.VPCConfig.SubnetIDs)
4480+
}
4481+
res.VpcConfig = f19
4482+
}
4483+
`
4484+
actual := code.SetSDK(crd.Config(), crd, model.OpTypeCreate, "r.ko", "res", 1)
4485+
fmt.Print(actual)
4486+
4487+
assert.Equal(expected, actual)
4488+
}
4489+
4490+
func TestSetSDK_Lambda_Function_EnvironmentVariable_MapOfSecrets_Update(t *testing.T) {
4491+
assert := assert.New(t)
4492+
require := require.New(t)
4493+
4494+
lambdaModel := testutil.NewModelForServiceWithOptions(t, "lambda", &testutil.TestingModelOptions{
4495+
GeneratorConfigFile: "generator-environment-vars-secret.yaml",
4496+
})
4497+
4498+
crd := testutil.GetCRDByName(t, lambdaModel, "Function")
4499+
require.NotNil(crd)
4500+
4501+
expected := `
4502+
if r.ko.Spec.DeadLetterConfig != nil {
4503+
f0 := &svcsdktypes.DeadLetterConfig{}
4504+
if r.ko.Spec.DeadLetterConfig.TargetARN != nil {
4505+
f0.TargetArn = r.ko.Spec.DeadLetterConfig.TargetARN
4506+
}
4507+
res.DeadLetterConfig = f0
4508+
}
4509+
if r.ko.Spec.Description != nil {
4510+
res.Description = r.ko.Spec.Description
4511+
}
4512+
if r.ko.Spec.Environment != nil {
4513+
f2 := &svcsdktypes.Environment{}
4514+
if r.ko.Spec.Environment.Variables != nil {
4515+
f2f0 := map[string]string{}
4516+
for f2f0key, f2f0valiter := range r.ko.Spec.Environment.Variables {
4517+
var f2f0val string
4518+
if f2f0valiter != nil {
4519+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, f2f0valiter)
4520+
if err != nil {
4521+
return nil, ackrequeue.Needed(err)
4522+
}
4523+
if tmpSecret != "" {
4524+
f2f0val = tmpSecret
4525+
}
4526+
}
4527+
f2f0[f2f0key] = f2f0val
4528+
}
4529+
f2.Variables = f2f0
4530+
}
4531+
res.Environment = f2
4532+
}
4533+
if r.ko.Status.EphemeralStorage != nil {
4534+
f3 := &svcsdktypes.EphemeralStorage{}
4535+
if r.ko.Status.EphemeralStorage.Size != nil {
4536+
sizeCopy0 := *r.ko.Status.EphemeralStorage.Size
4537+
if sizeCopy0 > math.MaxInt32 || sizeCopy0 < math.MinInt32 {
4538+
return nil, fmt.Errorf("error: field Size is of type int32")
4539+
}
4540+
sizeCopy := int32(sizeCopy0)
4541+
f3.Size = &sizeCopy
4542+
}
4543+
res.EphemeralStorage = f3
4544+
}
4545+
if r.ko.Spec.FileSystemConfigs != nil {
4546+
f4 := []svcsdktypes.FileSystemConfig{}
4547+
for _, f4iter := range r.ko.Spec.FileSystemConfigs {
4548+
f4elem := &svcsdktypes.FileSystemConfig{}
4549+
if f4iter.ARN != nil {
4550+
f4elem.Arn = f4iter.ARN
4551+
}
4552+
if f4iter.LocalMountPath != nil {
4553+
f4elem.LocalMountPath = f4iter.LocalMountPath
4554+
}
4555+
f4 = append(f4, *f4elem)
4556+
}
4557+
res.FileSystemConfigs = f4
4558+
}
4559+
if r.ko.Spec.FunctionName != nil {
4560+
res.FunctionName = r.ko.Spec.FunctionName
4561+
}
4562+
if r.ko.Spec.Handler != nil {
4563+
res.Handler = r.ko.Spec.Handler
4564+
}
4565+
if r.ko.Spec.ImageConfig != nil {
4566+
f7 := &svcsdktypes.ImageConfig{}
4567+
if r.ko.Spec.ImageConfig.Command != nil {
4568+
f7.Command = aws.ToStringSlice(r.ko.Spec.ImageConfig.Command)
4569+
}
4570+
if r.ko.Spec.ImageConfig.EntryPoint != nil {
4571+
f7.EntryPoint = aws.ToStringSlice(r.ko.Spec.ImageConfig.EntryPoint)
4572+
}
4573+
if r.ko.Spec.ImageConfig.WorkingDirectory != nil {
4574+
f7.WorkingDirectory = r.ko.Spec.ImageConfig.WorkingDirectory
4575+
}
4576+
res.ImageConfig = f7
4577+
}
4578+
if r.ko.Spec.KMSKeyARN != nil {
4579+
res.KMSKeyArn = r.ko.Spec.KMSKeyARN
4580+
}
4581+
if r.ko.Spec.Layers != nil {
4582+
res.Layers = aws.ToStringSlice(r.ko.Spec.Layers)
4583+
}
4584+
if r.ko.Status.LoggingConfig != nil {
4585+
f10 := &svcsdktypes.LoggingConfig{}
4586+
if r.ko.Status.LoggingConfig.ApplicationLogLevel != nil {
4587+
f10.ApplicationLogLevel = svcsdktypes.ApplicationLogLevel(*r.ko.Status.LoggingConfig.ApplicationLogLevel)
4588+
}
4589+
if r.ko.Status.LoggingConfig.LogFormat != nil {
4590+
f10.LogFormat = svcsdktypes.LogFormat(*r.ko.Status.LoggingConfig.LogFormat)
4591+
}
4592+
if r.ko.Status.LoggingConfig.LogGroup != nil {
4593+
f10.LogGroup = r.ko.Status.LoggingConfig.LogGroup
4594+
}
4595+
if r.ko.Status.LoggingConfig.SystemLogLevel != nil {
4596+
f10.SystemLogLevel = svcsdktypes.SystemLogLevel(*r.ko.Status.LoggingConfig.SystemLogLevel)
4597+
}
4598+
res.LoggingConfig = f10
4599+
}
4600+
if r.ko.Spec.MemorySize != nil {
4601+
memorySizeCopy0 := *r.ko.Spec.MemorySize
4602+
if memorySizeCopy0 > math.MaxInt32 || memorySizeCopy0 < math.MinInt32 {
4603+
return nil, fmt.Errorf("error: field MemorySize is of type int32")
4604+
}
4605+
memorySizeCopy := int32(memorySizeCopy0)
4606+
res.MemorySize = &memorySizeCopy
4607+
}
4608+
if r.ko.Status.RevisionID != nil {
4609+
res.RevisionId = r.ko.Status.RevisionID
4610+
}
4611+
if r.ko.Spec.Role != nil {
4612+
res.Role = r.ko.Spec.Role
4613+
}
4614+
if r.ko.Spec.Runtime != nil {
4615+
res.Runtime = svcsdktypes.Runtime(*r.ko.Spec.Runtime)
4616+
}
4617+
if r.ko.Status.SnapStart != nil {
4618+
f15 := &svcsdktypes.SnapStart{}
4619+
if r.ko.Status.SnapStart.ApplyOn != nil {
4620+
f15.ApplyOn = svcsdktypes.SnapStartApplyOn(*r.ko.Status.SnapStart.ApplyOn)
4621+
}
4622+
res.SnapStart = f15
4623+
}
4624+
if r.ko.Spec.Timeout != nil {
4625+
timeoutCopy0 := *r.ko.Spec.Timeout
4626+
if timeoutCopy0 > math.MaxInt32 || timeoutCopy0 < math.MinInt32 {
4627+
return nil, fmt.Errorf("error: field Timeout is of type int32")
4628+
}
4629+
timeoutCopy := int32(timeoutCopy0)
4630+
res.Timeout = &timeoutCopy
4631+
}
4632+
if r.ko.Spec.TracingConfig != nil {
4633+
f17 := &svcsdktypes.TracingConfig{}
4634+
if r.ko.Spec.TracingConfig.Mode != nil {
4635+
f17.Mode = svcsdktypes.TracingMode(*r.ko.Spec.TracingConfig.Mode)
4636+
}
4637+
res.TracingConfig = f17
4638+
}
4639+
if r.ko.Spec.VPCConfig != nil {
4640+
f18 := &svcsdktypes.VpcConfig{}
4641+
if r.ko.Spec.VPCConfig.SecurityGroupIDs != nil {
4642+
f18.SecurityGroupIds = aws.ToStringSlice(r.ko.Spec.VPCConfig.SecurityGroupIDs)
4643+
}
4644+
if r.ko.Spec.VPCConfig.SubnetIDs != nil {
4645+
f18.SubnetIds = aws.ToStringSlice(r.ko.Spec.VPCConfig.SubnetIDs)
4646+
}
4647+
res.VpcConfig = f18
4648+
}
4649+
`
4650+
4651+
actual := code.SetSDK(crd.Config(), crd, model.OpTypeUpdate, "r.ko", "res", 1)
4652+
4653+
assert.Equal(expected, actual)
4654+
}

pkg/model/model_lambda_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,23 @@ func TestLambda_customNestedFields_Status_Depth3(t *testing.T) {
183183
assert.Contains(ErrorField.MemberFields, "New")
184184
assert.Contains(ErrorField.ShapeRef.Shape.MemberRefs, "New")
185185
}
186+
187+
func TestLambda_EnvironmentVariables_MapOfSecrets(t *testing.T) {
188+
assert := assert.New(t)
189+
require := require.New(t)
190+
191+
model := testutil.NewModelForServiceWithOptions(t, "lambda", &testutil.TestingModelOptions{
192+
GeneratorConfigFile: "generator-environment-vars-secret.yaml",
193+
})
194+
crds, err := model.GetCRDs()
195+
require.Nil(err)
196+
197+
crd := getCRDByName("Function", crds)
198+
199+
variablesField := crd.SpecFields["Environment"].MemberFields["Variables"]
200+
require.NotNil(variablesField)
201+
202+
assert.Equal("map[string]*ackv1alpha1.SecretKeyReference", variablesField.GoType)
203+
assert.Equal("map[string]*ackv1alpha1.SecretKeyReference", variablesField.GoTypeElem)
204+
assert.Equal("map[string]*ackv1alpha1.SecretKeyReference", variablesField.GoTypeWithPkgName)
205+
}

pkg/model/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ func CleanGoType(
5858

5959
gt = "[]" + mgt
6060
gtwp = "[]" + mgtwp
61+
} else if shape.Type == "map" && fieldCfg != nil && fieldCfg.IsSecret {
62+
_, _, vgtwp := CleanGoType(api, cfg, shape.ValueRef.Shape, fieldCfg)
63+
kgte, _, _ := CleanGoType(api, cfg, shape.KeyRef.Shape, nil)
64+
65+
gte = "map[" + kgte + "]" + vgtwp
66+
gt = "map[" + kgte + "]" + vgtwp
67+
gtwp = "map[" + kgte + "]" + vgtwp
68+
69+
return gte, gt, gtwp
6170
} else if shape.Type == "timestamp" {
6271
// time.Time needs to be converted to apimachinery/metav1.Time
6372
// otherwise there is no DeepCopy support

0 commit comments

Comments
 (0)