

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 測試您的許可
<a name="testing-permissions"></a>

若要使用 AWS KMS，您必須擁有 AWS 可用於驗證 API 請求的登入資料。憑證必須包含存取 KMS 金鑰與別名的許可。許可由金鑰政策、IAM 政策、授權以及跨帳戶存取控制決定。除控制 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)
+ [解密](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html)
+ [DeriveSharedSecret](https://docs.aws.amazon.com/kms/latest/APIReference/API_DeriveSharedSecret.html)
+ [加密](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)
+ [符號](https://docs.aws.amazon.com/kms/latest/APIReference/API_Sign.html)
+ [確認](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 KMS API 呼叫中 AWS CLI 指定 參數。當您這麼做時， 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 金鑰](symm-asymm-choose-key-spec.md#symmetric-cmks)呼叫 [Decrypt](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
```