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.

142 lines
4.6 KiB

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