-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathsign_s3_url.bash
executable file
·200 lines (157 loc) · 6 KB
/
sign_s3_url.bash
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
#!/bin/bash -e
function displayUsage()
{
local -r scriptName="$(basename "${BASH_SOURCE[0]}")"
echo -e "\033[1;33m"
echo "SYNOPSIS :"
echo " ${scriptName}"
echo " --help"
echo " --aws-access-key-id <AWS_ACCESS_KEY_ID>"
echo " --aws-secret-access-key <AWS_SECRET_ACCESS_KEY>"
echo " --region <REGION>"
echo " --bucket <BUCKET>"
echo " --file-path <FILE_PATH>"
echo " --method <METHOD>"
echo " --minute-expire <MINUTE_EXPIRE>"
echo -e "\033[1;32m"
echo "USE CASES :"
echo " If you have a private/public S3 bucket and would like to share the downloadable links to anyone,"
echo " this tool will help to generate signed S3 URLs"
echo -e "\033[1;35m"
echo "DESCRIPTION :"
echo " --help Help page"
echo " --aws-access-key-id AWS Access Key ID (optional, defaults to \${AWS_ACCESS_KEY_ID})"
echo " --aws-secret-access-key AWS Secret Access Key (optional, defaults to \${AWS_SECRET_ACCESS_KEY})"
echo " --region Region (optional, defaults to \${AWS_DEFAULT_REGION})"
echo " Valid regions: $(getAllowedRegions)"
echo " --bucket Bucket name (require)"
echo " --file-path File path (require)"
echo " --method HTTP request method (optional, defaults to '${METHOD}' METHOD)"
echo " --minute-expire Minutes to expire signed URL (optional, defaults to '${MINUTE_EXPIRE}' minutes)"
echo -e "\033[1;36m"
echo "EXAMPLES :"
echo " ./${scriptName} --help"
echo " ./${scriptName} --bucket 'my_bucket_name' --file-path 'my_path/my_file.txt'"
echo " ./${scriptName} --aws-access-key-id '5KI6IA4AXMA39FV7O4E0' --aws-secret-access-key '5N2j9gJlw9azyLEVpbIOn/tZ2u3sVjjHM03qJfIA' --region 'us-west-1' --bucket 'my_bucket_name' --file-path 'my_path/my_file.txt' --method 'PUT' --minute-expire '30'"
echo -e "\033[0m"
exit "${1}"
}
function generateSignURL()
{
local -r awsAccessKeyID="${1}"
local -r awsSecretAccessKey="${2}"
local region="${3}"
local -r bucket="${4}"
local -r filePath="${5}"
local -r method="${6}"
local -r minuteExpire="${7}"
if [[ "${region}" = 'us-east-1' ]]
then
region=''
fi
local -r endPoint="$("$(isEmptyString "${region}")" = 'true' && echo 's3.amazonaws.com' || echo "s3-${region}.amazonaws.com")"
local -r expire="$(($(date +'%s') + minuteExpire * 60))"
local -r signature="$(
echo -en "${method}\n\n\n${expire}\n/${bucket}/${filePath}" |
openssl dgst -sha1 -binary -hmac "${awsSecretAccessKey}" |
openssl base64
)"
local -r query="AWSAccessKeyId=$(encodeURL "${awsAccessKeyID}")&Expires=${expire}&Signature=$(encodeURL "${signature}")"
echo "https://${endPoint}/${bucket}/${filePath}?${query}"
}
function main()
{
source "$(dirname "${BASH_SOURCE[0]}")/libraries/aws.bash"
source "$(dirname "${BASH_SOURCE[0]}")/libraries/util.bash"
# Set Default Values
local awsAccessKeyID="${AWS_ACCESS_KEY_ID}"
local awsSecretAccessKey="${AWS_SECRET_ACCESS_KEY}"
REGION="${AWS_DEFAULT_REGION}"
METHOD='GET'
MINUTE_EXPIRE='15'
# Parse Inputs
local -r optCount="${#}"
while [[ "${#}" -gt '0' ]]
do
case "${1}" in
--help)
displayUsage 0
;;
--aws-access-key-id)
shift
if [[ "${#}" -gt '0' ]]
then
awsAccessKeyID="$(trimString "${1}")"
echo
fi
;;
--aws-secret-access-key)
shift
if [[ "${#}" -gt '0' ]]
then
awsSecretAccessKey="$(trimString "${1}")"
fi
;;
--region)
shift
if [[ "${#}" -gt '0' ]]
then
REGION="$(trimString "${1}")"
fi
;;
--bucket)
shift
if [[ "${#}" -gt '0' ]]
then
local bucket=''
bucket="$(trimString "${1}")"
fi
;;
--file-path)
shift
if [[ "${#}" -gt '0' ]]
then
local filePath=''
filePath="$(formatPath "$(trimString "${1}")" | sed -e 's/^\///g')"
fi
;;
--method)
shift
if [[ "${#}" -gt '0' ]]
then
METHOD="$(trimString "${1}")"
fi
;;
--minute-expire)
shift
if [[ "${#}" -gt '0' ]]
then
MINUTE_EXPIRE="$(trimString "${1}")"
fi
;;
*)
shift
;;
esac
done
# Validate Inputs
if [[ "$(isEmptyString "${awsAccessKeyID}")" = 'true' || "$(isEmptyString "${awsSecretAccessKey}")" = 'true' || "$(isEmptyString "${bucket}")" = 'true' || "$(isEmptyString "${filePath}")" = 'true' ]]
then
if [[ "${optCount}" -lt '1' ]]
then
displayUsage 0
fi
error '\nERROR: awsAccessKeyID, awsSecretAccessKey, bucket, or filePath not found\n'
displayUsage 1
fi
if [[ "$(isEmptyString "${REGION}")" = 'true' || "$(isValidRegion "${REGION}")" = 'false' ]]
then
fatal "\nFATAL: region must be valid string of $(getAllowedRegions)\n"
fi
if [[ "${MINUTE_EXPIRE}" -lt '1' ]]
then
fatal '\nFATAL: invalid MINUTE_EXPIRE\n'
fi
generateSignURL "${awsAccessKeyID}" "${awsSecretAccessKey}" "${REGION}" "${bucket}" "${filePath}" "${METHOD}" "${MINUTE_EXPIRE}"
}
main "$@"