Skip to content

Commit 126a61c

Browse files
chore: add exclusivity condition on project_org_id and project_folder_id (#5)
* reformat * chore: add exclusivity condition on project_org_id and project_folder_id
1 parent 3564c4c commit 126a61c

File tree

4 files changed

+89
-84
lines changed

4 files changed

+89
-84
lines changed

main.tf

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
11
locals {
2-
stacklet_assumed_role = "arn:aws:sts::${var.stacklet_aws_account_id}:assumed-role/${var.stacklet_aws_role_name}"
2+
stacklet_assumed_role = "arn:aws:sts::${var.stacklet_aws_account_id}:assumed-role/${var.stacklet_aws_role_name}"
33

4-
source_tables = [for key in var.billing_tables : {
5-
"key" : key,
6-
"project_id" : split(".", key)[0],
7-
"dataset_id" : split(".", key)[1],
8-
"table_id" : split(".", key)[2],
9-
}]
4+
source_tables = [for key in var.billing_tables : {
5+
"key" : key,
6+
"project_id" : split(".", key)[0],
7+
"dataset_id" : split(".", key)[1],
8+
"table_id" : split(".", key)[2],
9+
}]
1010
}
1111

1212

1313
# A project for all the resources to live in, and the APIs it needs activated.
1414
resource "google_project" "billing_export" {
15-
name = "Stacklet billing export"
16-
project_id = var.project_id
17-
org_id = var.project_org_id
18-
folder_id = var.project_folder_id
19-
billing_account = var.project_billing_account_id
15+
name = "Stacklet billing export"
16+
project_id = var.project_id
17+
org_id = var.project_org_id
18+
folder_id = var.project_folder_id
19+
billing_account = var.project_billing_account_id
2020
}
2121
resource "google_project_service" "iamcredentials" {
22-
project = google_project.billing_export.project_id
23-
service = "iamcredentials.googleapis.com"
22+
project = google_project.billing_export.project_id
23+
service = "iamcredentials.googleapis.com"
2424
}
2525
resource "google_project_service" "bigquery" {
26-
project = google_project.billing_export.project_id
27-
service = "bigquery.googleapis.com"
26+
project = google_project.billing_export.project_id
27+
service = "bigquery.googleapis.com"
2828
}
2929

3030

3131
# Allow AWS roles from the Stacklet account to assume identities in GCP.
3232
resource "google_iam_workload_identity_pool" "stacklet_access" {
33-
project = google_project.billing_export.project_id
34-
workload_identity_pool_id = "stacklet-access"
35-
display_name = "Stacklet billing export"
33+
project = google_project.billing_export.project_id
34+
workload_identity_pool_id = "stacklet-access"
35+
display_name = "Stacklet billing export"
3636
}
3737
resource "google_iam_workload_identity_pool_provider" "stacklet_account" {
38-
project = google_project.billing_export.project_id
39-
workload_identity_pool_id = google_iam_workload_identity_pool.stacklet_access.workload_identity_pool_id
40-
workload_identity_pool_provider_id = "stacklet-account"
41-
display_name = "Stacklet FOCUS export"
42-
disabled = false
38+
project = google_project.billing_export.project_id
39+
workload_identity_pool_id = google_iam_workload_identity_pool.stacklet_access.workload_identity_pool_id
40+
workload_identity_pool_provider_id = "stacklet-account"
41+
display_name = "Stacklet FOCUS export"
42+
disabled = false
4343

44-
# The default attribute mapping for AWS sets `aws_role` attribute which matches the
45-
# `local.stacklet_assumed_role` as used in the service account IAM policy.
46-
aws {
47-
account_id = var.stacklet_aws_account_id
48-
}
44+
# The default attribute mapping for AWS sets `aws_role` attribute which matches the
45+
# `local.stacklet_assumed_role` as used in the service account IAM policy.
46+
aws {
47+
account_id = var.stacklet_aws_account_id
48+
}
4949
}
5050

5151

5252
# Service account, which can be impersonated by `local.stacklet_assumed_role`.
5353
resource "google_service_account" "billing_access" {
54-
project = google_project.billing_export.project_id
55-
account_id = "stacklet-billing-access"
56-
display_name = "Stacklet WIF billing access"
54+
project = google_project.billing_export.project_id
55+
account_id = "stacklet-billing-access"
56+
display_name = "Stacklet WIF billing access"
5757
}
5858
data "google_iam_policy" "stacklet_role_access" {
59-
binding {
60-
role = "roles/iam.serviceAccountTokenCreator"
61-
members = ["principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.stacklet_access.name}/attribute.aws_role/${local.stacklet_assumed_role}"]
62-
}
59+
binding {
60+
role = "roles/iam.serviceAccountTokenCreator"
61+
members = ["principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.stacklet_access.name}/attribute.aws_role/${local.stacklet_assumed_role}"]
62+
}
6363
}
6464
resource "google_service_account_iam_policy" "billing_access" {
6565
service_account_id = google_service_account.billing_access.name
@@ -74,20 +74,20 @@ resource "google_project_iam_member" "sa_bq_jobs" {
7474
member = google_service_account.billing_access.member
7575
}
7676
resource "google_bigquery_table_iam_member" "sa_bq_tables" {
77-
for_each = { for table in local.source_tables : table.key => table }
77+
for_each = { for table in local.source_tables : table.key => table }
7878

79-
project = each.value.project_id
80-
dataset_id = each.value.dataset_id
81-
table_id = each.value.table_id
82-
role = "roles/bigquery.dataViewer"
83-
member = google_service_account.billing_access.member
79+
project = each.value.project_id
80+
dataset_id = each.value.dataset_id
81+
table_id = each.value.table_id
82+
role = "roles/bigquery.dataViewer"
83+
member = google_service_account.billing_access.member
8484
}
8585

8686

8787
# Discover dataset locations for output.
8888
data "google_bigquery_dataset" "table_datasets" {
89-
for_each = { for table in local.source_tables : table.key => table }
89+
for_each = { for table in local.source_tables : table.key => table }
9090

91-
project = each.value.project_id
92-
dataset_id = each.value.dataset_id
91+
project = each.value.project_id
92+
dataset_id = each.value.dataset_id
9393
}

output.tf

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
locals {
2-
project_id = google_project.billing_export.project_id
3-
table_locations = [ for key in var.billing_tables : { table = key, location = data.google_bigquery_dataset.table_datasets[key].location }]
4-
wif_audience = "//iam.googleapis.com/projects/${google_project.billing_export.number}/locations/global/workloadIdentityPools/${google_iam_workload_identity_pool.stacklet_access.workload_identity_pool_id}/providers/${google_iam_workload_identity_pool_provider.stacklet_account.workload_identity_pool_provider_id}"
5-
wif_impersonation_url = "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/${google_service_account.billing_access.email}:generateAccessToken"
2+
project_id = google_project.billing_export.project_id
3+
table_locations = [for key in var.billing_tables : { table = key, location = data.google_bigquery_dataset.table_datasets[key].location }]
4+
wif_audience = "//iam.googleapis.com/projects/${google_project.billing_export.number}/locations/global/workloadIdentityPools/${google_iam_workload_identity_pool.stacklet_access.workload_identity_pool_id}/providers/${google_iam_workload_identity_pool_provider.stacklet_account.workload_identity_pool_provider_id}"
5+
wif_impersonation_url = "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/${google_service_account.billing_access.email}:generateAccessToken"
66
}
77

88
output "project_id" {
9-
value = local.project_id
9+
value = local.project_id
1010
}
1111

1212
output "table_locations" {
13-
value = local.table_locations
13+
value = local.table_locations
1414
}
1515

1616
output "wif_audience" {
17-
value = local.wif_audience
17+
value = local.wif_audience
1818
}
1919

2020
output "wif_impersonation_url" {
21-
value = local.wif_impersonation_url
21+
value = local.wif_impersonation_url
2222
}
2323

2424
output "access_blob" {
25-
value = base64encode(jsonencode({
26-
projectId = local.project_id,
27-
tableLocations = local.table_locations,
28-
wifAudience = local.wif_audience,
29-
wifImpersonationURL = local.wif_impersonation_url,
30-
}))
25+
value = base64encode(jsonencode({
26+
projectId = local.project_id,
27+
tableLocations = local.table_locations,
28+
wifAudience = local.wif_audience,
29+
wifImpersonationURL = local.wif_impersonation_url,
30+
}))
3131
}

provider.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
provider "google" {
2-
default_labels = var.resource_labels
2+
default_labels = var.resource_labels
33
}

vars.tf

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
11
variable "resource_labels" {
2-
type = map(string)
3-
default = {}
4-
description = "Labels to apply to the project and applicable resources"
2+
type = map(string)
3+
default = {}
4+
description = "Labels to apply to the project and applicable resources"
55
}
66

77
variable "project_id" {
8-
type = string
9-
description = "ID of project to hold all resources"
8+
type = string
9+
description = "ID of project to hold all resources"
1010
}
1111

1212
variable "project_org_id" {
13-
type = string
14-
default = null
15-
description = "Where to create the project (optional, exclusive of project_folder_id)"
13+
type = string
14+
default = null
15+
description = "Where to create the project (optional, exclusive of project_folder_id)"
1616
}
1717

1818
variable "project_folder_id" {
19-
type = string
20-
default = null
21-
description = "Where to create the project (optional, exclusive of project_org_id)"
19+
type = string
20+
default = null
21+
description = "Where to create the project (optional, exclusive of project_org_id)"
22+
23+
validation {
24+
condition = var.project_org_id == null || var.project_folder_id == null
25+
error_message = "project_org_id and project_folder_id are exclusive"
26+
}
2227
}
2328

2429
variable "project_billing_account_id" {
25-
type = string
26-
default = null
27-
description = "Billing account responsible for any costs incurred"
30+
type = string
31+
default = null
32+
description = "Billing account responsible for any costs incurred"
2833
}
2934

3035
variable "billing_tables" {
31-
type = list(string)
32-
description = "Billing export tables in <project_id>.<dataset_id>.<table_id> format."
33-
validation {
34-
condition = alltrue([for t in var.billing_tables : length(split(".", t)) == 3])
35-
error_message = "All tables must be <project_id>.<dataset_id>.<table_id>"
36-
}
36+
type = list(string)
37+
description = "Billing export tables in <project_id>.<dataset_id>.<table_id> format."
38+
validation {
39+
condition = alltrue([for t in var.billing_tables : length(split(".", t)) == 3])
40+
error_message = "All tables must be <project_id>.<dataset_id>.<table_id>"
41+
}
3742
}
3843

3944
variable "stacklet_aws_account_id" {
40-
type = string
41-
description = "AWS account which will use WIF to query billing data (chosen by Stacklet)"
45+
type = string
46+
description = "AWS account which will use WIF to query billing data (chosen by Stacklet)"
4247
}
4348

4449
variable "stacklet_aws_role_name" {
45-
type = string
46-
description = "AWS IAM role which will use WIF to query billing data (chosen by Stacklet)"
50+
type = string
51+
description = "AWS IAM role which will use WIF to query billing data (chosen by Stacklet)"
4752
}

0 commit comments

Comments
 (0)