Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

added pagination and properties to iampolicy #593

Merged
merged 4 commits into from
Feb 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions resources/iam-policies.go
Original file line number Diff line number Diff line change
@@ -4,11 +4,15 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

type IAMPolicy struct {
svc *iam.IAM
arn string
svc *iam.IAM
name string
policyId string
arn string
path string
}

func init() {
@@ -17,29 +21,34 @@ func init() {

func ListIAMPolicies(sess *session.Session) ([]Resource, error) {
svc := iam.New(sess)

params := &iam.ListPoliciesInput{
Scope: aws.String("Local"),
}
resources := make([]Resource, 0)

for {
resp, err := svc.ListPolicies(params)
if err != nil {
return nil, err
}
policies := make([]*iam.Policy, 0)

for _, out := range resp.Policies {
resources = append(resources, &IAMPolicy{
svc: svc,
arn: *out.Arn,
})
}
err := svc.ListPoliciesPages(params,
func(page *iam.ListPoliciesOutput, lastPage bool) bool {
for _, policy := range page.Policies {
policies = append(policies, policy)
}
return true
})
if err != nil {
return nil, err
}

if *resp.IsTruncated == false {
break
}
resources := make([]Resource, 0)

params.Marker = resp.Marker
for _, out := range policies {
resources = append(resources, &IAMPolicy{
svc: svc,
name: *out.PolicyName,
path: *out.Path,
arn: *out.Arn,
policyId: *out.PolicyId,
})
}

return resources, nil
@@ -74,6 +83,16 @@ func (e *IAMPolicy) Remove() error {
return nil
}

func (policy *IAMPolicy) Properties() types.Properties {
properties := types.NewProperties()

properties.Set("Name", policy.name)
properties.Set("ARN", policy.arn)
properties.Set("Path", policy.path)
properties.Set("PolicyID", policy.policyId)
return properties
}

func (e *IAMPolicy) String() string {
return e.arn
}
49 changes: 40 additions & 9 deletions resources/sns-topics.go
Original file line number Diff line number Diff line change
@@ -5,41 +5,72 @@ import (

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

type SNSTopic struct {
svc *sns.SNS
id *string
tags []*sns.Tag
}

func init() {
register("SNSTopic", ListSNSTopics)
}

func ListSNSTopics(sess *session.Session) ([]Resource, error) {
svc := sns.New(sess)

resp, err := svc.ListTopics(nil)
topics := make([]*sns.Topic, 0)

params := &sns.ListTopicsInput{}

err := svc.ListTopicsPages(params, func(page *sns.ListTopicsOutput, lastPage bool) bool {
for _, out := range page.Topics {
topics = append(topics, out)
}
return true
})
if err != nil {
return nil, err
}
resources := make([]Resource, 0)
for _, topic := range resp.Topics {
for _, topic := range topics {
tags, err := svc.ListTagsForResource(&sns.ListTagsForResourceInput{
ResourceArn: topic.TopicArn,
})

if err != nil {
continue
}

resources = append(resources, &SNSTopic{
svc: svc,
id: topic.TopicArn,
svc: svc,
id: topic.TopicArn,
tags: tags.Tags,
})
}
return resources, nil
}

type SNSTopic struct {
svc *sns.SNS
id *string
}

func (topic *SNSTopic) Remove() error {
_, err := topic.svc.DeleteTopic(&sns.DeleteTopicInput{
TopicArn: topic.id,
})
return err
}

func (topic *SNSTopic) Properties() types.Properties {
properties := types.NewProperties()

for _, tag := range topic.tags {
properties.SetTag(tag.Key, tag.Value)
}
properties.Set("TopicARN", topic.id)

return properties
}

func (topic *SNSTopic) String() string {
return fmt.Sprintf("TopicARN: %s", *topic.id)
}