Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [ ] I have added tests to cover my changes
- [ ] I have updated the documentation accordingly
- [ ] This PR is a result of pair or mob programming
- [ ] If I have used the 'skip-trivy-package' label I have done so responsibly and in the knowledge that this is being fixed as part of a separate ticket/PR.

---

Expand Down
18 changes: 18 additions & 0 deletions .github/actions/trivy-iac/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Trivy IaC Scan"
description: "Scan Terraform IaC using Trivy"
runs:
using: "composite"
steps:
- name: "Trivy Terraform IaC Scan"
shell: bash
run: |
components_exit_code=0
modules_exit_code=0

./scripts/terraform/trivy-scan.sh --mode iac ./infrastructure/terraform/components || components_exit_code=$?
./scripts/terraform/trivy-scan.sh --mode iac ./infrastructure/terraform/modules || modules_exit_code=$?

if [ $components_exit_code -ne 0 ] || [ $modules_exit_code -ne 0 ]; then
echo "Trivy misconfigurations detected."
exit 1
fi
16 changes: 16 additions & 0 deletions .github/actions/trivy-package/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: "Trivy Package Scan"
description: "Scan project packages using Trivy"
runs:
using: "composite"
steps:
- name: "Trivy Package Scan"
shell: bash
run: |
exit_code=0

./scripts/terraform/trivy-scan.sh --mode package . || exit_code=$?

if [ $exit_code -ne 0 ]; then
echo "Trivy has detected package vulnerablilites. Please refer to https://nhsd-confluence.digital.nhs.uk/spaces/RIS/pages/1257636917/PLAT-KOP-012+-+Trivy+Pipeline+Vulnerability+Scanning+Exemption"
exit 1
fi
33 changes: 31 additions & 2 deletions .github/workflows/cicd-1-pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
terraform_version: ${{ steps.variables.outputs.terraform_version }}
version: ${{ steps.variables.outputs.version }}
does_pull_request_exist: ${{ steps.pr_exists.outputs.does_pull_request_exist }}
pr_number: ${{ steps.pr_exists.outputs.pr_number }}
skip_trivy_package: ${{ steps.skip_trivy.outputs.skip_trivy_package }}
steps:
- name: "Checkout code"
uses: actions/checkout@v4
Expand All @@ -47,12 +49,38 @@ jobs:
run: |
branch_name=${GITHUB_HEAD_REF:-$(echo $GITHUB_REF | sed 's#refs/heads/##')}
echo "Current branch is '$branch_name'"
if gh pr list --head $branch_name | grep -q .; then
echo "Pull request exists"

pr_json=$(gh pr list --head "$branch_name" --state open --json number --limit 1)
pr_number=$(echo "$pr_json" | jq -r '.[0].number // empty')

if [[ -n "$pr_number" ]]; then
echo "Pull request exists: #$pr_number"
echo "does_pull_request_exist=true" >> $GITHUB_OUTPUT
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
else
echo "Pull request doesn't exist"
echo "does_pull_request_exist=false" >> $GITHUB_OUTPUT
echo "pr_number=" >> $GITHUB_OUTPUT
fi
- name: "Determine if Trivy package scan should be skipped"
id: skip_trivy
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ steps.pr_exists.outputs.pr_number }}
run: |
if [[ -z "$PR_NUMBER" ]]; then
echo "No pull request detected; Trivy package scan will run."
echo "skip_trivy_package=false" >> $GITHUB_OUTPUT
exit 0
fi

labels=$(gh pr view "$PR_NUMBER" --json labels --jq '.labels[].name')
echo "Labels on PR #$PR_NUMBER: $labels"

if echo "$labels" | grep -Fxq 'skip-trivy-package'; then
echo "skip_trivy_package=true" >> $GITHUB_OUTPUT
else
echo "skip_trivy_package=false" >> $GITHUB_OUTPUT
fi
- name: "List variables"
run: |
Expand All @@ -76,6 +104,7 @@ jobs:
build_epoch: "${{ needs.metadata.outputs.build_epoch }}"
nodejs_version: "${{ needs.metadata.outputs.nodejs_version }}"
python_version: "${{ needs.metadata.outputs.python_version }}"
skip_trivy_package: ${{ needs.metadata.outputs.skip_trivy_package == 'true' }}
terraform_version: "${{ needs.metadata.outputs.terraform_version }}"
version: "${{ needs.metadata.outputs.version }}"
secrets: inherit
Expand Down
34 changes: 28 additions & 6 deletions .github/workflows/stage-1-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ on:
description: "Python version, set by the CI/CD pipeline workflow"
required: true
type: string
skip_trivy_package:
description: "Skip Trivy package scan when true"
type: boolean
default: false
terraform_version:
description: "Terraform version, set by the CI/CD pipeline workflow"
required: true
Expand Down Expand Up @@ -146,21 +150,39 @@ jobs:
uses: actions/checkout@v4
- name: "Lint Terraform"
uses: ./.github/actions/lint-terraform
trivy:
name: "Trivy Scan"
trivy-iac:
name: "Trivy IaC Scan"
permissions:
contents: read
runs-on: ubuntu-latest
timeout-minutes: 5
timeout-minutes: 10
needs: detect-terraform-changes
if: needs.detect-terraform-changes.outputs.terraform_changed == 'true'
steps:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Setup ASDF"
uses: asdf-vm/actions/setup@v4
uses: asdf-vm/actions/setup@1902764435ca0dd2f3388eea723a4f92a4eb8302
- name: "Perform Setup"
uses: ./.github/actions/setup
- name: "Trivy IaC Scan"
uses: ./.github/actions/trivy-iac
trivy-package:
if: ${{ !inputs.skip_trivy_package }}
name: "Trivy Package Scan"
permissions:
contents: read
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Setup ASDF"
uses: asdf-vm/actions/setup@1902764435ca0dd2f3388eea723a4f92a4eb8302
- name: "Perform Setup"
uses: ./.github/actions/setup
- name: "Trivy Scan"
uses: ./.github/actions/trivy
- name: "Trivy Package Scan"
uses: ./.github/actions/trivy-package
count-lines-of-code:
name: "Count lines of code"
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion docs/adr/assets/ADR-003/examples/golang/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.0

require (
github.com/go-resty/resty/v2 v2.7.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang-jwt/jwt v5.3.0+incompatible
)

require golang.org/x/net v0.23.0 // indirect
1 change: 1 addition & 0 deletions scripts/config/trivy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ exit-code: 1 # When issues are found
scan:
skip-files:
- "**/.terraform/**/*"
- "**/node_modules/**/*"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ itsdangerous==2.1.2
Jinja2==3.1.5
MarkupSafe==2.1.3
pip==23.3
setuptools==78.1.0
setuptools==78.1.1
Werkzeug==3.0.6
wheel==0.41.1
WTForms==3.0.1
8 changes: 5 additions & 3 deletions scripts/githooks/check-file-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ function main() {
;;
esac

if command -v editorconfig > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
if command -v editorconfig-checker > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
echo "Running editorconfig-checker natively"
filter="$filter" dry_run_opt="${dry_run_opt:-}" run-editorconfig-natively
else
echo "Running editorconfig-checker in Docker"
filter="$filter" dry_run_opt="${dry_run_opt:-}" run-editorconfig-in-docker
fi
}
Expand All @@ -80,7 +82,7 @@ function main() {
function run-editorconfig-natively() {

# shellcheck disable=SC2046,SC2086
editorconfig \
editorconfig-checker \
--exclude '.git/' $dry_run_opt $($filter)
}

Expand All @@ -101,7 +103,7 @@ function run-editorconfig-in-docker() {
docker run --rm --platform linux/amd64 \
--volume "$PWD":/check \
"$image" \
sh -c "ec --exclude '.git/' $dry_run_opt \$($filter) /dev/null"
sh -c "set -x; ec --exclude '.git/' $dry_run_opt \$($filter) /dev/null"
}

# ==============================================================================
Expand Down
Loading
Loading