

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS KMS API と AWS SDK for PHP バージョン 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)」を参照してください。

以下の例では、次の方法を示しています。
+ [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) を使用して KMS キーの許可を無効にする。
+ [RevokeGrant](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kms-2014-11-01.html#revokegrant) を使用して KMS キーの許可を取り消す。

のすべてのサンプルコード AWS SDK for PHP は[GitHub で入手できます](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)。

## 認証情報
<a name="examplecredentials"></a>

サンプルコードを実行する前に、「」の説明に従って AWS 認証情報を設定します[AWS SDK for PHP バージョン 3 AWS を使用した での認証](credentials.md)。次に AWS SDK for PHP、「」の説明に従って をインポートします[AWS SDK for PHP バージョン 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";
}
```