From 1df2f4a716960ec459ef22e4a1c461f21bf1353e Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sun, 15 Jun 2025 10:43:38 -0500 Subject: [PATCH] add security scan for docker --- .github/workflows/docker-security-scan.yml | 61 ++++++++++++++++++++++ Dockerfile | 13 +++-- 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/docker-security-scan.yml diff --git a/.github/workflows/docker-security-scan.yml b/.github/workflows/docker-security-scan.yml new file mode 100644 index 0000000..b1b4ff4 --- /dev/null +++ b/.github/workflows/docker-security-scan.yml @@ -0,0 +1,61 @@ +name: Docker Security Scan + +on: + push: + branches: [ "main" ] + paths: + - 'Dockerfile' + - '.github/workflows/docker-security-scan.yml' + - 'mix.exs' + - 'mix.lock' + pull_request: + branches: [ "main" ] + paths: + - 'Dockerfile' + - '.github/workflows/docker-security-scan.yml' + - 'mix.exs' + - 'mix.lock' + schedule: + - cron: '0 0 * * 0' # Run weekly on Sundays at midnight + workflow_dispatch: # Allow manual triggers + +jobs: + scan: + name: Security Scan + runs-on: ubuntu-latest + permissions: + contents: read + security-events: write + actions: read + packages: read + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Build Docker image + run: docker build -t aprs:${{ github.sha }} . + + - name: Run Trivy vulnerability scanner + uses: aquasecurity/trivy-action@master + with: + image-ref: aprs:${{ github.sha }} + format: 'sarif' + output: 'trivy-results.sarif' + severity: 'CRITICAL,HIGH' + timeout: '10m' + scanners: 'vuln,config,secret' + + - name: Upload Trivy scan results to GitHub Security tab + uses: github/codeql-action/upload-sarif@v3 + if: always() + with: + sarif_file: 'trivy-results.sarif' + category: 'Trivy Scan' + + - name: Fail on CRITICAL vulnerabilities + if: ${{ failure() }} + run: | + echo "Critical vulnerabilities found in the Docker image." + echo "Please review the scan results in the Security tab." + exit 1 diff --git a/Dockerfile b/Dockerfile index cb1b021..a1616b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ # - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20250113-slim - for the release image # - https://pkgs.org/ - resource for finding needed packages # - Ex: hexpm/elixir:1.18.2-erlang-27.2-debian-bullseye-20250113-slim +# - https://hub.docker.com/r/aquasec/trivy - for security scanning # ARG ELIXIR_VERSION=1.18.2 ARG OTP_VERSION=27.2 @@ -18,7 +19,7 @@ ARG DEBIAN_VERSION=bullseye-20250113-slim ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}" ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}" -FROM ${BUILDER_IMAGE} as builder +FROM ${BUILDER_IMAGE} AS builder # install build dependencies RUN apt-get update -y && apt-get install -y build-essential git \ @@ -68,8 +69,8 @@ RUN mix release FROM ${RUNNER_IMAGE} RUN apt-get update -y && \ - apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates \ - && apt-get clean && rm -f /var/lib/apt/lists/*_* + apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates \ + && apt-get clean && rm -f /var/lib/apt/lists/*_* # Set the locale RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen @@ -95,3 +96,9 @@ USER nobody # ENTRYPOINT ["/tini", "--"] CMD ["/app/bin/server"] + +# Security scan stage +FROM aquasec/trivy:latest AS trivy +WORKDIR /workspace +COPY --from=builder /app/_build/prod/rel/aprs ./ +RUN trivy filesystem --no-progress --exit-code 1 --severity HIGH,CRITICAL ./