Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ Format based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
Sentinel, a custom script), checked against the JSON plan at both plan-time
and apply-time. Nonzero exit fails the run. See the README's "Policy gate"
section for the `IAC_PLAN_JSON` contract and why it checks twice.
- `aws-role-arn` and `aws-role-session-name` inputs: OIDC auth for
`backend: s3`, assuming an IAM role via `aws-actions/configure-aws-credentials`
instead of static access keys. Additive to `access-key-id`/`secret-access-key`
as inputs, not a replacement, setting both fails loud by design. `s3` only,
R2 has no OIDC equivalent. See the README's "OIDC for AWS S3" section for
the trust policy and the required `permissions: id-token: write`.
77 changes: 75 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,77 @@ already has elsewhere in this action. Keep it static workflow config.
Never build it from PR-controlled input like a title, label, or comment,
there's no quoting or sanitization boundary here.

## OIDC for AWS S3

For `backend: s3`, you can skip long-lived AWS keys and assume an IAM role
through GitHub's OIDC token instead. Set `aws-role-arn` (and optionally
`aws-role-session-name`) and `iac-cicd` assumes that role via
`aws-actions/configure-aws-credentials` before running `init`. This only
applies to `backend: s3`, R2 has no OIDC equivalent, it authenticates
through its own API token.

`aws-role-arn` is additive to `access-key-id`/`secret-access-key` as
inputs, not a replacement for them: its presence is what selects OIDC auth
over static keys. If you set both `aws-role-arn` and the static keys, the
action fails loud with a validation error instead of silently picking one.
That's by design.

The calling job needs `permissions: id-token: write`. `iac-cicd` can't
grant that permission on your behalf, GitHub permissions are set at the
workflow or job level only, an action included in a job has no way to add
scopes to it. If that permission is missing,
`aws-actions/configure-aws-credentials` fails at runtime with AWS's own
`AssumeRoleWithWebIdentity` error, not a message from this action.

You also need the GitHub OIDC provider already registered in your AWS
account. That's a one-time AWS account setup step this action doesn't
perform for you, see
[GitHub's docs on configuring OpenID Connect in AWS](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services)
for how to set it up.

A trust policy scoped to the consumer repo looks like this:

```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::<ACCOUNT_ID>:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:<owner>/<repo>:*"
}
}
}
]
}
```

Minimal `with:` block for OIDC:

```yaml
permissions:
id-token: write

steps:
- uses: zeroconfigio/github-action-iac-cicd@v1
with:
backend: s3
region: us-east-1
aws-role-arn: arn:aws:iam::123456789012:role/iac-cicd-deploy
bucket: ${{ secrets.AWS_BUCKET }}
```

No `access-key-id`/`secret-access-key` needed, the role is assumed
directly.

## Inputs

| Input | Required | Default | Description |
Expand All @@ -165,8 +236,10 @@ there's no quoting or sanitization boundary here.
| `command` | no | auto | Override auto-detect (`apply` on `push`, `plan` otherwise) |
| `backend` | no | `r2` | `r2` (Cloudflare R2) or `s3` (AWS S3) |
| `bucket` | yes | | Bucket for state |
| `access-key-id` | yes | | S3-compatible access key ID |
| `secret-access-key` | yes | | S3-compatible secret access key |
| `access-key-id` | required unless `aws-role-arn` is set (`s3`), or always (`r2`) | | S3-compatible access key ID |
| `secret-access-key` | required unless `aws-role-arn` is set (`s3`), or always (`r2`) | | S3-compatible secret access key |
| `aws-role-arn` | no | | IAM role ARN to assume via OIDC, `s3` only, see [OIDC for AWS S3](#oidc-for-aws-s3) |
| `aws-role-session-name` | no | `iac-cicd` | Session name used when assuming `aws-role-arn` |
| `account-id` | required for `r2` | | Cloudflare account ID (ignored for `s3`, or when `endpoint` is set) |
| `region` | required for `s3` | `auto` for `r2` | Bucket region |
| `endpoint` | no | derived | Override the S3-compatible endpoint URL |
Expand Down
71 changes: 58 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,20 @@ inputs:
required: true
access-key-id:
description: 'S3-compatible API access key ID.'
required: true
required: false
default: ''
secret-access-key:
description: 'S3-compatible API secret access key.'
required: true
required: false
default: ''
aws-role-arn:
description: "IAM role ARN to assume via OIDC. Only meaningful when backend is 's3'. Presence selects OIDC auth over static access-key-id/secret-access-key."
required: false
default: ''
aws-role-session-name:
description: 'Session name used when assuming aws-role-arn via OIDC.'
required: false
default: 'iac-cicd'
account-id:
description: "Cloudflare account ID that owns the bucket. Required when backend is 'r2', unless endpoint is set explicitly."
required: false
Expand Down Expand Up @@ -106,7 +116,15 @@ runs:
endpoint="${{ inputs.endpoint }}"
region="${{ inputs.region }}"

access_key_id="${{ inputs.access-key-id }}"
secret_access_key="${{ inputs.secret-access-key }}"
aws_role_arn="${{ inputs.aws-role-arn }}"

if [ "$backend" = "r2" ]; then
if [ -n "$aws_role_arn" ]; then
echo "::error::aws-role-arn is only meaningful when backend is 's3', r2 has no OIDC equivalent"
exit 1
fi
if [ -z "$endpoint" ]; then
if [ -z "${{ inputs.account-id }}" ]; then
echo "::error::account-id is required when backend is 'r2', unless endpoint is set explicitly"
Expand All @@ -118,6 +136,11 @@ runs:
use_path_style=true
skip_checks=true
encrypt=false
if [ -z "$access_key_id" ] || [ -z "$secret_access_key" ]; then
echo "::error::access-key-id and secret-access-key are both required when backend is 'r2'"
exit 1
fi
auth_mode=static
else
if [ -z "$region" ]; then
echo "::error::region is required when backend is 's3'"
Expand All @@ -126,13 +149,28 @@ runs:
use_path_style=false
skip_checks=false
encrypt=true

if [ -n "$aws_role_arn" ]; then
if [ -n "$access_key_id" ] || [ -n "$secret_access_key" ]; then
echo "::error::set either aws-role-arn or access-key-id/secret-access-key for backend 's3', not both"
exit 1
fi
auth_mode=oidc
else
if [ -z "$access_key_id" ] || [ -z "$secret_access_key" ]; then
echo "::error::access-key-id and secret-access-key are both required for backend 's3' unless aws-role-arn is set for OIDC"
exit 1
fi
auth_mode=static
fi
fi

echo "endpoint=$endpoint" >> "$GITHUB_OUTPUT"
echo "region=$region" >> "$GITHUB_OUTPUT"
echo "use-path-style=$use_path_style" >> "$GITHUB_OUTPUT"
echo "skip-checks=$skip_checks" >> "$GITHUB_OUTPUT"
echo "encrypt=$encrypt" >> "$GITHUB_OUTPUT"
echo "auth-mode=$auth_mode" >> "$GITHUB_OUTPUT"

- name: Set up OpenTofu
if: inputs.tool == 'opentofu'
Expand Down Expand Up @@ -207,12 +245,25 @@ runs:
echo "encrypt = ${{ steps.validate.outputs.encrypt }}"
} > "$RUNNER_TEMP/iac-cicd-backend.hcl"

- name: Configure credentials (OIDC)
if: steps.validate.outputs.auth-mode == 'oidc'
uses: aws-actions/configure-aws-credentials@cbe3b392738ccf3f987d68400dafcf4b0624a56c # v6.2.4
with:
role-to-assume: ${{ inputs.aws-role-arn }}
role-session-name: ${{ inputs.aws-role-session-name }}
aws-region: ${{ steps.validate.outputs.region }}

- name: Configure credentials (static)
if: steps.validate.outputs.auth-mode == 'static'
shell: bash
run: |
set -euo pipefail
echo "AWS_ACCESS_KEY_ID=${{ inputs.access-key-id }}" >> "$GITHUB_ENV"
echo "AWS_SECRET_ACCESS_KEY=${{ inputs.secret-access-key }}" >> "$GITHUB_ENV"

- name: Init
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
AWS_ACCESS_KEY_ID: ${{ inputs.access-key-id }}
AWS_SECRET_ACCESS_KEY: ${{ inputs.secret-access-key }}
run: |
set -euo pipefail
"${{ steps.resolve.outputs.bin }}" init -input=false \
Expand All @@ -223,9 +274,6 @@ runs:
id: plan
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
AWS_ACCESS_KEY_ID: ${{ inputs.access-key-id }}
AWS_SECRET_ACCESS_KEY: ${{ inputs.secret-access-key }}
run: |
# keep in sync with the Apply step's plan+show sequence below
set -euo pipefail
Expand Down Expand Up @@ -258,7 +306,7 @@ runs:
IAC_PLAN_JSON: ${{ runner.temp }}/iac-cicd-tfplan.json
run: |
set -euo pipefail
if ! (unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY; ${{ inputs.policy-command }}); then
if ! (unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN; ${{ inputs.policy-command }}); then
echo "::error::policy check failed"
exit 1
fi
Expand Down Expand Up @@ -310,9 +358,6 @@ runs:
id: apply
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
AWS_ACCESS_KEY_ID: ${{ inputs.access-key-id }}
AWS_SECRET_ACCESS_KEY: ${{ inputs.secret-access-key }}
run: |
# keep in sync with the Plan step's plan+show sequence above
set -euo pipefail
Expand All @@ -332,7 +377,7 @@ runs:

if [ -n "${{ inputs.policy-command }}" ]; then
export IAC_PLAN_JSON="$RUNNER_TEMP/iac-cicd-tfplan.json"
if ! (unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY; ${{ inputs.policy-command }}); then
if ! (unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN; ${{ inputs.policy-command }}); then
echo "::error::policy check failed"
exit 1
fi
Expand Down
41 changes: 38 additions & 3 deletions examples/plan-apply.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
# Required repo secrets: R2_ACCOUNT_ID, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_BUCKET.
# Required repo setup: Settings -> Environments -> "production" -> add required reviewers.
#
# This example uses the default backend (Cloudflare R2). For AWS S3, add
# `backend: s3` and `region: <region>` to both `with:` blocks and swap the
# R2 secrets for AWS ones, see the README's Quickstart for the full diff.
# This example uses the default backend (Cloudflare R2). For AWS S3 with
# static keys, add `backend: s3` and `region: <region>` to both `with:`
# blocks and swap the R2 secrets for AWS ones, see the README's Quickstart
# for the full diff. For AWS S3 with OIDC (no long-lived keys at all), see
# the commented-out alternate `with:` and `permissions:` blocks below each
# job's active `with:` block, and the README's "OIDC for AWS S3" section.

name: iac

Expand Down Expand Up @@ -48,6 +51,23 @@ jobs:
secret-access-key: ${{ secrets.R2_SECRET_ACCESS_KEY }}
bucket: ${{ secrets.R2_BUCKET }}

# AWS S3 with OIDC, alternate to the `with:` block above. Needs
# `id-token: write` added to this job's `permissions:` above (not
# shown active here so this file keeps running against R2). No
# access-key-id/secret-access-key: the role is assumed directly.
#
# permissions:
# contents: read
# pull-requests: write
# id-token: write
#
# - uses: zeroconfigio/github-action-iac-cicd@v1
# with:
# backend: s3
# region: us-east-1
# aws-role-arn: arn:aws:iam::123456789012:role/iac-cicd-deploy
# bucket: ${{ secrets.AWS_BUCKET }}

apply:
if: github.event_name == 'push'
runs-on: ubuntu-latest
Expand All @@ -63,3 +83,18 @@ jobs:
access-key-id: ${{ secrets.R2_ACCESS_KEY_ID }}
secret-access-key: ${{ secrets.R2_SECRET_ACCESS_KEY }}
bucket: ${{ secrets.R2_BUCKET }}

# AWS S3 with OIDC, same idea as the plan job above: add
# `id-token: write` to this job's `permissions:` too, apply assumes
# the role independently of the plan run.
#
# permissions:
# contents: read
# id-token: write
#
# - uses: zeroconfigio/github-action-iac-cicd@v1
# with:
# backend: s3
# region: us-east-1
# aws-role-arn: arn:aws:iam::123456789012:role/iac-cicd-deploy
# bucket: ${{ secrets.AWS_BUCKET }}
Loading