-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-share-link
324 lines (294 loc) · 9.08 KB
/
get-share-link
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
#!/bin/bash
# Moved to https://github.com/sara-nl/GridScripts/blob/master/get-macaroon
# 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/onnozweers/dcache-scripts/blob/master/get-share-link
#
# Uses a script called view-macaroon, which is available here:
# https://github.com/onnozweers/dcache-scripts/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
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
--permissions <list> - Comma separated list of allowed activities
DOWNLOAD,UPLOAD,DELETE,MANAGE,LIST,
READ_METADATA,UPDATE_METADATA
--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.
--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 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.
--debug - Provide extra output to learn 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
}
url=
path_or_root='path'
activity=DOWNLOAD,LIST
user=
proxy=false
duration=PT12H # Default: 12 hours
ip=
output='link'
debug=false
max_file_size=
while [ $# -gt 0 ] ; do
case "$1" in
--url )
url="$2"
shift 2
;;
--chroot )
path_or_root="root"
shift
;;
--user )
user="$2"
proxy=false
shift 2
;;
--proxy )
proxy=true
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" == "rclone" ] ; then
remote_name="$2"
shift
fi
shift
;;
--debug )
debug=true
shift 1
;;
*)
usage
;;
esac
done
#
# Input checking.
#
if [ -z "$url" ] ; then
usage
fi
if [ -n "$user" -a "$proxy" == "true" ] ; then
echo "ERROR: you can't specify both --user and --proxy." 1>&2
exit 1
fi
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
if $proxy ; then
# Check if the proxy is still valid; if not, exit after the error message.
if [ -x 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"
else
if [ -z "$user" ] ; then
echo "Please specify --proxy, or specify a username with --user." 1>&2
exit 1
fi
authn="-u $user"
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
case $output in
link|macaroon|curl )
# output format OK
;;
rclone )
if [ ! -x 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
;;
* )
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 html2text ]; then
echo "$result" | html2text
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 when --debug is specified
if $debug ; then
if [ -x view-macaroon ] ; then
echo "=== View deserialized macaroon ==="
view-macaroon "$macaroon"
echo "=== End deserialized macaroon ==="
echo
fi
fi
# Log macaroon properties in a (private) file; strip signature to prevent theft
if [ -x view-macaroon ] ; then
{
echo "=== $(date) ==="
view-macaroon "$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"
;;
curl )
# Show download and upload command with curl
if echo "$activity" | grep --silent -e 'DOWNLOAD' -e 'LIST' ; then
echo "Curl download/listing command:"
echo
echo "curl $link"
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:"
;;
* )
echo "Unrecognised output format '$output'." 1>&2
exit 1
esac