Skip to content

Commit 211e2f6

Browse files
test it, everything is working
1 parent 17b8f95 commit 211e2f6

File tree

6 files changed

+270
-37
lines changed

6 files changed

+270
-37
lines changed

README.md

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
1-
# Module Blueprint
1+
# AWS GitLab OIDC Provider Terraform Module
22

3-
Terraform module blueprint
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.
4+
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.
6+
7+
## Use-Cases
8+
9+
1. Retrieve temporary credentials from AWS to access cloud services
10+
1. Use credentials to retrieve secrets or deploy to an environment
11+
1. Scope role to branch or project
12+
1. Create an AWS OIDC provider for GitHub Actions
13+
14+
## Features
15+
16+
2. Create one or more IAM role that can be assumed by GitHub Actions
17+
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
21+
22+
| Feature | Status |
23+
|--------------------------------------------------------------------------------------------------------|--------|
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) ||
429

530
---
631

7-
[![linter](https://github.com/terraform-module/terraform-module-blueprint/actions/workflows/linter.yml/badge.svg)](https://github.com/terraform-module/terraform-module-blueprint/actions/workflows/linter.yml)
8-
[![release.draft](https://github.com/terraform-module/terraform-module-blueprint/actions/workflows/release.draft.yml/badge.svg)](https://github.com/terraform-module/terraform-module-blueprint/actions/workflows/release.draft.yml)
9-
10-
[![](https://img.shields.io/github/license/terraform-module/terraform-module-blueprint)](https://github.com/terraform-module/terraform-module-blueprint)
11-
![](https://img.shields.io/github/v/tag/terraform-module/terraform-module-blueprint)
12-
![](https://img.shields.io/issues/github/terraform-module/terraform-module-blueprint)
13-
![](https://img.shields.io/github/issues/terraform-module/terraform-module-blueprint)
14-
![](https://img.shields.io/github/issues-closed/terraform-module/terraform-module-blueprint)
15-
[![](https://img.shields.io/github/languages/code-size/terraform-module/terraform-module-blueprint)](https://github.com/terraform-module/terraform-module-blueprint)
16-
[![](https://img.shields.io/github/repo-size/terraform-module/terraform-module-blueprint)](https://github.com/terraform-module/terraform-module-blueprint)
17-
![](https://img.shields.io/github/languages/top/terraform-module/terraform-module-blueprint?color=green&logo=terraform&logoColor=blue)
18-
![](https://img.shields.io/github/commit-activity/m/terraform-module/terraform-module-blueprint)
19-
![](https://img.shields.io/github/contributors/terraform-module/terraform-module-blueprint)
20-
![](https://img.shields.io/github/last-commit/terraform-module/terraform-module-blueprint)
21-
[![Maintenance](https://img.shields.io/badge/Maintenu%3F-oui-green.svg)](https://GitHub.com/terraform-module/terraform-module-blueprint/graphs/commit-activity)
22-
[![GitHub forks](https://img.shields.io/github/forks/terraform-module/terraform-module-blueprint.svg?style=social&label=Fork)](https://github.com/terraform-module/terraform-module-blueprint)
32+
[![linter](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider/actions/workflows/linter.yml/badge.svg)](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider/actions/workflows/linter.yml)
33+
[![release.draft](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider/actions/workflows/release.draft.yml/badge.svg)](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider/actions/workflows/release.draft.yml)
34+
35+
[![](https://img.shields.io/github/license/terraform-module/terraform-aws-gitlab-oidc-provider)](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider)
36+
![](https://img.shields.io/github/v/tag/terraform-module/terraform-aws-gitlab-oidc-provider)
37+
![](https://img.shields.io/issues/github/terraform-module/terraform-aws-gitlab-oidc-provider)
38+
![](https://img.shields.io/github/issues/terraform-module/terraform-aws-gitlab-oidc-provider)
39+
![](https://img.shields.io/github/issues-closed/terraform-module/terraform-aws-gitlab-oidc-provider)
40+
[![](https://img.shields.io/github/languages/code-size/terraform-module/terraform-aws-gitlab-oidc-provider)](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider)
41+
[![](https://img.shields.io/github/repo-size/terraform-module/terraform-aws-gitlab-oidc-provider)](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider)
42+
![](https://img.shields.io/github/languages/top/terraform-module/terraform-aws-gitlab-oidc-provider?color=green&logo=terraform&logoColor=blue)
43+
![](https://img.shields.io/github/commit-activity/m/terraform-module/terraform-aws-gitlab-oidc-provider)
44+
![](https://img.shields.io/github/contributors/terraform-module/terraform-aws-gitlab-oidc-provider)
45+
![](https://img.shields.io/github/last-commit/terraform-module/terraform-aws-gitlab-oidc-provider)
46+
[![Maintenance](https://img.shields.io/badge/Maintenu%3F-oui-green.svg)](https://GitHub.com/terraform-module/terraform-aws-gitlab-oidc-provider/graphs/commit-activity)
47+
[![GitHub forks](https://img.shields.io/github/forks/terraform-module/terraform-aws-gitlab-oidc-provider.svg?style=social&label=Fork)](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider)
2348

2449
---
2550

@@ -29,21 +54,27 @@ Terraform module blueprint
2954

3055
## Usage example
3156

32-
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-module-blueprint/releases).
57+
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).
3358

3459
```hcl
35-
module "blueprint" {
36-
source = "terraform-module/blueprint"
37-
version = "0.0.0"
38-
# insert required variables here
60+
module "gitlab-oidc" {
61+
source = "terraform-module/gitlab-oidc-provider/aws"
62+
version = "~> 1"
63+
64+
create_oidc_provider = true
65+
create_oidc_role = true
66+
67+
repositories = ["terraform-module/module-blueprint"]
68+
oidc_role_attach_policies = ["arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"]
3969
}
4070
```
4171

4272
## Examples
4373

4474
See `examples` directory for working examples to reference
4575

46-
- [Examples Dir](https://github.com/terraform-module/module-blueprint/tree/master/examples/)
76+
- [Examples TFM Dir](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider)
77+
- [Examples Gitlab Pipeline](./gitlab/)
4778

4879
## Assumptions
4980

@@ -101,21 +132,28 @@ Submit a pull request
101132

102133
# Authors
103134

104-
Currently maintained by [Ivan Katliarchuk](https://github.com/ivankatliarchuk) and these [awesome contributors](https://github.com/terraform-module/terraform-module-blueprint/graphs/contributors).
135+
Currently maintained by [Ivan Katliarchuk](https://github.com/ivankatliarchuk) and these [awesome contributors](https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider/graphs/contributors).
105136

106137
[![ForTheBadge uses-git](http://ForTheBadge.com/images/badges/uses-git.svg)](https://GitHub.com/)
107138

108139
## Terraform Registry
109140

110141
- [Module](https://registry.terraform.io/modules/terraform-module/todo/aws)
142+
- [Terraform modules](https://registry.terraform.io/namespaces/terraform-module)
111143

112144
## Resources
113145

114-
- [Terraform modules](https://registry.terraform.io/namespaces/terraform-module)
146+
- [AWS: create oidc](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html)
147+
- [Blog: OIDC with AWS and GitLab](https://oblcc.com/blog/configure-openid-connect-for-gitlab-and-aws/)
148+
- [Blog: Gitlab OIDC](https://docs.gitlab.com/ee/ci/cloud_services/aws/)
149+
- [Tfm: OIDC Gitlab](https://gitlab.com/guided-explorations/aws/configure-openid-connect-in-aws/)
115150

116151
## Clone Me
117152

118153
[**Create a repository using this template →**][template.generate]
119154

120155
<!-- resources -->
121-
[template.generate]: https://github.com/terraform-module/terraform-module-blueprint/generate
156+
[template.generate]: https://github.com/terraform-module/terraform-aws-gitlab-oidc-provider/generate
157+
158+
159+
<!-- https://github.com/moritzheiber/terraform-aws-oidc-github-actions-module -->

examples/basic/main.tf

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
################################################################################
2-
# Supporting Resources
3-
################################################################################
4-
51
################################################################################
62
# Resources
73
################################################################################
84

5+
module "gitlab-oidc" {
6+
source = "../.."
7+
8+
create_oidc_provider = true
9+
create_oidc_role = true
10+
11+
repositories = ["terraform-module/terraform-aws-github-oidc-provider"]
12+
oidc_role_attach_policies = ["arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"]
13+
}
14+
915
################################################################################
1016
# OUTPUTS
1117
################################################################################
18+
19+
output "oidc_provider_arn" {
20+
description = "OIDC provider ARN"
21+
value = module.gitlab-oidc.oidc_provider_arn
22+
}
23+
24+
output "oidc_role" {
25+
description = "CICD GiLab role."
26+
value = module.gitlab-oidc.oidc_role
27+
}

gitlab/.gitlab-ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
stages:
2+
- authenticate
3+
4+
authenticate:
5+
stage: authenticate
6+
image:
7+
name: amazon/aws-cli:latest
8+
entrypoint: [""]
9+
variables:
10+
ROLE_ARN: arn:aws:iam::XXXXXXXXXXX:role/gitlab-oidc-provider-aws
11+
script:
12+
- aws --version
13+
- >
14+
STS=($(aws sts assume-role-with-web-identity
15+
--role-arn ${ROLE_ARN}
16+
--role-session-name "GitLabRunner-${CI_PROJECT_ID}-${CI_PIPELINE_ID}"
17+
--web-identity-token $CI_JOB_JWT_V2
18+
--duration-seconds 3600
19+
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]'
20+
--output text))
21+
- export AWS_ACCESS_KEY_ID="${STS[0]}"
22+
- export AWS_SECRET_ACCESS_KEY="${STS[1]}"
23+
- export AWS_SESSION_TOKEN="${STS[2]}"
24+
- aws sts get-caller-identity
25+
allow_failure: false

main.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* # AWS Gitlab OIDC Provider Terraform Module
3+
*
4+
* ## 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
6+
*
7+
*/
8+
data "tls_certificate" "gitlab" {
9+
url = var.url
10+
}
11+
12+
resource "aws_iam_openid_connect_provider" "this" {
13+
count = var.create_oidc_provider ? 1 : 0
14+
15+
client_id_list = var.aud_value
16+
thumbprint_list = ["${data.tls_certificate.gitlab.certificates.0.sha1_fingerprint}"]
17+
url = var.url
18+
}
19+
20+
resource "aws_iam_role" "this" {
21+
count = var.create_oidc_provider && var.create_oidc_role ? 1 : 0
22+
name = var.role_name
23+
description = var.role_description
24+
max_session_duration = var.max_session_duration
25+
assume_role_policy = join("", data.aws_iam_policy_document.this.*.json)
26+
tags = var.tags
27+
28+
depends_on = [ aws_iam_openid_connect_provider.this ]
29+
}
30+
31+
resource "aws_iam_role_policy_attachment" "attach" {
32+
count = var.create_oidc_role ? length(var.oidc_role_attach_policies) : 0
33+
34+
policy_arn = var.oidc_role_attach_policies[count.index]
35+
role = join("", aws_iam_role.this.*.name)
36+
37+
depends_on = [ aws_iam_role.this ]
38+
}
39+
40+
data "aws_iam_policy_document" "this" {
41+
42+
dynamic "statement" {
43+
for_each = aws_iam_openid_connect_provider.this
44+
45+
content {
46+
actions = ["sts:AssumeRoleWithWebIdentity"]
47+
effect = "Allow"
48+
49+
condition {
50+
test = "StringLike"
51+
values = var.repositories
52+
variable = "${join("", aws_iam_openid_connect_provider.this.*.url)}:${var.match_field}"
53+
}
54+
55+
principals {
56+
identifiers = [ statement.value.arn ]
57+
type = "Federated"
58+
}
59+
}
60+
}
61+
}

outputs.tf

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
output "used" {
2-
description = "used value"
3-
value = var.variable
1+
output "oidc_provider_arn" {
2+
description = "OIDC provider ARN"
3+
value = try(aws_iam_openid_connect_provider.this[0].arn, "")
4+
}
5+
6+
output "oidc_role" {
7+
description = "CICD GitHub role."
8+
value = try(aws_iam_role.this[0].arn, "")
9+
}
10+
11+
output "thumbprint" {
12+
description = "TLS endpoint certificate SHA1 Fingerprint"
13+
value = data.tls_certificate.gitlab.certificates.0.sha1_fingerprint
14+
}
15+
16+
output "policy_document" {
17+
value = join("", data.aws_iam_policy_document.this.*.json)
418
}

variables.tf

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,84 @@
1-
variable "variable" {
2-
default = "variable"
3-
description = "defaul,description,type"
1+
variable "create_oidc_provider" {
2+
description = "Whether or not to create the associated oidc provider. If false, variable 'oidc_provider_arn' is required"
3+
type = bool
4+
default = true
5+
}
6+
7+
variable "create_oidc_role" {
8+
description = "Whether or not to create the OIDC attached role"
9+
type = bool
10+
default = true
11+
}
12+
13+
variable "url" {
14+
description = "GitLab OpenID TLS certificate URL. The address of your GitLab instance, such as https://gitlab.com or http://gitlab.example.com."
15+
type = string
16+
default = "https://gitlab.com"
17+
}
18+
19+
variable "tags" {
20+
description = "A mapping of tags to assign to all resources"
21+
type = map(string)
22+
default = {}
23+
}
24+
25+
variable "role_name" {
26+
description = "(Optional, Forces new resource) Friendly name of the role."
27+
type = string
28+
default = "gitlab-oidc-provider-aws"
29+
}
30+
31+
variable "role_description" {
32+
description = "(Optional) Description of the role."
433
type = string
34+
default = "Role assumed by the Gitlab OIDC provider."
35+
}
36+
37+
variable "repositories" {
38+
description = "List of GitLab organization/repository names authorized to assume the role."
39+
type = list(string)
40+
default = []
41+
42+
validation {
43+
# Ensures each element of github_repositories list matches the
44+
# organization/repository format used by GitHub.
45+
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."
50+
}
51+
}
52+
53+
variable "max_session_duration" {
54+
description = "Maximum session duration in seconds."
55+
type = number
56+
default = 3600
57+
58+
validation {
59+
condition = var.max_session_duration >= 3600 && var.max_session_duration <= 43200
60+
error_message = "Maximum session duration must be between 3600 and 43200 seconds."
61+
}
62+
}
63+
64+
variable "oidc_role_attach_policies" {
65+
description = "Attach policies to OIDC role."
66+
type = list(string)
67+
default = []
68+
}
69+
70+
variable "match_field" {
71+
type = string
72+
default = "sub"
73+
}
74+
75+
variable "gitlab_url" {
76+
type = string
77+
default = "https://gitlab.com"
78+
}
79+
80+
variable "aud_value" {
81+
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.)"
82+
type = list(string)
83+
default = ["https://gitlab.com"]
584
}

0 commit comments

Comments
 (0)