This topic shows how to manage access to Cloud KMS resources.
Overview
To manage access to Cloud KMS resources, such as keys and key rings, you grant Identity and Access Management (IAM) roles. You can grant or restrict the ability to perform specific cryptographic operations, such as rotating a key or encrypting data. You can grant IAM roles on:
- A key directly
- A key ring, inherited by all keys in that key ring
- A Google Cloud project, inherited by all keys in the project
- A Google Cloud folder, inherited by all keys in all projects in the folder
- A Google Cloud organization, inherited by all keys in folders in the organization
For a complete list of Cloud KMS actions and IAM roles and permissions, see Permissions and roles. For a complete list of Cloud KMS resources and how they relate to each other, see Cloud KMS resources.
Before you begin
To complete these tasks, you need permission to administer Cloud KMS
resources in the Google Cloud project. The Cloud KMS Admin role
(roles/cloudkms.admin
) includes the required permissions.
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the required API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the required API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- Create a resource, such as a key ring.
- Get the resource IDs for the resources created, such as a key ring, key, and key version.
Only IAM principals with Owner (roles/owner
) or Cloud KMS Admin
(roles/cloudkms.admin
) roles can grant or revoke access to Cloud KMS
resources.
Granting roles on a resource
The following example grants a role that provides access to a Cloud KMS key:
gcloud
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
gcloud kms keys add-iam-policy-binding key \ --keyring key-ring \ --location location \ --member principal-type:principal-email \ --role roles/role
Replace key with the name of the key. Replace key-ring with the name of the key ring where the key is located. Replace location with the Cloud KMS location for the key ring. Replace principal-type and principal-email with the type of principal and the principal's email address. Replace role with the name of the role to add.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Revoking access to a resource
To remove a principal's access to a Cloud KMS key:
gcloud
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
gcloud kms keys remove-iam-policy-binding key \ --keyring key-ring \ --location location \ --member principal-type:principal-email \ --role roles/role-name
Replace key with the name of the key. Replace key-ring with the name of the key ring where the key is located. Replace location with the Cloud KMS location for the key ring. Replace principal-type and principal-email with the type of principal and the principal's email address. Replace role-name with the name of the role to remove.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Viewing permissions on a resource
To view the IAM policy for a Cloud KMS key:
gcloud
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
gcloud kms keys get-iam-policy key \ --keyring key-ring \ --location location
Replace key with the name of the key. Replace key-ring with the name of the key ring where the key is located. Replace location with the Cloud KMS location for the key ring.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Principle of least privilege
To practice the principle of least privilege, grant the most limited set of permissions to the lowest object in the resource hierarchy.
To grant a principal permissions to encrypt (but not decrypt) data, grant the
roles/cloudkms.cryptoKeyEncrypter
role on the key.To grant a principal permissions to encrypt and decrypt data, grant the
roles/cloudkms.cryptoKeyEncrypterDecrypter
role on the key.To grant a principal permissions to verify (but not sign) data, grant the
roles/cloudkms.publicKeyViewer
role on the key.To grant a principal permissions to sign and verify data, grant the
roles/cloudkms.signerVerifier
role on the key.To grant a principal permissions to manage a key, grant the
roles/cloudkms.admin
role on the key.
This is not an exhaustive list. See Cloud KMS permissions and roles for a full list of permissions and roles.
Hierarchy and inheritance
Policy bindings can be specified on the project, key ring, key, import job, and other Cloud KMS resources.
Since keys belong to key rings, and key rings belong to projects, a principal
with a specific role or permission at a higher level in that hierarchy inherits
the same permissions on the child resources. That is, a user who has the role of
owner
on a project is also an owner
on all the key rings and keys in that
project. Similarly, if a user is granted the cloudkms.admin
role on a key
ring, they have the associated permissions on all the keys in that key ring.
The inverse is not true; that is, a user who has a permission on a key but does not have the permission on the parent key ring has no permissions on that key ring.
What's next
- Learn more about Permissions and roles in Cloud KMS.
- Create a key.
- Encrypt and decrypt data.