Skip to content

Commit

Permalink
Merge pull request #3 from lablabs/add_pagination
Browse files Browse the repository at this point in the history
Add pagination loop
  • Loading branch information
martinhaus authored May 4, 2022
2 parents 71ce986 + 6660109 commit ed4e076
Showing 1 changed file with 52 additions and 33 deletions.
85 changes: 52 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,62 +111,81 @@ func getSecret(secretName string) *string {
}
}

func listAllSecrets() *secretsmanager.ListSecretsOutput {
func listAllSecrets() []*secretsmanager.ListSecretsOutput {
s, err := session.NewSession()
if err != nil {
panic(err)
}

svc := secretsmanager.New(s,
aws.NewConfig().WithRegion(region))
input := &secretsmanager.ListSecretsInput{
MaxResults: aws.Int64(100),

result := &secretsmanager.ListSecretsOutput{
NextToken: nil,
}

result, err := svc.ListSecrets(input)
if err != nil {
fmt.Println(err.Error())
var results []*secretsmanager.ListSecretsOutput

for {
input := &secretsmanager.ListSecretsInput{
MaxResults: aws.Int64(100),
NextToken: result.NextToken,
}

result, err = svc.ListSecrets(input)
if err != nil {
fmt.Println(err.Error())
}

results = append(results, result)

if result.NextToken == nil {
break
}
}
return result

return results
}

func filterSecrets(targetTags map[string]string) []string {
allSecrets := listAllSecrets()
var filteredSecrets []string

for _, secret := range allSecrets.SecretList {
// If secret has no tags, skip it
if len(secret.Tags) == 0 {
continue
}
for _, secretOutput := range allSecrets {
for _, secret := range secretOutput.SecretList {
// If secret has no tags, skip it
if len(secret.Tags) == 0 {
continue
}

// Convert tags on resource into map
resourceTags := make(map[string]string)
ignored := false
for _, tag := range secret.Tags {
// Convert tags on resource into map
resourceTags := make(map[string]string)
ignored := false
for _, tag := range secret.Tags {

if *tag.Key == ignoreTag && *tag.Value == "true" {
ignored = true
break
if *tag.Key == ignoreTag && *tag.Value == "true" {
ignored = true
break
}
resourceTags[*tag.Key] = *tag.Value
}
resourceTags[*tag.Key] = *tag.Value
}

if ignored {
continue
}
if ignored {
continue
}

// Check if resource has all required tags specified in env
hasAllTags := true
for key, value := range targetTags {
if resourceTags[key] != value {
hasAllTags = false
break
// Check if resource has all required tags specified in env
hasAllTags := true
for key, value := range targetTags {
if resourceTags[key] != value {
hasAllTags = false
break
}
}
}

if hasAllTags {
filteredSecrets = append(filteredSecrets, *secret.Name)
if hasAllTags {
filteredSecrets = append(filteredSecrets, *secret.Name)
}
}
}

Expand Down

0 comments on commit ed4e076

Please sign in to comment.