

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

# 使用 AWS KMS API 和 适用于 PHP 的 AWS SDK 版本 3 处理授权
<a name="kms-example-grants"></a>

授权 是提供权限的另一种机制。它是密钥策略的替代形式。您可以使用授权来授予长期访问权限，允许 AWS 委托人使用您的 AWS Key Management Service (AWS KMS) 客户[AWS KMS keys](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#kms_keys)管理的权限。有关更多信息，请参阅 *AWS Key Management Service 开发人员指南*中的 [AWS KMS中的授权](https://docs.aws.amazon.com/kms/latest/developerguide/grants.html)。

以下示例演示如何：
+ 使用为 KMS 密钥创建授权[CreateGrant](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kms-2014-11-01.html#creategrant)。
+ 使用查看 KMS 密钥的授权[ListGrants](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kms-2014-11-01.html#listgrants)。
+ 使用取消对 KMS 密钥的授权[RetireGrant](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kms-2014-11-01.html#retiregrant)。
+ 使用[RevokeGrant](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kms-2014-11-01.html#revokegrant)撤销 KMS 密钥的授权。

的所有示例代码都可以在[此 适用于 PHP 的 AWS SDK 处找到 GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)。

## 凭据
<a name="examplecredentials"></a>

在运行示例代码之前，请配置您的 AWS 证书，如中所述[AWS 使用 适用于 PHP 的 AWS SDK 版本 3 进行身份验证](credentials.md)。然后导入 适用于 PHP 的 AWS SDK，如中所述[安装 适用于 PHP 的 AWS SDK 版本 3](getting-started_installation.md)。

有关使用 AWS Key Management Service (AWS KMS) 的更多信息，请参阅《[AWS KMS 开发者指南》](https://docs.aws.amazon.com/kms/latest/developerguide/)。

## 创建授权
<a name="create-a-grant"></a>

要为创建授权 AWS KMS key，请使用[CreateGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateGrant.html)操作。

 **导入** 

```
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";
}
```

## 查看授权
<a name="view-a-grant"></a>

要获取有关授予的详细信息 AWS KMS key，请使用[ListGrants](https://docs.aws.amazon.com/kms/latest/APIReference/API_ListGrants.html)操作。

 **导入** 

```
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";
}
```

## 停用授权
<a name="retire-a-grant"></a>

要取消的授权 AWS KMS key，请使用[RetireGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_RetireGrant.html)操作。在使用完授权后，请停用授权以将其清除。

 **导入** 

```
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";
}
```

## 撤销授权
<a name="revoke-a-grant"></a>

要撤消对的授权 AWS KMS key，请使用[RevokeGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_RevokeGrant.html)操作。您可以撤销授予，以显式拒绝依赖它的操作。

 **导入** 

```
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";
}
```