2017-01-11 13:09:58 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# This deploy hook will deploy ssl cert on kong proxy engine based on api request_host parameter.
|
|
|
|
# Note that ssl plugin should be available on Kong instance
|
|
|
|
# The hook will match cdomain to request_host, in case of multiple domain it will always take the first
|
|
|
|
# one (acme.sh behaviour).
|
|
|
|
# If ssl config already exist it will update only cert and key not touching other parameter
|
|
|
|
# If ssl config doesn't exist it will only upload cert and key and not set other parameter
|
|
|
|
# Not that we deploy full chain
|
|
|
|
# See https://getkong.org/plugins/dynamic-ssl/ for other options
|
|
|
|
# Written by Geoffroi Genot <ggenot@voxbone.com>
|
|
|
|
|
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
#domain keyfile certfile cafile fullchain
|
2017-01-11 13:54:52 +00:00
|
|
|
kong_deploy() {
|
2017-01-11 13:09:58 +00:00
|
|
|
_cdomain="$1"
|
|
|
|
_ckey="$2"
|
|
|
|
_ccert="$3"
|
|
|
|
_cca="$4"
|
|
|
|
_cfullchain="$5"
|
|
|
|
_info "Deploying certificate on Kong instance"
|
2017-01-11 13:52:52 +00:00
|
|
|
if [ -z "$KONG_URL" ]; then
|
2017-01-11 14:05:26 +00:00
|
|
|
_debug "KONG_URL Not set, using default http://localhost:8001"
|
|
|
|
KONG_URL="http://localhost:8001"
|
2017-01-11 13:09:58 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
_debug _cdomain "$_cdomain"
|
|
|
|
_debug _ckey "$_ckey"
|
|
|
|
_debug _ccert "$_ccert"
|
|
|
|
_debug _cca "$_cca"
|
|
|
|
_debug _cfullchain "$_cfullchain"
|
|
|
|
|
2017-04-27 10:23:43 +00:00
|
|
|
#Get ssl_uuid linked to the domain
|
|
|
|
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}')
|
|
|
|
if [ -z "$ssl_uuid" ]; then
|
|
|
|
_debug "Unable to get Kong ssl_uuid for domain $_cdomain"
|
|
|
|
_debug "Make sure that KONG_URL is correctly configured"
|
|
|
|
_debug "Make sure that a Kong certificate match the sni"
|
|
|
|
_debug "Kong url: $KONG_URL"
|
|
|
|
_info "No existing certificate, creating..."
|
|
|
|
#return 1
|
2017-01-11 13:09:58 +00:00
|
|
|
fi
|
|
|
|
#Save kong url if it's succesful (First run case)
|
|
|
|
_saveaccountconf KONG_URL "$KONG_URL"
|
|
|
|
#Generate DEIM
|
2017-03-26 05:26:20 +00:00
|
|
|
delim="-----MultipartDelimiter$(date "+%s%N")"
|
2017-01-11 15:17:16 +00:00
|
|
|
nl="\015\012"
|
2017-01-11 13:09:58 +00:00
|
|
|
#Set Header
|
|
|
|
_H1="Content-Type: multipart/form-data; boundary=$delim"
|
|
|
|
#Generate data for request (Multipart/form-data with mixed content)
|
2017-04-27 10:23:43 +00:00
|
|
|
content="--$delim${nl}"
|
|
|
|
if [ -z "$ssl_uuid" ]; then
|
|
|
|
#set sni to domain
|
|
|
|
content="$content{nl}Content-Disposition: form-data; name=\"snis\"${nl}${nl}$_cdomain"
|
|
|
|
fi
|
2017-01-11 13:09:58 +00:00
|
|
|
#add key
|
2017-04-27 10:23:43 +00:00
|
|
|
content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"; filename=\"$(basename "$_ckey")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")"
|
2017-01-11 13:09:58 +00:00
|
|
|
#Add cert
|
2017-04-27 10:23:43 +00:00
|
|
|
content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"cert\"; filename=\"$(basename "$_cfullchain")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_cfullchain")"
|
2017-01-11 13:09:58 +00:00
|
|
|
#Close multipart
|
|
|
|
content="$content${nl}--$delim--${nl}"
|
2017-01-11 15:17:16 +00:00
|
|
|
#Convert CRLF
|
|
|
|
content=$(printf %b "$content")
|
2017-01-11 13:09:58 +00:00
|
|
|
#DEBUG
|
|
|
|
_debug header "$_H1"
|
|
|
|
_debug content "$content"
|
2017-04-27 10:23:43 +00:00
|
|
|
#Check if sslcreated (if not => POST else => PATCH)
|
|
|
|
|
2017-04-27 12:38:08 +00:00
|
|
|
if [ ! -z "$ssl_uuid" ]; then
|
2017-01-11 13:09:58 +00:00
|
|
|
#Post certificate to Kong
|
2017-04-27 10:23:43 +00:00
|
|
|
response=$(_post "$content" "$KONG_URL/certificates" "" "POST")
|
2017-01-11 13:09:58 +00:00
|
|
|
else
|
|
|
|
#patch
|
2017-04-27 10:23:43 +00:00
|
|
|
response=$(_post "$content" "$KONG_URL/certificates/$ssl_uuid" "" "PATCH")
|
2017-01-11 13:09:58 +00:00
|
|
|
fi
|
2017-04-27 10:23:43 +00:00
|
|
|
if ! [ "$(echo "$response" | _egrep_o "created_at")" = "created_at" ]; then
|
2017-03-26 05:28:57 +00:00
|
|
|
_err "An error occurred with cert upload. Check response:"
|
2017-01-11 13:09:58 +00:00
|
|
|
_err "$response"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
_debug response "$response"
|
|
|
|
_info "Certificate successfully deployed"
|
|
|
|
}
|