Skip to content

Commit 81f1000

Browse files
authored
Fix service agent context interpolation in project factory sub-resources (GoogleCloudPlatform#4095)
* Fix service agent context interpolation in project factory sub-resources * Address review: fix example agent refs, aspect-types/taxonomies _self_ agents, per-path test guards
1 parent d737932 commit 81f1000

9 files changed

Lines changed: 56 additions & 12 deletions

File tree

modules/project-factory/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ Assuming keys of the form `my_folder`, `my_project`, `my_sa`, etc. this is an ex
281281
- `$folder_ids:my_folder`
282282
- `$iam_principals:my_principal`
283283
- `$iam_principals:service_accounts/my_project/my_sa`
284-
- `$iam_principals:service_agents/_self_/my_api`
285-
- `$iam_principals:service_agents/my_project/my_api`
284+
- `$iam_principals:service_agents/_self_/my_api` *only resolves for agents whose API is enabled in the project*
285+
- `$iam_principals:service_agents/my_project/my_api` *only resolves for agents whose API is enabled in the target project*
286286
- `$iam_principalsets:service_accounts/all`
287287
- `$kms_keys:my_key`
288288
- `$log_buckets:my_project/my_bucket`
@@ -664,7 +664,7 @@ service_accounts:
664664
roles/iam.serviceAccountUser:
665665
- $iam_principals:service_accounts/_self_/app-0-fe
666666
- $iam_principals:service_agents/_self_/compute
667-
- $iam_principals:service_agents/dev-tb-app0-0/compute
667+
- $iam_principals:service_agents/dev-tb-app0-0/storage
668668
iam_bindings_additive:
669669
test:
670670
role: roles/iam.serviceAccountUser
@@ -694,6 +694,11 @@ billing_budgets:
694694
buckets:
695695
app-0-bucket-a:
696696
location: europe-west8
697+
iam:
698+
roles/storage.objectViewer:
699+
- $iam_principals:service_agents/_self_/compute
700+
roles/storage.legacyObjectReader:
701+
- $iam_principals:service_agents/dev-tb-app0-0/storage
697702
tag_bindings:
698703
context: $tag_values:context/gke
699704
app-0-bucket-b:
@@ -730,6 +735,8 @@ pubsub_topics:
730735
iam:
731736
roles/pubsub.subscriber:
732737
- group:team-a-admins@example.org
738+
roles/pubsub.viewer:
739+
- $iam_principals:service_agents/_self_/pubsub
733740
app-0-topic-b:
734741
subscriptions:
735742
app-0-topic-b-sub: {}

modules/project-factory/aspect-types.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ module "aspect-types" {
3333
context = merge(local.ctx, {
3434
iam_principals = merge(
3535
local.ctx_iam_principals,
36-
lookup(local.self_sas_iam_emails, each.key, {}),
37-
local.projects_service_agents
36+
local.projects_service_agents,
37+
lookup(local.per_project_service_agents, each.key, {}),
38+
lookup(local.self_sas_iam_emails, each.key, {})
3839
)
3940
project_ids = merge(
4041
local.ctx.project_ids,

modules/project-factory/projects-bigquery.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ module "bigquery-datasets" {
5959
local.ctx.iam_principals,
6060
local.projects_sas_iam_emails,
6161
local.automation_sas_iam_emails,
62+
local.projects_service_agents,
63+
lookup(local.per_project_service_agents, each.value.project_key, {}),
6264
lookup(local.self_sas_iam_emails, each.value.project_key, {})
6365
)
6466
kms_keys = merge(local.ctx.kms_keys, local.kms_keys, local.kms_autokeys)

modules/project-factory/projects-buckets.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ module "buckets" {
8181
local.ctx.iam_principals,
8282
local.projects_sas_iam_emails,
8383
local.automation_sas_iam_emails,
84+
local.projects_service_agents,
85+
lookup(local.per_project_service_agents, each.value.project_key, {}),
8486
lookup(local.self_sas_iam_emails, each.value.project_key, {})
8587
)
8688
kms_keys = merge(local.ctx.kms_keys, local.kms_keys, local.kms_autokeys)

modules/project-factory/projects-kms.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ module "kms" {
7676
local.ctx.iam_principals,
7777
local.projects_sas_iam_emails,
7878
local.automation_sas_iam_emails,
79-
lookup(local.self_sas_iam_emails, each.value.project_key, {}),
80-
local.projects_service_agents
79+
local.projects_service_agents,
80+
lookup(local.per_project_service_agents, each.value.project_key, {}),
81+
lookup(local.self_sas_iam_emails, each.value.project_key, {})
8182
)
8283
locations = local.ctx.locations
8384
project_ids = local.ctx_project_ids

modules/project-factory/projects-log-buckets.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ module "log-buckets" {
5151
local.ctx.iam_principals,
5252
local.projects_sas_iam_emails,
5353
local.automation_sas_iam_emails,
54+
local.projects_service_agents,
55+
lookup(local.per_project_service_agents, each.value.project_key, {}),
5456
lookup(local.self_sas_iam_emails, each.value.project_key, {})
5557
)
5658
kms_keys = merge(local.ctx.kms_keys, local.kms_keys, local.kms_autokeys)

modules/project-factory/projects-pubsub.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ module "pubsub" {
4747
local.ctx.iam_principals,
4848
local.projects_sas_iam_emails,
4949
local.automation_sas_iam_emails,
50+
local.projects_service_agents,
51+
lookup(local.per_project_service_agents, each.value.project_key, {}),
5052
lookup(local.self_sas_iam_emails, each.value.project_key, {})
5153
)
5254
kms_keys = merge(local.ctx.kms_keys, local.kms_keys, local.kms_autokeys)

modules/project-factory/taxonomies.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ module "taxonomies" {
3535
context = merge(local.ctx, {
3636
iam_principals = merge(
3737
local.ctx_iam_principals,
38-
lookup(local.self_sas_iam_emails, each.key, {}),
39-
local.projects_service_agents
38+
local.projects_service_agents,
39+
lookup(local.per_project_service_agents, each.key, {}),
40+
lookup(local.self_sas_iam_emails, each.key, {})
4041
)
4142
project_ids = merge(
4243
local.ctx.project_ids,

tests/modules/project_factory/examples/example.yaml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ values:
197197
uniform_bucket_level_access: true
198198
versioning:
199199
- enabled: false
200+
? module.project-factory.module.buckets["dev-ta-app0-be/app-0-bucket-a"].google_storage_bucket_iam_binding.authoritative["roles/storage.legacyObjectReader"]
201+
: bucket: test-pf-dev-ta-app0-be-app-0-bucket-a
202+
condition: []
203+
# members must be unknown at plan time: the cross-project service agent
204+
# reference resolves to an email embedding the project number, while an
205+
# unresolved reference would be passed through as a known literal
206+
members: __missing__
207+
role: roles/storage.legacyObjectReader
208+
timeouts: null
209+
? module.project-factory.module.buckets["dev-ta-app0-be/app-0-bucket-a"].google_storage_bucket_iam_binding.authoritative["roles/storage.objectViewer"]
210+
: bucket: test-pf-dev-ta-app0-be-app-0-bucket-a
211+
condition: []
212+
# members must be unknown at plan time: the _self_ service agent
213+
# reference resolves to an email embedding the project number, while an
214+
# unresolved reference would be passed through as a known literal
215+
members: __missing__
216+
role: roles/storage.objectViewer
217+
timeouts: null
200218
module.project-factory.module.buckets["dev-ta-app0-be/app-0-bucket-a"].google_tags_location_tag_binding.binding["context"]:
201219
deletion_policy: DELETE
202220
location: europe-west8
@@ -870,6 +888,14 @@ values:
870888
- group:team-a-admins@example.org
871889
project: test-pf-dev-ta-app0-be
872890
role: roles/pubsub.subscriber
891+
? module.project-factory.module.pubsub["dev-ta-app0-be/app-0-topic-a"].google_pubsub_topic_iam_binding.authoritative["roles/pubsub.viewer"]
892+
: condition: []
893+
# members must be unknown at plan time: the _self_ service agent
894+
# reference resolves to an email embedding the project number, while an
895+
# unresolved reference would be passed through as a known literal
896+
members: __missing__
897+
project: test-pf-dev-ta-app0-be
898+
role: roles/pubsub.viewer
873899
? module.project-factory.module.pubsub["dev-ta-app0-be/app-0-topic-b"].google_pubsub_subscription.default["app-0-topic-b-sub"]
874900
: bigquery_config: []
875901
cloud_storage_config: []
@@ -1065,20 +1091,20 @@ counts:
10651091
google_project_service_identity: 5
10661092
google_pubsub_subscription: 1
10671093
google_pubsub_topic: 2
1068-
google_pubsub_topic_iam_binding: 1
1094+
google_pubsub_topic_iam_binding: 2
10691095
google_service_account: 6
10701096
google_service_account_iam_binding: 2
10711097
google_service_account_iam_member: 3
10721098
google_storage_bucket: 3
1073-
google_storage_bucket_iam_binding: 2
1099+
google_storage_bucket_iam_binding: 4
10741100
google_storage_project_service_account: 4
10751101
google_tags_location_tag_binding: 2
10761102
google_tags_tag_binding: 3
10771103
google_tags_tag_key: 1
10781104
google_tags_tag_value: 2
10791105
google_tags_tag_value_iam_binding: 1
10801106
modules: 39
1081-
resources: 122
1107+
resources: 125
10821108
terraform_data: 2
10831109

10841110
outputs: {}

0 commit comments

Comments
 (0)