-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget-macaroon
More file actions
executable file
·586 lines (549 loc) · 18.8 KB
/
Copy pathget-macaroon
File metadata and controls
executable file
·586 lines (549 loc) · 18.8 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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#!/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-token, which is available here:
# https://github.com/sara-nl/GridScripts/blob/master/view-token
#
# 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
# 2025-08-13 - Onno - Show better error messages
# 2025-08-13 - Onno - Fix token authentication
# 2025-08-13 - Onno - Improve quoting and other things suggested by ShellCheck
# 2025-08-13 - Onno - Use view-token instead of view-macaroon
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 X509 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.
--tokenfile <file> - Read input authentication token from this file
If no authentication method is specified,
get-macaroon will try to use \$BEARER_TOKEN.
--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
STAGE - Stage (restore) a file from tape
READ_METADATA - Read file status
UPDATE_METADATA - Change properties, change QoS
--duration - Duration that the link or macaroon 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 only.
This may protect your macaroon from abuse.
Multiple subnets may be specified, comma separated.
A system may have both an IPv4 and an IPv6 address.
In that case, add them both to this list.
The person whom the macaroon is for, can find the
external addresses of his/her system with
these commands:
curl -4 https://ifconfig.co
curl -6 https://ifconfig.co
or by browsing to https://whatismyipaddress.com/.
On SURF systems, get-macaroon is pre-configured
with the subnets of many SURF services. You may not
need to use the --ip option.
'--ip auto' will use the external IP address(es)
of your local system. The macaroon will only be valid
on your own system, not on any other. This may
prevent abuse.
--max-file-size - Set a maximum file size for uploads.
--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
#
# Some functions
#
get_my_ip () {
# Collect external addresses of the local system.
# We need *external* addresses because those are the ones dCache
# will see and validate. Local, private addresses
# (like 192.168.0.2) are useless.
# We try several providers (just in case one is down).
{ curl --silent -6 https://ifconfig.co \
|| curl --silent -6 https://api64.ipify.org \
|| curl --silent -6 https://v6.ident.me; \
curl --silent -4 https://ifconfig.co \
|| curl --silent -4 https://api.ipify.org \
|| curl --silent -4 https://checkip.amazonaws.com;
} \
| grep --invert-match '^$' \
| paste -s -d , -
}
#
# End of functions
#
while [ $# -gt 0 ] ; do
case "$1" in
--url )
url="$2"
shift ; shift
;;
--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
;;
--tokenfile )
auth_method=token
tokenfile="$2"
token=$(<"$tokenfile")
shift ; shift
;;
--permissions )
activity="$2"
shift ; shift
;;
--duration )
duration="$2"
shift ; shift
;;
--ip )
ip="$2"
shift ; shift
;;
--max-file-size )
max_file_size="$2"
shift ; shift
;;
--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
if [ -z "$auth_method" ] ; then
# User did not specify authentication method. Let's try if BEARER_TOKEN is set and use that.
if [ -n "$BEARER_TOKEN" ] ; then
echo 1>&2 "Using \$BEARER_TOKEN for authentication."
token="$BEARER_TOKEN"
auth_method='token'
else
echo 1>&2 "ERROR: no authentication method was specified, and BEARER_TOKEN is not set."
exit 1
fi
fi
case $auth_method in
netrc )
if [ ! -f "$netrcfile" ] ; then
echo 1>&2 "ERROR: could not open netrc file '$netrcfile'."
exit 1
fi
authn="--netrc-file $netrcfile"
;;
proxy )
if [ ! -f "$X509_USER_PROXY" ] ; then
echo 1>&2 "ERROR: could not open proxy '$X509_USER_PROXY'."
exit 1
fi
if [ ! -d /etc/grid-security/certificates ] ; then
echo 1>&2 "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 other users on the same system could read the token
# with the ps command and steal and abuse it.
# 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 1>&2 "ERROR: you have to specify a valid authentication method."
exit 1
;;
esac
server=$(echo "$url" | grep -E -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
# Additional validation based on the output format
case $output in
link | macaroon | curl )
# No extra validation needed
;;
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
if [ "$ip" == 'auto' ] ; then
# Collect external addresses from the local system.
ips=$(get_my_ip)
if [ -z "$ips" ] ; then
echo 1>&2 "ERROR: could not obtain external IP address of the local system."
exit 1
fi
ip_caveat=", \"ip:$ips\""
else
ip_caveat=", \"ip:$ip\""
fi
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' " \
"--fail --show-error " \
"--dump-header >(grep '^HTTP/1.1' | grep -v '200' >&2)"
fi
# This is where we actually obtain the macaroon from the dCache WebDAV door.
# We use --fail, --show-error and --dump-header to show clear error messages.
# --fail --show-error will show connection errors ("Could not resolve host")
# --dump-header will show the HTTP code and its description ("HTTP/1.1 401 login failed").
result=$(curl --silent \
$authn \
-X POST -H 'Content-Type: application/macaroon-request' \
-d "$json_request" \
"$server" \
--fail --show-error \
--dump-header >(grep '^HTTP/1.1' | grep -v '200' >&2)
)
if ! echo "$result" | grep --silent "targetWithMacaroon" ; then
# No macaroon was returned. Print possible error messages to stderr.
{
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-token" ] ; then
macaroon_viewer="${script_dir}/view-token"
else
macaroon_viewer="$(command -v view-token)"
fi
if [ -x "$macaroon_viewer" ] ; then
echo 1>&2 "Macaroon viewer: $macaroon_viewer"
# The <(cat) construction is to prevent the macaroon to be visible with the 'ps' command.
macaroon_decoded=$($macaroon_viewer --minimal <(cat <<<"$macaroon") )
{
echo "=== View deserialized macaroon ==="
echo "$macaroon_decoded"
echo "=== End deserialized macaroon ==="
echo
} 1>&2
#
# Log macaroon properties in a (private) file
{
echo "=== $(date) ==="
echo "$macaroon_decoded"
echo -e "=====================================\n"
} >> ~/macaroons.log
chmod 600 ~/macaroons.log
else
{
echo
echo "Warning: could not find command view-token." \
"Unable to show or log the macaroon properties."
echo "Install view-token (from the same repo as get-macaroon) to get rid of this message."
echo
} 1>&2
fi
case $output in
link )
# Show link that can be pasted in browser
echo "$link"
;;
macaroon )
# Just the bare token, nothing else
# It's important that all other output is sent to stderr, so users can do this:
# export BEARER_TOKEN=$(get-macaroon ... --output macaroon)
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 1>&2 "ERROR: Could not create QR file."
exit 1
fi
;;
* )
echo "Unrecognised output format '$output'." 1>&2
exit 1
esac