Read the config with Viper and environment variables, on Go 1.27.4
The config loader uses Viper instead of BurntSushi/toml. Every setting can also come from UBERBRINGER_<SECTION>_<KEY>, which wins over the file, and UBERBRINGER_CONFIG sets the default of --config; set empty, no file is read, so a container can run from the environment alone. Each setting is bound to its own variable, so a variable named after a section changes nothing. The file is parsed with go-toml and checked as written: unknown keys, empty tables and keys that differ only in case are errors, a UTF-8 BOM is skipped and a UTF-16 file is refused. Values are decoded strictly: durations are strings, numbers decimal, flags true or false, and a fraction for a whole number is refused. Errors name the file, the line of a syntax error and the variables that were set, never their values. `uberbringer config get <key>` prints one setting as the daemon would use it. install.sh reads body_limit, bind_ip and port through it, so the config is checked before the service restarts and Caddy proxies to the configured address. The script then waits for the daemon to answer, downloads Caddy's key and source list before replacing them, requires Caddy's own repository, keeps the Caddyfile during dpkg upgrades, detects an existing import loosely, logs Caddy's other errors, drops query strings from the access log and validates the domain and printer address more tightly. go.mod names Go 1.27.4 with no toolchain line, and the workflows check against the go line. The changelog folds the unreleased entries into 0.2.0, which is not tagged yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
92dcacb3b9
commit
eacd0bfd27
18 files changed
+1244
-248
No files matched your search
+126
-57
@@ -5,7 +5,8 @@
|
||||
#
|
||||
# Usage: sudo ./install.sh <domain> <acme-email> <printer-address>
|
||||
#
|
||||
# Installs the latest release. To install a given release instead, name its tag:
|
||||
# Installs the latest release. To install a given release, v0.2.0 or later,
|
||||
# name its tag:
|
||||
# sudo UBERBRINGER_VERSION=v0.2.0 ./install.sh <domain> <acme-email> <printer-address>
|
||||
# Re-running the script upgrades the binary, the systemd unit and Caddy,
|
||||
# rewrites Überbringer's Caddy site and restarts the service. It keeps the
|
||||
@@ -26,8 +27,6 @@ SERVICE_USER=uberbringer
|
||||
# /etc/systemd/system take precedence over it, so operator changes belong in
|
||||
# drop-ins there.
|
||||
UNIT_FILE=/usr/local/lib/systemd/system/uberbringer.service
|
||||
# The daemon's default webserver.body_limit, used when the config sets none.
|
||||
DEFAULT_BODY_LIMIT=4194304
|
||||
CADDYFILE=/etc/caddy/Caddyfile
|
||||
CADDY_SITE=/etc/caddy/uberbringer.caddy
|
||||
CADDY_LOG=/var/log/caddy/uberbringer-access.log
|
||||
@@ -41,6 +40,9 @@ RELEASE_URL="https://git.anomalous.dev/57_Wolve/uberbringer/releases/download/${
|
||||
|
||||
CONFIG_CREATED=0
|
||||
WORK_DIR=""
|
||||
# Set by read_settings from the config, as the daemon reads it.
|
||||
BODY_LIMIT=""
|
||||
UPSTREAM=""
|
||||
|
||||
if [[ "$EUID" -ne 0 ]]; then
|
||||
echo -e "\e[31m[FATAL]\e[39m This script must be run as root." >&2
|
||||
@@ -54,17 +56,20 @@ fi
|
||||
|
||||
# The domain and the email go unquoted into the Caddy site file, and the
|
||||
# printer address into a TOML string, so each is limited to the characters
|
||||
# its kind of value needs.
|
||||
if [[ ! "$DOMAIN" =~ ^[A-Za-z0-9.-]+$ ]]; then
|
||||
echo "Invalid domain: $DOMAIN" >&2
|
||||
# its kind of value needs. The domain must be a DNS name with at least one
|
||||
# dot that a certificate can be issued for, so not an IP address.
|
||||
DOMAIN_RE='^[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)+$'
|
||||
if [[ ! "$DOMAIN" =~ $DOMAIN_RE || "$DOMAIN" =~ ^[0-9.]+$ ]]; then
|
||||
echo "Invalid domain: $DOMAIN (expected a DNS name such as printer.example.com)" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ ! "$ACME_EMAIL" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+$ ]]; then
|
||||
echo "Invalid email address: $ACME_EMAIL" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ ! "$PRINTER_ADDRESS" =~ ^[A-Za-z0-9.:-]+$ ]]; then
|
||||
echo "Invalid printer address: $PRINTER_ADDRESS (expected an IP address or host name)" >&2
|
||||
# A single colon is a host:port pair; an IPv6 address has at least two.
|
||||
if [[ ! "$PRINTER_ADDRESS" =~ ^[A-Za-z0-9.:-]+$ || "$PRINTER_ADDRESS" =~ ^[^:]*:[^:]*$ ]]; then
|
||||
echo "Invalid printer address: $PRINTER_ADDRESS (expected an IP address or host name without a port; a port other than 9100 goes in printer.port in the config)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -115,6 +120,15 @@ detect_arch() {
|
||||
esac
|
||||
}
|
||||
|
||||
# apt_install installs or upgrades packages without asking anything. Its
|
||||
# output goes to the log, where a question would go unseen, so dpkg keeps a
|
||||
# changed configuration file such as the Caddyfile instead of asking, and
|
||||
# nothing is read from the terminal.
|
||||
apt_install() {
|
||||
apt-get install -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold \
|
||||
"$@" < /dev/null >> "$LOG" 2>&1
|
||||
}
|
||||
|
||||
install_base_packages() {
|
||||
info "Installing curl and gnupg..."
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
@@ -122,7 +136,7 @@ install_base_packages() {
|
||||
# packages below are installed or still installable, so only the install
|
||||
# is fatal.
|
||||
apt-get update >> "$LOG" 2>&1 || warn "apt-get update failed; trying the install anyway (see $LOG)."
|
||||
apt-get install -y curl gnupg >> "$LOG" 2>&1 || fatal "apt-get install failed; see $LOG."
|
||||
apt_install curl gnupg || fatal "apt-get install failed; see $LOG."
|
||||
|
||||
require curl
|
||||
require sha256sum
|
||||
@@ -154,13 +168,15 @@ download_release() {
|
||||
tar -xf "$WORK_DIR/$archive" -C "$WORK_DIR/release"
|
||||
[[ -f "$WORK_DIR/release/uberbringer" ]] || fatal "$archive does not contain the uberbringer binary."
|
||||
[[ -f "$WORK_DIR/release/packaging/uberbringer.service" ]] ||
|
||||
fatal "$archive does not contain packaging/uberbringer.service."
|
||||
fatal "$archive does not contain packaging/uberbringer.service; this script installs v0.2.0 or later."
|
||||
}
|
||||
|
||||
install_binary() {
|
||||
install -m 0755 "$WORK_DIR/release/uberbringer" "$BIN"
|
||||
local version
|
||||
# Run from its installed path: WORK_DIR may be on a noexec /tmp.
|
||||
info "Installed $("$BIN" version)"
|
||||
version="$("$BIN" version)" || fatal "$BIN does not run on this host."
|
||||
info "Installed $version"
|
||||
}
|
||||
|
||||
create_service_user() {
|
||||
@@ -216,57 +232,86 @@ write_initial_token() {
|
||||
debug "Wrote the first API token to $TOKEN_FILE."
|
||||
}
|
||||
|
||||
# install_service installs the unit and restarts the service, which reads its
|
||||
# config only at startup.
|
||||
# config_get prints one setting as the installed binary reads it from the
|
||||
# config. It fails, with the reason on stderr, when the config does not
|
||||
# load.
|
||||
config_get() {
|
||||
"$BIN" config get --config "$CONFIG" "$1"
|
||||
}
|
||||
|
||||
# read_settings checks the config before the service is restarted, and takes
|
||||
# from it the body limit and the address Caddy proxies to.
|
||||
read_settings() {
|
||||
local bind_ip port
|
||||
BODY_LIMIT="$(config_get webserver.body_limit)" ||
|
||||
fatal "$CONFIG does not load (the reason is above). Fix it and rerun; the service was not restarted."
|
||||
bind_ip="$(config_get webserver.bind_ip)"
|
||||
port="$(config_get webserver.port)"
|
||||
case "$bind_ip" in
|
||||
0.0.0.0) UPSTREAM="127.0.0.1:$port" ;;
|
||||
::) UPSTREAM="[::1]:$port" ;;
|
||||
*:*) UPSTREAM="[$bind_ip]:$port" ;;
|
||||
*) UPSTREAM="$bind_ip:$port" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# install_service installs the unit, restarts the service, which reads its
|
||||
# config only at startup, and waits for it to answer.
|
||||
install_service() {
|
||||
install -D -m 0644 "$WORK_DIR/release/packaging/uberbringer.service" "$UNIT_FILE"
|
||||
systemctl daemon-reload
|
||||
systemctl enable uberbringer >> "$LOG" 2>&1
|
||||
systemctl restart uberbringer
|
||||
systemctl enable uberbringer >> "$LOG" 2>&1 || fatal "Enabling uberbringer.service failed; see $LOG."
|
||||
systemctl restart uberbringer || fatal "Restarting uberbringer.service failed; see 'journalctl -u uberbringer'."
|
||||
wait_for_daemon
|
||||
}
|
||||
|
||||
# wait_for_daemon waits up to 15 seconds for the daemon to answer. systemd
|
||||
# reports the service started as soon as the process runs, before it has
|
||||
# opened its port or failed.
|
||||
wait_for_daemon() {
|
||||
local attempt
|
||||
for attempt in {1..30}; do
|
||||
if curl -fs -o /dev/null --max-time 2 "http://$UPSTREAM/"; then
|
||||
debug "Überbringer answered on $UPSTREAM after $attempt attempts."
|
||||
return
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
fatal "Überbringer did not answer on $UPSTREAM; see 'journalctl -u uberbringer'."
|
||||
}
|
||||
|
||||
# install_caddy installs or upgrades Caddy's Debian package from Caddy's
|
||||
# signed apt repository, as Caddy's install guide describes.
|
||||
# signed apt repository. The key and the source list are fetched into
|
||||
# WORK_DIR first, so a failed download leaves the installed ones as they
|
||||
# were.
|
||||
install_caddy() {
|
||||
info "Installing Caddy from its apt repository..."
|
||||
# Optional helpers from Caddy's guide; gnupg and curl are what the steps
|
||||
# below need.
|
||||
apt-get install -y debian-keyring debian-archive-keyring apt-transport-https >> "$LOG" 2>&1 ||
|
||||
# Optional keyring helpers; curl and gnupg are what the steps below need.
|
||||
apt_install debian-keyring debian-archive-keyring apt-transport-https ||
|
||||
debug "Optional keyring packages are unavailable; continuing."
|
||||
curl -fsSL --proto '=https' --tlsv1.2 "$CADDY_REPO/gpg.key" |
|
||||
gpg --dearmor --yes -o "$CADDY_KEYRING" ||
|
||||
curl -fsSL --proto '=https' --tlsv1.2 -o "$WORK_DIR/caddy.key" "$CADDY_REPO/gpg.key" &&
|
||||
gpg --dearmor --yes -o "$WORK_DIR/caddy.gpg" "$WORK_DIR/caddy.key" ||
|
||||
fatal "Couldn't fetch Caddy's signing key."
|
||||
curl -fsSL --proto '=https' --tlsv1.2 "$CADDY_REPO/debian.deb.txt" > "$CADDY_SOURCES" ||
|
||||
curl -fsSL --proto '=https' --tlsv1.2 -o "$WORK_DIR/caddy.list" "$CADDY_REPO/debian.deb.txt" ||
|
||||
fatal "Couldn't fetch Caddy's apt source list."
|
||||
chmod o+r "$CADDY_KEYRING" "$CADDY_SOURCES"
|
||||
apt-get update >> "$LOG" 2>&1 || warn "apt-get update failed; trying the install anyway (see $LOG)."
|
||||
apt-get install -y caddy >> "$LOG" 2>&1 || fatal "Installing Caddy failed; see $LOG."
|
||||
install -m 0644 "$WORK_DIR/caddy.gpg" "$CADDY_KEYRING"
|
||||
install -m 0644 "$WORK_DIR/caddy.list" "$CADDY_SOURCES"
|
||||
# Only Caddy's source is updated, and a failure is fatal: without its
|
||||
# index, apt would install the distribution's older caddy package.
|
||||
apt-get update -o Dir::Etc::sourcelist="$CADDY_SOURCES" -o Dir::Etc::sourceparts=- \
|
||||
-o APT::Get::List-Cleanup=0 >> "$LOG" 2>&1 ||
|
||||
fatal "Couldn't read Caddy's apt repository; see $LOG."
|
||||
apt_install caddy || fatal "Installing Caddy failed; see $LOG."
|
||||
require caddy
|
||||
}
|
||||
|
||||
# body_limit prints webserver.body_limit from the config, so Caddy accepts
|
||||
# exactly the request bodies the daemon does.
|
||||
body_limit() {
|
||||
local limit
|
||||
limit="$(awk '
|
||||
/^[ \t]*\[/ { section = $0; gsub(/[][ \t]/, "", section); next }
|
||||
section == "webserver" && /^[ \t]*body_limit[ \t]*=/ {
|
||||
sub(/^[^=]*=[ \t]*/, ""); sub(/[ \t#].*$/, ""); print; exit
|
||||
}' "$CONFIG")"
|
||||
limit="${limit//_/}"
|
||||
if [[ "$limit" =~ ^[0-9]+$ ]] && ((limit > 0)); then
|
||||
echo "$limit"
|
||||
else
|
||||
echo "$DEFAULT_BODY_LIMIT"
|
||||
fi
|
||||
}
|
||||
|
||||
# managed_caddyfile prints the Caddyfile this script writes. Once the
|
||||
# operator edits it, it no longer matches and is kept.
|
||||
managed_caddyfile() {
|
||||
printf '%s\n%s\n%s\n' \
|
||||
"# Written by Überbringer's install.sh. Your edits are kept: a re-run" \
|
||||
"# only adds the import below if it is missing." \
|
||||
printf '%s\n' \
|
||||
"# Written by Überbringer's install.sh. Your edits are kept: a re-run only" \
|
||||
"# adds the import below if nothing imports it. Global options go at the" \
|
||||
"# top, above every import and site." \
|
||||
"$CADDY_IMPORT"
|
||||
}
|
||||
|
||||
@@ -281,14 +326,24 @@ caddyfile_is_package_default() {
|
||||
[[ "$current" == "$packaged" ]]
|
||||
}
|
||||
|
||||
# caddyfile_imports_site reports whether a line of the Caddyfile imports the
|
||||
# site file, by its path or by a glob over /etc/caddy.
|
||||
caddyfile_imports_site() {
|
||||
tr -d '\r' < "$CADDYFILE" |
|
||||
grep -qE '^[[:space:]]*import[[:space:]]+/etc/caddy/(uberbringer\.caddy|\*\.caddy|\*)([[:space:]]|#|$)'
|
||||
}
|
||||
|
||||
# validate_caddy checks the configuration as the caddy user, with its home,
|
||||
# so the files validation opens or creates are the caddy user's, as they are
|
||||
# for caddy.service.
|
||||
# for caddy.service. Caddy's output goes to WORK_DIR and the log.
|
||||
validate_caddy() {
|
||||
local home
|
||||
local home status=0
|
||||
home="$(getent passwd caddy | cut -d: -f6)"
|
||||
runuser -u caddy -- env HOME="${home:-/var/lib/caddy}" \
|
||||
caddy validate --config "$CADDYFILE" --adapter caddyfile >> "$LOG" 2>&1
|
||||
caddy validate --config "$CADDYFILE" --adapter caddyfile > "$WORK_DIR/caddy-validate.log" 2>&1 ||
|
||||
status=$?
|
||||
cat "$WORK_DIR/caddy-validate.log" >> "$LOG"
|
||||
return "$status"
|
||||
}
|
||||
|
||||
# write_caddy_site writes Überbringer's site file and makes the Caddyfile
|
||||
@@ -307,8 +362,6 @@ write_caddy_site() {
|
||||
install -o caddy -g caddy -m 0640 /dev/null "$CADDY_LOG"
|
||||
fi
|
||||
|
||||
local limit
|
||||
limit="$(body_limit)"
|
||||
cat <<EOT > "$CADDY_SITE"
|
||||
# Written by Überbringer's install.sh, which rewrites it on every run.
|
||||
(uberbringer_headers) {
|
||||
@@ -326,11 +379,13 @@ $DOMAIN {
|
||||
|
||||
request_body {
|
||||
# webserver.body_limit from $CONFIG, in bytes.
|
||||
max_size $limit
|
||||
max_size $BODY_LIMIT
|
||||
}
|
||||
|
||||
reverse_proxy 127.0.0.1:8080 {
|
||||
# Überbringer trusts X-Real-IP from 127.0.0.1 for per-client rate limits.
|
||||
# webserver.bind_ip and webserver.port from $CONFIG.
|
||||
reverse_proxy $UPSTREAM {
|
||||
# Überbringer takes the client's address from X-Real-IP for
|
||||
# per-client rate limits.
|
||||
header_up X-Real-IP {remote_host}
|
||||
}
|
||||
|
||||
@@ -345,8 +400,19 @@ $DOMAIN {
|
||||
respond \`{"error":"request body is too large","code":"body_too_large"}\` 413
|
||||
}
|
||||
|
||||
# Any other error, such as a 502 while Überbringer restarts, is passed
|
||||
# on, so Caddy logs its cause.
|
||||
handle_errors {
|
||||
error "{err.message}" {err.status_code}
|
||||
}
|
||||
|
||||
log {
|
||||
output file $CADDY_LOG
|
||||
# Query strings are dropped: a misdirected client may put a token
|
||||
# in one.
|
||||
format filter {
|
||||
request>uri regexp "[?].*\$" ""
|
||||
}
|
||||
}
|
||||
}
|
||||
EOT
|
||||
@@ -362,7 +428,7 @@ EOT
|
||||
fi
|
||||
managed_caddyfile > "$CADDYFILE"
|
||||
chmod 0644 "$CADDYFILE"
|
||||
elif ! grep -qxF "$CADDY_IMPORT" "$CADDYFILE"; then
|
||||
elif ! caddyfile_imports_site; then
|
||||
cp -p "$CADDYFILE" "$CADDYFILE.bak"
|
||||
printf '\n%s\n' "$CADDY_IMPORT" >> "$CADDYFILE"
|
||||
info "Added '$CADDY_IMPORT' to your $CADDYFILE (previous copy kept as $CADDYFILE.bak)."
|
||||
@@ -371,18 +437,20 @@ EOT
|
||||
if ! validate_caddy; then
|
||||
if [[ -f "$backup/site" ]]; then cp -p "$backup/site" "$CADDY_SITE"; else rm -f "$CADDY_SITE"; fi
|
||||
if [[ -f "$backup/Caddyfile" ]]; then cp -p "$backup/Caddyfile" "$CADDYFILE"; else rm -f "$CADDYFILE"; fi
|
||||
fatal "Caddy rejected the configuration, so $CADDYFILE and $CADDY_SITE were put back as they were (details in $LOG). Überbringer's site must be defined only in $CADDY_SITE."
|
||||
local reason
|
||||
reason="$(grep -m1 '^Error:' "$WORK_DIR/caddy-validate.log" || true)"
|
||||
fatal "Caddy rejected the configuration, so $CADDYFILE and $CADDY_SITE were put back as they were. ${reason:-See $LOG for the error from Caddy.}"
|
||||
fi
|
||||
}
|
||||
|
||||
start_caddy() {
|
||||
systemctl enable caddy >> "$LOG" 2>&1
|
||||
systemctl enable caddy >> "$LOG" 2>&1 || fatal "Enabling caddy.service failed; see $LOG."
|
||||
if systemctl is-active --quiet caddy; then
|
||||
systemctl reload caddy || fatal "Reloading Caddy failed; see 'journalctl -u caddy'."
|
||||
else
|
||||
systemctl restart caddy || fatal "Caddy did not start; see 'journalctl -u caddy' (ports 80 and 443 must be free)."
|
||||
fi
|
||||
info "Caddy is requesting a certificate for $DOMAIN. If https://$DOMAIN does not answer within a minute, see 'journalctl -u caddy': the domain's DNS must point here, and ports 80 and 443 must be reachable."
|
||||
info "Caddy serves https://$DOMAIN and obtains or renews its certificate itself. If the site does not answer within a minute, see 'journalctl -u caddy': the domain's DNS must point here, and ports 80 and 443 must be reachable."
|
||||
}
|
||||
|
||||
token_notice() {
|
||||
@@ -412,6 +480,7 @@ main() {
|
||||
create_service_user
|
||||
write_config
|
||||
write_initial_token
|
||||
read_settings
|
||||
install_service
|
||||
|
||||
install_caddy
|
||||
|
||||
Reference in new issue
Block a user