Skip to content

Enable unused_access_key policy in action#974

Draft
pragya811 wants to merge 10 commits intomainfrom
unused-key-changes
Draft

Enable unused_access_key policy in action#974
pragya811 wants to merge 10 commits intomainfrom
unused-key-changes

Conversation

@pragya811
Copy link
Member

Type of change

Note: Fill x in []

  • bug
  • enhancement
  • documentation
  • dependencies

Description

  1. Enable unused_access_key policy in action, set dry_run to 'no'.
  2. Change days to take action from 90 to 180

For security reasons, all pull requests need to be approved first before running any automated CI

@pragya811 pragya811 self-assigned this Feb 25, 2026
@pragya811 pragya811 requested review from ebattat and inntran February 25, 2026 10:16
CLOUDWATCH_METRICS_AVAILABLE_DAYS = 14
AWS_DEFAULT_GLOBAL_REGION = 'us-east-1'
UNUSED_ACCESS_KEY_DAYS = 90
UNUSED_ACCESS_KEY_DAYS = 180
Copy link
Member

Choose a reason for hiding this comment

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

UNUSED_ACCESS_KEY_DAYS = 90
DELETE_ACCESS_KEY_DAYS = 180

Copy link
Member Author

Choose a reason for hiding this comment

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

changed to 90, For delete access key policy, will raise a separate PR

Copy link
Member

Choose a reason for hiding this comment

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

ok thx

@ebattat
Copy link
Member

ebattat commented Feb 25, 2026

@inntran, any comment ?

@inntran
Copy link
Collaborator

inntran commented Feb 25, 2026

I wish we could move configurations from Python files to YAML. Other than that, we can not fix all code smells at this time, so let it be.

@pragya811 pragya811 marked this pull request as draft March 2, 2026 08:04
@pragya811
Copy link
Member Author

Changes:

  1. Added code changes for sending email reminders to users with inactive access keys. Between 80 - 90 days, the user will get 2 reminders to take action on the unused key. Keys > 120 days of age and inactive will be eligible for deletion as mentioned above.

  2. Keys older than 90 days are deactivated (after any grace period) and tagged with UnusedAccessKeyNInactiveDate.

  3. Deletion:
    Default: delete only inactive keys that have this policy’s tag and have been inactive for more than 120 days.
    With DELETE_INACTIVE_KEYS_WITHOUT_TAG=true: delete any inactive key older than 120 days, even without the tag.

image

@pragya811 pragya811 requested a review from ebattat March 4, 2026 09:23
Copy link
Member

@ebattat ebattat left a comment

Choose a reason for hiding this comment

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

ADD .DS_Store to git ignore

raise err

def untag_user(self, user_name: str, tag_keys: list):
"""
Copy link
Member

Choose a reason for hiding this comment

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

why we need to untag user ?

Copy link
Member Author

Choose a reason for hiding this comment

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

After we delete the access keys, we will remove the tags 'UnusedAccessKey1InactiveDate', etc which we added to send email reminders.

self.tag_user(username, [{'Key': inactive_tag_key, 'Value': inactive_date}])
except Exception as e:
logger.error(f"Failed to deactivate access key '{access_key_id}' for user '{username}': {e}")
else:
Copy link
Member

Choose a reason for hiding this comment

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

Add .DS_Store to git ignore

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

if should_delete:
if self._dry_run == 'no':
try:
self._delete_inactive_access_key(user_name, access_key_label)
Copy link
Member

Choose a reason for hiding this comment

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

We should send email alert before deleting the access key

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the part about deletion for keys aged > 120 days in the email alert reminder before deactivation.

tag_key = f"UnusedAccessKey{access_key_label.split()[-1]}ReminderCount"
to_user = self.get_tag_name_from_tags(tags=tags, tag_name='User') or user_name
display_name = self._mail_message.get_user_ldap_details(user_name=to_user) or to_user
subject, body = self._mail_message.unused_access_key_reminder(
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should mention that we will delete the access key after 120 days, if no key rotation is done.

if reminder_val in ('1', '2'):
reminder_count = int(reminder_val)
if reminder_count < 2 and self._dry_run == 'no':
self._send_reminder_and_update_tag(
Copy link
Member

Choose a reason for hiding this comment

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

Unused Access Key Already Participates in Alerts in send_aggregated_alerts.py, why we need to send it separately ?

Copy link
Member Author

Choose a reason for hiding this comment

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

send_aggregated_alerts.py sends alerts post deactivation (>90 days) based on Cleanup Days. Hence added a separate email alert to alert the user twice before UNUSED_ACCESS_KEY_REMINDER_DAYS.

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.

3 participants