diff --git a/README.md b/README.md index b4aa160..23eb0ca 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Reusable composite actions for Gitea CI/CD pipelines. | Action | Description | |---|---| -| [infisical/fetch-secret](infisical/fetch-secret/README.md) | Fetch a single secret value from Infisical using a machine identity token | +| [infisical/fetch-secret](infisical/fetch-secret/README.md) | Fetch one or more secret values from Infisical using a machine identity token | ### Infrastructure diff --git a/infisical/README.md b/infisical/README.md index 22e4e7a..45a182a 100644 --- a/infisical/README.md +++ b/infisical/README.md @@ -4,4 +4,4 @@ Composite actions for Infisical secret management. | action | description | | --- | --- | -| [fetch-secret](fetch-secret/README.md) | Fetches a single secret value from Infisical using a machine identity token | +| [fetch-secret](fetch-secret/README.md) | Fetches one or more secret values from Infisical using a machine identity token | diff --git a/infisical/fetch-secret/README.md b/infisical/fetch-secret/README.md index 7b68dd6..214542a 100644 --- a/infisical/fetch-secret/README.md +++ b/infisical/fetch-secret/README.md @@ -3,7 +3,7 @@ ## Description -Fetches a single secret value from Infisical using a machine identity token +Fetches one or more secret values from Infisical using a machine identity token @@ -12,9 +12,10 @@ Fetches a single secret value from Infisical using a machine identity token | name | description | required | default | | --- | --- | --- | --- | | `INFISICAL_TOKEN` |

Machine identity access token

| `true` | `""` | -| `SECRET_NAME` |

The secret key to fetch

| `true` | `""` | +| `SECRET_NAME` |

A single secret key to fetch; its value is returned base64-encoded in the value output

| `false` | `""` | +| `SECRET_NAMES` |

Secret keys to fetch, separated by newlines, commas, or spaces; each is exported as an environment variable of the same name for subsequent steps

| `false` | `""` | | `INFISICAL_HOST` |

Infisical API base URL

| `false` | `https://infisical.pixelparasol.com` | -| `WORKSPACE_ID` |

Infisical project UUID

| `false` | `""` | +| `WORKSPACE_ID` |

Infisical project UUID

| `true` | `""` | | `ENVIRONMENT` |

Infisical environment slug

| `false` | `prod` | | `SECRET_PATH` |

Folder path within the environment

| `false` | `/` | @@ -24,7 +25,7 @@ Fetches a single secret value from Infisical using a machine identity token | name | description | | --- | --- | -| `value` |

The fetched secret value

| +| `value` |

The fetched SECRET_NAME value (base64-encoded)

| @@ -32,3 +33,36 @@ Fetches a single secret value from Infisical using a machine identity token This action is a `composite` action. + +## Usage + +### Multiple secrets + +Each name in `SECRET_NAMES` is exported as an environment variable of the same name (raw value, not base64) to all subsequent steps in the job. Names must be valid environment variable names. The action fails if any secret is missing. + +```yaml +- uses: pixel-parasol-public/gitea-actions/infisical/fetch-secret@main + with: + INFISICAL_TOKEN: ${{ secrets.INFISICAL_TOKEN }} + WORKSPACE_ID: + SECRET_NAMES: | + DATABASE_URL + API_KEY + +- run: ./deploy.sh # $DATABASE_URL and $API_KEY are set +``` + +### Single secret + +```yaml +- id: secret + uses: pixel-parasol-public/gitea-actions/infisical/fetch-secret@main + with: + INFISICAL_TOKEN: ${{ secrets.INFISICAL_TOKEN }} + WORKSPACE_ID: + SECRET_NAME: DATABASE_URL + +- run: echo "${{ steps.secret.outputs.value }}" | base64 -d > db_url +``` + +`SECRET_NAME` and `SECRET_NAMES` can be combined in one call. diff --git a/infisical/fetch-secret/action.yml b/infisical/fetch-secret/action.yml index ae45d3a..3b60a0e 100644 --- a/infisical/fetch-secret/action.yml +++ b/infisical/fetch-secret/action.yml @@ -1,12 +1,17 @@ name: Fetch Secret from Infisical -description: Fetches a single secret value from Infisical using a machine identity token +description: Fetches one or more secret values from Infisical using a machine identity token inputs: INFISICAL_TOKEN: description: "Machine identity access token" required: true SECRET_NAME: - description: "The secret key to fetch" - required: true + description: "A single secret key to fetch; its value is returned base64-encoded in the `value` output" + required: false + default: "" + SECRET_NAMES: + description: "Secret keys to fetch, separated by newlines, commas, or spaces; each is exported as an environment variable of the same name for subsequent steps" + required: false + default: "" INFISICAL_HOST: description: "Infisical API base URL" required: false @@ -24,48 +29,88 @@ inputs: default: "/" outputs: value: - description: "The fetched secret value (base64-encoded)" + description: "The fetched `SECRET_NAME` value (base64-encoded)" value: ${{ steps.fetch.outputs.value }} runs: using: composite steps: - - name: Fetch secret + - name: Fetch secrets id: fetch shell: sh + env: + INFISICAL_TOKEN: ${{ inputs.INFISICAL_TOKEN }} + INFISICAL_HOST: ${{ inputs.INFISICAL_HOST }} + SECRET_NAME: ${{ inputs.SECRET_NAME }} + SECRET_NAMES: ${{ inputs.SECRET_NAMES }} + WORKSPACE_ID: ${{ inputs.WORKSPACE_ID }} + ENVIRONMENT: ${{ inputs.ENVIRONMENT }} + SECRET_PATH: ${{ inputs.SECRET_PATH }} run: | + if [ -z "$SECRET_NAME" ] && [ -z "$SECRET_NAMES" ]; then + echo "Error: at least one of SECRET_NAME or SECRET_NAMES must be set" >&2 + exit 1 + fi + if ! command -v jq >/dev/null 2>&1; then apk add --no-cache jq 2>/dev/null \ || apt-get install -y -q --no-install-recommends jq 2>/dev/null \ || { echo "Error: jq not available and could not be installed" >&2; exit 1; } fi - HTTP_STATUS=$(curl -s -o /tmp/_infisical_resp.json -w "%{http_code}" \ - -H "Authorization: Bearer ${{ inputs.INFISICAL_TOKEN }}" \ - "${{ inputs.INFISICAL_HOST }}/api/v3/secrets/raw/${{ inputs.SECRET_NAME }}?workspaceId=${{ inputs.WORKSPACE_ID }}&environment=${{ inputs.ENVIRONMENT }}&secretPath=${{ inputs.SECRET_PATH }}") + RESP_FILE=$(mktemp) + trap 'rm -f "$RESP_FILE"' EXIT - if [ "$HTTP_STATUS" != "200" ]; then - echo "Error: Infisical returned HTTP $HTTP_STATUS for secret '${{ inputs.SECRET_NAME }}'" >&2 - echo "Response: $(cat /tmp/_infisical_resp.json)" >&2 - rm -f /tmp/_infisical_resp.json - exit 1 + # Sets VALUE to the secret's raw value + fetch_secret() { + name="$1" + http_status=$(curl -s -o "$RESP_FILE" -w "%{http_code}" \ + -H "Authorization: Bearer $INFISICAL_TOKEN" \ + "$INFISICAL_HOST/api/v3/secrets/raw/$name?workspaceId=$WORKSPACE_ID&environment=$ENVIRONMENT&secretPath=$SECRET_PATH") + + if [ "$http_status" != "200" ]; then + echo "Error: Infisical returned HTTP $http_status for secret '$name'" >&2 + echo "Response: $(cat "$RESP_FILE")" >&2 + exit 1 + fi + + VALUE=$(jq -r '.secret.secretValue' "$RESP_FILE") + + if [ -z "$VALUE" ] || [ "$VALUE" = "null" ]; then + echo "Error: secret '$name' is empty or not found" >&2 + exit 1 + fi + + printf '%s\n' "$VALUE" | while IFS= read -r line; do + [ -n "$line" ] && echo "::add-mask::$line" + done + } + + if [ -n "$SECRET_NAME" ]; then + fetch_secret "$SECRET_NAME" + B64=$(printf '%s' "$VALUE" | base64 | tr -d '\n') + echo "::add-mask::$B64" + + DELIMITER="INFISICAL_EOF_$$" + echo "value<<${DELIMITER}" >> "$GITHUB_OUTPUT" + printf '%s\n' "$B64" >> "$GITHUB_OUTPUT" + echo "${DELIMITER}" >> "$GITHUB_OUTPUT" + echo "Successfully fetched secret '$SECRET_NAME'" fi - RESPONSE=$(cat /tmp/_infisical_resp.json) - rm -f /tmp/_infisical_resp.json + for name in $(printf '%s' "$SECRET_NAMES" | tr ',' ' '); do + case "$name" in + [!A-Za-z_]* | *[!A-Za-z0-9_]*) + echo "Error: '$name' is not a valid environment variable name" >&2 + exit 1 + ;; + esac - VALUE=$(echo "$RESPONSE" | jq -r '.secret.secretValue') + fetch_secret "$name" - if [ -z "$VALUE" ] || [ "$VALUE" = "null" ]; then - echo "Error: secret '${{ inputs.SECRET_NAME }}' is empty or not found" >&2 - exit 1 - fi - - B64=$(printf '%s' "$VALUE" | base64 | tr -d '\n') - echo "::add-mask::$B64" - - DELIMITER="INFISICAL_EOF_$$" - echo "value<<${DELIMITER}" >> "$GITHUB_OUTPUT" - printf '%s\n' "$B64" >> "$GITHUB_OUTPUT" - echo "${DELIMITER}" >> "$GITHUB_OUTPUT" - echo "Successfully fetched secret '${{ inputs.SECRET_NAME }}'" + DELIMITER="INFISICAL_EOF_$(od -An -N8 -tx1 /dev/urandom | tr -d ' \n')" + echo "${name}<<${DELIMITER}" >> "$GITHUB_ENV" + printf '%s\n' "$VALUE" >> "$GITHUB_ENV" + echo "${DELIMITER}" >> "$GITHUB_ENV" + echo "Successfully fetched secret '$name' into \$$name" + done