diff --git a/.github/workflows/close-pull-request.yml b/.github/workflows/close-pull-request.yml index e01d1aff38..7212341bfe 100644 --- a/.github/workflows/close-pull-request.yml +++ b/.github/workflows/close-pull-request.yml @@ -11,18 +11,40 @@ on: jobs: remove_lagoon_environment: + #;< VORTEX_DEV + # The template repository has no Lagoon project and no webhook token. + if: ${{ github.repository != 'drevops/vortex' }} + #;> VORTEX_DEV runs-on: ubuntu-latest permissions: contents: read steps: - name: Remove environment after a PR is closed run: | - cat "$GITHUB_EVENT_PATH" | curl \ + if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi + + # Lagoon verifies the signature as GitHub webhooks do, so sign the + # exact bytes sent as the body. + signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + + status=0 + response="$(curl \ + --silent \ + --show-error \ + --fail-with-body \ -X POST \ --header "Content-Type: application/json" \ --header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \ --header "X-GitHub-Event: pull_request" \ - --data-binary @- \ - "${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}" + --header "X-Hub-Signature-256: sha256=${signature_sha256}" \ + --header "X-Hub-Signature: sha1=${signature_sha1}" \ + --data-binary @"$GITHUB_EVENT_PATH" \ + "${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$? + echo "${response}" + + # Lagoon answers a rejected webhook with HTTP 200 and an error body. + if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi env: LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }} + LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }} diff --git a/.vortex/docs/content/deployment/lagoon.mdx b/.vortex/docs/content/deployment/lagoon.mdx index 4eeacdd182..c2305e7a01 100644 --- a/.vortex/docs/content/deployment/lagoon.mdx +++ b/.vortex/docs/content/deployment/lagoon.mdx @@ -168,13 +168,29 @@ types](https://docs.lagoon.sh/concepts-advanced/environment-types/). On GitHub, the shipped `.github/workflows/close-pull-request.yml` workflow runs when a pull request closes. The workflow forwards the event to the Lagoon webhook endpoint, so Lagoon removes the pull request's environment. -The endpoint defaults to `https://hooks.lagoon.amazeeio.cloud/`. To use -another Lagoon instance, set the `LAGOON_WEBHOOK_ENDPOINT` repository -variable. +Lagoon accepts only webhooks signed with the project's webhook token. The +workflow signs the event with the `LAGOON_WEBHOOK_TOKEN` repository secret. + +To add the secret: + +1. In the Lagoon UI, copy the **Webhook token** value from the project's + **Details** page. For a polysite, copy the organization's webhook token + instead. +2. In GitHub, add the value under + **Settings > Secrets and variables > Actions** as the + `LAGOON_WEBHOOK_TOKEN` repository secret. + +When the secret is missing, the workflow fails with +`LAGOON_WEBHOOK_TOKEN secret is not set.`. The workflow also fails when +Lagoon rejects the request, and it prints Lagoon's response on every run. + +The endpoint defaults to `https://webhook.amazeeio.cloud/`. To use another +Lagoon instance, set the `LAGOON_WEBHOOK_ENDPOINT` repository variable. The installer keeps this workflow for CircleCI projects too, so the -workflow still runs if GitHub Actions is enabled for the repository. The -CircleCI config has no step that removes pull request environments. +workflow still runs if GitHub Actions is enabled for the repository. Add +the `LAGOON_WEBHOOK_TOKEN` secret to such a repository too. The CircleCI +config has no step that removes pull request environments. ## See also diff --git a/.vortex/docs/content/installation.mdx b/.vortex/docs/content/installation.mdx index aaedc7e28d..fb00d0cea5 100644 --- a/.vortex/docs/content/installation.mdx +++ b/.vortex/docs/content/installation.mdx @@ -287,9 +287,13 @@ To set up GitHub Actions: secret too, for database fetches. With separate keys, use the read-only key. Acquia fetches the database through the Acquia Cloud API, so it needs no such secret. -4. On Acquia, add the API key and secret from [step 6](#6-set-up-hosting) +4. On Lagoon, add the project's webhook token as the `LAGOON_WEBHOOK_TOKEN` + secret, so Lagoon removes a pull request's environment when the pull + request closes. [Lagoon deployment](/docs/deployment/lagoon#environments) + says where to find the token. +5. On Acquia, add the API key and secret from [step 6](#6-set-up-hosting) as the `VORTEX_ACQUIA_KEY` and `VORTEX_ACQUIA_SECRET` repository secrets. -5. On Acquia, add these 2 variables under +6. On Acquia, add these 2 variables under **Settings > Secrets and variables > Actions > Variables**: - `VORTEX_DEPLOY_ARTIFACT_GIT_REMOTE`: your application's Git repository URL @@ -330,6 +334,10 @@ To set up CircleCI: URL - `VORTEX_DEPLOY_ARTIFACT_GIT_USER_EMAIL`: the email address for the deployment commits +9. On Lagoon with GitHub Actions enabled for the repository, add the + project's webhook token in GitHub as the `LAGOON_WEBHOOK_TOKEN` secret. + [Lagoon deployment](/docs/deployment/lagoon#environments) says where to + find the token and why the secret is needed. On Lagoon, the pipeline fetches and deploys with the SSH keys you added. diff --git a/.vortex/docs/cspell.json b/.vortex/docs/cspell.json index db7e21a182..bfa984ba3d 100644 --- a/.vortex/docs/cspell.json +++ b/.vortex/docs/cspell.json @@ -86,6 +86,7 @@ "phpcompatibility", "phpspec", "phpstorm", + "polysite", "postbuild", "preboot", "prompty", diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_lagoon/.github/workflows/close-pull-request.yml b/.vortex/installer/tests/Fixtures/handler_process/hosting_lagoon/.github/workflows/close-pull-request.yml index e01d1aff38..22f12ed8a6 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_lagoon/.github/workflows/close-pull-request.yml +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_lagoon/.github/workflows/close-pull-request.yml @@ -17,12 +17,30 @@ jobs: steps: - name: Remove environment after a PR is closed run: | - cat "$GITHUB_EVENT_PATH" | curl \ + if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi + + # Lagoon verifies the signature as GitHub webhooks do, so sign the + # exact bytes sent as the body. + signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + + status=0 + response="$(curl \ + --silent \ + --show-error \ + --fail-with-body \ -X POST \ --header "Content-Type: application/json" \ --header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \ --header "X-GitHub-Event: pull_request" \ - --data-binary @- \ - "${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}" + --header "X-Hub-Signature-256: sha256=${signature_sha256}" \ + --header "X-Hub-Signature: sha1=${signature_sha1}" \ + --data-binary @"$GITHUB_EVENT_PATH" \ + "${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$? + echo "${response}" + + # Lagoon answers a rejected webhook with HTTP 200 and an error body. + if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi env: LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }} + LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }} diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___lagoon/.github/workflows/close-pull-request.yml b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___lagoon/.github/workflows/close-pull-request.yml index e01d1aff38..22f12ed8a6 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___lagoon/.github/workflows/close-pull-request.yml +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___lagoon/.github/workflows/close-pull-request.yml @@ -17,12 +17,30 @@ jobs: steps: - name: Remove environment after a PR is closed run: | - cat "$GITHUB_EVENT_PATH" | curl \ + if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi + + # Lagoon verifies the signature as GitHub webhooks do, so sign the + # exact bytes sent as the body. + signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + + status=0 + response="$(curl \ + --silent \ + --show-error \ + --fail-with-body \ -X POST \ --header "Content-Type: application/json" \ --header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \ --header "X-GitHub-Event: pull_request" \ - --data-binary @- \ - "${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}" + --header "X-Hub-Signature-256: sha256=${signature_sha256}" \ + --header "X-Hub-Signature: sha1=${signature_sha1}" \ + --data-binary @"$GITHUB_EVENT_PATH" \ + "${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$? + echo "${response}" + + # Lagoon answers a rejected webhook with HTTP 200 and an error body. + if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi env: LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }} + LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }} diff --git a/.vortex/installer/tests/Fixtures/handler_process/migration_disabled_lagoon/.github/workflows/close-pull-request.yml b/.vortex/installer/tests/Fixtures/handler_process/migration_disabled_lagoon/.github/workflows/close-pull-request.yml index e01d1aff38..22f12ed8a6 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/migration_disabled_lagoon/.github/workflows/close-pull-request.yml +++ b/.vortex/installer/tests/Fixtures/handler_process/migration_disabled_lagoon/.github/workflows/close-pull-request.yml @@ -17,12 +17,30 @@ jobs: steps: - name: Remove environment after a PR is closed run: | - cat "$GITHUB_EVENT_PATH" | curl \ + if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi + + # Lagoon verifies the signature as GitHub webhooks do, so sign the + # exact bytes sent as the body. + signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + + status=0 + response="$(curl \ + --silent \ + --show-error \ + --fail-with-body \ -X POST \ --header "Content-Type: application/json" \ --header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \ --header "X-GitHub-Event: pull_request" \ - --data-binary @- \ - "${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}" + --header "X-Hub-Signature-256: sha256=${signature_sha256}" \ + --header "X-Hub-Signature: sha1=${signature_sha1}" \ + --data-binary @"$GITHUB_EVENT_PATH" \ + "${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$? + echo "${response}" + + # Lagoon answers a rejected webhook with HTTP 200 and an error body. + if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi env: LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }} + LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }} diff --git a/.vortex/installer/tests/Fixtures/handler_process/migration_enabled_lagoon/.github/workflows/close-pull-request.yml b/.vortex/installer/tests/Fixtures/handler_process/migration_enabled_lagoon/.github/workflows/close-pull-request.yml index e01d1aff38..22f12ed8a6 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/migration_enabled_lagoon/.github/workflows/close-pull-request.yml +++ b/.vortex/installer/tests/Fixtures/handler_process/migration_enabled_lagoon/.github/workflows/close-pull-request.yml @@ -17,12 +17,30 @@ jobs: steps: - name: Remove environment after a PR is closed run: | - cat "$GITHUB_EVENT_PATH" | curl \ + if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi + + # Lagoon verifies the signature as GitHub webhooks do, so sign the + # exact bytes sent as the body. + signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + + status=0 + response="$(curl \ + --silent \ + --show-error \ + --fail-with-body \ -X POST \ --header "Content-Type: application/json" \ --header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \ --header "X-GitHub-Event: pull_request" \ - --data-binary @- \ - "${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}" + --header "X-Hub-Signature-256: sha256=${signature_sha256}" \ + --header "X-Hub-Signature: sha1=${signature_sha1}" \ + --data-binary @"$GITHUB_EVENT_PATH" \ + "${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$? + echo "${response}" + + # Lagoon answers a rejected webhook with HTTP 200 and an error body. + if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi env: LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }} + LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }} diff --git a/.vortex/installer/tests/Fixtures/handler_process/provision_database_lagoon/.github/workflows/close-pull-request.yml b/.vortex/installer/tests/Fixtures/handler_process/provision_database_lagoon/.github/workflows/close-pull-request.yml index e01d1aff38..22f12ed8a6 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/provision_database_lagoon/.github/workflows/close-pull-request.yml +++ b/.vortex/installer/tests/Fixtures/handler_process/provision_database_lagoon/.github/workflows/close-pull-request.yml @@ -17,12 +17,30 @@ jobs: steps: - name: Remove environment after a PR is closed run: | - cat "$GITHUB_EVENT_PATH" | curl \ + if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi + + # Lagoon verifies the signature as GitHub webhooks do, so sign the + # exact bytes sent as the body. + signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')" + + status=0 + response="$(curl \ + --silent \ + --show-error \ + --fail-with-body \ -X POST \ --header "Content-Type: application/json" \ --header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \ --header "X-GitHub-Event: pull_request" \ - --data-binary @- \ - "${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}" + --header "X-Hub-Signature-256: sha256=${signature_sha256}" \ + --header "X-Hub-Signature: sha1=${signature_sha1}" \ + --data-binary @"$GITHUB_EVENT_PATH" \ + "${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$? + echo "${response}" + + # Lagoon answers a rejected webhook with HTTP 200 and an error body. + if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi env: LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }} + LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }}