Utilisation d'octrois - AWS Key Management Service

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Utilisation d'octrois

Les exemples de cette rubrique utilisent l'API AWS KMS pour créer, afficher, retirer et révoquer des octrois sur les AWS KMS keys. Pour de plus amples informations sur l'utilisation des octrois dans AWS KMS, veuillez consulter Octrois dans AWS KMS.

Création d'un octroi

Pour créer une subvention pour unAWS KMS key, utilisez l'CreateGrantopération. La réponse inclut uniquement l'ID d'octroi et le jeton d'octroi. Pour obtenir des informations détaillées sur la subvention, utilisez l'ListGrantsopération, comme indiqué dansAffichage d'un octroi.

Ces exemples créent une autorisation qui permet aux utilisateurs qui peuvent assumer le ExampleKeyUser rôle d'appeler l'GenerateDataKeyopération sur la clé KMS identifiée par le KeyId paramètre.

Dans les langues qui nécessitent un objet client, ces exemples utilisent l'objet client AWS KMS que vous avez créé dans Création d'un client.

Java

Pour obtenir des détails, veuillez consulter la méthode createGrant dans la Référence d'API AWS SDK for Java.

// Create a grant // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String granteePrincipal = "arn:aws:iam::111122223333:role/ExampleKeyUser"; String operation = GrantOperation.GenerateDataKey.toString(); CreateGrantRequest request = new CreateGrantRequest() .withKeyId(keyId) .withGranteePrincipal(granteePrincipal) .withOperations(operation); CreateGrantResult result = kmsClient.createGrant(request);
C#

Pour obtenir des détails, consultez la méthode CreateGrant dans AWS SDK for .NET.

// Create a grant // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String granteePrincipal = "arn:aws:iam::111122223333:role/ExampleKeyUser"; String operation = GrantOperation.GenerateDataKey; CreateGrantRequest createGrantRequest = new CreateGrantRequest() { KeyId = keyId, GranteePrincipal = granteePrincipal, Operations = new List<string>() { operation } }; CreateGrantResponse createGrantResult = kmsClient.CreateGrant(createGrantRequest);
Python

Pour obtenir des détails, consultez la méthode create_grant dans AWS SDK for Python (Boto3).

# Create a grant # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' grantee_principal = 'arn:aws:iam::111122223333:role/ExampleKeyUser' operation = ['GenerateDataKey'] response = kms_client.create_grant( KeyId=key_id, GranteePrincipal=grantee_principal, Operations=operation )
Ruby

Pour obtenir des détails, consultez la méthode d'instance create_grant dans le kit AWS SDK for Ruby.

# Create a grant # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' grantee_principal = 'arn:aws:iam::111122223333:role/ExampleKeyUser' operation = ['GenerateDataKey'] response = kmsClient.create_grant({ key_id: key_id, grantee_principal: grantee_principal, operations: operation })
PHP

Pour obtenir des détails, consultez la méthode CreateGrant dans AWS SDK for PHP.

// Create a grant // // Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $granteePrincipal = "arn:aws:iam::111122223333:role/ExampleKeyUser"; $operation = ['GenerateDataKey'] $result = $KmsClient->createGrant([ 'GranteePrincipal' => $granteePrincipal, 'KeyId' => $keyId, 'Operations' => $operation ]);
Node.js

Pour plus de détails, consultez la propriété CreateGrant dans le AWSSDK pour JavaScript Node.js.

// Create a grant // // Replace the following example key ARN with a valid key ID or key ARN const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const GranteePrincipal = 'arn:aws:iam::111122223333:role/ExampleKeyUser'; const Operations: ["GenerateDataKey"]; kmsClient.createGrant({ KeyId, GranteePrincipal, Operations }, (err, data) => { ... });
PowerShell

Pour créer un octroi, utilisez l'applet de commande New-KMSGrant.

# Create a grant # Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' $granteePrincipal = 'arn:aws:iam::111122223333:role/ExampleKeyUser' $operation = 'GenerateDataKey' $response = New-KMSGrant -GranteePrincipal $granteePrincipal -KeyId $keyId -Operation $operation

Pour utiliser les AWS KMS PowerShell applets de commande, installez le fichier AWS.tools. KeyManagementServicemodule. Pour plus d’informations, consultez le Guide de l’utilisateur AWS Tools for Windows PowerShell.

Affichage d'un octroi

Pour obtenir des informations détaillées sur les autorisations associées à une clé KMS, utilisez l'ListGrantsopération.

Note

Le champ GranteePrincipal de la réponse ListGrants contient habituellement le bénéficiaire principal. Toutefois, lorsque le principal bénéficiaire de l'attribution est un service AWS, le champ GranteePrincipal contient le mandataire de service, qui peut représenter plusieurs bénéficiaires principaux.

Dans les langues qui nécessitent un objet client, ces exemples utilisent l'objet client AWS KMS que vous avez créé dans Création d'un client.

Ces exemples utilisent le paramètre facultatif Limits, qui détermine le nombre d'octrois renvoyés par l'opération.

Java

Pour plus de détails sur l'implémentation Java, veuillez consulter la méthode listGrants dans la Référence d'API AWS SDK for Java.

// Listing grants on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; Integer limit = 10; ListGrantsRequest req = new ListGrantsRequest().withKeyId(keyId).withLimit(limit); ListGrantsResult result = kmsClient.listGrants(req);
C#

Pour obtenir des détails, consultez la méthode ListGrants dans AWS SDK for .NET.

// Listing grants on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; int limit = 10; ListGrantsRequest listGrantsRequest = new ListGrantsRequest() { KeyId = keyId, Limit = limit }; ListGrantsResponse listGrantsResponse = kmsClient.ListGrants(listGrantsRequest);
Python

Pour obtenir des détails, consultez la méthode list_grants dans AWS SDK for Python (Boto3).

# Listing grants on a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kms_client.list_grants( KeyId=key_id, Limit=10 )
Ruby

Pour obtenir des détails, consultez la méthode d'instance list_grants dans le kit AWS SDK for Ruby.

# Listing grants on a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kmsClient.list_grants({ key_id: key_id, limit: 10 })
PHP

Pour obtenir des détails, consultez la méthode ListGrants dans AWS SDK for PHP.

// Listing grants on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $limit = 10; $result = $KmsClient->listGrants([ 'KeyId' => $keyId, 'Limit' => $limit, ]);
Node.js

Pour plus de détails, consultez la propriété ListGrants dans le AWSSDK pour JavaScript Node.js.

// Listing grants on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const Limit = 10; kmsClient.listGrants({ KeyId, Limit }, (err, data) => { ... });
PowerShell

Pour afficher les détails de toutes les AWS KMS autorisations relatives à une clé KMS, utilisez l'applet de commande Get-KMS GrantList.

Pour limiter le nombre d'objets en sortie, cet exemple utilise l'applet de commande Select-Object au lieu du paramètre Limit, obsolète dans cette applet de commande. Pour obtenir de l'aide sur la pagination de sortie dans AWS Tools for PowerShell, veuillez consulter le billet de blog Output Pagination with AWS Tools for PowerShell.

# Listing grants on a KMS key # Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' $limit = 10 $response = Get-KMSGrantList -KeyId $keyId | Select-Object -First $limit

Pour utiliser les AWS KMS PowerShell applets de commande, installez le fichier AWS.tools. KeyManagementServicemodule. Pour plus d’informations, consultez le Guide de l’utilisateur AWS Tools for Windows PowerShell.

Vous devez spécifier la clé KMS dans chaque opération ListGrants. Toutefois, vous pouvez filtrer davantage la liste d'octrois en spécifiant l'ID d'octroi ou le principal bénéficiaire. Les exemples suivants obtiennent uniquement les octrois pour une clé KMS dans laquelle le rôle test-engineer est le principal bénéficiaire.

Java

Pour plus de détails sur l'implémentation Java, veuillez consulter la méthode listGrants dans la Référence d'API AWS SDK for Java.

// Listing grants on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String grantee = "arn:aws:iam::111122223333:role/test-engineer"; ListGrantsRequest req = new ListGrantsRequest().withKeyId(keyId).withGranteePrincipal(grantee); ListGrantsResult result = kmsClient.listGrants(req);
C#

Pour obtenir des détails, consultez la méthode ListGrants dans AWS SDK for .NET.

// Listing grants on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String grantee = "arn:aws:iam::111122223333:role/test-engineer"; ListGrantsRequest listGrantsRequest = new ListGrantsRequest() { KeyId = keyId, GranteePrincipal = grantee }; ListGrantsResponse listGrantsResponse = kmsClient.ListGrants(listGrantsRequest);
Python

Pour obtenir des détails, consultez la méthode list_grants dans AWS SDK for Python (Boto3).

# Listing grants on a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' grantee = 'arn:aws:iam::111122223333:role/test-engineer' response = kms_client.list_grants( KeyId=key_id, GranteePrincipal=grantee )
Ruby

Pour obtenir des détails, consultez la méthode d'instance list_grants dans le kit AWS SDK for Ruby.

# Listing grants on a KMS key # Replace the following example key ARN with a valid key ID or key ARN keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' grantee = 'arn:aws:iam::111122223333:role/test-engineer' response = kmsClient.list_grants({ key_id: keyId, grantee_principal: grantee })
PHP

Pour obtenir des détails, consultez la méthode ListGrants dans AWS SDK for PHP.

// Listing grants on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $grantee = 'arn:aws:iam::111122223333:role/test-engineer'; $result = $KmsClient->listGrants([ 'KeyId' => $keyId, 'GranteePrincipal' => $grantee, ]);
Node.js

Pour plus de détails, consultez la propriété ListGrants dans le AWSSDK pour JavaScript Node.js.

// Listing grants on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const Grantee = 'arn:aws:iam::111122223333:role/test-engineer'; kmsClient.listGrants({ KeyId, Grantee }, (err, data) => { ... });
PowerShell

Pour afficher les détails de toutes les AWS KMS autorisations relatives à une clé KMS, utilisez l'applet de commande Get-KMS GrantList.

# Listing grants on a KMS key # Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' $grantee = 'arn:aws:iam::111122223333:role/test-engineer' $response = Get-KMSGrantList -KeyId $keyId -GranteePrincipal $grantee

Pour utiliser les AWS KMS PowerShell applets de commande, installez le fichier AWS.tools. KeyManagementServicemodule. Pour plus d’informations, consultez le Guide de l’utilisateur AWS Tools for Windows PowerShell.

Abandon d'un octroi

Pour annuler une autorisation pour une clé KMS, utilisez l'RetireGrantopération. Vous devez abandonner un octroi pour nettoyer une fois que vous avez fini de l'utiliser.

Pour retirer un octroi, fournissez le jeton d'octroi ou à la fois l'ID d'octroi et l'ID de clé KMS. Pour cette opération, l'ID de clé KMS doit être l'Amazon Resource Name (ARN) de la clé KMS. Le jeton de subvention est renvoyé par l'CreateGrantopération. L'ID de subvention est renvoyé par les ListGrantsopérations CreateGrant et.

RetireGrant ne renvoie pas de réponse. Pour vérifier son efficacité, utilisez l'ListGrantsopération.

Dans les langues qui nécessitent un objet client, ces exemples utilisent l'objet client AWS KMS que vous avez créé dans Création d'un client.

Java

Pour obtenir des détails, veuillez consulter la méthode retireGrant dans la Référence d'API AWS SDK for Java.

// Retire a grant // String grantToken = Place your grant token here; RetireGrantRequest req = new RetireGrantRequest().withGrantToken(grantToken); kmsClient.retireGrant(req);
C#

Pour obtenir des détails, consultez la méthode RetireGrant dans AWS SDK for .NET.

// Retire a grant // String grantToken = "Place your grant token here"; RetireGrantRequest retireGrantRequest = new RetireGrantRequest() { GrantToken = grantToken }; kmsClient.RetireGrant(retireGrantRequest);
Python

Pour obtenir des détails, consultez la méthode retire_grant dans AWS SDK for Python (Boto3).

# Retire a grant grant_token = Place your grant token here response = kms_client.retire_grant( GrantToken=grant_token )
Ruby

Pour obtenir des détails, consultez la méthode d'instance retire_grant dans le kit AWS SDK for Ruby.

# Retire a grant grant_token = Place your grant token here response = kmsClient.retire_grant({ grant_token: grant_token })
PHP

Pour obtenir des détails, consultez la méthode RetireGrant dans AWS SDK for PHP.

// Retire a grant // $grantToken = 'Place your grant token here'; $result = $KmsClient->retireGrant([ 'GrantToken' => $grantToken, ]);
Node.js

Pour plus de détails, consultez la propriété RetireGrant dans AWSle SDK JavaScript pour Node.js.

// Retire a grant // const GrantToken = 'Place your grant token here'; kmsClient.retireGrant({ GrantToken }, (err, data) => { ... });
PowerShell

Pour retirer un octroi, utilisez l'applet de commande Disable-KMSGrant. Pour obtenir un jeton d'octroi, utilisez l'applet de commande New-KMSGrant. Le paramètre GrantToken prend une chaîne, vous n'avez donc pas besoin de convertir la sortie renvoyée par l'applet de commande Read-Host.

# Retire a grant $grantToken = Read-Host -Message Place your grant token here Disable-KMSGrant -GrantToken $grantToken

Pour utiliser les AWS KMS PowerShell applets de commande, installez le fichier AWS.tools. KeyManagementServicemodule. Pour plus d’informations, consultez le Guide de l’utilisateur AWS Tools for Windows PowerShell.

Révocation d'un octroi

Pour révoquer une autorisation accordée à une clé KMS, utilisez l'RevokeGrantopération. Vous pouvez révoquer un octroi pour refuser explicitement les opérations qui en dépendent.

Dans les langues qui nécessitent un objet client, ces exemples utilisent l'objet client AWS KMS que vous avez créé dans Création d'un client.

Java

Pour obtenir des détails, veuillez consulter la méthode revokeGrant dans la Référence d'API AWS SDK for Java.

// Revoke a grant on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; // Replace the following example grant ID with a valid one String grantId = "grant1"; RevokeGrantRequest req = new RevokeGrantRequest().withKeyId(keyId).withGrantId(grantId); kmsClient.revokeGrant(req);
C#

Pour obtenir des détails, consultez la méthode RevokeGrant dans AWS SDK for .NET.

// Revoke a grant on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; // Replace the following example grant ID with a valid one String grantId = "grant1"; RevokeGrantRequest revokeGrantRequest = new RevokeGrantRequest() { KeyId = keyId, GrantId = grantId }; kmsClient.RevokeGrant(revokeGrantRequest);

Pour utiliser les AWS KMS PowerShell applets de commande, installez le fichier AWS.tools. KeyManagementServicemodule. Pour plus d’informations, consultez le Guide de l’utilisateur AWS Tools for Windows PowerShell.

Python

Pour obtenir des détails, consultez la méthode revoke_grant dans AWS SDK for Python (Boto3).

# Revoke a grant on a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' # Replace the following example grant ID with a valid one grant_id = 'grant1' response = kms_client.revoke_grant( KeyId=key_id, GrantId=grant_id )
Ruby

Pour obtenir des détails, consultez la méthode d'instance revoke_grant dans le kit AWS SDK for Ruby.

# Revoke a grant on a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' # Replace the following example grant ID with a valid one grant_id = 'grant1' response = kmsClient.revoke_grant({ key_id: key_id, grant_id: grant_id })
PHP

Pour obtenir des détails, consultez la méthode RevokeGrant dans AWS SDK for PHP.

// Revoke a grant on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; // Replace the following example grant ID with a valid one $grantId = "grant1"; $result = $KmsClient->revokeGrant([ 'KeyId' => $keyId, 'GrantId' => $grantId, ]);
Node.js

Pour plus de détails, consultez la propriété RevokeGrant dans AWSle SDK JavaScript pour Node.js.

// Revoke a grant on a KMS key // // Replace the following example key ARN with a valid key ID or key ARN const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; // Replace the following example grant ID with a valid one const GrantId = 'grant1'; kmsClient.revokeGrant({ GrantId, KeyId }, (err, data) => { ... });
PowerShell

Pour révoquer un octroi, utilisez l'applet de commande Revoke-KMSGrant.

# Revoke a grant on a KMS key # Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' # Replace the following example grant ID with a valid one $grantId = 'grant1' Revoke-KMSGrant -KeyId $keyId -GrantId $grantId

Pour utiliser les AWS KMS PowerShell applets de commande, installez le fichier AWS.tools. KeyManagementServicemodule. Pour de plus amples informations, veuillez consulter le Guide de l'utilisateur AWS Tools for Windows PowerShell.