

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 测试您的权限
<a name="testing-permissions"></a>

要使用 AWS KMS，您必须拥有 AWS 可用于对您的 API 请求进行身份验证的凭证。此凭证必须包括访问 KMS 密钥和别名的权限。权限由密钥政策、IAM policy、授权和跨账户存取控制决定。除了控制对 KMS 密钥的访问外，您还可以控制对 CloudHSM 和自定义密钥存储的访问权限。

您可以指定 `DryRun` API 参数来确认您具有使用 AWS KMS 密钥的所需权限。您还可以使用`DryRun`来验证 AWS KMS API 调用中的请求参数指定是否正确。

**Topics**
+ [DryRun 参数是什么？](#what-is-dryrun)
+ [使用 API DryRun 进行指定](#dryrun-api)

## DryRun 参数是什么？
<a name="what-is-dryrun"></a>

 `DryRun` 是一个可选的 API 参数，您可以指定该参数来验证 AWS KMS API 调用是否成功。在实际调用 AWS KMS之前，请使用 `DryRun` 测试您的 API 调用。您可以验证如下内容。
+ 您具有使用 AWS KMS 密钥的所需权限。
+ 您已正确指定调用中的参数。

AWS KMS 支持在某些 API 操作中使用该`DryRun`参数：
+ [CreateGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateGrant.html)
+ [Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html)
+ [DeriveSharedSecret](https://docs.aws.amazon.com/kms/latest/APIReference/API_DeriveSharedSecret.html)
+ [Encrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Encrypt.html)
+ [GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html)
+ [GenerateDataKeyPair](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyPair.html)
+ [GenerateDataKeyPairWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyPairWithoutPlaintext.html)
+ [GenerateDataKeyWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyWithoutPlaintext.html)
+ [GenerateMac](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateMac.html)
+ [ReEncrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_ReEncrypt.html)
+ [RetireGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_RetireGrant.html)
+ [RevokeGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_RevokeGrant.html)
+ [Sign](https://docs.aws.amazon.com/kms/latest/APIReference/API_Sign.html)
+ [Verify](https://docs.aws.amazon.com/kms/latest/APIReference/API_Verify.html)
+ [VerifyMac](https://docs.aws.amazon.com/kms/latest/APIReference/API_VerifyMac.html)

使用 `DryRun` 参数将产生费用，并将按标准 API 请求计费。有关 AWS KMS 定价的更多信息，请参阅[AWS Key Management Service 定价](https://aws.amazon.com/kms/pricing/)。

 使用 `DryRun` 参数的所有 API 请求都适用于 API 的请求限额，如果您超过 API 请求限额，则可能会导致节流异常。例如，无论使用 `DryRun` 还是不使用 `DryRun` 调用 [Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html)，都将计入相同的加密操作限额。请参阅 [限制请求 AWS KMS](throttling.md)，了解更多信息。

对 AWS KMS API 操作的每一次调用都被捕获为事件并记录在 AWS CloudTrail 日志中。任何指定`DryRun`参数的操作的输出都会出现在您的 CloudTrail 日志中。有关更多信息，请参阅 [使用记录 AWS KMS API 调用 AWS CloudTrail](logging-using-cloudtrail.md)。

## 使用 API DryRun 进行指定
<a name="dryrun-api"></a>

要使用`DryRun`，请在支持该`—dry-run`参数的 AWS CLI 命令和 AWS KMS API 调用中指定该参数。当你这样做时， AWS KMS 将验证你的呼叫是否会成功。 AWS KMS 使用的呼叫`DryRun`将始终失败并返回一条消息，其中包含有关呼叫失败原因的信息。消息可能包括以下例外情况：
+ `DryRunOperationException` ‐ 如果 `DryRun` 未指定，则请求会成功。
+ `ValidationException` ‐ 请求因指定错误的 API 参数而失败。
+ `AccessDeniedException` ‐ 您无权在 KMS 资源上执行指定的 API 操作。

例如，以下命令使用该[CreateGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateGrant.html)操作并创建授权，允许有权担任该`keyUserRole`角色的用户[对指定的对称 KMS 密钥调用 Decry](symm-asymm-choose-key-spec.md#symmetric-cmks) [pt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html) 操作。`DryRun` 参数已指定。

```
$  aws kms create-grant \
    --key-id 1234abcd-12ab-34cd-56ef-1234567890ab \
    --grantee-principal arn:aws:iam::111122223333:role/keyUserRole \
    --operations Decrypt \
    --dry-run
```