This rule checks if the managed AWS Identity and Access Management (IAM) policies that you create do not allow blocked actions on AWS Key Management Service (KMS) keys. The rule is NON_COMPLIANT if any blocked action is allowed on AWS KMS keys by the managed IAM policy. Note that this rule does not evaluate the conditions provided in IAM policies.
To prevent blocked KMS actions in IAM policies using the AWS Management Console, follow these steps:
Navigate to IAM Policies:
Open the AWS Management Console.
In the navigation pane, choose “Policies” under the “Access management” section.
Create or Edit a Policy:
To create a new policy, click on the “Create policy” button.
To edit an existing policy, find the policy you want to modify and click on its name, then click the “Edit policy” button.
Add KMS Permissions:
In the policy editor, switch to the “JSON” tab.
Ensure that the policy includes the necessary KMS actions (e.g., kms:Encrypt, kms:Decrypt, kms:GenerateDataKey) and does not include any actions that should be blocked.
After adding the necessary permissions, click on the “Review policy” button.
Provide a name and description for the policy if creating a new one.
Click on the “Create policy” or “Save changes” button to apply the policy.
By following these steps, you can ensure that your IAM policies are correctly configured to allow necessary KMS actions while preventing any blocked actions.
Using CLI
To prevent blocked KMS actions in IAM policies using AWS CLI, you need to ensure that your IAM policies are correctly configured to allow necessary KMS actions and deny any inappropriate ones. Here are the steps to achieve this:
Create a JSON Policy Document:
First, create a JSON policy document that specifies the allowed and denied KMS actions. Save this document locally, for example, as kms_policy.json.
Use the AWS CLI to create a new IAM policy or update an existing one with the JSON policy document.
Copy
Ask AI
aws iam create-policy --policy-name MyKMSPolicy --policy-document file://kms_policy.json
If you need to update an existing policy, use:
Copy
Ask AI
aws iam update-policy --policy-arn arn:aws:iam::aws:policy/MyKMSPolicy --policy-document file://kms_policy.json
Attach the Policy to IAM Users, Groups, or Roles:
Attach the newly created or updated policy to the relevant IAM users, groups, or roles.
Copy
Ask AI
aws iam attach-user-policy --user-name MyUser --policy-arn arn:aws:iam::aws:policy/MyKMSPolicy
Similarly, for groups or roles:
Copy
Ask AI
aws iam attach-group-policy --group-name MyGroup --policy-arn arn:aws:iam::aws:policy/MyKMSPolicy
Copy
Ask AI
aws iam attach-role-policy --role-name MyRole --policy-arn arn:aws:iam::aws:policy/MyKMSPolicy
Verify the Policy Attachment:
Verify that the policy has been correctly attached to the IAM users, groups, or roles.
Copy
Ask AI
aws iam list-attached-user-policies --user-name MyUser
Similarly, for groups or roles:
Copy
Ask AI
aws iam list-attached-group-policies --group-name MyGroup
Copy
Ask AI
aws iam list-attached-role-policies --role-name MyRole
By following these steps, you can ensure that your IAM policies are configured to allow necessary KMS actions while blocking potentially harmful ones.
Using Python
To prevent blocked KMS actions in IAM policies using Python scripts, you can use the AWS SDK for Python (Boto3). Here are the steps to ensure that KMS actions are properly set in IAM policies:
Install Boto3:
Ensure you have Boto3 installed in your Python environment. You can install it using pip if you haven’t already:
Copy
Ask AI
pip install boto3
Initialize Boto3 Client:
Initialize the Boto3 client for IAM and KMS.
Retrieve and Update IAM Policies:
Retrieve the IAM policies and check for KMS actions. If they are not set correctly, update the policy.
Copy
Ask AI
def get_iam_policies(): paginator = iam_client.get_paginator('list_policies') for response in paginator.paginate(Scope='Local'): for policy in response['Policies']: policy_arn = policy['Arn'] policy_version = iam_client.get_policy_version( PolicyArn=policy_arn, VersionId=policy['DefaultVersionId'] ) yield policy_arn, policy_version['PolicyVersion']['Document']def update_policy(policy_arn, policy_document): # Add or modify the KMS actions in the policy document as needed # Example: Adding a statement to allow KMS actions kms_statement = { "Effect": "Allow", "Action": [ "kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey" ], "Resource": "*" } policy_document['Statement'].append(kms_statement) # Update the policy with the new document iam_client.create_policy_version( PolicyArn=policy_arn, PolicyDocument=json.dumps(policy_document), SetAsDefault=True )for policy_arn, policy_document in get_iam_policies(): update_policy(policy_arn, policy_document)
Validate the Changes:
Ensure that the changes have been applied correctly by validating the updated policies.
Copy
Ask AI
def validate_policies(): for policy_arn, policy_document in get_iam_policies(): for statement in policy_document['Statement']: if 'kms:' in statement['Action']: print(f"KMS actions are correctly set in policy: {policy_arn}") else: print(f"KMS actions are missing in policy: {policy_arn}")validate_policies()
This script will help you ensure that KMS actions are properly set in IAM policies by retrieving existing policies, updating them if necessary, and validating the changes.
Assistant
Responses are generated using AI and may contain mistakes.