Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ while [[ $# -gt 0 ]]; do
esac
done

# PLUGIN_TTY: the tests answer from a file. Opened, not tested with -r:
# /dev/tty is mode 0666 even where there is no terminal to open (cron, ssh
# without -t), and a read that fails there takes every default.
unattended() {
[ "$ASSUME_YES" = true ] || ! { : <"${PLUGIN_TTY:-/dev/tty}"; } 2>/dev/null
}

ask() {
local prompt="$1" default="$2" answer
if [ "$ASSUME_YES" = true ] || [ ! -r /dev/tty ]; then
if unattended; then
echo "$default"; return
fi
printf " %s [%s]: " "$prompt" "$default" > /dev/tty
read -r answer < /dev/tty || answer=""
read -r -p " $prompt [$default]: " answer < "${PLUGIN_TTY:-/dev/tty}" || answer=""
echo "${answer:-$default}"
}

Expand Down Expand Up @@ -436,15 +442,25 @@ if [ -n "$DOMAIN_VAR" ] && [ -n "$DOMAIN_FILE" ]; then
step "Domain"

CURRENT="$(grep -h "^$DOMAIN_VAR=" "$TARGET/$DOMAIN_FILE" 2>/dev/null | cut -d '=' -f2- || true)"
SHIPPED="$(grep -h "^$DOMAIN_VAR=" "$TARGET/$DOMAIN_FILE.example" 2>/dev/null | cut -d '=' -f2- || true)"
if [ -z "$DOMAIN" ]; then
if [ -n "$CURRENT" ] && [ -n "$WEB_DOMAIN" ] \
&& { [[ "$CURRENT" == *"$WEB_DOMAIN" ]] || cookie_domain_covers "$COOKIE_DOMAIN" "$CURRENT"; }; then
if [ -n "$CURRENT" ] && [ -n "$COOKIE_DOMAIN" ] && cookie_domain_covers "$COOKIE_DOMAIN" "$CURRENT"; then
default_domain="$CURRENT"
elif [ -n "$CURRENT" ] && [ "$CURRENT" != "$SHIPPED" ] && unattended; then
# An earlier install chose it, and its DNS and the panel's
# registration point at it: only a person moves it. The warning
# below still says the cookie never reaches it.
default_domain="$CURRENT"
elif [ -n "$WEB_DOMAIN" ]; then
default_domain="$DOMAIN_PREFIX.$WEB_DOMAIN"
else
default_domain="$CURRENT"
fi
if [ -n "$CURRENT" ] && [ "$CURRENT" != "$SHIPPED" ] && [ "$default_domain" != "$CURRENT" ]; then
warn "$CURRENT, the current host, is outside the session cookie's domain"
warn "(.$COOKIE_DOMAIN), so signed-in calls from the panel fail there. Moving it"
warn "also means DNS for the new host and pressing Detect on it under Panel Plugins."
fi
DOMAIN="$(ask "Host for $NAME" "$default_domain")"
fi

Expand Down Expand Up @@ -594,10 +610,12 @@ if [ -n "$DOMAIN" ]; then
echo " 2. The certificate issues once that DNS is live:"
echo " kubectl -n 5stack get certificate $CERT_NAME -w"
fi
echo " 3. Register it: Settings → Application → Panel Plugins → Add,"
echo " 3. Register it in Settings → Application → Panel Plugins:"
echo " Turn on the Panel Plugins switch, then Add Panel Plugin,"
echo " paste https://$DOMAIN and press Detect."
else
echo " Register it: Settings → Application → Panel Plugins → Add, then Detect."
echo " Register it in Settings → Application → Panel Plugins:"
echo " Turn on the Panel Plugins switch, then Add Panel Plugin and press Detect."
fi
echo
echo " ${C_DIM}Re-run ./plugin.sh with the same repo to upgrade; your answers are kept.${C_RESET}"
Expand Down
65 changes: 65 additions & 0 deletions tests/scripts/plugin_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ run_plugin() {
|| fail "plugin.sh exited $?: $(tail -n 20 "$WORK_DIR/plugin-output")"
}

# Enter at every question, as a person at a terminal would.
run_plugin_interactively() {
printf '\n\n\n\n' >"$WORK_DIR/tty"
(cd "$WORK_DIR" && PATH="$STUB_BIN:$PATH" PLUGIN_TTY="$WORK_DIR/tty" bash "$PANEL/plugin.sh" "$PLUGIN" --dry-run) \
>"$WORK_DIR/plugin-output" 2>&1 \
|| fail "plugin.sh exited $?: $(tail -n 20 "$WORK_DIR/plugin-output")"
}

plugin_env() {
grep "^$1=" "$PANEL/custom/hello/k8s/base/${2:-hello}.env" 2>/dev/null || true
}
Expand Down Expand Up @@ -98,6 +106,63 @@ test_a_rerun_keeps_a_host_inside_the_cookie_domain() {
assert_eq "$(plugin_env HELLO_DOMAIN)" "HELLO_DOMAIN=skins.example.com"
}

# A sibling host the cookie never reaches was offered straight back, because
# `inventory-cs-panel.example.com` ends in the text `cs-panel.example.com`.
test_an_interactive_rerun_offers_a_host_the_cookie_reaches() {
make_panel cs-panel.example.com
make_plugin
run_plugin --domain hello-cs-panel.example.com
run_plugin_interactively
assert_eq "$(plugin_env HELLO_DOMAIN)" "HELLO_DOMAIN=hello.cs-panel.example.com"
}

# Its DNS and the panel's registration point at it, so only a person moves it.
test_an_unattended_rerun_never_moves_a_host_an_install_chose() {
make_panel cs-panel.example.com
make_plugin
run_plugin --domain hello-cs-panel.example.com
run_plugin
assert_eq "$(plugin_env HELLO_DOMAIN)" "HELLO_DOMAIN=hello-cs-panel.example.com"
assert_contains "$(cat "$WORK_DIR/plugin-output")" "outside the session cookie's domain"
}

test_a_first_install_does_not_keep_the_example_host() {
make_panel cs-panel.example.com
make_plugin
cp "$PLUGIN/k8s/base/hello.env.example" "$PLUGIN/k8s/base/hello.env"
run_plugin
assert_eq "$(plugin_env HELLO_DOMAIN)" "HELLO_DOMAIN=hello.cs-panel.example.com"
}

# cron, Ansible, ssh without -t: /dev/tty passes -r there but cannot be opened.
test_a_run_without_a_terminal_counts_as_unattended() {
make_panel cs-panel.example.com
make_plugin
run_plugin --domain hello-cs-panel.example.com
# A socket passes -r and cannot be opened, as /dev/tty does with no terminal.
python3 -c 'import socket, sys; socket.socket(socket.AF_UNIX).bind(sys.argv[1])' "$WORK_DIR/no-tty"
(cd "$WORK_DIR" && PATH="$STUB_BIN:$PATH" PLUGIN_TTY="$WORK_DIR/no-tty" bash "$PANEL/plugin.sh" "$PLUGIN" --dry-run) \
>"$WORK_DIR/plugin-output" 2>&1 || fail "plugin.sh exited $?: $(tail -n 20 "$WORK_DIR/plugin-output")"
assert_eq "$(plugin_env HELLO_DOMAIN)" "HELLO_DOMAIN=hello-cs-panel.example.com"
}

test_an_interactive_rerun_says_why_it_offers_another_host() {
make_panel cs-panel.example.com
make_plugin
run_plugin --domain hello-cs-panel.example.com
run_plugin_interactively
assert_contains "$(cat "$WORK_DIR/plugin-output")" "hello-cs-panel.example.com, the current host, is outside"
}

# A fresh panel has the switch off, and the Add button only appears once it is on.
test_the_finish_says_to_turn_panel_plugins_on() {
make_panel cs-panel.example.com
make_plugin
(cd "$WORK_DIR" && PATH="$STUB_BIN:$PATH" bash "$PANEL/plugin.sh" "$PLUGIN" -y) \
>"$WORK_DIR/plugin-output" 2>&1 || fail "plugin.sh exited $?: $(tail -n 20 "$WORK_DIR/plugin-output")"
assert_contains "$(cat "$WORK_DIR/plugin-output")" "Turn on the Panel Plugins switch"
}

test_a_host_under_the_panel_is_not_warned() {
make_panel cs-panel.example.com
make_plugin
Expand Down
Loading