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.

120 lines
4.2 KiB

  1. #!/usr/bin/env sh
  2. # Deploy certificates to a proxmox backup server using the API.
  3. #
  4. # Environment variables that can be set are:
  5. # `DEPLOY_PROXMOXBS_SERVER`: The hostname of the proxmox backup server. Defaults to
  6. # _cdomain.
  7. # `DEPLOY_PROXMOXBS_SERVER_PORT`: The port number the management interface is on.
  8. # Defaults to 8007.
  9. # `DEPLOY_PROXMOXBS_USER`: The user we'll connect as. Defaults to root.
  10. # `DEPLOY_PROXMOXBS_USER_REALM`: The authentication realm the user authenticates
  11. # with. Defaults to pam.
  12. # `DEPLOY_PROXMOXBS_API_TOKEN_NAME`: The name of the API token created for the
  13. # user account. Defaults to acme.
  14. # `DEPLOY_PROXMOXBS_API_TOKEN_KEY`: The API token. Required.
  15. proxmoxbs_deploy() {
  16. _cdomain="$1"
  17. _ckey="$2"
  18. _ccert="$3"
  19. _cca="$4"
  20. _cfullchain="$5"
  21. _debug _cdomain "$_cdomain"
  22. _debug2 _ckey "$_ckey"
  23. _debug _ccert "$_ccert"
  24. _debug _cca "$_cca"
  25. _debug _cfullchain "$_cfullchain"
  26. # "Sane" defaults.
  27. _getdeployconf DEPLOY_PROXMOXBS_SERVER
  28. if [ -z "$DEPLOY_PROXMOXBS_SERVER" ]; then
  29. _target_hostname="$_cdomain"
  30. else
  31. _target_hostname="$DEPLOY_PROXMOXBS_SERVER"
  32. _savedeployconf DEPLOY_PROXMOXBS_SERVER "$DEPLOY_PROXMOXBS_SERVER"
  33. fi
  34. _debug2 DEPLOY_PROXMOXBS_SERVER "$_target_hostname"
  35. _getdeployconf DEPLOY_PROXMOXBS_SERVER_PORT
  36. if [ -z "$DEPLOY_PROXMOXBS_SERVER_PORT" ]; then
  37. _target_port="8007"
  38. else
  39. _target_port="$DEPLOY_PROXMOXBS_SERVER_PORT"
  40. _savedeployconf DEPLOY_PROXMOXBS_SERVER_PORT "$DEPLOY_PROXMOXBS_SERVER_PORT"
  41. fi
  42. _debug2 DEPLOY_PROXMOXBS_SERVER_PORT "$_target_port"
  43. # Complete URL.
  44. _target_url="https://${_target_hostname}:${_target_port}/api2/json/nodes/localhost/certificates/custom"
  45. _debug TARGET_URL "$_target_url"
  46. # More "sane" defaults.
  47. _getdeployconf DEPLOY_PROXMOXBS_USER
  48. if [ -z "$DEPLOY_PROXMOXBS_USER" ]; then
  49. _proxmoxbs_user="root"
  50. else
  51. _proxmoxbs_user="$DEPLOY_PROXMOXBS_USER"
  52. _savedeployconf DEPLOY_PROXMOXBS_USER "$DEPLOY_PROXMOXBS_USER"
  53. fi
  54. _debug2 DEPLOY_PROXMOXBS_USER "$_proxmoxbs_user"
  55. _getdeployconf DEPLOY_PROXMOXBS_USER_REALM
  56. if [ -z "$DEPLOY_PROXMOXBS_USER_REALM" ]; then
  57. _proxmoxbs_user_realm="pam"
  58. else
  59. _proxmoxbs_user_realm="$DEPLOY_PROXMOXBS_USER_REALM"
  60. _savedeployconf DEPLOY_PROXMOXBS_USER_REALM "$DEPLOY_PROXMOXBS_USER_REALM"
  61. fi
  62. _debug2 DEPLOY_PROXMOXBS_USER_REALM "$_proxmoxbs_user_realm"
  63. _getdeployconf DEPLOY_PROXMOXBS_API_TOKEN_NAME
  64. if [ -z "$DEPLOY_PROXMOXBS_API_TOKEN_NAME" ]; then
  65. _proxmoxbs_api_token_name="acme"
  66. else
  67. _proxmoxbs_api_token_name="$DEPLOY_PROXMOXBS_API_TOKEN_NAME"
  68. _savedeployconf DEPLOY_PROXMOXBS_API_TOKEN_NAME "$DEPLOY_PROXMOXBS_API_TOKEN_NAME"
  69. fi
  70. _debug2 DEPLOY_PROXMOXBS_API_TOKEN_NAME "$_proxmoxbs_api_token_name"
  71. # This is required.
  72. _getdeployconf DEPLOY_PROXMOXBS_API_TOKEN_KEY
  73. if [ -z "$DEPLOY_PROXMOXBS_API_TOKEN_KEY" ]; then
  74. _err "API key not provided."
  75. return 1
  76. else
  77. _proxmoxbs_api_token_key="$DEPLOY_PROXMOXBS_API_TOKEN_KEY"
  78. _savedeployconf DEPLOY_PROXMOXBS_API_TOKEN_KEY "$DEPLOY_PROXMOXBS_API_TOKEN_KEY"
  79. fi
  80. _debug2 DEPLOY_PROXMOXBS_API_TOKEN_KEY "$_proxmoxbs_api_token_key"
  81. # PBS API Token header value. Used in "Authorization: PBSAPIToken".
  82. _proxmoxbs_header_api_token="${_proxmoxbs_user}@${_proxmoxbs_user_realm}!${_proxmoxbs_api_token_name}:${_proxmoxbs_api_token_key}"
  83. _debug2 "Auth Header" "$_proxmoxbs_header_api_token"
  84. # Ugly. I hate putting heredocs inside functions because heredocs don't
  85. # account for whitespace correctly but it _does_ work and is several times
  86. # cleaner than anything else I had here.
  87. #
  88. # This dumps the json payload to a variable that should be passable to the
  89. # _psot function.
  90. _json_payload=$(
  91. cat <<HEREDOC
  92. {
  93. "certificates": "$(tr '\n' ':' <"$_cfullchain" | sed 's/:/\\n/g')",
  94. "key": "$(tr '\n' ':' <"$_ckey" | sed 's/:/\\n/g')",
  95. "node":"localhost",
  96. "restart":true,
  97. "force":true
  98. }
  99. HEREDOC
  100. )
  101. _debug2 Payload "$_json_payload"
  102. _info "Push certificates to server"
  103. export HTTPS_INSECURE=1
  104. export _H1="Authorization: PBSAPIToken=${_proxmoxbs_header_api_token}"
  105. _post "$_json_payload" "$_target_url" "" POST "application/json"
  106. }