Skip to content

Commit

Permalink
Only awsls supported resources
Browse files Browse the repository at this point in the history
Some resources couldn't be listed with awsls yet,
but are now covered.
  • Loading branch information
jckuester committed Jan 2, 2021
1 parent ccde0b5 commit d9e60cc
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 439 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ bin/
.terradozer/
.terraform/
*.tfstate
*tfstate.backup
coverage.txt
awsweeper
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ require (
github.com/aws/aws-sdk-go-v2 v0.24.0
github.com/fatih/color v1.9.0
github.com/gruntwork-io/terratest v0.24.2
github.com/jckuester/awsls v0.8.0
github.com/jckuester/awsls v0.8.1
github.com/jckuester/terradozer v0.1.3
github.com/onsi/gomega v1.9.0
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
github.com/zclconf/go-cty v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jckuester/awsls v0.8.0 h1:ZuQRfMzEa2ZoZ/pcNJLmXXB0S14I5X7kvIENWdryxdQ=
github.com/jckuester/awsls v0.8.0/go.mod h1:PfviZPTqz64c9EfNj6b1ygggheRl51JrJWT7R3z+2BQ=
github.com/jckuester/awsls v0.8.1 h1:k329Ey/odG/hdbTiubvGHFxIEVRpL1KLNTOaO8P+Eow=
github.com/jckuester/awsls v0.8.1/go.mod h1:PfviZPTqz64c9EfNj6b1ygggheRl51JrJWT7R3z+2BQ=
github.com/jckuester/terradozer v0.1.3 h1:xrRxr+L58QAVz5Kwq2fyWCNiK1NWOuKo8g5Q2664WZ4=
github.com/jckuester/terradozer v0.1.3/go.mod h1:ER3EJojZmO2u6lfcdgnmC+Nrg/TV2T2bacY5FZpqgks=
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func mainExitCode() int {
}

// initialize a Terraform AWS provider for each AWS client with a matching config
providers, err := util.NewProviderPool(clientKeys, "2.68.0", "~/.awsweeper", timeoutDuration)
providers, err := util.NewProviderPool(clientKeys, "v3.16.0", "~/.awsweeper", timeoutDuration)
if err != nil {
fmt.Fprint(os.Stderr, color.RedString("\nError: %s\n", err))

Expand Down
43 changes: 43 additions & 0 deletions pkg/resource/dependency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package resource

var (
// DependencyOrder is the order in which resource types should be deleted,
// since dependent resources need to be deleted before their dependencies
// (e.g. aws_subnet before aws_vpc)
DependencyOrder = map[string]int{
"aws_lambda_function": 10100,
"aws_ecs_cluster": 10000,
"aws_autoscaling_group": 9990,
"aws_instance": 9980,
"aws_key_pair": 9970,
"aws_elb": 9960,
"aws_vpc_endpoint": 9950,
"aws_nat_gateway": 9940,
"aws_cloudformation_stack": 9930,
"aws_route53_zone": 9920,
"aws_efs_file_system": 9910,
"aws_launch_configuration": 9900,
"aws_eip": 9890,
"aws_internet_gateway": 9880,
"aws_subnet": 9870,
"aws_route_table": 9860,
"aws_security_group": 9850,
"aws_network_acl": 9840,
"aws_vpc": 9830,
"aws_db_instance": 9825,
"aws_iam_policy": 9820,
"aws_iam_group": 9810,
"aws_iam_user": 9800,
"aws_iam_role": 9790,
"aws_iam_instance_profile": 9780,
"aws_s3_bucket": 9750,
"aws_ami": 9740,
"aws_ebs_volume": 9730,
"aws_ebs_snapshot": 9720,
"aws_kms_alias": 9610,
"aws_kms_key": 9600,
"aws_network_interface": 9000,
"aws_cloudwatch_log_group": 8900,
"aws_cloudtrail": 8800,
}
)
2 changes: 1 addition & 1 deletion pkg/resource/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewFilter(path string) (*Filter, error) {
// Validate checks if all resource types appearing in the config are currently supported.
func (f Filter) Validate() error {
for _, rType := range f.Types() {
if !(SupportedResourceType(rType) || resource.IsSupportedType(rType)) {
if resource.IsSupportedType(rType) {
return fmt.Errorf("unsupported resource type: %s", rType)
}

Expand Down
97 changes: 33 additions & 64 deletions pkg/resource/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,82 +26,51 @@ func List(filter *Filter, clients map[util.AWSClientKey]awsls.Client,
var destroyableRes []terradozerRes.DestroyableResource

for _, rType := range filter.Types() {
if SupportedResourceType(rType) {
for key, client := range clients {
err := client.SetAccountID()
if err != nil {
log.WithError(err).Fatal("failed to set account ID")
continue
}

rawResources, err := AWS(client).RawResources(rType)
if err != nil {
log.WithError(err).Fatal("failed to get raw resources")
}

deletableResources, err := DeletableResources(rType, rawResources, client)
if err != nil {
log.WithError(err).Fatal("failed to convert raw resources into deletable resources")
}

resourcesWithStates := awslsRes.GetStates(deletableResources, providers)

filteredRes := filter.Apply(resourcesWithStates)
print(filteredRes, outputType)

p := providers[key]

for _, r := range filteredRes {
destroyableRes = append(destroyableRes, terradozerRes.NewWithState(r.Type, r.ID, &p, r.State()))
}
for key, client := range clients {
err := client.SetAccountID()
if err != nil {
log.WithError(err).Fatal("failed to set account ID")
continue
}
} else {
for key, client := range clients {
err := client.SetAccountID()
if err != nil {
log.WithError(err).Fatal("failed to set account ID")
continue
}

resources, err := awsls.ListResourcesByType(&client, rType)
if err != nil {
log.WithError(err).Fatal("failed to list awsls supported resources")
continue
}
resources, err := awsls.ListResourcesByType(&client, rType)
if err != nil {
log.WithError(err).Fatal("failed to list awsls supported resources")
continue
}

resourcesWithStates := awslsRes.GetStates(resources, providers)
resourcesWithStates := awslsRes.GetStates(resources, providers)

filteredRes := filter.Apply(resourcesWithStates)
print(filteredRes, outputType)
filteredRes := filter.Apply(resourcesWithStates)
print(filteredRes, outputType)

p := providers[key]
p := providers[key]

switch rType {
case "aws_iam_user":
attachedPolicies := getAttachedUserPolicies(filteredRes, client, &p)
print(attachedPolicies, outputType)
switch rType {
case "aws_iam_user":
attachedPolicies := getAttachedUserPolicies(filteredRes, client, &p)
print(attachedPolicies, outputType)

inlinePolicies := getInlineUserPolicies(filteredRes, client, &p)
print(inlinePolicies, outputType)
inlinePolicies := getInlineUserPolicies(filteredRes, client, &p)
print(inlinePolicies, outputType)

filteredRes = append(filteredRes, attachedPolicies...)
filteredRes = append(filteredRes, inlinePolicies...)
case "aws_iam_policy":
policyAttachments := getPolicyAttachments(filteredRes, &p)
print(policyAttachments, outputType)
filteredRes = append(filteredRes, attachedPolicies...)
filteredRes = append(filteredRes, inlinePolicies...)
case "aws_iam_policy":
policyAttachments := getPolicyAttachments(filteredRes, &p)
print(policyAttachments, outputType)

filteredRes = append(filteredRes, policyAttachments...)
filteredRes = append(filteredRes, policyAttachments...)

case "aws_efs_file_system":
mountTargets := getEfsMountTargets(filteredRes, client, &p)
print(mountTargets, outputType)
case "aws_efs_file_system":
mountTargets := getEfsMountTargets(filteredRes, client, &p)
print(mountTargets, outputType)

filteredRes = append(filteredRes, mountTargets...)
}
filteredRes = append(filteredRes, mountTargets...)
}

for _, r := range filteredRes {
destroyableRes = append(destroyableRes, terradozerRes.NewWithState(r.Type, r.ID, &p, r.State()))
}
for _, r := range filteredRes {
destroyableRes = append(destroyableRes, terradozerRes.NewWithState(r.Type, r.ID, &p, r.State()))
}
}
}
Expand Down
75 changes: 0 additions & 75 deletions pkg/resource/resource.go

This file was deleted.

59 changes: 0 additions & 59 deletions pkg/resource/resource_test.go

This file was deleted.

Loading

0 comments on commit d9e60cc

Please sign in to comment.