diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml index e09275f0..c6517808 100644 --- a/.github/actionlint.yaml +++ b/.github/actionlint.yaml @@ -1,6 +1,15 @@ --- # actionlint configuration: https://github.com/rhysd/actionlint/blob/main/docs/config.md paths: + .github/workflows/apm-sync.yml: + ignore: + - 'property "workflow_(repository|sha)" is not defined in object type' + .github/workflows/autofix.yml: + ignore: + - 'property "workflow_(repository|sha)" is not defined in object type' + .github/workflows/config-sync.yml: + ignore: + - 'property "workflow_(repository|sha)" is not defined in object type' .github/workflows/label-sync.yml: ignore: # Same job.workflow_* gap as lint.yml below. @@ -21,3 +30,6 @@ paths: ignore: # Same job.workflow_* gap as lint.yml above. - 'property "workflow_(repository|sha)" is not defined in object type' + .github/workflows/vendored-file-sync.yml: + ignore: + - 'property "workflow_(repository|sha)" is not defined in object type' diff --git a/.github/workflows/apm-materialize.yml b/.github/workflows/apm-materialize.yml index f188b9e3..a2de6318 100644 --- a/.github/workflows/apm-materialize.yml +++ b/.github/workflows/apm-materialize.yml @@ -74,31 +74,18 @@ jobs: - name: Check for apm.yml id: precheck - run: | - if [ ! -f apm.yml ]; then - echo "::notice::No apm.yml in this repo; nothing to materialize." - echo "skip=true" >> "${GITHUB_OUTPUT}" - else - echo "skip=false" >> "${GITHUB_OUTPUT}" - fi + run: bash scripts/apm-materialize/check-manifest.sh - name: Install apm CLI if: steps.precheck.outputs.skip != 'true' env: # renovate: datasource=pypi depName=apm-cli apm_cli_version: "0.35.0" - run: | - set -euo pipefail - # ubuntu-24.04 ships python3 + pip; install the pinned apm-cli for this user. - python3 -m pip install --user "apm-cli==${apm_cli_version}" - echo "${HOME}/.local/bin" >> "${GITHUB_PATH}" + run: bash scripts/apm-materialize/install-apm.sh - name: Materialize primitives (apm install) if: steps.precheck.outputs.skip != 'true' - run: | - set -euo pipefail - apm --version - apm install + run: bash scripts/apm-materialize/install.sh - name: Refresh config-sync copies of the ai-toolkit instructions # Keeps config-sync/files/.github/instructions/ in step with the diff --git a/.github/workflows/apm-sync.yml b/.github/workflows/apm-sync.yml index efc810c4..39a8bd8b 100644 --- a/.github/workflows/apm-sync.yml +++ b/.github/workflows/apm-sync.yml @@ -115,6 +115,10 @@ jobs: - name: Resolve GitHub App credentials id: app + # This step reads the App key, so tests/app-key-confinement.py pins its + # `run:` by SHA-256 and lets no step but harden-runner run before it. A + # script fetched by an earlier step would escape both checks (#537). + # lint: allow-inline-script reviewed App-key emptiness check, pinned by tests/app-key-confinement.py env: APP_ID: ${{ inputs.app-id || vars.AUTOMATION_APP_ID }} APP_PRIVATE_KEY: ${{ secrets.app-private-key || secrets.AUTOMATION_APP_PRIVATE_KEY }} @@ -142,6 +146,24 @@ jobs: fi echo "::warning title=APM Sync falls back to GITHUB_TOKEN::${reason} GITHUB_TOKEN can only open the PR when 'Allow GitHub Actions to create and approve pull requests' is enabled, and that PR does not trigger CI. See https://github.com/DevSecNinja/.github/blob/main/docs/automation-app.md" + - name: Checkout apm-sync scripts + # The apm-sync scripts ship with this reusable workflow: fetch them + # from the same commit of DevSecNinja/.github that the caller pinned. + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: ${{ job.workflow_repository }} + ref: ${{ job.workflow_sha }} + path: .devsecninja-apm-sync + sparse-checkout: |- + scripts/apm-sync/ + sparse-checkout-cone-mode: false + persist-credentials: false + + - name: Move apm-sync scripts out of the workspace + # Outside the workspace the sync PR can't pick them up, and a code + # checkout doesn't delete them. + run: mv .devsecninja-apm-sync "${RUNNER_TEMP}/devsecninja-apm-sync" + - name: Generate App token id: app-token if: steps.app.outputs.available == 'true' @@ -165,13 +187,7 @@ jobs: - name: Check for apm.yml id: precheck - run: | - if [ ! -f apm.yml ]; then - echo "::notice::No apm.yml in this repo; nothing to sync." - echo "skip=true" >> "${GITHUB_OUTPUT}" - else - echo "skip=false" >> "${GITHUB_OUTPUT}" - fi + run: bash "${RUNNER_TEMP}/devsecninja-apm-sync/scripts/apm-sync/check-manifest.sh" - name: Install apm CLI if: steps.precheck.outputs.skip != 'true' @@ -179,20 +195,13 @@ jobs: VERSION: ${{ inputs.apm-version }} APM_INSTALL_DIR: ${{ runner.temp }}/apm/bin APM_LIB_DIR: ${{ runner.temp }}/apm/lib/apm - run: | - set -euo pipefail - curl -sSL https://aka.ms/apm-unix | sh - echo "${APM_INSTALL_DIR}" >> "${GITHUB_PATH}" + run: bash "${RUNNER_TEMP}/devsecninja-apm-sync/scripts/apm-sync/install-apm.sh" - name: Refresh APM primitives if: steps.precheck.outputs.skip != 'true' env: PACKAGES: ${{ inputs.packages }} - run: | - set -euo pipefail - apm --version - # shellcheck disable=SC2086 # PACKAGES is an intentional arg list. - apm update --yes ${PACKAGES} + run: bash "${RUNNER_TEMP}/devsecninja-apm-sync/scripts/apm-sync/update.sh" - name: Open sync PR id: open-pr diff --git a/.github/workflows/autofix.yml b/.github/workflows/autofix.yml index f1cde612..dd2c4e55 100644 --- a/.github/workflows/autofix.yml +++ b/.github/workflows/autofix.yml @@ -42,6 +42,24 @@ jobs: name: auto-fix formatting runs-on: ubuntu-24.04 steps: + - name: Checkout autofix scripts + # The autofix scripts ship with this reusable workflow: fetch them from + # the same commit of DevSecNinja/.github that the caller pinned. + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: ${{ job.workflow_repository }} + ref: ${{ job.workflow_sha }} + path: .devsecninja-autofix + sparse-checkout: |- + scripts/autofix/ + sparse-checkout-cone-mode: false + persist-credentials: false + + - name: Move autofix scripts out of the workspace + # Outside the workspace the formatters don't touch them and the commit + # doesn't pick them up, and a code checkout doesn't delete them. + run: mv .devsecninja-autofix "${RUNNER_TEMP}/devsecninja-autofix" + - name: Checkout code # zizmor: ignore[artipacked] # Persisted credentials are required: the final step commits the @@ -62,42 +80,19 @@ jobs: if: ${{ inputs.autofix-dprint }} env: CONFIG_DIR: ${{ inputs.autofix-config-dir }} - run: |- - if [ -n "${CONFIG_DIR}" ] && [ -f "${CONFIG_DIR}/dprint.json" ]; then - mise exec -- dprint fmt \ - --config "${CONFIG_DIR}/dprint.json" \ - --config-discovery=false - else - mise exec -- dprint fmt - fi + run: bash "${RUNNER_TEMP}/devsecninja-autofix/scripts/autofix/dprint.sh" - name: Auto-fix with yamlfmt if: ${{ inputs.autofix-yamlfmt }} env: CONFIG_DIR: ${{ inputs.autofix-config-dir }} - run: |- - if [ -n "${CONFIG_DIR}" ] && [ -f "${CONFIG_DIR}/.yamlfmt.yaml" ]; then - find . \( -name '*.yaml' -o -name '*.yml' \) -print0 | - xargs -0 mise exec -- yamlfmt -conf "${CONFIG_DIR}/.yamlfmt.yaml" - else - find . \( -name '*.yaml' -o -name '*.yml' \) -print0 | - xargs -0 mise exec -- yamlfmt - fi + run: bash "${RUNNER_TEMP}/devsecninja-autofix/scripts/autofix/yamlfmt.sh" - name: Auto-fix with shfmt if: ${{ inputs.autofix-shfmt }} - run: find . -name '*.sh' -print0 | xargs -0 mise exec -- shfmt --write + run: bash "${RUNNER_TEMP}/devsecninja-autofix/scripts/autofix/shfmt.sh" - name: Commit and push auto-fix changes env: COMMIT_MESSAGE: ${{ inputs.commit-message }} - run: |- - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - if git diff --quiet; then - echo "No formatting changes to commit." - else - git add . - git commit -m "${COMMIT_MESSAGE}" - git push - fi + run: bash "${RUNNER_TEMP}/devsecninja-autofix/scripts/autofix/commit-and-push.sh" diff --git a/.github/workflows/config-sync.yml b/.github/workflows/config-sync.yml index f1c3f442..ef069c27 100644 --- a/.github/workflows/config-sync.yml +++ b/.github/workflows/config-sync.yml @@ -103,6 +103,10 @@ jobs: steps: - name: Resolve GitHub App credentials id: app + # This step reads the App key, so tests/app-key-confinement.py pins its + # `run:` by SHA-256 and lets no other step run before it. A script + # fetched by an earlier step would escape both checks (#537). + # lint: allow-inline-script reviewed App-key emptiness check, pinned by tests/app-key-confinement.py env: APP_ID: ${{ inputs.app-id || vars.AUTOMATION_APP_ID }} APP_PRIVATE_KEY: ${{ secrets.app-private-key || secrets.AUTOMATION_APP_PRIVATE_KEY }} @@ -129,6 +133,22 @@ jobs: reason="No GitHub App credentials (AUTOMATION_APP_ID variable, AUTOMATION_APP_PRIVATE_KEY secret)." fi echo "::warning title=Config Sync falls back to GITHUB_TOKEN::${reason} GITHUB_TOKEN can only open the PR when 'Allow GitHub Actions to create and approve pull requests' is enabled, and that PR does not trigger CI. See https://github.com/DevSecNinja/.github/blob/main/docs/automation-app.md" + - name: Checkout config-sync scripts + # The config-sync scripts ship with this reusable workflow: fetch them + # from the same commit of DevSecNinja/.github that the caller pinned. + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: ${{ job.workflow_repository }} + ref: ${{ job.workflow_sha }} + path: .devsecninja-config-sync + sparse-checkout: |- + scripts/config-sync/ + sparse-checkout-cone-mode: false + persist-credentials: false + - name: Move config-sync scripts out of the workspace + # Outside the workspace the sync PR can't pick them up, and a code + # checkout doesn't delete them. + run: mv .devsecninja-config-sync "${RUNNER_TEMP}/devsecninja-config-sync" - name: Generate App token id: app-token if: steps.app.outputs.available == 'true' @@ -162,239 +182,12 @@ jobs: SYNC_TEMPLATES: ${{ inputs.sync-templates }} IGNORE_FILE: .github/.config-sync-ignore SOURCE_DIR: .config-sync-source/config-sync - run: |- - set -euo pipefail - - updated=0 - source_sha=$(git -C .config-sync-source rev-parse HEAD) - echo "Source: DevSecNinja/.github @ ${source_sha}" - echo "source_sha=${source_sha}" >> "${GITHUB_OUTPUT}" - - # Load ignore list (paths relative to repo root, one per line). - ignored="" - if [ -f "${IGNORE_FILE}" ]; then - ignored=$(cat "${IGNORE_FILE}") - echo "Loaded ignore list from ${IGNORE_FILE}:" - while IFS= read -r line; do - [ -z "${line}" ] && continue - case "${line}" in \#*) continue ;; esac - echo " - ${line}" - done <<< "${ignored}" - fi - - is_ignored() { - local target="$1" - local pattern trimmed - [ -z "${ignored}" ] && return 1 - while IFS= read -r pattern; do - trimmed="${pattern#"${pattern%%[![:space:]]*}"}" - trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" - [ -z "${trimmed}" ] && continue - case "${trimmed}" in \#*) continue ;; esac - if [ "${target}" = "${trimmed}" ]; then - return 0 - fi - done <<< "${ignored}" - return 1 - } - - # Files APM owns in this repository are left alone: - # - paths APM deploys here (`deployed_files` in apm.lock.yaml); - # - files generated from a package (their `