Skip to content

Add delete_access_key policy, enhance unused_access_key policy#974

Open
pragya811 wants to merge 15 commits intomainfrom
unused-key-changes
Open

Add delete_access_key policy, enhance unused_access_key policy#974
pragya811 wants to merge 15 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.

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.

@pragya811
Copy link
Member Author

pragya811 commented Mar 9, 2026

[UPDATE 9 March 2026]:

  1. Created new policy, delete_access_key policy thereby separating the functionality from unused_access_key policy. Keys > 120 days age will be eligible for deletion.
  2. With DELETE_INACTIVE_KEYS_WITHOUT_TAG=true: delete any inactive key older than 120 days, even without the tag. Default set to False.
  3. Added internal grace period calculator instead of using CleanupDays tag since one single iam user can have upto 2 keys eligible for deletion/deactivation. Grace period begins post 90 days for deactivation and post 120 days for deletion upto DAYS_TO_TAKE_ACTION. User will get email alerts during this time informing of the necessary action and steps.
  4. Included custom email alert for unused_access_key and delete_access_key policy.

TBD:
Enable configs for unsed_access_key and delete_access_key

@pragya811 pragya811 marked this pull request as ready for review March 10, 2026 07:09
@pragya811 pragya811 changed the title Enable unused_access_key policy in action Add delete_access_key policy, enhance unused_access_key policy Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

3 participants