This page explains how to restore a previous version of an IAM allow policy after making any inadvertent changes or accidental deletion.
To restore a previous IAM policy, find the last successful
setIamPolicy prior to the change or deletion and use it to restore the policy.
Required roles
To get the permissions that you need to restore a previous allow policy, ask your administrator to grant you the following IAM roles on the project, folder, or organization:
-
Restore allow policies with
setIamPolicy:-
Logging Viewer (
roles/logging.viewer) -
Organization Administrator (
roles/resourcemanager.organizationAdmin) -
Owner (
roles/owner)
-
Logging Viewer (
-
Restore allow policies with Cloud Asset Inventory:
Cloud Asset Owner (
roles/cloudasset.owner)
For more information about granting roles, see Manage access to projects, folders, and organizations.
When restoring allow policies withsetIamPolicy, the Organization
Administrator role is necessary for organization and folder-level allow
policies, and the Owner role is necessary for project-level allow policies.
You might also be able to get these permissions with custom roles or other predefined roles.
Restore an allow policy with the last successful setIamPolicy instance
To restore an allow policy to a previous version, you can review your audit logs
for the last successful instance of setIamPolicy prior to the change or
deletion and use the information from that instance to restore the policy. This
process involves the following steps:
- Identify the last successful
setIamPolicyinstance for the allow policy and retrieve theinsertId. - Use the
insertIdfrom thesetIamPolicyrequest to export the allow policy in YAML or JSON format. - Modify the output file to match the previous working allow policy and override the current allow policy.
Identify the last successful instance of setIamPolicy and retrieve the insertId
To identify the last successful instance of setIamPolicy and retrieve the
isertId, use the Google Cloud console or the gcloud CLI to review your
audit logs.
Console
In the Google Cloud console, go to the Logs Explorer page.
In the query editor, enter one of the following queries. These queries search your audit logs for entries that have
SetIamPolicyin themethodNamefield of theprotoPayload:To get the logs of all allow policy changes made on a resource, use the following query:
logName="RESOURCE_TYPE/RESOURCE_ID/logs/cloudaudit.googleapis.com%2Factivity" protoPayload.methodName:SetIamPolicy
To get the logs of allow policy changes that involve a specific user or service account, use the following query:
logName="RESOURCE_TYPE/RESOURCE_ID/logs/cloudaudit.googleapis.com%2Factivity" protoPayload.methodName:SetIamPolicy protoPayload.serviceData.policyDelta.bindingDeltas.member:"EMAIL_ADDRESS"
Replace the following:
RESOURCE_TYPE: The resource type that you're listing audit logs for. Valid values areprojects,folders, ororganizations.RESOURCE_ID: Your Google Cloud project, folder, or organization ID. Project IDs are alphanumeric, likemy-project. Folder and organization IDs are numeric, like123456789012.EMAIL_ADDRESS: The email address of the user or service account—for example,example-service-account@example-project.iam.gserviceaccount.com.
To run the query, click Run query.
Use the Timeline selector to specify the appropriate time range for the query. Alternatively, you can add a timestamp expression directly to the query editor. For more information, see View logs by time range.
Copy the
insertIdfrom the last successful instance ofsetIamPolicyprior to the change or deletion.
gcloud
The
gcloud logging read
command reads log entries.
Before using any of the command data below, make the following replacements:
-
RESOURCE_TYPE: The resource type that you are listing audit logs for. Use the valueprojects,folders, ororganizations. -
RESOURCE_ID: Your Google Cloud project, organization, or folder ID. Project IDs are alphanumeric strings, likemy-project. Folder and organization IDs are numeric, like123456789012. -
TIME_PERIOD: The time period that you are listing audit logs for. The entries returned are not older than this value. If left unspecified, the default value is1d. For information about time formats, see gcloud topic datetimes. -
RESOURCE_TYPE_SINGULAR: The resource type that you are listing audit logs for. Use the valueproject,folder, ororganization.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud logging read \ 'logName:RESOURCE_TYPE/RESOURCE_ID/logs/cloudaudit.googleapis.com%2Factivity AND protoPayload.methodName=SetIamPolicy' \ --freshness=TIME_PERIOD \ --RESOURCE_TYPE_SINGULAR=RESOURCE_ID
Windows (PowerShell)
gcloud logging read ` 'logName:RESOURCE_TYPE/RESOURCE_ID/logs/cloudaudit.googleapis.com%2Factivity AND protoPayload.methodName=SetIamPolicy' ` --freshness=TIME_PERIOD ` --RESOURCE_TYPE_SINGULAR=RESOURCE_ID
Windows (cmd.exe)
gcloud logging read ^ 'logName:RESOURCE_TYPE/RESOURCE_ID/logs/cloudaudit.googleapis.com%2Factivity AND protoPayload.methodName=SetIamPolicy' ^ --freshness=TIME_PERIOD ^ --RESOURCE_TYPE_SINGULAR=RESOURCE_ID
Copy the insertId from the last successful instance of setIamPolicy prior
to the change or deletion.
Use the insertId to export the allow policy
Use the insertId you retrieved from the last successful instance of
setIamPolicy to run the following command in the gcloud CLI to
export the allow policy in JSON or YAML format. You can export allow policies
for organizations and projects.
Organization level
The
gcloud logging read
command reads log entries.
Before using any of the command data below, make the following replacements:
-
ORGANIZATION_ID: The organization that you want to view and export the allow policy for. -
INSERT_ID: TheinsertIdof thesetIamPolicyrequest. -
FORMAT: The format for the response. Usejsonoryaml. -
TIME_PERIOD: The time period that you're listing audit logs for. The entries returned are not older than this value. If left unspecified, the default value is1d. For information about time formats, see gcloud topic datetimes. -
OUTPUT_FILE: The file name and extension for the output. For example,previous_policy.jsonorprevious_policy.yaml.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud logging read organizations/ORGANIZATION_ID/logs/cloudaudit.googleapis.com insertId="INSERT_ID"' --organization=ORGANIZATION_ID --format="FORMAT(protoPayload.request.policy)" --freshness=TIME_PERIOD > OUTPUT_FILE
Windows (PowerShell)
gcloud logging read organizations/ORGANIZATION_ID/logs/cloudaudit.googleapis.com insertId="INSERT_ID"' --organization=ORGANIZATION_ID --format="FORMAT(protoPayload.request.policy)" --freshness=TIME_PERIOD > OUTPUT_FILE
Windows (cmd.exe)
gcloud logging read organizations/ORGANIZATION_ID/logs/cloudaudit.googleapis.com insertId="INSERT_ID"' --organization=ORGANIZATION_ID --format="FORMAT(protoPayload.request.policy)" --freshness=TIME_PERIOD > OUTPUT_FILE
Project level
The
gcloud logging read
command reads log entries.
Before using any of the command data below, make the following replacements:
-
INSERT_ID: TheinsertIdof thesetIamPolicyrequest. -
TIME_PERIOD: The time period that you're listing audit logs for. The entries returned are not older than this value. If left unspecified, the default value is1d. For information about time formats, see gcloud topic datetimes. -
FORMAT: The format for the response. Usejsonoryaml. -
OUTPUT_FILE: The name of the file where you want to save the output—for example,previous_policy.jsonorprevious_policy.yaml.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud logging read \ 'protoPayload.methodName="SetIamPolicy" AND insertId="INSERT_ID"' \ --freshness=TIME_PERIOD \ --format="FORMAT(protoPayload.request.policy)" > OUTPUT_FILE
Windows (PowerShell)
gcloud logging read ` 'protoPayload.methodName="SetIamPolicy" AND insertId="INSERT_ID"' ` --freshness=TIME_PERIOD ` --format="FORMAT(protoPayload.request.policy)" > OUTPUT_FILE
Windows (cmd.exe)
gcloud logging read ^ 'protoPayload.methodName="SetIamPolicy" AND insertId="INSERT_ID"' ^ --freshness=TIME_PERIOD ^ --format="FORMAT(protoPayload.request.policy)" > OUTPUT_FILE
Modify the output file and override current allow policy
Modify the output file in the following ways and override the current allow policy.
Programmatically or using a text editor, modify the output file by deleting the following lines:
--- protoPayload: request: policy:The resulting file starts with
auditConfigs:.Get the current allow policy.
gcloud
The
gcloud get-iam-policycommand gets a project's, folder's, or organization's allow policy.Before using any of the command data below, make the following replacements:
-
RESOURCE_TYPE: The type of the resource that you want to get the allow policy for. Valid values areprojects,resource-manager folders, ororganizations. -
RESOURCE_ID: Your Google Cloud project, folder, or organization ID. Project IDs are alphanumeric, likemy-project. Folder and organization IDs are numeric, like123456789012. -
FORMAT: The desired format for the allow policy. Usejsonoryaml. -
PATH: The path to a new output file for the allow policy.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud RESOURCE_TYPE get-iam-policy RESOURCE_ID --format=FORMAT > PATH
Windows (PowerShell)
gcloud RESOURCE_TYPE get-iam-policy RESOURCE_ID --format=FORMAT > PATH
Windows (cmd.exe)
gcloud RESOURCE_TYPE get-iam-policy RESOURCE_ID --format=FORMAT > PATH
For example, the following command gets the allow policy for the project
my-projectand saves it to your home directory in JSON format:gcloud projects get-iam-policy my-project --format json > ~/policy.json
C#
To authenticate to Resource Manager, set up Application Default Credentials. For more information, see Before you begin.
To learn how to install and use the client library for Resource Manager, see Resource Manager client libraries.
The following example shows how to get the allow policy for a project. To learn how to get the allow policy of a folder or organization, review the Resource Managerclient library documentation for your programming language.
Java
To authenticate to Resource Manager, set up Application Default Credentials. For more information, see Before you begin.
To learn how to install and use the client library for Resource Manager, see Resource Manager client libraries.
The following example shows how to get the allow policy for a project. To learn how to get the allow policy of a folder or organization, review the Resource Managerclient library documentation for your programming language.
Python
To authenticate to Resource Manager, set up Application Default Credentials. For more information, see Before you begin.
To learn how to install and use the client library for Resource Manager, see Resource Manager client libraries.
The following example shows how to get the allow policy for a project. To learn how to get the allow policy of a folder or organization, review the Resource Managerclient library documentation for your programming language.
REST
The Resource Manager API's
get-iam-policymethod gets a project's, folder's, or organization's allow policy.Before using any of the request data, make the following replacements:
API_VERSION: The API version to use. For projects and organizations, usev1. For folders, usev2.RESOURCE_TYPE: The resource type whose policy you want to manage. Use the valueprojects,folders, ororganizations.RESOURCE_ID: Your Google Cloud project, organization, or folder ID. Project IDs are alphanumeric strings, likemy-project. Folder and organization IDs are numeric, like123456789012.POLICY_VERSION: The policy version to be returned. Requests should specify the most recent policy version, which is policy version 3. See Specifying a policy version when getting a policy for details.
HTTP method and URL:
POST https://cloudresourcemanager.googleapis.com/API_VERSION/RESOURCE_TYPE/RESOURCE_ID:getIamPolicy
Request JSON body:
{ "options": { "requestedPolicyVersion": POLICY_VERSION } }To send your request, expand one of these options:
The response contains the resource's allow policy. For example:
{ "version": 1, "etag": "BwWKmjvelug=", "bindings": [ { "role": "roles/owner", "members": [ "user:my-user@example.com" ] } ] }Save the response in a file of the appropriate type (
jsonoryaml).-
Copy the
etagvalue from the current allow policy.Replace the
etagvalue in the output file with theetagvalue that you copied from the current allow policy. Theetagin the output file must match the currentetagto initiate a newsetIamPolicyrequest. Ensure that the spacing for the etag has not changed in the output file.Review the file to ensure it matches the previous working allow policy. You might consider creating a new project and applying the policy to ensure it works as intended.
Set the allow policy to override the current allow policy with the one in the output file you created.
gcloud
The
gcloud set-iam-policycommand sets the policy in the request as the new allow policy for the project, folder, or organization.Before using any of the command data below, make the following replacements:
-
RESOURCE_TYPE: The type of the resource that you want to set the allow policy for. Valid values areprojects,resource-manager folders, ororganizations. -
RESOURCE_ID: Your Google Cloud project, folder, or organization ID. Project IDs are alphanumeric, likemy-project. Folder and organization IDs are numeric, like123456789012. -
PATH: The path to a file that contains the new allow policy.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud RESOURCE_TYPE set-iam-policy RESOURCE_ID PATH
Windows (PowerShell)
gcloud RESOURCE_TYPE set-iam-policy RESOURCE_ID PATH
Windows (cmd.exe)
gcloud RESOURCE_TYPE set-iam-policy RESOURCE_ID PATH
The response contains the updated allow policy.
For example, the following command sets the allow policy stored in
policy.jsonas the allow policy for the projectmy-project:gcloud projects set-iam-policy my-project ~/policy.json
C#
Java
To authenticate to Resource Manager, set up Application Default Credentials. For more information, see Before you begin.
To learn how to install and use the client library for Resource Manager, see Resource Manager client libraries.
The following example shows how to set the allow policy for a project. To learn how to set the allow policy of a folder or organization, review the Resource Manager client library documentation for your programming language.
Python
To authenticate to Resource Manager, set up Application Default Credentials. For more information, see Before you begin.
To learn how to install and use the client library for Resource Manager, see Resource Manager client libraries.
The following example shows how to set the allow policy for a project. To learn how to set the allow policy of a folder or organization, review the Resource Manager client library documentation for your programming language.
REST
The Resource Manager API's
set-iam-policymethod sets the policy in the request as the new allow policy for the project, folder, or organization.Before using any of the request data, make the following replacements:
API_VERSION: The API version to use. For projects and organizations, usev1. For folders, usev2.RESOURCE_TYPE: The resource type whose policy you want to manage. Use the valueprojects,folders, ororganizations.RESOURCE_ID: Your Google Cloud project, organization, or folder ID. Project IDs are alphanumeric strings, likemy-project. Folder and organization IDs are numeric, like123456789012.-
POLICY: A JSON representation of the policy that you want to set. For more information about the format of a policy, see the Policy reference.
HTTP method and URL:
POST https://cloudresourcemanager.googleapis.com/API_VERSION/RESOURCE_TYPE/RESOURCE_ID:setIamPolicy
Request JSON body:
{ "policy": POLICY }To send your request, expand one of these options:
The response contains the updated allow policy.
-
Restore an allow policy with Cloud Asset Inventory
To restore an allow policy to a previous version, you can use Cloud Asset Inventory to view the change history of the resource's allow policies and use that information to restore the policy. This process involves the following steps:
- Use Cloud Asset Inventory to review the change history of allow policies and identify the allow policy you want to restore.
- Copy the allow policy and create a new file in JSON format.
- Override the current allow policy with the one in the new file.
Review allow policy change history with Cloud Asset Inventory
Console
In the Google Cloud console, go to the Asset Inventory page.
Click the IAM Policy tab.
Run the following query in the Filter field:
Resource :
RESOURCE_IDReplace
RESOURCE_IDwith your Google Cloud project, folder, or organization ID. Project IDs are alphanumeric, likemy-project. Folder and organization IDs are numeric, like123456789012.To view the change history of the resource's allow policy, click the resource's name, then select the Change History tab.
To compare any changes to the allow policy for the resource, select two different timestamped records from the Select a record to compare menu.
gcloud
The
gcloud asset get-history
command gets the updated history of allow policies on an asset that overlaps a
time window.
Before using any of the command data below, make the following replacements:
-
RESOURCE_TYPE: The resource type that you are listing audit logs for. Use the valueproject,folder, ororganization. -
RESOURCE_ID: Your Google Cloud project, organization, or folder ID. Project IDs are alphanumeric strings, likemy-project. Folder and organization IDs are numeric, like123456789012. -
ASSET_NAME: A comma-separated list of formatted resource names for the resources whose allow policy histories you want to view. For example,//cloudresourcemanager.googleapis.com/projects/my-project. These resources can be any of the resource types that accept allow policies. -
START_TIME: The beginning of the time range. The maximum time range is 7 days. The value must be the current time or a time no more than 35 days in the past. For information about time formats, see gcloud topic datetimes. -
END_TIME: Optional. The finishing point of the time range. The maximum time range is 7 days. The value must be the current time or a time no more than 35 days in the past. When not provided, the end time is assumed to be the current time. For information about time formats, see gcloud topic datetimes.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud asset get-history \ --RESOURCE_TYPE=RESOURCE_ID \ --asset-names=ASSET_NAME_1,ASSET_NAME_2,... \ --content-type=iam-policy \ --start-time=START_TIME \ --end-time=END_TIME
Windows (PowerShell)
gcloud asset get-history ` --RESOURCE_TYPE=RESOURCE_ID ` --asset-names=ASSET_NAME_1,ASSET_NAME_2,... ` --content-type=iam-policy ` --start-time=START_TIME ` --end-time=END_TIME
Windows (cmd.exe)
gcloud asset get-history ^ --RESOURCE_TYPE=RESOURCE_ID ^ --asset-names=ASSET_NAME_1,ASSET_NAME_2,... ^ --content-type=iam-policy ^ --start-time=START_TIME ^ --end-time=END_TIME
The response contains the updated history of allow policies.
Create a new allow policy file
After you've reviewed the allow policy history and identified the allow policy you want to restore, complete the following steps.
Copy the allow policy that you want to restore starting from the
"bindings"line to the"etag"line.Paste the allow policy you copied into a new file and use curly braces (
{}) to enclose the policy. For example, the allow policy might look like the following:{ "bindings": [ { "role": "roles/resourcemanager.organizationAdmin", "members": [ "user:mike@example.com", "group:admins@example.com", "domain:google.com", "serviceAccount:my-service-account@my-project.iam.gserviceaccount.com" ] }, { "role": "roles/resourcemanager.organizationViewer", "members": [ "user:eve@example.com" ], "condition": { "title": "expirable access", "description": "Does not grant access after Sep 2020", "expression": "request.time < timestamp('2020-10-01T00:00:00.000Z')", } } ], "etag": "BwWWja0YfJA=", }Save the file in JSON format. For example, you might name the new file
previous_iam_policy.json.
Set the new allow policy
Set the allow policy to override the current allow policy with the one in the output file you created.
gcloud
The
gcloud set-iam-policy
command sets the policy in the request as the new allow policy for the project, folder, or organization.
Before using any of the command data below, make the following replacements:
-
RESOURCE_TYPE: The type of the resource that you want to set the allow policy for. Valid values areprojects,resource-manager folders, ororganizations. -
RESOURCE_ID: Your Google Cloud project, folder, or organization ID. Project IDs are alphanumeric, likemy-project. Folder and organization IDs are numeric, like123456789012. -
PATH: The path to a file that contains the new allow policy.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud RESOURCE_TYPE set-iam-policy RESOURCE_ID PATH
Windows (PowerShell)
gcloud RESOURCE_TYPE set-iam-policy RESOURCE_ID PATH
Windows (cmd.exe)
gcloud RESOURCE_TYPE set-iam-policy RESOURCE_ID PATH
The response contains the updated allow policy.
For example, the following command sets the allow policy stored in policy.json
as the allow policy for the project my-project:
gcloud projects set-iam-policy my-project ~/policy.json
C#
Java
To authenticate to Resource Manager, set up Application Default Credentials. For more information, see Before you begin.
To learn how to install and use the client library for Resource Manager, see Resource Manager client libraries.
The following example shows how to set the allow policy for a project. To learn how to set the allow policy of a folder or organization, review the Resource Manager client library documentation for your programming language.
Python
To authenticate to Resource Manager, set up Application Default Credentials. For more information, see Before you begin.
To learn how to install and use the client library for Resource Manager, see Resource Manager client libraries.
The following example shows how to set the allow policy for a project. To learn how to set the allow policy of a folder or organization, review the Resource Manager client library documentation for your programming language.
REST
The Resource Manager API's
set-iam-policy
method sets the policy in the request as the new allow policy for the project, folder, or organization.
Before using any of the request data, make the following replacements:
API_VERSION: The API version to use. For projects and organizations, usev1. For folders, usev2.RESOURCE_TYPE: The resource type whose policy you want to manage. Use the valueprojects,folders, ororganizations.RESOURCE_ID: Your Google Cloud project, organization, or folder ID. Project IDs are alphanumeric strings, likemy-project. Folder and organization IDs are numeric, like123456789012.-
POLICY: A JSON representation of the policy that you want to set. For more information about the format of a policy, see the Policy reference.
HTTP method and URL:
POST https://cloudresourcemanager.googleapis.com/API_VERSION/RESOURCE_TYPE/RESOURCE_ID:setIamPolicy
Request JSON body:
{
"policy": POLICY
}
To send your request, expand one of these options:
The response contains the updated allow policy.