|
| 1 | +name: Unit and Security Scanning |
| 2 | +on: |
| 3 | + workflow_call: |
| 4 | + |
| 5 | +jobs: |
| 6 | + # Run unit test cases for the Docker image |
| 7 | + unit_test: |
| 8 | + name: Run unit test |
| 9 | + runs-on: ubuntu-latest |
| 10 | + needs: ['trivy_scans', 'owasp_zap_scan'] # Ensure this job runs after the security scans |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout repository |
| 14 | + uses: actions/checkout@v4 |
| 15 | + |
| 16 | + - name: Install dependencies |
| 17 | + run: | |
| 18 | + pip install -r requirements.txt |
| 19 | + |
| 20 | + - name: Run tests |
| 21 | + run: pytest tests/ |
| 22 | + |
| 23 | + # Scan the contianer and lists all security vulnerabilities |
| 24 | + trivy_scans: |
| 25 | + name: Run Trivy security scanner against the image |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Checkout code |
| 29 | + uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Build Docker Image |
| 32 | + run: | |
| 33 | + docker build -t python-fastapi:${{ github.sha }} . ###- This section needed to be added becasue the image was not persisting between jobs--## |
| 34 | +
|
| 35 | + - name: Run Trivy Vulnerability Scanner |
| 36 | + uses: aquasecurity/[email protected] |
| 37 | + with: |
| 38 | + image-ref: 'python-fastapi:${{ github.sha }}' |
| 39 | + format: 'sarif' |
| 40 | + output: 'trivy-results.sarif' |
| 41 | + severity: 'CRITICAL,HIGH' |
| 42 | + |
| 43 | + - name: Upload Trivy scan results to GitHub Security tab |
| 44 | + uses: github/codeql-action/upload-sarif@v3 |
| 45 | + with: |
| 46 | + sarif_file: 'trivy-results.sarif' |
| 47 | + |
| 48 | + owasp_zap_scan: |
| 49 | + runs-on: ubuntu-latest |
| 50 | + name: app scan |
| 51 | + steps: |
| 52 | + - name: Checkout |
| 53 | + uses: actions/checkout@v4 |
| 54 | + |
| 55 | + # Build and Tag Image |
| 56 | + # Run Docker Image in detached mode |
| 57 | + - name: Build Docker Image |
| 58 | + run: | |
| 59 | + docker build -t python-fastapi:${{ github.sha }} . |
| 60 | + docker run -d -p 8080:8080 python-fastapi:${{ github.sha }} |
| 61 | + |
| 62 | + - name: Wait for Docker container to be ready |
| 63 | + run: sleep 30 |
| 64 | + |
| 65 | + - name: Confirm Docker container is running |
| 66 | + run: docker ps |
| 67 | + |
| 68 | + # Run OWASP ZAP scan |
| 69 | + - name: zap scan |
| 70 | + |
| 71 | + with: |
| 72 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 73 | + docker_name: 'ghcr.io/zaproxy/zaproxy:stable' |
| 74 | + format: openapi |
| 75 | + target: 'http://0.0.0.0:8080' |
| 76 | + rules_file_name: '.zap/rules.tsv' |
| 77 | + cmd_options: '-a' |
| 78 | + allow_issue_writing: false |
0 commit comments