Skip to content

Commit 7545d4d

Browse files
author
Gaurav Patil
committed
Add security scan pipeline with scan.sh
1 parent 2a121c7 commit 7545d4d

3 files changed

Lines changed: 194 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Security Scan
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
scan:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- name: Install jq
15+
run: sudo apt-get install -y jq
16+
17+
- name: Setup Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: "1.22"
21+
22+
- name: Make script executable
23+
run: chmod +x scan.sh
24+
25+
- name: Run scan
26+
run: ./scan.sh
27+
28+
- name: Upload reports
29+
uses: actions/upload-artifact@v3
30+
with:
31+
name: security-reports
32+
path: security-reports

Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ---------- Build Stage ----------
2+
FROM golang:1.25-alpine AS build
3+
4+
# Set working directory inside the container
5+
WORKDIR /app
6+
7+
# Copy Go module files and download dependencies first (better cache)
8+
COPY go.mod go.sum ./
9+
RUN go mod download
10+
11+
# Copy the rest of the project
12+
COPY . .
13+
14+
# Build the DockPulse binary
15+
# (adjust ./cmd/dashboard if your main.go is in a different path)
16+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o dockpulse ./cmd/dashboard
17+
18+
# ---------- Runtime Stage ----------
19+
FROM alpine:latest
20+
21+
# Where our app will run inside the container
22+
WORKDIR /app
23+
24+
# Copy the built binary from the build stage
25+
COPY --from=build /app/dockpulse /usr/local/bin/dockpulse
26+
27+
# Ensure it is executable
28+
RUN chmod +x /usr/local/bin/dockpulse
29+
30+
# This is what will run when container starts
31+
ENTRYPOINT ["dockpulse"]

scan.sh

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/bin/bash
2+
3+
# ==========================================
4+
# Gaurav's Go Security + Lint Scanner
5+
# ==========================================
6+
7+
set -euo pipefail
8+
9+
# Colors
10+
RED='\033[0;31m'
11+
GREEN='\033[0;32m'
12+
YELLOW='\033[1;33m'
13+
BLUE='\033[0;34m'
14+
NC='\033[0m'
15+
16+
PROJECT_NAME="DevOps Dashboard"
17+
REPORT_DIR="security-reports"
18+
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
19+
20+
echo -e "${BLUE}🔍 Starting Security Scan for: ${GREEN}$PROJECT_NAME${NC}"
21+
echo ""
22+
23+
# ==========================================
24+
# Validate Go project
25+
# ==========================================
26+
if [ ! -f "go.mod" ]; then
27+
echo -e "${RED}❌ go.mod not found. Run script from project root.${NC}"
28+
exit 1
29+
fi
30+
31+
echo -e "${GREEN}✓ go.mod found${NC}"
32+
PROJECT_ROOT=$(pwd)
33+
34+
# ==========================================
35+
# Prepare Report directory
36+
# ==========================================
37+
rm -rf "$REPORT_DIR"
38+
mkdir -p "$REPORT_DIR"
39+
40+
echo -e "${GREEN}✓ Clean report folder created: $REPORT_DIR${NC}"
41+
42+
# ==========================================
43+
# Install Tools
44+
# ==========================================
45+
echo -e "${BLUE}📦 Installing Go Security Tools...${NC}"
46+
47+
go install golang.org/x/vuln/cmd/govulncheck@latest
48+
go install github.com/securego/gosec/v2/cmd/gosec@latest
49+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
50+
51+
export PATH="$PATH:$(go env GOPATH)/bin"
52+
53+
# ==========================================
54+
# Download Dependencies
55+
# ==========================================
56+
go mod download
57+
go mod verify
58+
59+
echo -e "${GREEN}✓ Dependencies installed${NC}"
60+
61+
# ==========================================
62+
# 1️⃣ govulncheck – Dependency Vulnerability Scan
63+
# ==========================================
64+
echo -e "${BLUE}🔍 Running govulncheck...${NC}"
65+
66+
VULN_JSON="$REPORT_DIR/govulncheck-$TIMESTAMP.json"
67+
if govulncheck -json ./... > "$VULN_JSON" 2>/dev/null; then
68+
VULN_STATUS="PASSED"
69+
else
70+
VULN_STATUS="FAILED"
71+
fi
72+
73+
VULN_COUNT=$(jq '[.finding] | length' "$VULN_JSON" 2>/dev/null || echo 0)
74+
echo -e "${GREEN}✓ govulncheck done. Issues: ${VULN_COUNT}${NC}"
75+
76+
# ==========================================
77+
# 2️⃣ gosec – Static Code Security Analysis
78+
# ==========================================
79+
echo -e "${BLUE}🔒 Running gosec security analysis...${NC}"
80+
81+
GOSEC_JSON="$REPORT_DIR/gosec-$TIMESTAMP.json"
82+
GOSEC_HTML="$REPORT_DIR/gosec-$TIMESTAMP.html"
83+
84+
gosec -fmt=json -out="$GOSEC_JSON" ./... >/dev/null 2>&1 || true
85+
gosec -fmt=html -out="$GOSEC_HTML" ./... >/dev/null 2>&1 || true
86+
87+
ISSUES_FOUND=$(jq -r '.Stats.found // 0' "$GOSEC_JSON" 2>/dev/null || echo 0)
88+
echo -e "${GREEN}✓ gosec completed. Issues: ${ISSUES_FOUND}${NC}"
89+
90+
# ==========================================
91+
# 3️⃣ golangci-lint – Code Quality
92+
# ==========================================
93+
echo -e "${BLUE}📊 Running golangci-lint...${NC}"
94+
95+
LINT_JSON="$REPORT_DIR/golangci-$TIMESTAMP.json"
96+
golangci-lint run --out-format json ./... > "$LINT_JSON" 2>&1 || true
97+
98+
LINT_ISSUES=$(jq '[.Issues[]] | length' "$LINT_JSON" 2>/dev/null || echo 0)
99+
echo -e "${GREEN}✓ Linting done. Issues: ${LINT_ISSUES}${NC}"
100+
101+
# ==========================================
102+
# Summary Report
103+
# ==========================================
104+
TOTAL_CRITICAL=$((ISSUES_FOUND + VULN_COUNT))
105+
106+
SUMMARY="$REPORT_DIR/summary-$TIMESTAMP.txt"
107+
108+
echo "==================== SECURITY SUMMARY ====================" > "$SUMMARY"
109+
echo "Project: $PROJECT_NAME" >> "$SUMMARY"
110+
echo "Time: $TIMESTAMP" >> "$SUMMARY"
111+
echo "" >> "$SUMMARY"
112+
echo "govulncheck Issues : $VULN_COUNT" >> "$SUMMARY"
113+
echo "gosec Issues : $ISSUES_FOUND" >> "$SUMMARY"
114+
echo "Lint Issues : $LINT_ISSUES" >> "$SUMMARY"
115+
echo "" >> "$SUMMARY"
116+
echo "Total Critical : $TOTAL_CRITICAL" >> "$SUMMARY"
117+
echo "==========================================================" >> "$SUMMARY"
118+
119+
echo ""
120+
echo -e "${BLUE}📄 Summary Report: ${GREEN}$SUMMARY${NC}"
121+
122+
# ==========================================
123+
# Exit with correct status code
124+
# ==========================================
125+
if [ "$TOTAL_CRITICAL" -gt 0 ]; then
126+
echo -e "${RED}❌ Critical issues found. Failing pipeline.${NC}"
127+
exit 1
128+
else
129+
echo -e "${GREEN}✅ No critical issues. Scan successful.${NC}"
130+
exit 0
131+
fi

0 commit comments

Comments
 (0)