From be8c61ac2c126c3b07c190f1439b90c015f2c1e1 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sat, 13 Feb 2021 06:55:22 +0100 Subject: [PATCH] Cloud Identity Group module (#182) * New Cloud Identity module * Add tests to cloud-identity-group module * Fix boilerplate * Fix outputs * Remove support for creating managers and admins * Update cloud-identity-group README * Small fix to cloud-identity-group README --- modules/cloud-identity-group/README.md | 52 +++++++++++++++++ modules/cloud-identity-group/main.tf | 52 +++++++++++++++++ modules/cloud-identity-group/outputs.tf | 28 +++++++++ modules/cloud-identity-group/variables.tf | 58 +++++++++++++++++++ modules/cloud-identity-group/versions.tf | 22 +++++++ .../modules/cloud_identity_group/__init__.py | 13 +++++ .../cloud_identity_group/fixture/main.tf | 24 ++++++++ .../cloud_identity_group/fixture/variables.tf | 40 +++++++++++++ .../modules/cloud_identity_group/test_plan.py | 49 ++++++++++++++++ 9 files changed, 338 insertions(+) create mode 100644 modules/cloud-identity-group/README.md create mode 100644 modules/cloud-identity-group/main.tf create mode 100644 modules/cloud-identity-group/outputs.tf create mode 100644 modules/cloud-identity-group/variables.tf create mode 100644 modules/cloud-identity-group/versions.tf create mode 100644 tests/modules/cloud_identity_group/__init__.py create mode 100644 tests/modules/cloud_identity_group/fixture/main.tf create mode 100644 tests/modules/cloud_identity_group/fixture/variables.tf create mode 100644 tests/modules/cloud_identity_group/test_plan.py diff --git a/modules/cloud-identity-group/README.md b/modules/cloud-identity-group/README.md new file mode 100644 index 00000000..25f5e808 --- /dev/null +++ b/modules/cloud-identity-group/README.md @@ -0,0 +1,52 @@ +# Cloud Identity Group Module + +This module allows creating a Cloud Identity group and assigning members. + +## Usage +To use this module you must either run terraform as a user that has the Groups Admin role in Cloud Identity or [enable domain-wide delegation](https://developers.google.com/admin-sdk/directory/v1/guides/delegation) to the service account used by terraform. If you use a service account, you must also grant that service account the Groups Admin role in Cloud Identity. + +Please note that the underlying terraform resources only allow the creation of groups with members that are part of the organization. If you want to create memberships for identities outside your own organization, you have to manually allow members outside your organization in the Cloud Identity admin console. + +As of version 3.50 of the GCP Terraform provider two operations are not working: +- removing a group that has at least one OWNER managed by terraform ([bug](https://github.com/hashicorp/terraform-provider-google/issues/7617)) +- removing a role from an existing membership ([bug](https://github.com/hashicorp/terraform-provider-google/issues/7616)) + +Until those two bugs are fixed, this module will only support the creation of MEMBER memberships. + +## Examples + +### Simple Group +```hcl +module "group" { + source = "./modules/cloud-identity-group" + customer_id = "customers/C01234567" + name = "mygroup@example.com" + display_name = "My group name" + description = "My group Description" + members = [ + "user1@example.com", + "user2@example.com", + "service-account@my-gcp-project.iam.gserviceaccount.com" + ] +} +# tftest:modules=1:resources=4 +``` + + +## Variables + +| name | description | type | required | default | +|---|---|:---: |:---:|:---:| +| customer_id | Directory customer ID in the form customers/C0xxxxxxx. | string | ✓ | | +| display_name | Group display name. | string | ✓ | | +| name | Group ID (usually an email). | string | ✓ | | +| *description* | Group description | string | | null | +| *members* | List of group members. | list(string) | | [] | + +## Outputs + +| name | description | sensitive | +|---|---|:---:| +| id | Group ID. | | +| name | Group name. | | + diff --git a/modules/cloud-identity-group/main.tf b/modules/cloud-identity-group/main.tf new file mode 100644 index 00000000..5a23c3a7 --- /dev/null +++ b/modules/cloud-identity-group/main.tf @@ -0,0 +1,52 @@ +/** + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +resource "google_cloud_identity_group" "group" { + display_name = var.display_name + parent = var.customer_id + + group_key { + id = var.name + } + + labels = { + "cloudidentity.googleapis.com/groups.discussion_forum" = "" + } +} + +# resource "google_cloud_identity_group_membership" "owners" { +# group = google_cloud_identity_group.group.id +# for_each = toset(var.owners) +# preferred_member_key { id = each.key } +# roles { name = "OWNER" } +# roles { name = "MEMBER" } +# roles { name = "MANAGER" } +# } + +# resource "google_cloud_identity_group_membership" "managers" { +# group = google_cloud_identity_group.group.id +# for_each = toset(var.managers) +# preferred_member_key { id = each.key } +# roles { name = "MEMBER" } +# roles { name = "MANAGER" } +# } + +resource "google_cloud_identity_group_membership" "members" { + group = google_cloud_identity_group.group.id + for_each = toset(var.members) + preferred_member_key { id = each.key } + roles { name = "MEMBER" } +} diff --git a/modules/cloud-identity-group/outputs.tf b/modules/cloud-identity-group/outputs.tf new file mode 100644 index 00000000..15b08d78 --- /dev/null +++ b/modules/cloud-identity-group/outputs.tf @@ -0,0 +1,28 @@ +/** + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +output "id" { + description = "Group ID." + value = google_cloud_identity_group.group.id +} + +output "name" { + description = "Group name." + value = var.name + depends_on = [ + google_cloud_identity_group.group + ] +} diff --git a/modules/cloud-identity-group/variables.tf b/modules/cloud-identity-group/variables.tf new file mode 100644 index 00000000..78873bef --- /dev/null +++ b/modules/cloud-identity-group/variables.tf @@ -0,0 +1,58 @@ +/** + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +variable "display_name" { + description = "Group display name." + type = string +} + +variable "name" { + description = "Group ID (usually an email)." + type = string +} + +variable "description" { + description = "Group description" + type = string + default = null +} + +variable "customer_id" { + description = "Directory customer ID in the form customers/C0xxxxxxx." + type = string + validation { + condition = can(regex("^customers/C0[a-z0-9]{7}$", var.customer_id)) + error_message = "Customer ID must be in the form customers/C0xxxxxxx." + } +} + +# variable "owners" { +# description = "List of group owners." +# type = list(string) +# default = [] +# } + +# variable "managers" { +# description = "List of group managers." +# type = list(string) +# default = [] +# } + +variable "members" { + description = "List of group members." + type = list(string) + default = [] +} diff --git a/modules/cloud-identity-group/versions.tf b/modules/cloud-identity-group/versions.tf new file mode 100644 index 00000000..07234c8e --- /dev/null +++ b/modules/cloud-identity-group/versions.tf @@ -0,0 +1,22 @@ +/** + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +terraform { + required_version = ">= 0.13.0" + required_providers { + google = "~> 3.49" + } +} diff --git a/tests/modules/cloud_identity_group/__init__.py b/tests/modules/cloud_identity_group/__init__.py new file mode 100644 index 00000000..6913f02e --- /dev/null +++ b/tests/modules/cloud_identity_group/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/tests/modules/cloud_identity_group/fixture/main.tf b/tests/modules/cloud_identity_group/fixture/main.tf new file mode 100644 index 00000000..b7998465 --- /dev/null +++ b/tests/modules/cloud_identity_group/fixture/main.tf @@ -0,0 +1,24 @@ +/** + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +module "test" { + source = "../../../../modules/cloud-identity-group" + name = var.name + display_name = var.display_name + description = var.description + customer_id = var.customer_id + members = var.members +} diff --git a/tests/modules/cloud_identity_group/fixture/variables.tf b/tests/modules/cloud_identity_group/fixture/variables.tf new file mode 100644 index 00000000..93ed3f09 --- /dev/null +++ b/tests/modules/cloud_identity_group/fixture/variables.tf @@ -0,0 +1,40 @@ +/** + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +variable "display_name" { + type = string + default = "display name" +} + +variable "name" { + type = string + default = "my-group@example.com" +} + +variable "description" { + type = string + default = null +} + +variable "customer_id" { + type = string + default = "customers/C01234567" +} + +variable "members" { + type = list(string) + default = [] +} diff --git a/tests/modules/cloud_identity_group/test_plan.py b/tests/modules/cloud_identity_group/test_plan.py new file mode 100644 index 00000000..798927f1 --- /dev/null +++ b/tests/modules/cloud_identity_group/test_plan.py @@ -0,0 +1,49 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import os +import pytest + +from collections import Counter + + +FIXTURES_DIR = os.path.join(os.path.dirname(__file__), 'fixture') + + +def test_group(plan_runner): + "Test group." + _, resources = plan_runner(FIXTURES_DIR) + assert len(resources) == 1 + r = resources[0] + assert r['type'] == 'google_cloud_identity_group' + assert r['values']['display_name'] == 'display name' + assert r['values']['group_key'][0]['id'] == 'my-group@example.com' + assert r['values']['parent'] == 'customers/C01234567' + + +def test_members(plan_runner): + "Test group members." + members = '["member@example.com"]' + _, resources = plan_runner(FIXTURES_DIR, members=members) + + resource_types = Counter([r['type'] for r in resources]) + assert resource_types == { + 'google_cloud_identity_group': 1, + 'google_cloud_identity_group_membership': 1, + } + + values = next(r['values'] for r in resources if r['name'] == 'members') + assert values['preferred_member_key'][0]['id'] == 'member@example.com' + assert [role['name'] for role in values['roles']] == ['MEMBER']