You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env sh
  2. # 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).
  3. # Returns 0 when success.
  4. #
  5. # Written by temoffey <temofffey@gmail.com>
  6. # Public domain, 2019
  7. # Update by DreamOfIce <admin@dreamofice.cn> in 2023
  8. #export DEPLOY_GCORE_CDN_USERNAME=myusername
  9. #export DEPLOY_GCORE_CDN_PASSWORD=mypassword
  10. ######## Public functions #####################
  11. #domain keyfile certfile cafile fullchain
  12. gcore_cdn_deploy() {
  13. _cdomain="$1"
  14. _ckey="$2"
  15. _ccert="$3"
  16. _cca="$4"
  17. _cfullchain="$5"
  18. _debug _cdomain "$_cdomain"
  19. _debug _ckey "$_ckey"
  20. _debug _ccert "$_ccert"
  21. _debug _cca "$_cca"
  22. _debug _cfullchain "$_cfullchain"
  23. _fullchain=$(tr '\r\n' '*#' <"$_cfullchain" | sed 's/*#/#/g;s/##/#/g;s/#/\\n/g')
  24. _key=$(tr '\r\n' '*#' <"$_ckey" | sed 's/*#/#/g;s/#/\\n/g')
  25. _debug _fullchain "$_fullchain"
  26. _debug _key "$_key"
  27. if [ -z "$DEPLOY_GCORE_CDN_USERNAME" ]; then
  28. if [ -z "$Le_Deploy_gcore_cdn_username" ]; then
  29. _err "Please define the target username: export DEPLOY_GCORE_CDN_USERNAME=username"
  30. return 1
  31. fi
  32. else
  33. Le_Deploy_gcore_cdn_username="$DEPLOY_GCORE_CDN_USERNAME"
  34. _savedomainconf Le_Deploy_gcore_cdn_username "$Le_Deploy_gcore_cdn_username"
  35. fi
  36. if [ -z "$DEPLOY_GCORE_CDN_PASSWORD" ]; then
  37. if [ -z "$Le_Deploy_gcore_cdn_password" ]; then
  38. _err "Please define the target password: export DEPLOY_GCORE_CDN_PASSWORD=password"
  39. return 1
  40. fi
  41. else
  42. Le_Deploy_gcore_cdn_password="$DEPLOY_GCORE_CDN_PASSWORD"
  43. _savedomainconf Le_Deploy_gcore_cdn_password "$Le_Deploy_gcore_cdn_password"
  44. fi
  45. _info "Get authorization token"
  46. _request="{\"username\":\"$Le_Deploy_gcore_cdn_username\",\"password\":\"$Le_Deploy_gcore_cdn_password\"}"
  47. _debug _request "$_request"
  48. export _H1="Content-Type:application/json"
  49. _response=$(_post "$_request" "https://api.gcore.com/auth/jwt/login")
  50. _debug _response "$_response"
  51. _regex=".*\"access\":\"\([-._0-9A-Za-z]*\)\".*$"
  52. _debug _regex "$_regex"
  53. _token=$(echo "$_response" | sed -n "s/$_regex/\1/p")
  54. _debug _token "$_token"
  55. if [ -z "$_token" ]; then
  56. _err "Error G-Core Labs API authorization"
  57. return 1
  58. fi
  59. _info "Find CDN resource with cname $_cdomain"
  60. export _H2="Authorization:Bearer $_token"
  61. _response=$(_get "https://api.gcore.com/cdn/resources")
  62. _debug _response "$_response"
  63. _regex="\"primary_resource\":null},"
  64. _debug _regex "$_regex"
  65. _response=$(echo "$_response" | sed "s/$_regex/$_regex\n/g")
  66. _debug _response "$_response"
  67. _regex="^.*\"cname\":\"$_cdomain\".*$"
  68. _debug _regex "$_regex"
  69. _resource=$(echo "$_response" | _egrep_o "$_regex")
  70. _debug _resource "$_resource"
  71. _regex=".*\"id\":\([0-9]*\).*$"
  72. _debug _regex "$_regex"
  73. _resourceId=$(echo "$_resource" | sed -n "s/$_regex/\1/p")
  74. _debug _resourceId "$_resourceId"
  75. _regex=".*\"sslData\":\([0-9]*\).*$"
  76. _debug _regex "$_regex"
  77. _sslDataOld=$(echo "$_resource" | sed -n "s/$_regex/\1/p")
  78. _debug _sslDataOld "$_sslDataOld"
  79. _regex=".*\"originGroup\":\([0-9]*\).*$"
  80. _debug _regex "$_regex"
  81. _originGroup=$(echo "$_resource" | sed -n "s/$_regex/\1/p")
  82. _debug _originGroup "$_originGroup"
  83. if [ -z "$_resourceId" ] || [ -z "$_originGroup" ]; then
  84. _err "Not found CDN resource with cname $_cdomain"
  85. return 1
  86. fi
  87. _info "Add new SSL certificate"
  88. _date=$(date "+%d.%m.%Y %H:%M:%S")
  89. _request="{\"name\":\"$_cdomain ($_date)\",\"sslCertificate\":\"$_fullchain\",\"sslPrivateKey\":\"$_key\"}"
  90. _debug _request "$_request"
  91. _response=$(_post "$_request" "https://api.gcore.com/cdn/sslData")
  92. _debug _response "$_response"
  93. _regex=".*\"id\":\([0-9]*\).*$"
  94. _debug _regex "$_regex"
  95. _sslDataAdd=$(echo "$_response" | sed -n "s/$_regex/\1/p")
  96. _debug _sslDataAdd "$_sslDataAdd"
  97. if [ -z "$_sslDataAdd" ]; then
  98. _err "Error new SSL certificate add"
  99. return 1
  100. fi
  101. _info "Update CDN resource"
  102. _request="{\"originGroup\":$_originGroup,\"sslData\":$_sslDataAdd}"
  103. _debug _request "$_request"
  104. _response=$(_post "$_request" "https://api.gcore.com/cdn/resources/$_resourceId" '' "PUT")
  105. _debug _response "$_response"
  106. _regex=".*\"sslData\":\([0-9]*\).*$"
  107. _debug _regex "$_regex"
  108. _sslDataNew=$(echo "$_response" | sed -n "s/$_regex/\1/p")
  109. _debug _sslDataNew "$_sslDataNew"
  110. if [ "$_sslDataNew" != "$_sslDataAdd" ]; then
  111. _err "Error CDN resource update"
  112. return 1
  113. fi
  114. if [ -z "$_sslDataOld" ] || [ "$_sslDataOld" = "null" ]; then
  115. _info "Not found old SSL certificate"
  116. else
  117. _info "Delete old SSL certificate"
  118. _response=$(_post '' "https://api.gcore.com/cdn/sslData/$_sslDataOld" '' "DELETE")
  119. _debug _response "$_response"
  120. fi
  121. _info "Certificate successfully deployed"
  122. return 0
  123. }