Skip to content

Commit 2c202c1

Browse files
committed
Added IAP principals in load balancer backend service
1 parent 6e7e878 commit 2c202c1

File tree

15 files changed

+149
-11
lines changed

15 files changed

+149
-11
lines changed

.terraform.lock

Whitespace-only changes.

examples/backend-with-IAP/main.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module "lb-backend-iap" {
18+
# source = "terraform-google-modules/lb-http/google//modules/backend"
19+
# version = "~> 12.0"
20+
21+
source = "../../modules/backend"
22+
23+
project_id = var.project_id
24+
name = "backend-with-iap"
25+
iap_config = {
26+
iap_members = ["user:[email protected]"]
27+
}
28+
}
29+
30+
module "lb-frontend" {
31+
source = "terraform-google-modules/lb-http/google//modules/frontend"
32+
version = "~> 12.0"
33+
34+
project_id = var.project_id
35+
name = "global-lb-fe-bucket"
36+
url_map_input = module.lb-backend-iap.backend_service_info
37+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "load-balancer-ip" {
18+
value = module.lb-frontend.external_ip
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
type = string
19+
}

metadata.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ spec:
4040
- name: serverless_negs
4141
location: modules/serverless_negs
4242
examples:
43+
- name: backend-with-IAP
44+
location: examples/backend-with-IAP
4345
- name: cdn-policy
4446
location: examples/cdn-policy
4547
- name: certificate-map
@@ -346,6 +348,7 @@ spec:
346348
- roles/certificatemanager.owner
347349
- roles/vpcaccess.admin
348350
- roles/iam.serviceAccountAdmin
351+
- roles/iap.admin
349352
services:
350353
- cloudresourcemanager.googleapis.com
351354
- storage-api.googleapis.com

modules/backend/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This module creates `google_compute_backend_service` resource and its dependenci
2222
| groups | The list of backend instance group which serves the traffic. | <pre>list(object({<br> group = string<br> description = optional(string)<br><br> balancing_mode = optional(string)<br> capacity_scaler = optional(number)<br> max_connections = optional(number)<br> max_connections_per_instance = optional(number)<br> max_connections_per_endpoint = optional(number)<br> max_rate = optional(number)<br> max_rate_per_instance = optional(number)<br> max_rate_per_endpoint = optional(number)<br> max_utilization = optional(number)<br> }))</pre> | `[]` | no |
2323
| health\_check | Input for creating HttpHealthCheck or HttpsHealthCheck resource for health checking this BackendService. A health check must be specified unless the backend service uses an internet or serverless NEG as a backend. | <pre>object({<br> host = optional(string, null)<br> request_path = optional(string, null)<br> request = optional(string, null)<br> response = optional(string, null)<br> port = optional(number, null)<br> port_name = optional(string, null)<br> proxy_header = optional(string, null)<br> port_specification = optional(string, null)<br> protocol = optional(string, null)<br> check_interval_sec = optional(number, 5)<br> timeout_sec = optional(number, 5)<br> healthy_threshold = optional(number, 2)<br> unhealthy_threshold = optional(number, 2)<br> logging = optional(bool, false)<br> })</pre> | `null` | no |
2424
| host\_path\_mappings | The list of host/path for which traffic could be sent to the backend service | <pre>list(object({<br> host = string<br> path = string<br> }))</pre> | <pre>[<br> {<br> "host": "*",<br> "path": "/*"<br> }<br>]</pre> | no |
25-
| iap\_config | Settings for enabling Cloud Identity Aware Proxy Structure. | <pre>object({<br> enable = bool<br> oauth2_client_id = optional(string)<br> oauth2_client_secret = optional(string)<br> })</pre> | <pre>{<br> "enable": false<br>}</pre> | no |
25+
| iap\_config | Settings for enabling Cloud Identity Aware Proxy Structure and Users/SAs to be given IAP HttpResourceAccessor access to the service. | <pre>object({<br> iap_members = list(string)<br> oauth2_client_id = optional(string)<br> oauth2_client_secret = optional(string)<br> })</pre> | `null` | no |
2626
| load\_balancing\_scheme | Load balancing scheme type (EXTERNAL for classic external load balancer, EXTERNAL\_MANAGED for Envoy-based load balancer, INTERNAL\_MANAGED for internal load balancer and INTERNAL\_SELF\_MANAGED for traffic director) | `string` | `"EXTERNAL_MANAGED"` | no |
2727
| locality\_lb\_policy | The load balancing algorithm used within the scope of the locality. | `string` | `null` | no |
2828
| log\_config | This field denotes the logging options for the load balancer traffic served by this backend service. If logging is enabled, logs will be exported to Stackdriver. | <pre>object({<br> enable = bool<br> sample_rate = number<br> })</pre> | <pre>{<br> "enable": true,<br> "sample_rate": 1<br>}</pre> | no |

modules/backend/main.tf

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ resource "google_compute_backend_service" "default" {
8080
}
8181

8282
dynamic "iap" {
83-
for_each = var.iap_config.enable ? [1] : []
83+
for_each = length(var.iap_config.iap_members) > 0 ? [1] : []
8484
content {
8585
oauth2_client_id = lookup(var.iap_config, "oauth2_client_id", "")
86-
enabled = var.iap_config.enable
86+
enabled = length(var.iap_config.iap_members) > 0
8787
oauth2_client_secret = lookup(var.iap_config, "oauth2_client_secret", "")
8888
}
8989
}
@@ -365,3 +365,12 @@ resource "google_compute_backend_bucket" "default" {
365365
}
366366
}
367367
}
368+
369+
resource "google_iap_web_backend_service_iam_member" "member" {
370+
for_each = toset(var.iap_config.iap_members)
371+
project = google_compute_backend_service.default[0].project
372+
web_backend_service = google_compute_backend_service.default[0].name
373+
role = "roles/iap.httpsResourceAccessor"
374+
member = each.value
375+
}
376+

modules/backend/metadata.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ spec:
3232
description: {}
3333
content:
3434
examples:
35+
- name: backend-with-IAP
36+
location: examples/backend-with-IAP
3537
- name: cdn-policy
3638
location: examples/cdn-policy
3739
- name: certificate-map
@@ -189,15 +191,13 @@ spec:
189191
spec:
190192
outputExpr: name
191193
- name: iap_config
192-
description: Settings for enabling Cloud Identity Aware Proxy Structure.
194+
description: Settings for enabling Cloud Identity Aware Proxy Structure and Users/SAs to be given IAP HttpResourceAccessor access to the service.
193195
varType: |-
194196
object({
195-
enable = bool
197+
iap_members = list(string)
196198
oauth2_client_id = optional(string)
197199
oauth2_client_secret = optional(string)
198200
})
199-
defaultValue:
200-
enable: false
201201
- name: cdn_policy
202202
description: Cloud CDN configuration for this BackendService.
203203
varType: |-
@@ -333,6 +333,7 @@ spec:
333333
- roles/certificatemanager.owner
334334
- roles/vpcaccess.admin
335335
- roles/iam.serviceAccountAdmin
336+
- roles/iap.admin
336337
services:
337338
- cloudresourcemanager.googleapis.com
338339
- storage-api.googleapis.com

modules/backend/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ variable "backend_bucket_name" {
154154
}
155155

156156
variable "iap_config" {
157-
description = "Settings for enabling Cloud Identity Aware Proxy Structure."
157+
description = "Settings for enabling Cloud Identity Aware Proxy Structure and Users/SAs to be given IAP HttpResourceAccessor access to the service."
158158
type = object({
159-
enable = bool
159+
iap_members = list(string)
160160
oauth2_client_id = optional(string)
161161
oauth2_client_secret = optional(string)
162162
})
163-
default = { enable = false }
163+
default = null
164164
}
165165

166166
variable "cdn_policy" {

modules/dynamic_backends/metadata.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ spec:
3232
description: {}
3333
content:
3434
examples:
35+
- name: backend-with-IAP
36+
location: examples/backend-with-IAP
3537
- name: cdn-policy
3638
location: examples/cdn-policy
3739
- name: certificate-map
@@ -338,6 +340,7 @@ spec:
338340
- roles/certificatemanager.owner
339341
- roles/vpcaccess.admin
340342
- roles/iam.serviceAccountAdmin
343+
- roles/iap.admin
341344
services:
342345
- cloudresourcemanager.googleapis.com
343346
- storage-api.googleapis.com

0 commit comments

Comments
 (0)