diff --git a/.github/workflows/update-rules.yml b/.github/workflows/update-rules.yml new file mode 100644 index 0000000..26bb955 --- /dev/null +++ b/.github/workflows/update-rules.yml @@ -0,0 +1,112 @@ +name: Update AppSec Rules +on: + workflow_dispatch: # manually + schedule: + - cron: "0 6 * * 1" # every Monday at 06:00 UTC + +jobs: + update: + runs-on: ubuntu-latest + name: Get latest ruleset + outputs: + mutation_happened: ${{ steps.detect.outputs.mutation_happened }} + version: ${{ steps.update.outputs.version }} + steps: + - name: Checkout + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - name: Setup Go + uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0 + - name: Generate a GitHub token + id: generate-token + uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6 + with: + app-id: ${{ vars.DD_K9_LIBRARY_GO_APP_ID }} + private-key: ${{ secrets.DD_K9_LIBRARY_GO_APP_PRIVATE_KEY }} + owner: DataDog + repositories: appsec-event-rules + permission-contents: read + - name: Upgrade AppSec Rules + id: update + run: |- + ./_tools/rules-updater/update.sh latest + VERSION=$(jq -r '.metadata.rules_version' < ./appsec/rules.json) + echo "Ruleset version: ${VERSION}" + echo "version=${VERSION}" >> "${GITHUB_OUTPUT}" + env: + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} + - name: Detect Updated Code + id: detect + run: |- + git add . + git diff --staged --patch --exit-code > ${{ runner.temp }}/repo.patch || echo "mutation_happened=true" >> "${GITHUB_OUTPUT}" + - name: Upload Patch + if: steps.detect.outputs.mutation_happened + uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0 + with: + name: repo.patch + path: ${{ runner.temp }}/repo.patch + + create-pr: + needs: update + name: Create PR + runs-on: ubuntu-latest + permissions: + contents: write + if: needs.update.outputs.mutation_happened + steps: + - name: Generate a GitHub token + id: generate-token + uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6 + with: + app-id: ${{ vars.DD_K9_LIBRARY_GO_APP_ID }} + private-key: ${{ secrets.DD_K9_LIBRARY_GO_APP_PRIVATE_KEY }} + - name: Checkout + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + token: ${{ steps.generate-token.outputs.token }} + persist-credentials: true + - name: Download Patch + uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 + with: + name: repo.patch + path: ${{ runner.temp }} + - name: Apply Patch + id: apply + run: |- + git apply ${{ runner.temp }}/repo.patch + echo "version=$(jq -r '.metadata.rules_version' < ./appsec/rules.json)" >> $GITHUB_OUTPUT + - name: Configure git identity + run: |- + git config user.name "${APP_SLUG}[bot]" + git config user.email "$(gh api "/users/${APP_SLUG}[bot]" | jq .id)+${APP_SLUG}[bot]@users.noreply.github.com" + env: + APP_SLUG: ${{ steps.generate-token.outputs.app-slug }} + GITHUB_TOKEN: ${{ github.token }} + - name: Create PR Branch + id: create-branch + run: |- + branch="automation/default-ruleset-update/${VERSION}" + git push --force origin "${{ github.sha }}":"refs/heads/${branch}" + echo "branch=${branch}" >> "${GITHUB_OUTPUT}" + git fetch origin "${branch}" + env: + VERSION: ${{ needs.update.outputs.version }} + - name: Create Commit on PR Branch + uses: planetscale/ghcommit-action@6a383e778f6620afde4bf4b45069d3c6983c1ae2 # v0.2.15 + with: + commit_message: "chore: update default ruleset to v${{ needs.update.outputs.version }}" + branch: ${{ steps.create-branch.outputs.branch }} + repo: ${{ github.repository }} + env: + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} + - name: Create Pull Request + run: |- + git fetch origin "${{ steps.create-branch.outputs.branch }}" + git reset --hard HEAD + git switch "${{ steps.create-branch.outputs.branch }}" + gh pr create --title "chore: update default ruleset to v${VERSION}" \ + --body "Updated default ruleset to v${VERSION}." \ + --head="${{ steps.create-branch.outputs.branch }}" + env: + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} + VERSION: ${{ needs.update.outputs.version }}