loader image
Monitor and delete permissions on Google Drive in an easy way in 10 minutes

How to Automate: Monitor and delete permissions on Google Drive in an easy way in 10 minutes

Hugo David

Today in How to Automate, we will learn how a Google admin can fully automate one critical part of their daily job: monitoring the accesses to the organization’s resources to delete permissions on Google Drive that were not authorized.

Accessing, viewing, and editing resources from anywhere is incredibly helpful for organizations, that is for sure. Employees can work from anywhere, documents are easily sharable within the organization, and everyone can work together. Besides drastically facilitating work and collaboration, cloud-based collaborative workspaces such as Google Workspace or Microsoft 365 also enable direct external access to the organization’s sometimes sensitive resources. An employee shall have access to a document, and they could share it with someone outside the organization.

Of course, it is most of the time very welcomed for work. Sharing a business case, templates, forms, or other resources is way easier than downloading the document and sharing it via email. However, it also means that some internal documents could go public.

An organization could withdraw once and for all the ability to share externally, but does it solve the issue? The employee could always download the document and share it. You would only force the employee to follow a less traceable path. Also, you would impact the balance between security and productivity, which is a balance that every security or IT folk receives admonition about!

So, for lack of a better answer, admins have to monitor Drive’s logs to try and find ones that could relate to unauthorized external shares. Sometimes they even create custom rules to be alerted when a share could indicate a data loss. As the organization grows, this task devours analysts’ time with many logs to analyze, which is growing exponentially.

What’s more, is the remediation phase. On Google Drive, it is tricky as even Admins cannot access the entirety of files stored on Drive. To ensure a proper remediation capability, admins would have to be owners of every Drive document, notify owners of the external share and prey for them to answer quickly, or find a way to impersonate the owner. A mess that is dramatically impeding your ability to remediate ongoing incidents.

This is where automation steps in. Through Mindflow, we will create a flow that automatically alerts the IT or Security team about an external share with first-hand details. We will also create an automated remediation capability allowing the incident assignee to delete the permission in one click.

This workflow will orchestrate five services: Google Admin ReportsGoogle IAM Service Accounts CredentialsGoogle OAuthGoogle Drive, and Slack.

Automate monitoring and delete permissions on Google Drive – First steps

Before starting to build, let’s make sure that the following API  and credentials are properly set:

You will have to enable the Audit and reports API on Google and then add the following scope to your domain-wide delegation client: https://www.googleapis.com/auth/admin.reports.audit.readonly.

Register the Google credentials in the Vault.

First, we must set up an initial Flow to watch the resources we want. To do so, create a Flow and name it “Schedule: Start watching Google Drive activities by filters”. Choose your emoji and open the Flow.

Start by setting up the Scheduler. Click the calendar icon on the top left hand of the canvas, next to the Flow’s title. Select At regular intervals, then Daily, and then any time frame you feel like, BUT bear in mind that it has to be in relation to the expiration time we will set in the API call because the Watch action must be limited in time up to 6 hours.

In this example, we are setting this Flow to be triggered every 6 hours at 5, 11, 17, and 23 and 59 minutes am and pm. Click Save. Your Scheduler is ready!

Monitor and delete permissions on Google Drive - 2

Now create a new step by looking for the operation reports.activities.watch, or Watchs activities in Google Admin Reports SDK API. Configure it as such:

  1. User key *all
  2. Application name *drive
  3. Address: In the second part of this tutorial, you will fill in the Address field with the webhook URL from the Flow you will be creating.
  4. Typeweb_hook
  5. Expiration: Click fx, click Add a function and look for Get Time. Set the Granularity * to Milliseconds (ms). Then apply a new one by looking for Sum. Next to “previous value”, add a space and “21600000”. Set Delimiter * to “Space”. This will compute a 6 hours expiration time considering the current time of execution.
Monitor and delete permissions on Google Drive - 4
  1. Id: Give a name to the channel the execution will create, such as drive_change_user_access
  2. Event name: We are going to look the change_user_access events.
Monitor and delete permissions on Google Drive - 3

Make sure you select the credentials in the SETTINGS panel.

Et voilà, you have your first Flow ready to go!

Monitor and delete permissions on Google Drive - 1

Automate monitoring and delete permissions on Google Drive – Creating the Flow

This section is divided into two subsections, each representing a milestone. The first is about incident detection and notification to the targeted team. The second describes how you can set up the automated remediation process.

Before starting the dirty work, make sure the following credentials and scopes are properly set and have a look at the final Flow

Monitor and delete permissions on Google Drive - 5

Create a JWT credential in the Vault with the scope https://www.googleapis.com/auth/iam and configure the credentials, as shown in the picture below. Ensure you create this credential for an HTTP Request, not a Google service.

Monitor and delete permissions on Google Drive - credentials

Make sure your bot has the scope channel:write.

Monitor Google Drive sharings and notify upon incident.

Go back to the Homepage and create your second Flow. Name it “Webhook: File shared to someone outside the organization in GDrive Alert”, for instance. Choose your emoji! Then, click the gear icon and copy the second webhook URL from the bottom. Go back to the first Flow and paste it into the Address field.

We will first triage incoming events because the change_user_access eventName also contains permissions such as those generated at the creation of the file. We will triage the exact name of the event in the Google webhook payload. Hop on Drive to create permission on an existing file to generate a log that we will use to configure the Flow.

To do so, create a condition. Name it Action?. Create a first branch, named new permission, leading to another Condition named External Share?. In your Compare input, type “/” to open the Data Picker tool. Select APIs-Google in TRIGGERS and the last execution. Follow the path value/events/0 and Pick name.

Monitor and delete permissions on Google Drive - 6

Select the operator “Is equal to” and type “edit” below. Then, create a second branch named else leading to Success. On the condition’s branches panel, click the dots on the left of the branch Success to mark it as “Else”. This condition will filter out events such as create that we are not interested in for this Flow.

In External share? Create a first branch named Yes leading to a Transform data named Extracting docTitle. Open the Data Picker tool in the compare panel and navigate to value/events/2/parameters/3. Under the couple name/target_user, pick value. No worries, the value we are interested in will always be on this path.

Monitor and delete permissions on Google Drive - 7

Select the operator “Does not contain” and, below, type your organization’s domain. Wait, that’s not all. Click “Add a comparison”. Select the operator “And”. In the first field, open the Data Picker and navigate to value/events/2/parameters/5/multiValue/0. This is the type of permission given to the destinatory of the new permission. In this example, we are triaging to keep only external shares with edit rights. So, as the operator, select “Is equal to” and then type can_edit.

Create a second branch leading to the already existing step Success. Mark it and name it as else. Triaging is done!

Now, what we want to do is to automate the extraction of the document’s title. To do so, open Extracting docTitle setting panel and click “Add an item”. Name this item docTitle. Open the Data Picker to Pick the field parameters in value/events/0. Then, click fx and apply a JSON path function. In Query *, type $.[?(@.name=='doc_title')].value and turn off “Prevent eval *”. This will inspect the JSON to extract the value of the object doc_title. Apply a new function that is Find/Replace and, in Find * type ” and leave Replace * empty.

Monitor and delete permissions on Google Drive - 10

Following Extracting docTitle, create a Slack step by fetching the operation Sends a message to a channel or chat_postMessage. Copy and Paste the desired Channel ID in Channel *. Then, click the processor icon to display all the available fields. Look for “Blocks”. Turn the field into JSON and paste the following:

[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "ALERT - EXTERNAL SHARE"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "Actor:\n[Type "/" and pick the actor email address in the Google payload]"
},
{
"type": "mrkdwn",
"text": "Title of the document:\n[Invoke docTitle]"
},
{
"type": "mrkdwn",
"text": "Owner of the document:\n[Pick value in value/events/0/parameters/3]"
},
{
"type": "mrkdwn",
"text": "Document shared to:\n[Pick value in value/events/2/parameters/3]"
},
{
"type": "mrkdwn",
"text": "Role attributed:\n[Pick 0 in value/events/2/parameters/5/multiValue]"
}
]
},
{
"type": "actions",
"block_id": "[Pick Resume execution ID in FLOW]",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": true,
"text": "Delete permission"
},
"style": "primary",
"value": "click_me_123"
},
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": true,
"text": "Close"
},
"style": "danger",
"value": "click_me_123"
}
]
}
]

This will create an asynchronous step where the assigned user must either Close the ticket or Start the remediation process. We will ingest the user’s input when they click either button and create a condition leading to two follow-ups.

To do so, create a condition named Decision?. Create two branches, one to a chat_postMessage step named Close. One to a TD named Prepare impersonation, named Start remediation. Hop on the first branch to configure it. Open the Data Picker to inspect the Slack resume execution. Inspect the payload to find payload/actions/0 and pick style. As the operator, select “Is not equal to” and then type danger.

Repeat the same process for the second branch, but select “Is equal to” this time.

Congrats on the first milestone achieved! Let’s hop on the most interesting part of the Flow: the remediation process.

Automatically delete permissions on Google Drive when the incident is confirmed.

In this TD, we will prepare the impersonation process on Google Drive. Why? There is one issue with Drive’s API. Although it’s noticeable and appreciated that Drive doesn’t allow “Super Admins” that can see every file stored in the company’s Drive, even personal ones, it leaves a loophole for admins regarding monitoring. Through the API, you will make requests identified as a user, you, to be more specific. This means you won’t be able to act on files you don’t have access to. But Google also provides a workaround. We will use the IAM Service account credentials and OAuth APIs.

Of course, pay a good deal of carefulness since this workaround must be closely monitored and delimited to specific purposes. Here, we only want to create an impersonation to delete an unauthorized permission.

Let’s go back to our TD. Inside, create 5 items that you will name: iatsubexpimpersonated_claim_set, and escape. Configure them as such:

Monitor and delete permissions on Google Drive - 14
  • iat (issued at): create a Get time function to generate the current Unix time in seconds.
  • sub (the impersonated user): Pick the email address of the actor in the Google webhook payload.
  • exp: Again, create a Get time function in seconds and apply a Sum function where you will add 3600 seconds to the previous value, which is the current Unix time. This will set the lifetime duration of the JSON Web Token that we will generate later.
  • impersonated_claim_set: paste and complete the following JSON:
  • { "iss": "[YOUR GOOGLE SERVICE ACCOUNT ADDRESS]", "scope": "https://www.googleapis.com/auth/drive", "aud": "https://oauth2.googleapis.com/token", "iat": [call iat], "exp": [call exp], "sub": "[call sub]" }
  • escape: call impersonated_claim_set and apply an Escape string function configured as such: Escape level * Special chars, Escape quote * Double, JSON Compatible enabled.
Monitor and delete permissions on Google Drive - 16

You are ready to proceed forward! From there, we will have to create HTTP Requests by hand. I know, I know you are worried! But don’t worry, it is easy!

Create the first request. Set Method * to POST. In URL * paste https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/[YOUR SERVICE ACCOUNT ADDRESS]:signJwt. Open the Body section and mark it as JSON. Inside write

{ 
   "payload": [Invoke Escape]
}
Monitor and delete permissions on Google Drive - 17

In the SETTINGS panel, make sure you have selected your JWT credential. Run it once to generate a JWT. Once done, create another HTTP Request. Select POST and paste https://oauth2.googleapis.com/token in URL *. In Body as URL-encoded, create grant_type and paste urn:ietf:params:oauth:grant-type:jwt-bearer. Below, create assertion and Pick signedJwt from the first HTTP Request logs. Run it once to generate logs. At this point, the impersonation process is done. We will now configure steps to find and delete the unauthorized permission.

Monitor and delete permissions on Google Drive - 18

To do so, create another HTTP Request. Select GET for Method *, and paste https://www.googleapis.com/drive/v2/files/[Pick the file id in the webhook at value/events/0/parameters/4 ]/permissions. Open Headers and create Authorization. Write “Bearer” and Pick access_token from the second HTTP Request. This will retrieve the files’ permissions and the last and litigious permission attributed.

Monitor and delete permissions on Google Drive - 19

On to the final HTTP Request! Set Method * to DELETE. Paste https://www.googleapis.com/drive/v2/files/[file id from the webhook]/permissions/[Pick id from the last permission shown, that will be the object 0 in items]. Then, again, create a Authorization Header with the Bearer token.

Monitor and delete permissions on Google Drive - 20

      The remediation process is done!

      All that is left is to create Slack steps to notify that the remediation has been successfully performed. Create a chat_postMessage and configure it as such: Channel *: pick the Channel from the first Slack step. Display the additional fields and find Thread ts. Find and pick ts in the first Slack step logs. This will send the message as a reply to avoid flooding the channel. Finally, as text, you can type “Permission has been deleted.” or anything else you want.

      Of course, don’t forget the other branch, Close, that is much shorter! Create the same Slack step but, as text, write something like “Ticket closed. Reason: not an incident.” for instance.

      Et voilà! Congrats, you have automated the task to monitor and delete permissions on Google Drive!

          The remediation process is done!

          All that is left is to create Slack steps to notify that the remediation has been successfully performed. Create a chat_postMessage and configure it as such: Channel *: pick the Channel from the first Slack step. Display the additional fields and find Thread ts. Find and pick ts in the first Slack step logs. This will send the message as a reply to avoid flooding the channel. Finally, as text, you can type “Permission has been deleted.” or anything else you want.

          Of course, don’t forget the other branch, Close, that is much shorter! Create the same Slack step but, as text, write something like “Ticket closed. Reason: not an incident.” for instance.

          Et voilà! Congrats, you have automated the task to monitor and delete permissions on Google Drive!