Skip to content

Commit cb3d91b

Browse files
authored
Fix nukeSecurityGroup for delete chained-security-group (#317)
1 parent 3044692 commit cb3d91b

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

aws/ec2.go

+38
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,44 @@ func (v Vpc) nukeSecurityGroups() error {
343343
},
344344
},
345345
)
346+
347+
for _, securityGroup := range securityGroups.SecurityGroups {
348+
securityGroupRules, _ := v.svc.DescribeSecurityGroupRules(
349+
&ec2.DescribeSecurityGroupRulesInput{
350+
Filters: []*ec2.Filter{
351+
{
352+
Name: awsgo.String("group-id"),
353+
Values: []*string{securityGroup.GroupId},
354+
},
355+
},
356+
},
357+
)
358+
for _, securityGroupRule := range securityGroupRules.SecurityGroupRules {
359+
logging.Logger.Infof("...deleting Security Group Rule %s", awsgo.StringValue(securityGroupRule.SecurityGroupRuleId))
360+
if *securityGroupRule.IsEgress {
361+
_, err := v.svc.RevokeSecurityGroupEgress(
362+
&ec2.RevokeSecurityGroupEgressInput{
363+
GroupId: securityGroup.GroupId,
364+
SecurityGroupRuleIds: []*string{securityGroupRule.SecurityGroupRuleId},
365+
},
366+
)
367+
if err != nil {
368+
return errors.WithStackTrace(err)
369+
}
370+
} else {
371+
_, err := v.svc.RevokeSecurityGroupIngress(
372+
&ec2.RevokeSecurityGroupIngressInput{
373+
GroupId: securityGroup.GroupId,
374+
SecurityGroupRuleIds: []*string{securityGroupRule.SecurityGroupRuleId},
375+
},
376+
)
377+
if err != nil {
378+
return errors.WithStackTrace(err)
379+
}
380+
}
381+
}
382+
}
383+
346384
for _, securityGroup := range securityGroups.SecurityGroups {
347385
logging.Logger.Infof("...deleting Security Group %s", awsgo.StringValue(securityGroup.GroupId))
348386
if *securityGroup.GroupName != "default" {

aws/ec2_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
ExampleSecurityGroupId = "sg-" + ExampleId
3333
ExampleSecurityGroupIdTwo = "sg-" + ExampleIdTwo
3434
ExampleSecurityGroupIdThree = "sg-" + ExampleIdThree
35+
ExampleSecurityGroupRuleId = "sgr-" + ExampleId
3536
ExampleInternetGatewayId = "igw-" + ExampleId
3637
ExampleEndpointId = "vpce-" + ExampleId
3738
)

aws/ec2_unit_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ func TestNukeMockVpcs(t *testing.T) {
149149
}
150150
deleteNetworkAclInput := getDeleteNetworkAclInput(ExampleNetworkAclId)
151151

152+
describeSecurityGroupRulesInput := getDescribeSecurityGroupRulesInput(ExampleSecurityGroupId)
153+
describeSecurityGroupRulesOutput := getDescribeSecurityGroupRulesOutput([]string{ExampleSecurityGroupRuleId})
154+
describeSecurityGroupRulesFunc := func(input *ec2.DescribeSecurityGroupRulesInput) (*ec2.DescribeSecurityGroupRulesOutput, error) {
155+
return describeSecurityGroupRulesOutput, nil
156+
}
157+
revokeSecurityGroupEgressInput := getRevokeSecurityGroupEgressInput(ExampleSecurityGroupId ,ExampleSecurityGroupRuleId)
158+
revokeSecurityGroupIngressInput := getRevokeSecurityGroupIngressInput(ExampleSecurityGroupId, ExampleSecurityGroupRuleId)
159+
152160
describeSecurityGroupsInput := getDescribeSecurityGroupsInput(vpc.VpcId)
153161
describeSecurityGroupsOutput := getDescribeSecurityGroupsOutput([]string{ExampleSecurityGroupId})
154162
describeSecurityGroupsFunc := func(input *ec2.DescribeSecurityGroupsInput) (*ec2.DescribeSecurityGroupsOutput, error) {
@@ -174,6 +182,9 @@ func TestNukeMockVpcs(t *testing.T) {
174182
mockEC2.EXPECT().DescribeNetworkAcls(describeNetworkAclsInput).DoAndReturn(describeNetworkAclsFunc),
175183
mockEC2.EXPECT().DeleteNetworkAcl(deleteNetworkAclInput),
176184
mockEC2.EXPECT().DescribeSecurityGroups(describeSecurityGroupsInput).DoAndReturn(describeSecurityGroupsFunc),
185+
mockEC2.EXPECT().DescribeSecurityGroupRules(describeSecurityGroupRulesInput).DoAndReturn(describeSecurityGroupRulesFunc),
186+
mockEC2.EXPECT().RevokeSecurityGroupEgress(revokeSecurityGroupEgressInput),
187+
mockEC2.EXPECT().RevokeSecurityGroupIngress(revokeSecurityGroupIngressInput),
177188
mockEC2.EXPECT().DeleteSecurityGroup(deleteSecurityGroupInput),
178189
mockEC2.EXPECT().DeleteVpc(deleteVpcInput),
179190
)

aws/mock_ec2_utils_for_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,46 @@ func getDeleteNetworkAclInput(networkAclId string) *ec2.DeleteNetworkAclInput {
127127
}
128128
}
129129

130+
func getDescribeSecurityGroupRulesInput(securityGroupId string) *ec2.DescribeSecurityGroupRulesInput {
131+
return &ec2.DescribeSecurityGroupRulesInput{
132+
Filters: []*ec2.Filter{
133+
&ec2.Filter{
134+
Name: awsgo.String("group-id"),
135+
Values: []*string{awsgo.String(securityGroupId)},
136+
},
137+
},
138+
}
139+
}
140+
141+
func getDescribeSecurityGroupRulesOutput(securityGroupRuleIds []string) *ec2.DescribeSecurityGroupRulesOutput {
142+
var securityGroupRules []*ec2.SecurityGroupRule
143+
for _, securityGroupRule := range securityGroupRuleIds {
144+
securityGroupRules = append(securityGroupRules, &ec2.SecurityGroupRule{
145+
IsEgress: awsgo.Bool(true), // egress rule
146+
SecurityGroupRuleId: awsgo.String(securityGroupRule),
147+
})
148+
securityGroupRules = append(securityGroupRules, &ec2.SecurityGroupRule{
149+
IsEgress: awsgo.Bool(false), // ingress rule
150+
SecurityGroupRuleId: awsgo.String(securityGroupRule),
151+
})
152+
}
153+
return &ec2.DescribeSecurityGroupRulesOutput{SecurityGroupRules: securityGroupRules}
154+
}
155+
156+
func getRevokeSecurityGroupEgressInput(securityGroupId string, securityGroupRuleId string) *ec2.RevokeSecurityGroupEgressInput {
157+
return &ec2.RevokeSecurityGroupEgressInput{
158+
GroupId: awsgo.String(securityGroupId),
159+
SecurityGroupRuleIds: []*string{awsgo.String(securityGroupRuleId)},
160+
}
161+
}
162+
163+
func getRevokeSecurityGroupIngressInput(securityGroupId string, securityGroupRuleId string) *ec2.RevokeSecurityGroupIngressInput {
164+
return &ec2.RevokeSecurityGroupIngressInput{
165+
GroupId: awsgo.String(securityGroupId),
166+
SecurityGroupRuleIds: []*string{awsgo.String(securityGroupRuleId)},
167+
}
168+
}
169+
130170
func getDescribeSecurityGroupsInput(vpcId string) *ec2.DescribeSecurityGroupsInput {
131171
return &ec2.DescribeSecurityGroupsInput{
132172
Filters: []*ec2.Filter{

0 commit comments

Comments
 (0)