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
5.1 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
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. # Uses command line curl for send requests and jq for parse responses.
  4. # Returns 0 when success.
  5. #
  6. # Written by temoffey <temofffey@gmail.com>
  7. # Public domain, 2019
  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=$(while read -r line; do printf "%s" "$line\n"; done <"$_cfullchain")
  24. _key=$(while read -r line; do printf "%s" "$line\n"; done <"$_ckey")
  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. if ! [ -x "$(command -v jq)" ]; then
  46. _err "Please install the package jq: sudo apt-get install jq"
  47. return 1
  48. fi
  49. _info "Get authorization token"
  50. _request="{ \"username\": \"$Le_Deploy_gcore_cdn_username\", \"password\": \"$Le_Deploy_gcore_cdn_password\" }"
  51. _debug _request "$_request"
  52. _response=$(_H1="Content-Type:application/json" && _post "$_request" "https://api.gcdn.co/auth/signin")
  53. _debug _response "$_response"
  54. _regex="\"token\":\"([^\"]+)\""
  55. _debug _regex "$_regex"
  56. _token=$(if [[ $_response =~ $_regex ]]; then printf "%s" "${BASH_REMATCH[1]}"; fi)
  57. _debug _token "$_token"
  58. if [ -z "$_token" ]; then
  59. _err "Error G-Core Labs API authorization"
  60. return 1
  61. fi
  62. _info "Find CDN resource with cname $_cdomain"
  63. _response=$(_H1="Content-Type:application/json" && _H2="Authorization:Token $_token" && _get "https://api.gcdn.co/resources")
  64. _debug _response "$_response"
  65. _regex=".*(\"id\".*?\"cname\":\"$_cdomain\".*?})"
  66. _debug _regex "$_regex"
  67. _resource=$(if [[ $_response =~ $_regex ]]; then printf "%s" "${BASH_REMATCH[1]}"; fi)
  68. _debug _resource "$_resource"
  69. _regex="\"id\":([0-9]+)"
  70. _debug _regex "$_regex"
  71. _resourceId=$(if [[ $_resource =~ $_regex ]]; then printf "%s" "${BASH_REMATCH[1]}"; fi)
  72. _debug _resourceId "$_resourceId"
  73. _regex="\"sslData\":([0-9]+|null)"
  74. _debug _regex "$_regex"
  75. _sslDataOld=$(if [[ $_resource =~ $_regex ]]; then printf "%s" "${BASH_REMATCH[1]}"; fi)
  76. _debug _sslDataOld "$_sslDataOld"
  77. _regex="\"originGroup\":([0-9]+)"
  78. _debug _regex "$_regex"
  79. _originGroup=$(if [[ $_resource =~ $_regex ]]; then printf "%s" "${BASH_REMATCH[1]}"; fi)
  80. _debug _originGroup "$_originGroup"
  81. if [ -z "$_resourceId" ] || [ -z "$_originGroup" ]; then
  82. _err "Not found CDN resource with cname $_cdomain"
  83. return 1
  84. fi
  85. _info "Add new SSL certificate"
  86. _date=$(date "+%d.%m.%Y %H:%M:%S")
  87. _request="{ \"name\": \"$_cdomain ($_date)\", \"sslCertificate\": \"$_fullchain\", \"sslPrivateKey\": \"$_key\" }"
  88. _debug _request "$_request"
  89. _response=$(_H1="Content-Type:application/json" && _H2="Authorization:Token $_token" && _post "$_request" "https://api.gcdn.co/sslData")
  90. _debug _response "$_response"
  91. _regex="\"id\":([0-9]+)"
  92. _debug _regex "$_regex"
  93. _sslDataAdd=$(if [[ $_response =~ $_regex ]]; then printf "%s" "${BASH_REMATCH[1]}"; fi)
  94. _debug _sslDataAdd "$_sslDataAdd"
  95. if [ -z "$_sslDataAdd" ]; then
  96. _err "Error new SSL certificate add"
  97. return 1
  98. fi
  99. _info "Update CDN resource"
  100. _request="{ \"originGroup\": $_originGroup, \"sslData\": $_sslDataAdd }"
  101. _debug _request "$_request"
  102. _response=$(_H1="Content-Type:application/json" && _H2="Authorization:Token $_token" && _post "$_request" "https://api.gcdn.co/resources/$_resourceId" '' "PUT")
  103. _debug _response "$_response"
  104. _regex="\"sslData\":([0-9]+)"
  105. _debug _regex "$_regex"
  106. _sslDataNew=$(if [[ $_response =~ $_regex ]]; then printf "%s" "${BASH_REMATCH[1]}"; fi)
  107. _debug _sslDataNew "$_sslDataNew"
  108. if [ "$_sslDataNew" != "$_sslDataAdd" ]; then
  109. _err "Error CDN resource update"
  110. return 1
  111. fi
  112. if [ -z "$_sslDataOld" ] || [ "$_sslDataOld" = "null" ]; then
  113. _info "Not found old SSL certificate"
  114. else
  115. _info "Delete old SSL certificate"
  116. _response=$(_H1="Content-Type:application/json" && _H2="Authorization:Token $_token" && _post '' "https://api.gcdn.co/sslData/$_sslDataOld" '' "DELETE")
  117. _debug _response "$_response"
  118. fi
  119. _info "Certificate successfully deployed"
  120. return 0
  121. }