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
233 changes: 233 additions & 0 deletions .github/workflows/cdk-preview.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
name: CDK Preview
# Reusable workflow - credential-isolated CDK preview for pull requests.
#
# Job `deployed templates` (TRUSTED, has credentials) fetches the deployed CloudFormation templates of
# the named stacks with a read-only role. Job `diff` (UNTRUSTED, no credentials) checks out the pull
# request, synthesizes it, and runs `cdk diff <stack> --template <deployed>.json`. Pull request code
# therefore never runs where AWS credentials exist.
#
# Security invariants (keep them when changing this file):
# - `deployed templates` never checks out, downloads, or caches anything from the caller. Only the
# runner image's AWS CLI and jq and the commit-pinned actions below run while credentials exist.
# - `diff` has no `id-token` permission and receives only the deployed templates as data.
# - Caller inputs are validated and reach commands through environment variables or argument arrays.
# - The role is expected to trust this workflow by `job_workflow_ref` pinned to a full commit SHA, and
# to allow only `cloudformation:GetTemplate` on an allowlist of stacks. IAM is the authoritative
# allowlist; the checks here only reject malformed input early.
on:
workflow_call:
inputs:
roleArn:
description: "ARN of the read-only role to assume through GitHub OIDC"
required: true
type: string
awsRegion:
description: "AWS region of the stacks"
required: false
default: "us-east-1"
type: string
stacks:
description: >-
JSON array (1-20) of stacks to preview. Each entry: {"stack": "<deployed stack name>",
"context": {"<key>": "<value>"} (optional `cdk -c` values), "contextFromDeployed":
{"<key>": "<regex with a (?<value>...) group>"} (optional values read from the deployed
template's strings, e.g. a currently deployed image tag)}
required: true
type: string
cdkVersion:
description: "Exact aws-cdk CLI version, e.g. 2.1128.1"
required: true
type: string
dotnetVersion:
description: ".NET SDK version(s) for a C# CDK app, e.g. 11.x (leave empty with globalJsonFile, or for non-.NET apps)"
required: false
default: ""
type: string
globalJsonFile:
description: "Optional global.json for SDK selection"
required: false
default: ""
type: string
nodeVersion:
description: "Node version for the CDK CLI"
required: false
default: "lts/*"
type: string
workingDirectory:
description: "Directory containing cdk.json"
required: false
default: "."
type: string

permissions:
id-token: write
contents: read

jobs:
fetch:
name: deployed templates
if: github.event_name != 'pull_request' || github.event.pull_request.draft == false
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
id-token: write
outputs:
artifactName: ${{ steps.names.outputs.artifactName }}

steps:
- name: Validate inputs
id: names
env:
ROLE_ARN: ${{ inputs.roleArn }}
AWS_REGION_INPUT: ${{ inputs.awsRegion }}
STACKS: ${{ inputs.stacks }}
run: |
set -euo pipefail
[[ "$ROLE_ARN" =~ ^arn:aws:iam::[0-9]{12}:role/[A-Za-z0-9+=,.@_/-]{1,64}$ ]] || { echo "::error::roleArn is not an IAM role ARN."; exit 1; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Accept valid IAM role partitions and paths

For callers using China/GovCloud partitions or a role path whose combined path and name exceeds 64 characters, this validation rejects an otherwise valid role ARN before credential setup. IAM ARNs can use partitions such as aws-cn, and AWS permits a role path of up to 512 characters in addition to the 64-character role name (IAM identifier documentation, role limits); validate the partition, path, and role name separately rather than limiting the entire resource suffix.

Useful? React with 👍 / 👎.

[[ "$AWS_REGION_INPUT" =~ ^[a-z]{2}(-[a-z]+)+-[0-9]$ ]] || { echo "::error::awsRegion is not an AWS region."; exit 1; }
jq -e 'type == "array" and length >= 1 and length <= 20 and (map(type == "object" and (.stack | type) == "string") | all) and (map(.stack) | unique | length) == length' <<<"$STACKS" >/dev/null \
|| { echo "::error::stacks must be a JSON array of 1-20 objects with distinct \"stack\" names."; exit 1; }
while IFS= read -r name; do
[[ "$name" =~ ^[A-Za-z][A-Za-z0-9-]{0,127}$ ]] || { echo "::error::Invalid CloudFormation stack name: $name"; exit 1; }
done < <(jq -r '.[].stack' <<<"$STACKS")
jq -r '.[].stack' <<<"$STACKS" > "$RUNNER_TEMP/stack-names.txt"
echo "artifactName=cdk-deployed-templates-${{ github.run_id }}-${{ github.run_attempt }}" >> "$GITHUB_OUTPUT"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make artifact names unique per workflow invocation

When a caller invokes this reusable workflow more than once in a run—for example, once per role or region—every fetch job receives the same github.run_id and github.run_attempt, so they upload under the same name. upload-artifact@v7 requires artifact names to be unique within a workflow run and the later upload fails with a conflict (official action documentation); include a value unique to the individual reusable-workflow invocation.

Useful? React with 👍 / 👎.


- name: Assume read-only role
uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
with:
role-to-assume: ${{ inputs.roleArn }}
aws-region: ${{ inputs.awsRegion }}
audience: sts.amazonaws.com
role-session-name: cdk-preview-${{ github.run_id }}-${{ github.run_attempt }}
role-duration-seconds: 900
mask-aws-account-id: true

- name: Fetch deployed templates
run: |
set -euo pipefail
out="$RUNNER_TEMP/deployed-templates"
mkdir -p "$out"
while IFS= read -r name; do
# Original stage: the template as submitted. GetTemplate never resolves dynamic
# references, Fn::GetAtt values, or NoEcho parameters.
if ! aws cloudformation get-template \
--stack-name "$name" \
--template-stage Original \
--query TemplateBody \
--output json > "$out/$name.raw.json"; then
echo "::error::Could not read the deployed template of $name."
exit 1
fi
# TemplateBody is a JSON object for JSON templates and a string for YAML templates.
if jq -e 'type == "object"' "$out/$name.raw.json" >/dev/null; then
jq . "$out/$name.raw.json" > "$out/$name.json"
else
echo "::error::Stack $name has a non-JSON template; only CDK-synthesized JSON templates are supported."
exit 1
fi
rm "$out/$name.raw.json"
echo "Fetched $name ($(wc -c < "$out/$name.json") bytes)"
done < "$RUNNER_TEMP/stack-names.txt"

- name: Upload deployed templates
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: ${{ steps.names.outputs.artifactName }}
path: ${{ runner.temp }}/deployed-templates/*.json
if-no-files-found: error
retention-days: 1

diff:
name: diff
needs: fetch
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
contents: read

steps:
- name: Validate inputs
env:
STACKS: ${{ inputs.stacks }}
CDK_VERSION: ${{ inputs.cdkVersion }}
run: |
set -euo pipefail
[[ "$CDK_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "::error::cdkVersion must be an exact version such as 2.1128.1."; exit 1; }
jq -e '
map(
((.context // {}) | type == "object")
and ((.context // {}) | to_entries | map((.key | test("^[A-Za-z@][A-Za-z0-9@:._/-]*$")) and (.value | type == "string") and (.value | test("\n") | not)) | all)
and ((.contextFromDeployed // {}) | type == "object")
and ((.contextFromDeployed // {}) | to_entries | map((.key | test("^[A-Za-z@][A-Za-z0-9@:._/-]*$")) and (.value | type == "string") and (.value | contains("(?<value>"))) | all)
) | all' <<<"$STACKS" >/dev/null \
|| { echo "::error::Each stack's context values must be single-line strings, and each contextFromDeployed value a regex with a (?<value>...) group."; exit 1; }

- uses: actions/checkout@v7
with:
persist-credentials: false

- name: Setup .NET
if: inputs.dotnetVersion != '' || inputs.globalJsonFile != ''
uses: actions/setup-dotnet@v6.0.0
with:
dotnet-version: ${{ inputs.dotnetVersion }}
global-json-file: ${{ inputs.globalJsonFile }}

- uses: actions/setup-node@v7
with:
node-version: ${{ inputs.nodeVersion }}

- uses: actions/download-artifact@v8
with:
name: ${{ needs.fetch.outputs.artifactName }}
path: ${{ runner.temp }}/deployed

- name: Diff against deployed templates (no AWS credentials)
working-directory: ${{ inputs.workingDirectory }}
env:
STACKS: ${{ inputs.stacks }}
CDK_VERSION: ${{ inputs.cdkVersion }}
DEPLOYED: ${{ runner.temp }}/deployed
JSII_SILENCE_WARNING_UNTESTED_NODE_VERSION: "1"
run: |
set -euo pipefail
summary() { echo "$1" >> "$GITHUB_STEP_SUMMARY"; }
summary "## CDK preview"
failed=0
count=$(jq length <<<"$STACKS")
for i in $(seq 0 $((count - 1))); do
entry=$(jq -c ".[$i]" <<<"$STACKS")
stack=$(jq -r '.stack' <<<"$entry")
deployed="$DEPLOYED/$stack.json"
args=()
while IFS=$'\t' read -r key value; do
[[ -n "$key" ]] && args+=(-c "$key=$value")
done < <(jq -r '(.context // {}) | to_entries[] | [.key, .value] | @tsv' <<<"$entry")
while IFS=$'\t' read -r key regex; do
[[ -n "$key" ]] || continue
value=$(jq -r --arg re "$regex" '[.. | strings | capture($re)?.value] | first // empty' "$deployed")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Fix the invalid jq optional-capture expression

Whenever a stack supplies a nonempty contextFromDeployed object—including the documented README example—jq rejects capture($re)?.value with syntax error, unexpected FIELD. Because the step uses set -e, it aborts before running any CDK diff; the optional result must be parenthesized or piped to .value using valid jq syntax.

Useful? React with 👍 / 👎.

if [[ -z "$value" ]]; then
echo "::error::$stack: no deployed template string matched contextFromDeployed.$key."
failed=1
continue 2
fi
args+=(-c "$key=$value")
done < <(jq -r '(.contextFromDeployed // {}) | to_entries[] | [.key, .value] | @tsv' <<<"$entry")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve regex escapes when reading deployed contexts

When a contextFromDeployed regex contains a backslash, as common patterns such as (?<value>\d+) do, @tsv escapes that backslash and read -r intentionally leaves the escape doubled. The altered regex is then passed to jq and fails to match the intended template string; use a transport that round-trips arbitrary JSON strings instead of TSV. The same serialization pattern on line 206 also alters direct context values containing backslashes or tabs.

Useful? React with 👍 / 👎.


echo "::group::cdk diff $stack"
if output=$(npx --yes "aws-cdk@$CDK_VERSION" diff "$stack" --template "$deployed" "${args[@]}" 2>&1); then
result=""
else
result=" (failed)"
failed=1
echo "::error::cdk diff failed for $stack"
fi
echo "$output"
echo "::endgroup::"
summary "### \`$stack\`$result"
summary '```'
summary "$(grep -vE '^\s*$|JSII|!!' <<<"$output" | tail -n 200)"
summary '```'
done
exit "$failed"
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,43 @@ workflow definition; never put secrets there.
- **Node.js validation.** Use `nodeProjects` to run lint/type-check/test/build scripts; `spaSites`
remains for build-output packaging.

## CDK Preview (`cdk-preview.yaml`)

A credential-isolated CDK diff for pull requests, in two jobs:

- `deployed templates` (credentials): assumes a read-only role through GitHub OIDC and downloads the
deployed CloudFormation templates of the named stacks (`GetTemplate`, original stage). It never
checks out, downloads, or runs anything from the caller.
- `diff` (no credentials): checks out the pull request and runs
`cdk diff <stack> --template <deployed>.json`, writing each result to the job summary.

```yaml
permissions:
contents: read

jobs:
cdk-preview:
uses: LayeredCraft/devops-templates/.github/workflows/cdk-preview.yaml@<commit SHA>
permissions:
id-token: write
contents: read
with:
roleArn: arn:aws:iam::123456789012:role/my-cdk-preview
cdkVersion: 2.1128.1
dotnetVersion: 11.x
stacks: >-
[{"stack":"my-app-dev","context":{"env":"dev"}},
{"stack":"my-app-web","contextFromDeployed":{"imageTag":":web-(?<value>[0-9a-f]+)$"}}]
```

Each `stacks` entry names a deployed stack, optional `context` (`cdk -c key=value`), and optional
`contextFromDeployed` (a regex with a `(?<value>...)` group matched against the deployed template's
strings, e.g. to keep the currently deployed image tag so only infrastructure changes show).

Reference it by **commit SHA**: the role should trust exactly this workflow revision through the
`job_workflow_ref` claim (plus `aud`, `sub`, and `repository_id`) and allow only
`cloudformation:GetTemplate` on the listed stacks. IAM is the authoritative stack allowlist.

## Draft Pull Requests

Draft pull requests run only lightweight feedback workflows. `pr-title-check.yml` can still run on draft PRs, but workflows that build, deploy, publish packages, tag commits, or update release metadata skip draft PRs.
Expand Down
Loading