Skip to content

Commit

Permalink
Add a ruletype that fails if no workflows contain pull_request
Browse files Browse the repository at this point in the history
Having a workflow with a pull_request is indicative of having a CI/CD
pipeline.
  • Loading branch information
jhrozek committed Oct 24, 2024
1 parent ddccb78 commit 19d0a2a
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 0 deletions.
49 changes: 49 additions & 0 deletions rule-types/github/workflow_pull_request.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
tests:
- name: "Has a pull_request event"
def: {}
params: {}
expect: "pass"
git:
repo_base: "has_pull_request_direct"
- name: "Does not have a pull_request event"
def: {}
params: {}
expect: "fail"
git:
repo_base: "does_not_have_pull_request_array"
- name: "Has a pull_request event in an array"
def: {}
params: {}
expect: "pass"
git:
repo_base: "has_pull_request_array"
- name: "Does not have a pull_request event in an array"
def: {}
params: {}
expect: "fail"
git:
repo_base: "does_not_have_pull_request_array"
- name: "Has a pull_request event in an object"
def: {}
params: {}
expect: "pass"
git:
repo_base: "has_pull_request_object"
- name: "Does not have a pull_request event in an object"
def: {}
params: {}
expect: "fail"
git:
repo_base: "does_not_have_pull_request_object"
- name: "None of multiple workflows have a pull_request event"
def: {}
params: {}
expect: "fail"
git:
repo_base: "does_not_have_pull_request_multiple"
- name: "One of multiple workflows has a pull_request event"
def: {}
params: {}
expect: "pass"
git:
repo_base: "has_pull_request_multiple"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# INSECURE. Provided as an example only.
on: push

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# INSECURE. Provided as an example only.
on:
- push
- release

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# INSECURE. Provided as an example only.
on:
- push
- release

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# INSECURE. Provided as an example only.
on: push

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
on:
push:
branches: [master]
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
on:
push:
branches: [master]
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# INSECURE. Provided as an example only.
on:
- push
- pull_request

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# INSECURE. Provided as an example only.
on:
pull_request

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# INSECURE. Provided as an example only.
on:
- push
- release

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# INSECURE. Provided as an example only.
on: push

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
on:
push:
branches: [master]
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
on:
push:
branches: [master]
pull_request:
types: [opened, synchronize]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
on:
push:
branches: [master]
pull_request:
types: [opened, synchronize]
55 changes: 55 additions & 0 deletions rule-types/github/workflow_pull_request.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
version: v1
release_phase: beta
type: rule-type
name: workflow_pull_request
display_name: Ensure there exists a GitHub Actions workflow that uses the pull_request event
short_failure_message: GitHub Actions workflows do not use the pull_request event
severity:
value: high
context:
provider: github
description: |
Alerts if there are no GitHub Actions workflows that use the pull_request event.
Workflows that use the pull_request event are often used to run e.g. CI/CD
pipelines and are indicators of a healthy repository.
guidance: |
Ensure that there exists at least one GitHub Actions workflow that uses the
pull_request event.
def:
in_entity: repository
rule_schema:
type: object
ingest:
type: git
git: {}
eval:
type: rego
rego:
type: deny-by-default
def: |
package minder
import future.keywords.if
# List all workflows
workflows := file.ls("./.github/workflows")
# Read all workflows and check for pull_request trigger
allow if {
some w
# Read the workflow file
workflowstr := file.read(workflows[w])
parsed := parse_yaml(workflowstr)
print(parsed)
jq_query := ".on | (type == \"string\" and . == \"pull_request\") or (type == \"object\" and has(\"pull_request\")) or (type == \"array\" and any(.[]; . == \"pull_request\"))"
jq.is_true(parsed, jq_query)
}
# Defines the configuration for alerting on the rule
alert:
type: security_advisory
security_advisory: {}

0 comments on commit 19d0a2a

Please sign in to comment.