Skip to content

Commit 2402597

Browse files
committed
Fix panic when deleting an ALB with no default SG in the VPC
It still fails to delete the ALB because ALBs require at least one SG, but at least it wont panic. Subnets that are shared across accounts using AWS Resource Access Manager do not have a `default` security group by default.
1 parent 866f0e2 commit 2402597

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

internal/alb/sg/lb_attachment.go

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

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/aws/aws-sdk-go/service/elbv2"
@@ -62,5 +63,8 @@ func (controller *lbAttachmentController) getDefaultSecurityGroupID() (string, e
6263
if err != nil {
6364
return "", err
6465
}
66+
if defaultSG == nil {
67+
return "", errors.New("default security group not found")
68+
}
6569
return aws.StringValue(defaultSG.GroupId), nil
6670
}

internal/alb/sg/lb_attachment_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ func Test_LBAttachmentDelete(t *testing.T) {
142142
},
143143
ExpectedError: errors.New("failed to get default securityGroup for current vpc due to GetSecurityGroupByNameCall"),
144144
},
145+
{
146+
Name: "delete failed when no default securityGroup",
147+
Instance: elbv2.LoadBalancer{
148+
LoadBalancerArn: aws.String("arn"),
149+
SecurityGroups: []*string{aws.String("sg-abcd")},
150+
},
151+
GetSecurityGroupByNameCall: &GetSecurityGroupByNameCall{
152+
GroupName: "default",
153+
Instance: nil,
154+
},
155+
ExpectedError: errors.New("failed to get default securityGroup for current vpc due to default security group not found"),
156+
},
145157
{
146158
Name: "delete failed when modify SG to default one",
147159
Instance: elbv2.LoadBalancer{

0 commit comments

Comments
 (0)