Skip to content

Commit f4aa9b2

Browse files
author
Nils
committed
more attributes
1 parent 387946d commit f4aa9b2

File tree

7 files changed

+90
-29
lines changed

7 files changed

+90
-29
lines changed

README.md

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
This Terraform module creates a Workload Identity Pool and Provider for GitLab.
1010

1111
Service account keys are a security risk if compromised.
12-
Avoid service account keys and instead use the [Workload Identity Federation](https://cloud.google.com/iam/docs/configuring-workload-identity-federation).
12+
Avoid service account keys and instead use the [Workload Identity Federation](https://github.com/Cyclenerd/google-workload-identity-federation#readme).
1313
For more information about Workload Identity Federation and how to best authenticate service accounts on Google Cloud, please see my GitHub repo [Cyclenerd/google-workload-identity-federation](https://github.com/Cyclenerd/google-workload-identity-federation#readme).
1414

1515
> There is also a ready-to-use Terraform module for [GitHub](https://github.com/Cyclenerd/terraform-google-wif-github#readme).
@@ -22,7 +22,7 @@ Create Workload Identity Pool and Provider:
2222
# Create Workload Identity Pool Provider for GitLab
2323
module "gitlab-wif" {
2424
source = "Cyclenerd/wif-gitlab/google"
25-
version = "1.0.0"
25+
version = "~> 1.0.0"
2626
project_id = "your-project-id"
2727
}
2828
@@ -47,7 +47,7 @@ data "google_service_account" "gitlab" {
4747
# Allow service account to login via WIF and only from GitLab repository (project path)
4848
module "gitlab-service-account" {
4949
source = "Cyclenerd/wif-service-account/google"
50-
version = "1.0.0"
50+
version = "~> 1.0.0"
5151
project_id = "your-project-id"
5252
pool_name = module.gitlab-wif.pool_name
5353
account_id = data.google_service_account.gitlab.account_id
@@ -61,28 +61,52 @@ module "gitlab-service-account" {
6161

6262
## OIDC Token Attribute Mapping
6363

64-
Attribute mapping:
65-
66-
| Attribute | Claim |
67-
|------------------------|-------------------------------------------------------|
68-
| `google.subject` | `assertion.sub` |
69-
| `attribute.sub` | `assertion.sub` |
70-
| `attribute.repository` | `assertion.project_path` (not `assertion.repository`) |
71-
| `attribute.user_login` | `assertion.user_login` |
72-
| `attribute.ref` | `assertion.ref` |
64+
> The attributes `attribute.sub` and `attribute.repository` are used in the Terrform module [Cyclenerd/wif-service-account/google](https://github.com/Cyclenerd/terraform-google-wif-service-account).
65+
> Please do not remove these attributes.
66+
67+
Default attribute mapping:
68+
69+
| Attribute | Claim | Description |
70+
|-----------------------------------|-----------------------------------|-------------|
71+
| `google.subject` | `assertion.sub` | Subject
72+
| `attribute.sub` | `assertion.sub` | Defines the subject claim (`project_path:{group}/{project}:ref_type:{type}:ref:{branch_name}`) that is to be validated by the cloud provider. This setting is essential for making sure that access tokens are only allocated in a predictable way.
73+
| `attribute.repository` | `assertion.project_path` | The repository (project path) from where the workflow is running
74+
| `attribute.aud` | `assertion.aud` | Intended audience for the token. Specified in the [ID tokens configuration](https://docs.gitlab.com/ee/ci/yaml/index.html#id_tokens). The domain of the GitLab instance by default.
75+
| `attribute.iss` | `assertion.iss` | Issuer of the token, which is the domain of the GitLab instance.
76+
| `attribute.namespace_id` | `assertion.namespace_id` | Use this to scope to group or user level namespace by ID.
77+
| `attribute.namespace_path` | `assertion.namespace_path` | Use this to scope to group or user level namespace by path.
78+
| `attribute.project_id` | `assertion.project_id` | Use this to scope to project by ID.
79+
| `attribute.project_path` | `assertion.project_path` | Use this to scope to project by path.
80+
| `attribute.user_id` | `assertion.user_id` | ID of the user executing the job.
81+
| `attribute.user_login` | `assertion.user_login` | Username of the user executing the job.
82+
| `attribute.user_email` | `assertion.user_email` | Email of the user executing the job.
83+
| `attribute.pipeline_id` | `assertion.pipeline_id` | ID of the pipeline.
84+
| `attribute.pipeline_source` | `assertion.pipeline_source` | Pipeline source.
85+
| `attribute.job_id` | `assertion.job_id` | ID of the job.
86+
| `attribute.ref` | `assertion.ref` | Git ref for the job.
87+
| `attribute.ref_type` | `assertion.ref_type` | Git ref type, either `branch` or `tag`.
88+
| `attribute.ref_protected` | `assertion.ref_protected` | `true` if the Git ref is protected, `false` otherwise.
89+
| `attribute.environment` | `assertion.environment` | Environment this job deploys to (introduced in GitLab 13.9).
90+
| `attribute.environment_protected` | `assertion.environment_protected` | `true` if deployed environment is protected, `false` otherwise (introduced in GitLab 13.9).
91+
| `attribute.deployment_tier` | `assertion.deployment_tier` | Deployment tier of the environment the job specifies. Introduced in GitLab 15.2.
92+
| `attribute.runner_id` | `assertion.runner_id` | ID of the runner executing the job. Introduced in GitLab 16.0.
93+
| `attribute.runner_environment` | `assertion.runner_environment` | The type of runner used by the job. Can be either `gitlab-hosted` or `self-hosted`. Introduced in GitLab 16.0.
94+
| `attribute.sha` | `assertion.sha` | The commit SHA for the job. Introduced in GitLab 16.0.
7395

7496
<!-- BEGIN_TF_DOCS -->
7597
## Providers
7698

7799
| Name | Version |
78100
|------|---------|
79-
| <a name="provider_google"></a> [google](#provider\_google) | >= 4.61.0 |
101+
| <a name="provider_google"></a> [google](#provider\_google) | 4.62.0 |
80102

81103
## Inputs
82104

83105
| Name | Description | Type | Default | Required |
84106
|------|-------------|------|---------|:--------:|
85107
| <a name="input_allowed_audiences"></a> [allowed\_audiences](#input\_allowed\_audiences) | Workload Identity Pool Provider allowed audiences | `string` | `"https://gitlab.com"` | no |
108+
| <a name="input_attribute_condition"></a> [attribute\_condition](#input\_attribute\_condition) | (Optional) Workload Identity Pool Provider attribute condition expression | `string` | `null` | no |
109+
| <a name="input_attribute_mapping"></a> [attribute\_mapping](#input\_attribute\_mapping) | Workload Identity Pool Provider attribute mapping | `map(string)` | <pre>{<br> "attribute.aud": "attribute.aud",<br> "attribute.deployment_tier": "assertion.deployment_tier",<br> "attribute.environment": "assertion.environment",<br> "attribute.environment_protected": "assertion.environment_protected",<br> "attribute.iss": "attribute.iss",<br> "attribute.job_id": "assertion.job_id",<br> "attribute.namespace_id": "assertion.namespace_id",<br> "attribute.namespace_path": "assertion.namespace_path",<br> "attribute.pipeline_id": "assertion.pipeline_id",<br> "attribute.pipeline_source": "assertion.pipeline_source",<br> "attribute.project_id": "assertion.project_id",<br> "attribute.project_path": "assertion.project_path",<br> "attribute.ref": "assertion.ref",<br> "attribute.ref_protected": "assertion.ref_protected",<br> "attribute.ref_type": "assertion.ref_type",<br> "attribute.repository": "assertion.project_path",<br> "attribute.runner_environment": "assertion.runner_environment",<br> "attribute.runner_id": "assertion.runner_id",<br> "attribute.sha": "assertion.sha",<br> "attribute.sub": "attribute.sub",<br> "attribute.user_email": "assertion.user_email",<br> "attribute.user_id": "assertion.user_id",<br> "attribute.user_login": "assertion.user_login",<br> "google.subject": "assertion.sub"<br>}</pre> | no |
86110
| <a name="input_issuer_uri"></a> [issuer\_uri](#input\_issuer\_uri) | Workload Identity Pool Provider issuer URI | `string` | `"https://gitlab.com"` | no |
87111
| <a name="input_pool_description"></a> [pool\_description](#input\_pool\_description) | Workload Identity Pool description | `string` | `"Workload Identity Pool for GitLab (Terraform managed)"` | no |
88112
| <a name="input_pool_disabled"></a> [pool\_disabled](#input\_pool\_disabled) | Workload Identity Pool disabled | `bool` | `false` | no |
@@ -109,3 +133,5 @@ Attribute mapping:
109133
## License
110134

111135
All files in this repository are under the [Apache License, Version 2.0](LICENSE) unless noted otherwise.
136+
137+
Based on [Terraform module for workload identity federation on GCP](https://github.com/mscribellito/terraform-google-workload-identity-federation) by [Michael S](https://github.com/mscribellito).

examples/gitlab-ci-custom/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ With this example the following steps are executed and configured:
1919
# Create Workload Identity Pool Provider for self-managed GitLab installation
2020
module "gitlab-custom-wif" {
2121
source = "Cyclenerd/wif-gitlab/google"
22-
version = "1.0.0"
22+
version = "~> 1.0.0"
2323
project_id = var.project_id
2424
allowed_audiences = "https://gitlab.example.com"
2525
issuer_uri = "https://gitlab.example.com"
@@ -42,7 +42,7 @@ resource "google_service_account" "gitlab" {
4242
# Allow service account to login via WIF and only from GitLab repository (project path)
4343
module "github-service-account" {
4444
source = "Cyclenerd/wif-service-account/google"
45-
version = "1.0.0"
45+
version = "~> 1.0.0"
4646
project_id = var.project_id
4747
pool_name = module.gitlab-custom-wif.pool_name
4848
account_id = google_service_account.gitlab.account_id

examples/gitlab-ci-custom/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Create Workload Identity Pool Provider for self-managed GitLab installation
22
module "gitlab-custom-wif" {
33
source = "Cyclenerd/wif-gitlab/google"
4-
version = "1.0.0"
4+
version = "~> 1.0.0"
55
project_id = var.project_id
66
allowed_audiences = "https://gitlab.example.com"
77
issuer_uri = "https://gitlab.example.com"
@@ -24,7 +24,7 @@ resource "google_service_account" "gitlab" {
2424
# Allow service account to login via WIF and only from GitLab repository (project path)
2525
module "github-service-account" {
2626
source = "Cyclenerd/wif-service-account/google"
27-
version = "1.0.0"
27+
version = "~> 1.0.0"
2828
project_id = var.project_id
2929
pool_name = module.gitlab-custom-wif.pool_name
3030
account_id = google_service_account.gitlab.account_id

examples/gitlab-ci/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ With this example the following steps are executed and configured:
1919
# Create Workload Identity Pool Provider for GitLab
2020
module "gitlab-wif" {
2121
source = "Cyclenerd/wif-gitlab/google"
22-
version = "1.0.0"
22+
version = "~> 1.0.0"
2323
project_id = var.project_id
2424
}
2525
@@ -34,7 +34,7 @@ resource "google_service_account" "gitlab" {
3434
# Allow service account to login via WIF and only from GitLab repository (project path)
3535
module "github-service-account" {
3636
source = "Cyclenerd/wif-service-account/google"
37-
version = "1.0.0"
37+
version = "~> 1.0.0"
3838
project_id = var.project_id
3939
pool_name = module.gitlab-wif.pool_name
4040
account_id = google_service_account.gitlab.account_id

examples/gitlab-ci/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Create Workload Identity Pool Provider for GitLab
22
module "gitlab-wif" {
33
source = "Cyclenerd/wif-gitlab/google"
4-
version = "1.0.0"
4+
version = "~> 1.0.0"
55
project_id = var.project_id
66
}
77

@@ -16,7 +16,7 @@ resource "google_service_account" "gitlab" {
1616
# Allow service account to login via WIF and only from GitLab repository (project path)
1717
module "github-service-account" {
1818
source = "Cyclenerd/wif-service-account/google"
19-
version = "1.0.0"
19+
version = "~> 1.0.0"
2020
project_id = var.project_id
2121
pool_name = module.gitlab-wif.pool_name
2222
account_id = google_service_account.gitlab.account_id

main.tf

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,8 @@ resource "google_iam_workload_identity_pool_provider" "provider" {
6767
display_name = var.provider_display_name
6868
description = var.provider_description
6969
disabled = var.provider_disabled
70-
71-
attribute_mapping = {
72-
"google.subject" = "assertion.sub"
73-
"attribute.sub" = "assertion.sub"
74-
"attribute.user_login" = "assertion.user_login"
75-
"attribute.repository" = "assertion.project_path"
76-
"attribute.ref" = "assertion.ref"
77-
}
70+
attribute_mapping = var.attribute_mapping
71+
attribute_condition = var.attribute_condition
7872
oidc {
7973
allowed_audiences = [var.allowed_audiences]
8074
issuer_uri = var.issuer_uri

variables.tf

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,44 @@ variable "allowed_audiences" {
102102
description = "Workload Identity Pool Provider allowed audiences"
103103
default = "https://gitlab.com"
104104
}
105+
106+
variable "attribute_mapping" {
107+
type = map(string)
108+
description = "Workload Identity Pool Provider attribute mapping"
109+
default = {
110+
# Default attributes used in:
111+
# https://registry.terraform.io/modules/Cyclenerd/wif-service-account/google/latest
112+
"google.subject" = "assertion.sub" # Subject
113+
"attribute.sub" = "attribute.sub" # Subject
114+
"attribute.repository" = "assertion.project_path" # The repository (project path) from where the workflow is running
115+
# More
116+
# https://docs.gitlab.com/ee/ci/secrets/id_token_authentication.html#token-payload
117+
"attribute.aud" = "attribute.aud" # Audience
118+
"attribute.iss" = "attribute.iss" # Issuer
119+
"attribute.namespace_id" = "assertion.namespace_id" # Use this to scope to group or user level namespace by ID.
120+
"attribute.namespace_path" = "assertion.namespace_path" # Use this to scope to group or user level namespace by path.
121+
"attribute.project_id" = "assertion.project_id" # Use this to scope to project by ID.
122+
"attribute.project_path" = "assertion.project_path" # Use this to scope to project by path.
123+
"attribute.user_id" = "assertion.user_id" # ID of the user executing the job.
124+
"attribute.user_login" = "assertion.user_login" # Username of the user executing the job.
125+
"attribute.user_email" = "assertion.user_email" # Email of the user executing the job.
126+
"attribute.pipeline_id" = "assertion.pipeline_id" # ID of the pipeline.
127+
"attribute.pipeline_source" = "assertion.pipeline_source" # Pipeline source.
128+
"attribute.job_id" = "assertion.job_id" # ID of the job.
129+
"attribute.ref" = "assertion.ref" # Git ref for the job.
130+
"attribute.ref_type" = "assertion.ref_type" # Git ref type, either branch or tag.
131+
"attribute.ref_protected" = "assertion.ref_protected" # true if the Git ref is protected, false otherwise.
132+
"attribute.environment" = "assertion.environment" # Environment this job deploys to (introduced in GitLab 13.9).
133+
"attribute.environment_protected" = "assertion.environment_protected" # true if deployed environment is protected, false otherwise (introduced in GitLab 13.9).
134+
"attribute.deployment_tier" = "assertion.deployment_tier" # Deployment tier of the environment the job specifies. Introduced in GitLab 15.2.
135+
"attribute.runner_id" = "assertion.runner_id" # ID of the runner executing the job. Introduced in GitLab 16.0.
136+
"attribute.runner_environment" = "assertion.runner_environment" # The type of runner used by the job. Can be either gitlab-hosted or self-hosted. Introduced in GitLab 16.0.
137+
"attribute.sha" = "assertion.sha" # The commit SHA for the job. Introduced in GitLab 16.0.
138+
}
139+
}
140+
141+
variable "attribute_condition" {
142+
type = string
143+
description = "(Optional) Workload Identity Pool Provider attribute condition expression"
144+
default = null
145+
}

0 commit comments

Comments
 (0)