

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

# AWS 付款密碼編譯的資源型政策
<a name="security_iam_resource-based-policies"></a>

以資源為基礎的政策是您連接到資源的 JSON 政策文件，例如 AWS 付款密碼編譯金鑰。在資源型政策中，您可以指定誰可以存取金鑰，以及他們可以對其執行的動作。您可以使用資源型政策：
+ 將單一金鑰的存取權授予多個使用者和角色。
+ 將存取權授予其他 AWS 帳戶中的使用者或角色。

**Topics**
+ [考量事項](#security_iam_resource-based-policies-considerations)
+ [管理以資源為基礎的政策](#security_iam_resource-based-policies-manage)
+ [資源型政策範例](#security_iam_resource-based-policies-examples)

當您將資源型政策連接至 AWS 付款密碼編譯金鑰時， AWS 付款密碼編譯會使用 IAM 政策評估邏輯來判斷指定的委託人是否有權執行請求的動作。若要啟用跨帳戶存取，您可以指定在其他帳戶內的所有帳戶或 IAM 實體，作為[資源型政策的委託人](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_principal.html)。跨帳戶存取需要兩個政策：

1. *資源型政策 （金鑰擁有者的帳戶）* — 金鑰擁有者使用 `PutResourcePolicy` 授予呼叫者帳戶或 IAM 主體的存取權。

1. *身分型政策 （來電者的帳戶）* — 發起人的 IAM 管理員也必須在發起人的 IAM 政策中允許 AWS 付款密碼編譯動作 （例如 `payment-cryptography:EncryptData`)。

這兩個政策都必須允許 動作。如果其中一個遺失，則使用 拒絕跨帳戶請求`AccessDeniedException`。

如果以資源為基礎的政策授予相同帳戶中的委託人存取權，則不需要額外的以身分為基礎的政策。如需詳細資訊，請參閱《*IAM 使用者指南*》中的 [IAM 角色與以資源為基礎的原則有何差異](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_compare-resource-policies.html)。

**資源政策控制平面操作**  
以資源為基礎的政策不適用於資源政策控制平面操作[https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_GetResourcePolicy.html](https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_GetResourcePolicy.html)，例如 [https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_PutResourcePolicy.html](https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_PutResourcePolicy.html)、 和 [https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_DeleteResourcePolicy.html](https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_DeleteResourcePolicy.html)。這可防止潛在鎖定案例，其中資源政策可能會拒絕修改或移除政策本身的能力。存取這些控制平面操作僅受 IAM 身分型政策的約束。

## 考量事項
<a name="security_iam_resource-based-policies-considerations"></a>

將資源型政策與 AWS 付款密碼編譯搭配使用時，請記住下列事項。
+ AWS 付款密碼編譯會自動強制不公開存取金鑰。您無法建立將存取權授予匿名或公有主體的資源型政策。所有對 AWS Payment Cryptography 金鑰的存取都需要經過驗證的 AWS 主體，而且一律會封鎖公開存取。
+ 每個金鑰套用以資源為基礎的政策。每個 AWS 付款密碼編譯金鑰最多可連接一個以資源為基礎的政策。
+ 資源型政策不適用於別名。當您依索引鍵別名參考索引鍵時，會評估連接至基礎索引鍵的資源政策。
+ 以資源為基礎的政策目前不適用於使用多區域金鑰複寫建立的唯讀複本區域金鑰。資源政策只能連接到主要區域金鑰。
+ 資源型政策中的 `Resource`元素必須與政策連接之金鑰的 ARN 相符`"*"`或完全相符。`"*"` 建議使用 ，因為它允許在多個金鑰之間重複使用相同的政策文件。
+ 資源政策管理 APIs(`PutResourcePolicy`、 `GetResourcePolicy`和 `DeleteResourcePolicy`) 僅限於 AWS 帳戶 擁有金鑰的 。只有金鑰擁有者帳戶中的主體才能管理資源政策。

## 管理以資源為基礎的政策
<a name="security_iam_resource-based-policies-manage"></a>

您可以使用 AWS CLI 或 AWS API 管理 AWS 付款密碼編譯金鑰的資源型政策。若要使用此命令，請以您自己的資訊取代範例命令中的{{斜體預留位置文字}}。

**連接以資源為基礎的政策**  
使用 [https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_PutResourcePolicy.html](https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_PutResourcePolicy.html) API 動作或 [https://docs.aws.amazon.com/cli/latest/reference/payment-cryptography/put-resource-policy.html](https://docs.aws.amazon.com/cli/latest/reference/payment-cryptography/put-resource-policy.html) CLI 命令，將資源型政策連接至金鑰。如果政策已存在，命令會取代它。

下列範例會將資源型政策從 JSON 檔案連接至金鑰。

```
aws payment-cryptography put-resource-policy \
    --resource-arn arn:aws:payment-cryptography:{{us-east-2}}:{{111122223333}}:key/{{kwapwa6qaifllw2h}} \
    --policy file://{{policy.json}}
```

**擷取以資源為基礎的政策**  
使用 [https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_GetResourcePolicy.html](https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_GetResourcePolicy.html) API 動作或 [https://docs.aws.amazon.com/cli/latest/reference/payment-cryptography/get-resource-policy.html](https://docs.aws.amazon.com/cli/latest/reference/payment-cryptography/get-resource-policy.html) CLI 命令來擷取連接到金鑰的資源型政策。

下列範例會擷取連接至金鑰的資源型政策。

```
aws payment-cryptography get-resource-policy \
    --resource-arn arn:aws:payment-cryptography:{{us-east-2}}:{{111122223333}}:key/{{kwapwa6qaifllw2h}}
```

回應會傳回政策文件：

```
{
    "Policy": {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Principal": {
                    "AWS": "arn:aws:iam::{{111122223333}}:role/{{ExampleRole}}"
                },
                "Action": [
                    "payment-cryptography:EncryptData",
                    "payment-cryptography:DecryptData"
                ],
                "Resource": "*"
            }
        ]
    }
}
```

**刪除以資源為基礎的政策**  
使用 [https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_DeleteResourcePolicy.html](https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_DeleteResourcePolicy.html) API 動作或 [https://docs.aws.amazon.com/cli/latest/reference/payment-cryptography/delete-resource-policy.html](https://docs.aws.amazon.com/cli/latest/reference/payment-cryptography/delete-resource-policy.html) CLI 命令，從金鑰中移除資源型政策。

下列範例會刪除連接至金鑰的資源型政策。

```
aws payment-cryptography delete-resource-policy \
    --resource-arn arn:aws:payment-cryptography:{{us-east-2}}:{{111122223333}}:key/{{kwapwa6qaifllw2h}}
```

## 資源型政策範例
<a name="security_iam_resource-based-policies-examples"></a>

### 授予金鑰的跨帳戶存取權
<a name="security_iam_resource-based-policies-cross-account"></a>

下列資源型政策授予另一個 AWS 帳戶中的角色許可，以使用 AWS 付款密碼編譯金鑰進行密碼編譯操作。

```
{
    "Version": "2012-10-17", 		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::{{111122223333}}:role/{{ExampleRole}}"
            },
            "Action": [
                "payment-cryptography:GenerateCardValidationData",
                "payment-cryptography:VerifyCardValidationData"
            ],
            "Resource": "*"
        }
    ]
}
```

### 將不同的許可授予不同的帳戶
<a name="security_iam_resource-based-policies-restrict-actions"></a>

下列資源型政策示範如何將不同的許可授予不同帳戶中的委託人。在此範例中，一個帳戶中的 3DS 存取控制伺服器 (ACS) 可以產生卡片驗證資料，而不同帳戶中的付款授權服務只能驗證 3DS 加密。

```
{
    "Version": "2012-10-17", 		 	 	 
    "Statement": [
        {
            "Sid": "Allow3DSACSToGenerate",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::{{111122223333}}:role/{{3dsAcsRole}}"
            },
            "Action": [
                "payment-cryptography:GenerateCardValidationData"
            ],
            "Resource": "*"
        },
        {
            "Sid": "AllowPaymentAuthToVerify",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::{{444455556666}}:role/{{PaymentAuthRole}}"
            },
            "Action": [
                "payment-cryptography:VerifyAuthRequestCryptogram"
            ],
            "Resource": "*"
        }
    ]
}
```