2019-01-11 10:17:38 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Script to create certificate to qiniu.com
|
|
|
|
#
|
|
|
|
# This deployment required following variables
|
|
|
|
# export QINIU_AK="QINIUACCESSKEY"
|
|
|
|
# export QINIU_SK="QINIUSECRETKEY"
|
2019-01-13 04:23:15 +00:00
|
|
|
# export QINIU_CDN_DOMAIN="cdn.example.com"
|
2019-01-11 10:17:38 +00:00
|
|
|
|
|
|
|
QINIU_API_BASE="https://api.qiniu.com"
|
|
|
|
|
|
|
|
qiniu_deploy() {
|
|
|
|
_cdomain="$1"
|
|
|
|
_ckey="$2"
|
|
|
|
_ccert="$3"
|
|
|
|
_cca="$4"
|
|
|
|
_cfullchain="$5"
|
|
|
|
|
|
|
|
_debug _cdomain "$_cdomain"
|
|
|
|
_debug _ckey "$_ckey"
|
|
|
|
_debug _ccert "$_ccert"
|
|
|
|
_debug _cca "$_cca"
|
|
|
|
_debug _cfullchain "$_cfullchain"
|
|
|
|
|
|
|
|
if [ -z "$QINIU_AK" ]; then
|
2019-01-19 15:58:55 +00:00
|
|
|
_err "QINIU_AK is not defined."
|
|
|
|
return 1
|
2019-01-11 10:17:38 +00:00
|
|
|
else
|
2019-01-19 15:58:55 +00:00
|
|
|
_savedomainconf QINIU_AK "$QINIU_AK"
|
2019-01-11 10:17:38 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$QINIU_SK" ]; then
|
2019-01-19 15:58:55 +00:00
|
|
|
_err "QINIU_SK is not defined."
|
|
|
|
return 1
|
2019-01-11 10:17:38 +00:00
|
|
|
else
|
2019-01-19 15:58:55 +00:00
|
|
|
_savedomainconf QINIU_SK "$QINIU_SK"
|
2019-01-11 10:17:38 +00:00
|
|
|
fi
|
|
|
|
|
2019-01-20 15:58:10 +00:00
|
|
|
if [ "$QINIU_CDN_DOMAIN" ]; then
|
2019-01-21 06:33:15 +00:00
|
|
|
_savedomainconf QINIU_CDN_DOMAIN "$QINIU_CDN_DOMAIN"
|
2019-01-20 15:58:10 +00:00
|
|
|
else
|
2019-01-21 06:33:15 +00:00
|
|
|
QINIU_CDN_DOMAIN="$_cdomain"
|
2019-01-14 14:19:00 +00:00
|
|
|
fi
|
|
|
|
|
2019-01-12 07:54:42 +00:00
|
|
|
## upload certificate
|
2019-01-12 13:07:22 +00:00
|
|
|
string_fullchain=$(sed 's/$/\\n/' "$_cfullchain" | tr -d '\n')
|
|
|
|
string_key=$(sed 's/$/\\n/' "$_ckey" | tr -d '\n')
|
2019-01-11 10:17:38 +00:00
|
|
|
|
2019-01-12 07:54:42 +00:00
|
|
|
sslcert_path="/sslcert"
|
2019-01-19 15:58:55 +00:00
|
|
|
sslcerl_body="{\"name\":\"$_cdomain\",\"common_name\":\"$QINIU_CDN_DOMAIN\",\"ca\":\"$string_fullchain\",\"pri\":\"$string_key\"}"
|
2019-01-14 14:19:00 +00:00
|
|
|
sslcert_access_token="$(_make_access_token "$sslcert_path")"
|
2019-01-11 11:19:07 +00:00
|
|
|
_debug sslcert_access_token "$sslcert_access_token"
|
|
|
|
export _H1="Authorization: QBox $sslcert_access_token"
|
2019-01-12 07:54:42 +00:00
|
|
|
sslcert_response=$(_post "$sslcerl_body" "$QINIU_API_BASE$sslcert_path" 0 "POST" "application/json" | _dbase64 "multiline")
|
2019-01-11 10:17:38 +00:00
|
|
|
|
2019-01-12 15:11:19 +00:00
|
|
|
if ! _contains "$sslcert_response" "certID"; then
|
2019-01-11 11:19:07 +00:00
|
|
|
_err "Error in creating certificate:"
|
|
|
|
_err "$sslcert_response"
|
2019-01-11 10:17:38 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2019-01-11 11:19:07 +00:00
|
|
|
_debug sslcert_response "$sslcert_response"
|
|
|
|
_info "Certificate successfully uploaded, updating domain $_cdomain"
|
|
|
|
|
2019-01-12 07:54:42 +00:00
|
|
|
## extract certId
|
2019-01-12 15:11:19 +00:00
|
|
|
_certId="$(printf "%s" "$sslcert_response" | _normalizeJson | _egrep_o "certID\": *\"[^\"]*\"" | cut -d : -f 2)"
|
2019-01-11 11:19:07 +00:00
|
|
|
_debug certId "$_certId"
|
|
|
|
|
2019-01-12 07:54:42 +00:00
|
|
|
## update domain ssl config
|
2019-01-19 15:58:55 +00:00
|
|
|
update_path="/domain/$QINIU_CDN_DOMAIN/httpsconf"
|
2019-01-14 14:19:00 +00:00
|
|
|
update_body="{\"certid\":$_certId,\"forceHttps\":false}"
|
|
|
|
update_access_token="$(_make_access_token "$update_path")"
|
2019-01-11 11:19:07 +00:00
|
|
|
_debug update_access_token "$update_access_token"
|
|
|
|
export _H1="Authorization: QBox $update_access_token"
|
2019-01-12 11:58:57 +00:00
|
|
|
update_response=$(_post "$update_body" "$QINIU_API_BASE$update_path" 0 "PUT" "application/json" | _dbase64 "multiline")
|
2019-01-11 11:19:07 +00:00
|
|
|
|
2019-01-12 15:11:19 +00:00
|
|
|
if _contains "$update_response" "error"; then
|
2019-01-12 07:54:42 +00:00
|
|
|
_err "Error in updating domain httpsconf:"
|
2019-01-11 11:19:07 +00:00
|
|
|
_err "$update_response"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_debug update_response "$update_response"
|
2019-01-11 10:17:38 +00:00
|
|
|
_info "Certificate successfully deployed"
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-01-14 14:19:00 +00:00
|
|
|
_make_access_token() {
|
2019-02-28 15:43:58 +00:00
|
|
|
_token="$(printf "%s\n" "$1" | _hmac "sha1" "$(printf "%s" "$QINIU_SK" | _hex_dump | tr -d " ")" | _base64 | tr -- '+/' '-_')"
|
2019-01-19 15:58:55 +00:00
|
|
|
echo "$QINIU_AK:$_token"
|
2019-01-11 10:17:38 +00:00
|
|
|
}
|