Skip to content

Commit 4f551d3

Browse files
authored
regional-go-service: grant compute.networkUser to Cloud Run service account. (#82)
This covers the case where the network may not be in the same project, and the permission to use the network is not granted by the host project run.serviceAgent role. Should resolve this error: https://github.com/chainguard-dev/mono/actions/runs/7647002860/job/20871902146#step:16:160
1 parent 848b0fd commit 4f551d3

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

modules/regional-go-service/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ No requirements.
8383
| [cosign_sign.this](https://registry.terraform.io/providers/chainguard-dev/cosign/latest/docs/resources/sign) | resource |
8484
| [google-beta_google_cloud_run_v2_service.this](https://registry.terraform.io/providers/hashicorp/google-beta/latest/docs/resources/google_cloud_run_v2_service) | resource |
8585
| [google_cloud_run_v2_service_iam_member.public-services-are-unauthenticated](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_v2_service_iam_member) | resource |
86+
| [google_compute_subnetwork_iam_member.member](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_subnetwork_iam_member) | resource |
8687
| [ko_build.this](https://registry.terraform.io/providers/ko-build/ko/latest/docs/resources/build) | resource |
8788

8889
## Inputs
@@ -93,6 +94,7 @@ No requirements.
9394
| <a name="input_egress"></a> [egress](#input\_egress) | The egress mode for the service. Must be one of ALL\_TRAFFIC, or PRIVATE\_RANGES\_ONLY. Egress traffic is routed through the regional VPC network from var.regions. | `string` | `"ALL_TRAFFIC"` | no |
9495
| <a name="input_ingress"></a> [ingress](#input\_ingress) | The ingress mode for the service. Must be one of INGRESS\_TRAFFIC\_ALL, INGRESS\_TRAFFIC\_INTERNAL\_LOAD\_BALANCER, or INGRESS\_TRAFFIC\_INTERNAL\_ONLY. | `string` | `"INGRESS_TRAFFIC_INTERNAL_ONLY"` | no |
9596
| <a name="input_name"></a> [name](#input\_name) | n/a | `string` | n/a | yes |
97+
| <a name="input_network_project"></a> [network\_project](#input\_network\_project) | (optional) The project in which the network and subnetworks reside. | `string` | `null` | no |
9698
| <a name="input_project_id"></a> [project\_id](#input\_project\_id) | n/a | `string` | n/a | yes |
9799
| <a name="input_regions"></a> [regions](#input\_regions) | A map from region names to a network and subnetwork. A pub/sub topic and ingress service (publishing to the respective topic) will be created in each region, with the ingress service configured to egress all traffic via the specified subnetwork. | <pre>map(object({<br> network = string<br> subnet = string<br> }))</pre> | n/a | yes |
98100
| <a name="input_service_account"></a> [service\_account](#input\_service\_account) | The service account as which to run the service. | `string` | n/a | yes |

modules/regional-go-service/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,17 @@ resource "google_cloud_run_v2_service_iam_member" "public-services-are-unauthent
155155
role = "roles/run.invoker"
156156
member = "allUsers"
157157
}
158+
159+
// Grant service account access to use subnet. This is typically granted with roles/run.serviceAgent,
160+
// but that role does not necessarily grant access if the network resides in another project.
161+
// See https://cloud.google.com/run/docs/configuring/vpc-direct-vpc#direct-vpc-service for more details.
162+
resource "google_compute_subnetwork_iam_member" "member" {
163+
for_each = var.regions
164+
165+
// If not set, provider project should be used.
166+
project = var.network_project
167+
region = each.key
168+
subnetwork = each.value.subnet
169+
role = "roles/compute.networkUser"
170+
member = "serviceAccount:${var.service_account}"
171+
}

modules/regional-go-service/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,9 @@ variable "volumes" {
8888
}))
8989
default = []
9090
}
91+
92+
variable "network_project" {
93+
description = "(optional) The project in which the network and subnetworks reside."
94+
type = string
95+
default = null
96+
}

0 commit comments

Comments
 (0)