From e94b13b2de8b1901a81a09f8afbf8bed5a5be7b2 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Fri, 29 Mar 2024 10:20:53 +0100 Subject: [PATCH] new: [docker-host] add ``ntfy`` installation and connection --- docker-host/hooks/install.d/90-ntfy.sh | 89 +++++++++++++++++++++++++ docker-host/src/etc/ssh/ntfy-key | Bin 0 -> 1458 bytes 2 files changed, 89 insertions(+) create mode 100755 docker-host/hooks/install.d/90-ntfy.sh create mode 100644 docker-host/src/etc/ssh/ntfy-key diff --git a/docker-host/hooks/install.d/90-ntfy.sh b/docker-host/hooks/install.d/90-ntfy.sh new file mode 100755 index 0000000..b87976e --- /dev/null +++ b/docker-host/hooks/install.d/90-ntfy.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +set -eux + + +NTFY_BROKER="${NTFY_BROKER:-core-01.0k.io}" + +## Copy Ntfy key to root/.ssh/ + +umask 077 +ntfy_key="src/etc/ssh/ntfy-key" +if [ ! -f "$ntfy_key" ]; then + echo "Error: ntfy key not found" >&2 + exit 1 +fi + +ntfy_key_dest=/etc/ssh/ntfy-key +if [ ! -f "$ntfy_key_dest" ]; then + cat "$ntfy_key" | + gpg -d --batch --yes --passphrase 'uniquepass' > "$ntfy_key_dest" || { + echo "Error while unpacking ntfy key to '${ntfy_key_dest}'" >&2 + exit 1 + } +fi + +## Request token to ntfy server and add to config file +known_host="/root/.ssh/known_hosts" +if ! ssh-keygen -F "$NTFY_BROKER" -f "$known_host" >/dev/null; then + ssh-keyscan -H "$NTFY_BROKER" >> "$known_host" || { + echo "Error while adding '$NTFY_BROKER' to known_hosts" >&2 + exit 1 + } +fi + +config_file="/etc/ntfy/ntfy.conf" +mkdir -p "${config_file%/*}" +if ! [ -f "$config_file" ]; then + touch "$config_file" || { + echo "Error: couldn’t create config file $config_file" >&2; + exit 1 + } +fi + +LOGIN="" +PASSWORD="" +source "$config_file" + +## Note that we reauire the forcing of stdin to /dev/null to avoid +## the rest of the script to be vacuumed by the ssh command. +## This effect will only happen when launching this script in special +## conditions involving stdin. +cred=$(ssh -i "$ntfy_key_dest" ntfy@"${NTFY_BROKER}" \ + request-token "$LOGIN" "$PASSWORD" &2 + exit 1 +} + +## XXXvlab: ideally it should be received from the last call +server="https://ntfy.0k.io/" +login=$(printf "%q" "${cred%$'\n'*}") +password=$(printf "%q" "${cred#*$'\n'}") + +## check if password doesn't contain '%' + +for var in server login password; do + if [ -z "${!var}" ] || [[ "${!var}" == *$'\n'* ]]; then + echo "Error: couldn't infer $var from ntfy server. Received:" >&2 + printf "%s" "$cred" | sed -r 's/^/ | /g' >&2 + exit 1 + fi + if [[ "${!var}" == *%* ]]; then + ## We need a separator char for sed replacement in the config file + echo "Error: forbidden character '%' found in $var" >&2 + exit 1 + fi + if grep -qE "^${var^^}=" "$config_file"; then + sed -ri "s%^${var^^}=.*$%${var^^}=\"${!var}\"%g" "$config_file" + else + echo "${var^^}=\"${!var}\"" >> "$config_file" + fi +done + + +if ! [ -f "/etc/ntfy/topics.yml" ]; then + cat < /etc/ntfy/topics.yml +main: + - \${LOGIN}_main +EOF +fi diff --git a/docker-host/src/etc/ssh/ntfy-key b/docker-host/src/etc/ssh/ntfy-key new file mode 100644 index 0000000000000000000000000000000000000000..b971d3dd25b80ac1675c79abf36e1a3174cedafb GIT binary patch literal 1458 zcmV;j1x@;l4Fm}T0^7du>h-j)DF4#x0gDX*#5D)Ew5*OOtRQwr5acd>9IbY7=o&Sk z$(umP-(G~yHzZ^vvzPBT+`m@C1bRocFS?|1&3+F}US|*zaXMR=qmJ!tvvma4w&dRW z7yuyasRc|=`PiYHumZ82AMn5c3!whVlS$+46sfW?R~D6i#Oc?3HNE0R=;xkqp3cBt zIpclUIc}+$2=d0lZAjNk-E+uKb;p;_?(4IrR{IsCcDkN510bF>G5Y(+KUa1Zaj-LF zJSVu?a8S7By-%5u1`-l=G&*!PYfW%}y%a+O@6YHX-y#A8Uu=tW0;28#WY@p&8)KBU zV1rY-kepWf?U4@b2jzexFfEAj6~y{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