]> git.proxmox.com Git - mirror_acme.sh.git/blob - deploy/qiniu.sh
Merge branch 'dev' into feature/dns-openprovider
[mirror_acme.sh.git] / deploy / qiniu.sh
1 #!/usr/bin/env sh
2
3 # Script to create certificate to qiniu.com
4 #
5 # This deployment required following variables
6 # export QINIU_AK="QINIUACCESSKEY"
7 # export QINIU_SK="QINIUSECRETKEY"
8 # export QINIU_CDN_DOMAIN="cdn.example.com"
9
10 QINIU_API_BASE="https://api.qiniu.com"
11
12 qiniu_deploy() {
13 _cdomain="$1"
14 _ckey="$2"
15 _ccert="$3"
16 _cca="$4"
17 _cfullchain="$5"
18
19 _debug _cdomain "$_cdomain"
20 _debug _ckey "$_ckey"
21 _debug _ccert "$_ccert"
22 _debug _cca "$_cca"
23 _debug _cfullchain "$_cfullchain"
24
25 if [ -z "$QINIU_AK" ]; then
26 _err "QINIU_AK is not defined."
27 return 1
28 else
29 _savedomainconf QINIU_AK "$QINIU_AK"
30 fi
31
32 if [ -z "$QINIU_SK" ]; then
33 _err "QINIU_SK is not defined."
34 return 1
35 else
36 _savedomainconf QINIU_SK "$QINIU_SK"
37 fi
38
39 if [ "$QINIU_CDN_DOMAIN" ]; then
40 _savedomainconf QINIU_CDN_DOMAIN "$QINIU_CDN_DOMAIN"
41 else
42 QINIU_CDN_DOMAIN="$_cdomain"
43 fi
44
45 ## upload certificate
46 string_fullchain=$(sed 's/$/\\n/' "$_cfullchain" | tr -d '\n')
47 string_key=$(sed 's/$/\\n/' "$_ckey" | tr -d '\n')
48
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")
55
56 if ! _contains "$sslcert_response" "certID"; then
57 _err "Error in creating certificate:"
58 _err "$sslcert_response"
59 return 1
60 fi
61
62 _debug sslcert_response "$sslcert_response"
63 _info "Certificate successfully uploaded, updating domain $_cdomain"
64
65 ## extract certId
66 _certId="$(printf "%s" "$sslcert_response" | _normalizeJson | _egrep_o "certID\": *\"[^\"]*\"" | cut -d : -f 2)"
67 _debug certId "$_certId"
68
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")
76
77 if _contains "$update_response" "error"; then
78 _err "Error in updating domain httpsconf:"
79 _err "$update_response"
80 return 1
81 fi
82
83 _debug update_response "$update_response"
84 _info "Certificate successfully deployed"
85
86 return 0
87 }
88
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"
92 }