putKeyPolicy method
Attaches a key policy to the specified KMS key.
For more information about key policies, see Key Policies in the Key Management Service Developer Guide. For help writing and formatting a JSON policy document, see the IAM JSON Policy Reference in the Identity and Access Management User Guide . For examples of adding a key policy in multiple programming languages, see Use PutKeyPolicy with an Amazon Web Services SDK or CLI in the Key Management Service Developer Guide.
Cross-account use: No. You cannot perform this operation on a KMS key in a different Amazon Web Services account.
Required permissions: kms:PutKeyPolicy (key policy)
Related operations: GetKeyPolicy
Eventual consistency: The KMS API follows an eventual consistency model. For more information, see KMS eventual consistency.
May throw DependencyTimeoutException.
May throw InvalidArnException.
May throw KMSInternalException.
May throw KMSInvalidStateException.
May throw LimitExceededException.
May throw MalformedPolicyDocumentException.
May throw NotFoundException.
May throw UnsupportedOperationException.
Parameter keyId :
Sets the key policy on the specified KMS key.
Specify the key ID or key ARN of the KMS key.
For example:
-
Key ID:
1234abcd-12ab-34cd-56ef-1234567890ab -
Key ARN:
arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab
Parameter policy :
The key policy to attach to the KMS key.
The key policy must meet the following criteria:
-
The key policy must allow the calling principal to make a subsequent
PutKeyPolicyrequest on the KMS key. This reduces the risk that the KMS key becomes unmanageable. For more information, see Default key policy in the Key Management Service Developer Guide. (To omit this condition, setBypassPolicyLockoutSafetyCheckto true.) - Each statement in the key policy must contain one or more principals. The principals in the key policy must exist and be visible to KMS. When you create a new Amazon Web Services principal, you might need to enforce a delay before including the new principal in a key policy because the new principal might not be immediately visible to KMS. For more information, see Changes that I make are not always immediately visible in the Amazon Web Services Identity and Access Management User Guide.
Implementation
Future<void> putKeyPolicy({
required String keyId,
required String policy,
bool? bypassPolicyLockoutSafetyCheck,
String? policyName,
}) async {
final headers = <String, String>{
'Content-Type': 'application/x-amz-json-1.1',
'X-Amz-Target': 'TrentService.PutKeyPolicy'
};
await _protocol.send(
method: 'POST',
requestUri: '/',
exceptionFnMap: _exceptionFns,
// TODO queryParams
headers: headers,
payload: {
'KeyId': keyId,
'Policy': policy,
if (bypassPolicyLockoutSafetyCheck != null)
'BypassPolicyLockoutSafetyCheck': bypassPolicyLockoutSafetyCheck,
if (policyName != null) 'PolicyName': policyName,
},
);
}