本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭CreateGrant
配使用 AWS SDK或 CLI
下列程式碼範例會示範如何使用CreateGrant
。
- .NET
-
- AWS SDK for .NET
-
注意
還有更多關於 GitHub。尋找完整的範例,並瞭解如何設定和執行 AWS 代碼示例存儲庫
。 public static async Task Main() { var client = new AmazonKeyManagementServiceClient(); // The identity that is given permission to perform the operations // specified in the grant. var grantee = "arn:aws:iam::111122223333:role/ExampleRole"; // The identifier of the AWS KMS key to which the grant applies. You // can use the key ID or the Amazon Resource Name (ARN) of the KMS key. var keyId = "7c9eccc2-38cb-4c4f-9db3-766ee8dd3ad4"; var request = new CreateGrantRequest { GranteePrincipal = grantee, KeyId = keyId, // A list of operations that the grant allows. Operations = new List<string> { "Encrypt", "Decrypt", }, }; var response = await client.CreateGrantAsync(request); string grantId = response.GrantId; // The unique identifier of the grant. string grantToken = response.GrantToken; // The grant token. Console.WriteLine($"Id: {grantId}, Token: {grantToken}"); } }
-
有API關詳細資訊,請參閱 CreateGrant(AWS SDK for .NET API參考。
-
- CLI
-
- AWS CLI
-
若要建立授權
下列
create-grant
範例會建立允許使用exampleUser
者在範1234abcd-12ab-34cd-56ef-1234567890ab
例KMS索引鍵上使用decrypt
命令的授權。退休主體是adminRole
角色。只有在decrypt
要求中的加密內容包含"Department": "IT"
索引鍵值配對時,授權才會使用EncryptionContextSubset
grant 條件約束來允許此權限。aws kms create-grant \ --key-id
1234abcd-12ab-34cd-56ef-1234567890ab
\ --grantee-principalarn:aws:iam::123456789012:user/exampleUser
\ --operationsDecrypt
\ --constraintsEncryptionContextSubset={Department=IT}
\ --retiring-principalarn:aws:iam::123456789012:role/adminRole
輸出:
{ "GrantId": "1a2b3c4d2f5e69f440bae30eaec9570bb1fb7358824f9ddfa1aa5a0dab1a59b2", "GrantToken": "<grant token here>" }
若要檢視有關授權的詳細資訊,請使用
list-grants
指令。如需詳細資訊,請參閱 AWS KMS在 AWS 金鑰管理服務開發人員指南。
-
有API關詳細資訊,請參閱 CreateGrant
(AWS CLI 指令參考。
-
- Java
-
- SDK對於爪哇 2.x
-
注意
還有更多關於 GitHub。尋找完整的範例,並瞭解如何設定和執行 AWS 代碼示例存儲庫
。 /** * Grants permissions to a specified principal on a customer master key (CMK) asynchronously. * * @param keyId The unique identifier for the customer master key (CMK) that the grant applies to. * @param granteePrincipal The principal that is given permission to perform the operations that the grant permits on the CMK. * @return A {@link CompletableFuture} that, when completed, contains the ID of the created grant. * @throws RuntimeException If an error occurs during the grant creation process. */ public CompletableFuture<String> grantKeyAsync(String keyId, String granteePrincipal) { List<GrantOperation> grantPermissions = List.of( GrantOperation.ENCRYPT, GrantOperation.DECRYPT, GrantOperation.DESCRIBE_KEY ); CreateGrantRequest grantRequest = CreateGrantRequest.builder() .keyId(keyId) .name("grant1") .granteePrincipal(granteePrincipal) .operations(grantPermissions) .build(); CompletableFuture<CreateGrantResponse> responseFuture = getAsyncClient().createGrant(grantRequest); responseFuture.whenComplete((response, ex) -> { if (ex == null) { logger.info("Grant created successfully with ID: " + response.grantId()); } else { if (ex instanceof KmsException kmsEx) { throw new RuntimeException("Failed to create grant: " + kmsEx.getMessage(), kmsEx); } else { throw new RuntimeException("An unexpected error occurred: " + ex.getMessage(), ex); } } }); return responseFuture.thenApply(CreateGrantResponse::grantId); }
-
有API關詳細資訊,請參閱 CreateGrant(AWS SDK for Java 2.x API參考。
-
- Kotlin
-
- SDK對於科特林
-
注意
還有更多關於 GitHub。尋找完整的範例,並瞭解如何設定和執行 AWS 代碼示例存儲庫
。 suspend fun createNewGrant( keyIdVal: String?, granteePrincipalVal: String?, operation: String, ): String? { val operationOb = GrantOperation.fromValue(operation) val grantOperationList = ArrayList<GrantOperation>() grantOperationList.add(operationOb) val request = CreateGrantRequest { keyId = keyIdVal granteePrincipal = granteePrincipalVal operations = grantOperationList } KmsClient { region = "us-west-2" }.use { kmsClient -> val response = kmsClient.createGrant(request) return response.grantId } }
-
有API關詳細資訊,請參閱 CreateGrant
(AWS SDK對於科特林API參考。
-
- Python
-
- SDK對於 Python(肉毒桿菌 3)
-
注意
還有更多關於 GitHub。尋找完整的範例,並瞭解如何設定和執行 AWS 代碼示例存儲庫
。 class GrantManager: def __init__(self, kms_client): self.kms_client = kms_client def create_grant(self, key_id): """ Creates a grant for a key that lets a principal generate a symmetric data encryption key. :param key_id: The ARN or ID of the key. :return: The grant that is created. """ principal = input( f"Enter the ARN of a principal, such as an IAM role, to grant that role " f"GenerateDataKey permissions on key {key_id}: " ) if principal != "": try: grant = self.kms_client.create_grant( KeyId=key_id, GranteePrincipal=principal, Operations=["GenerateDataKey"], ) except ClientError as err: logger.error( "Couldn't create a grant on key %s. Here's why: %s", key_id, err.response["Error"]["Message"], ) else: print(f"Grant created on key {key_id}.") return grant else: print("Skipping grant creation.")
-
有API關詳細資訊,請參閱 CreateGrant(AWS SDK對於 Python(肉毒桿 3)API參考。
-
有關的完整列表 AWS SDK開發人員指南和代碼示例,請參閱使用 AWS KMS 用一個 AWS SDK。本主題也包含有關入門的資訊以及舊SDK版的詳細資訊。