Skip to content

Commit 5df1ab7

Browse files
authored
Add validation for vpcID in tgb spec (#3663)
* Add validation for vpcID in tgb spec * Add validation for vpcID in tgb spec * Addressing comments
1 parent 0173b0e commit 5df1ab7

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

webhooks/elbv2/targetgroupbinding_validator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package elbv2
22

33
import (
44
"context"
5+
"regexp"
56
"strings"
67

78
awssdk "github.com/aws/aws-sdk-go/aws"
@@ -20,6 +21,8 @@ import (
2021

2122
const apiPathValidateELBv2TargetGroupBinding = "/validate-elbv2-k8s-aws-v1beta1-targetgroupbinding"
2223

24+
var vpcIDPatternRegex = regexp.MustCompile("^(?:vpc-[0-9a-f]{8}|vpc-[0-9a-f]{17})$")
25+
2326
// NewTargetGroupBindingValidator returns a validator for TargetGroupBinding CRD.
2427
func NewTargetGroupBindingValidator(k8sClient client.Client, elbv2Client services.ELBV2, vpcID string, logger logr.Logger) *targetGroupBindingValidator {
2528
return &targetGroupBindingValidator{
@@ -165,6 +168,9 @@ func (v *targetGroupBindingValidator) checkTargetGroupVpcID(ctx context.Context,
165168
if tgb.Spec.VpcID == "" {
166169
return nil
167170
}
171+
if !vpcIDPatternRegex.MatchString(tgb.Spec.VpcID) {
172+
return errors.Errorf("ValidationError: vpcID %v failed to satisfy constraint: VPC Id must begin with 'vpc-' followed by 8 or 17 lowercase letters (a-f) or numbers.", tgb.Spec.VpcID)
173+
}
168174
vpcID, err := v.getVpcIDFromAWS(ctx, tgb.Spec.TargetGroupARN)
169175
if err != nil {
170176
return errors.Wrap(err, "unable to get target group VpcID")

webhooks/elbv2/targetgroupbinding_validator_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func Test_targetGroupBindingValidator_ValidateCreate(t *testing.T) {
3939
}
4040
instanceTargetType := elbv2api.TargetTypeInstance
4141
ipTargetType := elbv2api.TargetTypeIP
42-
clusterVpcID := "vpcid-02"
42+
clusterVpcID := "vpc-123456ab"
4343
tests := []struct {
4444
name string
4545
fields fields
@@ -264,11 +264,11 @@ func Test_targetGroupBindingValidator_ValidateCreate(t *testing.T) {
264264
TargetGroupARN: "tg-2",
265265
TargetType: &instanceTargetType,
266266
IPAddressType: &targetGroupIPAddressTypeIPv6,
267-
VpcID: "vpcid-01",
267+
VpcID: "vpc-1234567a",
268268
},
269269
},
270270
},
271-
wantErr: errors.New("invalid VpcID vpcid-01 doesnt match VpcID from TargetGroup tg-2"),
271+
wantErr: errors.New("invalid VpcID vpc-1234567a doesnt match VpcID from TargetGroup tg-2"),
272272
},
273273
}
274274
for _, tt := range tests {
@@ -691,27 +691,27 @@ func Test_targetGroupBindingValidator_checkImmutableFields(t *testing.T) {
691691
wantErr: errors.New("TargetGroupBinding update may not change these fields: spec.ipAddressType"),
692692
},
693693
{
694-
name: "VpcID modified from vpc-01 to vpc-02",
694+
name: "VpcID modified from vpc-0aaaaaaa to vpc-0bbbbbbb",
695695
args: args{
696696
tgb: &elbv2api.TargetGroupBinding{
697697
Spec: elbv2api.TargetGroupBindingSpec{
698698
TargetGroupARN: "tg-2",
699699
TargetType: &ipTargetType,
700-
VpcID: "vpc-02",
700+
VpcID: "vpc-0bbbbbbb",
701701
},
702702
},
703703
oldTGB: &elbv2api.TargetGroupBinding{
704704
Spec: elbv2api.TargetGroupBindingSpec{
705705
TargetGroupARN: "tg-2",
706706
TargetType: &ipTargetType,
707-
VpcID: "vpc-01",
707+
VpcID: "vpc-0aaaaaaa",
708708
},
709709
},
710710
},
711711
wantErr: errors.New("TargetGroupBinding update may not change these fields: spec.vpcID"),
712712
},
713713
{
714-
name: "VpcID modified from vpc-01 to nil",
714+
name: "VpcID modified from vpc-0aaaaaaa to nil",
715715
args: args{
716716
tgb: &elbv2api.TargetGroupBinding{
717717
Spec: elbv2api.TargetGroupBindingSpec{
@@ -723,20 +723,20 @@ func Test_targetGroupBindingValidator_checkImmutableFields(t *testing.T) {
723723
Spec: elbv2api.TargetGroupBindingSpec{
724724
TargetGroupARN: "tg-2",
725725
TargetType: &ipTargetType,
726-
VpcID: "vpc-01",
726+
VpcID: "vpc-0aaaaaaa",
727727
},
728728
},
729729
},
730730
wantErr: errors.New("TargetGroupBinding update may not change these fields: spec.vpcID"),
731731
},
732732
{
733-
name: "VpcID modified from nil to vpc-01",
733+
name: "VpcID modified from nil to vpc-0aaaaaaa",
734734
args: args{
735735
tgb: &elbv2api.TargetGroupBinding{
736736
Spec: elbv2api.TargetGroupBindingSpec{
737737
TargetGroupARN: "tg-2",
738738
TargetType: &ipTargetType,
739-
VpcID: "vpc-01",
739+
VpcID: "vpc-0aaaaaaa",
740740
},
741741
},
742742
oldTGB: &elbv2api.TargetGroupBinding{
@@ -1118,12 +1118,24 @@ func Test_targetGroupBindingValidator_checkTargetGroupVpcID(t *testing.T) {
11181118
obj: &elbv2api.TargetGroupBinding{
11191119
Spec: elbv2api.TargetGroupBindingSpec{
11201120
TargetGroupARN: "tg-2",
1121-
VpcID: "vpcid-01",
1121+
VpcID: "vpc-b234567a",
11221122
},
11231123
},
11241124
},
11251125
wantErr: errors.New("unable to get target group VpcID: vpcid not found"),
11261126
},
1127+
{
1128+
name: "[err] vpcID is not valid",
1129+
args: args{
1130+
obj: &elbv2api.TargetGroupBinding{
1131+
Spec: elbv2api.TargetGroupBindingSpec{
1132+
TargetGroupARN: "tg-2",
1133+
VpcID: "vpcid-123",
1134+
},
1135+
},
1136+
},
1137+
wantErr: errors.New("ValidationError: vpcID vpcid-123 failed to satisfy constraint: VPC Id must begin with 'vpc-' followed by 8 or 17 lowercase letters (a-f) or numbers."),
1138+
},
11271139
}
11281140
for _, tt := range tests {
11291141
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)