Skip to content

Commit f116dd5

Browse files
kaarigerbharathkkb
andauthored
feat: added the grant_services_network_role flag to control network IAM (#618)
* feat: added the grant_services_network_role flag to control network IAM * Update modules/shared_vpc_access/variables.tf Co-authored-by: Bharath KKB <bharathkrishnakb@gmail.com> * updated description for grant_services_network_role flag Co-authored-by: kaariger <kaariger@users.noreply.github.com> Co-authored-by: Bharath KKB <bharathkrishnakb@gmail.com>
1 parent 87d2df0 commit f116dd5

File tree

14 files changed

+100
-5
lines changed

14 files changed

+100
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ determining that location is as follows:
133133
| domain | The domain name (optional). | `string` | `""` | no |
134134
| enable\_shared\_vpc\_host\_project | If this project is a shared VPC host project. If true, you must *not* set svpc\_host\_project\_id variable. Default is false. | `bool` | `false` | no |
135135
| folder\_id | The ID of a folder to host this project | `string` | `""` | no |
136+
| grant\_services\_network\_role | Whether or not to grant service agents the network roles on the host project | `bool` | `true` | no |
136137
| grant\_services\_security\_admin\_role | Whether or not to grant Kubernetes Engine Service Agent the Security Admin role on the host project so it can manage firewall rules | `bool` | `false` | no |
137138
| group\_name | A group to control the project by being assigned group\_role (defaults to project editor) | `string` | `""` | no |
138139
| group\_role | The role to give the controlling group (group\_name) over the project (defaults to project editor) | `string` | `"roles/editor"` | no |

examples/shared_vpc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ It then attaches two new service projects to the host project.
2828
| network\_self\_link | The URI of the VPC being created |
2929
| service\_project | The service project info |
3030
| service\_project\_b | The second service project |
31+
| service\_project\_c | The third service project |
3132
| subnets | The shared VPC subets |
3233
| vpc | The network info |
3334

examples/shared_vpc/main.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,40 @@ module "service-project-b" {
156156
disable_services_on_destroy = false
157157
}
158158

159+
/******************************************
160+
Third Service Project Creation
161+
To test the grant_services_network_role
162+
*****************************************/
163+
module "service-project-c" {
164+
source = "../../modules/svpc_service_project"
165+
166+
name = "c-${var.service_project_name}"
167+
random_project_id = false
168+
169+
org_id = var.organization_id
170+
folder_id = var.folder_id
171+
billing_account = var.billing_account
172+
173+
shared_vpc = module.host-project.project_id
174+
175+
activate_apis = [
176+
"compute.googleapis.com",
177+
"container.googleapis.com",
178+
"dataproc.googleapis.com",
179+
]
180+
181+
activate_api_identities = [{
182+
api = "healthcare.googleapis.com"
183+
roles = [
184+
"roles/healthcare.serviceAgent",
185+
"roles/bigquery.jobUser",
186+
]
187+
}]
188+
189+
disable_services_on_destroy = false
190+
grant_services_network_role = false
191+
}
192+
159193
/******************************************
160194
Example dependency on service-project
161195
*****************************************/

examples/shared_vpc/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ output "service_project_b" {
3434
description = "The second service project"
3535
}
3636

37+
output "service_project_c" {
38+
value = module.service-project-c
39+
description = "The third service project"
40+
}
41+
3742
output "vpc" {
3843
value = module.vpc
3944
description = "The network info"

modules/shared_vpc_access/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module "shared_vpc_access" {
2929
|------|-------------|------|---------|:--------:|
3030
| active\_apis | The list of active apis on the service project. If api is not active this module will not try to activate it | `list(string)` | `[]` | no |
3131
| enable\_shared\_vpc\_service\_project | Flag set if SVPC enabled | `bool` | n/a | yes |
32+
| grant\_services\_network\_role | Whether or not to grant service agents the network roles on the host project | `bool` | `true` | no |
3233
| grant\_services\_security\_admin\_role | Whether or not to grant Kubernetes Engine Service Agent the Security Admin role on the host project so it can manage firewall rules | `bool` | `false` | no |
3334
| host\_project\_id | The ID of the host project which hosts the shared VPC | `string` | n/a | yes |
3435
| lookup\_project\_numbers | Whether to look up the project numbers from data sources. If false, `service_project_number` will be used instead. | `bool` | `true` | no |

modules/shared_vpc_access/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ locals {
4545
*****************************************/
4646
resource "google_compute_subnetwork_iam_member" "service_shared_vpc_subnet_users" {
4747
provider = google-beta
48-
count = length(local.subnetwork_api)
48+
count = var.grant_services_network_role ? length(local.subnetwork_api) : 0
4949
subnetwork = element(
5050
split("/", local.subnetwork_api[count.index][1]),
5151
index(
@@ -68,7 +68,7 @@ resource "google_compute_subnetwork_iam_member" "service_shared_vpc_subnet_users
6868
if "dataflow.googleapis.com" compute.networkUser role granted to dataflow service account for Dataflow on shared VPC Project if no subnets defined
6969
*****************************************/
7070
resource "google_project_iam_member" "service_shared_vpc_user" {
71-
for_each = (length(var.shared_vpc_subnets) == 0) && var.enable_shared_vpc_service_project ? local.active_apis : []
71+
for_each = (length(var.shared_vpc_subnets) == 0) && var.enable_shared_vpc_service_project && var.grant_services_network_role ? local.active_apis : []
7272
project = var.host_project_id
7373
role = "roles/compute.networkUser"
7474
member = format("serviceAccount:%s", local.apis[each.value])
@@ -79,7 +79,7 @@ resource "google_project_iam_member" "service_shared_vpc_user" {
7979
See: https://cloud.google.com/composer/docs/how-to/managing/configuring-shared-vpc
8080
*****************************************/
8181
resource "google_project_iam_member" "composer_host_agent" {
82-
count = local.composer_shared_vpc_enabled && var.enable_shared_vpc_service_project ? 1 : 0
82+
count = local.composer_shared_vpc_enabled && var.enable_shared_vpc_service_project && var.grant_services_network_role ? 1 : 0
8383
project = var.host_project_id
8484
role = "roles/composer.sharedVpcAgent"
8585
member = format("serviceAccount:%s", local.apis["composer.googleapis.com"])
@@ -90,7 +90,7 @@ resource "google_project_iam_member" "composer_host_agent" {
9090
See: https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc
9191
*****************************************/
9292
resource "google_project_iam_member" "gke_host_agent" {
93-
count = local.gke_shared_vpc_enabled && var.enable_shared_vpc_service_project ? 1 : 0
93+
count = local.gke_shared_vpc_enabled && var.enable_shared_vpc_service_project && var.grant_services_network_role ? 1 : 0
9494
project = var.host_project_id
9595
role = "roles/container.hostServiceAgentUser"
9696
member = format("serviceAccount:%s", local.apis["container.googleapis.com"])

modules/shared_vpc_access/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ variable "grant_services_security_admin_role" {
5858
type = bool
5959
default = false
6060
}
61+
62+
variable "grant_services_network_role" {
63+
description = "Whether or not to grant service agents the network roles on the host project"
64+
type = bool
65+
default = true
66+
}

modules/svpc_service_project/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module "service-project" {
4949
| disable\_services\_on\_destroy | Whether project services will be disabled when the resources are destroyed | `bool` | `true` | no |
5050
| domain | The domain name (optional). | `string` | `""` | no |
5151
| folder\_id | The ID of a folder to host this project | `string` | `""` | no |
52+
| grant\_services\_network\_role | Whether or not to grant service agents the network roles on the host project | `bool` | `true` | no |
5253
| grant\_services\_security\_admin\_role | Whether or not to grant Kubernetes Engine Service Agent the Security Admin role on the host project so it can manage firewall rules | `bool` | `false` | no |
5354
| group\_name | A group to control the project by being assigned group\_role (defaults to project editor) | `string` | `""` | no |
5455
| group\_role | The role to give the controlling group (group\_name) over the project (defaults to project editor) | `string` | `"roles/editor"` | no |

modules/svpc_service_project/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module "shared_vpc_access" {
7272
service_project_number = module.project-factory.project_number
7373
lookup_project_numbers = false
7474
grant_services_security_admin_role = var.grant_services_security_admin_role
75+
grant_services_network_role = var.grant_services_network_role
7576
}
7677

7778
/******************************************

modules/svpc_service_project/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,9 @@ variable "grant_services_security_admin_role" {
215215
type = bool
216216
default = false
217217
}
218+
219+
variable "grant_services_network_role" {
220+
description = "Whether or not to grant service agents the network roles on the host project"
221+
type = bool
222+
default = true
223+
}

0 commit comments

Comments
 (0)