IAM User Access Key
Automatically rotate AWS access keys using Keeper Secrets Manager rotations
Overview
This documentation explains how to rotate AWS IAM user access keys using KeeperPAM's rotation option called "Run PAM scripts only". This is a setting in the PAM User rotation settings which tells the Gateway to skip the primary rotation method and directly execute the post-rotation script attached to the PAM User record in the vault.
This guide includes prerequisites, step-by-step instructions, and a Python script example. This provided script supports both provided admin credentials (AWS Access key for an admin account) and EC2 instance role authentication. The script ensures secure access key rotation, including deletion of previous user keys. The new key is stored in the Keeper record after rotation is complete.
Prerequisites
KSM role: Ensure that the Keeper user has a role providing access to Keeper Secrets Manager, and Keeper Secrets Manager rotations.
A Linux instance to run the Keeper Gateway: The Gateway can be deployed in an EC2 instance, any private Cloud or on-prem. This script is capable of leveraging EC2 Instance Roles Authentication to perform the Access Key rotation if the Gateway runs in an EC2 instance. If the gateway runs somewhere else, then an Access Key with role privilege needs to be provided in the Keeper vault to perform the rotation of the end user Access Key.
Rotation Script Logic Flow
1. Admin Credentials Retrieval
The script retrieve admin credentials in three ways:
Record directly attached to the post rotation script.
The access key provided to the AWS PAM config selected for the rotation. This will be used if no access key is found in the record attached (method 1 above) to the post rotation script.
Uses AWS instance role authentication if no credentials are provided from either methods above. The gateway needs to be running on an EC2 Instance with an EC2 Instance Role in place.
2. Key Rotation Logics
The script provides two modes of operation based on the delete_all_keys_before_rotating custom field:
If False (default), a new access key is created first, then the old ones are deleted, keeping only the newly created key. This will fail if the user account has already two access keys: AWS will not allow the script to create a third one.
If this custom field is set to True, the script deletes all existing access keys for the IAM user before creating a new one. This helps in the scenario described above where the end user account has already two access keys.
3. Updating the Keeper PAM User Record in the Vault
After key rotation, the script updates the rotated PAM User Keeper record with the new AWS access key ID, secret access key, creation date, and any deleted access keys IDs.
PAM User Record - Fields Requirements
You need to create a PAM User record where the rotation will be configured later on. The fields below need to be created.
Fields required:
Login
Name of the user account in AWS where the access key needs to be rotated.
Password
It will be a dummy value in this case. The password field gets automatically rotated, but it is not used anywhere. This is still required field.
Custom fields required:
Text
This field will receive the new access key id after the rotation.
Hidden Field
This field will receive the new secret access key after the rotation.
Text
This field will contain the timestamp of when the new key has been generated by the script.
Text
This field will contain the old access key id(s) removed from the user account in AWS during the rotation.
Text
This custom field is optional. It could be set to “False” or “True”, the default value is “False”.
If set to “True” then the rotation script will start by deleting all existing access keys on the user account before creating a new one. This is needed when the user has already 2 access keys setup. AWS will not allow the script to create a third one, hence the need to delete the existing keys before adding a new one.
Text
This rotation requires the gateway to only execute the rotation script, and not try to rotate something using the built-in rotation features.
The value has to be:
Text
Second field to enable NOOP.
The value has to be:

Setting Up the Rotation in the Keeper Vault
When the gateway runs in an EC2 instance, you don’t need to provide an admin access key to the script. The gateway will leverage the AWS Instance Role permissions assigned to the VM.
The steps below explains how to set up an EC2 Instance role to the gateway EC2 instance with minimal permissions:
Steps to Add/Configure Instance Role With Minimum Permissions to Rotate Access Keys:
Step 1: Create a Policy in AWS
Go to the IAM Management Console.
Select Policies and click Create policy.
Select JSON and paste the following, make sure to replace your AWS Account ID:

Name the policy and save it.
Step 2: Create an IAM Role in AWS
Go to the IAM Management Console.
Select Roles and click create role.
Choose AWS service and select EC2.

Attach the necessary IAM policies (e.g., the policy we created above with the minimum permissions).

Step 3: Assign the Role to Your EC2 Instance
In the EC2 Management Console, find your instance.
Click Actions > Security > Modify IAM Role.

Select the role you created and click Update

Verify Instance Role Permissions
You can ensure that the instance role has appropriate permissions to interact with IAM by running the command below on the Gateway EC2 Instance:
Rotation Configuration From the Vault:
Create a shared folder in the vault
Create a PAM User record in the shared folder with the fields and custom fields described above.
In the Secret Manager tab of the Keeper vault, create a new application for the gateway if there is no gateway yet.
Make sure the Application has edit permissions on the shared folder created above.
Provision the gateway (gateway tab after selecting the application) on an EC2 instance. On the EC2 Instance run the install command provided by the Keeper vault and make sure boto3 and keeper_secrets_manager_core are installed by running the following commands in the EC2 instance:
In the Secret Manager tab of the Keeper vault, go to the PAM Configurations tab. Create a new PAM configuration if needed.
Under Environment you can select “Local Network” or “AWS”. If you select “AWS”, please make sure to leave the “Access Key” and “Secret Access Key” field empty. If you provide one, it will be automatically used by the script instead of using the Instance Role authentication. You will still need to provide the AWS Account ID to the AWS PAM configuration.
Select the gateway, select the shared folder and save the PAM configuration.

Edit the PAM User record previously described in this documentation:
Password Rotation Settings: select your desired schedule and the PAM configuration created above.
Add PAM Script to the record: select the provided file below and make sure to specify the script command:

Python Script
Last updated
Was this helpful?