-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,077 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
module github.com/rebuy-de/aws-nuke | ||
|
||
go 1.16 | ||
go 1.17 | ||
|
||
require ( | ||
github.com/Masterminds/goutils v1.1.1 // indirect | ||
github.com/Masterminds/semver v1.5.0 // indirect | ||
github.com/Masterminds/sprig v2.22.0+incompatible | ||
github.com/aws/aws-sdk-go v1.42.23 | ||
github.com/fatih/color v1.10.0 | ||
github.com/golang/mock v1.4.4 | ||
github.com/aws/aws-sdk-go v1.42.45 | ||
github.com/fatih/color v1.13.0 | ||
github.com/golang/mock v1.6.0 | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/huandu/xstrings v1.3.2 // indirect | ||
github.com/iancoleman/strcase v0.2.0 | ||
github.com/imdario/mergo v0.3.12 // indirect | ||
github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4 | ||
github.com/mitchellh/copystructure v1.2.0 // indirect | ||
github.com/pkg/errors v0.9.1 | ||
github.com/sirupsen/logrus v1.7.1 | ||
github.com/spf13/cobra v1.1.3 | ||
github.com/sirupsen/logrus v1.8.1 | ||
github.com/spf13/cobra v1.3.0 | ||
github.com/stretchr/testify v1.7.0 | ||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a | ||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c | ||
gopkg.in/yaml.v2 v2.4.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/inconshreveable/mousetrap v1.0.0 // indirect | ||
github.com/jmespath/go-jmespath v0.4.0 // indirect | ||
github.com/mattn/go-colorable v0.1.12 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
github.com/mitchellh/reflectwalk v1.0.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect | ||
golang.org/x/mod v0.5.0 // indirect | ||
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect | ||
golang.org/x/tools v0.1.5 // indirect | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package resources | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/rebuy-de/aws-nuke/pkg/types" | ||
) | ||
|
||
type EC2DefaultSecurityGroupRule struct { | ||
svc *ec2.EC2 | ||
id *string | ||
groupId *string | ||
isEgress *bool | ||
} | ||
|
||
func init() { | ||
register("EC2DefaultSecurityGroupRule", ListEC2SecurityGroupRules) | ||
} | ||
|
||
func ListEC2SecurityGroupRules(sess *session.Session) ([]Resource, error) { | ||
svc := ec2.New(sess) | ||
resources := make([]Resource, 0) | ||
|
||
sgFilters := []*ec2.Filter{ | ||
{ | ||
Name: aws.String("group-name"), | ||
Values: []*string{ | ||
aws.String("default"), | ||
}, | ||
}, | ||
} | ||
groupIds := make([]*string, 0) | ||
sgParams := &ec2.DescribeSecurityGroupsInput{Filters: sgFilters} | ||
err := svc.DescribeSecurityGroupsPages(sgParams, | ||
func(page *ec2.DescribeSecurityGroupsOutput, lastPage bool) bool { | ||
for _, group := range page.SecurityGroups { | ||
groupIds = append(groupIds, group.GroupId) | ||
} | ||
return !lastPage | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sgRuleFilters := []*ec2.Filter{ | ||
{ | ||
Name: aws.String("group-id"), | ||
Values: groupIds, | ||
}, | ||
} | ||
sgRuleParams := &ec2.DescribeSecurityGroupRulesInput{Filters: sgRuleFilters} | ||
err = svc.DescribeSecurityGroupRulesPages(sgRuleParams, | ||
func(page *ec2.DescribeSecurityGroupRulesOutput, lastPage bool) bool { | ||
for _, rule := range page.SecurityGroupRules { | ||
resources = append(resources, &EC2DefaultSecurityGroupRule{ | ||
svc: svc, | ||
id: rule.SecurityGroupRuleId, | ||
groupId: rule.GroupId, | ||
isEgress: rule.IsEgress, | ||
}) | ||
} | ||
return !lastPage | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
func (r *EC2DefaultSecurityGroupRule) Remove() error { | ||
rules := make([]*string, 1) | ||
rules[0] = r.id | ||
if *r.isEgress { | ||
params := &ec2.RevokeSecurityGroupEgressInput{ | ||
GroupId: r.groupId, | ||
SecurityGroupRuleIds: rules, | ||
} | ||
_, err := r.svc.RevokeSecurityGroupEgress(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
params := &ec2.RevokeSecurityGroupIngressInput{ | ||
GroupId: r.groupId, | ||
SecurityGroupRuleIds: rules, | ||
} | ||
_, err := r.svc.RevokeSecurityGroupIngress(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *EC2DefaultSecurityGroupRule) Properties() types.Properties { | ||
properties := types.NewProperties() | ||
properties.Set("SecurityGroupId", r.groupId) | ||
return properties | ||
} | ||
|
||
func (r *EC2DefaultSecurityGroupRule) String() string { | ||
return *r.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package resources | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/rebuy-de/aws-nuke/pkg/config" | ||
"github.com/rebuy-de/aws-nuke/pkg/types" | ||
) | ||
|
||
type EC2Host struct { | ||
svc *ec2.EC2 | ||
host *ec2.Host | ||
|
||
featureFlags config.FeatureFlags | ||
} | ||
|
||
func init() { | ||
register("EC2Host", ListEC2Hosts) | ||
} | ||
|
||
func ListEC2Hosts(sess *session.Session) ([]Resource, error) { | ||
svc := ec2.New(sess) | ||
params := &ec2.DescribeHostsInput{} | ||
resources := make([]Resource, 0) | ||
for { | ||
resp, err := svc.DescribeHosts(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, host := range resp.Hosts { | ||
resources = append(resources, &EC2Host{ | ||
svc: svc, | ||
host: host, | ||
}) | ||
} | ||
|
||
if resp.NextToken == nil { | ||
break | ||
} | ||
|
||
params = &ec2.DescribeHostsInput{ | ||
NextToken: resp.NextToken, | ||
} | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
func (i *EC2Host) Filter() error { | ||
if *i.host.State == "released" { | ||
return fmt.Errorf("already released") | ||
} | ||
return nil | ||
} | ||
|
||
func (i *EC2Host) Remove() error { | ||
params := &ec2.ReleaseHostsInput{ | ||
HostIds: []*string{i.host.HostId}, | ||
} | ||
|
||
_, err := i.svc.ReleaseHosts(params) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (i *EC2Host) Properties() types.Properties { | ||
properties := types.NewProperties() | ||
properties.Set("Identifier", i.host.HostId) | ||
properties.Set("HostInstanceFamily", i.host.HostProperties.InstanceFamily) | ||
properties.Set("HostCores", i.host.HostProperties.Cores) | ||
properties.Set("HostState", i.host.State) | ||
properties.Set("AllocationTime", i.host.AllocationTime.Format(time.RFC3339)) | ||
|
||
for _, tagValue := range i.host.Tags { | ||
properties.SetTag(tagValue.Key, tagValue.Value) | ||
} | ||
|
||
return properties | ||
} | ||
|
||
func (i *EC2Host) String() string { | ||
return *i.host.HostId | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.