Skip to content

Commit 4c154d2

Browse files
committed
Source blog post 1
0 parents  commit 4c154d2

File tree

9 files changed

+284
-0
lines changed

9 files changed

+284
-0
lines changed

Diff for: .github/workflows/debug-oidc.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Debug OIDC
2+
on:
3+
workflow_dispatch:
4+
5+
jobs:
6+
oidc_debug:
7+
permissions:
8+
contents: read
9+
id-token: write
10+
actions: write
11+
runs-on: ubuntu-latest
12+
name: A test of the oidc debugger
13+
steps:
14+
- name: Debug OIDC Claims
15+
uses: github/actions-oidc-debugger@main
16+
with:
17+
audience: 'https://github.com/github'

Diff for: .github/workflows/s3.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Blog example S3 (part 1)
2+
on:
3+
workflow_dispatch:
4+
5+
jobs:
6+
deploy:
7+
permissions:
8+
id-token: write
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/setup-node@v3
12+
with:
13+
node-version: 16
14+
15+
- name: configure aws credentials
16+
uses: aws-actions/configure-aws-credentials@v1-node16
17+
with:
18+
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/github-actions/blog
19+
role-session-name: gh-actions
20+
aws-region: eu-west-1
21+
22+
- name: deploy
23+
run: |
24+
npx cowsay -f ghostbusters "Running ${{ github.workflow }}" > message.txt
25+
aws s3 cp message.txt s3://${{ github.repository_owner }}-${{ github.event.repository.name }}/${{ github.run_id }}.txt
26+
rm message.txt
27+
28+
- name: check
29+
run: |
30+
aws s3 cp s3://${{ github.repository_owner }}-${{ github.event.repository.name }}/${{ github.run_id }}.txt result.txt
31+
cat result.txt

Diff for: .gitignore

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/terraform
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=terraform
3+
4+
### Terraform ###
5+
# Local .terraform directories
6+
**/.terraform/*
7+
8+
# .tfstate files
9+
*.tfstate
10+
*.tfstate.*
11+
12+
# Crash log files
13+
crash.log
14+
crash.*.log
15+
16+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
17+
# password, private keys, and other secrets. These should not be part of version
18+
# control as they are data points which are potentially sensitive and subject
19+
# to change depending on the environment.
20+
*.tfvars
21+
*.tfvars.json
22+
23+
# Ignore override files as they are usually used to override resources locally and so
24+
# are not checked in
25+
override.tf
26+
override.tf.json
27+
*_override.tf
28+
*_override.tf.json
29+
30+
# Include override files you do wish to add to version control using negated pattern
31+
# !example_override.tf
32+
33+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
34+
# example: *tfplan*
35+
36+
# Ignore CLI configuration files
37+
.terraformrc
38+
terraform.rc
39+
40+
# End of https://www.toptal.com/developers/gitignore/api/terraform
41+
n

Diff for: terraform/part1/.terraform.lock.hcl

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: terraform/part1/main.tf

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
data "tls_certificate" "github_actions" {
2+
url = var.github_actions_tls_certificate
3+
}
4+
5+
resource "aws_iam_openid_connect_provider" "github_actions" {
6+
url = var.github_actions_tls_certificate
7+
client_id_list = ["sts.amazonaws.com"]
8+
thumbprint_list = data.tls_certificate.github_actions.certificates.*.sha1_fingerprint
9+
}
10+
11+
data "aws_iam_policy_document" "github_actions_trusted_identity" {
12+
13+
dynamic "statement" {
14+
for_each = length(var.principals) > 0 ? ["1"] : []
15+
content {
16+
actions = ["sts:AssumeRole"]
17+
principals {
18+
type = "AWS"
19+
identifiers = var.principals
20+
}
21+
}
22+
}
23+
24+
statement {
25+
actions = ["sts:AssumeRoleWithWebIdentity"]
26+
principals {
27+
type = "Federated"
28+
identifiers = [aws_iam_openid_connect_provider.github_actions.arn]
29+
}
30+
31+
condition {
32+
test = "ForAllValues:StringEquals"
33+
variable = "token.actions.githubusercontent.com:aud"
34+
values = ["sts.amazonaws.com", var.github_actions_tls_certificate]
35+
}
36+
37+
condition {
38+
test = "StringLike"
39+
variable = "token.actions.githubusercontent.com:sub"
40+
values = ["repo:${var.repo}:ref:refs/heads/main*"]
41+
}
42+
}
43+
}
44+
45+
resource "aws_iam_role" "github_actions" {
46+
name = var.role.name
47+
path = var.role.path
48+
assume_role_policy = data.aws_iam_policy_document.github_actions_trusted_identity.json
49+
}
50+
51+
resource "aws_iam_role_policy" "s3" {
52+
name = "s3-policy"
53+
role = aws_iam_role.github_actions.name
54+
policy = data.aws_iam_policy_document.s3.json
55+
}
56+
57+
data "aws_iam_policy_document" "s3" {
58+
statement {
59+
sid = "1"
60+
61+
actions = [
62+
"s3:ListBucket",
63+
"s3:GetObject",
64+
"s3:PutObject"
65+
]
66+
67+
resources = [
68+
aws_s3_bucket.blog.arn, "${aws_s3_bucket.blog.arn}*"
69+
]
70+
}
71+
}
72+
73+
resource "random_uuid" "main" {
74+
}
75+
76+
resource "aws_s3_bucket" "blog" {
77+
bucket = replace(var.repo, "/", "-")
78+
force_destroy = true
79+
}

Diff for: terraform/part1/outputs.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "role" {
2+
value = aws_iam_role.github_actions.arn
3+
}
4+
5+
output "bucket" {
6+
value = aws_s3_bucket.blog.id
7+
}

Diff for: terraform/part1/provider.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
region = var.aws_region
3+
}

Diff for: terraform/part1/variables.tf

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
variable "aws_region" {
2+
type = string
3+
default = "eu-west-1"
4+
}
5+
6+
variable "github_actions_tls_certificate" {
7+
type = string
8+
default = "https://token.actions.githubusercontent.com"
9+
}
10+
11+
variable "principals" {
12+
type = list(string)
13+
default = ["arn:aws:iam::557218779171:user/niek"]
14+
}
15+
16+
variable "repo" {
17+
description = "Format, org/repo. The repo will also used to create an s3 bukcet where the / is replaced by -."
18+
type = string
19+
default = "040code/blog-oidc-github-actions-aws"
20+
}
21+
22+
variable "role" {
23+
type = object({
24+
name: string
25+
path: string
26+
})
27+
default = {
28+
name = "blog"
29+
path = "/github-actions/"
30+
}
31+
}

Diff for: terraform/part1/versions.tf

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 4.0"
6+
}
7+
tls = {
8+
source = "hashicorp/tls"
9+
version = "~> 4.0"
10+
}
11+
}
12+
required_version = ">= 1.3.0"
13+
}

0 commit comments

Comments
 (0)