Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Identity Group module #182

Merged
merged 7 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions modules/cloud-identity-group/README.md
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: potentially external members can be allowed after setting allowExternalMembers - this can't be easily automated with TF yet as the provisioner doesn't support the admin-sdk.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the terraform resource uses the Directory API, which doesn't support the allowExternalMembers option.


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 = "[email protected]"
display_name = "My group name"
description = "My group Description"
members = [
"[email protected]",
"[email protected]",
"[email protected]"
]
}
# tftest:modules=1:resources=4
```

<!-- BEGIN TFDOC -->
## Variables

| name | description | type | required | default |
|---|---|:---: |:---:|:---:|
| customer_id | Directory customer ID in the form customers/C0xxxxxxx. | <code title="string&#10;validation &#123;&#10;condition &#61; can&#40;regex&#40;&#34;&#94;customers&#47;C0&#91;a-z0-9&#93;&#123;7&#125;&#36;&#34;, var.customer_id&#41;&#41;&#10;error_message &#61; &#34;Customer ID must be in the form customers&#47;C0xxxxxxx.&#34;&#10;&#125;">string</code> | ✓ | |
| display_name | Group display name. | <code title="">string</code> | ✓ | |
| name | Group ID (usually an email). | <code title="">string</code> | ✓ | |
| *description* | Group description | <code title="">string</code> | | <code title="">null</code> |
| *members* | List of group members. | <code title="list&#40;string&#41;">list(string)</code> | | <code title="">[]</code> |

## Outputs

| name | description | sensitive |
|---|---|:---:|
| id | Group ID. | |
| name | Group name. | |
<!-- END TFDOC -->
52 changes: 52 additions & 0 deletions modules/cloud-identity-group/main.tf
Original file line number Diff line number Diff line change
@@ -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" }
}
28 changes: 28 additions & 0 deletions modules/cloud-identity-group/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
]
}
58 changes: 58 additions & 0 deletions modules/cloud-identity-group/variables.tf
Original file line number Diff line number Diff line change
@@ -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 = []
}
22 changes: 22 additions & 0 deletions modules/cloud-identity-group/versions.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 13 additions & 0 deletions tests/modules/cloud_identity_group/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
24 changes: 24 additions & 0 deletions tests/modules/cloud_identity_group/fixture/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions tests/modules/cloud_identity_group/fixture/variables.tf
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
}

variable "description" {
type = string
default = null
}

variable "customer_id" {
type = string
default = "customers/C01234567"
}

variable "members" {
type = list(string)
default = []
}
49 changes: 49 additions & 0 deletions tests/modules/cloud_identity_group/test_plan.py
Original file line number Diff line number Diff line change
@@ -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'] == '[email protected]'
assert r['values']['parent'] == 'customers/C01234567'


def test_members(plan_runner):
"Test group members."
members = '["[email protected]"]'
_, 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'] == '[email protected]'
assert [role['name'] for role in values['roles']] == ['MEMBER']