处理授权使用Amazon KMSAPI 和Amazon SDK for PHP版本 3 - Amazon SDK for PHP
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

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

处理授权使用Amazon KMSAPI 和Amazon SDK for PHP版本 3

授权是提供权限的另一种机制,是密钥策略的替代形式。您可以使用授权提供长期访问权限,这将允许Amazon负责人使用你的Amazon Key Management Service(Amazon KMS)客户托管 CMK. 有关更多信息,请参阅使用授权

以下示例演示如何:

Amazon SDK for PHPGitHub 上提供了的所有示例代码。

凭证

运行示例代码之前,请配置Amazon凭证,如中所述设置 凭证. 然后导入Amazon SDK for PHP,如中所述基本用法.

有关如何使用的更多信息Amazon Key Management Service(Amazon KMS),请参阅Amazon KMS开发人员指南.

创建授权

要为 Amazon KMS CMK 创建授权,请使用 CreateGrant 操作。

导入

require 'vendor/autoload.php'; use Aws\Kms\KmsClient; use Aws\Exception\AwsException;

示例代码

$KmsClient = new Aws\Kms\KmsClient([ 'profile' => 'default', 'version' => '2014-11-01', 'region' => 'us-east-2' ]); $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $granteePrincipal = "arn:aws:iam::111122223333:user/Alice"; $operation = ['Encrypt', 'Decrypt']; // A list of operations that the grant allows. try { $result = $KmsClient->createGrant([ 'GranteePrincipal' => $granteePrincipal, 'KeyId' => $keyId, 'Operations' => $operation ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

查看授权

要获取有关对 Amazon KMS CMK 的授予的详细信息,请使用 ListGrants 操作。

导入

require 'vendor/autoload.php'; use Aws\Kms\KmsClient; use Aws\Exception\AwsException;

示例代码

$KmsClient = new Aws\Kms\KmsClient([ 'profile' => 'default', 'version' => '2014-11-01', 'region' => 'us-east-2' ]); $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $limit = 10; try { $result = $KmsClient->listGrants([ 'KeyId' => $keyId, 'Limit' => $limit, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

停用授权

要停用 Amazon KMS CMK 的授权,请使用 RetireGrant 操作。在使用完授权后,请停用授权以将其清除。

导入

require 'vendor/autoload.php'; use Aws\Kms\KmsClient; use Aws\Exception\AwsException;

示例代码

$KmsClient = new Aws\Kms\KmsClient([ 'profile' => 'default', 'version' => '2014-11-01', 'region' => 'us-east-2' ]); $grantToken = 'Place your grant token here'; try { $result = $KmsClient->retireGrant([ 'GrantToken' => $grantToken, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; } //Can also identify grant to retire by a combination of the grant ID and the Amazon Resource Name (ARN) of the customer master key (CMK) $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $grantId = 'Unique identifier of the grant returned during CreateGrant operation' try { $result = $KmsClient->retireGrant([ 'GrantId' => $grantToken, 'KeyId' => $keyId, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

撤销授权

要撤销对 Amazon KMS CMK 的授权,请使用 RevokeGrant 操作。您可以撤销授予,以显式拒绝依赖它的操作。

导入

require 'vendor/autoload.php'; use Aws\Kms\KmsClient; use Aws\Exception\AwsException;

示例代码

$KmsClient = new Aws\Kms\KmsClient([ 'profile' => 'default', 'version' => '2014-11-01', 'region' => 'us-east-2' ]); $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $grantId = "grant1"; try { $result = $KmsClient->revokeGrant([ 'KeyId' => $keyId, 'GrantId' => $grantId, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }