2022-06-18 09:01:38 +02:00
|
|
|
#!/usr/bin/env sh
|
2022-06-11 19:49:31 +02:00
|
|
|
|
|
|
|
# Deploy certificates to a proxmox virtual environment node using the API.
|
|
|
|
#
|
|
|
|
# Environment variables that can be set are:
|
|
|
|
# `DEPLOY_PROXMOXVE_SERVER`: The hostname of the proxmox ve node. Defaults to
|
|
|
|
# _cdomain.
|
|
|
|
# `DEPLOY_PROXMOXVE_SERVER_PORT`: The port number the management interface is on.
|
|
|
|
# Defaults to 8006.
|
|
|
|
# `DEPLOY_PROXMOXVE_NODE_NAME`: The name of the node we'll be connecting to.
|
|
|
|
# Defaults to the host portion of the server
|
|
|
|
# domain name.
|
|
|
|
# `DEPLOY_PROXMOXVE_USER`: The user we'll connect as. Defaults to root.
|
|
|
|
# `DEPLOY_PROXMOXVE_USER_REALM`: The authentication realm the user authenticates
|
|
|
|
# with. Defaults to pam.
|
|
|
|
# `DEPLOY_PROXMOXVE_API_TOKEN_NAME`: The name of the API token created for the
|
|
|
|
# user account. Defaults to acme.
|
|
|
|
# `DEPLOY_PROXMOXVE_API_TOKEN_KEY`: The API token. Required.
|
|
|
|
|
|
|
|
proxmoxve_deploy(){
|
|
|
|
_cdomain="$1"
|
|
|
|
_ckey="$2"
|
|
|
|
_ccert="$3"
|
|
|
|
_cca="$4"
|
|
|
|
_cfullchain="$5"
|
|
|
|
|
|
|
|
_debug _cdomain "$_cdomain"
|
2022-06-18 21:41:38 +02:00
|
|
|
_debug2 _ckey "$_ckey"
|
2022-06-11 19:49:31 +02:00
|
|
|
_debug _ccert "$_ccert"
|
|
|
|
_debug _cca "$_cca"
|
|
|
|
_debug _cfullchain "$_cfullchain"
|
|
|
|
|
|
|
|
# "Sane" defaults.
|
2022-06-18 21:40:05 +02:00
|
|
|
_getdeployconf DEPLOY_PROXMOXVE_SERVER
|
|
|
|
if [ -z "$DEPLOY_PROXMOXVE_SERVER" ]; then
|
|
|
|
_target_hostname="$_cdomain"
|
2022-06-18 22:39:32 +02:00
|
|
|
else
|
|
|
|
_target_hostname="$DEPLOY_PROXMOXVE_SERVER"
|
2022-06-20 04:01:56 +02:00
|
|
|
_savedeployconf DEPLOY_PROXMOXVE_SERVER "$DEPLOY_PROXMOXVE_SERVER"
|
2022-06-11 19:49:31 +02:00
|
|
|
fi
|
2022-06-18 21:40:05 +02:00
|
|
|
_debug2 DEPLOY_PROXMOXVE_SERVER "$_target_hostname"
|
2022-06-11 19:49:31 +02:00
|
|
|
|
2022-06-18 21:40:05 +02:00
|
|
|
_getdeployconf DEPLOY_PROXMOXVE_SERVER_PORT
|
|
|
|
if [ -z "$DEPLOY_PROXMOXVE_SERVER_PORT" ]; then
|
|
|
|
_target_port="8006"
|
|
|
|
else
|
2022-06-11 19:49:31 +02:00
|
|
|
_target_port="$DEPLOY_PROXMOXVE_SERVER_PORT"
|
2022-06-20 04:01:56 +02:00
|
|
|
_savedeployconf DEPLOY_PROXMOXVE_SERVER_PORT "$DEPLOY_PROXMOXVE_SERVER_PORT"
|
2022-06-11 19:49:31 +02:00
|
|
|
fi
|
2022-06-18 21:40:05 +02:00
|
|
|
_debug2 DEPLOY_PROXMOXVE_SERVER_PORT "$_target_port"
|
2022-06-11 19:49:31 +02:00
|
|
|
|
2022-06-18 21:40:05 +02:00
|
|
|
_getdeployconf DEPLOY_PROXMOXVE_NODE_NAME
|
|
|
|
if [ -z "$DEPLOY_PROXMOXVE_NODE_NAME" ]; then
|
2022-06-23 08:08:24 +02:00
|
|
|
_node_name=$(echo "$_target_hostname" | cut -d. -f1)
|
2022-06-18 21:40:05 +02:00
|
|
|
else
|
|
|
|
_node_name="$DEPLOY_PROXMOXVE_NODE_NAME"
|
2022-06-20 04:01:56 +02:00
|
|
|
_savedeployconf DEPLOY_PROXMOXVE_NODE_NAME "$DEPLOY_PROXMOXVE_NODE_NAME"
|
2022-06-11 19:49:31 +02:00
|
|
|
fi
|
2022-06-18 21:40:05 +02:00
|
|
|
_debug2 DEPLOY_PROXMOXVE_NODE_NAME "$_node_name"
|
2022-06-11 19:49:31 +02:00
|
|
|
|
|
|
|
# Complete URL.
|
|
|
|
_target_url="https://${_target_hostname}:${_target_port}/api2/json/nodes/${_node_name}/certificates/custom"
|
2022-06-18 21:40:05 +02:00
|
|
|
_debug TARGET_URL "$_target_url"
|
2022-06-11 19:49:31 +02:00
|
|
|
|
|
|
|
# More "sane" defaults.
|
2022-06-18 21:40:05 +02:00
|
|
|
_getdeployconf DEPLOY_PROXMOXVE_USER
|
|
|
|
if [ -z "$DEPLOY_PROXMOXVE_USER" ]; then
|
|
|
|
_proxmoxve_user="root"
|
|
|
|
else
|
2022-06-11 19:49:31 +02:00
|
|
|
_proxmoxve_user="$DEPLOY_PROXMOXVE_USER"
|
2022-06-20 04:01:56 +02:00
|
|
|
_savedeployconf DEPLOY_PROXMOXVE_USER "$DEPLOY_PROXMOXVE_USER"
|
2022-06-11 19:49:31 +02:00
|
|
|
fi
|
2022-06-20 04:01:56 +02:00
|
|
|
_debug2 DEPLOY_PROXMOXVE_USER "$_proxmoxve_user"
|
2022-06-11 19:49:31 +02:00
|
|
|
|
2022-06-18 21:40:05 +02:00
|
|
|
_getdeployconf DEPLOY_PROXMOXVE_USER_REALM
|
|
|
|
if [ -z "$DEPLOY_PROXMOXVE_USER_REALM" ]; then
|
|
|
|
_proxmoxve_user_realm="pam"
|
|
|
|
else
|
2022-06-11 19:49:31 +02:00
|
|
|
_proxmoxve_user_realm="$DEPLOY_PROXMOXVE_USER_REALM"
|
2022-06-23 05:19:12 +02:00
|
|
|
_savedeployconf DEPLOY_PROXMOXVE_USER_REALM "$DEPLOY_PROXMOXVE_USER_REALM"
|
2022-06-11 19:49:31 +02:00
|
|
|
fi
|
2022-06-18 21:40:05 +02:00
|
|
|
_debug2 DEPLOY_PROXMOXVE_USER_REALM "$_proxmoxve_user_realm"
|
2022-06-11 19:49:31 +02:00
|
|
|
|
2022-06-18 21:40:05 +02:00
|
|
|
_getdeployconf DEPLOY_PROXMOXVE_API_TOKEN_NAME
|
|
|
|
if [ -z "$DEPLOY_PROXMOXVE_API_TOKEN_NAME" ]; then
|
|
|
|
_proxmoxve_api_token_name="acme"
|
|
|
|
else
|
2022-06-11 19:49:31 +02:00
|
|
|
_proxmoxve_api_token_name="$DEPLOY_PROXMOXVE_API_TOKEN_NAME"
|
2022-06-20 04:01:56 +02:00
|
|
|
_savedeployconf DEPLOY_PROXMOXVE_API_TOKEN_NAME "$DEPLOY_PROXMOXVE_API_TOKEN_NAME"
|
2022-06-11 19:49:31 +02:00
|
|
|
fi
|
2022-06-18 21:40:05 +02:00
|
|
|
_debug2 DEPLOY_PROXMOXVE_API_TOKEN_NAME "$_proxmoxve_api_token_name"
|
2022-06-11 19:49:31 +02:00
|
|
|
|
|
|
|
# This is required.
|
2022-06-18 21:40:05 +02:00
|
|
|
_getdeployconf DEPLOY_PROXMOXVE_API_TOKEN_KEY
|
2022-06-23 08:08:24 +02:00
|
|
|
if [ -z "$DEPLOY_PROXMOXVE_API_TOKEN_KEY" ]; then
|
2022-06-11 19:49:31 +02:00
|
|
|
_err "API key not provided."
|
|
|
|
return 1
|
2022-06-18 21:40:05 +02:00
|
|
|
else
|
|
|
|
_proxmoxve_api_token_key="$DEPLOY_PROXMOXVE_API_TOKEN_KEY"
|
2022-06-20 04:01:56 +02:00
|
|
|
_savedeployconf DEPLOY_PROXMOXVE_API_TOKEN_KEY "$DEPLOY_PROXMOXVE_API_TOKEN_KEY"
|
2022-06-11 19:49:31 +02:00
|
|
|
fi
|
2022-06-18 21:40:05 +02:00
|
|
|
_debug2 DEPLOY_PROXMOXVE_API_TOKEN_KEY _proxmoxve_api_token_key
|
2022-06-11 19:49:31 +02:00
|
|
|
|
|
|
|
# PVE API Token header value. Used in "Authorization: PVEAPIToken".
|
|
|
|
_proxmoxve_header_api_token="${_proxmoxve_user}@${_proxmoxve_user_realm}!${_proxmoxve_api_token_name}=${_proxmoxve_api_token_key}"
|
2022-06-18 21:40:05 +02:00
|
|
|
_debug2 "Auth Header" _proxmoxve_header_api_token
|
2022-06-11 19:49:31 +02:00
|
|
|
|
2022-06-18 18:18:33 +02:00
|
|
|
# Ugly. I hate putting heredocs inside functions because heredocs don't
|
|
|
|
# account for whitespace correctly but it _does_ work and is several times
|
|
|
|
# cleaner than anything else I had here.
|
2022-06-11 19:49:31 +02:00
|
|
|
#
|
2022-06-18 18:18:33 +02:00
|
|
|
# This dumps the json payload to a variable that should be passable to the
|
|
|
|
# _psot function.
|
2022-06-23 08:08:24 +02:00
|
|
|
_json_payload=$(
|
|
|
|
cat << HEREDOC
|
2022-06-11 19:49:31 +02:00
|
|
|
{
|
2022-06-18 23:00:36 +02:00
|
|
|
"certificates": "$(tr '\n' ':' < "$_cfullchain" | sed 's/:/\\n/g')",
|
2022-06-18 22:56:46 +02:00
|
|
|
"key": "$(tr '\n' ':' < "$_ckey" |sed 's/:/\\n/g')",
|
2022-06-11 19:49:31 +02:00
|
|
|
"node":"$_node_name",
|
|
|
|
"restart":"1",
|
|
|
|
"force":"1"
|
|
|
|
}
|
|
|
|
HEREDOC
|
2022-06-23 08:08:24 +02:00
|
|
|
)
|
2022-06-18 22:43:25 +02:00
|
|
|
_debug2 Payload "$_json_payload"
|
2022-06-23 08:08:24 +02:00
|
|
|
|
2022-06-11 19:49:31 +02:00
|
|
|
# Push certificates to server.
|
2022-06-18 18:18:33 +02:00
|
|
|
export _HTTPS_INSECURE=1
|
2022-06-18 18:21:14 +02:00
|
|
|
export _H1="Authorization: PVEAPIToken=${_proxmoxve_header_api_token}"
|
2022-06-19 07:46:10 +02:00
|
|
|
_post "$_json_payload" "$_target_url" "" POST "application/json"
|
2022-06-11 19:49:31 +02:00
|
|
|
|
|
|
|
}
|