-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3bash.sh
242 lines (220 loc) · 6.5 KB
/
s3bash.sh
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
#!/bin/bash
# show usage
display_help() {
cat <<EOF
Usage: $0 HTTP-METHOD URL [options...]
-H "k1:v1" -H "k2:v2"
-O save_http_response_body_to_file
--body-binary=localfile
--body-raw=xxx
#not impl --body-form-urlencode="k1:v1" --body-form-urlencode="k2:v2"
#not impl --body-form-text="k1:v1" --body-form-file="k2:file"
--cacl-md5 auto to cacl content-md5
-a access_key
-s secret_key
-e easy simple out
-h help
EOF
}
if [ $# -lt 2 ]; then
display_help
exit -1
fi
method=${1}
request_url=${2}
shift 2
resource=$request_url
declare -A param_map
typeset -l small_url
small_url=$request_url
if [[ ${small_url}x =~ ^http://.* ]]; then
resource=${request_url:7}
elif [[ ${small_url}x =~ ^https://.* ]]; then
resource=${request_url:8}
fi
if [[ $resource =~ "/" ]]; then
resource=/${resource#*/}
if [[ $resource =~ "?" ]]; then
param_str=${resource#*\?}
resource=${resource%\?${param_str}}
fi
elif [[ $resource =~ "?" ]]; then
param_str=${resource#*\?}
resource=/
else
resource=/
param_str=
fi
param_array=(${param_str//\&/\ })
for param_pair in ${param_array[@]}
do
param_key=${param_pair%%=*}
param_val=${param_pair#${param_key}\=}
if [ -z "$param_val" -o "x${param_val}" = "x${param_pair}" ]; then
param_val=
else
param_val=\=$param_val
fi
param_map[${param_key}]=${param_val}
done
declare -A header_map
declare -A amz_header_map
amz_header_map["x-amz-date"]=`TZ= date -R`
access_key=0555b35654ad1656d804
secret_key="h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q=="
content_md5_key=Content-Md5
content_type_key=Content-Type
date_key=Date
out_file=
declare -A bft_map
declare -A bff_map
declare -A bxwfu_map
body_br=
body_bb=
body_type=0 #0:none 1:form-data 2:x-www-form-urlencoded 3:raw 4:binary
cacl_md5=false
signature_v4=false
easy_out=false
signed_subresources=("acl", "cors", "delete", "lifecycle", "location", "logging", "notification", "partNumber", "policy", "requestPayment", "response-cache-control", "response-content-disposition", "response-content-encoding", "response-content-language", "response-content-type", "response-expires", "tagging", "torrent", "uploadId", "uploads", "versionId", "versioning", "versions", "website")
while getopts "H:O:-:a:s:eh" opt
do
case $opt in
-)
case "${OPTARG}" in
body-form-text=*)
body_type=1
val=${OPTARG#*=}
bft_map[${val%%:*}]=${val#*:};;
body-form-file=*)
body_type=1
val=${OPTARG#*=}
bff_map[${val%%:*}]=${val#*:};;
body-form-urlencode=*)
body_type=2
val=${OPTARG#*=}
bxwfu_map[${val%%:*}]=${val#*:};;
body-raw=*)
body_type=3
body_br=${OPTARG#*=};;
body-binary=*)
body_type=4
body_bb=${OPTARG#*=};;
cacl-md5)
cacl_md5=true;;
signature-v4)
signature_v4=true;;
*)
display_help
exit -1;;
esac;;
H)
normal_key=${OPTARG%%:*}
typeset -l small_key
small_key=${normal_key}
if [[ $small_key =~ ^x-amz-.* ]]; then
amz_header_map[${small_key}]=${OPTARG#*:}
else
if [[ x$small_key == xcontent-md5 ]]; then
content_md5_key=$normal_key
elif [[ x$small_key == xcontent-type ]]; then
content_type_key=$normal_key
elif [[ x$small_key == xdate ]]; then
date_key=$normal_key
fi
header_map[${normal_key}]=${OPTARG#*:}
fi
;;
O)
out_file=${OPTARG};;
a)
access_key=${OPTARG};;
s)
secret_key=${OPTARG};;
h)
display_help
exit 0;;
e)
easy_out=true;;
\?)
display_help
exit -1;;
esac
done
if [[ -z ${header_map[$content_type_key]} ]]; then
# be sure header_map_keys has content_type_key. then curl can not rewrite content-type
header_map[$content_type_key]=
#0:none 1:form-data 2:x-www-form-urlencoded 3:raw 4:binar
case $body_type in
1) header_map[$content_type_key]="multipart/form-data;";;
2) header_map[$content_type_key]="application/x-www-form-urlencoded";;
3) header_map[$content_type_key]="application/xml";;
esac
fi
if [[ n$cacl_md5 = ntrue ]]; then
header_map[$content_md5_key]=
case $body_type in
1);;
2);;
3) header_map[$content_md5_key]=`echo -en ${body_br} | openssl dgst -md5 -binary | openssl enc -base64`;;
4) header_map[$content_md5_key]=`openssl dgst -md5 -binary ${body_bb} | openssl enc -base64`;;
esac
fi
#echo ${!header_map[@]}
############# version 2 authorization start #############
# string_to_sign=HTTPVerb + "\n" + ContentMD5 + "\n" + ContentType + "\n" + Date + "\n" + AmzHeaders + Resource;
string_to_sign="${method}\n${header_map[$content_md5_key]}\n${header_map[$content_type_key]}\n${header_map[$date_key]}\n"
amz_header_keys=($(echo ${!amz_header_map[@]} | sed 's/ /\n/g' |sort ))
for key in ${amz_header_keys[@]}
do
string_to_sign="${string_to_sign}${key}:${amz_header_map[$key]}\n"
done
string_to_sign="${string_to_sign}${resource}"
param_keys=($(echo ${!param_map[@]} | sed 's/ /\n/g' |sort ))
first_param=true
for key in ${param_keys[@]}
do
if [[ "${signed_subresources[*]}" == *"$key"* ]]; then
if $first_param ; then
string_to_sign="${string_to_sign}?${key}${param_map[$key]}"
else
string_to_sign="${string_to_sign}&${key}${param_map[$key]}"
fi
first_param=false
fi
done
signature=`echo -en ${string_to_sign} | openssl sha1 -hmac ${secret_key} -binary | base64`
############## version 2 authorization end ##############
############# curl cmd start #############
if $easy_out ; then
curl_cmd=("curl" "-X" "${method}")
else
echo "###### string_to_sign: $string_to_sign"
curl_cmd=("curl" "-v" "-X" "${method}")
fi
if [[ x$method == xHEAD ]]; then
curl_cmd=("${curl_cmd[@]}" "-I")
fi
for key in ${!header_map[@]}
do
curl_cmd=("${curl_cmd[@]}" "-H" "\"${key}: ${header_map[$key]}\"")
done
for key in ${!amz_header_map[@]}
do
curl_cmd=("${curl_cmd[@]}" "-H" "\"${key}: ${amz_header_map[$key]}\"")
done
curl_cmd=("${curl_cmd[@]}" "-H" "\"Authorization: AWS ${access_key}:${signature}\"")
if [ -n "$out_file" ]; then
curl_cmd=("${curl_cmd[@]}" "-o" "${out_file}")
fi
case $body_type in
#0:none 1:form-data 2:x-www-form-urlencoded 3:raw 4:binary
3) curl_cmd=("${curl_cmd[@]}" "-d" "\"${body_br}\"");;
4) curl_cmd=("${curl_cmd[@]}" "-T" "${body_bb}");;
esac
curl_cmd=("${curl_cmd[@]}" "\"${request_url}\"")
if ! $easy_out ; then
echo "###### curl command: "${curl_cmd[@]}
fi
############## curl cmd end ##############
eval ${curl_cmd[@]}
exit $?