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.

84 lines
3.2 KiB

8 years ago
  1. #!/usr/bin/env sh
  2. # This deploy hook will deploy ssl cert on kong proxy engine based on api request_host parameter.
  3. # Note that ssl plugin should be available on Kong instance
  4. # The hook will match cdomain to request_host, in case of multiple domain it will always take the first
  5. # one (acme.sh behaviour).
  6. # If ssl config already exist it will update only cert and key not touching other parameter
  7. # If ssl config doesn't exist it will only upload cert and key and not set other parameter
  8. # Not that we deploy full chain
  9. # See https://getkong.org/plugins/dynamic-ssl/ for other options
  10. # Written by Geoffroi Genot <ggenot@voxbone.com>
  11. ######## Public functions #####################
  12. #domain keyfile certfile cafile fullchain
  13. kong_deploy() {
  14. _cdomain="$1"
  15. _ckey="$2"
  16. _ccert="$3"
  17. _cca="$4"
  18. _cfullchain="$5"
  19. _info "Deploying certificate on Kong instance"
  20. if [ -z "$KONG_URL" ]; then
  21. _debug "KONG_URL Not set, using default http://localhost:8001"
  22. KONG_URL="http://localhost:8001"
  23. fi
  24. _debug _cdomain "$_cdomain"
  25. _debug _ckey "$_ckey"
  26. _debug _ccert "$_ccert"
  27. _debug _cca "$_cca"
  28. _debug _cfullchain "$_cfullchain"
  29. #Get ssl_uuid linked to the domain
  30. ssl_uuid=$(_get "$KONG_URL/certificates/$_cdomain" | _normalizeJson | _egrep_o '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
  31. if [ -z "$ssl_uuid" ]; then
  32. _debug "Unable to get Kong ssl_uuid for domain $_cdomain"
  33. _debug "Make sure that KONG_URL is correctly configured"
  34. _debug "Make sure that a Kong certificate match the sni"
  35. _debug "Kong url: $KONG_URL"
  36. _info "No existing certificate, creating..."
  37. #return 1
  38. fi
  39. #Save kong url if it's succesful (First run case)
  40. _saveaccountconf KONG_URL "$KONG_URL"
  41. #Generate DEIM
  42. delim="-----MultipartDelimiter$(date "+%s%N")"
  43. nl="\015\012"
  44. #Set Header
  45. _H1="Content-Type: multipart/form-data; boundary=$delim"
  46. #Generate data for request (Multipart/form-data with mixed content)
  47. content="--$delim${nl}"
  48. if [ -z "$ssl_uuid" ]; then
  49. #set sni to domain
  50. content="$content{nl}Content-Disposition: form-data; name=\"snis\"${nl}${nl}$_cdomain"
  51. fi
  52. #add key
  53. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"; filename=\"$(basename "$_ckey")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")"
  54. #Add cert
  55. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"cert\"; filename=\"$(basename "$_cfullchain")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_cfullchain")"
  56. #Close multipart
  57. content="$content${nl}--$delim--${nl}"
  58. #Convert CRLF
  59. content=$(printf %b "$content")
  60. #DEBUG
  61. _debug header "$_H1"
  62. _debug content "$content"
  63. #Check if sslcreated (if not => POST else => PATCH)
  64. if [ -z "$ssl_uuid" ]; then
  65. #Post certificate to Kong
  66. response=$(_post "$content" "$KONG_URL/certificates" "" "POST")
  67. else
  68. #patch
  69. response=$(_post "$content" "$KONG_URL/certificates/$ssl_uuid" "" "PATCH")
  70. fi
  71. if ! [ "$(echo "$response" | _egrep_o "created_at")" = "created_at" ]; then
  72. _err "An error occurred with cert upload. Check response:"
  73. _err "$response"
  74. return 1
  75. fi
  76. _debug response "$response"
  77. _info "Certificate successfully deployed"
  78. }