-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathexport-risk.sh
More file actions
executable file
·271 lines (226 loc) · 7.61 KB
/
export-risk.sh
File metadata and controls
executable file
·271 lines (226 loc) · 7.61 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
#!/bin/bash
# Export Risk Information Script
# This script exports risk information from a specific deployment via API
set -euo pipefail
# Configuration
DEFAULT_API_BASE_URL="https://api.example.com"
DEFAULT_OUTPUT_FILE="risk-export-$(date +%Y%m%d-%H%M%S).json"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to display usage
usage() {
cat << EOF
Usage: $0 [OPTIONS]
Export risk information from a specific deployment via API.
OPTIONS:
-d, --deployment-id DEPLOYMENT_ID Deployment ID to export risks for (required)
-u, --api-url URL API base URL (required, default: ENV:ROX_ENDPOINT)
-t, --token TOKEN API authentication token (required,default: ENV:ROX_API_TOKEN)
-o, --output FILE Output file path (default: $DEFAULT_OUTPUT_FILE)
-v, --verbose Enable verbose output
-h, --help Display this help message
EXAMPLES:
export to timestampedfile:
$0 -d c7c15cde-cbb2-4daa-a569-d6f1c1657b54 -t your-api-token
export to specific file:
$0 -d c7c15cde-cbb2-4daa-a569-d6f1c1657b54 -t your-api-token -o risks.json
export to stdout:
$0 -d c7c15cde-cbb2-4daa-a569-d6f1c1657b54 -t your-api-token -o -
use specific api url:
$0 -d c7c15cde-cbb2-4daa-a569-d6f1c1657b54 -t your-api-token -u https://stackrox.example.com
enable verbose output:
$0 -d c7c15cde-cbb2-4daa-a569-d6f1c1657b54 -t your-api-token -v
ENVIRONMENT VARIABLES:
ROX_API_TOKEN API authentication token
ROX_ENDPOINT Host for StackRox central (central.example.com)
EOF
}
# Function to log messages
log() {
# Always log to the controlling terminal, not to redirected stdout/file
local level=$1
shift
local message="$*"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
case $level in
ERROR)
# Error and warning logs to stderr, and force output to /dev/tty if possible
if [ -t 2 ]; then
# Interactive session, log to stderr as usual
echo -e "${RED}[ERROR]${NC} $timestamp - $message" >&2
elif [ -w /dev/tty ]; then
# Non-interactive output, write to controlling terminal if available
echo -e "${RED}[ERROR]${NC} $timestamp - $message" > /dev/tty
fi
;;
WARN)
if [ -t 2 ]; then
echo -e "${YELLOW}[WARN]${NC} $timestamp - $message" >&2
elif [ -w /dev/tty ]; then
echo -e "${YELLOW}[WARN]${NC} $timestamp - $message" > /dev/tty
fi
;;
INFO)
if [[ -t 1 ]]; then
echo -e "${GREEN}[INFO]${NC} $timestamp - $message"
elif [ -w /dev/tty ]; then
echo -e "${GREEN}[INFO]${NC} $timestamp - $message" > /dev/tty
fi
;;
DEBUG)
if [[ ${VERBOSE:-false} == true ]]; then
if [ -t 1 ]; then
echo -e "${BLUE}[DEBUG]${NC} $timestamp - $message"
elif [ -w /dev/tty ]; then
echo -e "${BLUE}[DEBUG]${NC} $timestamp - $message" > /dev/tty
fi
fi
;;
esac
}
# Function to validate required tools
check_dependencies() {
local missing_tools=()
for tool in curl jq; do
if ! command -v "$tool" &> /dev/null; then
missing_tools+=("$tool")
fi
done
if [[ ${#missing_tools[@]} -gt 0 ]]; then
log ERROR "Missing required tools: ${missing_tools[*]}"
log ERROR "Please install the missing tools and try again"
exit 1
fi
}
# Function to validate API token
validate_token() {
local token=$1
local api_url=$2
log DEBUG "Validating API token..."
local response
log DEBUG "Validating API token with URL: $api_url/v1/auth/status"
response=$(curl -sL -w "%{http_code}" -H "Authorization: Bearer $token" "$api_url/v1/auth/status" -o /dev/null)
if [[ $response -ne 200 ]]; then
log ERROR "Invalid API token or authentication failed (HTTP $response)"
return 1
fi
log DEBUG "API token validated successfully"
return 0
}
# Function to get deployment info (used as the export function)
get_deployment_info() {
local deployment_id=$1
local token=$2
local api_url=$3
log DEBUG "Fetching deployment information for: $deployment_id"
local response
response=$(curl -sL -H "Authorization: Bearer $token" \
-H "Accept: application/json" \
"$api_url/v1/deploymentswithrisk/$deployment_id")
local http_code
http_code=$(curl -sL -w "%{http_code}" -H "Authorization: Bearer $token" \
"$api_url/v1/deploymentswithrisk/$deployment_id" -o /dev/null)
if [[ $http_code -ne 200 ]]; then
log ERROR "Failed to fetch deployment info (HTTP $http_code)"
return 1
fi
# Debug: Show first 200 characters of response
log DEBUG "Deployment API response (first 200 chars): ${response:0:200}"
# Basic format check (make sure not empty)
if [[ -z "$response" ]]; then
log ERROR "API returned empty response for deployment info"
return 1
fi
echo "$response"
}
# Function to save output (just raw, no formatting)
save_output() {
local data=$1
local output_file=$2
if [[ $output_file == "-" ]]; then
echo "$data" | jq -r
else
echo "$data" | jq -r > "$output_file"
log INFO "Risk data exported to: $output_file"
fi
}
# Main function
main() {
local deployment_id=""
local api_url="${ROX_ENDPOINT:-$DEFAULT_API_BASE_URL}"
local token="${ROX_API_TOKEN:-}"
local output_file="$DEFAULT_OUTPUT_FILE"
local verbose=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-d|--deployment-id)
deployment_id="$2"
shift 2
;;
-u|--api-url)
api_url="$2"
shift 2
;;
-t|--token)
token="$2"
shift 2
;;
-o|--output)
output_file="$2"
shift 2
;;
-v|--verbose)
verbose=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
log ERROR "Unknown option: $1"
usage
exit 1
;;
esac
done
# Set verbose flag for logging
VERBOSE=$verbose
# Validate required parameters
if [[ -z $deployment_id ]]; then
log ERROR "Deployment ID is required"
usage
exit 1
fi
if [[ -z $token ]]; then
log ERROR "API token is required"
usage
exit 1
fi
log DEBUG "Starting risk export process..."
log DEBUG "Deployment ID: $deployment_id"
log DEBUG "API URL: $api_url"
log DEBUG "Output file: $output_file"
# Check dependencies
check_dependencies
# Validate API token
if ! validate_token "$token" "$api_url"; then
exit 1
fi
# Get deployment info (and treat as exported risk data)
local risk_data
if ! risk_data=$(get_deployment_info "$deployment_id" "$token" "$api_url"); then
log ERROR "Deployment not found or inaccessible: $deployment_id"
exit 1
fi
# Save output (no formatting)
save_output "$risk_data" "$output_file"
log DEBUG "Risk export completed successfully"
}
# Run main function with all arguments
main "$@"