From f0d89e5162e5aa311a592018ff30496603c4f913 Mon Sep 17 00:00:00 2001 From: Nick Cipollina Date: Tue, 13 Oct 2026 15:55:42 -0400 Subject: [PATCH 1/2] feat(github): add credential-isolated CDK deployed-templates workflow Add a reusable workflow that assumes a read-only role through GitHub OIDC, fetches the deployed CloudFormation templates of named stacks, and uploads them as an artifact. It never checks out or runs anything from the caller, so callers can compare pull request synthesis against deployed state with cdk diff --template in a separate job that has no credentials. Inputs are validated before use and reach commands only through environment variables; actions are pinned to commit SHAs. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/cdk-deployed-templates.yaml | 116 ++++++++++++++++++ README.md | 35 ++++++ 2 files changed, 151 insertions(+) create mode 100644 .github/workflows/cdk-deployed-templates.yaml diff --git a/.github/workflows/cdk-deployed-templates.yaml b/.github/workflows/cdk-deployed-templates.yaml new file mode 100644 index 0000000..f1d8a94 --- /dev/null +++ b/.github/workflows/cdk-deployed-templates.yaml @@ -0,0 +1,116 @@ +name: CDK Deployed Templates +# Reusable workflow - the TRUSTED half of a credential-isolated CDK preview. +# +# Fetches the deployed CloudFormation templates of named stacks with a read-only role and uploads them +# as an artifact. A separate job WITHOUT credentials then synthesizes the pull request's code and runs +# `cdk diff --template .json`, so pull request code never runs where credentials exist. +# +# Security invariants (keep them when changing this file): +# - No checkout and no download of caller artifacts or caches: nothing from the caller's repository +# or pull request runs or is read here. Only the runner image's AWS CLI and jq, and the pinned +# actions below, execute. +# - Caller inputs are validated before use and passed to commands through environment variables. +# - 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 stack-name check here only rejects 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 + stackNames: + description: 'JSON array of deployed stack names, e.g. ["my-app-dev","my-app-dns"] (at most 20)' + required: true + type: string + artifactName: + description: "Name of the uploaded artifact holding .json files" + required: false + default: "cdk-deployed-templates" + type: string + outputs: + artifactName: + description: "Name of the uploaded artifact" + value: ${{ inputs.artifactName }} + +permissions: + id-token: write + +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 + + steps: + - name: Validate inputs + env: + ROLE_ARN: ${{ inputs.roleArn }} + AWS_REGION_INPUT: ${{ inputs.awsRegion }} + STACK_NAMES: ${{ inputs.stackNames }} + ARTIFACT_NAME: ${{ inputs.artifactName }} + 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; } + [[ "$AWS_REGION_INPUT" =~ ^[a-z]{2}(-[a-z]+)+-[0-9]$ ]] || { echo "::error::awsRegion is not an AWS region."; exit 1; } + [[ "$ARTIFACT_NAME" =~ ^[A-Za-z0-9._-]{1,100}$ ]] || { echo "::error::artifactName may contain only letters, digits, '.', '_' and '-'."; exit 1; } + jq -e 'type == "array" and length >= 1 and length <= 20 and (map(type == "string") | all) and (unique | length) == length' <<<"$STACK_NAMES" >/dev/null \ + || { echo "::error::stackNames must be a JSON array of 1-20 distinct strings."; 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_NAMES") + jq -r '.[]' <<<"$STACK_NAMES" > "$RUNNER_TEMP/stack-names.txt" + + - 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-templates-${{ 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: ${{ inputs.artifactName }} + path: ${{ runner.temp }}/deployed-templates/*.json + if-no-files-found: error + retention-days: 1 diff --git a/README.md b/README.md index 415ffff..315768a 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,41 @@ workflow definition; never put secrets there. values to keep previous behavior. - **Node.js validation.** Use `nodeProjects` to run lint/type-check/test/build scripts; `spaSites` remains for build-output packaging. +## CDK Deployed Templates (`cdk-deployed-templates.yaml`) + +The trusted half of a credential-isolated CDK preview. It assumes a read-only role through GitHub +OIDC, downloads the deployed CloudFormation templates of the named stacks (`GetTemplate`, original +stage), and uploads them as an artifact. It never checks out, downloads, or runs anything from the +caller, so pull request code never runs where credentials exist. A separate job without +credentials synthesizes the pull request and compares with `cdk diff --template `. + +```yaml +jobs: + deployed-templates: + uses: LayeredCraft/devops-templates/.github/workflows/cdk-deployed-templates.yaml@ + permissions: + id-token: write + with: + roleArn: arn:aws:iam::123456789012:role/my-cdk-preview + stackNames: '["my-app-dev","my-app-dns"]' + + cdk-diff: + needs: deployed-templates + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@ + - uses: actions/download-artifact@ + with: + name: ${{ needs.deployed-templates.outputs.artifactName }} + path: deployed + - run: npx aws-cdk@ diff my-app-dev --template deployed/my-app-dev.json +``` + +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 From d8a0aa3e0f17ede6d3755d2cd97cf718db3a8856 Mon Sep 17 00:00:00 2001 From: Nick Cipollina Date: Tue, 13 Oct 2026 16:13:40 -0400 Subject: [PATCH 2/2] feat(github): turn the deployed-templates workflow into a complete CDK preview Rename cdk-deployed-templates.yaml to cdk-preview.yaml and add the credential-free diff job: it checks out the pull request, synthesizes it, and runs cdk diff against each deployed template, reporting every stack in the job summary. Per-stack context values can be fixed or read from the deployed template (for example the currently deployed image tag), so callers need a single reusable-workflow reference. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/cdk-deployed-templates.yaml | 116 --------- .github/workflows/cdk-preview.yaml | 233 ++++++++++++++++++ README.md | 46 ++-- 3 files changed, 257 insertions(+), 138 deletions(-) delete mode 100644 .github/workflows/cdk-deployed-templates.yaml create mode 100644 .github/workflows/cdk-preview.yaml diff --git a/.github/workflows/cdk-deployed-templates.yaml b/.github/workflows/cdk-deployed-templates.yaml deleted file mode 100644 index f1d8a94..0000000 --- a/.github/workflows/cdk-deployed-templates.yaml +++ /dev/null @@ -1,116 +0,0 @@ -name: CDK Deployed Templates -# Reusable workflow - the TRUSTED half of a credential-isolated CDK preview. -# -# Fetches the deployed CloudFormation templates of named stacks with a read-only role and uploads them -# as an artifact. A separate job WITHOUT credentials then synthesizes the pull request's code and runs -# `cdk diff --template .json`, so pull request code never runs where credentials exist. -# -# Security invariants (keep them when changing this file): -# - No checkout and no download of caller artifacts or caches: nothing from the caller's repository -# or pull request runs or is read here. Only the runner image's AWS CLI and jq, and the pinned -# actions below, execute. -# - Caller inputs are validated before use and passed to commands through environment variables. -# - 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 stack-name check here only rejects 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 - stackNames: - description: 'JSON array of deployed stack names, e.g. ["my-app-dev","my-app-dns"] (at most 20)' - required: true - type: string - artifactName: - description: "Name of the uploaded artifact holding .json files" - required: false - default: "cdk-deployed-templates" - type: string - outputs: - artifactName: - description: "Name of the uploaded artifact" - value: ${{ inputs.artifactName }} - -permissions: - id-token: write - -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 - - steps: - - name: Validate inputs - env: - ROLE_ARN: ${{ inputs.roleArn }} - AWS_REGION_INPUT: ${{ inputs.awsRegion }} - STACK_NAMES: ${{ inputs.stackNames }} - ARTIFACT_NAME: ${{ inputs.artifactName }} - 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; } - [[ "$AWS_REGION_INPUT" =~ ^[a-z]{2}(-[a-z]+)+-[0-9]$ ]] || { echo "::error::awsRegion is not an AWS region."; exit 1; } - [[ "$ARTIFACT_NAME" =~ ^[A-Za-z0-9._-]{1,100}$ ]] || { echo "::error::artifactName may contain only letters, digits, '.', '_' and '-'."; exit 1; } - jq -e 'type == "array" and length >= 1 and length <= 20 and (map(type == "string") | all) and (unique | length) == length' <<<"$STACK_NAMES" >/dev/null \ - || { echo "::error::stackNames must be a JSON array of 1-20 distinct strings."; 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_NAMES") - jq -r '.[]' <<<"$STACK_NAMES" > "$RUNNER_TEMP/stack-names.txt" - - - 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-templates-${{ 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: ${{ inputs.artifactName }} - path: ${{ runner.temp }}/deployed-templates/*.json - if-no-files-found: error - retention-days: 1 diff --git a/.github/workflows/cdk-preview.yaml b/.github/workflows/cdk-preview.yaml new file mode 100644 index 0000000..5599e36 --- /dev/null +++ b/.github/workflows/cdk-preview.yaml @@ -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 --template .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": "", + "context": {"": ""} (optional `cdk -c` values), "contextFromDeployed": + {"": "...) 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; } + [[ "$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" + + - 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("(?"))) | 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 (?...) 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") + 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") + + 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" diff --git a/README.md b/README.md index 315768a..c05566b 100644 --- a/README.md +++ b/README.md @@ -80,38 +80,40 @@ workflow definition; never put secrets there. values to keep previous behavior. - **Node.js validation.** Use `nodeProjects` to run lint/type-check/test/build scripts; `spaSites` remains for build-output packaging. -## CDK Deployed Templates (`cdk-deployed-templates.yaml`) -The trusted half of a credential-isolated CDK preview. It assumes a read-only role through GitHub -OIDC, downloads the deployed CloudFormation templates of the named stacks (`GetTemplate`, original -stage), and uploads them as an artifact. It never checks out, downloads, or runs anything from the -caller, so pull request code never runs where credentials exist. A separate job without -credentials synthesizes the pull request and compares with `cdk diff --template `. +## 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 --template .json`, writing each result to the job summary. ```yaml +permissions: + contents: read + jobs: - deployed-templates: - uses: LayeredCraft/devops-templates/.github/workflows/cdk-deployed-templates.yaml@ + cdk-preview: + uses: LayeredCraft/devops-templates/.github/workflows/cdk-preview.yaml@ permissions: id-token: write + contents: read with: roleArn: arn:aws:iam::123456789012:role/my-cdk-preview - stackNames: '["my-app-dev","my-app-dns"]' - - cdk-diff: - needs: deployed-templates - runs-on: ubuntu-latest - permissions: - contents: read - steps: - - uses: actions/checkout@ - - uses: actions/download-artifact@ - with: - name: ${{ needs.deployed-templates.outputs.artifactName }} - path: deployed - - run: npx aws-cdk@ diff my-app-dev --template deployed/my-app-dev.json + cdkVersion: 2.1128.1 + dotnetVersion: 11.x + stacks: >- + [{"stack":"my-app-dev","context":{"env":"dev"}}, + {"stack":"my-app-web","contextFromDeployed":{"imageTag":":web-(?[0-9a-f]+)$"}}] ``` +Each `stacks` entry names a deployed stack, optional `context` (`cdk -c key=value`), and optional +`contextFromDeployed` (a regex with a `(?...)` 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.