Advice on specifying more granular permissions with Google Cloud IAM
Tags: googlecloud, iam
Published: 2020-05-12 23:01:05

If you've worked through any number of tutorial regarding more complex examples of multiple Google Cloud products connecting together, where you define a service account to take actions on services, you may have seen this: 

>  _If you are developing a production app, specify more granular permissions than "Project > Owner". For more information, see [granting roles to service accounts](https://cloud.google.com/iam/docs/granting-roles-to-service-accounts)._

That page details _how_ to grant roles to service accounts, sure. But how do you determine _what_ roles to grant? 

_This post deals specifically with [Google Cloud IAM](https://cloud.google.com/iam/docs). [Cloud Storage](https://cloud.google.com/storage/docs/access-control/lists) and [Kubernetes](https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control) have their own access control mechanisms and are outside of the scope of this post._


## Over-granted permissions

There are tools integrated into Google Cloud to help you with this. Those little lightbulb icons in [IAM & Admin > IAM](https://console.cloud.google.com/iam-admin/iam)? They are [recommendations](https://cloud.google.com/iam/docs/recommender-overview) for changes that you should make. These recommendations are over a period of time, with some machine learning used as well, so for some of your older projects, you may already have recommendations waiting for you. 


![Overgranted permissions icon](https://dev-to-uploads.s3.amazonaws.com/i/gqiox87t9ybj59m3ttsb.png)
_An example of over-granted permissions with recommendations from [https://console.cloud.google.com/iam-admin/iam](https://console.cloud.google.com/iam-admin/iam)_

If your project has recommendations that you can enact today, go do that! Just remember to [read and understand what those recommendations are](https://cloud.google.com/iam/docs/recommender-managing#review-apply), and don't be afraid to revert if something breaks.

If you're in a new project, though, there won't be any recommendations. Read on to understand what you can do today to help reduce your permissions footprint. 


## Understanding Roles and Permissions

Google Cloud defines a number of Roles as groups of permissions. The entire list of pre-defined roles is available in [IAM & Admin > Roles](https://console.cloud.google.com/iam-admin/roles). You can create your own roles, but for now we'll stick to the predefined ones. 

Roles have a title and an ID, and can be referred to by either, depending on the context. They are sometimes referred to with their grouping. For example, "Project > Owner" from earlier. The ID form is often the one used in `gcloud` calls, and the Name can be used in the search dialogs. For the purposes of this post, I'll use the form "Title (ID)". For example, "Owner (roles/owner)". 



![Select role dialog](https://dev-to-uploads.s3.amazonaws.com/i/gxhann58lcgi1nrops6d.png)
_A "Select a role" dialog_

Speaking of Owner, just what permissions does Owner grant? Checking the [role details for roles/owner](https://console.cloud.google.com/iam-admin/roles/details/roles%3Cowner), there are 2751 assigned permissions at time of writing. This number will change as Google Cloud changes, but that's still a very long list of permissions, and probably not what permissions you want your random service account to have. 

Each permission can be thought of as an 'action', normally named in a triple of values  "`service.resource.verb`", and that permission is probably required to perform that action. Roles can group permissions from different products for more logical collections. 

For example, the "[Cloud Functions Viewer (roles/cloudfunctions.viewer)](https://console.cloud.google.com/iam-admin/roles/details/roles%3Ccloudfunctions.viewer)" role grants a number of permissions to `cloudfunctions` -- like `cloudfunctions.functions.get` and `cloudfunctions.functions.list` -- but also permissions across `resourcemanager` and `serviceusage`. If you intend to view a Cloud Function, these are the permissions you'll need.

The [Understanding Roles](https://cloud.google.com/iam/docs/understanding-roles) doc describes some of the base concepts (including how Owner existed before Cloud IAM!), but also lists **all** the predefined rolls. Skip down to [Product-specific Cloud IAM documentation](https://cloud.google.com/iam/docs/understanding-roles#product_specific_documentation) and see if the product you're most interested in has more detailed information. 


## Which role is right for your use case?

When working out which roles to assign, using the Roles list and choosing something that "sounds" right as a first step is not a bad starting point. For instance, that Cloud Functions Viewer role sounds like it would be the right role if you wanted only view access on a Function.

Some of these roles are automatically associated with the dynamically created service accounts when you enable certain products. Removing any of the roles these service accounts use is a _really bad idea_, as functionality will probably break throughout your project. 

Instead, if you need to make changes to the role, create a new service account using the default service account's base roles as a starting point, then make changes. 


## What if you need a role that doesn't exist? 

That's when custom roles come into play. You can define your own role that has custom permissions, thus ensuring you can enforce the [Principle of least privilege](https://en.wikipedia.org/wiki/Principle_of_least_privilege). There are some limitations that the [Understanding Custom IAM Roles](https://cloud.google.com/iam/docs/understanding-custom-roles) docs goes into more detail for, and some [general recommendations](https://cloud.google.com/iam/docs/using-iam-securely).  


## Debugging permissions from first principles

Whether you create a new service account with predefined or custom roles, which permissions you need is the question.

No two projects are going to be the same, so the answer to the question "what is the minimum set of permissions I need for my project" is going to be different for every project. But there are a number of things you can do to work out what you need for yourself. 

_Important note:_ you should probably do your debugging outside of your production project. Just saying ✨


### Start from nothing

First, create a new service account. Then use that service account for your action. If you've set it up right, **it should fail** because the service account has zero permissions. If you're still able to perform your action, confirm you are actually using the new service account identity. 


### Work through the errors

Logging and error outputs will be your friend here. Turn on DEBUG for your code, or verbose output where you can, and look for instances of those permissions triples if you get explicit "Permission Denied" errors. Calling `gcloud` commands with `--verbosity=debug` is extremely helpful here. As are checking not only your local error logs, but also checking [Cloud Logging](https://console.cloud.google.com/logs/viewer) for output against all your services.  


### Hitting the edge cases

Making sure your site loads is one thing, but performing actions that exercise all the complex use cases is another. If possible, get someone that's not you to try out the new deployment and see if they get any issues trying to use some of the more complex actions. A fresh set of eyes is extremely helpful in this sort of complex tasking. 

Better yet, if your use case is an application that can be set up in a new project, get _them_ to set up a new project using your instructions for minimal permissions. Defaults in your setup will become abundantly visible. 


### Eventual consistency

One of the biggest gotchas in all of this is that even when you make your changes, they may not be immediately available. It can take some time for IAM changes to propagate, so if you think a change didn't have any effect, wait a minute and try again. 


### Conditional access

If even permanent access is too much for your situation, you can define [conditional access](https://cloud.google.com/iam/docs/managing-conditional-policies), such as [temporary access](https://cloud.google.com/iam/docs/configuring-temporary-access) (access for a limited time), or [resource-based access](https://cloud.google.com/iam/docs/configuring-resource-based-access) (access to a limited set of resources). Resource-based access limitations are only available for a subset of Google Cloud products, but consider if these sort of limitations will help with your setup.   


## This isn't easy

The amount of work this process requires makes it clear why "just use roles/owner" is a timesaver in the short term, but a solid and situationally appropriate permission overview can do wonders for the security of your deployment, and your understanding of the ecosystem in which your deployment lives. Try some of these tricks on your next project, and check if there are any recommendations for your existing projects ✨

In the [next post](https://dev.to/googlecloud/practical-advice-on-specifying-more-granular-permissions-with-google-cloud-iam-436l), we'll work through an example of how to apply some of these concepts in an example deployment. 

*Updated May 18 2020 with a link to part two*