3 # Script to create certificate to qiniu.com
5 # This deployment required following variables
6 # export QINIU_AK="QINIUACCESSKEY"
7 # export QINIU_SK="QINIUSECRETKEY"
8 # export QINIU_CDN_DOMAIN="cdn.example.com"
10 QINIU_API_BASE
="https://api.qiniu.com"
19 _debug _cdomain
"$_cdomain"
21 _debug _ccert
"$_ccert"
23 _debug _cfullchain
"$_cfullchain"
25 if [ -z "$QINIU_AK" ]; then
26 _err
"QINIU_AK is not defined."
29 _savedomainconf QINIU_AK
"$QINIU_AK"
32 if [ -z "$QINIU_SK" ]; then
33 _err
"QINIU_SK is not defined."
36 _savedomainconf QINIU_SK
"$QINIU_SK"
39 if [ "$QINIU_CDN_DOMAIN" ]; then
40 _savedomainconf QINIU_CDN_DOMAIN
"$QINIU_CDN_DOMAIN"
42 QINIU_CDN_DOMAIN
="$_cdomain"
46 string_fullchain
=$
(sed 's/$/\\n/' "$_cfullchain" |
tr -d '\n')
47 string_key
=$
(sed 's/$/\\n/' "$_ckey" |
tr -d '\n')
49 sslcert_path
="/sslcert"
50 sslcerl_body
="{\"name\":\"$_cdomain\",\"common_name\":\"$QINIU_CDN_DOMAIN\",\"ca\":\"$string_fullchain\",\"pri\":\"$string_key\"}"
51 sslcert_access_token
="$(_make_access_token "$sslcert_path")"
52 _debug sslcert_access_token
"$sslcert_access_token"
53 export _H1
="Authorization: QBox $sslcert_access_token"
54 sslcert_response
=$
(_post
"$sslcerl_body" "$QINIU_API_BASE$sslcert_path" 0 "POST" "application/json" | _dbase64
"multiline")
56 if ! _contains
"$sslcert_response" "certID"; then
57 _err
"Error in creating certificate:"
58 _err
"$sslcert_response"
62 _debug sslcert_response
"$sslcert_response"
63 _info
"Certificate successfully uploaded, updating domain $_cdomain"
66 _certId
="$(printf "%s
" "$sslcert_response" | _normalizeJson | _egrep_o "certID
\": *\"[^
\"]*\"" | cut -d : -f 2)"
67 _debug certId
"$_certId"
69 ## update domain ssl config
70 update_path
="/domain/$QINIU_CDN_DOMAIN/httpsconf"
71 update_body
="{\"certid\":$_certId,\"forceHttps\":false}"
72 update_access_token
="$(_make_access_token "$update_path")"
73 _debug update_access_token
"$update_access_token"
74 export _H1
="Authorization: QBox $update_access_token"
75 update_response
=$
(_post
"$update_body" "$QINIU_API_BASE$update_path" 0 "PUT" "application/json" | _dbase64
"multiline")
77 if _contains
"$update_response" "error"; then
78 _err
"Error in updating domain httpsconf:"
79 _err
"$update_response"
83 _debug update_response
"$update_response"
84 _info
"Certificate successfully deployed"
89 _make_access_token
() {
90 _token
="$(printf "%s
\n" "$1" | _hmac "sha1
" "$
(printf "%s" "$QINIU_SK" | _hex_dump |
tr -d " ")" | _base64 | tr -- '+/' '-_')"
91 echo "$QINIU_AK:$_token"