Most Google Cloud resources expose the testIamPermissions()
method, which
allows you to programmatically check whether the currently authenticated caller
has been granted one or more specific IAM permissions on the
resource. The testIamPermissions()
method takes a resource identifier and a
set of permissions as input parameters, and returns the set of permissions that
the caller is allowed.
You can use the testIamPermissions()
method to determine whether a user should
have access to an administrative tool in a web application. For example, you can
use this method to decide, based on the user's permissions, whether to display
detailed information about a Google Cloud resource.
For example, to determine if the currently authenticated user has the permission
to delete a project, call the projects.testIamPermissions()
method by providing the project ID (such as foo-project
) and the resourcemanager.projects.delete
permission as input parameters. If the caller
has been granted the resourcemanager.projects.delete
permission, it will be
listed in the response body. If the caller does not have this permission,
the response body will list no permissions.
The testIamPermissions()
method is intended for third-party graphical
user interfaces (GUIs) that need to display Google Cloud resources based
on what the authenticated user has permissions to see. For example, the
Google Cloud console internally uses the testIamPermissions()
method to
determine what resources and functionality are visible to you after
authenticating. Different users are typically granted different permissions,
and the Google Cloud console hides or exposes items accordingly.
Before you begin
-
Enable the Resource Manager API.
-
Set up authentication.
Select the tab for how you plan to use the samples on this page:
C#
To use the .NET samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity .
For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.
C++
To use the C++ samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity .
For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.
Java
To use the Java samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity .
For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.
Python
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity .
For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
Required roles
No IAM role is required to test permissions.
How to test permissions
This example shows how to test the resourcemanager.projects.get
and resourcemanager.projects.delete
permissions for a Google Cloud project
. To
test permissions for other Google Cloud resources, use the testIamPermissions()
method exposed by each resource. For example, you can
test the IAM permissions for a Cloud Storage bucket
.
What's next
Learn how to grant, change, and revoke access to principals .