Skip to content

Commit ff70faa

Browse files
Add validation for encryptionKeyCRN for VPC Machine spec
1 parent b05203c commit ff70faa

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

internal/webhooks/common.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package webhooks
1818

1919
import (
2020
"fmt"
21+
"regexp"
2122
"strconv"
2223

2324
"k8s.io/apimachinery/pkg/util/intstr"
@@ -26,6 +27,9 @@ import (
2627
infrav1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
2728
)
2829

30+
// IBM Cloud CRN validation regex.
31+
var crnRegex = regexp.MustCompile(`^crn:v[0-9]+:[a-z0-9-]+:[a-z0-9-]+:[a-z0-9-]+:[a-z0-9-]*:([a-z]\/[a-z0-9-]+)?:[a-z0-9-]*:[a-z0-9-]*:[a-zA-Z0-9-_\.\/]*$`)
32+
2933
func defaultIBMPowerVSMachineSpec(spec *infrav1.IBMPowerVSMachineSpec) {
3034
if spec.MemoryGiB == 0 {
3135
spec.MemoryGiB = 2
@@ -99,6 +103,9 @@ func validateVolumes(spec infrav1.IBMVPCMachineSpec) field.ErrorList {
99103
if spec.AdditionalVolumes[i].Iops != 0 && spec.AdditionalVolumes[i].Profile != customProfile {
100104
allErrs = append(allErrs, field.Invalid(field.NewPath(fmt.Sprintf("spec.AdditionalVolumes[%d]", i)), spec, "iops applicable only to volumes using a profile of type `custom`"))
101105
}
106+
if spec.AdditionalVolumes[i].EncryptionKeyCRN != "" && !isValidCRN(spec.AdditionalVolumes[i].EncryptionKeyCRN) {
107+
allErrs = append(allErrs, field.Invalid(field.NewPath(fmt.Sprintf("spec.AdditionalVolumes[%d]", i)), spec, "encryptionKeyCRN not in proper IBM Cloud CRN format"))
108+
}
102109
}
103110

104111
if spec.BootVolume == nil {
@@ -113,7 +120,15 @@ func validateVolumes(spec infrav1.IBMVPCMachineSpec) field.ErrorList {
113120
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.bootVolume.iops"), spec, "iops applicable only to volumes using a profile of type `custom`"))
114121
}
115122

116-
//TODO: Add validation for the spec.BootVolume.EncryptionKeyCRN to ensure its in proper IBM Cloud CRN format
123+
// Validate spec.BootVolume.EncryptionKeyCRN to ensure its in proper IBM Cloud CRN format
124+
if spec.BootVolume.EncryptionKeyCRN != "" && !isValidCRN(spec.BootVolume.EncryptionKeyCRN) {
125+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.bootVolume.encryptionKeyCRN"), spec, "encryptionKeyCRN not in proper IBM Cloud CRN format"))
126+
}
117127

118128
return allErrs
119129
}
130+
131+
// isValidCRN checks whether the provided string is a valid IBM Cloud CRN.
132+
func isValidCRN(crn string) bool {
133+
return crnRegex.MatchString(crn)
134+
}

internal/webhooks/common_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ func Test_validateVolumes(t *testing.T) {
160160
},
161161
wantError: false,
162162
},
163+
{
164+
name: "Valid encryptionKeyCRN",
165+
spec: infrav1.IBMVPCMachineSpec{
166+
BootVolume: &infrav1.VPCVolume{SizeGiB: 20, EncryptionKeyCRN: "crn:v1:bluemix:public:kms:us-south:a/aa2432b1fa4d4ace891e9b80fc104e34:e4a29d1a-2ef0-42a6-8fd2-350deb1c647e:key:5437653b-c4b1-447f-9646-b2a2a4cd6179"},
167+
},
168+
wantError: false,
169+
},
170+
{
171+
name: "Invalid encryptionKeyCRN",
172+
spec: infrav1.IBMVPCMachineSpec{
173+
BootVolume: &infrav1.VPCVolume{EncryptionKeyCRN: "invalid-crn-format"},
174+
},
175+
wantError: true,
176+
},
163177
}
164178
for _, tt := range tests {
165179
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)