本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 AWS KMS API 和AWS SDK for PHP版本 3 的補助金
授與是提供許可的另一個機制。它是密鑰策略的替代方案。您可以使用授權來提供長期存取權,讓AWS主體可以使用您的 AWS Key Management Service (AWS KMS) 客戶AWS KMS keys管理。如需詳細資訊,請參閱AWS Key Management Service開發人員指南AWS KMS中的授權。
下列範例示範如何:
-
使用建立 KMS 金鑰的授權CreateGrant。
-
使用檢視 KMS 金鑰的授權ListGrants。
-
使RetireGrant用淘汰 KMS 金鑰的授權。
-
使用撤銷 KMS 金鑰的授權RevokeGrant。
所有的範例程式碼都可以AWS SDK for PHP在這裡取
登入資料
在執行範例程式碼之前,請依照中的說明設定您的AWS認證憑證。然後匯入AWS SDK for PHP,如中所述基本使用。
如需使用 AWS Key Management Service (AWS KMS) 的詳細資訊,請參閱AWS KMS開發人員指南。
建立授與
若要建立授權AWS 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"; }
檢視授權
若要取得上授與的詳細資訊AWS 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"; }
退休補助金
若要淘汰的授權AWS 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"; }
撤銷授予
若要撤銷授權AWS 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"; }