Skip to content

Commit 76da6d4

Browse files
committed
add validation to ensure it is really an ALB gateway
1 parent 2f442de commit 76da6d4

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

pkg/deploy/stack_deployer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ func (d *defaultStackDeployer) Deploy(ctx context.Context, stack core.Stack, met
122122

123123
if d.enableFrontendNLB {
124124
var desiredFENLBState []*elbv2model.FrontendNlbTargetGroupDesiredState
125-
err := stack.ListResources(&desiredFENLBState)
126-
d.logger.Info(fmt.Sprintf("Got this result!!! %+v %+v", desiredFENLBState, err))
127-
125+
stack.ListResources(&desiredFENLBState)
128126
var frontendNLBState *elbv2model.FrontendNlbTargetGroupDesiredState
129127
if len(desiredFENLBState) == 1 {
130128
frontendNLBState = desiredFENLBState[0]

pkg/gateway/routeutils/backend_gateway.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
1515
"sigs.k8s.io/controller-runtime/pkg/client"
1616
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
17+
"strings"
1718
)
1819

1920
var _ TargetGroupConfigurator = &GatewayBackendConfig{}
@@ -160,8 +161,24 @@ func gatewayLoader(ctx context.Context, k8sClient client.Client, routeIdentifier
160161
// If the ARN is not available, then the backend is not yet usable.
161162
initialErrorMessage := fmt.Sprintf("Gateway (%s:%s) is not usable yet, LB ARN is not provisioned)", gwIdentifier.Namespace, gwIdentifier.Name)
162163
wrappedGatewayErrorMessage := generateInvalidMessageWithRouteDetails(initialErrorMessage, routeKind, routeIdentifier)
163-
return nil, wrapError(errors.Errorf("%s", initialErrorMessage), gwv1.GatewayReasonListenersNotValid, gwv1.RouteReasonBackendNotFound, &wrappedGatewayErrorMessage, nil), nil
164+
// This needs to be a fatal error, otherwise we will not run another reconcile cycle to pick up the ARN.
165+
return nil, nil, wrapError(errors.Errorf("%s", initialErrorMessage), gwv1.GatewayReasonListenersNotValid, gwv1.RouteReasonBackendNotFound, &wrappedGatewayErrorMessage, nil)
166+
}
167+
168+
err = validateGatewayARN(arn)
169+
if err != nil {
170+
wrappedGatewayErrorMessage := generateInvalidMessageWithRouteDetails(err.Error(), routeKind, routeIdentifier)
171+
// This can be a warning, as we know that retrying reconcile will do nothing to fix this situation.
172+
return nil, wrapError(err, gwv1.GatewayReasonListenersNotValid, gwv1.RouteReasonBackendNotFound, &wrappedGatewayErrorMessage, nil), nil
164173
}
165174

166175
return NewGatewayBackendConfig(gw, tgProps, arn, int32(*backendRef.Port)), nil, nil
167176
}
177+
178+
func validateGatewayARN(arn string) error {
179+
parts := strings.Split(arn, "/")
180+
if len(parts) < 2 || parts[1] != "app" {
181+
return errors.Errorf("invalid gateway ARN: %s, the resource type must be application load balancer", arn)
182+
}
183+
return nil
184+
}

pkg/gateway/routeutils/backend_gateway_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,46 @@ func TestGatewayBackendConfig_GetHealthCheckPort(t *testing.T) {
205205
})
206206
}
207207
}
208+
209+
func TestValidateGatewayARN(t *testing.T) {
210+
tests := []struct {
211+
name string
212+
arn string
213+
wantErr bool
214+
}{
215+
{
216+
name: "valid ALB ARN",
217+
arn: "arn:aws:elasticloadbalancing:us-east-1:565768096483:loadbalancer/app/k8s-echoserv-testgwal-3c92fc24ed/9604d5627427405c",
218+
wantErr: false,
219+
},
220+
{
221+
name: "invalid NLB ARN",
222+
arn: "arn:aws:elasticloadbalancing:us-east-1:565768096483:loadbalancer/net/my-nlb/1234567890123456",
223+
wantErr: true,
224+
},
225+
{
226+
name: "invalid format - no slashes",
227+
arn: "arn:aws:elasticloadbalancing:us-east-1:565768096483:loadbalancer",
228+
wantErr: true,
229+
},
230+
{
231+
name: "invalid format - only one part",
232+
arn: "arn:aws:elasticloadbalancing:us-east-1:565768096483:loadbalancer/",
233+
wantErr: true,
234+
},
235+
{
236+
name: "empty string",
237+
arn: "",
238+
wantErr: true,
239+
},
240+
}
241+
242+
for _, tt := range tests {
243+
t.Run(tt.name, func(t *testing.T) {
244+
err := validateGatewayARN(tt.arn)
245+
if (err != nil) != tt.wantErr {
246+
t.Errorf("validateGatewayARN() error = %v, wantErr %v", err, tt.wantErr)
247+
}
248+
})
249+
}
250+
}

0 commit comments

Comments
 (0)