|
| 1 | +name: ES|QL Validation |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + branches: [ "*" ] |
| 5 | +jobs: |
| 6 | + build-and-validate: |
| 7 | + runs-on: ubuntu-latest |
| 8 | + |
| 9 | + steps: |
| 10 | + - name: Setup Detection Rules |
| 11 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 |
| 12 | + with: |
| 13 | + fetch-depth: 0 |
| 14 | + path: detection-rules |
| 15 | + |
| 16 | + - name: Check if new or modified rule files are ESQL rules |
| 17 | + id: check-esql |
| 18 | + run: | |
| 19 | + cd detection-rules |
| 20 | +
|
| 21 | + # Check if the event is a push |
| 22 | + if [ "${{ github.event_name }}" = "push" ]; then |
| 23 | + echo "Triggered by a push event. Setting run_esql=true." |
| 24 | + echo "run_esql=true" >> $GITHUB_ENV |
| 25 | + exit 0 |
| 26 | + fi |
| 27 | +
|
| 28 | + MODIFIED_FILES=$(git diff --name-only --diff-filter=AM HEAD~1 | grep '^rules/.*\.toml$' || true) |
| 29 | + if [ -z "$MODIFIED_FILES" ]; then |
| 30 | + echo "No modified or new .toml files found. Skipping workflow." |
| 31 | + echo "run_esql=false" >> $GITHUB_ENV |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | +
|
| 35 | + if ! grep -q 'type = "esql"' $MODIFIED_FILES; then |
| 36 | + echo "No 'type = \"esql\"' found in the modified .toml files. Skipping workflow." |
| 37 | + echo "run_esql=false" >> $GITHUB_ENV |
| 38 | + exit 0 |
| 39 | + fi |
| 40 | +
|
| 41 | + echo "run_esql=true" >> $GITHUB_ENV |
| 42 | +
|
| 43 | + - name: Check out repository |
| 44 | + env: |
| 45 | + DR_CLOUD_ID: ${{ secrets.dr_cloud_id }} |
| 46 | + DR_API_KEY: ${{ secrets.dr_api_key }} |
| 47 | + if: ${{ !env.DR_CLOUD_ID && !env.DR_API_KEY && env.run_esql == 'true' }} |
| 48 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 |
| 49 | + with: |
| 50 | + path: elastic-container |
| 51 | + repository: peasead/elastic-container |
| 52 | + |
| 53 | + - name: Build and run containers |
| 54 | + env: |
| 55 | + DR_CLOUD_ID: ${{ secrets.dr_cloud_id }} |
| 56 | + DR_API_KEY: ${{ secrets.dr_api_key }} |
| 57 | + if: ${{ !env.DR_CLOUD_ID && !env.DR_API_KEY && env.run_esql == 'true' }} |
| 58 | + run: | |
| 59 | + cd elastic-container |
| 60 | + GENERATED_PASSWORD=$(openssl rand -base64 16) |
| 61 | + sed -i "s|changeme|$GENERATED_PASSWORD|" .env |
| 62 | + echo "::add-mask::$GENERATED_PASSWORD" |
| 63 | + echo "GENERATED_PASSWORD=$GENERATED_PASSWORD" >> $GITHUB_ENV |
| 64 | + set -x |
| 65 | + bash elastic-container.sh start |
| 66 | + |
| 67 | + - name: Get API Key and setup auth |
| 68 | + env: |
| 69 | + DR_CLOUD_ID: ${{ secrets.dr_cloud_id }} |
| 70 | + DR_API_KEY: ${{ secrets.dr_api_key }} |
| 71 | + DR_ELASTICSEARCH_URL: "https://localhost:9200" |
| 72 | + ES_USER: "elastic" |
| 73 | + ES_PASSWORD: ${{ env.GENERATED_PASSWORD }} |
| 74 | + if: ${{ !env.DR_CLOUD_ID && !env.DR_API_KEY && env.run_esql == 'true' }} |
| 75 | + run: | |
| 76 | + cd detection-rules |
| 77 | + response=$(curl -k -X POST -u "$ES_USER:$ES_PASSWORD" -H "Content-Type: application/json" -d '{ |
| 78 | + "name": "tmp-api-key", |
| 79 | + "expiration": "1d" |
| 80 | + }' "$DR_ELASTICSEARCH_URL/_security/api_key") |
| 81 | +
|
| 82 | + DR_API_KEY=$(echo "$response" | jq -r '.encoded') |
| 83 | + echo "::add-mask::$DR_API_KEY" |
| 84 | + echo "DR_API_KEY=$DR_API_KEY" >> $GITHUB_ENV |
| 85 | +
|
| 86 | + - name: Set up Python 3.13 |
| 87 | + if: ${{ env.run_esql == 'true' }} |
| 88 | + uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6 |
| 89 | + with: |
| 90 | + python-version: '3.13' |
| 91 | + |
| 92 | + - name: Install dependencies |
| 93 | + if: ${{ env.run_esql == 'true' }} |
| 94 | + run: | |
| 95 | + cd detection-rules |
| 96 | + python -m pip install --upgrade pip |
| 97 | + pip cache purge |
| 98 | + pip install .[dev] |
| 99 | +
|
| 100 | + - name: Remote Test ESQL Rules |
| 101 | + if: ${{ env.run_esql == 'true' }} |
| 102 | + env: |
| 103 | + DR_CLOUD_ID: ${{ secrets.dr_cloud_id || '' }} |
| 104 | + DR_KIBANA_URL: ${{ secrets.dr_cloud_id == '' && 'https://localhost:5601' || '' }} |
| 105 | + DR_ELASTICSEARCH_URL: ${{ secrets.dr_cloud_id == '' && 'https://localhost:9200' || '' }} |
| 106 | + DR_API_KEY: ${{ secrets.dr_api_key || env.DR_API_KEY }} |
| 107 | + DR_IGNORE_SSL_ERRORS: ${{ secrets.dr_cloud_id == '' && 'true' || '' }} |
| 108 | + run: | |
| 109 | + cd detection-rules |
| 110 | + python -m detection_rules dev test esql-remote-validation |
0 commit comments