11#! /usr/bin/env sh
2+ # shellcheck disable=SC2034,SC2154
23
34# Script to create certificate to Alibaba Cloud CDN
45#
6+ # Docs: https://github.com/acmesh-official/acme.sh/wiki/deployhooks#33-deploy-your-certificate-to-cdn-or-dcdn-of-alibaba-cloud-aliyun
7+ #
58# This deployment required following variables
69# export Ali_Key="ALIACCESSKEY"
710# export Ali_Secret="ALISECRETKEY"
11+ # The credentials are shared with all the Alibaba Cloud deploy hooks and dnsapi
12+ #
13+ # To specify the CDN domain that is different from the certificate CN, usually used for multi-domain or wildcard certificates
814# export DEPLOY_ALI_CDN_DOMAIN="cdn.example.com"
9- # If you have more than one domain , just
15+ # If you have multiple CDN domains using the same certificate , just
1016# export DEPLOY_ALI_CDN_DOMAIN="cdn1.example.com cdn2.example.com"
1117#
12- # The credentials are shared with all domains, also shared with dns_ali api
18+ # For DCDN, see ali_dcdn deploy hook
1319
14- Ali_API =" https://cdn.aliyuncs.com/"
20+ Ali_CDN_API =" https://cdn.aliyuncs.com/"
1521
1622ali_cdn_deploy () {
1723 _cdomain=" $1 "
@@ -26,18 +32,16 @@ ali_cdn_deploy() {
2632 _debug _cca " $_cca "
2733 _debug _cfullchain " $_cfullchain "
2834
29- Ali_Key= " ${Ali_Key :- $(_readaccountconf_mutable Ali_Key)} "
30- Ali_Secret= " ${Ali_Secret :- $(_readaccountconf_mutable Ali_Secret)} "
31- if [ -z " $Ali_Key " ] || [ -z " $Ali_Secret " ] ; then
32- Ali_Key= " "
33- Ali_Secret= " "
34- _err " You don't specify aliyun api key and secret yet ."
35+ # Load dnsapi/dns_ali.sh to reduce the duplicated codes
36+ # https://github.com/acmesh-official/acme.sh/pull/5205#issuecomment-2357867276
37+ dnsapi_ali= " $( _findHook " $_cdomain " " $_SUB_FOLDER_DNSAPI " dns_ali ) "
38+ # shellcheck source=/dev/null
39+ if ! . " $dnsapi_ali " ; then
40+ _err " Error loading file $dnsapi_ali . Please check your API file and try again ."
3541 return 1
3642 fi
3743
38- # save the api key and secret to the account conf file.
39- _saveaccountconf_mutable Ali_Key " $Ali_Key "
40- _saveaccountconf_mutable Ali_Secret " $Ali_Secret "
44+ _prepare_ali_credentials || return 1
4145
4246 _getdeployconf DEPLOY_ALI_CDN_DOMAIN
4347 if [ " $DEPLOY_ALI_CDN_DOMAIN " ]; then
@@ -47,8 +51,8 @@ ali_cdn_deploy() {
4751 fi
4852
4953 # read cert and key files and urlencode both
50- _cert=$( _url_encode_upper < " $_cfullchain " )
51- _key=$( _url_encode_upper < " $_ckey " )
54+ _cert=$( _url_encode upper-hex < " $_cfullchain " )
55+ _key=$( _url_encode upper-hex < " $_ckey " )
5256
5357 _debug2 _cert " $_cert "
5458 _debug2 _key " $_key "
@@ -64,82 +68,9 @@ ali_cdn_deploy() {
6468 return 0
6569}
6670
67- # ################### Private functions below ##################################
68-
69- # act ign mtd
70- _ali_rest () {
71- act=" $1 "
72- ign=" $2 "
73- mtd=" $3 "
74-
75- signature=$( printf " %s" " $mtd &%2F&$( _ali_urlencode " $query " ) " | _hmac " sha1" " $( printf " %s" " $Ali_Secret &" | _hex_dump | tr -d " " ) " | _base64)
76- signature=$( _ali_urlencode " $signature " )
77- url=" $Ali_API ?$query &Signature=$signature "
78-
79- if [ " $mtd " = " GET" ]; then
80- response=" $( _get " $url " ) "
81- else
82- # post payload is not supported yet because of signature
83- response=" $( _post " " " $url " ) "
84- fi
85-
86- _ret=" $? "
87- _debug2 response " $response "
88- if [ " $_ret " != " 0" ]; then
89- _err " Error <$act >"
90- return 1
91- fi
92-
93- if [ -z " $ign " ]; then
94- message=" $( echo " $response " | _egrep_o " \" Message\" :\" [^\" ]*\" " | cut -d : -f 2 | tr -d \" ) "
95- if [ " $message " ]; then
96- _err " $message "
97- return 1
98- fi
99- fi
100- }
101-
102- _ali_urlencode () {
103- _str=" $1 "
104- _str_len=${# _str}
105- _u_i=1
106- while [ " $_u_i " -le " $_str_len " ]; do
107- _str_c=" $( printf " %s" " $_str " | cut -c " $_u_i " ) "
108- case $_str_c in [a-zA-Z0-9.~_-])
109- printf " %s" " $_str_c "
110- ;;
111- * )
112- printf " %%%02X" " '$_str_c "
113- ;;
114- esac
115- _u_i=" $( _math " $_u_i " + 1) "
116- done
117- }
118-
119- _ali_nonce () {
120- # _head_n 1 </dev/urandom | _digest "sha256" hex | cut -c 1-31
121- # Not so good...
122- date +" %s%N" | sed ' s/%N//g'
123- }
124-
125- _timestamp () {
126- date -u +" %Y-%m-%dT%H%%3A%M%%3A%SZ"
127- }
128-
129- # stdin stdout
130- _url_encode_upper () {
131- encoded=$( _url_encode)
132-
133- for match in $( echo " $encoded " | _egrep_o ' %..' | sort -u) ; do
134- upper=$( echo " $match " | _upper_case)
135- encoded=$( echo " $encoded " | sed " s/$match /$upper /g" )
136- done
137-
138- echo " $encoded "
139- }
140-
14171# domain pub pri
14272_set_cdn_domain_ssl_certificate_query () {
73+ endpoint=$Ali_CDN_API
14374 query=' '
14475 query=$query ' AccessKeyId=' $Ali_Key
14576 query=$query ' &Action=SetCdnDomainSSLCertificate'
0 commit comments