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.

267 lines
8.8 KiB

8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 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. #
  20. # export DEPLOY_HAPROXY_ISSUER="no"
  21. #
  22. # OPTIONAL: Places CA file as "${DEPLOY_HAPROXY_PEM}.issuer"
  23. # Note: Required for OCSP stapling to work
  24. #
  25. # export DEPLOY_HAPROXY_BUNDLE="no"
  26. #
  27. # OPTIONAL: Deploy this certificate as part of a multi-cert bundle
  28. # This adds a suffix to the certificate based on the certificate type
  29. # eg RSA certificates will have .rsa as a suffix to the file name
  30. # HAProxy will load all certificates and provide one or the other
  31. # depending on client capabilities
  32. # Note: This functionality requires HAProxy was compiled against
  33. # a version of OpenSSL that supports this.
  34. #
  35. ######## Public functions #####################
  36. #domain keyfile certfile cafile fullchain
  37. haproxy_deploy() {
  38. _cdomain="$1"
  39. _ckey="$2"
  40. _ccert="$3"
  41. _cca="$4"
  42. _cfullchain="$5"
  43. # Some defaults
  44. DEPLOY_HAPROXY_PEM_PATH_DEFAULT="/etc/haproxy"
  45. DEPLOY_HAPROXY_PEM_NAME_DEFAULT="${_cdomain}.pem"
  46. DEPLOY_HAPROXY_BUNDLE_DEFAULT="no"
  47. DEPLOY_HAPROXY_ISSUER_DEFAULT="no"
  48. DEPLOY_HAPROXY_RELOAD_DEFAULT="systemctl reload haproxy"
  49. if [ -f "${DOMAIN_CONF}" ]; then
  50. # shellcheck disable=SC1090
  51. . "${DOMAIN_CONF}"
  52. fi
  53. _debug _cdomain "${_cdomain}"
  54. _debug _ckey "${_ckey}"
  55. _debug _ccert "${_ccert}"
  56. _debug _cca "${_cca}"
  57. _debug _cfullchain "${_cfullchain}"
  58. # PEM_PATH is optional. If not provided then assume "${DEPLOY_HAPROXY_PEM_PATH_DEFAULT}"
  59. if [ -n "${DEPLOY_HAPROXY_PEM_PATH}" ]; then
  60. Le_Deploy_haproxy_pem_path="${DEPLOY_HAPROXY_PEM_PATH}"
  61. _savedomainconf Le_Deploy_haproxy_pem_path "${Le_Deploy_haproxy_pem_path}"
  62. elif [ -z "${Le_Deploy_haproxy_pem_path}" ]; then
  63. Le_Deploy_haproxy_pem_path="${DEPLOY_HAPROXY_PEM_PATH_DEFAULT}"
  64. fi
  65. # Ensure PEM_PATH exists
  66. if [ -d "${Le_Deploy_haproxy_pem_path}" ]; then
  67. _debug "PEM_PATH ${Le_Deploy_haproxy_pem_path} exists"
  68. else
  69. _err "PEM_PATH ${Le_Deploy_haproxy_pem_path} does not exist"
  70. return 1
  71. fi
  72. # PEM_NAME is optional. If not provided then assume "${DEPLOY_HAPROXY_PEM_NAME_DEFAULT}"
  73. if [ -n "${DEPLOY_HAPROXY_PEM_NAME}" ]; then
  74. Le_Deploy_haproxy_pem_name="${DEPLOY_HAPROXY_PEM_NAME}"
  75. _savedomainconf Le_Deploy_haproxy_pem_name "${Le_Deploy_haproxy_pem_name}"
  76. elif [ -z "${Le_Deploy_haproxy_pem_name}" ]; then
  77. Le_Deploy_haproxy_pem_name="${DEPLOY_HAPROXY_PEM_NAME_DEFAULT}"
  78. fi
  79. # BUNDLE is optional. If not provided then assume "${DEPLOY_HAPROXY_BUNDLE_DEFAULT}"
  80. if [ -n "${DEPLOY_HAPROXY_BUNDLE}" ]; then
  81. Le_Deploy_haproxy_bundle="${DEPLOY_HAPROXY_BUNDLE}"
  82. _savedomainconf Le_Deploy_haproxy_bundle "${Le_Deploy_haproxy_bundle}"
  83. elif [ -z "${Le_Deploy_haproxy_bundle}" ]; then
  84. Le_Deploy_haproxy_bundle="${DEPLOY_HAPROXY_BUNDLE_DEFAULT}"
  85. fi
  86. # ISSUER is optional. If not provided then assume "${DEPLOY_HAPROXY_ISSUER_DEFAULT}"
  87. if [ -n "${DEPLOY_HAPROXY_ISSUER}" ]; then
  88. Le_Deploy_haproxy_issuer="${DEPLOY_HAPROXY_ISSUER}"
  89. _savedomainconf Le_Deploy_haproxy_issuer "${Le_Deploy_haproxy_issuer}"
  90. elif [ -z "${Le_Deploy_haproxy_issuer}" ]; then
  91. Le_Deploy_haproxy_issuer="${DEPLOY_HAPROXY_ISSUER_DEFAULT}"
  92. fi
  93. # RELOAD is optional. If not provided then assume "${DEPLOY_HAPROXY_RELOAD_DEFAULT}"
  94. if [ -n "${DEPLOY_HAPROXY_RELOAD}" ]; then
  95. Le_Deploy_haproxy_reload="${DEPLOY_HAPROXY_RELOAD}"
  96. _savedomainconf Le_Deploy_haproxy_reload "${Le_Deploy_haproxy_reload}"
  97. elif [ -z "${Le_Deploy_haproxy_reload}" ]; then
  98. Le_Deploy_haproxy_reload="${DEPLOY_HAPROXY_RELOAD_DEFAULT}"
  99. fi
  100. # Set the suffix depending if we are creating a bundle or not
  101. if [ "${Le_Deploy_haproxy_bundle}" = "yes" ]; then
  102. _info "Bundle creation requested"
  103. # Initialise $Le_KeyLength if its not already set
  104. if [ -z "${Le_KeyLength}" ]; then
  105. Le_KeyLength=""
  106. fi
  107. if _isEccKey "${Le_KeyLength}"; then
  108. _info "ECC key type so set suffix to .ecc"
  109. _suffix=".ecc"
  110. else
  111. _info "RSA key type so set suffix to .rsa"
  112. _suffix=".rsa"
  113. fi
  114. else
  115. _suffix=""
  116. fi
  117. # Set variables for later
  118. _pem="${Le_Deploy_haproxy_pem_path}/${Le_Deploy_haproxy_pem_name}${_suffix}"
  119. _issuer="${_pem}.issuer"
  120. _ocsp="${_pem}.ocsp"
  121. _reload="${Le_Deploy_haproxy_reload}"
  122. _info "Deploying PEM file"
  123. # Create a temporary PEM file
  124. _temppem="$(_mktemp)"
  125. _debug _temppem "${_temppem}"
  126. cat "${_ckey}" "${_ccert}" "${_cca}" >"${_temppem}"
  127. _ret="$?"
  128. # Check that we could create the temporary file
  129. if [ "${_ret}" != "0" ]; then
  130. _err "Error code ${_ret} returned during PEM file creation"
  131. [ -f "${_temppem}" ] && rm -f "${_temppem}"
  132. return ${_ret}
  133. fi
  134. # Move PEM file into place
  135. _info "Moving new certificate into place"
  136. _debug _pem "${_pem}"
  137. cat "${_temppem}" >"${_pem}"
  138. _ret=$?
  139. # Clean up temp file
  140. [ -f "${_temppem}" ] && rm -f "${_temppem}"
  141. # Deal with any failure of moving PEM file into place
  142. if [ "${_ret}" != "0" ]; then
  143. _err "Error code ${_ret} returned while moving new certificate into place"
  144. return ${_ret}
  145. fi
  146. # Update .issuer file if requested
  147. if [ "${Le_Deploy_haproxy_issuer}" = "yes" ]; then
  148. _info "Updating .issuer file"
  149. _debug _issuer "${_issuer}"
  150. cat "${_cca}" >"${_issuer}"
  151. _ret="$?"
  152. if [ "${_ret}" != "0" ]; then
  153. _err "Error code ${_ret} returned while copying issuer/CA certificate into place"
  154. return ${_ret}
  155. fi
  156. else
  157. [ -f "${_issuer}" ] _err "Issuer file update not requested but .issuer file exists"
  158. fi
  159. # Update .ocsp file if certificate was requested with --ocsp/--ocsp-must-staple option
  160. if [ -z "${Le_OCSP_Staple}" ]; then
  161. Le_OCSP_Staple="0"
  162. fi
  163. if [ "${Le_OCSP_Staple}" = "1" ]; then
  164. _info "Updating OCSP stapling info"
  165. _debug _ocsp "${_ocsp}"
  166. _info "Extracting OCSP URL"
  167. _ocsp_url=$(openssl x509 -noout -ocsp_uri -in "${_pem}")
  168. _debug _ocsp_url "${_ocsp_url}"
  169. # Only process OCSP if URL was present
  170. if [ "${_ocsp_url}" != "" ]; then
  171. # Extract the hostname from the OCSP URL
  172. _info "Extracting OCSP URL"
  173. _ocsp_host=$(echo "${_ocsp_url}" | cut -d/ -f3)
  174. _debug _ocsp_host "${_ocsp_host}"
  175. # Only process the certificate if we have a .issuer file
  176. if [ -r "${_issuer}" ]; then
  177. # Check if issuer cert is also a root CA cert
  178. _subjectdn=$(openssl x509 -in "${_issuer}" -subject -noout | cut -d'/' -f2,3,4,5,6,7,8,9,10)
  179. _debug _subjectdn "${_subjectdn}"
  180. _issuerdn=$(openssl x509 -in "${_issuer}" -issuer -noout | cut -d'/' -f2,3,4,5,6,7,8,9,10)
  181. _debug _issuerdn "${_issuerdn}"
  182. _info "Requesting OCSP response"
  183. # Request the OCSP response from the issuer and store it
  184. if [ "${_subjectdn}" = "${_issuerdn}" ]; then
  185. # If the issuer is a CA cert then our command line has "-CAfile" added
  186. openssl ocsp \
  187. -issuer "${_issuer}" \
  188. -cert "${_pem}" \
  189. -url "${_ocsp_url}" \
  190. -header Host "${_ocsp_host}" \
  191. -respout "${_ocsp}" \
  192. -verify_other "${_issuer}" \
  193. -no_nonce \
  194. -CAfile "${_issuer}"
  195. _ret=$?
  196. else
  197. # Issuer is not a root CA so no "-CAfile" option
  198. openssl ocsp \
  199. -issuer "${_issuer}" \
  200. -cert "${_pem}" \
  201. -url "${_ocsp_url}" \
  202. -header Host "${_ocsp_host}" \
  203. -respout "${_ocsp}" \
  204. -verify_other "${_issuer}" \
  205. -no_nonce
  206. _ret=$?
  207. fi
  208. else
  209. # Non fatal: No issuer file was present so no OCSP stapling file created
  210. _err "OCSP stapling in use but no .issuer file was present"
  211. fi
  212. else
  213. # Non fatal: No OCSP url was found int the certificate
  214. _err "OCSP update requested but no OCSP URL was found in certificate"
  215. fi
  216. # Check return code of openssl command
  217. if [ "${_ret}" != "0" ]; then
  218. _err "Updating OCSP stapling failed with return code ${_ret}"
  219. return ${_ret}
  220. fi
  221. else
  222. # An OCSP file was already present but certificate did not have OCSP extension
  223. if [ -f "${_ocsp}" ]; then
  224. _err "OCSP was not requested but .ocsp file exists."
  225. # Should remove the file at this step, although HAProxy just ignores it in this case
  226. # rm -f "${_ocsp}" || _err "Problem removing stale .ocsp file"
  227. fi
  228. fi
  229. # Reload HAProxy
  230. _debug _reload "${_reload}"
  231. eval "${_reload}"
  232. _ret=$?
  233. if [ "${_ret}" != "0" ]; then
  234. _info "Reload successful"
  235. else
  236. _err "Error code ${_ret} during reload"
  237. return ${_ret}
  238. fi
  239. return 0
  240. }