-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(google_deployment_accounts): support circleci oidc * chore(google_deployment_accounts): add a fixme for 2024 * feat(google_deployment_accounts): add opinionated circleci interface * feat(google_deployment_accounts): allow overriding display name * chore(google_deployment_accounts): update docs * chore(google_deployment_accounts): add examples * chore(google_deployment_accounts): fix up docs and interface * chore(google_deployment_accounts): fixups post rebase
- Loading branch information
Showing
7 changed files
with
200 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Allow OIDC access from CircleCI jobs triggered in a specific repo | ||
data "terraform_remote_state" "wip_project" { | ||
backend = "gcs" | ||
|
||
config = { | ||
bucket = "my-wip-project" | ||
prefix = "wip-project/prefix" | ||
} | ||
} | ||
|
||
module "google_deployment_accounts" { | ||
source = "github.com/mozilla/terraform-modules//google_deployment_accounts?ref=main" | ||
project = "my-project" | ||
environment = "stage" | ||
github_repository = "org/project" | ||
wip_name = "circleci" | ||
wip_project_number = data.terraform_remote_state.wip_project.number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Allow OIDC access from CircleCI jobs triggered on the main branch only of a | ||
# specific repo | ||
data "terraform_remote_state" "wip_project" { | ||
backend = "gcs" | ||
|
||
config = { | ||
bucket = "my-wip-project" | ||
prefix = "wip-project/prefix" | ||
} | ||
} | ||
|
||
module "google_deployment_accounts" { | ||
source = "github.com/mozilla/terraform-modules//google_deployment_accounts?ref=main" | ||
project = "my-project" | ||
environment = "stage" | ||
github_repository = "org/project" | ||
wip_name = "circleci" | ||
wip_project_number = data.terraform_remote_state.wip_project.number | ||
circleci_branches = ["main"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# A more complex example using attribute specifiers directly. Allow OIDC access | ||
# from CircleCI jobs triggered on the main branch of org/repo1 and org/repo2, | ||
# as well as any job using the fake-context context | ||
data "terraform_remote_state" "wip_project" { | ||
backend = "gcs" | ||
|
||
config = { | ||
bucket = "my-wip-project" | ||
prefix = "wip-project/prefix" | ||
} | ||
} | ||
|
||
locals { | ||
allowed_repos = formatlist("attribute.vcs/github.com/org/%s:/refs/heads/main", ["repo1", "repo2"]) | ||
allowed_contexts = formatlist("attribute.context_id/%s", | ||
one(values({ "fake-context" = "6e1515f7-40f0-4063-a74a-d77d22ee9f7e" } | ||
))) | ||
} | ||
|
||
module "google_deployment_accounts" { | ||
source = "github.com/mozilla/terraform-modules//google_deployment_accounts?ref=main" | ||
project = "my-project" | ||
environment = "prod" | ||
wip_name = "circleci" | ||
wip_project_number = data.terraform_remote_state.wip_project.number | ||
circleci_attribute_specifiers = setunion( | ||
local.allowed_repos, | ||
local.allowed_contexts, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
data "terraform_remote_state" "wip_project" { | ||
backend = "gcs" | ||
|
||
config = { | ||
bucket = "my-wip-project" | ||
prefix = "wip-project/prefix" | ||
} | ||
} | ||
|
||
module "google_deployment_accounts" { | ||
source = "github.com/mozilla/terraform-modules//google_deployment_accounts?ref=main" | ||
project = "my-project" | ||
environment = "stage" | ||
github_repository = "org/project" | ||
wip_name = "github-actions" | ||
wip_project_number = data.terraform_remote_state.wip_project.number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,57 @@ | ||
/** | ||
* # Terraform Module: Service Accounts for deployment from GitHub Actions | ||
* Creates a Cloud IAM service accounts which let GitHub Actions workflows authenticate to GKE. | ||
* # Terraform Module: Service Accounts for deployment from GitHub Actions and CircleCI | ||
* Creates a Cloud IAM service account which lets CI workflows authenticate to GCP. | ||
*/ | ||
|
||
locals { | ||
gha_count = var.wip_name == "github-actions" ? 1 : 0 | ||
circleci_count = var.wip_name == "circleci" ? 1 : 0 | ||
} | ||
|
||
resource "google_service_account" "account" { | ||
account_id = coalesce(var.account_id, "deploy-${var.environment}") | ||
display_name = "Deployment to the ${var.environment} environment" | ||
display_name = coalesce(var.display_name, "Deployment to the ${var.environment} environment") | ||
project = var.project | ||
} | ||
|
||
resource "google_service_account_iam_binding" "github-actions-access" { | ||
count = local.gha_count | ||
service_account_id = google_service_account.account.name | ||
role = "roles/iam.workloadIdentityUser" | ||
members = local.github_deploy_members | ||
} | ||
|
||
locals { | ||
circleci = var.wip_name == "circleci" | ||
# explicit attributes replace all other kinds of assertions | ||
circleci_attribute_assertions = local.circleci ? [for attribute_specifier in var.circleci_attribute_specifiers : | ||
"principalSet://iam.googleapis.com/projects/${var.wip_project_number}/locations/global/workloadIdentityPools/${var.wip_name}/${attribute_specifier}" | ||
] : [] | ||
# single repo, all branches | ||
circleci_vcs_origin_assertions = local.circleci && var.github_repository != null && length(var.circleci_branches) == 0 ? ["principalSet://iam.googleapis.com/projects/${var.wip_project_number}/locations/global/workloadIdentityPools/${var.wip_name}/attribute.vcs_origin/github.com/${var.github_repository}", | ||
] : [] | ||
# single repo, specific branches | ||
circleci_vcs_assertions = var.wip_name == "circleci" && var.github_repository != null && length(var.circleci_branches) > 0 ? [ | ||
for branch in var.circleci_branches : | ||
"principalSet://iam.googleapis.com/projects/${var.wip_project_number}/locations/global/workloadIdentityPools/${var.wip_name}/attribute.vcs/github.com/${var.github_repository}:refs/heads/${branch}" | ||
] : [] | ||
# specific CircleCI Context | ||
circleci_context_id_assertions = local.circleci && length(var.circleci_context_ids) > 0 ? [ | ||
for context in var.circleci_context_ids : | ||
"principalSet://iam.googleapis.com/projects/${var.wip_project_number}/locations/global/workloadIdentityPools/${var.wip_name}/attribute.context_id/${context}" | ||
] : [] | ||
} | ||
|
||
resource "google_service_account_iam_binding" "circleci-access" { | ||
count = local.circleci_count | ||
service_account_id = google_service_account.account.name | ||
role = "roles/iam.workloadIdentityUser" | ||
# test value generated via GUI, assertions should look something like: | ||
# "principalSet://iam.googleapis.com/projects/12141114016/locations/global/workloadIdentityPools/circleci-2/attribute.aud/c3874144-7d38-44e8-8b38-f6b8778a4eb0" | ||
members = length(local.circleci_attribute_assertions) > 0 ? local.circleci_attribute_assertions : setunion( | ||
local.circleci_attribute_assertions, | ||
local.circleci_vcs_origin_assertions, | ||
local.circleci_vcs_assertions, | ||
local.circleci_context_id_assertions, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters