-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAliyunOpenApiSDK.sh
executable file
·150 lines (133 loc) · 4.54 KB
/
AliyunOpenApiSDK.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
#!/bin/bash
for _aliapi_command in openssl curl; do
if ! command -v $_aliapi_command &> /dev/null; then
echo "Aliyun OpenAPI SDK: $_aliapi_command command not found" >&2
exit 127
fi
done
unset _aliapi_command
[[ -v ALIYUN_SDK_RUN_ON_MUSL_LIBC ]] || ALIYUN_SDK_RUN_ON_MUSL_LIBC=$(ldd "$SHELL" | grep -q /lib/ld-musl && echo 1 || echo 0)
ALIYUN_SDK_LAST_HTTP_CODE=0
# aliapi_rpc <http_method> <host> <api_version> <api_action> [<--key> <value>...]
aliapi_rpc() {
_aliapi_check_vars || return $?
if [[ $# -lt 4 ]];then
echo "aliapi_rpc: not enough parameters" >&2
return 2
fi
local -r _AliAccessKeyId=$AliAccessKeyId _AliAccessKeySecret=$AliAccessKeySecret
local -u _http_method=$1
local _http_host=$2
local _api_version=$3
local _api_action=$4
shift 4
local _query_str _signature_nonce _timestamp
_signature_nonce=$(_aliapi_urlencode "$(_aliapi_signature_nonce)")
_timestamp=$(_aliapi_urlencode "$(_aliapi_timestamp_rpc)")
_query_str="AccessKeyId=$_AliAccessKeyId&Action=$_api_action&Format=JSON&SignatureMethod=HMAC-SHA1&SignatureVersion=1.0&SignatureNonce=$_signature_nonce&Timestamp=$_timestamp&Version=$_api_version&"
# 解析其余参数
local _key _value
while [[ $# -ne 0 ]]
do
case $1 in
--*)
if [[ $# -le 1 ]]; then
echo "aliapi_rpc: '$1' has no value" >&2
return 2
fi
_key=${1:2}
_value=$2
[[ $_value =~ .+\(\)$ && $(type -t "${_value:0:-2}") == "function" ]] && _value=$(${_value:0:-2})
_value=$(_aliapi_urlencode "$_value")
_query_str+="$_key=$_value&"
shift 2
;;
*)
echo "aliapi_rpc: '$1' is unknown parameter" >&2
return 2
;;
esac
done
local _signature
_signature=$(_aliapi_signature_rpc "$_http_method" "${_query_str:0:-1}")
_query_str+="Signature=$(_aliapi_urlencode "$_signature")"
local _curl_out _http_url="https://$_http_host/?$_query_str"
_curl_out=$(curl --location --silent --show-error --request "$_http_method" --write-out "%{http_code}" --connect-timeout 3 "$_http_url")
printf %s "${_curl_out:0:-3}"
ALIYUN_SDK_LAST_HTTP_CODE=${_curl_out:${#_curl_out}-3}
[[ $ALIYUN_SDK_LAST_HTTP_CODE -eq 200 ]] && return 0 || return 1
}
_aliapi_check_vars() {
if [[ ! -v AliAccessKeyId || ! -v AliAccessKeySecret ]]; then
echo "Aliyun OpenAPI SDK: 'AliAccessKeyId' or 'AliAccessKeySecret' environment variable not found" >&2
return 3
fi
}
_aliapi_signature_rpc() {
if [[ ${LC_ALL:-X} != C ]]; then
LC_ALL=C _aliapi_signature_rpc "$@"
return $?
fi
local -u _http_method=$1
local _str=$2 _query_str _sign_str
local _newline="
"
_str=$(sort <<< "${_str//"&"/"$_newline"}")
_query_str=${_str//"$_newline"/"&"}
_sign_str="$_http_method&$(_aliapi_urlencode "/")&$(_aliapi_urlencode "$_query_str")"
printf "%s" "$_sign_str" | openssl dgst -sha1 -hmac "$_AliAccessKeySecret&" -binary | openssl base64 -e
}
_aliapi_timestamp_rpc() {
# ISO8601 UTC
date -u -Iseconds
}
_aliapi_signature_nonce() {
local nonce=""
if [[ -f /proc/sys/kernel/random/uuid ]]; then
nonce=$(</proc/sys/kernel/random/uuid)
else
nonce=$(date +%s%N)
fi
echo "$RANDOM${nonce//-/}$RANDOM"
}
_aliapi_urlencode() {
if [[ ${LC_ALL:-X} != C ]]; then
LC_ALL=C _aliapi_urlencode "$@"
return $?
fi
local char hex string=$1
while [[ -n $string ]]; do
char=${string:0:1}
string=${string:1}
case $char in
[-._~0-9A-Za-z]) printf %c "$char";;
*)
if [[ ALIYUN_SDK_RUN_ON_MUSL_LIBC -eq 0 ]]; then
printf %%%02X "'$char"
else
# Hack musl libc for not ASCII chars (incomplete test)
hex=$(printf %02X "'$char")
printf %%%s "${hex:${#hex}-2}"
fi
;;
esac
done
echo
}
if [[ ${#BASH_SOURCE[@]} -eq 1 ]]; then
set -euf -o pipefail
if [[ $# -eq 0 ]]; then
echo "$(basename "$0") <--rpc> <http_method> <host> <api_version> <api_action> [<--key> <value>...]" >&2
exit 2
fi
case $1 in
--rpc)
shift
aliapi_rpc "$@"
;;
*)
echo "Aliyun OpenAPI SDK: '$1' is unknown parameter" >&2
exit 2
;;
esac
fi