Skip to content

Commit 6d81a81

Browse files
Merge pull request #6 from SamuelBagattin/feat/oidc_dynamic_fingerprint
Add dynamic OIDC fingerprint + add support for filtering by tag, environment and pull request
2 parents 6b23635 + 14d3515 commit 6d81a81

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

locals.tf

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
locals {
22
org_defaults = {
3-
role_name = "githubActions-iamRole"
4-
allowed_branches = ["master"]
3+
role_name = "githubActions-iamRole"
4+
allowed_branches = ["main"]
5+
allowed_tags = []
6+
allowed_environments = []
7+
pull_requests = false
58
repositories = {
69
"*" = {}
710
}
@@ -36,9 +39,13 @@ locals {
3639
for org_name, org_data in local.github_orgs_with_repos : [
3740
for repo_name, repo_data in org_data["repositories"] : {
3841
role_name : repo_data["role_name"]
39-
github_subs : [
40-
for branch in repo_data["allowed_branches"] : "repo:${org_name}/${repo_name}:ref:refs/heads/${branch}"
41-
] }
42+
github_subs : flatten([
43+
[for branch in repo_data["allowed_branches"] : "repo:${org_name}/${repo_name}:ref:refs/heads/${branch}"],
44+
[for tag in repo_data["allowed_tags"] : "repo:${org_name}/${repo_name}:ref:refs/tags/${tag}"],
45+
[for env in repo_data["allowed_environments"] : "repo:${org_name}/${repo_name}:environment:${env}"],
46+
[for dummy in ["DUMMY"] : "repo:${org_name}/${repo_name}:pull_request" if repo_data["pull_requests"] == true]
47+
])
48+
}
4249
]
4350
])
4451

main.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ resource "aws_iam_openid_connect_provider" "github_actions" {
1111
"sts.amazonaws.com",
1212
]
1313
thumbprint_list = [
14-
"6938fd4d98bab03faadb97b34396831e3780aea1"
14+
data.tls_certificate.this.certificates[0].sha1_fingerprint
1515
]
1616
url = "https://token.actions.githubusercontent.com"
1717
}
1818

19+
data "tls_certificate" "this" {
20+
url = "https://token.actions.githubusercontent.com"
21+
}
22+
1923
module "github_actions_assumable_role" {
2024
source = "./modules/github_actions_assumable_role"
2125
for_each = var.create_iam_roles ? local.github_subs_by_role : {}

modules/github_actions_assumable_role/main.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
terraform {
77
required_version = ">= 0.13.0"
88
required_providers {
9-
aws = "> 3.0, ~> 4.0"
9+
aws = "~> 4.0"
1010
}
1111
}
1212

@@ -28,5 +28,15 @@ data "aws_iam_policy_document" "this" {
2828
values = var.github_subs
2929
variable = "token.actions.githubusercontent.com:sub"
3030
}
31+
condition {
32+
test = "ForAllValues:StringEquals"
33+
values = ["sts.amazonaws.com"]
34+
variable = "token.actions.githubusercontent.com:aud"
35+
}
36+
condition {
37+
test = "ForAllValues:StringEquals"
38+
values = ["https://token.actions.githubusercontent.com"]
39+
variable = "token.actions.githubusercontent.com:iss"
40+
}
3141
}
3242
}

providers.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
terraform {
22
required_version = ">= 0.13.0"
33
required_providers {
4-
aws = "> 3.0, ~> 4.0"
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = "~> 4.0"
7+
}
8+
tls = {
9+
source = "hashicorp/tls"
10+
version = "~> 4.0"
11+
}
512
}
613
}

tflint.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ config {
66

77
plugin "aws" {
88
enabled = true
9-
version = "0.11.0"
9+
version = "0.30.0"
1010
source = "github.com/terraform-linters/tflint-ruleset-aws"
1111
}
1212

0 commit comments

Comments
 (0)