Skip to content

Commit 8916c06

Browse files
committed
Add a parameter 'registry_project_id'
The PR allows configuring the project holding the GCR registry when used in connection with 'create_service_account'=true and grant_registry_access=true. Holding the GCR is a project with other resources increases the risk of exposing sensitive data to the service account running the nodes, as the required permissions of role roles/storage.objectViewer provide access to all storage objects in the project.
1 parent 81eb717 commit 8916c06

File tree

23 files changed

+127
-11
lines changed

23 files changed

+127
-11
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ Then perform the following commands on the root folder:
108108
- `terraform apply` to apply the infrastructure build
109109
- `terraform destroy` to destroy the built infrastructure
110110

111+
## Upgrade to v3.0.0
112+
113+
v3.0.0 is a breaking release. Refer to the
114+
[Upgrading to v3.0 guide][upgrading-to-v3.0] for details.
115+
116+
## Upgrade to v2.0.0
117+
118+
v2.0.0 is a breaking release. Refer to the
119+
[Upgrading to v2.0 guide][upgrading-to-v2.0] for details.
120+
121+
## Upgrade to v1.0.0
122+
123+
Version 1.0.0 of this module introduces a breaking change: adding the `disable-legacy-endpoints` metadata field to all node pools. This metadata is required by GKE and [determines whether the `/0.1/` and `/v1beta1/` paths are available in the nodes' metadata server](https://cloud.google.com/kubernetes-engine/docs/how-to/protecting-cluster-metadata#disable-legacy-apis). If your applications do not require access to the node's metadata server, you can leave the default value of `true` provided by the module. If your applications require access to the metadata server, be sure to read the linked documentation to see if you need to set the value for this field to `false` to allow your applications access to the above metadata server paths.
124+
125+
In either case, upgrading to module version `v1.0.0` will trigger a recreation of all node pools in the cluster.
126+
111127
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
112128
## Inputs
113129

@@ -151,6 +167,7 @@ Then perform the following commands on the root folder:
151167
| project\_id | The project ID to host the cluster in (required) | string | n/a | yes |
152168
| region | The region to host the cluster in (required) | string | n/a | yes |
153169
| regional | Whether is a regional cluster (zonal cluster if set false. WARNING: changing this after cluster creation is destructive!) | bool | `"true"` | no |
170+
| registry\_project\_id | Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project. | string | `""` | no |
154171
| remove\_default\_node\_pool | Remove default node pool while setting up the cluster | bool | `"false"` | no |
155172
| service\_account | The service account to run nodes as if not overridden in `node_pools`. The create_service_account variable default value (true) will cause a cluster-specific service account to be created. | string | `""` | no |
156173
| stub\_domains | Map of stub domains and their resolvers to forward DNS queries for a certain domain to an external DNS server | map(list(string)) | `<map>` | no |
@@ -212,6 +229,9 @@ following project roles:
212229
- roles/iam.serviceAccountUser
213230
- roles/resourcemanager.projectIamAdmin (only required if `service_account` is set to `create`)
214231

232+
Additionally, if `service_account` is set to `create` and `grant_registry_access` is requested, the service account requires the following role on the `registry_project_id` project:
233+
- roles/resourcemanager.projectIamAdmin
234+
215235
### Enable APIs
216236
In order to operate with the Service Account you must activate the following APIs on the project where the Service Account was created:
217237

autogen/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ following project roles:
269269
- roles/iam.serviceAccountUser
270270
- roles/resourcemanager.projectIamAdmin (only required if `service_account` is set to `create`)
271271

272+
Additionally, if `service_account` is set to `create` and `grant_registry_access` is requested, the service account requires the following role on the `registry_project_id` project:
273+
- roles/resourcemanager.projectIamAdmin
274+
272275
### Enable APIs
273276
In order to operate with the Service Account you must activate the following APIs on the project where the Service Account was created:
274277

autogen/sa.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ resource "google_project_iam_member" "cluster_service_account-monitoring_viewer"
6464

6565
resource "google_project_iam_member" "cluster_service_account-gcr" {
6666
count = var.create_service_account && var.grant_registry_access ? 1 : 0
67-
project = var.project_id
67+
project = var.registry_project_id == "" ? var.project_id : var.registry_project_id
6868
role = "roles/storage.objectViewer"
6969
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
7070
}

autogen/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ variable "grant_registry_access" {
269269
default = false
270270
}
271271

272+
variable "registry_project_id" {
273+
type = string
274+
description = "Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project."
275+
default = ""
276+
}
277+
272278
variable "service_account" {
273279
type = string
274280
description = "The service account to run nodes as if not overridden in `node_pools`. The create_service_account variable default value (true) will cause a cluster-specific service account to be created."

examples/workload_metadata_config/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ module "gke" {
4040
subnetwork = var.subnetwork
4141
ip_range_pods = var.ip_range_pods
4242
ip_range_services = var.ip_range_services
43-
create_service_account = false
44-
service_account = var.compute_engine_service_account
43+
create_service_account = true
44+
grant_registry_access = true
45+
registry_project_id = var.registry_project_id
4546
enable_private_endpoint = true
4647
enable_private_nodes = true
4748
master_ipv4_cidr_block = "172.16.0.0/28"

examples/workload_metadata_config/variables.tf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ variable "ip_range_services" {
4848
description = "The secondary ip range to use for pods"
4949
}
5050

51-
variable "compute_engine_service_account" {
52-
description = "Service account to associate to the nodes in the cluster"
51+
variable "registry_project_id" {
52+
description = "Project name for the GCR registry"
5353
}
54-

modules/beta-private-cluster/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
190190
| project\_id | The project ID to host the cluster in (required) | string | n/a | yes |
191191
| region | The region to host the cluster in (required) | string | n/a | yes |
192192
| regional | Whether is a regional cluster (zonal cluster if set false. WARNING: changing this after cluster creation is destructive!) | bool | `"true"` | no |
193+
| registry\_project\_id | Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project. | string | `""` | no |
193194
| remove\_default\_node\_pool | Remove default node pool while setting up the cluster | bool | `"false"` | no |
194195
| resource\_usage\_export\_dataset\_id | The dataset id for which network egress metering for this cluster will be enabled. If enabled, a daemonset will be created in the cluster to meter network egress traffic. | string | `""` | no |
195196
| sandbox\_enabled | (Beta) Enable GKE Sandbox (Do not forget to set `image_type` = `COS_CONTAINERD` and `node_version` = `1.12.7-gke.17` or later to use it). | bool | `"false"` | no |
@@ -258,6 +259,9 @@ following project roles:
258259
- roles/iam.serviceAccountUser
259260
- roles/resourcemanager.projectIamAdmin (only required if `service_account` is set to `create`)
260261

262+
Additionally, if `service_account` is set to `create` and `grant_registry_access` is requested, the service account requires the following role on the `registry_project_id` project:
263+
- roles/resourcemanager.projectIamAdmin
264+
261265
### Enable APIs
262266
In order to operate with the Service Account you must activate the following APIs on the project where the Service Account was created:
263267

modules/beta-private-cluster/sa.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ resource "google_project_iam_member" "cluster_service_account-monitoring_viewer"
6464

6565
resource "google_project_iam_member" "cluster_service_account-gcr" {
6666
count = var.create_service_account && var.grant_registry_access ? 1 : 0
67-
project = var.project_id
67+
project = var.registry_project_id == "" ? var.project_id : var.registry_project_id
6868
role = "roles/storage.objectViewer"
6969
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
7070
}

modules/beta-private-cluster/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ variable "grant_registry_access" {
267267
default = false
268268
}
269269

270+
variable "registry_project_id" {
271+
type = string
272+
description = "Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project."
273+
default = ""
274+
}
275+
270276
variable "service_account" {
271277
type = string
272278
description = "The service account to run nodes as if not overridden in `node_pools`. The create_service_account variable default value (true) will cause a cluster-specific service account to be created."

modules/beta-public-cluster/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
181181
| project\_id | The project ID to host the cluster in (required) | string | n/a | yes |
182182
| region | The region to host the cluster in (required) | string | n/a | yes |
183183
| regional | Whether is a regional cluster (zonal cluster if set false. WARNING: changing this after cluster creation is destructive!) | bool | `"true"` | no |
184+
| registry\_project\_id | Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project. | string | `""` | no |
184185
| remove\_default\_node\_pool | Remove default node pool while setting up the cluster | bool | `"false"` | no |
185186
| resource\_usage\_export\_dataset\_id | The dataset id for which network egress metering for this cluster will be enabled. If enabled, a daemonset will be created in the cluster to meter network egress traffic. | string | `""` | no |
186187
| sandbox\_enabled | (Beta) Enable GKE Sandbox (Do not forget to set `image_type` = `COS_CONTAINERD` and `node_version` = `1.12.7-gke.17` or later to use it). | bool | `"false"` | no |
@@ -249,6 +250,9 @@ following project roles:
249250
- roles/iam.serviceAccountUser
250251
- roles/resourcemanager.projectIamAdmin (only required if `service_account` is set to `create`)
251252

253+
Additionally, if `service_account` is set to `create` and `grant_registry_access` is requested, the service account requires the following role on the `registry_project_id` project:
254+
- roles/resourcemanager.projectIamAdmin
255+
252256
### Enable APIs
253257
In order to operate with the Service Account you must activate the following APIs on the project where the Service Account was created:
254258

modules/beta-public-cluster/sa.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ resource "google_project_iam_member" "cluster_service_account-monitoring_viewer"
6464

6565
resource "google_project_iam_member" "cluster_service_account-gcr" {
6666
count = var.create_service_account && var.grant_registry_access ? 1 : 0
67-
project = var.project_id
67+
project = var.registry_project_id == "" ? var.project_id : var.registry_project_id
6868
role = "roles/storage.objectViewer"
6969
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
7070
}

modules/beta-public-cluster/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ variable "grant_registry_access" {
267267
default = false
268268
}
269269

270+
variable "registry_project_id" {
271+
type = string
272+
description = "Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project."
273+
default = ""
274+
}
275+
270276
variable "service_account" {
271277
type = string
272278
description = "The service account to run nodes as if not overridden in `node_pools`. The create_service_account variable default value (true) will cause a cluster-specific service account to be created."

modules/private-cluster/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
176176
| project\_id | The project ID to host the cluster in (required) | string | n/a | yes |
177177
| region | The region to host the cluster in (required) | string | n/a | yes |
178178
| regional | Whether is a regional cluster (zonal cluster if set false. WARNING: changing this after cluster creation is destructive!) | bool | `"true"` | no |
179+
| registry\_project\_id | Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project. | string | `""` | no |
179180
| remove\_default\_node\_pool | Remove default node pool while setting up the cluster | bool | `"false"` | no |
180181
| service\_account | The service account to run nodes as if not overridden in `node_pools`. The create_service_account variable default value (true) will cause a cluster-specific service account to be created. | string | `""` | no |
181182
| stub\_domains | Map of stub domains and their resolvers to forward DNS queries for a certain domain to an external DNS server | map(list(string)) | `<map>` | no |
@@ -237,6 +238,9 @@ following project roles:
237238
- roles/iam.serviceAccountUser
238239
- roles/resourcemanager.projectIamAdmin (only required if `service_account` is set to `create`)
239240

241+
Additionally, if `service_account` is set to `create` and `grant_registry_access` is requested, the service account requires the following role on the `registry_project_id` project:
242+
- roles/resourcemanager.projectIamAdmin
243+
240244
### Enable APIs
241245
In order to operate with the Service Account you must activate the following APIs on the project where the Service Account was created:
242246

modules/private-cluster/sa.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ resource "google_project_iam_member" "cluster_service_account-monitoring_viewer"
6464

6565
resource "google_project_iam_member" "cluster_service_account-gcr" {
6666
count = var.create_service_account && var.grant_registry_access ? 1 : 0
67-
project = var.project_id
67+
project = var.registry_project_id == "" ? var.project_id : var.registry_project_id
6868
role = "roles/storage.objectViewer"
6969
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
7070
}

modules/private-cluster/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ variable "grant_registry_access" {
257257
default = false
258258
}
259259

260+
variable "registry_project_id" {
261+
type = string
262+
description = "Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project."
263+
default = ""
264+
}
265+
260266
variable "service_account" {
261267
type = string
262268
description = "The service account to run nodes as if not overridden in `node_pools`. The create_service_account variable default value (true) will cause a cluster-specific service account to be created."

sa.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ resource "google_project_iam_member" "cluster_service_account-monitoring_viewer"
6464

6565
resource "google_project_iam_member" "cluster_service_account-gcr" {
6666
count = var.create_service_account && var.grant_registry_access ? 1 : 0
67-
project = var.project_id
67+
project = var.registry_project_id == "" ? var.project_id : var.registry_project_id
6868
role = "roles/storage.objectViewer"
6969
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
7070
}

test/fixtures/shared/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,6 @@ output "service_account" {
7979
value = module.example.service_account
8080
}
8181

82+
output "registry_project_id" {
83+
value = var.registry_project_id
84+
}

test/fixtures/shared/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ variable "compute_engine_service_account" {
3333
description = "The email address of the service account to associate with the GKE cluster"
3434
}
3535

36+
variable "registry_project_id" {
37+
description = "Project to use for granting access to the GCR registry, if requested"
38+
}
39+

test/fixtures/workload_metadata_config/example.tf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@
1717
module "example" {
1818
source = "../../../examples/workload_metadata_config"
1919

20+
<<<<<<< HEAD
21+
<<<<<<< HEAD
22+
=======
23+
>>>>>>> 5258f89... Removing a few conflicting files.
24+
project_id = var.project_id
25+
cluster_name_suffix = "-${random_string.suffix.result}"
26+
region = var.region
27+
zones = slice(var.zones, 0, 1)
28+
network = google_compute_network.main.name
29+
subnetwork = google_compute_subnetwork.main.name
30+
ip_range_pods = google_compute_subnetwork.main.secondary_ip_range[0].range_name
31+
ip_range_services = google_compute_subnetwork.main.secondary_ip_range[1].range_name
32+
<<<<<<< HEAD
33+
=======
2034
project_id = var.project_id
2135
cluster_name_suffix = "-${random_string.suffix.result}"
2236
region = var.region
@@ -25,5 +39,8 @@ module "example" {
2539
subnetwork = google_compute_subnetwork.main.name
2640
ip_range_pods = google_compute_subnetwork.main.secondary_ip_range[0].range_name
2741
ip_range_services = google_compute_subnetwork.main.secondary_ip_range[1].range_name
28-
compute_engine_service_account = var.compute_engine_service_account
42+
>>>>>>> d791335... Removed the custom test for create_service_account
43+
=======
44+
>>>>>>> 5258f89... Removing a few conflicting files.
45+
registry_project_id = var.registry_project_id
2946
}

test/integration/workload_metadata_config/controls/gcloud.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
# limitations under the License.
1414

1515
project_id = attribute('project_id')
16+
registry_project_id = attribute('registry_project_id')
1617
location = attribute('location')
1718
cluster_name = attribute('cluster_name')
19+
service_account = attribute('service_account')
1820

1921
control "gcloud" do
2022
title "Google Compute Engine GKE configuration"
@@ -55,4 +57,20 @@
5557
end
5658
end
5759
end
60+
61+
describe command("gcloud projects get-iam-policy #{registry_project_id} --format=json") do
62+
its(:exit_status) { should eq 0 }
63+
its(:stderr) { should eq '' }
64+
65+
let!(:iam) do
66+
if subject.exit_status == 0
67+
JSON.parse(subject.stdout)
68+
else
69+
{}
70+
end
71+
end
72+
it "has expected registry roles" do
73+
expect(iam['bindings']).to include("members" => ["serviceAccount:#{service_account}"], "role" => "roles/storage.objectViewer")
74+
end
75+
end
5876
end

test/integration/workload_metadata_config/inspec.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ attributes:
99
- name: project_id
1010
required: true
1111
type: string
12+
- name: service_account
13+
required: true
14+
type: string
15+
- name: registry_project_id
16+
required: false
17+
type: string

test/setup/make_source.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ echo "#!/usr/bin/env bash" > ../source.sh
1919
project_id=$(terraform output project_id)
2020
echo "export TF_VAR_project_id='$project_id'" >> ../source.sh
2121

22+
# We use the same project for registry project in the tests.
23+
echo "export TF_VAR_registry_project_id='$project_id'" >> ../source.sh
24+
2225
sa_json=$(terraform output sa_key)
2326
# shellcheck disable=SC2086
2427
echo "export SERVICE_ACCOUNT_JSON='$(echo $sa_json | base64 --decode)'" >> ../source.sh

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ variable "grant_registry_access" {
257257
default = false
258258
}
259259

260+
variable "registry_project_id" {
261+
type = string
262+
description = "Project holding the Google Container Registry. If empty, we use the cluster project. If grant_registry_access is true, storage.objectViewer role is assigned on this project."
263+
default = ""
264+
}
265+
260266
variable "service_account" {
261267
type = string
262268
description = "The service account to run nodes as if not overridden in `node_pools`. The create_service_account variable default value (true) will cause a cluster-specific service account to be created."

0 commit comments

Comments
 (0)