Skip to content

Commit 93559e2

Browse files
authored
ROX-24175: vuln-mgmt export API examples (#100)
* ROX-24175: vuln-mgmt export API examples * address feedback
1 parent eb027a0 commit 93559e2

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Export workload vulnerabilities via shell script
2+
3+
The `/v1/export/vuln-mgmt/workloads` API exports workload vulnerabilities in the form
4+
of deployments and their associated images including image vulnerabilities.
5+
6+
The following sections provide use case examples utilizing either the shell or Python.
7+
8+
## Shell script
9+
10+
See `export-workloads.sh` for a shell script example based on `curl` and `jq`.
11+
12+
### Export all workloads
13+
```shell
14+
export-workloads.sh
15+
```
16+
17+
### Export all workloads from the cluster `prod`
18+
```shell
19+
export-workloads.sh "Cluster%3Aprod"
20+
```
21+
22+
### Export all workloads matching the query `Deployment:app Namespace:default`
23+
```shell
24+
export-workloads.sh "Deployment%3Aapp%2BNamespace%3Adefault"
25+
```
26+
27+
### Export all workloads with a timeout of 60 seconds
28+
```shell
29+
export-workloads.sh "" 60
30+
```
31+
32+
## Python script
33+
34+
See `export-workloads.py` for a python script example.
35+
36+
### Export all workloads
37+
```shell
38+
export-workloads.py
39+
```
40+
41+
### Export all workloads from the cluster `prod`
42+
```shell
43+
export-workloads.py --query="Cluster%3Aprod"
44+
```
45+
46+
### Export all workloads matching the query `Deployment:app Namespace:default`
47+
```shell
48+
export-workloads.py --query="Deployment%3Aapp%2BNamespace%3Adefault"
49+
```
50+
51+
### Export all workloads with a timeout of 60 seconds
52+
```shell
53+
export-workloads.py --timeout=60
54+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
# This script pulls workload vulnerabilities in the form of deployments and their
4+
# associated images including image vulnerabilities.
5+
#
6+
# The output is streamed to STDOUT as a series of Python objects with the schema
7+
#
8+
# {"result": {"deployment": {...}, "images": [...]}}
9+
# ...
10+
# {"result": {"deployment": {...}, "images": [...]}}
11+
#
12+
# Further processing may be done on the parsed objects.
13+
#
14+
# Requires ROX_ENDPOINT and ROX_API_TOKEN environment variables.
15+
# The API token requires read access on images and deployments.
16+
17+
import argparse
18+
import json
19+
import os
20+
import requests
21+
22+
parser = argparse.ArgumentParser("export-workloads")
23+
parser.add_argument("--query", help="query to filter the deployments (default \"\")", default="")
24+
parser.add_argument("--timeout", help="timeout in seconds (default 0 = no timeout)", default=0, type=int)
25+
args = parser.parse_args()
26+
27+
endpoint = os.environ["ROX_ENDPOINT"].removeprefix("https://")
28+
url = f"https://{endpoint}/v1/export/vuln-mgmt/workloads"
29+
parameters = f"query={args.query}&timeout={args.timeout}"
30+
headers = {"Authorization": f"Bearer {os.environ['ROX_API_TOKEN']}"}
31+
32+
session = requests.Session()
33+
with session.get(f"{url}?{parameters}", headers=headers, stream=True) as resp:
34+
resp.raise_for_status()
35+
for line in resp.iter_lines():
36+
if line:
37+
# Parse JSON object for further processing. Here we simply print out the content.
38+
obj = json.loads(line)
39+
print(f"{obj}\n")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
3+
# This script pulls workload vulnerabilities in the form of deployments and their
4+
# associated images including image vulnerabilities.
5+
#
6+
# The output is streamed to STDOUT as valid JSON with the schema
7+
#
8+
# [
9+
# {"result": {"deployment": {...}, "images": [...]}},
10+
# ...
11+
# {"result": {"deployment": {...}, "images": [...]}}
12+
# ]
13+
#
14+
# Requires ROX_ENDPOINT and ROX_API_TOKEN environment variables.
15+
# The API token requires read access on images and deployments.
16+
17+
set -euo pipefail
18+
19+
case $1 in
20+
*help)
21+
echo "$0 [query] [timeout]"
22+
;;
23+
esac
24+
25+
if [[ -z "${ROX_ENDPOINT}" ]]; then
26+
echo >&2 "ROX_ENDPOINT must be set"
27+
exit 1
28+
fi
29+
30+
if [[ -z "${ROX_API_TOKEN}" ]]; then
31+
echo >&2 "ROX_API_TOKEN must be set"
32+
exit 1
33+
fi
34+
35+
endpoint=https://${ROX_ENDPOINT#https://}
36+
query=$1
37+
timeout=${2:-0}
38+
39+
curl -sk -H "Authorization: Bearer ${ROX_API_TOKEN}" \
40+
"${endpoint}/v1/export/vuln-mgmt/workloads?query=$query&timeout=$timeout" |
41+
# Use `jq -nc --slurp` instead for higher throughput but more memory usage.
42+
jq -nc --stream "[fromstream(inputs)]"

0 commit comments

Comments
 (0)