Skip to content

Commit 3a79355

Browse files
github oidc setup
1 parent 802cdc1 commit 3a79355

File tree

7 files changed

+224
-37
lines changed

7 files changed

+224
-37
lines changed

.github/settings.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ _extends: .github
33

44
repository:
55
# See https://developer.github.com/v3/repos/#edit for all available settings.
6-
name: terraform-module-blueprint
7-
description: "ℹ️ Terraform module blueprint."
6+
name: terraform-aws-github-oidc-provider
7+
description: "ℹ️ Terraform GitHub OIDC module."
88
homepage: https://ivankatliarchuk.github.io
9-
topics: ivank, terraform, terraform-module
9+
topics: ivank, terraform, terraform-module, github, oidc
1010
private: false
1111
has_issues: true
1212
has_projects: false

.github/workflows/oidc.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
name: oidc.example
3+
4+
on:
5+
workflow_dispatch:

README.md

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
1-
# Module Blueprint
1+
# AWS Github 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+
## Features
6+
7+
1. Create an AWS OIDC provider for GitHub Actions
8+
1. Create one or more IAM role that can be assumed by GitHub Actions
9+
1. IAM roles can be scoped to :
10+
* One or more GitHub organisations
11+
* One or more GitHub repository
12+
* One or more branches in a repository
13+
14+
15+
| Feature | Status |
16+
|--------------------------------------------------------------------------------------------------------|--------|
17+
| Create a role for all repositories in a specific Github organisation ||
18+
| Create a role specific to a repository for a specific organisation ||
19+
| Create a role specific to a branch in a repository ||
20+
| Create a role for multiple organisations/repositories/branches ||
21+
| Create a role for organisations/repositories/branches selected by wildcard (e.g. `feature/*` branches) ||
422

523
---
624

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)
25+
[![linter](https://github.com/terraform-module/terraform-aws-github-oidc-provider/actions/workflows/linter.yml/badge.svg)](https://github.com/terraform-module/terraform-aws-github-oidc-provider/actions/workflows/linter.yml)
26+
[![release.draft](https://github.com/terraform-module/terraform-aws-github-oidc-provider/actions/workflows/release.draft.yml/badge.svg)](https://github.com/terraform-module/terraform-aws-github-oidc-provider/actions/workflows/release.draft.yml)
27+
28+
[![](https://img.shields.io/github/license/terraform-module/terraform-aws-github-oidc-provider)](https://github.com/terraform-module/terraform-aws-github-oidc-provider)
29+
![](https://img.shields.io/github/v/tag/terraform-module/terraform-aws-github-oidc-provider)
30+
![](https://img.shields.io/issues/github/terraform-module/terraform-aws-github-oidc-provider)
31+
![](https://img.shields.io/github/issues/terraform-module/terraform-aws-github-oidc-provider)
32+
![](https://img.shields.io/github/issues-closed/terraform-module/terraform-aws-github-oidc-provider)
33+
[![](https://img.shields.io/github/languages/code-size/terraform-module/terraform-aws-github-oidc-provider)](https://github.com/terraform-module/terraform-aws-github-oidc-provider)
34+
[![](https://img.shields.io/github/repo-size/terraform-module/terraform-aws-github-oidc-provider)](https://github.com/terraform-module/terraform-aws-github-oidc-provider)
35+
![](https://img.shields.io/github/languages/top/terraform-module/terraform-aws-github-oidc-provider?color=green&logo=terraform&logoColor=blue)
36+
![](https://img.shields.io/github/commit-activity/m/terraform-module/terraform-aws-github-oidc-provider)
37+
![](https://img.shields.io/github/contributors/terraform-module/terraform-aws-github-oidc-provider)
38+
![](https://img.shields.io/github/last-commit/terraform-module/terraform-aws-github-oidc-provider)
39+
[![Maintenance](https://img.shields.io/badge/Maintenu%3F-oui-green.svg)](https://GitHub.com/terraform-module/terraform-aws-github-oidc-provider/graphs/commit-activity)
40+
[![GitHub forks](https://img.shields.io/github/forks/terraform-module/terraform-aws-github-oidc-provider.svg?style=social&label=Fork)](https://github.com/terraform-module/terraform-aws-github-oidc-provider)
2341

2442
---
2543

@@ -29,13 +47,18 @@ Terraform module blueprint
2947

3048
## Usage example
3149

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).
50+
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-github-oidc-provider/releases).
3351

3452
```hcl
35-
module "blueprint" {
53+
module "github-oidc" {
3654
source = "terraform-module/blueprint"
3755
version = "0.0.0"
38-
# insert required variables here
56+
57+
create_oidc_provider = true
58+
create_oidc_role = true
59+
60+
github_repositories = var.github_repositories
61+
oidc_role_attach_policies = ["arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"]
3962
}
4063
```
4164

@@ -91,7 +114,7 @@ No resources.
91114

92115
## License
93116

94-
Copyright 2019 Ivan Katliarhcuk
117+
Copyright 2022 Ivan Katliarhcuk
95118

96119
MIT Licensed. See [LICENSE](./LICENSE) for full details.
97120

@@ -101,7 +124,7 @@ Submit a pull request
101124

102125
# Authors
103126

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

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

@@ -113,9 +136,21 @@ Currently maintained by [Ivan Katliarchuk](https://github.com/ivankatliarchuk) a
113136

114137
- [Terraform modules](https://registry.terraform.io/namespaces/terraform-module)
115138

139+
## Resources
140+
141+
- [AWS: create oidc](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html)
142+
- [Github: configure OIDC aws](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services)
143+
- [Github: OIDC cloud](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-cloud-providers)
144+
- [AWS creds github action](https://github.com/aws-actions/configure-aws-credentials)
145+
- [AWS Docs](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html)
146+
- [Github OIDC](https://www.cloudquery.io/blog/keyless-access-to-aws-in-github-actions-with-oidc)
147+
- [Terraform: oidc complex](https://github.com/SamuelBagattin/terraform-aws-github-oidc-provider)
148+
- [Terraform: oidc simple](https://github.com/unfunco/terraform-aws-oidc-github)
149+
- [Terraform: oidc](https://github.com/philips-labs/terraform-aws-github-oidc)
150+
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-github-oidc-provider/generate

examples/basic/main.tf

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
################################################################################
2-
# Supporting Resources
3-
################################################################################
4-
51
################################################################################
62
# Resources
73
################################################################################
4+
module "github-oidc" {
5+
source = "../.."
86

7+
create_oidc_provider = true
8+
create_oidc_role = true
9+
10+
github_repositories = ["terraform-module/terraform-aws-github-oidc-provider:ref:refs/heads/main"]
11+
oidc_role_attach_policies = ["arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"]
12+
}
913
################################################################################
1014
# OUTPUTS
1115
################################################################################
16+
output "oidc_provider_arn" {
17+
description = "OIDC provider ARN"
18+
value = module.github-oidc.oidc_provider_arn
19+
}
20+
21+
output "github_oidc_role" {
22+
description = "CICD GitHub role."
23+
value = module.github-oidc.oidc_role
24+
}

main.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* # AWS Github OIDC Provider Terraform Module
3+
*
4+
* ## Purpose
5+
* This module allows you to create a Github OIDC provider for your AWS account, that will help Github Actions to securely authenticate against the AWS API using an IAM role
6+
*
7+
*/
8+
resource "aws_iam_openid_connect_provider" "this" {
9+
count = var.create_oidc_provider ? 1 : 0
10+
client_id_list = [
11+
"sts.amazonaws.com",
12+
]
13+
thumbprint_list = [var.github_thumbprint]
14+
url = "https://token.actions.githubusercontent.com"
15+
}
16+
17+
resource "aws_iam_role" "this" {
18+
count = var.create_oidc_provider && var.create_oidc_role ? 1 : 0
19+
name = var.role_name
20+
description = var.role_description
21+
max_session_duration = var.max_session_duration
22+
assume_role_policy = data.aws_iam_policy_document.this.json
23+
tags = var.tags
24+
# path = var.iam_role_path
25+
# permissions_boundary = var.iam_role_permissions_boundary
26+
depends_on = [ aws_iam_openid_connect_provider.this ]
27+
}
28+
29+
resource "aws_iam_role_policy_attachment" "attach" {
30+
count = var.create_oidc_role ? length(var.oidc_role_attach_policies) : 0
31+
32+
policy_arn = var.oidc_role_attach_policies[count.index]
33+
role = aws_iam_role.this[0].id
34+
35+
depends_on = [ aws_iam_role.this ]
36+
}
37+
38+
data "aws_iam_policy_document" "this" {
39+
40+
dynamic "statement" {
41+
for_each = aws_iam_openid_connect_provider.this
42+
43+
content {
44+
actions = ["sts:AssumeRoleWithWebIdentity"]
45+
effect = "Allow"
46+
47+
condition {
48+
test = "StringLike"
49+
values = [
50+
for repo in var.github_repositories :
51+
"repo:%{if length(regexall(":+", repo)) > 0}${repo}%{else}${repo}:*%{endif}"
52+
]
53+
variable = "token.actions.githubusercontent.com:sub"
54+
}
55+
56+
principals {
57+
identifiers = [ statement.value.arn ]
58+
type = "Federated"
59+
}
60+
}
61+
}
62+
}

outputs.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
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, "")
49
}

variables.tf

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
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+
// Refer to the README for information on obtaining the thumbprint.
14+
// This is specified as a variable to allow it to be updated quickly if it is
15+
// unexpectedly changed by GitHub.
16+
// See: https://github.blog/changelog/2022-01-13-github-actions-update-on-oidc-based-deployments-to-aws/
17+
variable "github_thumbprint" {
18+
description = "GitHub OpenID TLS certificate thumbprint."
19+
type = string
20+
default = "6938fd4d98bab03faadb97b34396831e3780aea1"
21+
}
22+
23+
variable "github_repositories" {
24+
description = "List of GitHub organization/repository names authorized to assume the role."
25+
type = list(string)
26+
default = []
27+
28+
validation {
29+
// Ensures each element of github_repositories list matches the
30+
// organization/repository format used by GitHub.
31+
condition = length([
32+
for repo in var.github_repositories : 1
33+
if length(regexall("^[A-Za-z0-9_.-]+?/([A-Za-z0-9_.:/-]+|\\*)$", repo)) > 0
34+
]) == length(var.github_repositories)
35+
error_message = "Repositories must be specified in the organization/repository format."
36+
}
37+
}
38+
39+
variable "max_session_duration" {
40+
description = "Maximum session duration in seconds."
41+
type = number
42+
default = 3600
43+
44+
validation {
45+
condition = var.max_session_duration >= 3600 && var.max_session_duration <= 43200
46+
error_message = "Maximum session duration must be between 3600 and 43200 seconds."
47+
}
48+
}
49+
50+
variable "oidc_role_attach_policies" {
51+
description = "Attach policies to OIDC role."
52+
type = list(string)
53+
default = []
54+
}
55+
56+
variable "tags" {
57+
description = "A mapping of tags to assign to all resources"
58+
type = map(string)
59+
default = {}
60+
}
61+
62+
variable "role_name" {
63+
description = "(Optional, Forces new resource) Friendly name of the role."
64+
type = string
65+
default = "oidc-provider-aws-github-action"
66+
}
67+
68+
variable "role_description" {
69+
description = "(Optional) Description of the role."
470
type = string
71+
default = "Role assumed by the GitHub OIDC provider."
572
}

0 commit comments

Comments
 (0)