Skip to content

Commit

Permalink
Merge pull request #33 from ncats/SLS
Browse files Browse the repository at this point in the history
feat: Added Shift-left strategy to repo for snyk, codeql and zap scanning
  • Loading branch information
tsheils authored May 3, 2024
2 parents 19c5218 + a745035 commit 1357392
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/codeql-sls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Define the name of the workflow
name: "CodeQL SLS"

# Define when the workflow should be triggered (on push to a specific branch and pull requests to the master branch)
on:
push:
branches:
- development
pull_request:
branches:
- development
workflow_dispatch:

# Define the jobs that will be executed as part of the workflow
jobs:
analyze:
name: Analyze
runs-on: [ncats-awsci-ln-githubrunner-dv-v-01, prod, ncats-aws-devops]

# Define the matrix strategy for parallel runs with different languages
strategy:
fail-fast: false
matrix:
language: [ 'javascript-typescript' ]

steps:
# Step 1: Checkout repository
- name: Checkout repository
uses: actions/checkout@v4

# Step 2: Initialize CodeQL for scanning
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}

# Step 3: Autobuild the code.
- name: Autobuild
uses: github/codeql-action/autobuild@v3

# Step 4: Analyzes the code using CodeQL, with the analysis category based on the matrix language.
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"

# Step 5: Generate Security Report
- name: Generate Security Report
uses: rsdmike/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}

# Step 6: Uploads artifacts (PDF reports) generated during the workflow to download.
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: codeql-reports
path: ./*.pdf
152 changes: 152 additions & 0 deletions .github/workflows/synk-zap-sls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Define the name of the workflow
name: snyk-zap

# Define when the workflow should be triggered (on push to a specific branch and pull requests to the master branch)
on:
push:
branches:
- development
pull_request:
branches:
- development
workflow_dispatch:

# Define the jobs that will be executed as part of the workflow
jobs:
# Job to build and push the ZAP Docker image to Docker Hub
Snyk-Docker-Image:
runs-on: [ncats-awsci-ln-githubrunner-dv-v-01, prod, ncats-aws-devops]
permissions:
actions: read
contents: read
security-events: write
issues: write

outputs:
build_version: ${{ steps.get_build_version.outputs.build_version }}

steps:
# Step 1: Checkout repository
- name: Checkout code
uses: actions/checkout@v4

# Step 2: Generate Build Version Number
- name: Generate Build Version Number
id: GET_BUILD_VERSION
run: |
# Get the last recorded date from the environment variable
LAST_DATE=$(date -d "$LAST_BUILD_DATE" +'%Y-%m-%d' 2>/dev/null || echo "")
# Get the current date
CURRENT_DATE=$(date +'%Y-%m-%d')
echo "Last recorded date: $LAST_DATE"
echo "Current date: $CURRENT_DATE"

# Check if it's a new day
if [ "$LAST_DATE" != "$CURRENT_DATE" ]; then
# Reset BUILDS_TODAY to 0 for the new day
BUILDS_TODAY=0
echo "Resetting BUILDS_TODAY to 0 for the new day"
else
# Calculate the number of builds today
BUILDS_TODAY=$(seq -f v$GITHUB_RUN_NUMBER.%g $(($GITHUB_RUN_NUMBER - 1)) | wc -l)
echo "Incrementing BUILDS_TODAY"
fi

# Store the current date for the next run
echo "LAST_BUILD_DATE=$CURRENT_DATE" >> $GITHUB_ENV

# Generate the build version with the number of builds today
BUILD_VERSION_GENERATED=$(date +v%Y.%m%d.$BUILDS_TODAY)
echo "Generated Build Version: $BUILD_VERSION_GENERATED"
echo "BUILD_VERSION=$BUILD_VERSION_GENERATED" >> $GITHUB_ENV
echo "BUILD=true" >> $GITHUB_ENV
echo "::set-output name=build_version::$BUILD_VERSION_GENERATED"

# # Step 3: Login to Dockerhub
# - name: Login to Dockerhub
# run: docker login -u "${{ secrets.DKRHUB_NCATSSVCDVOPS_USERNAME }}" -p "${{ secrets.DKRHUB_NCATSSVCDVOPS_TOKEN_WRITE }}"

# Step 4: Build a Docker image
- name: Build a Docker image
run: docker build --no-cache -f ./Dockerfile --build-arg NPM_TOKEN=${{ secrets.NPM_INSTALL_TOKEN }} --build-arg BUILD_VERSION=$BUILD_VERSION -t ncats/smartgraph_frontend:$BUILD_VERSION .

# Step 5: Run Snyk to check Docker image for vulnerabilities
- name: Run Snyk to check Docker image for vulnerabilities
continue-on-error: true
uses: snyk/actions/docker@master
id: docker-image-scan
env:
SNYK_TOKEN: ${{ secrets.SNYK_CLI }}
with:
image: ncats/smartgraph_frontend:$BUILD_VERSION
args: --sarif-file-output=snyk.sarif --file=Dockerfile

- name: Replace security-severity undefined for license-related findings
run: |
sed -i 's/"security-severity": "undefined"/"security-severity": "0"/g' snyk.sarif
sed -i 's/"security-severity": "null"/"security-severity": "0"/g' snyk.sarif
# Step 6: Upload result to GitHub Code Scanning
- name: Upload result to GitHub Code Scanning
# continue-on-error: true
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: snyk.sarif

# Step 7: Generate Security Report
- name: Generate Security Report
uses: rsdmike/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}

# Step 8: Uploads artifacts (PDF reports) generated during the workflow to download.
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: snyk-reports
path: ./*.pdf

ZAP-Docker-Scan:
needs: Snyk-Docker-Image
runs-on: [ncats-awsci-ln-githubrunner-dv-v-01, prod, ncats-aws-devops]
permissions:
actions: read
contents: read
security-events: write
issues: write

steps:
# Step 1: Get BUILD_VERSION from Snyk-Docker-Image job
- name: Get BUILD_VERSION from Snyk-Docker-Image job
id: get_runner_ip
run: |
echo "BUILD_VERSION=${{ needs.Snyk-Docker-Image.outputs.build_version }}" >> $GITHUB_ENV
echo "::set-output name=runner_ip::$(hostname -I | cut -d' ' -f1)"
# Step 2: Add the command to start Docker image on port 4200
- name: Start Docker image on port 4200
run: docker run -d -p 4200:4200 ncats/smartgraph_frontend:${{ needs.Snyk-Docker-Image.outputs.build_version }}

# Step 3: ZAP BASELINE SCAN
- name: ZAP base Scan
uses: zaproxy/[email protected]
with:
target: 'http://${{ steps.get_runner_ip.outputs.runner_ip }}:4200' # ip address of the runner
docker_name: 'ghcr.io/zaproxy/zaproxy:stable'
token: ${{ secrets.GITHUB_TOKEN }}
fail_action: false

# Step 4: Create SARIF file from ZAP results
- name: Create SARIF file from ZAP results
uses: SvanBoxel/zaproxy-to-ghas@main

# Step 5: Upload SARIF file to GitHub Code Scanning
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif

# Step 6: Stop and remove the Docker container
- name: Stop and remove Docker container
run: docker stop $(docker ps -q --filter ancestor=ncats/smartgraph_frontend:$BUILD_VERSION) && docker rm $(docker ps -a -q --filter ancestor=ncats/smartgraph_frontend:$BUILD_VERSION) || true

0 comments on commit 1357392

Please sign in to comment.