-
Notifications
You must be signed in to change notification settings - Fork 1
192 lines (156 loc) · 7.48 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
name: SecureCodeBox Penetration Tests
on:
push:
branches:
- main
workflow_dispatch:
jobs:
penetration-tests:
runs-on: ubuntu-latest
steps:
# Step 1: Set up Kubernetes Kind cluster
- name: Set up kind cluster
uses: engineerd/setup-kind@v0.5.0
with:
version: "v0.17.0"
# Step 2: Configure kubectl
- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: v1.27.1
# Step 3: Install Helm for SecureCodeBox operator installation
- name: Install Helm
uses: azure/setup-helm@v1
with:
version: v3.8.0
# Step 4: Deploy SecureCodeBox Operator using Helm from the OCI registry
- name: Deploy SecureCodeBox
run: |
helm --namespace securecodebox-system upgrade --install --create-namespace securecodebox-operator oci://ghcr.io/securecodebox/helm/operator
# Step 5: Deploy Juice-Shop demo target
- name: Deploy Juice-Shop Demo Target
run: |
helm upgrade --install juice-shop oci://ghcr.io/securecodebox/helm/juice-shop
# Step 6: Deploy ZAP-Advanced scanner
- name: Deploy ZAP-Advanced Scanner
run: |
helm upgrade --install zap-advanced oci://ghcr.io/securecodebox/helm/zap-advanced
# Step 7: Initiate ZAP Scan on the Juice-Shop target
- name: Download scan.yaml
run: |
curl -O https://raw.githubusercontent.com/GHARBIyasmine/SecureCodeBox-Penetration-testing/main/scan.yaml
- name: Apply scan.yaml
run: kubectl apply -f scan.yaml
# Step 8: Wait for ZAP Scan Completion
- name: Wait for ZAP Scan Results
run: |
# Start watch mode for kubectl get scan
#kubectl get scan zap-authenticated-full-scan-juiceshop -o wide --watch &
#kubectl wait --for=condition=Done scan/zap-authenticated-full-scan-juiceshop --timeout=1800s
# Continuously check scan status and print it
# Poll for the scan state to become "Done"
while true; do
# Fetch the scan status and check for "Done" state
scan_state=$(kubectl get scan zap-authenticated-full-scan-juiceshop -o=jsonpath='{.status.state}')
# Print the current scan state for debugging
echo "Current scan state: $scan_state"
# Break out of loop if scan is done
if [[ "$scan_state" == "Done" ]]; then
echo "Scan completed!"
break
fi
# Wait 30 seconds before checking again
sleep 30
done
- name: Install jq
run: sudo apt-get install -y jq
- name: Checkout repository
uses: actions/checkout@v3
# Step 9: Retrieve and save the ZAP scan results
- name: Retrieve ZAP Scan Results
run: |
kubectl port-forward -n securecodebox-system service/securecodebox-operator-minio 9000:9000 &
sleep 5
curl -O https://dl.min.io/client/mc/release/linux-amd64/mc
chmod +x mc
sudo mv mc /usr/local/bin
mc alias set local http://localhost:9000 admin password
scan_uid=$(kubectl get scan zap-authenticated-full-scan-juiceshop -o=jsonpath='{.metadata.uid}')
echo "SCAN_UID=${scan_uid}" >> $GITHUB_ENV
scan_name=$(kubectl get scan zap-authenticated-full-scan-juiceshop -o=jsonpath='{.metadata.name}')
scan_findings_categories=$(kubectl get scan zap-authenticated-full-scan-juiceshop -o=jsonpath='{.status.findings.categories}')
scan_findings_categories=$(echo "$scan_findings_categories" | tr -d '\n')
scan_findings_count=$(kubectl get scan zap-authenticated-full-scan-juiceshop -o=jsonpath='{.status.findings.count}')
scan_findings_count=$(echo "$scan_findings_count" | tr -d '\n')
echo "Categories: ${scan_findings_categories}"
echo "Count: ${scan_findings_count}"
# Check if findings count is zero
if [[ "$scan_findings_count" -eq 0 ]]; then
echo "No issues found. Pipeline passes."
exit 0
fi
# Create summary file and findings file
summary_file="$GITHUB_WORKSPACE/report-summary-${scan_uid}.json"
echo "{
\"scan_name\": \"${scan_name}\",
\"scan_uid\": \"${scan_uid}\",
\"findings\": {
\"count\": ${scan_findings_count},
\"categories\": ${scan_findings_categories}
}
}" > ${summary_file}
mc cp local/securecodebox/scan-${scan_uid}/findings.json $GITHUB_WORKSPACE/findings.json
echo "Reports saved locally."
# Step 3: Check for Baseline and Compare Reports
- name: Check Baseline and Compare
run: |
BASELINE_REPORT=$(ls $GITHUB_WORKSPACE | grep '^report-summary-.*\.json$' | head -n 1)
if [[ -z "$BASELINE_REPORT" ]]; then
echo "::warning::No baseline report found. New issues detected."
exit 1
fi
echo "Baseline found. Proceeding with comparison."
CURRENT_REPORT="$GITHUB_WORKSPACE/report-summary-${SCAN_UID}.json"
BASELINE_CATEGORIES=$(jq -r '.findings.categories' "$BASELINE_REPORT")
CURRENT_CATEGORIES=$(jq -r '.findings.categories' "$CURRENT_REPORT")
echo "Baseline Categories: $BASELINE_CATEGORIES"
echo "Current Categories: $CURRENT_CATEGORIES"
for category in $(echo "$CURRENT_CATEGORIES" | jq -r '.[]'); do
if [[ ! $(echo "$BASELINE_CATEGORIES" | jq -r ".[]" | grep -w "$category") ]]; then
echo "::warning::New category detected: $category"
else
BASELINE_COUNT=$(jq -r ".findings.categories | .[\"$category\"]" "$BASELINE_REPORT")
CURRENT_COUNT=$(jq -r ".findings.categories | .[\"$category\"]" "$CURRENT_REPORT")
if [[ "$CURRENT_COUNT" -gt "$BASELINE_COUNT" ]]; then
echo "::warning::Category '$category' count increased from $BASELINE_COUNT to $CURRENT_COUNT."
elif [[ "$CURRENT_COUNT" -lt "$BASELINE_COUNT" ]]; then
echo "Category '$category' count decreased from $BASELINE_COUNT to $CURRENT_COUNT. Improvement noted."
else
echo "Category '$category' count remains the same: $CURRENT_COUNT. Persistent issue."
fi
fi
done
for baseline_category in $(echo "$BASELINE_CATEGORIES" | jq -r '.[]'); do
if [[ ! $(echo "$CURRENT_CATEGORIES" | jq -r ".[]" | grep -w "$baseline_category") ]]; then
echo "Category '$baseline_category' is no longer present. Improvement noted."
fi
done
- name: Configure Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# - name: Move files to repo root
# run: |
# mv findings.json $GITHUB_WORKSPACE/
# mv report-summary-*.json $GITHUB_WORKSPACE/
- name: Debug file paths
run: ls -al $GITHUB_WORKSPACE/
- name: Save Reports and Fail Pipeline
run: |
git add findings.json report-summary-*.json
git commit -m "Update findings and report summaries [skip ci]" || echo "No changes to commit"
git push
echo "::error::Pipeline failed due to new issues."
exit 1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}