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.

81 lines
3.1 KiB

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