diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fd664b4..0e4e5b2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,6 +2,9 @@ name: CI +permissions: + contents: read + # Controls when the action will run. on: # Triggers the workflow on push or pull request events but only for the main branch @@ -23,7 +26,7 @@ jobs: # Steps represent a sequence of tasks that will be executed as part of the job steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - uses: actions/checkout@v6 + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 - name: Prepare a test file run: | @@ -42,7 +45,7 @@ jobs: runs-on: ubuntu-latest steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - uses: actions/checkout@v6 + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 - name: Prepare a test file run: | @@ -65,7 +68,7 @@ jobs: test-functions: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 # Create test functions - name: Prepare test functions @@ -131,3 +134,32 @@ jobs: token: ${{ secrets.QUANT_TOKEN }} dir: build functions: "./functions/config.json" + + test-rules-guard: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 + + - name: Valid rules inputs are accepted + env: + RULES: edge-rules.json + FUNCTIONS: .quant-functions.json + CUSTOMER: quant + PROJECT: github-actions + RULES_FORCE: "false" + API_TOKEN: dummy + API_BASE_URL: https://dashboard.quantcdn.io/api/v2 + run: ./scripts/validate-rules-inputs.sh + + - name: Inputs that would become extra CLI flags are refused + run: | + base=(RULES=edge-rules.json FUNCTIONS=.quant-functions.json CUSTOMER=quant PROJECT=github-actions RULES_FORCE=false API_TOKEN=dummy API_BASE_URL=https://dashboard.quantcdn.io/api/v2) + for bad in "RULES=r.json --api-base-url=https://attacker.example/api/v2" "RULES=--api-token=x" 'RULES=r.json"x' \ + "FUNCTIONS=f.json -c other" "CUSTOMER=quant --api-base-url=x" "PROJECT=-p" "RULES_FORCE=yes" \ + "API_TOKEN=" "API_BASE_URL="; do + if env "${base[@]}" "$bad" ./scripts/validate-rules-inputs.sh >/dev/null 2>&1; then + echo "::error::accepted unsafe input: $bad"; exit 1 + fi + done + echo "All unsafe inputs refused." + diff --git a/README.md b/README.md index e02de4d..3e7d168 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ This GitHub Action deploys your static site and/or functions to QuantCDN using t |-------|-------------|----------|---------| | `customer` | Your QuantCDN customer account name | Yes | - | | `project` | Your QuantCDN project name | Yes | - | -| `token` | Your QuantCDN API token | Yes | - | +| `token` | Your content write token (uploads only; never used for rules) | Yes | - | | `dir` | The directory to deploy | No | - | | `functions` | Path to JSON file containing functions configuration | No | - | | `skip-unpublish` | Skip automatic unpublishing of assets | No | `false` | @@ -41,6 +41,10 @@ This GitHub Action deploys your static site and/or functions to QuantCDN using t | `endpoint` | Specify the QuantCDN API endpoint | No | `https://api.quantcdn.io/v1` | | `revision-log` | Specify a location for the local revision log file | No | `false` | | `enable-index-html` | Enable index.html creation in Quant | No | `false` | +| `rules` | Path to an edge rules manifest (`edge-rules.json`) | No | - | +| `api-token` | Project-scoped portal API token for rules (`QUANT_API_TOKEN`); required with `rules` | No | - | +| `api-base-url` | Portal API base URL including `/api/v2` (`QUANT_BASE_URL`); required with `rules` | No | - | +| `rules-force` | Overwrite managed rules changed outside code (never takes over another rule) | No | `false` | ## Example Workflows @@ -155,6 +159,36 @@ jobs: - For search functionality, please use the dedicated search action - Make sure your `QUANT_TOKEN` is stored securely in your repository secrets +## Edge rules + +Provide `rules: edge-rules.json`, `functions: edge-functions.json`, +`api-token: ${{ secrets.QUANT_API_TOKEN }}` and +`api-base-url: ${{ vars.QUANT_BASE_URL }}` alongside the normal deployment inputs. +The Quant portal injects the project-scoped API secret and the environment-specific +portal `/api/v2` URL when a static or Studio template includes `edge-rules.json`. +Uploads continue to use the separate `token` input. Never substitute the content +write token for `api-token`. + +The action runs in this order: + +1. **Validate rules inputs.** `rules`, `functions`, `customer` and `project` must be + plain paths or slugs (no spaces, quotes or leading `-`), and `api-token` and + `api-base-url` must be set. +2. **Validate rules access** with a dry run: token scope, manifest and drift are + checked before anything is uploaded. +3. Deploy functions, then assets. +4. **Deploy rules.** + +Rules deployed this way are managed in code and are read-only in the dashboard. +If one was changed outside code anyway, step 2 fails before any upload and the +log lists the changed fields. Set `rules-force: true` for one run to overwrite +those rules with the manifest; it never takes over a rule the manifest did not +create. + +If step 4 fails after the uploads (for example a transient portal error), the new +assets are live with the previous rules. Re-run the workflow: deploys are +idempotent, so the rules step converges and unchanged assets are skipped. + ## License MIT License diff --git a/action.yml b/action.yml index 008a161..7ca67bc 100644 --- a/action.yml +++ b/action.yml @@ -9,7 +9,7 @@ inputs: description: "Your project name" required: true token: - description: "Your API token" + description: "Your content write token (uploads only; never used for rules)" required: true dir: description: "The directory to deploy" @@ -49,12 +49,54 @@ inputs: functions: description: "Path to JSON file containing functions configuration" required: false + rules: + description: "Path to an edge rules JSON manifest" + required: false + api-token: + description: "Project-scoped portal API token for rules (QUANT_API_TOKEN)" + required: false + api-base-url: + description: "Portal API base URL, including /api/v2 (QUANT_BASE_URL)" + required: false + rules-force: + description: "Overwrite managed rules changed outside code (never takes over another rule)" + required: false + default: "false" runs: using: "composite" steps: + - name: Validate Rules Inputs + if: inputs.rules != '' + shell: bash + env: + RULES: ${{ inputs.rules }} + FUNCTIONS: ${{ inputs.functions }} + CUSTOMER: ${{ inputs.customer }} + PROJECT: ${{ inputs.project }} + RULES_FORCE: ${{ inputs.rules-force }} + API_TOKEN: ${{ inputs.api-token }} + API_BASE_URL: ${{ inputs.api-base-url }} + run: '"$GITHUB_ACTION_PATH/scripts/validate-rules-inputs.sh"' + + - name: Validate Rules Access + if: inputs.rules != '' + uses: "docker://quantcdn/cli:6.3.0@sha256:cc5025e8749068b3a445331430d461be9467ce0f811838fdba9710dd388d4054" + env: + QUANT_API_TOKEN: ${{ inputs.api-token }} + QUANT_BASE_URL: ${{ inputs.api-base-url }} + with: + args: > + rules + "${{ inputs.rules }}" + --functions="${{ inputs.functions }}" + -c "${{ inputs.customer }}" + -p "${{ inputs.project }}" + --force=${{ inputs.rules-force }} + --dry-run + - name: Deploy Functions if: inputs.functions != '' - uses: "docker://quantcdn/cli:6.1.0" + uses: "docker://quantcdn/cli:6.3.0@sha256:cc5025e8749068b3a445331430d461be9467ce0f811838fdba9710dd388d4054" env: CUSTOMER: ${{ inputs.customer }} TOKEN: ${{ inputs.token }} @@ -71,7 +113,7 @@ runs: - name: Deploy Assets if: inputs.dir != '' - uses: "docker://quantcdn/cli:6.1.0" + uses: "docker://quantcdn/cli:6.3.0@sha256:cc5025e8749068b3a445331430d461be9467ce0f811838fdba9710dd388d4054" with: args: > deploy @@ -87,6 +129,22 @@ runs: --endpoint=${{ inputs.endpoint }} --revision-log=${{ inputs.revision-log }} --enable-index-html=${{ inputs.enable-index-html }} + + - name: Deploy Rules + if: inputs.rules != '' + uses: "docker://quantcdn/cli:6.3.0@sha256:cc5025e8749068b3a445331430d461be9467ce0f811838fdba9710dd388d4054" + env: + QUANT_API_TOKEN: ${{ inputs.api-token }} + QUANT_BASE_URL: ${{ inputs.api-base-url }} + with: + args: > + rules + "${{ inputs.rules }}" + --functions="${{ inputs.functions }}" + -c "${{ inputs.customer }}" + -p "${{ inputs.project }}" + --force=${{ inputs.rules-force }} + branding: icon: "upload-cloud" color: "blue" diff --git a/scripts/validate-rules-inputs.sh b/scripts/validate-rules-inputs.sh new file mode 100755 index 0000000..c330792 --- /dev/null +++ b/scripts/validate-rules-inputs.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Refuse rules inputs that could become extra CLI arguments. +# +# A docker:// step passes `args:` to the container as one string that is split +# on whitespace, so a value such as "r.json --api-base-url=https://x/api/v2" +# would become flags; CLI flags override QUANT_API_TOKEN/QUANT_BASE_URL and +# could send the portal token to another host. Values reach this script only +# through the environment, never through the command line. +set -euo pipefail + +fail() { echo "::error::$1"; exit 1; } + +path_ok() { [[ "$1" =~ ^[A-Za-z0-9_./][A-Za-z0-9_./-]*$ ]]; } +slug_ok() { [[ "$1" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]]; } + +path_ok "${RULES:-}" || fail "rules must be a plain file path (letters, digits, '.', '_', '-', '/'), not '${RULES:-}'." +if [ -n "${FUNCTIONS:-}" ]; then + path_ok "$FUNCTIONS" || fail "functions must be a plain file path when rules is set." +fi +slug_ok "${CUSTOMER:-}" || fail "customer must be an organisation slug when rules is set." +slug_ok "${PROJECT:-}" || fail "project must be a project machine name when rules is set." +[[ "${RULES_FORCE:-false}" =~ ^(true|false)$ ]] || fail "rules-force must be true or false." +[ -n "${API_TOKEN:-}" ] || fail "rules requires api-token (the project-scoped QUANT_API_TOKEN), never the content token." +[ -n "${API_BASE_URL:-}" ] || fail "rules requires api-base-url (the portal /api/v2 URL, QUANT_BASE_URL)." +echo "Rules inputs validated."