Repository navigation
Expand file tree
/
Copy pathcloud-shell-scripts.ts
More file actions
425 lines (381 loc) · 14.9 KB
/
Copy pathcloud-shell-scripts.ts
File metadata and controls
425 lines (381 loc) · 14.9 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Cloud shell / web terminal helper scripts served by the get-stackql.io worker.
//
// These were previously packaged into the linux release zip by the
// cloud-shell-scripts repo's build_package.sh. That repo is being retired; the
// scripts now live here and are delivered on demand via /install/<provider>.
// They assume a ./stackql binary in the current directory (the provider
// installer drops one alongside them).
export const AWS_CLOUD_SHELL = `#!/bin/sh
show_usage() {
echo "Script to run StackQL in AWS Cloud Shell"
echo
echo "Usage:"
echo " ./stackql-aws-cloud-shell.sh [--role-arn ARN] [shell | exec] [flags]"
echo
echo " --role-arn (optional)"
echo " If supplied, the program will assume the role specified;"
echo " if not, then the current user context in cloud shell will be used."
echo
echo " Command (optional):"
echo " 'shell' (default) enters the StackQL command shell to execute queries interactively."
echo " 'exec' is used to execute StackQL queries or files to provide batch outputs"
echo " (such as CSV or JSON output files). If not specified, 'shell' is assumed."
echo
echo " Flags:"
echo " StackQL args are optional global flags, documented at https://stackql.io/docs/command-line-usage/global-flags"
echo
echo " Examples:"
echo " # Launch the StackQL shell using the current user context"
echo " sh stackql-aws-cloud-shell.sh"
echo
echo " # Assume a role and execute a query from a file, writing the output to a CSV file"
echo " sh stackql-aws-cloud-shell.sh --role-arn arn:aws:iam::824532806693:role/SecurityReviewerRole exec --infile /path/to/query.sql --output csv --outfile /path/to/output.csv"
echo
}
pull_aws_docs() {
echo "Pulling latest AWS provider (aws)..."
./stackql exec "REGISTRY PULL aws"
echo "Pulling latest AWS Cloud Control provider (awscc)..."
./stackql exec "REGISTRY PULL awscc"
}
fetch_and_export_aws_creds() {
if [ -z "$AWS_CONTAINER_AUTHORIZATION_TOKEN" ] || [ -z "$AWS_CONTAINER_CREDENTIALS_FULL_URI" ]; then
echo "Error: AWS_CONTAINER_AUTHORIZATION_TOKEN or AWS_CONTAINER_CREDENTIALS_FULL_URI environment variable not set. If you are not running in AWS Cloud Shell, please provide a role ARN."
exit 1
fi
creds=$(curl -s -H "Authorization: $AWS_CONTAINER_AUTHORIZATION_TOKEN" "$AWS_CONTAINER_CREDENTIALS_FULL_URI")
if [ -z "$creds" ]; then
echo "Error: Failed to fetch AWS credentials."
exit 1
fi
if ! jq -e . >/dev/null 2>&1 <<< "$creds"; then
echo "Failed to retrieve AWS credentials, try refreshing your browser."
exit 1
else
user_identity=$(aws sts get-caller-identity)
user_name=$(echo "$user_identity" | jq -r '.Arn' | awk -F'/' '{print $NF}')
echo "Launching StackQL as: $user_name..."
export AWS_ACCESS_KEY_ID=$(echo "$creds" | jq -r '.AccessKeyId')
export AWS_SECRET_ACCESS_KEY=$(echo "$creds" | jq -r '.SecretAccessKey')
export AWS_SESSION_TOKEN=$(echo "$creds" | jq -r '.Token')
fi
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ] || [ -z "$AWS_SESSION_TOKEN" ]; then
echo "Error: Failed to set AWS credentials."
exit 1
fi
}
assume_role_and_export_creds() {
if [ -z "$ROLE_ARN" ]; then
echo "Error: ROLE_ARN not provided."
exit 1
fi
ASSUME_ROLE_OUTPUT=$(aws sts assume-role --role-arn "$ROLE_ARN" --role-session-name StackQLSession)
if [ $? -ne 0 ] || [ -z "$ASSUME_ROLE_OUTPUT" ]; then
echo "Error: Failed to assume role."
exit 1
fi
export AWS_ACCESS_KEY_ID=$(echo "$ASSUME_ROLE_OUTPUT" | jq -r '.Credentials.AccessKeyId')
export AWS_SECRET_ACCESS_KEY=$(echo "$ASSUME_ROLE_OUTPUT" | jq -r '.Credentials.SecretAccessKey')
export AWS_SESSION_TOKEN=$(echo "$ASSUME_ROLE_OUTPUT" | jq -r '.Credentials.SessionToken')
aws sts get-caller-identity # to show what you are about to run as
if [ $? -ne 0 ]; then
echo "Error: Failed to verify assumed role."
exit 1
fi
}
ROLE_ARN=""
CMD="shell" # Default to 'shell' command
FLAGS=""
# Parse command line arguments
while [ $# -gt 0 ]; do
case "$1" in
--role-arn)
shift # Move to the value of --role-arn
ROLE_ARN="$1" # Capture the role ARN
shift # Move past the value
;;
shell|exec|ext)
CMD="$1"
shift # Move past the command
;;
*)
# Check if the argument contains spaces
if echo "$1" | grep -q " "; then
# Argument contains spaces, wrap it in quotes
FLAGS="$FLAGS \\"$1\\""
else
# Argument does not contain spaces, add as is
FLAGS="$FLAGS $1"
fi
shift # Move past each flag
;;
esac
done
# Assume role and export credentials if ROLE_ARN is provided, else fetch from Cloud Shell
if [ -n "$ROLE_ARN" ]; then
assume_role_and_export_creds
else
fetch_and_export_aws_creds
fi
# Execute the StackQL command
if [ "$CMD" = "shell" ]; then
pull_aws_docs
echo "Entering StackQL shell..."
eval "./stackql shell $FLAGS"
elif [ "$CMD" = "exec" ]; then
pull_aws_docs
echo "Executing StackQL query..."
eval "./stackql exec $FLAGS"
elif [ "$CMD" = "ext" ]; then
pull_aws_docs
echo "Creds exported for use with external tools (like pystackql)..."
echo $AWS_ACCESS_KEY_ID
else
show_usage
echo
echo "Error: invalid command ($CMD)"
exit 1
fi
`;
export const GOOGLE_CLOUD_SHELL = `#!/bin/sh
show_usage() {
echo "Script to run StackQL in Google Cloud Shell"
echo
echo "Usage:"
echo " ./stackql-google-cloud-shell.sh [shell | exec] [flags]"
echo
echo " Command (optional):"
echo " 'shell' (default) enters the StackQL command shell to execute queries interactively."
echo " 'exec' is used to execute StackQL queries or files to provide batch outputs"
echo " (such as CSV or JSON output files). If not specified, 'shell' is assumed."
echo
echo " Flags:"
echo " StackQL args are optional global flags, documented at https://stackql.io/docs/command-line-usage/global-flags"
echo
echo " Examples:"
echo " # Launch the StackQL shell using interactive authentication"
echo " sh stackql-google-cloud-shell.sh"
echo
echo " # Execute a query from a file, writing the output to a CSV file with interactive authentication"
echo " sh stackql-google-cloud-shell.sh exec --infile /path/to/query.sql --output csv --outfile /path/to/output.csv"
echo
}
pull_google_docs() {
echo "Pulling latest Google Cloud provider..."
./stackql exec "REGISTRY PULL google"
}
CMD="shell" # Default to 'shell' command
FLAGS=""
# Parse command line arguments
while [ $# -gt 0 ]; do
case "$1" in
shell|exec)
CMD="$1"
shift # Move past the command
;;
*)
# Check if the argument contains spaces
if echo "$1" | grep -q " "; then
# Argument contains spaces, wrap it in quotes
FLAGS="$FLAGS \\"$1\\""
else
# Argument does not contain spaces, add as is
FLAGS="$FLAGS $1"
fi
shift # Move past each flag
;;
esac
done
# Set authentication for Google Cloud
AUTH='{ "google": { "type": "interactive" }}'
# Execute the StackQL command
if [ "$CMD" = "shell" ]; then
pull_google_docs
echo "Entering StackQL shell..."
eval "./stackql shell --auth='\${AUTH}' $FLAGS"
elif [ "$CMD" = "exec" ]; then
pull_google_docs
echo "Executing StackQL query..."
eval "./stackql exec --auth='\${AUTH}' $FLAGS"
else
show_usage
echo
echo "Error: invalid command ($CMD)"
exit 1
fi
`;
export const AZURE_CLOUD_SHELL = `#!/bin/sh
show_usage() {
echo "Script to run StackQL in Azure Cloud Shell"
echo
echo "Usage:"
echo " ./stackql-azure-cloud-shell.sh [shell | exec] [flags]"
echo
echo " Command (optional):"
echo " 'shell' (default) enters the StackQL command shell to execute queries interactively."
echo " 'exec' is used to execute StackQL queries or files to provide batch outputs"
echo " (such as CSV or JSON output files). If not specified, 'shell' is assumed."
echo
echo " Flags:"
echo " StackQL args are optional global flags, documented at https://stackql.io/docs/command-line-usage/global-flags"
echo
echo " Examples:"
echo " # Launch the StackQL shell using interactive authentication (default in Azure)"
echo " sh stackql-azure-cloud-shell.sh"
echo
echo " # Execute a query from a file, writing the output to a CSV file with interactive authentication"
echo " sh stackql-azure-cloud-shell.sh exec --infile /path/to/query.sql --output csv --outfile /path/to/output.csv"
echo
}
pull_azure_docs() {
echo "Pulling latest Azure provider (azure)..."
./stackql exec "REGISTRY PULL azure"
# echo "Pulling latest Azure Extras provider (azure_extras)..."
# ./stackql exec "REGISTRY PULL azure_extras"
# echo "Pulling latest Azure ISV provider (azure_isv)..."
# ./stackql exec "REGISTRY PULL azure_isv"
# echo "Pulling latest Azure Stack provider (azure_stack)..."
# ./stackql exec "REGISTRY PULL azure_stack"
}
CMD="shell" # Default to 'shell' command
FLAGS=""
# Parse command line arguments
while [ $# -gt 0 ]; do
case "$1" in
shell|exec)
CMD="$1"
shift # Move past the command
;;
*)
# Check if the argument contains spaces
if echo "$1" | grep -q " "; then
# Argument contains spaces, wrap it in quotes
FLAGS="$FLAGS \\"$1\\""
else
# Argument does not contain spaces, add as is
FLAGS="$FLAGS $1"
fi
shift # Move past each flag
;;
esac
done
# Execute the StackQL command
if [ "$CMD" = "shell" ]; then
pull_azure_docs
echo "Entering StackQL shell..."
eval "./stackql shell $FLAGS"
elif [ "$CMD" = "exec" ]; then
pull_azure_docs
echo "Executing StackQL query..."
eval "./stackql exec $FLAGS"
else
show_usage
echo
echo "Error: invalid command ($CMD)"
exit 1
fi
`;
export const DATABRICKS_SHELL = `#!/bin/sh
show_usage() {
echo "Script to run StackQL in the Databricks web terminal"
echo
echo "Usage:"
echo " ./stackql-databricks-shell.sh [shell | exec] [flags]"
echo
echo " Command (optional):"
echo " 'shell' (default) enters the StackQL command shell to execute queries interactively."
echo " 'exec' is used to execute StackQL queries or files to provide batch outputs"
echo " (such as CSV or JSON output files). If not specified, 'shell' is assumed."
echo
echo " Flags:"
echo " StackQL args are optional global flags, documented at https://stackql.io/docs/command-line-usage/global-flags"
echo
echo " Examples:"
echo " # Launch the StackQL shell using the Databricks token from the environment"
echo " sh stackql-databricks-shell.sh"
echo
echo " # Execute a query from a file, writing the output to a CSV file"
echo " sh stackql-databricks-shell.sh exec --infile /path/to/query.sql --output csv --outfile /path/to/output.csv"
echo
}
check_databricks_token() {
if [ -z "$DATABRICKS_TOKEN" ]; then
echo "Error: DATABRICKS_TOKEN environment variable is not set. This script is intended to run in the Databricks web terminal."
exit 1
fi
}
ensure_latest_databricks_provider() {
echo "Checking installed providers..."
# get the latest version available in the registry (last entry in the comma-separated list)
registry_output=$(./stackql exec --output json "registry list databricks_workspace" 2>/dev/null)
if [ -z "$registry_output" ]; then
echo "Error: failed to query the StackQL registry."
exit 1
fi
latest_version=$(echo "$registry_output" | grep -o '"versions":"[^"]*"' | sed 's/"versions":"//;s/"//' | tr ',' '\\n' | tr -d ' ' | tail -1)
if [ -z "$latest_version" ]; then
echo "Error: could not determine the latest version of databricks_workspace from the registry."
exit 1
fi
# get the locally installed version for databricks_workspace (if any)
installed_output=$(./stackql exec --output json "show providers" 2>/dev/null)
installed_version=$(echo "$installed_output" | grep -o '"name":"databricks_workspace","version":"[^"]*"' | grep -o '"version":"[^"]*"' | sed 's/"version":"//;s/"//')
if [ -z "$installed_version" ]; then
echo "databricks_workspace provider not found locally, pulling $latest_version..."
./stackql exec "REGISTRY PULL databricks_workspace $latest_version" > /dev/null 2>&1
echo "databricks_workspace $latest_version installed."
elif [ "$installed_version" != "$latest_version" ]; then
echo "Updating databricks_workspace provider from $installed_version to $latest_version..."
./stackql exec "REGISTRY PULL databricks_workspace $latest_version" > /dev/null 2>&1
echo "databricks_workspace updated to $latest_version."
else
echo "databricks_workspace $installed_version is up to date."
fi
}
CMD="shell"
FLAGS=""
while [ $# -gt 0 ]; do
case "$1" in
shell|exec)
CMD="$1"
shift
;;
*)
if echo "$1" | grep -q " "; then
FLAGS="$FLAGS \\"$1\\""
else
FLAGS="$FLAGS $1"
fi
shift
;;
esac
done
check_databricks_token
AUTH='{ "databricks_workspace": { "type": "bearer", "credentialsenvvar": "DATABRICKS_TOKEN" }}'
if [ "$CMD" = "shell" ]; then
ensure_latest_databricks_provider
echo "Entering StackQL shell..."
eval "./stackql shell --auth='\${AUTH}' $FLAGS"
elif [ "$CMD" = "exec" ]; then
ensure_latest_databricks_provider
echo "Executing StackQL query..."
eval "./stackql exec --auth='\${AUTH}' $FLAGS"
else
show_usage
echo
echo "Error: invalid command ($CMD)"
exit 1
fi
`;
export interface CloudShellScript {
file: string;
body: string;
}
// Supported cloud shell / web terminal providers. Keys are matched against the
// <provider> path segment (lower-cased) in /install/<provider>.
export const CLOUD_SHELL_SCRIPTS: Record<string, CloudShellScript> = {
aws: { file: "stackql-aws-cloud-shell.sh", body: AWS_CLOUD_SHELL },
google: { file: "stackql-google-cloud-shell.sh", body: GOOGLE_CLOUD_SHELL },
azure: { file: "stackql-azure-cloud-shell.sh", body: AZURE_CLOUD_SHELL },
databricks: { file: "stackql-databricks-shell.sh", body: DATABRICKS_SHELL },
};