-
Notifications
You must be signed in to change notification settings - Fork 9
147 lines (128 loc) · 4.72 KB
/
Copy pathash-security-scan.yml
File metadata and controls
147 lines (128 loc) · 4.72 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
name: ASH Security Scan
on:
pull_request:
branches: [main]
permissions:
contents: read
pull-requests: write
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v46
with:
files: |
**/*.py
**/*.js
**/*.ts
**/*.yaml
**/*.yml
**/*.json
**/*.sh
**/Dockerfile*
**/requirements*.txt
**/package*.json
**/*.tf
**/*.tfvars
- name: Set up Python
if: steps.changed-files.outputs.any_changed == 'true'
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install ASH
if: steps.changed-files.outputs.any_changed == 'true'
run: pip install git+https://github.com/awslabs/automated-security-helper.git@v3.5.3
- name: Copy changed files and config to scan directory
if: steps.changed-files.outputs.any_changed == 'true'
run: |
mkdir -p /tmp/ash-scan/.ash
cp .ash/.ash.yaml /tmp/ash-scan/.ash/.ash.yaml
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
if [ -f "$file" ]; then
mkdir -p "/tmp/ash-scan/$(dirname "$file")"
cp "$file" "/tmp/ash-scan/$file"
fi
done
- name: Run ASH scan
if: steps.changed-files.outputs.any_changed == 'true'
id: ash-scan
run: |
cd /tmp/ash-scan
ash --mode precommit --config .ash/.ash.yaml 2>&1 | tee /tmp/ash-output.log
continue-on-error: true
- name: Build PR comment
if: steps.changed-files.outputs.any_changed == 'true'
id: build-comment
run: |
COMMENT_FILE="/tmp/pr_comment.md"
SHA="${{ github.event.pull_request.head.sha }}"
SHORT_SHA="${SHA:0:7}"
TIMESTAMP=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
echo "**ASH Security Scan** | Commit: \`${SHORT_SHA}\` | ${TIMESTAMP}" > "$COMMENT_FILE"
echo "" >> "$COMMENT_FILE"
if [ -f "/tmp/ash-scan/.ash/ash_output/reports/ash.summary.md" ]; then
# Use the markdown report ASH generates
cat "/tmp/ash-scan/.ash/ash_output/reports/ash.summary.md" >> "$COMMENT_FILE"
elif [ -f "/tmp/ash-output.log" ]; then
echo "### Scan Output" >> "$COMMENT_FILE"
echo "" >> "$COMMENT_FILE"
echo '```' >> "$COMMENT_FILE"
tail -50 /tmp/ash-output.log >> "$COMMENT_FILE"
echo '```' >> "$COMMENT_FILE"
else
echo "No scan results available." >> "$COMMENT_FILE"
fi
echo "" >> "$COMMENT_FILE"
echo "<!-- ASH-SECURITY-SCAN -->" >> "$COMMENT_FILE"
# Check for findings
if grep -q "Actionable findings detected!" /tmp/ash-output.log 2>/dev/null; then
echo "has_findings=true" >> $GITHUB_OUTPUT
else
echo "has_findings=false" >> $GITHUB_OUTPUT
fi
- name: Post or update PR comment
if: steps.changed-files.outputs.any_changed == 'true'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const body = fs.readFileSync('/tmp/pr_comment.md', 'utf8');
const prNumber = context.payload.pull_request.number;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(c =>
c.body.includes('<!-- ASH-SECURITY-SCAN -->')
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: body,
});
}
- name: Upload artifacts
if: steps.changed-files.outputs.any_changed == 'true' && always()
uses: actions/upload-artifact@v4
with:
name: ash-security-results
path: /tmp/ash-scan/.ash/
retention-days: 30
- name: Skip message
if: steps.changed-files.outputs.any_changed == 'false'
run: echo "No relevant files changed - skipping security scan"