Newer
Older
#!/bin/bash
#environment variables
BEARER=""
MAAP_ENV_TYPE=${MAAP_ENV_TYPE}
CLIENT_ID=${CLIENT_ID}
USER_INFO_FILE=/usr/bmap/maap-s3-userinfo.json
#Init the bearer
init() {
#Create file for password and token
if [[ ! -e $USER_INFO_FILE ]]; then
touch $USER_INFO_FILE
# ask the user for information email and password
read -p 'Your email: ' email
read -p 'Your password: ' password
echo
jq -n --arg email_var $email --arg pwd_var $password --arg token_var null '{"email":$email_var, "password": $pwd_var, "token": $token_var }' > $USER_INFO_FILE
echo "Start retrieving token for authent"
BEARER=$(curl -s -X POST -d "client_id=$CLIENT_ID&username=$email&password=$password&grant_type=password&scope=openid+profile" "https://iam.${MAAP_ENV_TYPE,,}.esa-maap.org/oxauth/restv1/token" -v 2>/dev/null | jq -r '.access_token')
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
#Check if the bearer is set
if [ -z "$BEARER" ]
then
echo "Token is empty. Please 1)run refresh function and check your password"
exit 1
else
#Set the token in the file
echo "Bearer is saved for 1 hour : $BEARER"
jq --arg a "${BEARER}" '.token = $a' $USER_INFO_FILE > "tmp" && mv "tmp" $USER_INFO_FILE
fi
else
#If one hous is exceeded (duration of the token)
if [ $(($(date +%s)-$(stat -c "%Y" $USER_INFO_FILE))) -lt 3600 ]
then
echo "INFO: Token is still valid"
else
echo "INFO: Token is expired, we generate a new one"
email=$(cat $USER_INFO_FILE | jq -r '.email')
password=$(cat $USER_INFO_FILE | jq -r '.password')
#Generate a new token
generate_token
fi
#read the json file to get the email and password
echo "Read personal user info"
email=$(cat $USER_INFO_FILE | jq -r '.email')
password=$(cat $USER_INFO_FILE | jq -r '.password')
BEARER=$(cat $USER_INFO_FILE | jq -r '.token')
fi
#Check if the bearer is set
if [ -z "$BEARER" ]
then
echo "Token is empty. Please 1)run refresh function 2)read the log for more info 3)check your password"
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
exit 1
else
echo "Bearer is ready to be used : $BEARER"
#jq --arg a "${BEARER}" '.token = $a' $USER_INFO_FILE > "tmp" && mv "tmp" $USER_INFO_FILE
#Set the variable with the access_token
export BEARER
fi
}
#########################
# Delete userinfo file #
#########################
generate_token() {
BEARER=$(curl -s -X POST -d "client_id=$CLIENT_ID&username=$email&password=$password&grant_type=password&scope=openid+profile" "https://iam.${MAAP_ENV_TYPE,,}.esa-maap.org/oxauth/restv1/token" 2>/dev/null | jq -r '.access_token')
echo "Bearer is correctly generated with user and password : $BEARER"
#Set the token in the file
jq --arg a "${BEARER}" '.token = $a' $USER_INFO_FILE > "tmp" && mv "tmp" $USER_INFO_FILE
}
#########################
# Delete userinfo file #
#########################
refresh() {
if [[ -e $USER_INFO_FILE ]]; then
rm $USER_INFO_FILE
init
else
init
fi
}
#########################
# Upload the data in S3 #
#########################
upload() {
if [ $# -eq 2 ]
then
sourceFile=$1
destination=$2
echo "0- Get an existing or fresh token"
init
echo
echo "1- Starting retrieving the presigned url for the creation of the file"
# xargs trim the result
LOCATION=$(curl -sI -X PUT -H "Authorization: bearer $BEARER" "https://gravitee-gateway.${MAAP_ENV_TYPE,,}.esa-maap.org/s3/$destination" -v | grep -Fi Location | grep -Po "(?<=Location:)[^,]*" | xargs)
#Check if the location is set
if [ -z "$LOCATION" ]
then
echo "Location is empty. Please 1)run init function for fresh token 2)read the log for more info"
exit 1
else
echo "Location is $LOCATION"
LOCATION=${LOCATION%$'\r'}
fi
echo
echo "2- Use the presigned url given by the header Location to upload the data"
echo "2.1- Upload starting"
curl -v -T $sourceFile "$LOCATION"
else
echo "ERROR: Arguments numbers are not correct, please read the help"
display_help
fi
}
#########################
# Delete a data in S3 #
#########################
delete() {
if [ $# -eq 1 ]
then
s3file=$1
echo "0- Get an existing or fresh token"
init
echo
echo "1- Starting retrieving the presigned url for the delation of the file"
# xargs trim the result
LOCATION=$(curl -sI -X DELETE -H "Authorization: bearer $BEARER" "https://gravitee-gateway.${MAAP_ENV_TYPE,,}.esa-maap.org/s3/$s3file" -v | grep -Fi Location | grep -Po "(?<=Location:)[^,]*" | xargs)
#Check if the location is set
if [ -z "$LOCATION" ]
then
echo "Location is empty. Please 1)run init function for fresh token 2)read the log for more info"
exit 1
else
echo "Location is $LOCATION"
LOCATION=${LOCATION%$'\r'}
fi
echo
echo "2- Use the presigned url given by the header Location to delete the data"
echo "2.1- Deletion starting"
curl -v -X DELETE "$LOCATION"
else
echo "ERROR: Arguments numbers are not correct, please read the help"
display_help
fi
}
#########################
# Download data from S3 #
#########################
download() {
if [ $# -eq 2 ]
then
name=$2
s3Path=$1
echo "0- Get an existing or fresh token"
init
echo
echo "1- Starting retrieving the presigned url for the download of the file"
# xargs trim the result
LOCATION=$(curl -sI -X GET -H "Authorization: bearer $BEARER" "https://gravitee-gateway.${MAAP_ENV_TYPE,,}.esa-maap.org/s3/$s3Path" | grep -Fi Location | grep -Po "(?<=Location:)[^,]*" | xargs)
echo $LOCATION
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
#Check if the location is set
if [ -z "$LOCATION" ]
then
echo
echo "Location is empty. Please 1)run init function for fresh token 2)read the log for more info"
exit 1
else
echo "Location used to download the file is $LOCATION"
LOCATION=${LOCATION%$'\r'}
fi
echo
echo "2- Use the presigned url given by the header Location to download the data"
echo "2.1- Download starting"
curl -o $name "$LOCATION"
else
echo "ERROR: Arguments numbers are not correct, please read the help"
display_help
fi
}
#########################
# List data in S3 #
#########################
list() {
if [ $# -eq 1 ]
then
sourceFile=$1
destination=$2
echo "0- Get an existing or fresh token"
init
echo
echo "1- List all files in folder $sourceFile"
curl -s -X GET -H "Authorization: bearer $BEARER" "https://gravitee-gateway.${MAAP_ENV_TYPE,,}.esa-maap.org/s3/$sourceFile?list=true"
else
echo "ERROR: Arguments numbers are not correct, please read the help"
display_help
fi
}
#########################
# The command line help #
#########################
display_help() {
echo "Usage: $0 [option...] {init|refresh|upload|download|ls|delete}" >&2
echo
echo " init Get a fresh token before any request. It ask for email and password"
echo " upload myFile.tiff path/myFile.tiff Upload data in the S3"
echo " download path/in/S3/file.tiff myFile.tiff Download a data from the S3"
echo " ls folder/path List data in a subfolder"
echo " delete path/in/S3/file.tiff Delete an existing data on S3"
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
echo
# echo some stuff here for the -a or --add-options
exit 1
}
################################
# Check if parameters options #
# are given on the commandline #
################################
while :
do
case "$1" in
-r | --resolution)
if [ $# -ne 0 ]; then
resolution="$2" # You may want to check validity of $2
fi
shift 2
;;
-h | --help)
display_help # Call your function
exit 0
;;
-d | --display)
display="$2"
shift 2
;;
-a | --add-options)
# do something here call function
# and write it in your help function display_help()
shift 2
;;
--) # End of all options
shift
break
;;
-*)
echo "Error: Unknown option: $1" >&2
## or call function display_help
exit 1
;;
*) # No more options
break
;;
esac
done
######################
# Check if parameter #
# is set too execute #
######################
case "$1" in
init)
init #calling function init()
;;
refresh)
refresh #calling function refresh()
;;
upload)
upload $2 $3 #calling function upload()
;;
ls)
list $2 # calling function ls()
;;
download)
download $2 $3 # calling function download()
;;
delete)
delete $2 $3 # calling function delete()
;;
*)
# echo "Usage: $0 {init|reset|upload|download|ls|delete}" >&2
display_help
exit 1
;;
esac