Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NO-JIRA: cleanup iamRoles as part of nightly test cleanup #4382

Merged
merged 6 commits into from
Oct 11, 2024

Conversation

NiniOak
Copy link
Contributor

@NiniOak NiniOak commented Oct 9, 2024

Changes proposed in this PR

PROBLEM

Acceptance test in CI are currently failing with error LimitExceeded: Cannot exceed quota for RolesPerAccount: 1000.

SOLUTION

Update the aws-acceptance-test-cleanup script to include cleaning up iamroles created as part of test environment setup.

How I've tested this PR

Tested locally to ensure all exisiting iAMRoles created as part of previous acceptance test runs were cleaned up using this script

How I expect reviewers to test this PR

Checklist

@NiniOak NiniOak added pr/no-changelog PR does not need a corresponding .changelog entry pr/no-backport signals that a PR will not contain a backport label labels Oct 9, 2024
@NiniOak NiniOak requested review from a team, nathancoleman and xwa153 and removed request for a team October 9, 2024 22:55
@NiniOak NiniOak changed the title NO-JIRA: cleanup iam_roles as part of nightly test cleanup NO-JIRA: cleanup iamRoles as part of nightly test cleanup Oct 9, 2024
Comment on lines 771 to 779
func(page *iam.ListRolesOutput, lastPage bool) bool {
for _, role := range page.Roles {
roleName := aws.StringValue(role.RoleName)
if strings.HasPrefix(roleName, rolePrefix) {
rolesToDelete = append(rolesToDelete, role)
}
}
return !lastPage
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we extract this to a named function and pass that in to make this a little more readable?

Comment on lines 814 to 825
}, func(page *iam.ListAttachedRolePoliciesOutput, lastPage bool) bool {
for _, policy := range page.AttachedPolicies {
_, err := iamClient.DetachRolePolicy(&iam.DetachRolePolicyInput{
RoleName: roleName,
PolicyArn: policy.PolicyArn,
})
if err != nil {
fmt.Printf("Failed to detach policy %s from role %s: %v", *policy.PolicyArn, *roleName, err)
}
}
return !lastPage
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar idea here, can we move this to a named function so that this is a bit more readable

PolicyArn: policy.PolicyArn,
})
if err != nil {
fmt.Printf("Failed to detach policy %s from role %s: %v", *policy.PolicyArn, *roleName, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to worry about the nil pointer deref here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are not de-ref the err and just check the nullity of the var, I think it's OK. Please correct me if I were wrong.

And the PR looks all good from my side! Thank you for your great work Anita!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue is on *roleName and *policy.PolicyArn which could potentially be a nil pointer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are coming from page.AttachedPolicies and page.Roles which are offered by the aws-sdk-go package, I checked the source, both are list of pointers. So maybe we could trust aws? 😂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's probably better to not trust that we're getting good intput and code defensively here, aws provides functions for dealing with pointers and we should probably use them here to avoid the potential nil derefs https://docs.aws.amazon.com/sdk-for-go/api/aws/#StringValue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I saw that and used that function to deref those values. Thanks for pointing it out

@NiniOak NiniOak requested review from jm96441n and xwa153 October 10, 2024 18:10
PolicyArn: policy.PolicyArn,
})
if err != nil {
fmt.Printf("Failed to detach policy %s from role %s: %v\n", aws.StringValue(policy.PolicyArn), *roleName, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, since we are also de-ref roleName, do we also need the aws.StringValue to wrap it? The same applies to line 835.

Copy link
Member

@nathancoleman nathancoleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just some nitpicks -- nothing to block merge

Comment on lines 768 to 776
var rolesToDelete []*iam.Role
rolePrefix := "consul-k8s-"
err := iamClient.ListRolesPagesWithContext(ctx, &iam.ListRolesInput{},
func(page *iam.ListRolesOutput, lastPage bool) bool {
return filterRolesWithPrefix(page, lastPage, &rolesToDelete, rolePrefix)
})
if err != nil {
return fmt.Errorf("failed to list roles: %v", err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#nit I think this and the filterRolesWithPrefix func get a little easier to understand with less dereferencing and whatnot if we just let the func that we're calling out to collect the roles itself.

rolesToDelete, err := filterIAMRolesWithPrefix(ctx, iamClient, "consul-k8s-")
if err != nil {
    return fmt.Errorf("failed to list roles: %w", err)
}
func filterIAMRolesWithPrefix(ctx context.Context, iamClient *iam.IAM, prefix string) ([]*iam.Role, error) {
	var roles []*iam.Role

	err := iamClient.ListRolesPagesWithContext(ctx, &iam.ListRolesInput{},
		func(page *iam.ListRolesOutput, lastPage bool) bool {
			for _, role := range page.Roles {
				name := aws.StringValue(role.RoleName)
				if strings.HasPrefix(name, prefix) {
					roles = append(roles, role)
				}
			}

			// can just always return true here since we always want to see all of the roles
			return true
		})
	if err != nil {
		return nil, err
	}

	return roles, nil
}

if len(rolesToDelete) == 0 {
fmt.Println("Found no iamRoles to clean up")
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#nit might be useful to log how many roles we're attempting to clean up in the else case

@NiniOak NiniOak enabled auto-merge (squash) October 10, 2024 22:37
@NiniOak NiniOak merged commit 8c23289 into main Oct 11, 2024
49 of 50 checks passed
@NiniOak NiniOak deleted the cleanup_iam_roles branch October 11, 2024 17:23
jm96441n pushed a commit that referenced this pull request Oct 22, 2024
* NO-JIRA: cleanup iam_roles as part of nightly test cleanup

* remove reference to log, use fmt

* fix panic in cleanup code

* code review feedback

* code review feedback
jm96441n pushed a commit that referenced this pull request Oct 22, 2024
* NO-JIRA: cleanup iam_roles as part of nightly test cleanup

* remove reference to log, use fmt

* fix panic in cleanup code

* code review feedback

* code review feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/no-backport signals that a PR will not contain a backport label pr/no-changelog PR does not need a corresponding .changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants