Skip to content

Commit ad489a3

Browse files
Merge pull request #68 from adamwolfe-tc/docs/verbiage_and_examples_update
docs: verbiage and examples update
2 parents 6cae72f + f17a98e commit ad489a3

File tree

8 files changed

+128
-53
lines changed

8 files changed

+128
-53
lines changed

README.md

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
# AWS GitLab OIDC Provider Terraform Module
22

3-
This module allows you to create a GitHub OIDC provider and the associated IAM roles, that will help Github Actions to securely authenticate against the AWS API using an IAM role.
3+
This module allows you to create an AWS IAM OIDC provider that trusts GitLab and the associated IAM roles, that will help GitLab Pipelines to securely authenticate against the AWS API using an IAM role.
44

5-
We recommend using GitHub's OIDC provider to get short-lived credentials needed for your actions. Specifying role-to-assume without providing an aws-access-key-id or a web-identity-token-file will signal to the action that you wish to use the OIDC provider. The default session duration is 1 hour when using the OIDC provider to directly assume an IAM Role. The default session duration is 6 hours when using an IAM User to assume an IAM Role (by providing an aws-access-key-id, aws-secret-access-key, and a role-to-assume) . If you would like to adjust this you can pass a duration to role-duration-seconds, but the duration cannot exceed the maximum that was defined when the IAM Role was created. The default session name is GitHubActions, and you can modify it by specifying the desired name in role-session-name.
5+
We recommend using GitLab's OIDC issuer to get short-lived credentials needed for your pipelines. Specifying role-to-assume without providing an aws-access-key-id or a web-identity-token-file will signal to the pipeline that you wish to use the OIDC provider. The default session duration is 1 hour when using the OIDC provider to directly assume an IAM Role. The default session duration is 6 hours when using an IAM User to assume an IAM Role (by providing an aws-access-key-id, aws-secret-access-key, and a role-to-assume) . If you would like to adjust this you can pass a duration to role-duration-seconds, but the duration cannot exceed the maximum that was defined when the IAM Role was created. The default session name is `GitLabRunner-${CI_PROJECT_ID}-${CI_PIPELINE_ID}`, and you can modify it by specifying the desired name in role-session-name.
66

77
## Use-Cases
88

99
1. Retrieve temporary credentials from AWS to access cloud services
1010
1. Use credentials to retrieve secrets or deploy to an environment
1111
1. Scope role to branch or project
12-
1. Create an AWS OIDC provider for GitHub Actions
12+
1. Create an AWS OIDC provider for GitLab Pipelines
1313

1414
## Features
1515

16-
2. Create one or more IAM role that can be assumed by GitHub Actions
16+
2. Create one or more IAM role that can be assumed by GitLab Pipelines
1717
3. IAM roles can be scoped to :
18-
* One or more GitHub organisations
19-
* One or more GitHub repository
20-
* One or more branches in a repository
18+
* One or more GitLab namespaces
19+
* One or more GitLab project
20+
* One or more branches in a project
2121

2222
| Feature | Status |
2323
|--------------------------------------------------------------------------------------------------------|--------|
24-
| Create a role for all repositories in a specific Github organisation ||
25-
| Create a role specific to a repository for a specific organisation ||
26-
| Create a role specific to a branch in a repository ||
27-
| Create a role for multiple organisations/repositories/branches ||
28-
| Create a role for organisations/repositories/branches selected by wildcard (e.g. `feature/*` branches) ||
24+
| Create a role for all projects in a specific GitLab namespace ||
25+
| Create a role specific to a project for a specific namespace ||
26+
| Create a role specific to a branch in a project ||
27+
| Create a role for multiple namespaces/projects/branches ||
28+
| Create a role for namesapces/projectss/branches selected by wildcard (e.g. `feature/*` branches) ||
2929

3030
---
3131

@@ -57,14 +57,14 @@ We recommend using GitHub's OIDC provider to get short-lived credentials needed
5757
IMPORTANT: The master branch is used in source just as an example. In your code, do not pin to master because there may be breaking changes between releases. Instead pin to the release tag (e.g. ?ref=tags/x.y.z) of one of our [latest releases](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider/releases).
5858

5959
```hcl
60-
module "gitlab-oidc" {
60+
module "gitlab_oidc" {
6161
source = "terraform-module/gitlab-oidc-provider/aws"
6262
version = "~> 1"
6363
6464
create_oidc_provider = true
6565
create_oidc_role = true
6666
67-
repositories = ["terraform-module/module-blueprint"]
67+
project_paths = ["project_path:terraform-module/module-blueprint", "project_path:foo/bar"]
6868
oidc_role_attach_policies = ["arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"]
6969
}
7070
```
@@ -81,35 +81,65 @@ See `examples` directory for working examples to reference
8181
## Available features
8282

8383
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
84+
# AWS Gitlab OIDC Provider Terraform Module
85+
86+
## Purpose
87+
This module allows you to create a Gitlab OIDC provider for your AWS account, that will allow Gitlab pipelines to securely authenticate against the AWS API using an IAM role
88+
8489
## Requirements
8590

8691
| Name | Version |
8792
|------|---------|
8893
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1 |
94+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.40 |
95+
| <a name="requirement_tls"></a> [tls](#requirement\_tls) | >= 3.0 |
8996

9097
## Providers
9198

92-
No providers.
99+
| Name | Version |
100+
|------|---------|
101+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.40 |
102+
| <a name="provider_tls"></a> [tls](#provider\_tls) | >= 3.0 |
93103

94104
## Modules
95105

96106
No modules.
97107

98108
## Resources
99109

100-
No resources.
110+
| Name | Type |
111+
|------|------|
112+
| [aws_iam_openid_connect_provider.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_openid_connect_provider) | resource |
113+
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
114+
| [aws_iam_role_policy_attachment.attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
115+
| [aws_iam_policy_document.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
116+
| [tls_certificate.gitlab](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/data-sources/certificate) | data source |
101117

102118
## Inputs
103119

104120
| Name | Description | Type | Default | Required |
105121
|------|-------------|------|---------|:--------:|
106-
| <a name="input_variable"></a> [variable](#input\_variable) | defaul,description,type | `string` | `"variable"` | no |
122+
| <a name="input_aud_value"></a> [aud\_value](#input\_aud\_value) | (Required) A list of client IDs (also known as audiences). When a mobile or web app registers with an OpenID Connect provider, they establish a value that identifies the application. (This is the value that's sent as the client\_id parameter on OAuth requests.) | `list(string)` | <pre>[<br> "https://gitlab.com"<br>]</pre> | no |
123+
| <a name="input_create_oidc_provider"></a> [create\_oidc\_provider](#input\_create\_oidc\_provider) | Whether or not to create the associated oidc provider. If false, variable 'oidc\_provider\_arn' is required | `bool` | `true` | no |
124+
| <a name="input_create_oidc_role"></a> [create\_oidc\_role](#input\_create\_oidc\_role) | Whether or not to create the OIDC attached role | `bool` | `true` | no |
125+
| <a name="input_gitlab_tls_url"></a> [gitlab\_tls\_url](#input\_gitlab\_tls\_url) | the Hashicorp TLS provider has started following redirects starting v4. so we use tls:// | `string` | `"tls://gitlab.com:443"` | no |
126+
| <a name="input_match_field"></a> [match\_field](#input\_match\_field) | the token field the OIDC provider filter on | `string` | `"sub"` | no |
127+
| <a name="input_max_session_duration"></a> [max\_session\_duration](#input\_max\_session\_duration) | Maximum session duration in seconds. | `number` | `3600` | no |
128+
| <a name="input_oidc_role_attach_policies"></a> [oidc\_role\_attach\_policies](#input\_oidc\_role\_attach\_policies) | Attach policies to OIDC role. | `list(string)` | `[]` | no |
129+
| <a name="input_projects"></a> [projects](#input\_projects) | List of GitLab namesapce/project names authorized to assume the role. | `list(string)` | `[]` | no |
130+
| <a name="input_role_description"></a> [role\_description](#input\_role\_description) | (Optional) Description of the role. | `string` | `"Role assumed by the Gitlab OIDC provider."` | no |
131+
| <a name="input_role_name"></a> [role\_name](#input\_role\_name) | (Optional, Forces new resource) Friendly name of the role. | `string` | `"gitlab-oidc-provider-aws"` | no |
132+
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to all resources | `map(string)` | `{}` | no |
133+
| <a name="input_url"></a> [url](#input\_url) | GitLab OpenID TLS certificate URL. The address of your GitLab instance, such as https://gitlab.com or http://gitlab.example.com. | `string` | `"https://gitlab.com"` | no |
107134

108135
## Outputs
109136

110137
| Name | Description |
111138
|------|-------------|
112-
| <a name="output_used"></a> [used](#output\_used) | used value |
139+
| <a name="output_oidc_provider_arn"></a> [oidc\_provider\_arn](#output\_oidc\_provider\_arn) | OIDC provider ARN |
140+
| <a name="output_oidc_role"></a> [oidc\_role](#output\_oidc\_role) | CICD GitHub role. |
141+
| <a name="output_policy_document"></a> [policy\_document](#output\_policy\_document) | joined IAM policy documents |
142+
| <a name="output_thumbprint"></a> [thumbprint](#output\_thumbprint) | TLS endpoint certificate SHA1 Fingerprint |
113143
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
114144

115145

examples/basic/README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Example
22

3-
Configuration in this directory creates a MySQL Aurora cluster.
3+
Configuration in this directory creates an AWS IAM OpenID Connect Provider, IAM Role and IAM Role Policy for trusing the OIDC provider.
44

55
## Usage
66

@@ -15,5 +15,34 @@ $ terraform apply
1515
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
1616

1717
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
## Requirements
1819

20+
| Name | Version |
21+
|------|---------|
22+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
23+
24+
## Providers
25+
26+
No providers.
27+
28+
## Modules
29+
30+
| Name | Source | Version |
31+
|------|--------|---------|
32+
| <a name="module_gitlab_oidc"></a> [gitlab\_oidc](#module\_gitlab\_oidc) | ../.. | n/a |
33+
34+
## Resources
35+
36+
No resources.
37+
38+
## Inputs
39+
40+
No inputs.
41+
42+
## Outputs
43+
44+
| Name | Description |
45+
|------|-------------|
46+
| <a name="output_oidc_provider_arn"></a> [oidc\_provider\_arn](#output\_oidc\_provider\_arn) | OIDC provider ARN |
47+
| <a name="output_oidc_role"></a> [oidc\_role](#output\_oidc\_role) | CICD GiLab role. |
1948
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/basic/main.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
}
4+
15
################################################################################
26
# Resources
37
################################################################################
48

5-
module "gitlab-oidc" {
9+
module "gitlab_oidc" {
610
source = "../.."
711

812
create_oidc_provider = true
913
create_oidc_role = true
1014

11-
repositories = ["terraform-module/terraform-aws-github-oidc-provider"]
15+
project_paths = ["project_path:terraform-module/terraform-aws-gitlab-oidc-provider"]
1216
oidc_role_attach_policies = ["arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"]
1317
}
1418

gitlab/.gitlab-ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ authenticate:
88
entrypoint: [""]
99
variables:
1010
ROLE_ARN: arn:aws:iam::XXXXXXXXXXX:role/gitlab-oidc-provider-aws
11+
id_tokens:
12+
GITLAB_OIDC_TOKEN:
13+
aud: https://gitlab.com
1114
script:
1215
- aws --version
1316
- >
1417
export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s"
1518
$(aws sts assume-role-with-web-identity
1619
--role-arn ${ROLE_ARN}
1720
--role-session-name "GitLabRunner-${CI_PROJECT_ID}-${CI_PIPELINE_ID}"
18-
--web-identity-token $CI_JOB_JWT_V2
21+
--web-identity-token $GITLAB_OIDC_TOKEN
1922
--duration-seconds 3600
2023
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]'
2124
--output text))

main.tf

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
* # AWS Gitlab OIDC Provider Terraform Module
33
*
44
* ## Purpose
5-
* This module allows you to create a Gitlab OIDC provider for your AWS account, that will help Gitlab Actions to securely authenticate against the AWS API using an IAM role
5+
* This module allows you to create a Gitlab OIDC provider for your AWS account, that will allow Gitlab pipelines to securely authenticate against the AWS API using an IAM role
66
*
77
*/
8+
89
data "tls_certificate" "gitlab" {
910
url = var.gitlab_tls_url
1011
}
@@ -13,7 +14,7 @@ resource "aws_iam_openid_connect_provider" "this" {
1314
count = var.create_oidc_provider ? 1 : 0
1415

1516
client_id_list = var.aud_value
16-
thumbprint_list = ["${data.tls_certificate.gitlab.certificates.0.sha1_fingerprint}"]
17+
thumbprint_list = [data.tls_certificate.gitlab.certificates[0].sha1_fingerprint]
1718
url = var.url
1819
}
1920

@@ -22,19 +23,19 @@ resource "aws_iam_role" "this" {
2223
name = var.role_name
2324
description = var.role_description
2425
max_session_duration = var.max_session_duration
25-
assume_role_policy = join("", data.aws_iam_policy_document.this.*.json)
26+
assume_role_policy = join("", data.aws_iam_policy_document.this[*].json)
2627
tags = var.tags
2728

28-
depends_on = [ aws_iam_openid_connect_provider.this ]
29+
depends_on = [aws_iam_openid_connect_provider.this]
2930
}
3031

3132
resource "aws_iam_role_policy_attachment" "attach" {
3233
count = var.create_oidc_role ? length(var.oidc_role_attach_policies) : 0
3334

3435
policy_arn = var.oidc_role_attach_policies[count.index]
35-
role = join("", aws_iam_role.this.*.name)
36+
role = join("", aws_iam_role.this[*].name)
3637

37-
depends_on = [ aws_iam_role.this ]
38+
depends_on = [aws_iam_role.this]
3839
}
3940

4041
data "aws_iam_policy_document" "this" {
@@ -47,13 +48,13 @@ data "aws_iam_policy_document" "this" {
4748
effect = "Allow"
4849

4950
condition {
50-
test = "StringLike"
51-
values = var.repositories
52-
variable = "${join("", aws_iam_openid_connect_provider.this.*.url)}:${var.match_field}"
51+
test = "StringLike"
52+
values = var.project_paths
53+
variable = "${join("", aws_iam_openid_connect_provider.this[*].url)}:${var.match_field}"
5354
}
5455

5556
principals {
56-
identifiers = [ statement.value.arn ]
57+
identifiers = [statement.value.arn]
5758
type = "Federated"
5859
}
5960
}

outputs.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ output "oidc_role" {
1010

1111
output "thumbprint" {
1212
description = "TLS endpoint certificate SHA1 Fingerprint"
13-
value = [data.tls_certificate.gitlab.certificates[0].sha1_fingerprint]
13+
value = [data.tls_certificate.gitlab.certificates[0].sha1_fingerprint]
1414
}
1515

1616
output "policy_document" {
17-
value = join("", data.aws_iam_policy_document.this.*.json)
17+
description = "joined IAM policy documents"
18+
value = join("", data.aws_iam_policy_document.this[*].json)
1819
}

variables.tf

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ variable "role_description" {
3434
default = "Role assumed by the Gitlab OIDC provider."
3535
}
3636

37-
variable "repositories" {
38-
description = "List of GitLab organization/repository names authorized to assume the role."
37+
variable "project_paths" {
38+
description = "List of GitLab namesapce/project names authorized to assume the role."
3939
type = list(string)
4040
default = []
4141

4242
validation {
43-
# Ensures each element of github_repositories list matches the
44-
# organization/repository format used by GitHub.
43+
# Ensures each element of gitlab_projects list matches the
44+
# namespace/project format used by GitLab.
4545
condition = length([
46-
for repo in var.repositories : 1
47-
if length(regexall("^project_path:[A-Za-z0-9_.-]+?/([A-Za-z0-9_.:/-]+|\\*)$", repo)) > 0
48-
]) == length(var.repositories)
49-
error_message = "Repositories must be specified in the organization/repository format."
46+
for path in var.project_paths : 1
47+
if length(regexall("project_path:[A-Za-z0-9_.-]+?/([A-Za-z0-9_.:/-]+|\\*)$", path)) > 0
48+
]) == length(var.project_paths)
49+
error_message = "Projects must be specified in the "project_path:namespace/project" format."
5050
}
5151
}
5252

@@ -68,24 +68,20 @@ variable "oidc_role_attach_policies" {
6868
}
6969

7070
variable "match_field" {
71-
type = string
72-
default = "sub"
73-
}
74-
75-
variable "gitlab_url" {
76-
type = string
77-
default = "https://gitlab.com"
71+
description = "the token field the OIDC provider filter on"
72+
type = string
73+
default = "sub"
7874
}
7975

8076
variable "gitlab_tls_url" {
81-
type = string
82-
# Avoid using https scheme because the Hashicorp TLS provider has started following redirects starting v4.
77+
type = string
78+
description = "the Hashicorp TLS provider has started following redirects starting v4. so we use tls://"
8379
# See https://github.com/hashicorp/terraform-provider-tls/issues/249
8480
default = "tls://gitlab.com:443"
8581
}
8682

8783
variable "aud_value" {
8884
description = "(Required) A list of client IDs (also known as audiences). When a mobile or web app registers with an OpenID Connect provider, they establish a value that identifies the application. (This is the value that's sent as the client_id parameter on OAuth requests.)"
89-
type = list(string)
90-
default = ["https://gitlab.com"]
85+
type = list(string)
86+
default = ["https://gitlab.com"]
9187
}

versions.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
terraform {
22
required_version = ">= 1"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 5.0"
8+
}
9+
tls = {
10+
source = "hashicorp/tls"
11+
version = ">= 3.0"
12+
}
13+
}
314
}

0 commit comments

Comments
 (0)