gcore_cdn.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env sh
  2. # Here is the script to deploy the cert to G-Core CDN servise (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=$(awk 1 ORS='\\n' "$_cfullchain")
  24. _key=$(awk 1 ORS='\\n' "$_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. else
  32. DEPLOY_GCORE_CDN_USERNAME="$Le_Deploy_gcore_cdn_username"
  33. fi
  34. else
  35. _savedomainconf Le_Deploy_gcore_cdn_username "$DEPLOY_GCORE_CDN_USERNAME"
  36. fi
  37. if [ -z "$DEPLOY_GCORE_CDN_PASSWORD" ]; then
  38. if [ -z "$Le_Deploy_gcore_cdn_password" ]; then
  39. _err "Please define the target password: export DEPLOY_GCORE_CDN_PASSWORD=password"
  40. return 1
  41. else
  42. DEPLOY_GCORE_CDN_PASSWORD="$Le_Deploy_gcore_cdn_password"
  43. fi
  44. else
  45. _savedomainconf Le_Deploy_gcore_cdn_password "$DEPLOY_GCORE_CDN_PASSWORD"
  46. fi
  47. if ! [ -x "$(command -v jq)" ]; then
  48. _err "Please install the package jq: sudo apt-get install jq"
  49. return 1
  50. fi
  51. _info "Get authorization token"
  52. _request="{ \"username\": \"$DEPLOY_GCORE_CDN_USERNAME\", \"password\": \"$DEPLOY_GCORE_CDN_PASSWORD\" }"
  53. _debug _request "$_request"
  54. _response=$(curl -s -X POST https://api.gcdn.co/auth/signin -H "Content-Type:application/json" -d "$_request")
  55. _debug _response "$_response"
  56. _token=$(echo "$_response" | jq -r '.token')
  57. _debug _token "$_token"
  58. if [ "$_token" == "null" ]; then
  59. _err "Error G-Core Labs API authorization"
  60. return 1
  61. fi
  62. _info "Find CDN resource with cname $_cdomain"
  63. _response=$(curl -s -X GET https://api.gcdn.co/resources -H "Authorization:Token $_token")
  64. _debug _response "$_response"
  65. _resource=$(echo "$_response" | jq -r ".[] | select(.cname == \"$_cdomain\")")
  66. _debug _resource "$_resource"
  67. _resourceId=$(echo "$_resource" | jq -r '.id')
  68. _sslDataOld=$(echo "$_resource" | jq -r '.sslData')
  69. _originGroup=$(echo "$_resource" | jq -r '.originGroup')
  70. _debug _resourceId "$_resourceId"
  71. _debug _sslDataOld "$_sslDataOld"
  72. _debug _originGroup "$_originGroup"
  73. if [ -z "$_resourceId" ] || [ "$_resourceId" == "null" ] || [ -z "$_originGroup" ] || [ "$_originGroup" == "null" ]; then
  74. _err "Not found CDN resource with cname $_cdomain"
  75. return 1
  76. fi
  77. _info "Add new SSL certificate"
  78. _date=$(date "+%d.%m.%Y %H:%M:%S")
  79. _request="{ \"name\": \"$_cdomain ($_date)\", \"sslCertificate\": \"$_fullchain\n\", \"sslPrivateKey\": \"$_key\n\" }"
  80. _debug _request "$_request"
  81. _response=$(curl -s -X POST https://api.gcdn.co/sslData -H "Content-Type:application/json" -H "Authorization:Token $_token" -d "$_request")
  82. _debug _response "$_response"
  83. _sslDataAdd=$(echo "$_response" | jq -r '.id')
  84. _debug _sslDataAdd "$_sslDataAdd"
  85. if [ "$_sslDataAdd" == "null" ]; then
  86. _err "Error new SSL certificate add"
  87. return 1
  88. fi
  89. _info "Update CDN resource"
  90. _request="{ \"originGroup\": $_originGroup, \"sslData\": $_sslDataAdd }"
  91. _debug _request "$_request"
  92. _response=$(curl -s -X PUT https://api.gcdn.co/resources/$_resourceId -H "Content-Type:application/json" -H "Authorization:Token $_token" -d "$_request")
  93. _debug _response "$_response"
  94. _sslDataNew=$(echo "$_response" | jq -r '.sslData')
  95. _debug _sslDataNew "$_sslDataNew"
  96. if [ "$_sslDataNew" != "$_sslDataAdd" ]; then
  97. _err "Error CDN resource update"
  98. return 1
  99. fi
  100. if [ -z "$_sslDataOld" ] || [ "$_sslDataOld" = "null" ]; then
  101. _info "Not found old SSL certificate"
  102. else
  103. _info "Delete old SSL certificate"
  104. _response=$(curl -s -X DELETE https://api.gcdn.co/sslData/$_sslDataOld -H "Authorization:Token $_token")
  105. _debug _response "$_response"
  106. fi
  107. _info "Certificate successfully deployed"
  108. return 0
  109. }