3 # Here is the script to deploy the cert to G-Core CDN service (https://gcore.com/) using the G-Core Labs API (https://apidocs.gcore.com/cdn).
4 # Returns 0 when success.
6 # Written by temoffey <temofffey@gmail.com>
8 # Update by DreamOfIce <admin@dreamofice.cn> in 2023
10 #export DEPLOY_GCORE_CDN_USERNAME=myusername
11 #export DEPLOY_GCORE_CDN_PASSWORD=mypassword
13 ######## Public functions #####################
15 #domain keyfile certfile cafile fullchain
24 _debug _cdomain
"$_cdomain"
26 _debug _ccert
"$_ccert"
28 _debug _cfullchain
"$_cfullchain"
30 _fullchain
=$
(tr '\r\n' '*#' <"$_cfullchain" |
sed 's/*#/#/g;s/##/#/g;s/#/\\n/g')
31 _key
=$
(tr '\r\n' '*#' <"$_ckey" |
sed 's/*#/#/g;s/#/\\n/g')
33 _debug _fullchain
"$_fullchain"
36 if [ -z "$DEPLOY_GCORE_CDN_USERNAME" ]; then
37 if [ -z "$Le_Deploy_gcore_cdn_username" ]; then
38 _err
"Please define the target username: export DEPLOY_GCORE_CDN_USERNAME=username"
42 Le_Deploy_gcore_cdn_username
="$DEPLOY_GCORE_CDN_USERNAME"
43 _savedomainconf Le_Deploy_gcore_cdn_username
"$Le_Deploy_gcore_cdn_username"
46 if [ -z "$DEPLOY_GCORE_CDN_PASSWORD" ]; then
47 if [ -z "$Le_Deploy_gcore_cdn_password" ]; then
48 _err
"Please define the target password: export DEPLOY_GCORE_CDN_PASSWORD=password"
52 Le_Deploy_gcore_cdn_password
="$DEPLOY_GCORE_CDN_PASSWORD"
53 _savedomainconf Le_Deploy_gcore_cdn_password
"$Le_Deploy_gcore_cdn_password"
56 _info
"Get authorization token"
57 _request
="{\"username\":\"$Le_Deploy_gcore_cdn_username\",\"password\":\"$Le_Deploy_gcore_cdn_password\"}"
58 _debug _request
"$_request"
59 export _H1
="Content-Type:application/json"
60 _response
=$
(_post
"$_request" "https://api.gcore.com/auth/jwt/login")
61 _debug _response
"$_response"
62 _regex
=".*\"access\":\"\([-._0-9A-Za-z]*\)\".*$"
63 _debug _regex
"$_regex"
64 _token
=$
(echo "$_response" |
sed -n "s/$_regex/\1/p")
65 _debug _token
"$_token"
67 if [ -z "$_token" ]; then
68 _err
"Error G-Core Labs API authorization"
72 _info
"Find CDN resource with cname $_cdomain"
73 export _H2
="Authorization:Bearer $_token"
74 _response
=$
(_get
"https://api.gcore.com/cdn/resources")
75 _debug _response
"$_response"
76 _regex
="\"primary_resource\":null},"
77 _debug _regex
"$_regex"
78 _response
=$
(echo "$_response" |
sed "s/$_regex/$_regex\n/g")
79 _debug _response
"$_response"
80 _regex
="^.*\"cname\":\"$_cdomain\".*$"
81 _debug _regex
"$_regex"
82 _resource
=$
(echo "$_response" | _egrep_o
"$_regex")
83 _debug _resource
"$_resource"
84 _regex
=".*\"id\":\([0-9]*\).*$"
85 _debug _regex
"$_regex"
86 _resourceId
=$
(echo "$_resource" |
sed -n "s/$_regex/\1/p")
87 _debug _resourceId
"$_resourceId"
88 _regex
=".*\"sslData\":\([0-9]*\).*$"
89 _debug _regex
"$_regex"
90 _sslDataOld
=$
(echo "$_resource" |
sed -n "s/$_regex/\1/p")
91 _debug _sslDataOld
"$_sslDataOld"
92 _regex
=".*\"originGroup\":\([0-9]*\).*$"
93 _debug _regex
"$_regex"
94 _originGroup
=$
(echo "$_resource" |
sed -n "s/$_regex/\1/p")
95 _debug _originGroup
"$_originGroup"
97 if [ -z "$_resourceId" ] ||
[ -z "$_originGroup" ]; then
98 _err
"Not found CDN resource with cname $_cdomain"
102 _info
"Add new SSL certificate"
103 _date
=$
(date "+%d.%m.%Y %H:%M:%S")
104 _request
="{\"name\":\"$_cdomain ($_date)\",\"sslCertificate\":\"$_fullchain\",\"sslPrivateKey\":\"$_key\"}"
105 _debug _request
"$_request"
106 _response
=$
(_post
"$_request" "https://api.gcore.com/cdn/sslData")
107 _debug _response
"$_response"
108 _regex
=".*\"id\":\([0-9]*\).*$"
109 _debug _regex
"$_regex"
110 _sslDataAdd
=$
(echo "$_response" |
sed -n "s/$_regex/\1/p")
111 _debug _sslDataAdd
"$_sslDataAdd"
113 if [ -z "$_sslDataAdd" ]; then
114 _err
"Error new SSL certificate add"
118 _info
"Update CDN resource"
119 _request
="{\"originGroup\":$_originGroup,\"sslData\":$_sslDataAdd}"
120 _debug _request
"$_request"
121 _response
=$
(_post
"$_request" "https://api.gcore.com/cdn/resources/$_resourceId" '' "PUT")
122 _debug _response
"$_response"
123 _regex
=".*\"sslData\":\([0-9]*\).*$"
124 _debug _regex
"$_regex"
125 _sslDataNew
=$
(echo "$_response" |
sed -n "s/$_regex/\1/p")
126 _debug _sslDataNew
"$_sslDataNew"
128 if [ "$_sslDataNew" != "$_sslDataAdd" ]; then
129 _err
"Error CDN resource update"
133 if [ -z "$_sslDataOld" ] ||
[ "$_sslDataOld" = "null" ]; then
134 _info
"Not found old SSL certificate"
136 _info
"Delete old SSL certificate"
137 _response
=$
(_post
'' "https://api.gcore.com/cdn/sslData/$_sslDataOld" '' "DELETE")
138 _debug _response
"$_response"
141 _info
"Certificate successfully deployed"