

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

# 控制對標籤的存取
<a name="tag-permissions"></a>

若要使用 API 新增、檢視和刪除標籤，委託人需要在 IAM 政策中標記許可。

您也可以使用標籤的 AWS 全域條件索引鍵來限制這些許可。在 AWS 付款密碼編譯中，這些條件可以控制標記操作的存取，例如 [TagResource](https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_TagResource.html) 和 [UntagResource](https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_UntagResource.html)。

如需政策和詳細資訊，請參閱《*IAM 使用者指南*》中的[根據標籤索引鍵控制存取](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_tags.html#access_tags_control-tag-keys)。

建立和管理標籤的許可如下所示。

**payment-cryptography：TagResource**  
允許主體新增或編輯標籤。若要在建立金鑰時新增標籤，委託人必須在 IAM 政策中擁有不限於特定金鑰的許可。

**payment-cryptography：ListTagsForResource**  
允許主體檢視金鑰上的標籤。

**payment-cryptography：UntagResource**  
允許主體從金鑰刪除標籤。

## 標記政策中的許可
<a name="tag-permission-examples"></a>

您可以在金鑰政策或 IAM 政策中提供標記許可。例如，下列範例金鑰政策為選取使用者提供金鑰的標記許可。它為所有可以擔任範例管理員或開發人員角色的使用者提供檢視標籤的許可。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Id": "example-key-policy",
  "Statement": [
    { 
      "Sid": "EnableIAMUserPermissions",
      "Effect": "Allow",
      "Principal": {"AWS": "arn:aws:iam::111122223333:root"},
      "Action": "payment-cryptography:*",
      "Resource": "*"
    },
    {
      "Sid": "AllowAllTaggingPermissions",
      "Effect": "Allow",
      "Principal": {"AWS": [
        "arn:aws:iam::111122223333:user/LeadAdmin",
        "arn:aws:iam::111122223333:user/SupportLead"
      ]},
      "Action": [
        "payment-cryptography:TagResource",
        "payment-cryptography:ListTagsForResource",
        "payment-cryptography:UntagResource"
      ],
      "Resource": "*"
    },
    {
      "Sid": "Allow roles to view tags",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::111122223333:role/Administrator",
          "arn:aws:iam::111122223333:role/Developer"
        ]
      },
      "Action": "payment-cryptography:ListTagsForResource",
      "Resource": "*"
    }
  ]
}
```

------

若要為主體提供多個金鑰的標記許可，您可以使用 IAM 政策。若要讓此政策有效，每個金鑰的金鑰政策必須允許帳戶使用 IAM 政策來控制對金鑰的存取。

例如，下列 IAM 政策允許主體建立金鑰。它還允許他們建立和管理指定帳戶中所有金鑰的標籤。此組合可讓主體使用 [CreateKey](https://docs.aws.amazon.com/payment-cryptography/latest/APIReference/API_CreateKey.html) 操作的標籤參數，在建立金鑰時將標籤新增至金鑰。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "IAMPolicyCreateKeys",
      "Effect": "Allow",
      "Action": "payment-cryptography:CreateKey",
      "Resource": "*"
    },
    {
      "Sid": "IAMPolicyTags",
      "Effect": "Allow",
      "Action": [
        "payment-cryptography:TagResource",
        "payment-cryptography:UntagResource",
        "payment-cryptography:ListTagsForResource"
      ],
      "Resource": "arn:aws:payment-cryptography:*:111122223333:key/*"
    }    
  ]
}
```

------

## 限制標籤許可
<a name="tag-permissions-conditions"></a>

您可以使用政策條件來限制標記許可。下列政策條件可套用至 `payment-cryptography:TagResource` 和 `payment-cryptography:UntagResource` 許可。例如，您可以使用 `aws:RequestTag/tag-key` 條件，允許主體僅新增特定標籤，或防止主體新增具有特定標籤索引鍵的標籤。
+ [aws:RequestTag](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-requesttag)
+ [aws:ResourceTag/*tag-key*](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-resourcetag) (僅限 IAM 政策)
+ [aws:TagKeys](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-tag-keys)

當您使用標籤來控制對金鑰的存取時，最佳實務是使用 `aws:RequestTag/tag-key`或 `aws:TagKeys`條件金鑰來判斷允許哪些標籤 （或標籤金鑰）。

例如，下列 IAM 政策與前一個類似。不過，此政策允許主體建立標籤 (`TagResource`) 並僅為具有 `Project` 標籤索引鍵的標籤刪除標籤 `UntagResource`。

由於 `TagResource` 和 `UntagResource` 請求可以包含多個標籤，您必須指定具有 [aws:TagKeys](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-tagkeys) 條件的 `ForAllValues` 或 `ForAnyValue` 集合運算子。`ForAnyValue` 運算子會要求請求中的至少一個標籤索引鍵與政策中的標籤索引鍵相符。`ForAllValues` 運算子會要求請求中的所有標籤索引鍵與政策中的其中一個標籤索引鍵相符。`ForAllValues` 運算子也會傳回 `true`，如果在請求中沒有標籤，但 TagResource 和 UntagResource 會在未指定標籤時失敗。如需集合運算子的詳細資訊，請參閱《*IAM 使用者指南*》中的[使用多個索引鍵和值](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_multi-value-conditions.html#reference_policies_multi-key-or-value-conditions)。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "IAMPolicyCreateKey",
      "Effect": "Allow",
      "Action": "payment-cryptography:CreateKey",
      "Resource": "*"
    },
    {
      "Sid": "IAMPolicyViewAllTags",
      "Effect": "Allow",
      "Action": "payment-cryptography:ListTagsForResource",
      "Resource": "arn:aws:payment-cryptography:*:111122223333:key/*"
    },
    {
      "Sid": "IAMPolicyManageTags",
      "Effect": "Allow",
      "Action": [
        "payment-cryptography:TagResource",
        "payment-cryptography:UntagResource"
      ],
      "Resource": "arn:aws:payment-cryptography:*:111122223333:key/*",
      "Condition": {
          "ForAllValues:StringEquals": {"aws:TagKeys": "Project"}
      }
    }
  ]
}
```

------