Skip to content

Commit

Permalink
chore: sync with upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Mar 10, 2022
2 parents 9ae42d1 + 8b0c1d8 commit 889cde0
Show file tree
Hide file tree
Showing 27 changed files with 1,077 additions and 159 deletions.
31 changes: 24 additions & 7 deletions go.mod
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
)
638 changes: 545 additions & 93 deletions go.sum

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion resources/autoscaling-groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

func init() {
Expand All @@ -19,8 +20,9 @@ func ListAutoscalingGroups(s *session.Session) ([]Resource, error) {
func(page *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool {
for _, asg := range page.AutoScalingGroups {
resources = append(resources, &AutoScalingGroup{
svc: svc,
name: asg.AutoScalingGroupName,
svc: svc,
tags: asg.Tags,
})
}
return !lastPage
Expand All @@ -36,6 +38,7 @@ func ListAutoscalingGroups(s *session.Session) ([]Resource, error) {
type AutoScalingGroup struct {
svc *autoscaling.AutoScaling
name *string
tags []*autoscaling.TagDescription
}

func (asg *AutoScalingGroup) Remove() error {
Expand All @@ -55,3 +58,14 @@ func (asg *AutoScalingGroup) Remove() error {
func (asg *AutoScalingGroup) String() string {
return *asg.name
}

func (asg *AutoScalingGroup) Properties() types.Properties {
properties := types.NewProperties()
for _, tag := range asg.tags {
properties.SetTag(tag.Key, tag.Value)
}

properties.Set("Name", asg.name)

return properties
}
6 changes: 6 additions & 0 deletions resources/cloudwatch-alarms.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

type CloudWatchAlarm struct {
Expand Down Expand Up @@ -55,6 +56,11 @@ func (f *CloudWatchAlarm) Remove() error {
return err
}

func (f *CloudWatchAlarm) Properties() types.Properties {
return types.NewProperties().
Set("Name", f.alarmName)
}

func (f *CloudWatchAlarm) String() string {
return *f.alarmName
}
108 changes: 108 additions & 0 deletions resources/ec2-default-security-group-rules.go
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
}
89 changes: 89 additions & 0 deletions resources/ec2-hosts.go
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
}
20 changes: 14 additions & 6 deletions resources/ec2-images.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
)

type EC2Image struct {
svc *ec2.EC2
id string
tags []*ec2.Tag
svc *ec2.EC2
creationDate string
id string
name string
tags []*ec2.Tag
}

func init() {
Expand All @@ -32,9 +34,11 @@ func ListEC2Images(sess *session.Session) ([]Resource, error) {
resources := make([]Resource, 0)
for _, out := range resp.Images {
resources = append(resources, &EC2Image{
svc: svc,
id: *out.ImageId,
tags: out.Tags,
svc: svc,
creationDate: *out.CreationDate,
id: *out.ImageId,
name: *out.Name,
tags: out.Tags,
})
}

Expand All @@ -50,6 +54,10 @@ func (e *EC2Image) Remove() error {

func (e *EC2Image) Properties() types.Properties {
properties := types.NewProperties()

properties.Set("CreationDate", e.creationDate)
properties.Set("Name", e.name)

for _, tagValue := range e.tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}
Expand Down
1 change: 1 addition & 0 deletions resources/ec2-internet-gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (e *EC2InternetGateway) Properties() types.Properties {
properties.SetTag(tagValue.Key, tagValue.Value)
}
properties.Set("DefaultVPC", e.defaultVPC)
properties.Set("OwnerID", e.igw.OwnerId)
return properties
}

Expand Down
14 changes: 14 additions & 0 deletions resources/ec2-key-pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package resources
import (
"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 EC2KeyPair struct {
svc *ec2.EC2
name string
tags []*ec2.Tag
}

func init() {
Expand All @@ -27,6 +29,7 @@ func ListEC2KeyPairs(sess *session.Session) ([]Resource, error) {
resources = append(resources, &EC2KeyPair{
svc: svc,
name: *out.KeyName,
tags: out.Tags,
})
}

Expand All @@ -46,6 +49,17 @@ func (e *EC2KeyPair) Remove() error {
return nil
}

func (e *EC2KeyPair) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Name", e.name)

for _, tag := range e.tags {
properties.SetTag(tag.Key, tag.Value)
}

return properties
}

func (e *EC2KeyPair) String() string {
return e.name
}
Loading

0 comments on commit 889cde0

Please sign in to comment.