Skip to content

Commit 4c01d41

Browse files
Update update_patterns.yml
Explanation of the Workflow: Checkout: Checks out the repository. fetch-depth: 0 gets the full Git history, which is necessary for tag manipulation. Setup Python: Sets up Python 3.11. Cache: Caches the pip directory to speed up dependency installation. Install Dependencies: Installs dependencies from requirements.txt. Run Scripts: Runs the owasp2json.py, json2nginx.py, json2apache.py, json2traefik.py, and json2haproxy.py scripts to generate the WAF configurations. These steps will now fail fast if any of the scripts encounter an error. Generate Bad Bot Blockers: Executes badbots.py. Commit and Push (Conditional): Configures Git with a bot user. Adds all changes. Uses git diff --quiet --exit-code to check for changes. If there are no changes, the git diff command exits with a non-zero code, and the if condition is false. If there are changes, commits them with a descriptive message and pushes to the repository. continue-on-error: true is used only on this step because it's okay if there are no changes to commit. Create Zip Archives: Creates ZIP files containing the generated configurations for each web server. The (cd ... && zip ...) command ensures that the ZIP files contain the correct directory structure (e.g., nginx_waf.zip should contain a nginx/ directory). Delete Existing Release: Deletes the latest tag (both locally and remotely) and the latest release (if they exist). This ensures that we always have a clean "latest" release. Uses the gh CLI (GitHub CLI) for release management. Create GitHub Release (Conditional): The if: success() condition ensures that this step only runs if all preceding steps were successful. This prevents creating a new release if the rule generation failed. Creates a new release tagged as latest. Upload Assets (Conditional): Uploads the generated ZIP files as assets to the new release. Also uses if: success(). Clean Up (Optional): Removes the pip cache. if: always() ensures this runs even if previous steps fail. Notify on Failure (Optional): Uses if: failure() to run only if a previous step failed. This step currently just prints a message, but you can replace it with a notification mechanism (e.g., sending a message to Slack or sending an email). You'll need to set up the necessary secrets (like SLACK_WEBHOOK) for your chosen notification method.
1 parent 23d11b6 commit 4c01d41

1 file changed

Lines changed: 68 additions & 71 deletions

File tree

Lines changed: 68 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
name: Update patterns
1+
name: Update Patterns
22

33
permissions:
4-
contents: write # Needed for committing changes and pushing updates
5-
statuses: write # Required for updating commit statuses (e.g., CI/CD status)
6-
4+
contents: write # Commit changes, push updates
5+
statuses: write # Update commit statuses
6+
actions: read # Required for checking out the repository
7+
packages: write # For GitHub Packages (if used)
8+
79
on:
810
schedule:
9-
- cron: '0 0 * * *' # Run daily at midnight UTC
10-
workflow_dispatch: # Allow manual trigger
11+
- cron: '0 0 * * *' # Daily at midnight UTC
12+
workflow_dispatch: # Manual trigger
1113

1214
jobs:
1315
update-owasp-waf:
@@ -17,15 +19,14 @@ jobs:
1719
- name: 🚚 Checkout Repository
1820
uses: actions/checkout@v3
1921
with:
20-
fetch-depth: 0 # Full history to avoid shallow clone issues
22+
fetch-depth: 0 # get full git history
2123

22-
- name: ⚙️ Set Up Python
24+
- name: ⚙️ Set Up Python 3.11
2325
uses: actions/setup-python@v4
2426
with:
2527
python-version: '3.11'
2628

27-
- name: 📦 Cache Python Packages
28-
id: cache-pip
29+
- name: 📦 Cache pip dependencies
2930
uses: actions/cache@v3
3031
with:
3132
path: ~/.cache/pip
@@ -34,134 +35,130 @@ jobs:
3435
${{ runner.os }}-pip-
3536
3637
- name: 📥 Install Dependencies
37-
if: steps.cache-pip.outputs.cache-hit != 'true'
3838
run: |
3939
python -m pip install --upgrade pip
4040
pip install -r requirements.txt
41-
continue-on-error: false # Fail the workflow if dependencies fail to install
4241
4342
- name: 🕷️ Run OWASP Scraper
44-
run: |
45-
python owasp2json.py
46-
continue-on-error: false
43+
run: python owasp2json.py
4744

4845
- name: 🔄 Convert OWASP to Nginx WAF
49-
run: |
50-
python json2nginx.py
51-
continue-on-error: false
46+
run: python json2nginx.py
5247

5348
- name: 🔄 Convert OWASP to Apache WAF
54-
run: |
55-
python json2apache.py
56-
continue-on-error: false
49+
run: python json2apache.py
5750

5851
- name: 🔄 Convert OWASP to Traefik WAF
59-
run: |
60-
python json2traefik.py
61-
continue-on-error: false
52+
run: python json2traefik.py
6253

6354
- name: 🔄 Convert OWASP to HAProxy WAF
55+
run: python json2haproxy.py
56+
57+
- name: 🔄 Generate Bad Bot Blockers (Placeholder - Provide badbots.py)
6458
run: |
65-
python json2haproxy.py
66-
continue-on-error: false
67-
68-
- name: 🔄 Generate Bad Bot Blockers
69-
run: |
70-
python badbots.py
71-
continue-on-error: false
72-
73-
# Ensure conf files are pushed even if no changes detected
74-
- name: 🚀 Commit and Push OWASP WAF patterns
59+
# Placeholder: Replace this with your actual badbots.py script.
60+
# Assuming badbots.py generates files in waf_patterns/
61+
# Example (if badbots.py creates nginx/bots.conf):
62+
# python badbots.py
63+
echo "Placeholder for badbots.py execution"
64+
65+
- name: 🚀 Commit and Push Changes (if any)
7566
run: |
7667
git config user.name "github-actions[bot]"
77-
git config user.email "github-actions[bot]@users.noreply.github.com"
68+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
7869
git add .
79-
git commit -m "Update: [$(date)]" || echo "No changes to commit"
80-
git push
81-
continue-on-error: true # Continue even if no changes are made
70+
# Check if there are any changes *before* committing.
71+
if ! git diff --quiet --exit-code; then
72+
git commit -m "Update WAF rules [$(date +'%Y-%m-%d')]"
73+
git push
74+
else
75+
echo "No changes to commit."
76+
fi
77+
continue-on-error: true # Continue even if no changes
8278

83-
- name: 📦 Create Zip Files for Each Web Server
79+
- name: 📦 Create Zip Archives
8480
run: |
85-
mkdir -p zips
86-
zip -r zips/nginx_waf.zip waf_patterns/nginx/
87-
zip -r zips/apache_waf.zip waf_patterns/apache/
88-
zip -r zips/traefik_waf.zip waf_patterns/traefik/
89-
zip -r zips/haproxy_waf.zip waf_patterns/haproxy/
81+
mkdir -p dist
82+
(cd waf_patterns/nginx && zip -r ../../dist/nginx_waf.zip .)
83+
(cd waf_patterns/apache && zip -r ../../dist/apache_waf.zip .)
84+
(cd waf_patterns/traefik && zip -r ../../dist/traefik_waf.zip .)
85+
(cd waf_patterns/haproxy && zip -r ../../dist/haproxy_waf.zip .)
9086
9187
- name: 🗑️ Delete Existing 'latest' Tag and Release (if they exist)
9288
run: |
93-
# Delete the local 'latest' tag
94-
if git rev-parse --verify --quiet refs/tags/latest; then
95-
git tag -d latest
96-
fi
97-
98-
# Delete the remote 'latest' tag
99-
git push origin :refs/tags/latest || echo "Tag 'latest' does not exist on remote."
100-
101-
# Delete the 'latest' release (if it exists)
102-
gh release delete latest --yes || echo "Release 'latest' does not exist."
103-
89+
gh auth login --with-token <<< "$GITHUB_TOKEN"
90+
# Delete local tag
91+
git tag -d latest || true
92+
# Delete remote tag (force)
93+
git push --delete origin latest || true
94+
# Delete release, --yes for confirmation
95+
gh release delete latest --yes || true
10496
env:
10597
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
10698

107-
- name: 🚀 Create GitHub Release
108-
id: create_release
99+
100+
- name: 🚀 Create GitHub Release (if previous steps succeeded)
101+
if: success() # Only create release if previous steps were successful
109102
uses: actions/create-release@v1
110103
env:
111104
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
112105
with:
113-
tag_name: latest # Use "latest" as the tag name
106+
tag_name: latest
114107
release_name: Latest Release
115108
draft: false
116109
prerelease: false
117110

118-
- name: 📤 Upload Nginx WAF Zip to Release
111+
- name: 📤 Upload Nginx WAF Zip
112+
if: success()
119113
uses: actions/upload-release-asset@v1
120114
env:
121115
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
122116
with:
123117
upload_url: ${{ steps.create_release.outputs.upload_url }}
124-
asset_path: zips/nginx_waf.zip
118+
asset_path: dist/nginx_waf.zip
125119
asset_name: nginx_waf.zip
126120
asset_content_type: application/zip
127121

128-
- name: 📤 Upload Apache WAF Zip to Release
122+
- name: 📤 Upload Apache WAF Zip
123+
if: success()
129124
uses: actions/upload-release-asset@v1
130125
env:
131126
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
132127
with:
133128
upload_url: ${{ steps.create_release.outputs.upload_url }}
134-
asset_path: zips/apache_waf.zip
129+
asset_path: dist/apache_waf.zip
135130
asset_name: apache_waf.zip
136131
asset_content_type: application/zip
137132

138-
- name: 📤 Upload Traefik WAF Zip to Release
133+
- name: 📤 Upload Traefik WAF Zip
134+
if: success()
139135
uses: actions/upload-release-asset@v1
140136
env:
141137
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
142138
with:
143139
upload_url: ${{ steps.create_release.outputs.upload_url }}
144-
asset_path: zips/traefik_waf.zip
140+
asset_path: dist/traefik_waf.zip
145141
asset_name: traefik_waf.zip
146142
asset_content_type: application/zip
147143

148-
- name: 📤 Upload HAProxy WAF Zip to Release
144+
- name: 📤 Upload HAProxy WAF Zip
145+
if: success()
149146
uses: actions/upload-release-asset@v1
150147
env:
151148
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
152149
with:
153150
upload_url: ${{ steps.create_release.outputs.upload_url }}
154-
asset_path: zips/haproxy_waf.zip
151+
asset_path: dist/haproxy_waf.zip
155152
asset_name: haproxy_waf.zip
156153
asset_content_type: application/zip
157154

158-
- name: 🧹 Cleanup Cache (Optional)
159-
run: |
160-
rm -rf ~/.cache/pip
161-
if: always() # Run this step even if previous steps fail
155+
- name: 🧹 Clean Up (Optional)
156+
if: always() # Run cleanup even on failure
157+
run: rm -rf ~/.cache/pip
162158

163159
- name: 🚨 Notify on Failure (Optional)
164160
if: failure()
165161
run: |
166162
echo "🚨 Workflow failed! Please investigate."
167-
# Slack or email notification logic (add webhook or SMTP integration here).
163+
# Example: Send a Slack notification (requires a Slack webhook URL)
164+
# curl -X POST -H 'Content-type: application/json' --data '{"text":"WAF update workflow failed!"}' ${{ secrets.SLACK_WEBHOOK }}

0 commit comments

Comments
 (0)