For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/ansible-aws-kms-key-with-full-permissions.md.
A documentation index is available at /llms.txt.
KMS key policies that grant broad permissions—such as Allow statements containing kms:* or wildcard principals—or that lack conditions can permit unauthorized principals to use, manage, or delete keys. This increases the risk of data exposure or loss.
For Ansible tasks using the amazon.aws.kms_key or aws_kms modules, inspect the policy property. Either omit a custom policy so the key uses a safe default, or ensure any provided policy does not include Effect: "Allow" statements that lack a Condition and contain wildcard actions like kms:* or wildcard principals (such as "*" or account-wide ARNs).
This rule flags KMS resources where a custom policy contains an Allow statement without a Condition that includes wildcard kms:* in Action or a wildcard Principal. It also flags cases where a custom policy is supplied when your organization requires the property to be undefined.
Secure examples — either omit the policy to use safer defaults or supply a restrictive policy that specifies explicit principals, limited actions, and Conditions:
- name:Create KMS key using default policyamazon.aws.kms_key:alias:alias/my-keydescription:"Encryption key for app"state:present
- name:Create KMS key with restricted policyamazon.aws.kms_key:alias:alias/my-keypolicy:Version:"2012-10-17"Statement:- Sid:"AllowSpecificUse"Effect:"Allow"Principal:AWS:"arn:aws:iam::123456789012:role/MyRole"Action:- "kms:Encrypt"- "kms:Decrypt"Resource:"*"Condition:StringEquals:aws:CalledVia:"my-allowed-service.amazonaws.com"
Compliant Code Examples
- name:Update IAM policy on an existing KMS keyamazon.aws.kms_key:alias:my-kms-keypolicy:| { Id: auto-ebs-2, Statement: [{Action: [kms:Encrypt, kms:Decrypt, kms:ReEncrypt*,
kms:GenerateDataKey*, kms:CreateGrant, kms:DescribeKey], Condition: {
StringEquals: {kms:CallerAccount: '111111111111', kms:ViaService: ec2.ap-southeast-2.amazonaws.com}},
Effect: Allow, Principal: {AWS: '*'}, Resource: '*',
Sid: Allow access through EBS for all principals in the account that are authorized to use EBS },
{ Action: [kms:Describe*, kms:Get*, kms:List*, kms:RevokeGrant], Effect: Allow,
Principal: {AWS: arn:aws:iam::111111111111:root}, Resource: '*',
Sid: Allow direct access to key metadata to the account}], Version: '2012-10-17' }state:present
Non-Compliant Code Examples
---- name:Update IAM policy on an existing KMS keyamazon.aws.kms_key:alias:my-kms-keypolicy:{'Id': 'auto-ebs-2', 'Statement':[{'Action': ['kms:*'], 'Effect': 'Allow', 'Principal':{'AWS': '*'}, 'Resource': '*', 'Sid':'Allow access through EBS for all principals in the account that are authorized to use EBS'},{'Action': ['kms:Describe*', 'kms:Get*', 'kms:List*', 'kms:RevokeGrant'], 'Effect': 'Allow', 'Principal':{'AWS': 'arn:aws:iam::111111111111:root'}, 'Resource': '*', 'Sid': 'Allow direct access to key metadata to the account'}], 'Version':'2012-10-17'}state:present
---- name:Update IAM policy on an existing KMS key2amazon.aws.kms_key:alias:my-kms-keystate:present
1
2
rulesets:- Ansible / AWS # Rules to enforce / AWS.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.