|
| 1 | +# Google Cloud Organization Policy v2 Terraform Module |
| 2 | + |
| 3 | +This Terraform module makes it easier to manage [organization policies](https://cloud.google.com/resource-manager/docs/organization-policy/overview) for your Google Cloud environment, particularly when you want to have exclusion rules. This module will allow you to set a top-level org policy and then disable it on individual projects or folders easily. This module allows **_conditional policy enforcements based on the latest API version_** of the organization policies API. |
| 4 | + |
| 5 | +Organization Policies are of two types `boolean` and `list`. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Usage |
| 10 | +Example usage is included in the [examples](./examples/org_policy_v2) folder, but simple usage is as follows: |
| 11 | + |
| 12 | +```hcl |
| 13 | +module "gcp_org_policy_v2" { |
| 14 | + source = "terraform-google-modules/org-policy/google//modules/org_policy_v2" |
| 15 | + version = "~> 5.2.0" |
| 16 | +
|
| 17 | + policy_root = "organization" # either of organization, folder or project |
| 18 | + policy_root_id = "123456789" # either of org id, folder id or project id |
| 19 | + constraint = "constraint name" # constraint identifier without constriants/ prefix. Example "compute.requireOsLogin" |
| 20 | + policy_type = "boolean" # either of list or boolean |
| 21 | + exclude_folders = [] |
| 22 | + exclude_projects = [] |
| 23 | +
|
| 24 | + rules = [ |
| 25 | + # Rule 1 |
| 26 | + { |
| 27 | + enforcement = true |
| 28 | + allow = [] |
| 29 | + deny = [] |
| 30 | + conditions = [] |
| 31 | + }, |
| 32 | + # Rule 2 |
| 33 | + { |
| 34 | + enforcement = true |
| 35 | + allow = [] |
| 36 | + deny = [] |
| 37 | + conditions = [{ |
| 38 | + description = "description of the condition" |
| 39 | + expression = "resource.matchTagId('tagKeys/123456789', 'tagValues/123456789') && resource.matchTag('123456789/1234', 'abcd')" |
| 40 | + location = "sample-location.log" |
| 41 | + title = "Title of the condition" |
| 42 | + }] |
| 43 | + }, |
| 44 | + ] |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### Variables |
| 49 | +To control module's behavior, change variables' values regarding the following: |
| 50 | + |
| 51 | +- `constraint`: set this variable with the [constraint value](https://cloud.google.com/resource-manager/docs/organization-policy/org-policy-constraints#available_constraints) in the form `{constraint identifier}`. For example, `serviceuser.services` |
| 52 | +- `policy_type`: Specify either `boolean` for boolean policies or `list` for list policies. |
| 53 | +- `policy_root`: set one of the following values to determine where the policy is applied. Values should be either one of the below. |
| 54 | + - organization |
| 55 | + - project |
| 56 | + - folder |
| 57 | +- `policy_root_id`: set one of the following values to determine where the policy is applied. Based on `policy_root`, either one of the below IDs should be provided. |
| 58 | + - organization_id |
| 59 | + - project_id |
| 60 | + - folder_id |
| 61 | +- `exclude_folders`: a list of folder IDs to be excluded from this policy. These folders must be lower in the hierarchy than the policy root. |
| 62 | +- `exclude_projects`: a list of project IDs to be excluded from this policy. They must be lower in the hierarchy than the policy root. |
| 63 | +- `rules`: Specify policy rules and conditions. Rules contain the following parameters: |
| 64 | + - `enforcement`: if `true` or `null`then policy will `deny_all`; if `false` then policy will `allow_all`. Applies for `boolean` based policies. |
| 65 | + - `allow`: list of values to include in the policy with ALLOW behavior. Set `enforce` to `null` to use it. |
| 66 | + - `deny`: list of values to include in the policy with DENY behavior. Set `enforce` to `null` to use it. |
| 67 | + - `conditions`: [Organization tags](https://cloud.google.com/resource-manager/docs/organization-policy/tags-organization-policy) provides a way to conditionally allow or deny policies based on whether a resource has a specific tag. You can use tags and conditional enforcement of organization policies to provide centralized control of the resources in your hierarchy. Each condition has the following properties: |
| 68 | + - `description`: Description of the condition |
| 69 | + - `expression`: Common Expression Language, or CEL, is the expression language used to specify conditional expressions. A conditional expression consists of one or more statements that are joined using logical operators (&&, ||, or !). For more information, see the [CEL spec](https://github.com/google/cel-spec) and its [language definition](https://github.com/google/cel-spec/blob/master/doc/langdef.md). |
| 70 | + - `location`: Log location |
| 71 | + - `title`: Title of the condition |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +### IMPORTANT |
| 76 | +- Boolean policies (with `type: "boolean"`) can set the following variables: |
| 77 | + - `enforcement`: if `true` or `null` then the policy is enforced at the root; if `false` then policy is not enforced at the root. |
| 78 | + - Following requirements apply, refer [Terraform Documentation](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/org_policy_policy#rules): |
| 79 | + - There must be one and only one Policy Rule where condition is unset. |
| 80 | + - Boolean Policy Rules with conditions must set enforced to the opposite of the PolicyRule without a condition. |
| 81 | + - During policy evaluation, Policy Rules with conditions that are true for a target resource take precedence. |
| 82 | + |
| 83 | +- List policies (with `type: "list"`) can set `allow` and `deny` with a list of resources to allow or deny. For `enforcement` you can set it as follows: |
| 84 | + - set `enforcement` = false for `allow all` |
| 85 | + - set `enforcement` = true for `deny all` |
| 86 | +- Each policy can have [maximum of 10 rules](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/org_policy_policy#rules) |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
| 91 | +## Inputs |
| 92 | + |
| 93 | +| Name | Description | Type | Default | Required | |
| 94 | +|------|-------------|------|---------|:--------:| |
| 95 | +| constraint | The constraint to be applied | `string` | n/a | yes | |
| 96 | +| exclude\_folders | Set of folders to exclude from the policy | `set(string)` | `[]` | no | |
| 97 | +| exclude\_projects | Set of projects to exclude from the policy | `set(string)` | `[]` | no | |
| 98 | +| policy\_root | Resource hierarchy node to apply the policy to: can be one of `organization`, `folder`, or `project`. | `string` | `"organization"` | no | |
| 99 | +| policy\_root\_id | The policy root id, either of organization\_id, folder\_id or project\_id | `string` | `null` | no | |
| 100 | +| policy\_type | The constraint type to work with (either 'boolean' or 'list') | `string` | `"list"` | no | |
| 101 | +| rules | List of rules per policy. Upto 10. | `list(any)` | n/a | yes | |
| 102 | + |
| 103 | +## Outputs |
| 104 | + |
| 105 | +| Name | Description | |
| 106 | +|------|-------------| |
| 107 | +| constraint | Policy Constraint Identifier without constraints/ prefix | |
| 108 | +| policy\_root | Policy Root in the hierarchy for the given policy | |
| 109 | +| policy\_root\_id | Project Root ID at which the policy is applied | |
| 110 | + |
| 111 | +<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Compatibility |
| 116 | +This module is meant for use with Terraform 0.13+ and tested using Terraform 1.0+. If you find incompatibilities using Terraform >=0.13, please open an issue. |
| 117 | + If you haven't |
| 118 | +[upgraded](https://www.terraform.io/upgrade-guides/0-13.html) and need a Terraform |
| 119 | +0.12.x-compatible version of this module, the last released version |
| 120 | +intended for Terraform 0.12.x is [v4.0.0](https://registry.terraform.io/modules/terraform-google-modules/-org-policy/google/v4.0.0). |
| 121 | + |
| 122 | +## Requirements |
| 123 | +### Terraform plugins |
| 124 | +- [Terraform](https://www.terraform.io/downloads.html) >= 0.13.0 |
| 125 | +- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) >= v2.5.0 |
| 126 | + |
| 127 | +### Permissions |
| 128 | +In order to execute this module, the Service Account you run as must have the **Organization Policy Administrator** (`roles/orgpolicy.PolicyAdmin`) role. |
| 129 | + |
| 130 | +## Install |
| 131 | +### Terraform |
| 132 | +Be sure you have the correct Terraform version (0.12.x), you can choose the binary here: |
| 133 | +- https://releases.hashicorp.com/terraform/ |
| 134 | + |
| 135 | +### Terraform plugins |
| 136 | + |
| 137 | +- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) >= v2.5.0 |
| 138 | + |
0 commit comments