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

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

使用 Amazon KMS API 和 Amazon SDK for PHP 版本 3 来处理授权

授权 是提供权限的另一种机制。它是密钥策略的替代形式。可以使用授权来提供长期访问权限,这将允许 Amazon 委托人使用 Amazon Key Management Service (Amazon KMS) 客户托管型 Amazon KMS keys。有关更多信息,请参阅 Amazon Key Management Service 开发人员指南中的 Amazon KMS 中的授权

以下示例演示如何:

的所有示例代码都可以在此Amazon SDK for PHP处找到 GitHub

凭证

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

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

创建授权

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

导入

require 'vendor/autoload.php'; 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 key,请使用ListGrants操作。

导入

require 'vendor/autoload.php'; 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 key,请使用RetireGrant操作。在使用完授权后,请停用授权以将其清除。

导入

require 'vendor/autoload.php'; 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 key,请使用RevokeGrant操作。您可以撤销授予,以显式拒绝依赖它的操作。

导入

require 'vendor/autoload.php'; 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"; }