|
3 | 3 | # Downloads a ZAP XML report from a URL and saves it to the results/ directory. |
4 | 4 | # After downloading, run createScorecards.sh to generate the scorecard. |
5 | 5 | # |
6 | | -# Usage: scripts/fetchZAPResults.sh <ZAP_REPORT_URL> [OUTPUT_FILENAME] |
| 6 | +# Usage: scripts/fetchZAPResults.sh <ZAP_REPORT_URL> [OUTPUT_FILENAME] [API_KEY] |
7 | 7 | # |
8 | 8 | # Examples: |
9 | 9 | # scripts/fetchZAPResults.sh http://172.17.0.3:8090/OTHER/core/other/xmlreport/ |
10 | | -# scripts/fetchZAPResults.sh "http://zap:8090/OTHER/core/other/xmlreport/?apikey=abc123" |
11 | 10 | # scripts/fetchZAPResults.sh http://zap:8090/OTHER/core/other/xmlreport/ my-zap-results.xml |
| 11 | +# scripts/fetchZAPResults.sh http://zap:8090/OTHER/core/other/xmlreport/ "" my-secret-api-key |
12 | 12 |
|
13 | 13 | source scripts/requireCommand.sh |
14 | 14 |
|
15 | 15 | requireCommand curl |
16 | 16 |
|
17 | 17 | if [ $# -eq 0 ]; then |
18 | | - echo "Usage: $0 <ZAP_REPORT_URL> [OUTPUT_FILENAME]" |
19 | | - echo "" |
20 | | - echo "Downloads a ZAP XML report from the given URL and saves it to results/." |
21 | | - echo "After downloading, run createScorecards.sh to generate the scorecard." |
22 | | - echo "" |
23 | | - echo "Examples:" |
24 | | - echo " $0 http://172.17.0.3:8090/OTHER/core/other/xmlreport/" |
25 | | - echo " $0 \"http://zap:8090/OTHER/core/other/xmlreport/?apikey=abc123\"" |
26 | | - echo " $0 http://zap:8090/OTHER/core/other/xmlreport/ my-zap-results.xml" |
| 18 | + echo "Usage: $0 <ZAP_REPORT_URL> [OUTPUT_FILENAME] [API_KEY]" >&2 |
| 19 | + echo "" >&2 |
| 20 | + echo "Downloads a ZAP XML report from the given URL and saves it to results/." >&2 |
| 21 | + echo "After downloading, run createScorecards.sh to generate the scorecard." >&2 |
| 22 | + echo "" >&2 |
| 23 | + echo "Arguments:" >&2 |
| 24 | + echo " ZAP_REPORT_URL URL to the ZAP XML report endpoint" >&2 |
| 25 | + echo " OUTPUT_FILENAME Optional custom filename (saved under results/)" >&2 |
| 26 | + echo " API_KEY Optional ZAP API key (passed via header, not in URL)" >&2 |
| 27 | + echo "" >&2 |
| 28 | + echo "Examples:" >&2 |
| 29 | + echo " $0 http://172.17.0.3:8090/OTHER/core/other/xmlreport/" >&2 |
| 30 | + echo " $0 http://zap:8090/OTHER/core/other/xmlreport/ my-zap-results.xml" >&2 |
| 31 | + echo " $0 http://zap:8090/OTHER/core/other/xmlreport/ \"\" my-secret-api-key" >&2 |
27 | 32 | exit 1 |
28 | 33 | fi |
29 | 34 |
|
30 | 35 | zap_url="$1" |
31 | 36 |
|
32 | | -if [ $# -ge 2 ]; then |
| 37 | +if [ -n "${2:-}" ]; then |
33 | 38 | filename="$2" |
34 | 39 | else |
35 | 40 | benchmark_version=$(scripts/getBenchmarkVersion.sh) |
| 41 | + if [ -z "${benchmark_version}" ]; then |
| 42 | + echo "ERROR: Could not determine Benchmark version from pom.xml." >&2 |
| 43 | + exit 1 |
| 44 | + fi |
36 | 45 | date_stamp=$(date +%Y%m%d) |
37 | 46 | filename="Benchmark_${benchmark_version}-ZAP-${date_stamp}.xml" |
38 | 47 | fi |
39 | 48 |
|
| 49 | +api_key="${3:-}" |
| 50 | + |
| 51 | +mkdir -p results/ |
40 | 52 | output="results/${filename}" |
41 | 53 |
|
| 54 | +curl_args=(-sS -o "${output}" -w '%{http_code}' --connect-timeout 10 --max-time 120) |
| 55 | +if [ -n "${api_key}" ]; then |
| 56 | + curl_args+=(-H "X-ZAP-API-Key: ${api_key}") |
| 57 | +fi |
| 58 | + |
42 | 59 | echo "Downloading ZAP report from: ${zap_url}" |
43 | | -http_code=$(curl -sS -o "${output}" -w '%{http_code}' --connect-timeout 10 --max-time 120 "${zap_url}") |
| 60 | +http_code=$(curl "${curl_args[@]}" "${zap_url}") |
44 | 61 |
|
45 | 62 | if [ "${http_code}" -ne 200 ]; then |
46 | | - echo "ERROR: Download failed with HTTP status ${http_code}" |
| 63 | + echo "ERROR: Download failed with HTTP status ${http_code}" >&2 |
47 | 64 | rm -f "${output}" |
48 | 65 | exit 1 |
49 | 66 | fi |
50 | 67 |
|
51 | 68 | if ! head -2 "${output}" | grep -q "OWASPZAPReport"; then |
52 | | - echo "ERROR: Downloaded file does not appear to be a ZAP XML report." |
53 | | - echo "First 3 lines of downloaded content:" |
54 | | - head -3 "${output}" |
| 69 | + echo "ERROR: Downloaded file does not appear to be a ZAP XML report." >&2 |
| 70 | + echo "First 3 lines of downloaded content:" >&2 |
| 71 | + head -3 "${output}" >&2 |
55 | 72 | rm -f "${output}" |
56 | 73 | exit 1 |
57 | 74 | fi |
|
0 commit comments