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

Add cross-project IAM binding support #955

Merged
merged 40 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a38b162
Add recipe for cross-project IAM bindings
pasha-gh Jul 13, 2021
22a0fee
Add iam bindgin dependencies to project.hcl recipe
pasha-gh Jul 13, 2021
cbfbcbe
Add example for cross-project IAM bindings
pasha-gh Jul 13, 2021
8cfdc5c
Add cross-project IAM bindings to team.hcl example
pasha-gh Jul 13, 2021
afd0e73
Add auto-generated files after running ./regen.sh
pasha-gh Jul 13, 2021
8061bec
Fix typo
pasha-gh Jul 14, 2021
743b372
Fix typo
pasha-gh Jul 14, 2021
cc2cf43
Update example deprecated roles
pasha-gh Jul 14, 2021
cb6031f
Fix typo
pasha-gh Jul 14, 2021
ab32013
Remove dependency between iam_bindings and project recipes
pasha-gh Jul 14, 2021
04dc775
Add newline at the end of file
pasha-gh Jul 14, 2021
f381401
Rename existing iam_members component to avoid confusion
pasha-gh Jul 14, 2021
a6822d0
Rename iam_bindings to iam_members for better clarity
pasha-gh Jul 14, 2021
3b690d0
Refactor iam_members component
pasha-gh Jul 14, 2021
06e83b8
Redesign iam_members recipe and example
pasha-gh Jul 15, 2021
92909c5
Update iam_members in team.hcl example
pasha-gh Jul 15, 2021
7d9d9bb
Add folder_iam_member to iam_members.hcl example
pasha-gh Jul 15, 2021
6d12009
Add comments indicating the type of parent_id in iam_members example
pasha-gh Jul 15, 2021
bd0df27
Move comments from iam_members example to recipe description
pasha-gh Jul 15, 2021
2e8bd8a
Add/Update auto-generated files by running ./regen.sh
pasha-gh Jul 15, 2021
eb48876
Add missing license
pasha-gh Jul 15, 2021
c7337f3
Fix typo
pasha-gh Jul 15, 2021
69f58ac
Refactor to remove redundant code
pasha-gh Jul 15, 2021
2336cdd
Add/Update auto-generated files by running ./regen.sh
pasha-gh Jul 15, 2021
d2fa6d5
Rename variables
pasha-gh Jul 15, 2021
f13ae85
Update docs and comments to consistently follow existing code, docs, …
pasha-gh Jul 16, 2021
f4b0972
Update parent_id description with examples
pasha-gh Jul 16, 2021
1c7a580
Fix typo
pasha-gh Jul 16, 2021
2541a58
Remove redundant if statement
pasha-gh Jul 16, 2021
206353b
Remove service accounts from supported iam members
pasha-gh Jul 16, 2021
00e210e
Add/Update auto-generated files by running ./regen.sh
pasha-gh Jul 16, 2021
c52867d
Rename parent_ids to resource_ids
pasha-gh Jul 16, 2021
5a56163
Include additional_iam_members template in the integration tests
pasha-gh Jul 16, 2021
6b3a20e
Remove redundant code
pasha-gh Jul 16, 2021
d56ddf0
Update iam_members exmaple code format
pasha-gh Jul 16, 2021
43c5a1d
Update IAM members component variable descriptions and types
pasha-gh Jul 16, 2021
d9d8a9d
Refactor for_each meta-argument for iam members
pasha-gh Jul 16, 2021
21440c0
Update iam members component variable descriptions.
pasha-gh Jul 16, 2021
7847f7a
Remove redundant data from iam members example
pasha-gh Jul 16, 2021
a9c86f0
Remove empty lines from generated iam members terraform.tfvars
pasha-gh Jul 16, 2021
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
13 changes: 13 additions & 0 deletions docs/tfengine/schemas/iam_bindings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Recipe for iam bindings

<!-- These files are auto generated -->

## Properties

| Property | Description | Type | Required | Default | Pattern |
| -------- | ----------- | ---- | -------- | ------- | ------- |
| iam_bindings | [Module](https://github.com/terraform-google-modules/terraform-google-iam) | array(object) | false | - | - |
| iam_bindings.bindings | Map of IAM role to list of members to grant access to the role. | object | true | - | - |
| iam_bindings.parent_ids | Ids of the parent to assign the bindings. | array(string) | false | - | - |
| iam_bindings.parent_type | Type of the resource to assign the bindings. | string | true | - | ^storage_bucket\|project\|organization\|folder\|billing_account$ |
| state_bucket | Bucket to store remote state. | string | false | - | - |
1 change: 1 addition & 0 deletions docs/tfengine/schemas/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

| Property | Description | Type | Required | Default | Pattern |
| -------- | ----------- | ---- | -------- | ------- | ------- |
| iam_bindings | IAM bindings for this or other projects. See [iam_bindings.md](./iam_bindings.md) for schema. | - | false | - | - |
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
| parent_id | ID of parent GCP resource to apply the policy Can be one of the organization ID or folder ID according to parent_type. | string | false | - | - |
| parent_type | Type of parent GCP resource to apply the policy Can be one of 'organization' or 'folder'. | string | false | - | ^organization\|folder$ |
| project | Config for the project. | object | true | - | - |
Expand Down
97 changes: 97 additions & 0 deletions examples/tfengine/generated/iam_bindings/iam_bindings/main.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/tfengine/generated/team/cicd/triggers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ resource "google_cloudbuild_trigger" "validate_prod" {

substitutions = {
_TERRAFORM_ROOT = "terraform"
_MANAGED_DIRS = "project_secrets project_networks project_apps project_data"
_MANAGED_DIRS = "project_secrets project_networks project_apps project_data projetc_iam"
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
}

depends_on = [
Expand Down Expand Up @@ -66,7 +66,7 @@ resource "google_cloudbuild_trigger" "plan_prod" {

substitutions = {
_TERRAFORM_ROOT = "terraform"
_MANAGED_DIRS = "project_secrets project_networks project_apps project_data"
_MANAGED_DIRS = "project_secrets project_networks project_apps project_data projetc_iam"
}

depends_on = [
Expand Down Expand Up @@ -97,7 +97,7 @@ resource "google_cloudbuild_trigger" "apply_prod" {

substitutions = {
_TERRAFORM_ROOT = "terraform"
_MANAGED_DIRS = "project_secrets project_networks project_apps project_data"
_MANAGED_DIRS = "project_secrets project_networks project_apps project_data projetc_iam"
}

depends_on = [
Expand Down
77 changes: 77 additions & 0 deletions examples/tfengine/generated/team/project_iam/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2021 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.14"
required_providers {
google = "~> 3.0"
google-beta = "~> 3.0"
kubernetes = "~> 1.0"
}
backend "gcs" {
bucket = "example-terraform-state"
prefix = "project_iam"
}
}

# Create the project and optionally enable APIs, create the deletion lien and add to shared VPC.
# Deletion lien: https://cloud.google.com/resource-manager/docs/project-liens
# Shared VPC: https://cloud.google.com/docs/enterprise/best-practices-for-enterprise-organizations#centralize_network_control
module "project" {
source = "terraform-google-modules/project-factory/google"
version = "~> 11.1.0"

name = "example-prod-iam"
org_id = ""
folder_id = "12345678"
billing_account = "000-000-000"
lien = true
# Create and keep default service accounts when certain APIs are enabled.
default_service_account = "keep"
# Do not create an additional project service account to be used for Compute Engine.
create_project_sa = false
# When Kubernetes Engine API is enabled, grant Kubernetes Engine Service Agent the
# Compute Security Admin role on the VPC host project so it can manage firewall rules.
# It is a no-op when Kubernetes Engine API is not enabled in the project.
grant_services_security_admin_role = true
activate_apis = []
}

module "storage_bucket_iam_bindings_0" {
source = "terraform-google-modules/iam/google//modules/storage_buckets_iam"
storage_buckets = [
"example-bucket",
]
mode = "additive"

bindings = {
"roles/storage.admin" = [
"serviceAccount:[email protected]",
],
}
}

module "project_iam_bindings_1" {
source = "terraform-google-modules/iam/google//modules/projects_iam"
projects = [
"example-prod-data",
]
mode = "additive"

bindings = {
"roles/browser" = [
"serviceAccount:[email protected]",
],
}
}
73 changes: 73 additions & 0 deletions examples/tfengine/iam_bindings.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2021 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.

# {{$recipes := "../../templates/tfengine/recipes"}}

data = {
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
parent_type = "folder" # One of `organization` or `folder`.
parent_id = "12345678"
billing_account = "000-000-000"
state_bucket = "example-terraform-state"
storage_location = "us-central1"
}

template "iam_bindings" {
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
recipe_path = "{{$recipes}}/project.hcl"
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
output_path = "./iam_bindings"
data = {
project = {
project_id = "example-iam"
exists = false
}
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
iam_bindings = [{
parent_type = "storage_bucket"
parent_ids = [
"example-bucket",
]
bindings = {
"roles/storage.legacyBucketReader" = [
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
"serviceAccount:[email protected]",
"group:[email protected]",
"user:[email protected]"
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
]
"roles/storage.legacyBucketWriter" = [
"serviceAccount:[email protected]",
]
}
},
{
parent_type = "project"
parent_ids = [
"example-project-one",
"example-project-two",
]
bindings = {
"roles/compute.networkAdmin" = [
"serviceAccount:[email protected]",
]
}
},
{
parent_type = "project"
parent_ids = [
"example-project-one"
]
bindings = {
"roles/compute.loadBalancerAdmin" = [
"serviceAccount:[email protected]",
]
}
}]
}
}
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions examples/tfengine/modules/foundation.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ template "cicd" {
"project_networks",
"project_apps",
"project_data",
"projetc_iam",
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
]
}
]
Expand Down
34 changes: 34 additions & 0 deletions examples/tfengine/modules/team.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,40 @@ EOF
}
}

# IAM bindings - adding storage admin permissions to a service account.
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
template "project_iam" {
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
recipe_path = "{{.recipes}}/project.hcl"
pasha-gh marked this conversation as resolved.
Show resolved Hide resolved
output_path = "./project_iam"
data = {
project = {
project_id = "{{.prefix}}-{{.env}}-iam"
// exists = false
}
iam_bindings = [{
parent_type = "storage_bucket"
parent_ids = [
"{{.prefix}}-bucket",
]
bindings = {
"roles/storage.admin" = [
"serviceAccount:runner@{{.prefix}}-{{.env}}-apps.iam.gserviceaccount.com"
]
}
},
{
parent_type = "project"
parent_ids = [
"{{.prefix}}-{{.env}}-data",
]
bindings = {
"roles/browser" = [
"serviceAccount:runner@{{.prefix}}-{{.env}}-apps.iam.gserviceaccount.com"
]
}
}]
}
}

# Kubernetes Terraform deployment. This should be deployed after the GKE Cluster has been deployed.
template "kubernetes" {
recipe_path = "{{.recipes}}/deployment.hcl"
Expand Down
Loading