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

Adding _remove_permissions_from_roles() and extracting parts of repo_role() into shared methods. #213

Merged
merged 7 commits into from
Mar 28, 2019

Conversation

scriptsrc
Copy link
Contributor

Adding _remove_permissions_from_roles() and extracting parts of repo_role() into shared methods.

Also updates the policy size checking to correctly remove all whitespace before checking the size.

Need a code review with @mcpeak.

@@ -48,6 +49,7 @@
from cloudaux.aws.iam import (delete_role_policy, get_account_authorization_details, get_role_inline_policies,
put_role_policy)
from cloudaux.aws.sts import sts_conn
from policyuniverse import ARN
Copy link
Contributor

Choose a reason for hiding this comment

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

please keep this in alphabetical order grouped by import type (stdlib, third party libs, module imports)



def _check_inline_policies_size(policies):
"""Validate the policies, when converted to JSON without whitespace, remain under the size limit."""
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment says validate policies remain under the limit but you return True when the size exceeds the limit.

role_name = arn.name.split('/')[-1]

role_id = find_role_in_cache(dynamo_table, account_number, role_name)
_remove_permissions_from_role(account_number, permissions, role_name, role_id, dynamo_table, config, hooks, commit=commit)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer putting the hook calling here so it's easier to see that it's happening.



def _remove_permissions_from_role(account_number, permissions, role_name, role_id, dynamo_table, config, hooks, commit=False):
"""Remove the given permissions from the given role.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is permissions here a single permission? A list of permissions? It's called permissions but below you reference "the selected permission".

@@ -1052,6 +1155,12 @@ def main():
if args.get('find_roles_with_permission'):
return find_roles_with_permission(args.get('<permission>'), dynamo_table)

if args.get('remove_permissions_from_roles'):
permissions = args.get('--permissions')
roles = args.get('--role-filename')
Copy link
Contributor

Choose a reason for hiding this comment

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

roles seems like a list of roles but it's a filename, make the variable name consistent.

@@ -1052,6 +1155,12 @@ def main():
if args.get('find_roles_with_permission'):
return find_roles_with_permission(args.get('<permission>'), dynamo_table)

if args.get('remove_permissions_from_roles'):
permissions = args.get('--permissions')
roles = args.get('--role-filename')
Copy link
Contributor

Choose a reason for hiding this comment

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

From a user perspective, where is the file supposed to come from? Do we want to provide other options to find the roles since we have a database?

@JohnVonNeumann
Copy link
Contributor

In line with #44 should all new functions be following a standard around docstrings? Is this a situation where development should start as it means to go on?

@@ -48,6 +49,7 @@
from cloudaux.aws.iam import (delete_role_policy, get_account_authorization_details, get_role_inline_policies,
put_role_policy)
from cloudaux.aws.sts import sts_conn
from policyuniverse.arn import ARN
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: alphabetise the imports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should probably have flake8 enforce import order if it's something we care about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But yes, as @mcpeak pointed this out, I will fix.

def _check_access_advisor_age(role, role_name, account_number, max_days_old):
"""Ensure access advisor data has been recently refreshed."""
old_aa_data_services = []
for aa_service in role.aa_data:
Copy link
Contributor

Choose a reason for hiding this comment

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

can this block be cleaned up using some variables for the various times/periods so that the arithmetic is easier to understand from a first look.

ie:

last_updated_time = datetime.datetime.strptime(aa_service['lastUpdated'], '%a, %d %b %Y %H:%M:%S %Z')
current_time = datetime.datetime.now()
expiration_time = datetime.timedelta(days=max_days_old)

        if( last_updated_time < current_time - expiration_time):
                old_aa_data_services.append(aa_service['serviceName'])

Probably need to play with var names, I'm tired and not picking the correct words/context for the block, but you get the idea of what I'm trying to convey.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was originally inline for the repo_role() method. I refactored it into it's own method thinking I would need this code for the main method that I am adding, but it turns out that I don't. I'll move this back into repo_role(), but won't be otherwise fixing the naming.

Not in this PR, anyways.

@scriptsrc
Copy link
Contributor Author

Thank you @JohnVonNeumann and @mcpeak for the review. I'll work on many of these items for this PR. Others are broader concerns that I may follow up on in other PRs.

Again, thanks.

@coveralls
Copy link

coveralls commented Mar 27, 2019

Coverage Status

Coverage increased (+3.4%) to 58.221% when pulling 0dc1149 on remove_specific_permission into ea37f65 on master.

@scriptsrc
Copy link
Contributor Author

scriptsrc commented Mar 27, 2019

Things I think I've completed:

  • _check_inline_policy_size() method name & return value not aligned.
  • import order now different
  • Permission used singular and plural in new code (make all plural)
  • Role filename variable not well named
  • Get find_roles_with_permission() to optionally output to a format that can be used with remove_permissions_from_roles()
  • Regularize docstrings for new methods.
  • move hook to upper level method
  • Move the contents of _check_access_advisor_age() back into repo_role() (No need to pull this into a separate method now.)

Todo:

  • Update Documentation to describe the new flow
  • Look at writing unit tests for this new flow
  • Functional tests (deploy and make sure old things and new things still work)

@scriptsrc
Copy link
Contributor Author

This PR has been put through it's paces in our test environment.

  • update_role_cache
  • display_role_cache
  • display_role
  • repo_role
  • rollback_role
  • find_roles_with_permissions
  • remove_permissions_from_roles

Seems to work as expected.

Future work may be to have the remove_permissions_from_roles flow not touch statements or policies that don't have the provided permission. Right now it will expand the wildcards on those otherwise unrelated statements, which cause problems with the policy size limit.

@scriptsrc scriptsrc merged commit 439f346 into master Mar 28, 2019
@scriptsrc scriptsrc deleted the remove_specific_permission branch March 28, 2019 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants