-
Notifications
You must be signed in to change notification settings - Fork 19
Add delete_access_key policy, enhance unused_access_key policy #974
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0b009cc
Enable unused_access_key policy in action
pragya811 182aaa3
Resolve pkg install error
pragya811 e124152
Resolve pkg dependency issues
pragya811 03ce378
Resolve pkg dependency issues
pragya811 f847f5a
Resolve pkg dependency issues
pragya811 4d348f7
Resolve pkg dependency issues
pragya811 2b5df99
Resolve pkg dependency issues
pragya811 ee5b3c5
Modify config value
pragya811 a96df8a
Unused access key policy changes
pragya811 5dd6d31
Review comments
pragya811 00c2ca3
Permissions for Policy changes
pragya811 4b659a5
Add delete policy, modify unused key policy
pragya811 d9987ad
Merge branch 'main' into unused-key-changes
pragya811 71d3946
Disable dry run no for unused_access_keys
pragya811 dfe482a
Unittest files
pragya811 9e75a0c
Enable delete without tag
pragya811 a55610b
delete access key policy modifications
pragya811 7ab778d
delete_access_key message changes
pragya811 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,3 +221,6 @@ empty_test_environment_variables.py | |
| cloudsensei/.env.txt | ||
| .vscode | ||
| env.yaml | ||
|
|
||
| # macOS | ||
| .DS_Store | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """ | ||
| Policy to delete inactive IAM access keys older than DELETE_ACCESS_KEY_DAYS. | ||
| """ | ||
| from cloud_governance.common.utils.configs import DELETE_ACCESS_KEY_DAYS | ||
| from cloud_governance.policy.helpers.aws.aws_policy_operations import AWSPolicyOperations | ||
|
|
||
|
|
||
| class DeleteAccessKey(AWSPolicyOperations): | ||
| RESOURCE_ACTION = "Delete" | ||
|
|
||
| def run_policy_operations(self): | ||
| """ | ||
| For inactive keys with age > DELETE_ACCESS_KEY_DAYS: apply a grace period | ||
| (deletion_grace_days = age_days - DELETE_ACCESS_KEY_DAYS, capped at DAYS_TO_TAKE_ACTION). During | ||
| grace period write to ES with cleanup_days 1..7 so send_aggregated_alerts sends | ||
| reminder emails. After grace period, delete the key. | ||
| """ | ||
| result = [] | ||
| days_to_take_action = int(self._days_to_take_action) | ||
| iam_users_access_keys = self._get_iam_users_access_keys() | ||
|
|
||
| for username, user_data in iam_users_access_keys.items(): | ||
| tags = user_data.get('tags', user_data.get('Tags', [])) | ||
| region = user_data['region'] | ||
| user_name = username | ||
|
|
||
| for access_key_label, access_key_data in user_data.items(): | ||
| if 'access key' not in access_key_label.lower(): | ||
| continue | ||
| age_days = access_key_data.get('age_days') | ||
| status = (access_key_data.get('status') or '').lower() | ||
| if age_days is None or status != 'inactive': | ||
| continue | ||
| age_days = int(age_days) | ||
|
|
||
| key_num = access_key_label.split()[-1] | ||
| inactive_tag_key = f"UnusedAccessKey{key_num}InactiveDate" | ||
| inactive_date_str = self.get_tag_name_from_tags(tags=tags, tag_name=inactive_tag_key) | ||
| delete_all_inactive = self._environment_variables_dict.get('DELETE_INACTIVE_KEYS_WITHOUT_TAG', False) | ||
| if not (age_days > DELETE_ACCESS_KEY_DAYS and (inactive_date_str or delete_all_inactive)): | ||
| continue | ||
|
|
||
| deletion_grace_days = min(age_days - DELETE_ACCESS_KEY_DAYS, days_to_take_action) | ||
| cleanup_result = self.verify_and_delete_resource( | ||
| resource_id=user_name, | ||
| tags=tags, | ||
| clean_up_days=deletion_grace_days, | ||
| access_key_label=access_key_label, | ||
| access_key_id=access_key_data.get('access_key_id'), | ||
| remove_inactive_tag=bool(inactive_date_str), | ||
| ) | ||
| resource_data = self._get_es_schema( | ||
| resource_id=user_name, | ||
| user=self.get_tag_name_from_tags(tags=tags, tag_name='User'), | ||
| skip_policy=self.get_skip_policy_value(tags=tags), | ||
| cleanup_days=deletion_grace_days, | ||
| dry_run=self._dry_run, | ||
| name=user_name, | ||
| region=region, | ||
| cleanup_result=str(cleanup_result), | ||
| resource_action=self.RESOURCE_ACTION, | ||
| cloud_name=self._cloud_name, | ||
| resource_type='UnusedAccessKey', | ||
| resource_state='Inactive', | ||
| age_days=age_days, | ||
| last_activity_days=access_key_data.get('last_activity_days'), | ||
| unit_price=0, | ||
| ) | ||
| result.append(resource_data) | ||
|
|
||
| return result |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.