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.

109 lines
3.5 KiB

  1. #!/usr/bin/env sh
  2. # Deploy certificates to rancher environmentsx
  3. # here are the defaults, overridable via env vars
  4. #
  5. #export RANCHER_CONFIG=${HOME}/.rancher/cli.json
  6. #export RANCHER_ENV=
  7. # usage:
  8. # - download rancher-cli from your rancher server and use it to create cli.json
  9. # the format of the file is quite simple, so you can just create your own
  10. # ! also run chmod 600 ~/.rancher/cli.json, since rancher-cli doesn't
  11. # - for multiple servers override RANCHER_CONFIG
  12. # - for multiple environments on a server set RANCHER_ENV appropriately
  13. # otherwise the one selected within cli.json is used
  14. # example
  15. # acme.sh --deploy -d my.website.com --deploy-hook rancher --debug
  16. # RANCHER_ENV=1a6 acme.sh --deploy -d my.website.com --deploy-hook rancher --debug
  17. ######## Public functions #####################
  18. #domain keyfile certfile cafile fullchain
  19. rancher_deploy() {
  20. _cdomain="$1"
  21. _ckey="$2"
  22. _ccert="$3"
  23. _cca="$4"
  24. _cfullchain="$5"
  25. _debug _cdomain "$_cdomain"
  26. _debug _ckey "$_ckey"
  27. _debug _ccert "$_ccert"
  28. _debug _cca "$_cca"
  29. _debug _cfullchain "$_cfullchain"
  30. if ! _exists jq; then
  31. _err "The command jq is not found."
  32. return 1
  33. fi
  34. _defaultRancherConfig=${HOME}/.rancher/cli.json
  35. _rancherConfig=${RANCHER_CONFIG:-${_defaultRancherConfig}}
  36. _info "Using rancher configuration $_rancherConfig"
  37. if [ ! -r "${_rancherConfig}" ] ; then
  38. _err "cannot read rancher configuration"
  39. return 1
  40. fi
  41. eval $(jq --monochrome-output < "${_rancherConfig}" \
  42. '@sh "_rancherUrl=\(.url)","_accessKey=\(.accessKey)","_secretKey=\(.secretKey)","_envId=\(.environment)"' | xargs)
  43. _debug _rancherUrl "$_rancherUrl"
  44. _debug _accessKey "$_accessKey"
  45. _secure_debug _secretKey "$_secretKey"
  46. _debug _envId "$_envId"
  47. if [ -n "${RANCHER_ENV}" ] ; then
  48. _envId="${RANCHER_ENV}"
  49. fi
  50. # when set by rancher-cli rancerUrl has an unwanted trailing "/schemas"
  51. _rancherUrl=${_rancherUrl%/schemas}
  52. _info "Deploying certificate $_cdomain into rancher environment $_envId at $_rancherUrl"
  53. _do_rancher_deploy_cert
  54. _success=$?
  55. if (( ! $_success )) ; then
  56. _info "Certificate successfully deployed"
  57. return 0
  58. else
  59. _err "Deployment failed: $_curlResult"
  60. return 1
  61. fi
  62. }
  63. function _do_rancher_deploy_cert () {
  64. _cert=$(<"$_ccert")
  65. _chain=$(<"$_cca")
  66. _privkey=$(<"$_ckey")
  67. _curlUrl="$_rancherUrl/projects/$_envId/certificates"
  68. _curlMethod="POST"
  69. _curlAuth="$_accessKey:$_secretKey"
  70. _certJson=$(jq --null-input --compact-output \
  71. --arg cert "$_cert" \
  72. --arg chain "$_chain" \
  73. --arg privkey "$_privkey" \
  74. --arg name "$_cdomain" \
  75. '{type:"certificate",cert:$cert,certChain:$chain,key:$privkey,name:$name}')
  76. _debug _curlUrl "$_curlUrl"
  77. _debug _curlMethod "$_curlMethod"
  78. _secure_debug _curlAuth "$_curlAuth"
  79. _secure_debug _certJson "$_certJson"
  80. _curlResult=$(curl -s \
  81. -u "${_curlAuth}" \
  82. -X "${_curlMethod}" \
  83. -H 'Content-Type: application/json' \
  84. -H 'Accept: application/json' \
  85. -d "${_certJson}" \
  86. "${_curlUrl}" |
  87. jq -r 'if (.type == "error") then "error: status="+(.status|tostring)+", code="+(.code|tostring)+", detail="+(.detail|tostring) else "success" end')
  88. _debug _curlResult "$_curlResult"
  89. [ "$_curlResult" == "success" ] && return 0 || return 1
  90. }