Skip to content

Commit 6548799

Browse files
committed
feat(secrets-manager): add KMS key options to create and update instance tests
1 parent 3b95e6b commit 6548799

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

internal/cmd/secrets-manager/instance/create/create_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ var testClient = &secretsmanager.APIClient{}
2525
var testProjectId = uuid.NewString()
2626
var testInstanceId = uuid.NewString()
2727

28+
const (
29+
testKmsKeyId = "key-id"
30+
testKmsKeyringId = "keyring-id"
31+
testKmsKeyVersion = int64(1)
32+
testKmsServiceAccountEmail = "my-service-account-1234567@sa.stackit.cloud"
33+
)
34+
2835
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2936
flagValues := map[string]string{
3037
projectIdFlag: testProjectId,
@@ -162,6 +169,24 @@ func TestParseInput(t *testing.T) {
162169
*model.Acls = append(*model.Acls, "1.2.3.4/32")
163170
}),
164171
},
172+
{
173+
description: "kms flags",
174+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
175+
delete(flagValues, aclFlag)
176+
flagValues[kmsKeyIdFlag] = testKmsKeyId
177+
flagValues[kmsKeyringIdFlag] = testKmsKeyringId
178+
flagValues[kmsKeyVersionFlag] = "1"
179+
flagValues[kmsServiceAccountEmailFlag] = testKmsServiceAccountEmail
180+
}),
181+
isValid: true,
182+
expectedModel: fixtureInputModel(func(model *inputModel) {
183+
model.Acls = nil
184+
model.KmsKeyId = utils.Ptr(testKmsKeyId)
185+
model.KmsKeyringId = utils.Ptr(testKmsKeyringId)
186+
model.KmsKeyVersion = utils.Ptr(testKmsKeyVersion)
187+
model.KmsServiceAccountEmail = utils.Ptr(testKmsServiceAccountEmail)
188+
}),
189+
},
165190
{
166191
description: "project id missing",
167192
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
@@ -205,6 +230,28 @@ func TestBuildCreateInstanceRequest(t *testing.T) {
205230
model: fixtureInputModel(),
206231
expectedRequest: fixtureRequest(),
207232
},
233+
{
234+
description: "with kms",
235+
model: fixtureInputModel(func(model *inputModel) {
236+
model.Acls = nil
237+
model.KmsKeyId = utils.Ptr(testKmsKeyId)
238+
model.KmsKeyringId = utils.Ptr(testKmsKeyringId)
239+
model.KmsKeyVersion = utils.Ptr(testKmsKeyVersion)
240+
model.KmsServiceAccountEmail = utils.Ptr(testKmsServiceAccountEmail)
241+
}),
242+
expectedRequest: fixtureRequest(func(request *secretsmanager.ApiCreateInstanceRequest) {
243+
payload := secretsmanager.CreateInstancePayload{
244+
Name: utils.Ptr("example"),
245+
KmsKey: &secretsmanager.KmsKeyPayload{
246+
KeyId: utils.Ptr(testKmsKeyId),
247+
KeyRingId: utils.Ptr(testKmsKeyringId),
248+
KeyVersion: utils.Ptr(testKmsKeyVersion),
249+
ServiceAccountEmail: utils.Ptr(testKmsServiceAccountEmail),
250+
},
251+
}
252+
*request = (*request).CreateInstancePayload(payload)
253+
}),
254+
},
208255
}
209256

210257
for _, tt := range tests {

internal/cmd/secrets-manager/instance/update/update_test.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ var (
3131
testInstanceId = uuid.NewString()
3232
)
3333

34+
const (
35+
testKmsKeyId = "key-id"
36+
testKmsKeyringId = "keyring-id"
37+
testKmsKeyVersion = int64(1)
38+
testKmsServiceAccountEmail = "my-service-account-1234567@sa.stackit.cloud"
39+
)
40+
3441
func fixtureArgValues(mods ...func(argValues []string)) []string {
3542
argValues := []string{
3643
testInstanceId,
@@ -80,6 +87,23 @@ func fixtureRequest(mods ...func(request *secretsmanager.ApiUpdateACLsRequest))
8087
return request
8188
}
8289

90+
func fixtureUpdateInstanceRequest(mods ...func(request *secretsmanager.ApiUpdateInstanceRequest)) secretsmanager.ApiUpdateInstanceRequest {
91+
request := testClient.UpdateInstance(testCtx, testProjectId, testInstanceId)
92+
request = request.UpdateInstancePayload(secretsmanager.UpdateInstancePayload{
93+
KmsKey: &secretsmanager.KmsKeyPayload{
94+
KeyId: utils.Ptr(testKmsKeyId),
95+
KeyRingId: utils.Ptr(testKmsKeyringId),
96+
KeyVersion: utils.Ptr(testKmsKeyVersion),
97+
ServiceAccountEmail: utils.Ptr(testKmsServiceAccountEmail),
98+
},
99+
})
100+
101+
for _, mod := range mods {
102+
mod(&request)
103+
}
104+
return request
105+
}
106+
83107
func TestParseInput(t *testing.T) {
84108
tests := []struct {
85109
description string
@@ -209,6 +233,25 @@ func TestParseInput(t *testing.T) {
209233
)
210234
}),
211235
},
236+
{
237+
description: "kms flags",
238+
argValues: fixtureArgValues(),
239+
flagValues: map[string]string{
240+
projectIdFlag: testProjectId,
241+
kmsKeyIdFlag: testKmsKeyId,
242+
kmsKeyringIdFlag: testKmsKeyringId,
243+
kmsKeyVersionFlag: "1",
244+
kmsServiceAccountEmailFlag: testKmsServiceAccountEmail,
245+
},
246+
isValid: true,
247+
expectedModel: fixtureInputModel(func(model *inputModel) {
248+
model.Acls = nil
249+
model.KmsKeyId = utils.Ptr(testKmsKeyId)
250+
model.KmsKeyringId = utils.Ptr(testKmsKeyringId)
251+
model.KmsKeyVersion = utils.Ptr(testKmsKeyVersion)
252+
model.KmsServiceAccountEmail = utils.Ptr(testKmsServiceAccountEmail)
253+
}),
254+
},
212255
}
213256

214257
for _, tt := range tests {
@@ -246,8 +289,12 @@ func TestBuildRequest(t *testing.T) {
246289
for _, tt := range tests {
247290
t.Run(tt.description, func(t *testing.T) {
248291
request := buildRequest(testCtx, tt.model, testClient)
292+
aclRequest, ok := request.(secretsmanager.ApiUpdateACLsRequest)
293+
if !ok {
294+
t.Fatalf("expected ACL update request, got %T", request)
295+
}
249296

250-
diff := cmp.Diff(request, tt.expectedRequest,
297+
diff := cmp.Diff(aclRequest, tt.expectedRequest,
251298
cmp.AllowUnexported(tt.expectedRequest),
252299
cmpopts.EquateComparable(testCtx),
253300
)
@@ -257,3 +304,28 @@ func TestBuildRequest(t *testing.T) {
257304
})
258305
}
259306
}
307+
308+
func TestBuildRequestKms(t *testing.T) {
309+
model := fixtureInputModel(func(model *inputModel) {
310+
model.Acls = nil
311+
model.KmsKeyId = utils.Ptr(testKmsKeyId)
312+
model.KmsKeyringId = utils.Ptr(testKmsKeyringId)
313+
model.KmsKeyVersion = utils.Ptr(testKmsKeyVersion)
314+
model.KmsServiceAccountEmail = utils.Ptr(testKmsServiceAccountEmail)
315+
})
316+
317+
request := buildRequest(testCtx, model, testClient)
318+
updateRequest, ok := request.(secretsmanager.ApiUpdateInstanceRequest)
319+
if !ok {
320+
t.Fatalf("expected instance update request, got %T", request)
321+
}
322+
323+
expectedRequest := fixtureUpdateInstanceRequest()
324+
diff := cmp.Diff(updateRequest, expectedRequest,
325+
cmp.AllowUnexported(expectedRequest),
326+
cmpopts.EquateComparable(testCtx),
327+
)
328+
if diff != "" {
329+
t.Fatalf("Data does not match: %s", diff)
330+
}
331+
}

0 commit comments

Comments
 (0)