All guides

Google Cloud IAM and organization policies

In Google's words, IAM focuses on who and Organization Policy focuses on what. IAM authorizes who can act on a resource. An organization policy restricts how resources can be configured. Both are inherited through the resource hierarchy.

A desk with two monitors showing code

The resource hierarchy

The organization is at the top of the hierarchy. Folders are optional and come next: a folder can contain projects and other folders. Projects contain the service resources, such as virtual machines and storage buckets. Every resource except the top one has exactly one parent.

Organizations, folders and projects are the inheritance points for allow policies, deny policies and organization policies. Roles granted on a folder are inherited by all projects and folders in it. The effective policy for a resource results from the policies applied directly to it and the policies inherited from its ancestors. So grant a role no higher in the hierarchy than the work requires.

Allow policies and role types

An allow policy is a collection of role bindings. A role binding associates one or more principals with a single role and, optionally, a condition. IAM has three kinds of role:

  • Basic roles give broad access across Google Cloud resources. They are Admin (roles/admin), Writer (roles/writer) and Reader (roles/reader), plus the legacy basic roles Owner (roles/owner), Editor (roles/editor) and Viewer (roles/viewer). Google's guidance: a production environment gets basic roles only where no alternative exists.
  • Predefined roles give granular access for a specific service. roles/storage.objectViewer is one.
  • Custom roles bundle permissions that you choose. You create them at the project or organization level. They cannot be defined at the folder level.

The command that grants a role on a project has this form:

Shell

gcloud projects add-iam-policy-binding <project-id> \
  --member=<principal> --role=<role>

A principal is written with its type as a prefix, as in group:[email protected]. To read the current allow policy, run gcloud projects get-iam-policy <project-id>.

Deny policies

A deny policy prevents principals from using specific permissions, regardless of the roles they are granted. IAM always checks relevant deny policies before it checks relevant allow policies. You attach a deny policy to an organization, a folder or a project, and it is inherited by the resources below.

Each deny rule lists the denied principals and the denied permissions. Exception principals and a condition are optional. This file wraps Google's own example rule. It stops the eng group from creating or deleting service account keys and exempts the eng-prod group. Save it as policy.json:

JSON

{
  "displayName": "My deny policy.",
  "rules": [
    {
      "denyRule": {
        "deniedPrincipals": [
          "principalSet://goog/group/[email protected]"
        ],
        "exceptionPrincipals": [
          "principalSet://goog/group/[email protected]"
        ],
        "deniedPermissions": [
          "iam.googleapis.com/serviceAccountKeys.create",
          "iam.googleapis.com/serviceAccountKeys.delete"
        ]
      }
    }
  ]
}

Shell

gcloud iam policies create my-deny-policy \
  --attachment-point=cloudresourcemanager.googleapis.com/projects/my-project \
  --kind=denypolicies \
  --policy-file=policy.json

A deny policy writes permissions in the IAM v2 format, where a fully qualified domain name identifies the service.

Organization policy constraints

An organization policy configures a single constraint. You set it on an organization, a folder or a project, and descendants inherit it by default.

Constraints come in three kinds. Managed constraints are designed to replace the equivalent legacy managed constraints. Legacy managed constraints, previously known as predefined constraints, have list rules or boolean rules. Custom constraints are managed by your organization. Six constraints by name:

  • constraints/gcp.resourceLocations defines the locations where location-based resources can be created.
  • constraints/iam.allowedPolicyMemberDomains defines whose principals can be added to IAM policies.
  • constraints/iam.managed.disableServiceAccountCreation disables the creation of service accounts.
  • constraints/iam.managed.disableServiceAccountKeyCreation blocks service account key creation.
  • constraints/storage.publicAccessPrevention enforces public access prevention on Cloud Storage data.
  • constraints/compute.managed.vmExternalIpAccess denies the creation or update of VM instances with external IPv4 addresses.

A policy is a YAML file. This is Google's example for a managed constraint:

YAML

name: organizations/<organization-id>/policies/iam.managed.disableServiceAccountCreation
spec:
  rules:
  - enforce: true

Shell

gcloud org-policies set-policy policy.yaml

gcloud org-policies describe iam.managed.disableServiceAccountCreation \
  --effective --organization=<organization-id>

describe --effective returns the policy as it is evaluated at that point in the hierarchy, with inheritance included. Full enforcement of a change can take up to 15 minutes. A new policy does not stop behavior that a service is already in: Google states that you address such a violation manually.

To test a policy, define the rules under dryRunSpec in place of spec. Violations are then audit logged, but the violating actions are not denied. Dry-run mode is available for custom constraints, managed constraints and four legacy ones: Restrict service usage, Restrict endpoint usage, Restrict TLS versions and Restrict TLS cipher suites.

A rollout order

  1. Group projects in folders and grant roles on the folder. All projects in it inherit them.
  2. Replace basic roles with the most limited predefined or custom roles that meet your needs.
  3. Add a deny policy where a permission must stay unused regardless of granted roles, and list exception principals for the group that still needs it.
  4. Set organization policies in dry-run mode where the constraint supports it, and read the audit logs before you enforce them.
  5. After each change, check one project with gcloud org-policies describe and --effective.

Managing organization policies requires the Organization policy administrator role (roles/orgpolicy.policyAdmin) on the organization. Grant that role to few people.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides