-
Notifications
You must be signed in to change notification settings - Fork 15
/
manage.sh
executable file
·368 lines (332 loc) · 9.92 KB
/
manage.sh
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
#!/bin/bash
set -e
PROGNAME=$(basename "$0")
REQUIRED_TOOLS="jq curl"
################################################################################
# Functions
die() {
echo "$PROGNAME: $*" >&2
exit 1
}
usage() {
if [ "$*" != "" ]; then
echo "Error: $*"
fi
cat << EOF
Usage: $PROGNAME [OPTION ...] [command]
Service Provider Utility
Commands:
new-provider Register a new provider.
auth-provider Authenticate as a provider.
get-provider Get the provider profile.
update-provider Update the provider profile.
delete-provider Delete the provider.
list-services List services.
new-service Add a new service.
get-service Get a service.
update-service Update a service profile.
update-service-conn Update a service connection.
delete-service Delete a service.
new-cert Generate a new self-signed certificate.
Options:
-h, --help Display this help text and exit.
-e, --env Environment to use [edge].
EOF
exit 1
}
check_tools() {
for TOOL in $REQUIRED_TOOLS; do
if ! command -v "$TOOL" > /dev/null; then
echo "Could not find $TOOL! Exiting."
exit 1
fi
done
}
check_auth() {
if [ ! -f "./.cookie" ]; then
echo "Not authenticated. Use the 'auth-provider' command."
exit 1
fi
}
authenticate() {
read -p "Local identifier [default: $zident]: " auth_ident
if [ -z "$auth_ident" ]; then
auth_ident="$zident"
fi
if [ ! -d "$auth_ident" ]; then
echo "Invalid identifier. Directory not found: $auth_ident"
exit 1
fi
auth_email=$(< "$auth_ident/.email")
auth_password=$(< "$auth_ident/.password")
curl -s -XPOST "$zapi/provider/login" \
-H 'Content-Type: application/json' \
-d '{"email":"'"$auth_email"'"
,"password":"'"$auth_password"'"}' \
-c ./.cookie
echo "$auth_ident" > .current
echo "Authenticated as $auth_email"
}
new_provider() {
read -p "Local identifier [default: $zident]: " provider_ident
if [ -z "$provider_ident" ]; then
provider_ident="$zident"
fi
if [ -d "$provider_ident" ]; then
echo "Directory exists: $provider_ident"
exit 1
fi
read -p "Provider name: " provider_name
read -p "Provider email: " provider_email
read -p "Provider homepage: " provider_url
read -p "Provider description: " provider_descr
echo "Creating directory $provider_ident ..."
mkdir ${provider_ident}
echo "Registering $provider_name ..."
resp=$(curl -s -X POST "$zapi/provider/register" \
-H 'Content-Type: application/json' \
-d '{"name": "'"$provider_name"'",
"email": "'"$provider_email"'",
"url": "'"$provider_url"'",
"description": "'"$provider_descr"'"
}')
echo "$resp"
echo "$resp" | jq -r '.password' > ${provider_ident}/.password
echo "$provider_email" > ${provider_ident}/.email
echo "Done. Please check your e-mail."
}
get_provider() {
check_auth
curl -s -XGET "$zapi/provider" -b ./.cookie | jq .
}
update_provider() {
check_auth
read -p "New provider name [default: no change]: " new_name
read -p "New provider URL [default: no change]: " new_url
read -p "New provider description [default: no change]: " new_descr
if [ -z "$new_name" ]; then
new_name="null"
else
new_name="\"$new_name\""
fi
if [ -z "$new_url" ]; then
new_url="null"
else
new_url="\"$new_url\""
fi
if [ -z "$new_descr" ]; then
new_descr="null"
else
new_descr="\"$new_descr\""
fi
echo "Updating provider profile ..."
curl -s -XPUT "$zapi/provider" \
-H 'Content-Type: application/json' \
-d '{"name": '"$new_name"',
"url": '"$new_url"',
"description": '"$new_descr"'
}' \
-b ./.cookie
echo "Done"
}
delete_provider() {
check_auth
auth_ident=$(read_ident)
auth_password=$(read_password)
read -p "Are you sure (yN)? " yn
if [ "$yn" == "y" ] ; then
echo "Deleting provider ..."
curl -s -XDELETE "$zapi/provider" \
-H 'Content-Type: application/json' \
-d '{"password": "'"$auth_password"'"}' \
-b ./.cookie
rm -f ./.current
rm -f ./.cookie
rm -rf "$auth_ident"
echo "Done"
fi
}
new_service() {
check_auth
read -p "Service name: " service_name
read -p "Service description: " service_descr
read -p "Service summary: " service_summary
read -p "Service base URL: " service_base_url
read -p "Service RSA public key file: " service_pubkey_file
service_pubkey=$(< "$service_pubkey_file")
echo "Registering service $service_name ..."
curl -s -XPOST "$zapi/provider/services" \
-H 'Content-Type: application/json' \
-d '{"name": "'"$service_name"'",
"description": "'"$service_descr"'",
"summary": "'"$service_summary"'",
"base_url": "'"$service_base_url"'",
"public_key": "'"$service_pubkey"'",
"tags": ["tutorial"]
}' \
-b ./.cookie \
| jq .
echo "Done"
}
get_service() {
check_auth
read -p "Service ID: " service_id
curl -s -XGET "$zapi/provider/services/$service_id" -b ./.cookie | jq .
}
list_services() {
check_auth
curl -s -XGET "$zapi/provider/services" -b ./.cookie | jq .
}
update_service() {
check_auth
read -p "Service ID: " service_id
read -p "New service name [default: no change]: " new_name
read -p "New service description [default: no change]: " new_descr
read -p "New service summary [default: no change]: " new_summary
if [ -z "$new_name" ]; then
new_name="null"
else
new_name="\"$new_name\""
fi
if [ -z "$new_descr" ]; then
new_descr="null"
else
new_descr="\"$new_descr\""
fi
if [ -z "$new_summary" ]; then
new_summary="null"
else
new_summary="\"$new_summary\""
fi
echo "Updating service profile ..."
curl -s -XPUT "$zapi/provider/services/$service_id" \
-H 'Content-Type: application/json' \
-d '{"name": '"$new_name"',
"description": '"$new_descr"',
"summary": '"$new_summary"'
}' \
-b ./.cookie
echo "Done"
}
update_service_conn() {
check_auth
read -p "Service ID: " service_id
read -p "New service base URL [default: no change]: " new_base_url
read -p "New service auth token [default: no change]: " new_auth_token
read -p "New service public key (file) [default: no change]: " new_pubkey_file
read -p "New service enabled status (true|false) [default: no change]: " new_enabled
if [ -z "$new_base_url" ]; then
new_base_url="null"
else
new_base_url="\"$new_base_url\""
fi
if [ -z "$new_auth_token" ]; then
new_auth_tokens="null"
else
new_auth_tokens="[\"$new_auth_token\"]"
fi
if [ -z "$new_pubkey_file" ]; then
new_pubkeys="null"
else
new_pubkey=$(< "$new_pubkey_file")
new_pubkeys="[\"$new_pubkey\"]"
fi
if [ -z "$new_enabled" ]; then
new_enabled="null"
fi
auth_password=$(read_password)
echo "Updating service connection data ..."
curl -s -XPUT "$zapi/provider/services/$service_id/connection" \
-H 'Content-Type: application/json' \
-d '{"base_url": '"$new_base_url"',
"auth_tokens": '"$new_auth_tokens"',
"public_keys": '"$new_pubkeys"',
"enabled": '"$new_enabled"',
"password": "'"$auth_password"'"
}' \
-b ./.cookie \
| jq .
echo "Done"
}
delete_service() {
check_auth
read -p "Service ID: " service_id
auth_password=$(read_password)
echo "Deleting service $service_id ..."
curl -s -XDELETE "$zapi/provider/services/$service_id" \
-H 'Content-Type: application/json' \
-d '{"password": "'"$auth_password"'"}' \
-b ./.cookie
echo "Done"
}
new_cert() {
read -p "Target directory: " cert_dir
mkdir -p "$cert_dir"
echo "Writing RSA key pair to $cert_dir/key.pem"
openssl genrsa -out "$cert_dir/key.pem" 4096
echo "Writing CSR to $cert_dir/csr.pem"
openssl req -new -key "$cert_dir/key.pem" -out "$cert_dir/csr.pem"
echo "Writing self-signed certificate to $cert_dir/cert.pem"
openssl x509 -req -days 7300 -in "$cert_dir/csr.pem" -signkey "$cert_dir/key.pem" -out "$cert_dir/cert.pem"
echo "Writing RSA public key to $cert_dir/pubkey.pem"
openssl rsa -in "$cert_dir/key.pem" -pubout -out "$cert_dir/pubkey.pem"
}
read_ident() {
cat ./.current
}
read_password() {
auth_ident=$(< .current)
cat "$auth_ident/.password"
}
################################################################################
# Program
check_tools
zident=$(whoami)
zcmd=""
zenv="prod"
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
usage
;;
-e|--env)
zenv="$2"
shift
;;
-*)
usage "Unknown option '$1'"
;;
*)
if [ -z "$zcmd" ] ; then
zcmd="$1"
else
usage "Too many arguments"
fi
;;
esac
shift
done
if [ $zenv = prod ]; then
zdomain=wire.com
else
zdomain=zinfra.io
fi
if [ -z "$zcmd" ] ; then
usage "Not enough arguments"
fi
zapi="https://${zenv}-nginz-https.${zdomain}"
case "$zcmd" in
"new-provider") new_provider ;;
"auth-provider") authenticate ;;
"get-provider") get_provider ;;
"update-provider") update_provider ;;
"delete-provider") delete_provider ;;
"new-service") new_service ;;
"list-services") list_services ;;
"get-service") get_service ;;
"update-service") update_service ;;
"update-service-conn") update_service_conn ;;
"delete-service") delete_service ;;
"new-cert") new_cert ;;
*) echo "Unknown command: $zcmd" ;;
esac