-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget-macaroon
executable file
·490 lines (459 loc) · 14.9 KB
/
get-macaroon
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/bin/bash
# This script talks to a dCache webdav door to obtain a share link
# (using Macaroons, see https://www.dcache.org/manuals/workshop-2017-05-29-Umea/000-Final/anupam_macaroons_v02.pdf )
# Thanks to Paul Millar for providing info and examples.
#
# Latest version available at:
# https://github.com/sara-nl/GridScripts/blob/master/get-macaroon
#
# Old versions available at:
# https://github.com/onnozweers/dcache-scripts/blob/master/get-share-link
#
# Uses a script called view-macaroon, which is available here:
# https://github.com/sara-nl/GridScripts/blob/master/view-macaroon
#
# Changes:
# 2018-06-22 - Onno - Initial version
# 2018-06-22 - Onno - Added --chroot option
# 2018-06-25 - Onno - Fixed X509 authentication; adding curl command to debug output
# 2018-07-03 - Onno - Added output options (macaroon, curl, rclone); added disclaimer
# 2018-07-10 - Onno - Added --ip option
# 2018-09-07 - Onno - Adding deserialized macaroon to debug output
# 2018-09-13 - Onno - Log macaroon properties in ~/macaroons.log, without signature
# 2018-09-14 - Onno - Added maximum upload file size option
# 2018-11-02 - Onno - Load defaults from file
# 2019-01-15 - Raymond Oonk - Fixed reference to view-macaroon
# 2019-03-21 - Onno - Added --output qr; show macaroon properties in color
# 2019-03-22 - Onno - QR option more flexible; fixed executable tests
# 2020-03-07 - Onno - Added --netrc option
# 2020-03-09 - Onno - Check for view-macaroon in same dir as get-macaroon
# 2020-03-31 - Juan Luis Font Calvo - Fix html2text call
usage() {
cat <<-EOF
Obtains a share link from a dCache webdav door using Macaroons.
Usage: $0 [options...]
Options are:
--url <url>
--chroot - Make specified path the root diectory, hiding upper dirs
--proxy - Use proxy specified in X509_USER_PROXY
--user <username> - Username/password authentication
--netrc [filename] - Authenticate with a curl netrc file.
If no filename was provided, use ~/.netrc.
--permissions <list> - Comma separated list of allowed activities. Example:
DOWNLOAD,LIST
These permissions can be given:
DOWNLOAD - Read a file
UPLOAD - Write a file
DELETE - Delete a file or directory
MANAGE - Rename or move a file or directory
LIST - List objects in a directory
READ_METADATA - Read file status
UPDATE_METADATA - Stage a file, change QoS
--duration - Duration that the link will be valid, in ISO8601 format
See https://en.wikipedia.org/wiki/ISO_8601#Durations
The duration may be limited by the server.
--ip <subnet-list> - Allow access from these addresses/subnets.
Multiple subnets may be specified, comma separated.
Don't forget IPv6.
The person whom the macaroon is for, can find the
external addresses of his/her system with this command:
curl -4 icanhazip.com ; curl -6 icanhazip.com
or by browsing to https://whatismyipaddress.com/.
--max-file-size - Set a maximum file size for uploads. dCache 5 required.
--output link - Print a link that anyone can open in a browser.
If you open the link in your browser, please do it in
a new private window, to be certain you're authenticated
by the macaroon and not by x509 or username/password.
--output macaroon - Print authentication token only.
--output file <name> - Save macaroon in a file.
--output curl - Print curl command(s) to use the data.
--output rclone <name> - Save an rclone config file, <name>.conf.
You need rclone v1.42-012-gfa051ff9 or newer to use it.
--output qr <name> - Create a QR PNG image based on the share link.
--output qr display - Create a QR PNG image and show it now.
--debug - Show what's going on.
Macaroon properties are logged in ~/macaroons.log.
Their signature is not logged to prevent theft.
Examples:
$0 --url https://my-dcache-server.org/users/homer/disk-shared/ --user homer
$0 --url https://my-dcache-server.org:2882/users/homer/disk-shared/ --proxy --duration P14D
$0 --url https://my-dcache-server.org:2882/users/homer/disk-shared/ --proxy --chroot --output rclone homers-share
$0 --url https://my-dcache-server.org/users/homer/disk-shared/ --user homer --ip 145.100.0.0/16,2001:610:108::/48
Warning:
A macaroon is token that authorizes anyone who gets it. Send it through private channels (not email).
Add sufficient caveats to limit the risk of accidents or abuse.
EOF
exit 1
}
script_dir=$(dirname "$0")
url=
path_or_root='path'
activity=DOWNLOAD,LIST
auth_method=
user=
duration=PT12H # Default: 12 hours
ip=
output='link'
debug=false
max_file_size=
# Load defaults from configuration file if exists
for configfile in /etc/get-macaroon.conf ~/.get-macaroon.conf ; do
if [ -f "$configfile" ] ; then
echo "Loading $configfile"
source "$configfile"
fi
done
while [ $# -gt 0 ] ; do
case "$1" in
--url )
url="$2"
shift 2
;;
--chroot )
path_or_root="root"
shift
;;
--user )
user="$2"
auth_method=user
shift ; shift
;;
--netrc )
auth_method=netrc
case $2 in
--* | '' )
# Next argument is another option or absent; not a file name
netrcfile=~/.netrc
;;
* )
# This must be a file name
netrcfile="$2"
shift
;;
esac
shift
;;
--proxy )
auth_method=proxy
shift
;;
--permissions )
activity="$2"
shift 2
;;
--duration )
duration="$2"
shift 2
;;
--ip )
ip="$2"
shift 2
;;
--max-file-size )
max_file_size="$2"
shift 2
;;
--output )
output="$2"
shift
if [ "$output" == "file" ] ; then
filename="$2"
shift
elif [ "$output" == "rclone" ] ; then
remote_name="$2"
shift
elif [ "$output" == "qr" ] ; then
png_filename="$2"
shift
fi
shift
;;
--debug )
debug=true
shift 1
;;
*)
usage
;;
esac
done
#
# Input checking.
#
if [ -z "$url" ] ; then
usage
fi
case $auth_method in
netrc )
if [ ! -f "$netrcfile" ] ; then
echo "ERROR: could not open netrc file '$netrcfile'."
exit 1
fi
authn="--netrc-file $netrcfile"
;;
proxy )
if [ ! -f "$X509_USER_PROXY" ] ; then
echo "ERROR: could not open proxy '$X509_USER_PROXY'."
exit 1
fi
if [ ! -d /etc/grid-security/certificates ] ; then
echo "ERROR: could not find /etc/grid-security/certificates/." \
"Please install the Grid root certificates if you want to use your proxy."
exit 1
fi
# Check if the proxy is still valid; if not, exit after the error message.
if [ -x "$(command -v voms-proxy-info)" ]; then
voms-proxy-info --exists 1>&2 || exit 1
fi
authn="--capath /etc/grid-security/certificates/ --cert $X509_USER_PROXY --cacert $X509_USER_PROXY"
;;
user )
authn="-u $user"
;;
token )
# We can't specify the token as a command line argument,
# because others could read that with the ps command.
# So we have to put the authorization header in a temporary file.
# The mktemp command differs on OSX.
curl_authorization_header_file=$(mktemp authorization_header_XXXXXXXXXXXX)
chmod 600 "$curl_authorization_header_file"
# File should be cleaned up when we're done,
# unless we're debugging
if $debug ; then
trap "{
echo
echo 'WARNING: in debug mode, the authorization header file' \
'$curl_authorization_header_file will not be cleaned up.' \
'Please clean it up yourself.'
}" EXIT
else
trap 'rm -f "$curl_authorization_header_file"' EXIT
fi
# Save the header in the file
echo "header \"Authorization: Bearer $token\"" > "$curl_authorization_header_file"
# Refer to the file with the header
authn="--config $curl_authorization_header_file"
;;
* )
echo "ERROR: you have to specify a valid authentication method."
exit 1
;;
esac
server=$(echo "$url" | egrep -o 'https://[^/]+/')
dir=$(echo "$url" | sed -e 's#https://[^/]*##')
if [ -z "$dir" ] ; then
dir=/
fi
if [ -z "$server" ] ; then
echo "Please include the server in '--url'." 1>&2
exit 1
fi
# Checking the duration; should be ISO8601 format.
# The grep in OSX does not support --perl-regex, so we give it a slightly simpler regex.
case $OSTYPE in
darwin* )
if ! echo "$duration" | grep -E --silent '^P(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?$' ; then
echo "Duration should be in ISO8601 duration format. Examples: PT5M (5 minutes), P1Y2M (1 year and 2 months)" 1>&2
exit 1
fi
;;
* )
if ! echo "$duration" \
| grep --silent --perl-regex '^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d+[HMS])(\d+H)?(\d+M)?(\d+S)?)?$'
then
echo "Duration should be in ISO8601 duration format. Examples: PT5M (5 minutes), P1Y2M (1 year and 2 months)" 1>&2
exit 1
fi
;;
esac
case $output in
link|macaroon|curl )
# output format OK
;;
file )
if [ -z "$filename" ] ; then
echo "If output is file, please specify a file name." 1>&2
exit 1
fi
;;
rclone )
if [ ! -x "$(command -v rclone)" ]; then
echo 'rclone not found in $PATH.' 1>&2
exit 1
fi
if [ -z "$remote_name" ] ; then
echo "If output is rclone, please specify a name for the remote." 1>&2
exit 1
fi
;;
qr )
if [ ! -x "$(command -v qrencode)" ] ; then
echo 'Need `qrencode` for this option.' 1>&2
exit 1
fi
if [ -z "$png_filename" ] ; then
echo "If output is qr, please specify a name for the PNG file." 1>&2
exit 1
fi
;;
* )
echo "Unrecognised output format '$output'." 1>&2
exit 1
esac
#
# End of input checking.
#
if [ -n "$ip" ] ; then
ip_caveat=", \"ip:$ip\""
else
ip_caveat=
fi
if [ -n "$max_file_size" ] ; then
max_size_caveat=", \"max-upload:$max_file_size\""
else
max_size_caveat=
fi
read -r -d '' json_request <<EOF
{
"caveats" : ["$path_or_root:$dir", "activity:$activity" $ip_caveat $max_size_caveat],
"validity" : "$duration"
}
EOF
if $debug ; then
echo "JSON input:"
echo "$json_request"
echo
echo "Curl command:"
echo "curl --silent " \
"$authn " \
"-X POST -H 'Content-Type: application/macaroon-request' " \
"-d \'$json_request\' " \
"$server "
fi
result=$(curl --silent \
$authn \
-X POST -H 'Content-Type: application/macaroon-request' \
-d "$json_request" \
$server )
if ! echo "$result" | grep --silent "targetWithMacaroon" ; then
{
echo "ERROR: could not get share link from $server."
# Show output from server in the nicest way possible
if [ -x "$(command -v html2text)" ]; then
echo "$result" | html2text /dev/stdin
else
echo "$result"
fi
} 1>&2
exit 1
fi
if $debug ; then
echo "JSON output:"
echo "$result"
echo
fi
macaroon=$(echo "$result" | jq -r '.macaroon')
link=$(echo "$result" | jq -r '.uri.targetWithMacaroon')
# Show contents of macaroon
if [ -x "${script_dir}/view-macaroon" ] ; then
macaroon_viewer="${script_dir}/view-macaroon"
else
macaroon_viewer="$(command -v view-macaroon)"
fi
echo "Macaroon viewer: $macaroon_viewer"
if [ -x "$macaroon_viewer" ] ; then
BLUE=$'\e[34m'
RESET=$'\e[39m'
echo -e "$BLUE"
echo "=== View deserialized macaroon ==="
$macaroon_viewer <<<"$macaroon"
echo "=== End deserialized macaroon ==="
echo -e "$RESET"
echo
#
# Log macaroon properties in a (private) file; strip signature to prevent theft
{
echo "=== $(date) ==="
$macaroon_viewer <<<"$macaroon" | grep -v signature
echo -e "=====================================\n"
} >> ~/macaroons.log
chmod 600 ~/macaroons.log
fi
case $output in
link )
# Show link that can be pasted in browser
echo "$link"
;;
macaroon )
# Just the bare token, nothing else
echo "$macaroon"
;;
file )
# Save token in a file
touch "$filename"
chmod 600 "$filename"
echo "$macaroon" > "$filename"
echo "Macaroon saved in file '$filename'."
;;
curl )
# Show download and upload command with curl
if echo "$activity" | grep --silent -e 'DOWNLOAD' -e 'LIST' ; then
echo "Curl download/listing command:"
echo
if [ "$path_or_root" = "root" ] ; then
echo "curl --fail --location $link --output myfile"
else
echo "curl --fail --location $url?authz=$macaroon --output myfile"
fi
echo
fi
if echo "$activity" | grep --silent 'UPLOAD' ; then
echo "Curl upload command:"
echo
if [ "$path_or_root" = "root" ] ; then
echo "curl --header 'Authorization: BEARER $macaroon' --upload-file myfile $server"
else
echo "curl --header 'Authorization: BEARER $macaroon' --upload-file myfile $url"
fi
echo
fi
;;
rclone )
echo "Creating rclone config file $remote_name.conf:"
echo
$debug && echo "rclone --config=$remote_name.conf config create $remote_name webdav url $server vendor other user '' password '' bearer_token $macaroon"
rclone --config=$remote_name.conf config create $remote_name webdav url $server vendor other user '' password '' bearer_token $macaroon
echo
echo "Send this file to the persons you want to share data with."
echo "They need rclone v1.42-012-gfa051ff9 or newer to access the data."
echo "Example command:"
echo "rclone --config=$remote_name.conf ls $remote_name:"
;;
qr )
if [[ $png_filename != *.png ]] ; then
png_filename="$png_filename.png"
fi
# Generate QR image
echo "Creating QR image file: $png_filename"
if qrencode -o $png_filename "$link" ; then
# If the name is "display[.png]", the user wants to see it now!
if [ "$png_filename" = "display.png" ] ; then
# Try to open it in an image viewer
for imageviewer in feh display xdg-open eog Xming ; do
if [ -x "$(command -v $imageviewer)" ] ; then
$imageviewer $png_filename
break
fi
done
else # User does not need to see it now. Show filename.
echo "Created QR image file: $png_filename"
fi
else
echo "ERROR: Could not create QR file."
exit 1
fi
;;
* )
echo "Unrecognised output format '$output'." 1>&2
exit 1
esac