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.

95 lines
3.1 KiB

  1. #!/usr/bin/bash
  2. # Deploys a certificate to the XAPI service of the XCP-ng hypervisor.
  3. # Further documentation: https://xcp-ng.org/docs/guides.html#tls-certificate-for-xcp-ng
  4. XAPI_SSL_PATH="/etc/xensource/xapi-ssl.pem"
  5. XCP_NG_BACKUP_DIR="/tmp/$(uuidgen)"
  6. # xcp-ng_deploy deploys the new certificate to XCP-ng.
  7. xcp-ng_deploy() {
  8. _cdomain="$1"
  9. _ckey="$2"
  10. _ccert="$3"
  11. _cca="$4"
  12. _cfullchain="$5"
  13. _debug _cdomain "$_cdomain"
  14. _debug _ckey "$_ckey"
  15. _debug _ccert "$_ccert"
  16. _debug _cca "$_cca"
  17. _debug _cfullchain "$_cfullchain"
  18. if [[ $(_xcp-ng_backup_certificate) -ne 0 ]]; then
  19. return 1
  20. fi
  21. _debug "Deploying certificate with 'xe host-server-certificate-install'"
  22. if [[ $(sudo xe host-server-certificate-install certificate="${_ccert}" private-key="${_ckey}" certificate-chain="${_cca}") -ne 0 ]]; then
  23. if [[ $(_xcp-ng_backup_restore) -eq 0 ]]; then
  24. xcp-ng_backup_delete 2>&1
  25. fi
  26. return 1
  27. fi
  28. _info "Certificate was deployed successfully."
  29. _xcp-ng_backup_delete 2>&1
  30. return 0
  31. }
  32. # _xcp-ng_backup_certificate saves the current certificate to a temporary folder.
  33. # The folder can be read/ written by the current user only (chmod 600).
  34. _xcp-ng_backup_certificate() {
  35. if [[ $(whoami) != "root" ]]; then
  36. _debug "Running as non-root user. Certificate backup not supported."
  37. exit 0
  38. fi
  39. _debug "Setting up temporary directory for backing up current certificate in '${XCP_NG_BACKUP_DIR}'"
  40. if [[ $(mkdir -m 600 "${XCP_NG_BACKUP_DIR}") -ne 0 ]]; then
  41. _err "Could not create temporary directory to backup the current key."
  42. return 1
  43. fi
  44. _debug "Moving current certificate to backup directory."
  45. if [[ $(mv ${XAPI_SSL_PATH} "${XCP_NG_BACKUP_DIR}") -ne 0 ]]; then
  46. _err "Could not move current certificate to backup directory."
  47. return 1
  48. fi
  49. return 0
  50. }
  51. # _xcp-ng_backup_restore restores the backup made by _xcp-ng_backup_certificate.
  52. # It is called when something went wrong deploying the certificate.
  53. _xcp-ng_backup_restore() {
  54. if [[ $(mv "${XCP_NG_BACKUP_DIR}/xapi-ssl.pem" "${XAPI_SSL_PATH}") -eq 0 ]]; then
  55. _info "Certificate restoration successful."
  56. return 0
  57. else
  58. _err "Certificate restoration from '${XCP-NG_BACKUP_DIR}' not possible."
  59. return 1
  60. fi
  61. }
  62. # _xcp-ng_backup_delete deletes the backup folder.
  63. _xcp-ng_backup_delete() {
  64. if [[ $(rm -rf "${XCP_NG_BACKUP_DIR}") -eq 0 ]]; then
  65. _debug "Certificate backup deleted."
  66. else
  67. _err "Could not delete Backup in '${XCP_NG_BACKUP_DIR}'. Please remove it manually."
  68. fi
  69. }
  70. # _xcp-ng_xapi_restart restarts the XAPI service the certificate was deployed to.
  71. # This is only neeeded when the old certificate had to be restored.
  72. _xcp-ng_xapi_restart() {
  73. if [[ $(systemctl restart xapi) -ne 0 ]]; then
  74. _err "XAPI did not restart properly after deployment. Restoring old certificate for now."
  75. if [[ $(_xcp-ng_backup_restore) -ne 0 ]]; then
  76. _err "Could not restore the old certificate!!!"
  77. fi
  78. if [[ $(systemctl restart xapi) -ne 0 ]]; then
  79. _err "XAPI did not start after restoring the old certifiate!!!"
  80. fi
  81. return 1
  82. else
  83. _debug "XAPI was restarted successfully."
  84. return 0
  85. fi
  86. }