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.

398 lines
15 KiB

8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. # Script for acme.sh to deploy certificates to haproxy
  3. #
  4. # The following variables can be exported:
  5. #
  6. # export DEPLOY_HAPROXY_PEM_NAME="${domain}.pem"
  7. #
  8. # Defines the name of the PEM file.
  9. # Defaults to "<domain>.pem"
  10. #
  11. # export DEPLOY_HAPROXY_PEM_PATH="/etc/haproxy"
  12. #
  13. # Defines location of PEM file for HAProxy.
  14. # Defaults to /etc/haproxy
  15. #
  16. # export DEPLOY_HAPROXY_RELOAD="systemctl reload haproxy"
  17. #
  18. # OPTIONAL: Reload command used post deploy
  19. # This defaults to be a no-op (ie "true").
  20. # It is strongly recommended to set this something that makes sense
  21. # for your distro.
  22. #
  23. # export DEPLOY_HAPROXY_ISSUER="no"
  24. #
  25. # OPTIONAL: Places CA file as "${DEPLOY_HAPROXY_PEM}.issuer"
  26. # Note: Required for OCSP stapling to work
  27. #
  28. # export DEPLOY_HAPROXY_BUNDLE="no"
  29. #
  30. # OPTIONAL: Deploy this certificate as part of a multi-cert bundle
  31. # This adds a suffix to the certificate based on the certificate type
  32. # eg RSA certificates will have .rsa as a suffix to the file name
  33. # HAProxy will load all certificates and provide one or the other
  34. # depending on client capabilities
  35. # Note: This functionality requires HAProxy was compiled against
  36. # a version of OpenSSL that supports this.
  37. #
  38. # export DEPLOY_HAPROXY_HOT_UPDATE="yes"
  39. # export DEPLOY_HAPROXY_STATS_SOCKET="UNIX:/run/haproxy/admin.sock"
  40. #
  41. # OPTIONAL: Deploy the certificate over the HAProxy stats socket without
  42. # needing to reload HAProxy. Default is "no".
  43. #
  44. # Require the socat binary. DEPLOY_HAPROXY_STATS_SOCKET variable uses the socat
  45. # address format.
  46. #
  47. # export DEPLOY_HAPROXY_MASTER_CLI="UNIX:/run/haproxy-master.sock"
  48. #
  49. # OPTIONAL: To use the master CLI with DEPLOY_HAPROXY_HOT_UPDATE="yes" instead
  50. # of a stats socket, use this variable.
  51. ######## Public functions #####################
  52. #domain keyfile certfile cafile fullchain
  53. haproxy_deploy() {
  54. _cdomain="$1"
  55. _ckey="$2"
  56. _ccert="$3"
  57. _cca="$4"
  58. _cfullchain="$5"
  59. _cmdpfx=""
  60. # Some defaults
  61. DEPLOY_HAPROXY_PEM_PATH_DEFAULT="/etc/haproxy"
  62. DEPLOY_HAPROXY_PEM_NAME_DEFAULT="${_cdomain}.pem"
  63. DEPLOY_HAPROXY_BUNDLE_DEFAULT="no"
  64. DEPLOY_HAPROXY_ISSUER_DEFAULT="no"
  65. DEPLOY_HAPROXY_RELOAD_DEFAULT="true"
  66. DEPLOY_HAPROXY_HOT_UPDATE_DEFAULT="no"
  67. DEPLOY_HAPROXY_STATS_SOCKET_DEFAULT="UNIX:/run/haproxy/admin.sock"
  68. _debug _cdomain "${_cdomain}"
  69. _debug _ckey "${_ckey}"
  70. _debug _ccert "${_ccert}"
  71. _debug _cca "${_cca}"
  72. _debug _cfullchain "${_cfullchain}"
  73. # PEM_PATH is optional. If not provided then assume "${DEPLOY_HAPROXY_PEM_PATH_DEFAULT}"
  74. _getdeployconf DEPLOY_HAPROXY_PEM_PATH
  75. _debug2 DEPLOY_HAPROXY_PEM_PATH "${DEPLOY_HAPROXY_PEM_PATH}"
  76. if [ -n "${DEPLOY_HAPROXY_PEM_PATH}" ]; then
  77. Le_Deploy_haproxy_pem_path="${DEPLOY_HAPROXY_PEM_PATH}"
  78. _savedomainconf Le_Deploy_haproxy_pem_path "${Le_Deploy_haproxy_pem_path}"
  79. elif [ -z "${Le_Deploy_haproxy_pem_path}" ]; then
  80. Le_Deploy_haproxy_pem_path="${DEPLOY_HAPROXY_PEM_PATH_DEFAULT}"
  81. fi
  82. # Ensure PEM_PATH exists
  83. if [ -d "${Le_Deploy_haproxy_pem_path}" ]; then
  84. _debug "PEM_PATH ${Le_Deploy_haproxy_pem_path} exists"
  85. else
  86. _err "PEM_PATH ${Le_Deploy_haproxy_pem_path} does not exist"
  87. return 1
  88. fi
  89. # PEM_NAME is optional. If not provided then assume "${DEPLOY_HAPROXY_PEM_NAME_DEFAULT}"
  90. _getdeployconf DEPLOY_HAPROXY_PEM_NAME
  91. _debug2 DEPLOY_HAPROXY_PEM_NAME "${DEPLOY_HAPROXY_PEM_NAME}"
  92. if [ -n "${DEPLOY_HAPROXY_PEM_NAME}" ]; then
  93. Le_Deploy_haproxy_pem_name="${DEPLOY_HAPROXY_PEM_NAME}"
  94. _savedomainconf Le_Deploy_haproxy_pem_name "${Le_Deploy_haproxy_pem_name}"
  95. elif [ -z "${Le_Deploy_haproxy_pem_name}" ]; then
  96. Le_Deploy_haproxy_pem_name="${DEPLOY_HAPROXY_PEM_NAME_DEFAULT}"
  97. fi
  98. # BUNDLE is optional. If not provided then assume "${DEPLOY_HAPROXY_BUNDLE_DEFAULT}"
  99. _getdeployconf DEPLOY_HAPROXY_BUNDLE
  100. _debug2 DEPLOY_HAPROXY_BUNDLE "${DEPLOY_HAPROXY_BUNDLE}"
  101. if [ -n "${DEPLOY_HAPROXY_BUNDLE}" ]; then
  102. Le_Deploy_haproxy_bundle="${DEPLOY_HAPROXY_BUNDLE}"
  103. _savedomainconf Le_Deploy_haproxy_bundle "${Le_Deploy_haproxy_bundle}"
  104. elif [ -z "${Le_Deploy_haproxy_bundle}" ]; then
  105. Le_Deploy_haproxy_bundle="${DEPLOY_HAPROXY_BUNDLE_DEFAULT}"
  106. fi
  107. # ISSUER is optional. If not provided then assume "${DEPLOY_HAPROXY_ISSUER_DEFAULT}"
  108. _getdeployconf DEPLOY_HAPROXY_ISSUER
  109. _debug2 DEPLOY_HAPROXY_ISSUER "${DEPLOY_HAPROXY_ISSUER}"
  110. if [ -n "${DEPLOY_HAPROXY_ISSUER}" ]; then
  111. Le_Deploy_haproxy_issuer="${DEPLOY_HAPROXY_ISSUER}"
  112. _savedomainconf Le_Deploy_haproxy_issuer "${Le_Deploy_haproxy_issuer}"
  113. elif [ -z "${Le_Deploy_haproxy_issuer}" ]; then
  114. Le_Deploy_haproxy_issuer="${DEPLOY_HAPROXY_ISSUER_DEFAULT}"
  115. fi
  116. # RELOAD is optional. If not provided then assume "${DEPLOY_HAPROXY_RELOAD_DEFAULT}"
  117. _getdeployconf DEPLOY_HAPROXY_RELOAD
  118. _debug2 DEPLOY_HAPROXY_RELOAD "${DEPLOY_HAPROXY_RELOAD}"
  119. if [ -n "${DEPLOY_HAPROXY_RELOAD}" ]; then
  120. Le_Deploy_haproxy_reload="${DEPLOY_HAPROXY_RELOAD}"
  121. _savedomainconf Le_Deploy_haproxy_reload "${Le_Deploy_haproxy_reload}"
  122. elif [ -z "${Le_Deploy_haproxy_reload}" ]; then
  123. Le_Deploy_haproxy_reload="${DEPLOY_HAPROXY_RELOAD_DEFAULT}"
  124. fi
  125. # HOT_UPDATE is optional. If not provided then assume "${DEPLOY_HAPROXY_HOT_UPDATE_DEFAULT}"
  126. _getdeployconf DEPLOY_HAPROXY_HOT_UPDATE
  127. _debug2 DEPLOY_HAPROXY_HOT_UPDATE "${DEPLOY_HAPROXY_HOT_UPDATE}"
  128. if [ -n "${DEPLOY_HAPROXY_HOT_UPDATE}" ]; then
  129. Le_Deploy_haproxy_hot_update="${DEPLOY_HAPROXY_HOT_UPDATE}"
  130. _savedomainconf Le_Deploy_haproxy_hot_update "${Le_Deploy_haproxy_hot_update}"
  131. elif [ -z "${Le_Deploy_haproxy_hot_update}" ]; then
  132. Le_Deploy_haproxy_hot_update="${DEPLOY_HAPROXY_HOT_UPDATE_DEFAULT}"
  133. fi
  134. # STATS_SOCKET is optional. If not provided then assume "${DEPLOY_HAPROXY_STATS_SOCKET_DEFAULT}"
  135. _getdeployconf DEPLOY_HAPROXY_STATS_SOCKET
  136. _debug2 DEPLOY_HAPROXY_STATS_SOCKET "${DEPLOY_HAPROXY_STATS_SOCKET}"
  137. if [ -n "${DEPLOY_HAPROXY_STATS_SOCKET}" ]; then
  138. Le_Deploy_haproxy_stats_socket="${DEPLOY_HAPROXY_STATS_SOCKET}"
  139. _savedomainconf Le_Deploy_haproxy_stats_socket "${Le_Deploy_haproxy_stats_socket}"
  140. elif [ -z "${Le_Deploy_haproxy_stats_socket}" ]; then
  141. Le_Deploy_haproxy_stats_socket="${DEPLOY_HAPROXY_STATS_SOCKET_DEFAULT}"
  142. fi
  143. # MASTER_CLI is optional. No defaults are used. When the master CLI is used,
  144. # all commands are sent with a prefix.
  145. _getdeployconf DEPLOY_HAPROXY_MASTER_CLI
  146. _debug2 DEPLOY_HAPROXY_MASTER_CLI "${DEPLOY_HAPROXY_MASTER_CLI}"
  147. if [ -n "${DEPLOY_HAPROXY_MASTER_CLI}" ]; then
  148. Le_Deploy_haproxy_stats_socket="${DEPLOY_HAPROXY_MASTER_CLI}"
  149. _savedomainconf Le_Deploy_haproxy_stats_socket "${Le_Deploy_haproxy_stats_socket}"
  150. _cmdpfx="@1 " # command prefix used for master CLI only.
  151. fi
  152. # Set the suffix depending if we are creating a bundle or not
  153. if [ "${Le_Deploy_haproxy_bundle}" = "yes" ]; then
  154. _info "Bundle creation requested"
  155. # Initialise $Le_Keylength if its not already set
  156. if [ -z "${Le_Keylength}" ]; then
  157. Le_Keylength=""
  158. fi
  159. if _isEccKey "${Le_Keylength}"; then
  160. _info "ECC key type detected"
  161. _suffix=".ecdsa"
  162. else
  163. _info "RSA key type detected"
  164. _suffix=".rsa"
  165. fi
  166. else
  167. _suffix=""
  168. fi
  169. _debug _suffix "${_suffix}"
  170. # Set variables for later
  171. _pem="${Le_Deploy_haproxy_pem_path}/${Le_Deploy_haproxy_pem_name}${_suffix}"
  172. _issuer="${_pem}.issuer"
  173. _ocsp="${_pem}.ocsp"
  174. _reload="${Le_Deploy_haproxy_reload}"
  175. _statssock="${Le_Deploy_haproxy_stats_socket}"
  176. _info "Deploying PEM file"
  177. # Create a temporary PEM file
  178. _temppem="$(_mktemp)"
  179. _debug _temppem "${_temppem}"
  180. cat "${_ccert}" "${_cca}" "${_ckey}" | grep . >"${_temppem}"
  181. _ret="$?"
  182. # Check that we could create the temporary file
  183. if [ "${_ret}" != "0" ]; then
  184. _err "Error code ${_ret} returned during PEM file creation"
  185. [ -f "${_temppem}" ] && rm -f "${_temppem}"
  186. return ${_ret}
  187. fi
  188. # Move PEM file into place
  189. _info "Moving new certificate into place"
  190. _debug _pem "${_pem}"
  191. cat "${_temppem}" >"${_pem}"
  192. _ret=$?
  193. # Clean up temp file
  194. [ -f "${_temppem}" ] && rm -f "${_temppem}"
  195. # Deal with any failure of moving PEM file into place
  196. if [ "${_ret}" != "0" ]; then
  197. _err "Error code ${_ret} returned while moving new certificate into place"
  198. return ${_ret}
  199. fi
  200. # Update .issuer file if requested
  201. if [ "${Le_Deploy_haproxy_issuer}" = "yes" ]; then
  202. _info "Updating .issuer file"
  203. _debug _issuer "${_issuer}"
  204. cat "${_cca}" >"${_issuer}"
  205. _ret="$?"
  206. if [ "${_ret}" != "0" ]; then
  207. _err "Error code ${_ret} returned while copying issuer/CA certificate into place"
  208. return ${_ret}
  209. fi
  210. else
  211. [ -f "${_issuer}" ] && _err "Issuer file update not requested but .issuer file exists"
  212. fi
  213. # Update .ocsp file if certificate was requested with --ocsp/--ocsp-must-staple option
  214. if [ -z "${Le_OCSP_Staple}" ]; then
  215. Le_OCSP_Staple="0"
  216. fi
  217. if [ "${Le_OCSP_Staple}" = "1" ]; then
  218. _info "Updating OCSP stapling info"
  219. _debug _ocsp "${_ocsp}"
  220. _info "Extracting OCSP URL"
  221. _ocsp_url=$(${ACME_OPENSSL_BIN:-openssl} x509 -noout -ocsp_uri -in "${_pem}")
  222. _debug _ocsp_url "${_ocsp_url}"
  223. # Only process OCSP if URL was present
  224. if [ "${_ocsp_url}" != "" ]; then
  225. # Extract the hostname from the OCSP URL
  226. _info "Extracting OCSP URL"
  227. _ocsp_host=$(echo "${_ocsp_url}" | cut -d/ -f3)
  228. _debug _ocsp_host "${_ocsp_host}"
  229. # Only process the certificate if we have a .issuer file
  230. if [ -r "${_issuer}" ]; then
  231. # Check if issuer cert is also a root CA cert
  232. _subjectdn=$(${ACME_OPENSSL_BIN:-openssl} x509 -in "${_issuer}" -subject -noout | cut -d'/' -f2,3,4,5,6,7,8,9,10)
  233. _debug _subjectdn "${_subjectdn}"
  234. _issuerdn=$(${ACME_OPENSSL_BIN:-openssl} x509 -in "${_issuer}" -issuer -noout | cut -d'/' -f2,3,4,5,6,7,8,9,10)
  235. _debug _issuerdn "${_issuerdn}"
  236. _info "Requesting OCSP response"
  237. # If the issuer is a CA cert then our command line has "-CAfile" added
  238. if [ "${_subjectdn}" = "${_issuerdn}" ]; then
  239. _cafile_argument="-CAfile \"${_issuer}\""
  240. else
  241. _cafile_argument=""
  242. fi
  243. _debug _cafile_argument "${_cafile_argument}"
  244. # if OpenSSL/LibreSSL is v1.1 or above, the format for the -header option has changed
  245. _openssl_version=$(${ACME_OPENSSL_BIN:-openssl} version | cut -d' ' -f2)
  246. _debug _openssl_version "${_openssl_version}"
  247. _openssl_major=$(echo "${_openssl_version}" | cut -d '.' -f1)
  248. _openssl_minor=$(echo "${_openssl_version}" | cut -d '.' -f2)
  249. if [ "${_openssl_major}" -eq "1" ] && [ "${_openssl_minor}" -ge "1" ] || [ "${_openssl_major}" -ge "2" ]; then
  250. _header_sep="="
  251. else
  252. _header_sep=" "
  253. fi
  254. # Request the OCSP response from the issuer and store it
  255. _openssl_ocsp_cmd="${ACME_OPENSSL_BIN:-openssl} ocsp \
  256. -issuer \"${_issuer}\" \
  257. -cert \"${_pem}\" \
  258. -url \"${_ocsp_url}\" \
  259. -header Host${_header_sep}\"${_ocsp_host}\" \
  260. -respout \"${_ocsp}\" \
  261. -verify_other \"${_issuer}\" \
  262. ${_cafile_argument} \
  263. | grep -q \"${_pem}: good\""
  264. _debug _openssl_ocsp_cmd "${_openssl_ocsp_cmd}"
  265. eval "${_openssl_ocsp_cmd}"
  266. _ret=$?
  267. else
  268. # Non fatal: No issuer file was present so no OCSP stapling file created
  269. _err "OCSP stapling in use but no .issuer file was present"
  270. fi
  271. else
  272. # Non fatal: No OCSP url was found int the certificate
  273. _err "OCSP update requested but no OCSP URL was found in certificate"
  274. fi
  275. # Non fatal: Check return code of openssl command
  276. if [ "${_ret}" != "0" ]; then
  277. _err "Updating OCSP stapling failed with return code ${_ret}"
  278. fi
  279. else
  280. # An OCSP file was already present but certificate did not have OCSP extension
  281. if [ -f "${_ocsp}" ]; then
  282. _err "OCSP was not requested but .ocsp file exists."
  283. # Could remove the file at this step, although HAProxy just ignores it in this case
  284. # rm -f "${_ocsp}" || _err "Problem removing stale .ocsp file"
  285. fi
  286. fi
  287. if [ "${Le_Deploy_haproxy_hot_update}" = "yes" ]; then
  288. # set the socket name for messages
  289. if [ -n "${_cmdpfx}" ]; then
  290. _socketname="master CLI"
  291. else
  292. _socketname="stats socket"
  293. fi
  294. # Update certificate over HAProxy stats socket or master CLI.
  295. if _exists socat; then
  296. # look for the certificate on the stats socket, to chose between updating or creating one
  297. _socat_cert_cmd="echo '${_cmdpfx}show ssl cert' | socat '${_statssock}' - | grep -q '^${_pem}$'"
  298. _debug _socat_cert_cmd "${_socat_cert_cmd}"
  299. eval "${_socat_cert_cmd}"
  300. _ret=$?
  301. if [ "${_ret}" != "0" ]; then
  302. _newcert="1"
  303. _info "Creating new certificate '${_pem}' over HAProxy ${_socketname}."
  304. # certificate wasn't found, it's a new one. We should check if the crt-list exists and creates/inserts the certificate.
  305. _socat_crtlist_show_cmd="echo '${_cmdpfx}show ssl crt-list' | socat '${_statssock}' - | grep -q '^${Le_Deploy_haproxy_pem_path}$'"
  306. _debug _socat_crtlist_show_cmd "${_socat_crtlist_show_cmd}"
  307. eval "${_socat_crtlist_show_cmd}"
  308. _ret=$?
  309. if [ "${_ret}" != "0" ]; then
  310. _err "Couldn't find '${Le_Deploy_haproxy_pem_path}' in haproxy 'show ssl crt-list'"
  311. return "${_ret}"
  312. fi
  313. # create a new certificate
  314. _socat_new_cmd="echo '${_cmdpfx}new ssl cert ${_pem}' | socat '${_statssock}' - | grep -q 'New empty'"
  315. _debug _socat_new_cmd "${_socat_new_cmd}"
  316. eval "${_socat_new_cmd}"
  317. _ret=$?
  318. if [ "${_ret}" != "0" ]; then
  319. _err "Couldn't create '${_pem}' in haproxy"
  320. return "${_ret}"
  321. fi
  322. else
  323. _info "Update existing certificate '${_pem}' over HAProxy ${_socketname}."
  324. fi
  325. _socat_cert_set_cmd="echo -e '${_cmdpfx}set ssl cert ${_pem} <<\n$(cat "${_pem}")\n' | socat '${_statssock}' - | grep -q 'Transaction created'"
  326. _debug _socat_cert_set_cmd "${_socat_cert_set_cmd}"
  327. eval "${_socat_cert_set_cmd}"
  328. _ret=$?
  329. if [ "${_ret}" != "0" ]; then
  330. _err "Can't update '${_pem}' in haproxy"
  331. return "${_ret}"
  332. fi
  333. _socat_cert_commit_cmd="echo '${_cmdpfx}commit ssl cert ${_pem}' | socat '${_statssock}' - | grep -q '^Success!$'"
  334. _debug _socat_cert_commit_cmd "${_socat_cert_commit_cmd}"
  335. eval "${_socat_cert_commit_cmd}"
  336. _ret=$?
  337. if [ "${_ret}" != "0" ]; then
  338. _err "Can't commit '${_pem}' in haproxy"
  339. return ${_ret}
  340. fi
  341. if [ "${_newcert}" = "1" ]; then
  342. # if this is a new certificate, it needs to be inserted into the crt-list`
  343. _socat_cert_add_cmd="echo '${_cmdpfx}add ssl crt-list ${Le_Deploy_haproxy_pem_path} ${_pem}' | socat '${_statssock}' - | grep -q 'Success!'"
  344. _debug _socat_cert_add_cmd "${_socat_cert_add_cmd}"
  345. eval "${_socat_cert_add_cmd}"
  346. _ret=$?
  347. if [ "${_ret}" != "0" ]; then
  348. _err "Can't update '${_pem}' in haproxy"
  349. return "${_ret}"
  350. fi
  351. fi
  352. else
  353. _err "'socat' is not available, couldn't update over ${_socketname}"
  354. fi
  355. else
  356. # Reload HAProxy
  357. _debug _reload "${_reload}"
  358. eval "${_reload}"
  359. _ret=$?
  360. if [ "${_ret}" != "0" ]; then
  361. _err "Error code ${_ret} during reload"
  362. return ${_ret}
  363. else
  364. _info "Reload successful"
  365. fi
  366. fi
  367. return 0
  368. }