kemplm.sh 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env sh
  2. #Here is a script to deploy cert to a Kemp Loadmaster.
  3. #returns 0 means success, otherwise error.
  4. #DEPLOY_KEMP_TOKEN="token"
  5. #DEPLOY_KEMP_URL="https://kemplm.example.com"
  6. ######## Public functions #####################
  7. #domain keyfile certfile cafile fullchain
  8. kemplm_deploy() {
  9. _domain="$1"
  10. _key_file="$2"
  11. _cert_file="$3"
  12. _ca_file="$4"
  13. _fullchain_file="$5"
  14. _debug _domain "$_domain"
  15. _debug _key_file "$_key_file"
  16. _debug _cert_file "$_cert_file"
  17. _debug _ca_file "$_ca_file"
  18. _debug _fullchain_file "$_fullchain_file"
  19. if ! _exists jq; then
  20. _err "jq not found"
  21. return 1
  22. fi
  23. # Rename wildcard certs, kemp accepts only alphanumeric names so we delete '*.' from filename
  24. _kemp_domain=$(echo "${_domain}" | sed 's/\*\.//')
  25. _debug _kemp_domain "$_kemp_domain"
  26. # Read config from saved values or env
  27. _getdeployconf DEPLOY_KEMP_TOKEN
  28. _getdeployconf DEPLOY_KEMP_URL
  29. _debug DEPLOY_KEMP_URL "$DEPLOY_KEMP_URL"
  30. _secure_debug DEPLOY_KEMP_TOKEN "$DEPLOY_KEMP_TOKEN"
  31. if [ -z "$DEPLOY_KEMP_TOKEN" ]; then
  32. _err "Kemp Loadmaster token is not found, please define DEPLOY_KEMP_TOKEN."
  33. return 1
  34. fi
  35. if [ -z "$DEPLOY_KEMP_URL" ]; then
  36. _err "Kemp Loadmaster URL is not found, please define DEPLOY_KEMP_URL."
  37. return 1
  38. fi
  39. # Save current values
  40. _savedeployconf DEPLOY_KEMP_TOKEN "$DEPLOY_KEMP_TOKEN"
  41. _savedeployconf DEPLOY_KEMP_URL "$DEPLOY_KEMP_URL"
  42. # Check if certificate is already installed
  43. _info "Check if certificate is already present"
  44. _list_request="{\"cmd\": \"listcert\", \"apikey\": \"${DEPLOY_KEMP_TOKEN}\"}"
  45. _debug3 _list_request "${_list_request}"
  46. _kemp_cert_count=$(HTTPS_INSECURE=1 _post "${_list_request}" "${DEPLOY_KEMP_URL}/accessv2" | jq -r '.cert[] | .name' | grep -c "${_kemp_domain}")
  47. _debug2 _kemp_cert_count "${_kemp_cert_count}"
  48. _kemp_replace_cert=1
  49. if [ "${_kemp_cert_count}" -eq 0 ]; then
  50. _kemp_replace_cert=0
  51. _info "Certificate does not exist on Kemp Loadmaster"
  52. else
  53. _info "Certificate already exists on Kemp Loadmaster"
  54. fi
  55. _debug _kemp_replace_cert "${_kemp_replace_cert}"
  56. # Upload new certificate to Kemp Loadmaster
  57. _kemp_upload_cert=$(_mktemp)
  58. cat "${_fullchain_file}" "${_key_file}" | base64 | tr -d '\n' >"${_kemp_upload_cert}"
  59. _info "Uploading certificate to Kemp Loadmaster"
  60. _add_data=$(cat "${_kemp_upload_cert}")
  61. _add_request="{\"cmd\": \"addcert\", \"apikey\": \"${DEPLOY_KEMP_TOKEN}\", \"replace\": ${_kemp_replace_cert}, \"cert\": \"${_kemp_domain}\", \"data\": \"${_add_data}\"}"
  62. _debug3 _add_request "${_add_request}"
  63. _kemp_post_result=$(HTTPS_INSECURE=1 _post "${_add_request}" "${DEPLOY_KEMP_URL}/accessv2")
  64. _retval=$?
  65. _debug2 _kemp_post_result "${_kemp_post_result}"
  66. if [ "${_retval}" -eq 0 ]; then
  67. _kemp_post_status=$(echo "${_kemp_post_result}" | jq -r '.status')
  68. _kemp_post_message=$(echo "${_kemp_post_result}" | jq -r '.message')
  69. if [ "${_kemp_post_status}" = "ok" ]; then
  70. _info "Upload successful"
  71. else
  72. _err "Upload failed: ${_kemp_post_message}"
  73. fi
  74. else
  75. _err "Upload failed"
  76. _retval=1
  77. fi
  78. rm "${_kemp_upload_cert}"
  79. return $_retval
  80. }