|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/aws/aws-sdk-go/aws" |
| 5 | + "github.com/aws/aws-sdk-go/aws/session" |
| 6 | + "github.com/aws/aws-sdk-go/service/ec2" |
| 7 | + "github.com/rebuy-de/aws-nuke/pkg/types" |
| 8 | +) |
| 9 | + |
| 10 | +type EC2DefaultSecurityGroupRule struct { |
| 11 | + svc *ec2.EC2 |
| 12 | + id *string |
| 13 | + groupId *string |
| 14 | + isEgress *bool |
| 15 | +} |
| 16 | + |
| 17 | +func init() { |
| 18 | + register("EC2DefaultSecurityGroupRule", ListEC2SecurityGroupRules) |
| 19 | +} |
| 20 | + |
| 21 | +func ListEC2SecurityGroupRules(sess *session.Session) ([]Resource, error) { |
| 22 | + svc := ec2.New(sess) |
| 23 | + resources := make([]Resource, 0) |
| 24 | + |
| 25 | + sgFilters := []*ec2.Filter{ |
| 26 | + { |
| 27 | + Name: aws.String("group-name"), |
| 28 | + Values: []*string{ |
| 29 | + aws.String("default"), |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | + groupIds := make([]*string, 0) |
| 34 | + sgParams := &ec2.DescribeSecurityGroupsInput{Filters: sgFilters} |
| 35 | + err := svc.DescribeSecurityGroupsPages(sgParams, |
| 36 | + func(page *ec2.DescribeSecurityGroupsOutput, lastPage bool) bool { |
| 37 | + for _, group := range page.SecurityGroups { |
| 38 | + groupIds = append(groupIds, group.GroupId) |
| 39 | + } |
| 40 | + return !lastPage |
| 41 | + }) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + sgRuleFilters := []*ec2.Filter{ |
| 47 | + { |
| 48 | + Name: aws.String("group-id"), |
| 49 | + Values: groupIds, |
| 50 | + }, |
| 51 | + } |
| 52 | + sgRuleParams := &ec2.DescribeSecurityGroupRulesInput{Filters: sgRuleFilters} |
| 53 | + err = svc.DescribeSecurityGroupRulesPages(sgRuleParams, |
| 54 | + func(page *ec2.DescribeSecurityGroupRulesOutput, lastPage bool) bool { |
| 55 | + for _, rule := range page.SecurityGroupRules { |
| 56 | + resources = append(resources, &EC2DefaultSecurityGroupRule{ |
| 57 | + svc: svc, |
| 58 | + id: rule.SecurityGroupRuleId, |
| 59 | + groupId: rule.GroupId, |
| 60 | + isEgress: rule.IsEgress, |
| 61 | + }) |
| 62 | + } |
| 63 | + return !lastPage |
| 64 | + }) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + return resources, nil |
| 70 | +} |
| 71 | + |
| 72 | +func (r *EC2DefaultSecurityGroupRule) Remove() error { |
| 73 | + rules := make([]*string, 1) |
| 74 | + rules[0] = r.id |
| 75 | + if *r.isEgress { |
| 76 | + params := &ec2.RevokeSecurityGroupEgressInput{ |
| 77 | + GroupId: r.groupId, |
| 78 | + SecurityGroupRuleIds: rules, |
| 79 | + } |
| 80 | + _, err := r.svc.RevokeSecurityGroupEgress(params) |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + } else { |
| 86 | + params := &ec2.RevokeSecurityGroupIngressInput{ |
| 87 | + GroupId: r.groupId, |
| 88 | + SecurityGroupRuleIds: rules, |
| 89 | + } |
| 90 | + _, err := r.svc.RevokeSecurityGroupIngress(params) |
| 91 | + |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func (r *EC2DefaultSecurityGroupRule) Properties() types.Properties { |
| 101 | + properties := types.NewProperties() |
| 102 | + properties.Set("SecurityGroupId", r.groupId) |
| 103 | + return properties |
| 104 | +} |
| 105 | + |
| 106 | +func (r *EC2DefaultSecurityGroupRule) String() string { |
| 107 | + return *r.id |
| 108 | +} |
0 commit comments