-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-config.sh
More file actions
executable file
·94 lines (79 loc) · 2.23 KB
/
Copy pathgenerate-config.sh
File metadata and controls
executable file
·94 lines (79 loc) · 2.23 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
#!/usr/bin/env bash
set -euo pipefail
CONFIG_FILE="/tmp/sast-config.yaml"
check_changed_files() {
if [[ ${INPUT_MODE} == "diff" && -z "${CHANGED_FILES}" ]]; then
echo "::notice::No files changed. Skipping scan."
echo "skip=true" >> "${GITHUB_OUTPUT}"
return 1
fi
}
prepare_config() {
if [[ -n "${USER_CONFIG_FILE:-}" ]]; then
python3 -c "
import yaml, os
with open(os.environ['USER_CONFIG_FILE']) as f:
cfg = yaml.safe_load(f) or {}
cfg['namespace'] = os.environ['GITHUB_REPOSITORY']
if os.environ['INPUT_MODE'] == 'diff':
cfg['sast'] = cfg.get('sast') or {}
cfg['sast']['include'] = os.environ['CHANGED_FILES'].split()
with open('${CONFIG_FILE}', 'w') as f:
yaml.dump(cfg, f, default_flow_style=False, sort_keys=False)
"
else
python3 -c "
import yaml, os
cfg = {}
cfg['namespace'] = os.environ['GITHUB_REPOSITORY']
cfg['sast'] = {
'include': os.environ['CHANGED_FILES'].split() if os.environ['INPUT_MODE'] == 'diff'
else ['.'],
}
cfg['output'] = {
'format': 'SARIF',
'file_path': '.fluidattacks-sast-results.sarif',
}
with open('${CONFIG_FILE}', 'w') as f:
yaml.dump(cfg, f, default_flow_style=False, sort_keys=False)
"
fi
}
run_scan() {
echo "::group::Generated configuration"
cat "${CONFIG_FILE}"
echo "::endgroup::"
local exit_code=0
docker run --rm \
-v "${GITHUB_WORKSPACE}:/src" \
-v "${CONFIG_FILE}:${CONFIG_FILE}:ro" \
"ghcr.io/fluidattacks/sast:latest" \
sast scan "${CONFIG_FILE}" || exit_code=$?
if [[ ${exit_code} -eq 0 ]]; then
echo "vulnerabilities_found=false" >> "${GITHUB_OUTPUT}"
elif [[ ${exit_code} -eq 1 ]]; then
echo "vulnerabilities_found=true" >> "${GITHUB_OUTPUT}"
else
echo "::error::Scanner exited with code ${exit_code}"
exit "${exit_code}"
fi
python3 -c "
import yaml, re
with open('${CONFIG_FILE}') as f:
cfg = yaml.safe_load(f)
fmt = cfg.get('output', {}).get('format', '')
if fmt in ('SARIF', 'ALL'):
path = cfg['output']['file_path']
sanitized = re.sub(r'[\r\n]', '', str(path))
print('sarif_file=' + sanitized)
" >> "${GITHUB_OUTPUT}" 2> /dev/null || true
}
main() {
if ! check_changed_files; then
exit 0
fi
prepare_config
echo "skip=false" >> "${GITHUB_OUTPUT}"
run_scan
}
main