-
Notifications
You must be signed in to change notification settings - Fork 0
287 lines (252 loc) ยท 8.89 KB
/
Copy pathsecurity-iac.yml
File metadata and controls
287 lines (252 loc) ยท 8.89 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Infrastructure as Code (IaC) Security Scanning with Checkov
# Scans infrastructure configurations for security misconfigurations
name: IaC Security Scan
on:
push:
branches: [ main, dev, 'kcs/*' ]
paths:
- 'infrastructure/**'
- 'backend/api/src/main/python/Dockerfile'
- '.github/workflows/**'
- '**/*.tf'
- '**/*.yaml'
- '**/*.yml'
- '**/*.json'
pull_request:
branches: [ main, dev ]
paths:
- 'infrastructure/**'
- 'backend/api/src/main/python/Dockerfile'
- '.github/workflows/**'
- '**/*.tf'
- '**/*.yaml'
- '**/*.yml'
- '**/*.json'
schedule:
# Run daily at 5 AM UTC
- cron: '0 5 * * *'
jobs:
checkov-scan:
name: Checkov IaC Security Scan
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install Checkov
run: |
pip install checkov
checkov --version
- name: Create Checkov configuration
run: |
mkdir -p .checkov
# Create Checkov configuration file
cat > .checkov/config.yaml << 'EOF'
# Checkov configuration for CMZ project
framework:
- dockerfile
- github_actions
- yaml
- json
- terraform
- cloudformation
# Skip specific checks that are not applicable
skip-check:
# Docker checks that may not be relevant for our use case
- CKV_DOCKER_7 # Ensure 'COPY' is used instead of 'ADD' (we use ADD intentionally)
# Enable severity filtering
compact: true
quiet: false
# Output formats
output:
- cli
- sarif
# Set baseline for progressive improvement
baseline: .checkov/baseline.json
EOF
- name: Run Checkov scan on Dockerfile
id: checkov-docker
continue-on-error: true
run: |
if [ -f "backend/api/src/main/python/Dockerfile" ]; then
echo "๐ณ Scanning Dockerfile..."
checkov -f backend/api/src/main/python/Dockerfile \
--framework dockerfile \
--output cli --output sarif \
--sarif-file-name checkov-docker.sarif \
--compact || true
else
echo "โน๏ธ No Dockerfile found to scan"
fi
- name: Run Checkov scan on GitHub Actions workflows
id: checkov-gha
continue-on-error: true
run: |
echo "โ๏ธ Scanning GitHub Actions workflows..."
checkov -d .github/workflows/ \
--framework github_actions \
--output cli --output sarif \
--sarif-file-name checkov-gha.sarif \
--compact || true
- name: Run Checkov scan on YAML/JSON files
id: checkov-config
continue-on-error: true
run: |
echo "๐ Scanning configuration files..."
# Scan YAML files
find . -name "*.yml" -o -name "*.yaml" | grep -v node_modules | grep -v .git | while read -r file; do
echo "Scanning: $file"
checkov -f "$file" \
--framework yaml \
--output cli \
--compact || true
done
# Scan JSON files (excluding package files and generated files)
find . -name "*.json" | grep -v node_modules | grep -v package | grep -v .git | while read -r file; do
echo "Scanning: $file"
checkov -f "$file" \
--framework json \
--output cli \
--compact || true
done
- name: Run Checkov scan on Terraform (if exists)
id: checkov-terraform
continue-on-error: true
run: |
if find . -name "*.tf" | grep -q .; then
echo "๐๏ธ Scanning Terraform files..."
checkov -d . \
--framework terraform \
--output cli --output sarif \
--sarif-file-name checkov-terraform.sarif \
--compact || true
else
echo "โน๏ธ No Terraform files found to scan"
fi
- name: Run Checkov scan on CloudFormation (if exists)
id: checkov-cfn
continue-on-error: true
run: |
if find . -name "*.template" -o -name "*cloudformation*" | grep -q .; then
echo "โ๏ธ Scanning CloudFormation templates..."
checkov -d . \
--framework cloudformation \
--output cli --output sarif \
--sarif-file-name checkov-cfn.sarif \
--compact || true
else
echo "โน๏ธ No CloudFormation templates found to scan"
fi
- name: Upload Checkov SARIF results
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: checkov-docker.sarif
continue-on-error: true
- name: Upload GitHub Actions SARIF results
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: checkov-gha.sarif
continue-on-error: true
- name: Upload Terraform SARIF results
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: checkov-terraform.sarif
continue-on-error: true
- name: Create IaC security summary
if: always()
run: |
{
echo "## ๐๏ธ Infrastructure as Code Security Scan"
echo ""
echo "**Scan Date**: $(date -u)"
echo "**Frameworks Scanned**:"
echo "- ๐ณ Docker configurations"
echo "- โ๏ธ GitHub Actions workflows"
echo "- ๐ YAML/JSON configuration files"
} > iac-summary.md
if find . -name "*.tf" | grep -q .; then
echo "- ๐๏ธ Terraform infrastructure" >> iac-summary.md
fi
if find . -name "*.template" -o -name "*cloudformation*" | grep -q .; then
echo "- โ๏ธ CloudFormation templates" >> iac-summary.md
fi
{
echo ""
echo "๐ **Key Security Areas Checked:**"
echo "- Container security configurations"
echo "- Secrets management in workflows"
echo "- Network security settings"
echo "- Access control configurations"
echo "- Resource encryption settings"
echo ""
echo "๐ **Next Steps:**"
echo "1. Review findings in the Security tab"
echo "2. Address high/critical severity issues first"
echo "3. Consider implementing policy-as-code for ongoing compliance"
} >> iac-summary.md
- name: Comment PR with IaC scan results
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
if (fs.existsSync('iac-summary.md')) {
const summary = fs.readFileSync('iac-summary.md', 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: summary
});
}
- name: Upload IaC scan artifacts
uses: actions/upload-artifact@v4
if: always()
with:
name: iac-security-results-${{ github.sha }}
path: |
checkov-*.sarif
iac-summary.md
.checkov/
retention-days: 30
hadolint:
name: Dockerfile Linting with Hadolint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Hadolint Dockerfile linter
uses: hadolint/hadolint-action@v3.1.0
if: hashFiles('backend/api/src/main/python/Dockerfile') != ''
with:
dockerfile: backend/api/src/main/python/Dockerfile
format: sarif
output-file: hadolint.sarif
- name: Upload Hadolint SARIF results
uses: github/codeql-action/upload-sarif@v3
if: always() && hashFiles('hadolint.sarif') != ''
with:
sarif_file: hadolint.sarif
actionlint:
name: GitHub Actions Workflow Linting
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run actionlint
uses: raven-actions/actionlint@v2
with:
files: .github/workflows/*.yml
- name: Upload actionlint results
uses: actions/upload-artifact@v4
if: always()
with:
name: actionlint-results
path: actionlint-results.json
retention-days: 15