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.

139 lines
4.5 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/signin")
  49. _debug _response "$_response"
  50. _regex=".*\"token\":\"\([-._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=".*(\"id\".*?\"cname\":\"$_cdomain\".*?})"
  63. _regex="^.*\"cname\":\"$_cdomain\".*$"
  64. _debug _regex "$_regex"
  65. _resource=$(echo "$_response" | sed 's/},{/},\n{/g' | _egrep_o "$_regex")
  66. _debug _resource "$_resource"
  67. _regex=".*\"id\":\([0-9]*\).*\"rules\".*$"
  68. _debug _regex "$_regex"
  69. _resourceId=$(echo "$_resource" | sed -n "s/$_regex/\1/p")
  70. _debug _resourceId "$_resourceId"
  71. _regex=".*\"sslData\":\([0-9]*\).*$"
  72. _debug _regex "$_regex"
  73. _sslDataOld=$(echo "$_resource" | sed -n "s/$_regex/\1/p")
  74. _debug _sslDataOld "$_sslDataOld"
  75. _regex=".*\"originGroup\":\([0-9]*\).*$"
  76. _debug _regex "$_regex"
  77. _originGroup=$(echo "$_resource" | sed -n "s/$_regex/\1/p")
  78. _debug _originGroup "$_originGroup"
  79. if [ -z "$_resourceId" ] || [ -z "$_originGroup" ]; then
  80. _err "Not found CDN resource with cname $_cdomain"
  81. return 1
  82. fi
  83. _info "Add new SSL certificate"
  84. _date=$(date "+%d.%m.%Y %H:%M:%S")
  85. _request="{\"name\":\"$_cdomain ($_date)\",\"sslCertificate\":\"$_fullchain\",\"sslPrivateKey\":\"$_key\"}"
  86. _debug _request "$_request"
  87. _response=$(_post "$_request" "https://api.gcdn.co/sslData")
  88. _debug _response "$_response"
  89. _regex=".*\"id\":\([0-9]*\).*$"
  90. _debug _regex "$_regex"
  91. _sslDataAdd=$(echo "$_response" | sed -n "s/$_regex/\1/p")
  92. _debug _sslDataAdd "$_sslDataAdd"
  93. if [ -z "$_sslDataAdd" ]; then
  94. _err "Error new SSL certificate add"
  95. return 1
  96. fi
  97. _info "Update CDN resource"
  98. _request="{\"originGroup\":$_originGroup,\"sslData\":$_sslDataAdd}"
  99. _debug _request "$_request"
  100. _response=$(_post "$_request" "https://api.gcdn.co/resources/$_resourceId" '' "PUT")
  101. _debug _response "$_response"
  102. _regex=".*\"sslData\":\([0-9]*\).*$"
  103. _debug _regex "$_regex"
  104. _sslDataNew=$(echo "$_response" | sed -n "s/$_regex/\1/p")
  105. _debug _sslDataNew "$_sslDataNew"
  106. if [ "$_sslDataNew" != "$_sslDataAdd" ]; then
  107. _err "Error CDN resource update"
  108. return 1
  109. fi
  110. if [ -z "$_sslDataOld" ] || [ "$_sslDataOld" = "null" ]; then
  111. _info "Not found old SSL certificate"
  112. else
  113. _info "Delete old SSL certificate"
  114. _response=$(_post '' "https://api.gcdn.co/sslData/$_sslDataOld" '' "DELETE")
  115. _debug _response "$_response"
  116. fi
  117. _info "Certificate successfully deployed"
  118. return 0
  119. }