Skip to content

Commit

Permalink
Allow additive IAM grants by robots name
Browse files Browse the repository at this point in the history
Regreatablly member name will be known after apply, hence changes in the
tests
  • Loading branch information
wiktorn committed Feb 16, 2023
1 parent 36a7347 commit 1901ea0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
23 changes: 23 additions & 0 deletions modules/project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ module "project" {
# tftest modules=1 resources=2
```

### Using shortcodes for Service Identities in additive IAM
Most Service Identities contains project number in their e-mail address and this prevents additive IAM to work, as these values are not known at moment of execution of `terraform plan` (its not an issue for authoritative IAM). To refer current project Service Identities you may use shortcodes for Service Identities similarly as for `service_identity_iam` when configuring Shared VPC.

```hcl
module "project" {
source = "./fabric/modules/project"
name = "project-example"
services = [
"run.googleapis.com",
"container.googleapis.com",
]
iam_additive = {
"roles/editor" = ["cloudservices"]
"roles/vpcaccess.user" = ["cloudrun"]
"roles/container.hostServiceAgentUser" = ["container-engine"]
}
}
# tftest modules=1 resources=6
```


### Service identities requiring manual IAM grants

The module will create service identities at project creation instead of creating of them at the time of first use. This allows granting these service identities roles in other projects, something which is usually necessary in a Shared VPC context.
Expand Down
13 changes: 12 additions & 1 deletion modules/project/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@ locals {
}
iam_additive = {
for pair in concat(local._iam_additive_pairs, local._iam_additive_member_pairs) :
"${pair.role}-${pair.member}" => pair
"${pair.role}-${pair.member}" => {
role = pair.role
member = (
pair.member == "cloudservices"
? "serviceAccount:${local.service_account_cloud_services}"
: pair.member == "default-compute"
? "serviceAccount:${local.service_accounts_default.compute}"
: pair.member == "default-gae"
? "serviceAccount:${local.service_accounts_default.gae}"
: try("serviceAccount:${local.service_accounts_robots[pair.member]}", pair.member)
)
}
}
}

Expand Down
3 changes: 0 additions & 3 deletions tests/modules/project/examples/iam-additive-members.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ values:
project_id: project-example
module.project.google_project_iam_member.additive["roles/editor-user:[email protected]"]:
condition: []
member: user:[email protected]
project: project-example
role: roles/editor
module.project.google_project_iam_member.additive["roles/owner-user:[email protected]"]:
condition: []
member: user:[email protected]
project: project-example
role: roles/owner
module.project.google_project_iam_member.additive["roles/owner-user:[email protected]"]:
condition: []
member: user:[email protected]
project: project-example
role: roles/owner

Expand Down
4 changes: 0 additions & 4 deletions tests/modules/project/examples/iam-additive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@ values:
module.project.google_project.project[0]: {}
module.project.google_project_iam_member.additive["roles/owner-group:[email protected]"]:
condition: []
member: group:[email protected]
project: project-example
role: roles/owner
module.project.google_project_iam_member.additive["roles/storage.objectAdmin-group:[email protected]"]:
condition: []
member: group:[email protected]
project: project-example
role: roles/storage.objectAdmin
module.project.google_project_iam_member.additive["roles/viewer-group:[email protected]"]:
condition: []
member: group:[email protected]
project: project-example
role: roles/viewer
module.project.google_project_iam_member.additive["roles/viewer-group:[email protected]"]:
condition: []
member: group:[email protected]
project: project-example
role: roles/viewer

Expand Down

0 comments on commit 1901ea0

Please sign in to comment.