From 0ad31c940989e1bacd3daa10985b7d02c863d5de Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 21:31:33 +0100 Subject: [PATCH 01/44] fix(storage): mask RCLONE_FLAGS values in the run log A backend credential passed in RCLONE_FLAGS (--s3-secret-access-key=X, --webdav-pass X) was written in plain text to the run log, which is copied to SECONDARY_LOG_PATH, uploaded to CLOUD_LOG_PATH and attached by --support: nine DEBUG lines print the full rclone argv (filesystem check, write test, copy, both verifications, delete), and with -vv in RCLONE_FLAGS rclone itself echoes its argv into the stderr that errors carry to WARNING/ERROR lines (measured on pve-test, rclone 1.60.1). NewCloudStorage now registers every RCLONE_FLAGS value (the text after '=' or a token that is not a flag) as a log secret, before the first rclone call, so each line shows "--s3-secret-access-key=************EKEY". The logger skips values under 6 characters, so the shipped flags stay readable and no line changes on the default configuration. Audit finding storage-10 (audit-prod-go-2026-10-26). --- internal/storage/cloud.go | 21 +++++ .../storage/cloud_rclone_flags_secret_test.go | 87 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 internal/storage/cloud_rclone_flags_secret_test.go diff --git a/internal/storage/cloud.go b/internal/storage/cloud.go index 2f68d32a..305a4041 100644 --- a/internal/storage/cloud.go +++ b/internal/storage/cloud.go @@ -488,6 +488,7 @@ func NewCloudStorage(cfg *config.Config, logger *logging.Logger, writtenHostname host := resolveRetentionHostname() serverID := types.NormalizeServerID(cfg.ServerID) logRetentionServerIdentity(logger, "Cloud storage", serverID) + registerRcloneFlagSecrets(logger, cfg.RcloneFlags) return &CloudStorage{ config: cfg, logger: logger, @@ -511,6 +512,26 @@ func NewCloudStorage(cfg *config.Config, logger *logging.Logger, writtenHostname }, nil } +// registerRcloneFlagSecrets masks the values of RCLONE_FLAGS in every later log line: +// a flag there may carry a backend credential, and the rclone argv is logged at DEBUG, +// and echoed by rclone itself into its stderr (so into errors) when the flags hold -vv. +// A value is the text after '=' or a token that is not a flag; the logger skips values +// under 6 characters, so the shipped flags (--checkers=4, --stats=0, ...) stay readable. +func registerRcloneFlagSecrets(logger *logging.Logger, flags []string) { + if logger == nil { + return + } + for _, tok := range flags { + if strings.HasPrefix(tok, "-") { + if _, value, ok := strings.Cut(tok, "="); ok { + logger.RegisterSecret(value) + } + continue + } + logger.RegisterSecret(tok) + } +} + // Name returns the storage backend name func (c *CloudStorage) Name() string { return "Cloud Storage (rclone)" diff --git a/internal/storage/cloud_rclone_flags_secret_test.go b/internal/storage/cloud_rclone_flags_secret_test.go new file mode 100644 index 00000000..0e74ecc3 --- /dev/null +++ b/internal/storage/cloud_rclone_flags_secret_test.go @@ -0,0 +1,87 @@ +package storage + +import ( + "bytes" + "context" + "errors" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/config" + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// A backend credential in RCLONE_FLAGS was written in plain text to the run log: the +// rclone argv is logged at DEBUG by the filesystem check, the copy, the verification and +// the delete, and rclone -vv echoes its argv into the stderr that errors carry. The +// values of RCLONE_FLAGS are now masked in every line, in both the --flag=value and the +// --flag value form. +func TestCloudStorageMasksRcloneFlagValuesInTheLog(t *testing.T) { + const keyValue = "wJalrXUtnFEMI-K7MDENG-bPxRfiCYEXAMPLEKEY" + const passValue = "Sep4r4teT0kenPassw0rdXYZ" + cfg := &config.Config{ + CloudEnabled: true, + CloudRemote: "s3remote:bucket", + CloudRemotePath: "proxsave", + RcloneTimeoutConnection: 30, + RcloneFlags: strings.Fields("--checkers=4 --s3-secret-access-key=" + keyValue + " --webdav-pass " + passValue), + } + + var out bytes.Buffer + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&out) + + cs, err := NewCloudStorage(cfg, logger, "pve1") + if err != nil { + t.Fatalf("NewCloudStorage: %v", err) + } + cs.lookPath = func(string) (string, error) { return "/usr/bin/rclone", nil } + argvCarriesValues := false + cs.runCommand = func(_ context.Context, _ string, args ...string) (rcloneOutput, error) { + joined := strings.Join(args, " ") + if strings.Contains(joined, keyValue) && strings.Contains(joined, passValue) { + argvCarriesValues = true + } + switch args[0] { + case "lsl": + return rcloneOutput{stdout: []byte("1234 2026-10-26 10:00:00.000000000 a.tar.zst\n")}, nil + case "hashsum": + return rcloneOutput{stdout: []byte(strings.Repeat("a", 64) + " a.tar.zst\n")}, nil + } + return rcloneOutput{}, nil + } + + ctx := context.Background() + if _, err := cs.DetectFilesystem(ctx); err != nil { + t.Fatalf("DetectFilesystem: %v", err) + } + if err := cs.rcloneCopy(ctx, "/var/lib/proxsave/backup/a.tar.zst", cs.remotePathFor("a.tar.zst")); err != nil { + t.Fatalf("rcloneCopy: %v", err) + } + if _, err := cs.verifyPrimary(ctx, cs.remotePathFor("a.tar.zst"), 1234, "a.tar.zst"); err != nil { + t.Fatalf("verifyPrimary: %v", err) + } + // What rclone -vv writes to stderr, carried by an error into a WARNING. + echo := errors.New(`DEBUG : rclone: Version "v1.60.1-DEV" starting with parameters ["rclone" "copyto" "--checkers=4" "--s3-secret-access-key=` + keyValue + `" "--webdav-pass" "` + passValue + `"]`) + logger.Warning("Cloud Storage: upload failed: %v", echo) + + if !argvCarriesValues { + t.Fatalf("precondition: the RCLONE_FLAGS values never reached the rclone argv") + } + log := out.String() + for _, secret := range []string{keyValue, passValue} { + if strings.Contains(log, secret) { + t.Fatalf("RCLONE_FLAGS value %q written in plain text; log:\n%s", secret, log) + } + } + for _, want := range []string{ + "--s3-secret-access-key=" + logging.MaskSecret(keyValue), + "--webdav-pass " + logging.MaskSecret(passValue), + "--checkers=4", + } { + if !strings.Contains(log, want) { + t.Fatalf("log lacks %q; log:\n%s", want, log) + } + } +} From d72453aaa7053cd6174bb8944ee50967269150ec Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 21:48:40 +0100 Subject: [PATCH 02/44] fix(telegram): keep the bot server's answer out of the startup WARNING For a status the startup handshake does not know, the WARNING printed on every centralized run read "Telegram: 502 - Unexpected response: " plus the raw answer, up to the 8 KiB read cap: a proxy's 502 page spanned 9 physical lines (8 without a prefix), a 5.4 KB body 89, and its escape and bidi bytes reached the console and the run log raw. The setup screens showed the same Message. Message now names the status only, as the 403/409/422/426 branches do: "Telegram: 502 - Unexpected response" on the run, and the same text on the CLI and TUI setup screens. The answer stays in the DEBUG bodyPreview line printed before it, quoted and cut to 200 characters. The error of the 403/409/422/426 branches, printed at DEBUG as "Telegram connection error", carried the raw body too; it is now quoted and cut the same way. Audit finding notify-health-7 (audit-prod-go-2026-10-26). --- .../telegram_server_status_body_test.go | 77 +++++++++++++++++++ internal/notify/telegram_registration.go | 17 ++-- 2 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 cmd/proxsave/telegram_server_status_body_test.go diff --git a/cmd/proxsave/telegram_server_status_body_test.go b/cmd/proxsave/telegram_server_status_body_test.go new file mode 100644 index 00000000..eeb056f2 --- /dev/null +++ b/cmd/proxsave/telegram_server_status_body_test.go @@ -0,0 +1,77 @@ +package main + +import ( + "bytes" + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/config" + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// A proxy page answered by the bot server reached the run's WARNING line raw: every +// CR/LF, escape byte and bidi rune, up to the 8 KiB read cap. The WARNING now names +// the status only, and the DEBUG error line carries the body quoted and cut. +func TestTelegramServerStatusKeepsTheServerBodyOutOfTheWarning(t *testing.T) { + page := "\r\n502 Bad Gateway\r\n\r\n" + + "

502 Bad Gateway

\r\n
nginx
\r\n\r\n\r\n" + + strings.Repeat("\n", 80) + + "\x1b[2J\x1b[H‮FAKE" + + for _, tc := range []struct { + status int + warning string + }{ + {http.StatusBadGateway, "Telegram: 502 - Unexpected response\n"}, + {http.StatusForbidden, "Telegram: 403 - Start the bot and send the Server ID\n"}, + } { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(tc.status) + _, _ = w.Write([]byte(page)) + })) + + prev := logging.GetDefaultLogger() + var out bytes.Buffer + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&out) + logging.SetDefaultLogger(logger) + + rt := &appRuntime{ + ctx: context.Background(), + logger: logger, + cfg: &config.Config{ + TelegramEnabled: true, + TelegramBotType: "centralized", + ServerAPIHost: srv.URL, + BaseDir: t.TempDir(), + }, + serverIDValue: "1234567890123456", + } + logTelegramServerStatus(rt) + logging.SetDefaultLogger(prev) + srv.Close() + + log := out.String() + if rt.cfg.TelegramEnabled { + t.Fatalf("HTTP %d: Telegram still enabled after the WARNING", tc.status) + } + if !strings.Contains(log, tc.warning) { + t.Fatalf("HTTP %d: no WARNING line %q; log:\n%s", tc.status, tc.warning, log) + } + for _, raw := range []string{"\x1b", "‮", "\r", "\n", "\r\n"} { + if strings.Contains(log, raw) { + t.Fatalf("HTTP %d: raw server body (%q) in the log:\n%s", tc.status, raw, log) + } + } + for _, line := range strings.Split(strings.TrimSuffix(log, "\n"), "\n") { + if !strings.HasPrefix(line, "[") { + t.Fatalf("HTTP %d: the body split a line, %q has no prefix; log:\n%s", tc.status, line, log) + } + } + } +} diff --git a/internal/notify/telegram_registration.go b/internal/notify/telegram_registration.go index e78cb230..53cd7274 100644 --- a/internal/notify/telegram_registration.go +++ b/internal/notify/telegram_registration.go @@ -31,6 +31,13 @@ func truncateTelegramRegistrationBody(body []byte, max int) string { return text } +// telegramRegistrationBodyError carries the server's answer into the status error, +// which is logged at DEBUG: quoted and cut, because a proxy page in front of the bot +// server can be kilobytes of HTML with control bytes that would split the line. +func telegramRegistrationBodyError(body []byte) error { + return fmt.Errorf("%q", truncateTelegramRegistrationBody(body, 200)) +} + // TelegramRegistrationStatus represents the result of the handshake with the centralized Telegram server. type TelegramRegistrationStatus struct { Code int @@ -261,25 +268,25 @@ func checkTelegramRegistrationWithSecret(ctx context.Context, serverAPIHost, ser return TelegramRegistrationStatus{Code: 200, Message: "200 - Relay provisioned (no Telegram chat)", LinkState: TelegramLinkStateRelayOnly}, secret case 403: logTelegramRegistrationDebug(logger, "Telegram registration: status 403 (bot not started / first contact)") - return TelegramRegistrationStatus{Code: 403, Message: "403 - Start the bot and send the Server ID", Error: fmt.Errorf("%s", body)}, "" + return TelegramRegistrationStatus{Code: 403, Message: "403 - Start the bot and send the Server ID", Error: telegramRegistrationBodyError(body)}, "" case 409: logTelegramRegistrationDebug(logger, "Telegram registration: status 409 (missing registration)") - return TelegramRegistrationStatus{Code: 409, Message: "409 - Registration missing on the bot", Error: fmt.Errorf("%s", body)}, "" + return TelegramRegistrationStatus{Code: 409, Message: "409 - Registration missing on the bot", Error: telegramRegistrationBodyError(body)}, "" case 422: logTelegramRegistrationDebug(logger, "Telegram registration: status 422 (invalid server ID)") - return TelegramRegistrationStatus{Code: 422, Message: "422 - Invalid Server ID", Error: fmt.Errorf("%s", body)}, "" + return TelegramRegistrationStatus{Code: 422, Message: "422 - Invalid Server ID", Error: telegramRegistrationBodyError(body)}, "" case 426: logTelegramRegistrationDebug(logger, "Telegram registration: status 426 (upgrade required)") return TelegramRegistrationStatus{ Code: 426, Message: "426 - Upgrade ProxSave to v0.28.0 or later to complete pairing", - Error: fmt.Errorf("%s", body), + Error: telegramRegistrationBodyError(body), }, "" default: logTelegramRegistrationDebug(logger, "Telegram registration: unexpected status %d", resp.Status) return TelegramRegistrationStatus{ Code: resp.Status, - Message: fmt.Sprintf("%d - Unexpected response: %s", resp.Status, string(body)), + Message: fmt.Sprintf("%d - Unexpected response", resp.Status), Error: fmt.Errorf("unexpected status %d", resp.Status), }, "" } From 97b4adfd42a717cb6cd2954671b49ed823f053db Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 21:59:57 +0100 Subject: [PATCH 03/44] fix(storage): mask RCLONE_FLAGS values in every form they reach the log 0ad31c94 registered the RCLONE_FLAGS values as log secrets but missed three forms (automated security review of that commit): 1. a separate value that starts with '-' ("--ftp-pass -x...") was taken for a flag and left in plain text. Every token that is not a --flag is now a value; 2. rclone -vv echoes its argv with %q, so a value holding a quote, a backslash or a control byte appears escaped there, a form the mask did not match. That form is registered too; 3. a failed copy keeps the last 4 KiB of rclone's output, and the cut can start inside an echoed value, whose rest the logger no longer recognises. The output is now masked before the cut. No line changes beyond the approved mask. Audit finding storage-10 (audit-prod-go-2026-10-26). --- internal/storage/cloud.go | 47 ++++++++++++------ .../storage/cloud_rclone_flags_secret_test.go | 49 +++++++++++++++++++ 2 files changed, 81 insertions(+), 15 deletions(-) diff --git a/internal/storage/cloud.go b/internal/storage/cloud.go index 305a4041..b5c51cd2 100644 --- a/internal/storage/cloud.go +++ b/internal/storage/cloud.go @@ -13,6 +13,7 @@ import ( "regexp" "slices" "sort" + "strconv" "strings" "sync" "syscall" @@ -75,6 +76,8 @@ type CloudStorage struct { parallelVerify bool verifyChecksum bool verifyDownload bool + // flagSecrets are the RCLONE_FLAGS values, masked in the log (rcloneFlagSecrets). + flagSecrets []string // runCommand runs one rclone call (defaultRunCommand); execCommand, when set, // replaces it with a test fake that answers on one stream, read as stdout. runCommand func(ctx context.Context, name string, args ...string) (rcloneOutput, error) @@ -488,7 +491,12 @@ func NewCloudStorage(cfg *config.Config, logger *logging.Logger, writtenHostname host := resolveRetentionHostname() serverID := types.NormalizeServerID(cfg.ServerID) logRetentionServerIdentity(logger, "Cloud storage", serverID) - registerRcloneFlagSecrets(logger, cfg.RcloneFlags) + flagSecrets := rcloneFlagSecrets(cfg.RcloneFlags) + if logger != nil { + for _, secret := range flagSecrets { + logger.RegisterSecret(secret) + } + } return &CloudStorage{ config: cfg, logger: logger, @@ -505,6 +513,7 @@ func NewCloudStorage(cfg *config.Config, logger *logging.Logger, writtenHostname parallelVerify: cfg.CloudParallelVerify, verifyChecksum: cfg.CloudVerifyChecksum, verifyDownload: cfg.CloudVerifyDownload, + flagSecrets: flagSecrets, runCommand: defaultRunCommand, lookPath: exec.LookPath, waitForRetry: currentCloudRetryWait(), @@ -512,24 +521,30 @@ func NewCloudStorage(cfg *config.Config, logger *logging.Logger, writtenHostname }, nil } -// registerRcloneFlagSecrets masks the values of RCLONE_FLAGS in every later log line: -// a flag there may carry a backend credential, and the rclone argv is logged at DEBUG, +// rcloneFlagSecrets lists what NewCloudStorage masks in every later log line: a flag in +// RCLONE_FLAGS may carry a backend credential, and the rclone argv is logged at DEBUG, // and echoed by rclone itself into its stderr (so into errors) when the flags hold -vv. -// A value is the text after '=' or a token that is not a flag; the logger skips values -// under 6 characters, so the shipped flags (--checkers=4, --stats=0, ...) stay readable. -func registerRcloneFlagSecrets(logger *logging.Logger, flags []string) { - if logger == nil { - return - } +// A value is the text after '=' in a --flag=value token, or any token that is not a +// --flag, so "--webdav-pass -x..." is covered too. rclone echoes its argv with %q, so a +// value holding a quote, a backslash or a control byte is listed in that form as well. +// The logger skips values under 6 characters, so the shipped flags (--checkers=4, +// --stats=0, ...) stay readable. +func rcloneFlagSecrets(flags []string) []string { + var secrets []string for _, tok := range flags { - if strings.HasPrefix(tok, "-") { - if _, value, ok := strings.Cut(tok, "="); ok { - logger.RegisterSecret(value) + value := tok + if strings.HasPrefix(tok, "--") { + var ok bool + if _, value, ok = strings.Cut(tok, "="); !ok { + continue } - continue } - logger.RegisterSecret(tok) + secrets = append(secrets, value) + if quoted := strconv.Quote(value); quoted[1:len(quoted)-1] != value { + secrets = append(secrets, quoted[1:len(quoted)-1]) + } } + return secrets } // Name returns the storage backend name @@ -1691,7 +1706,9 @@ func (c *CloudStorage) rcloneCopy(ctx context.Context, localFile, remoteFile str if ctx.Err() == context.DeadlineExceeded { return fmt.Errorf("rclone operation timeout") } - transcript := output.transcript() + // Masked before the cut: the kept tail can start inside an RCLONE_FLAGS value + // echoed by rclone -vv, and the logger would not recognise the rest of it. + transcript := []byte(logging.RedactSecrets(string(output.transcript()), c.flagSecrets...)) return &rcloneCommandError{ msg: fmt.Sprintf("rclone copy failed: %v: %s", err, strings.TrimSpace(string(truncateRcloneOutput(transcript)))), short: rcloneCause(string(transcript), err), diff --git a/internal/storage/cloud_rclone_flags_secret_test.go b/internal/storage/cloud_rclone_flags_secret_test.go index 0e74ecc3..93d96fd4 100644 --- a/internal/storage/cloud_rclone_flags_secret_test.go +++ b/internal/storage/cloud_rclone_flags_secret_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "strconv" "strings" "testing" @@ -85,3 +86,51 @@ func TestCloudStorageMasksRcloneFlagValuesInTheLog(t *testing.T) { } } } + +// Three forms the first masking missed: a separate value that starts with '-', a value +// that rclone -vv echoes escaped (it prints its argv with %q), and a value cut in two by +// the 4 KiB tail of a failed copy's output, whose rest the logger no longer recognises. +func TestCloudStorageMasksRcloneFlagValuesInEveryForm(t *testing.T) { + const dashValue = "-Dash3dPassw0rdValue" + const quoteValue = `Qu0te"Back\slash9` + const passValue = "Sep4r4teT0kenPassw0rdXYZ" + cfg := &config.Config{ + CloudEnabled: true, + CloudRemote: "s3remote:bucket", + CloudRemotePath: "proxsave", + RcloneTimeoutConnection: 30, + RcloneFlags: []string{"--ftp-pass", dashValue, "--sftp-pass=" + quoteValue, "--webdav-pass", passValue}, + } + + var out bytes.Buffer + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&out) + cs, err := NewCloudStorage(cfg, logger, "pve1") + if err != nil { + t.Fatalf("NewCloudStorage: %v", err) + } + + echo := `DEBUG : rclone: Version "v1.60.1-DEV" starting with parameters ["rclone" "copyto" "--ftp-pass" "` + dashValue + + `" ` + strconv.Quote("--sftp-pass="+quoteValue) + ` "--webdav-pass" "` + passValue + `"]` + "\n" + logger.Warning("Cloud Storage: upload failed: %s", echo) + + // The kept tail of the copy's output starts 6 characters into passValue. + cut := strings.Index(echo, passValue) + 6 + transcript := echo + strings.Repeat("x", cut+4096-len(echo)) + cs.lookPath = func(string) (string, error) { return "/usr/bin/rclone", nil } + cs.runCommand = func(_ context.Context, _ string, args ...string) (rcloneOutput, error) { + return rcloneOutput{stderr: []byte(transcript)}, errors.New("exit status 1") + } + copyErr := cs.rcloneCopy(context.Background(), "/var/lib/proxsave/backup/a.tar.zst", cs.remotePathFor("a.tar.zst")) + if copyErr == nil { + t.Fatalf("rcloneCopy: nil error, want the failed copy") + } + logger.Warning("Cloud Storage: upload failed: %v", copyErr) + + log := out.String() + for _, leak := range []string{dashValue, quoteValue, strconv.Quote(quoteValue), `Qu0te\"Back\\slash9`, "Passw0rd"} { + if strings.Contains(log, leak) { + t.Fatalf("RCLONE_FLAGS value %q written in plain text; log:\n%s", leak, log) + } + } +} From 2dc0ce216d80fdbcb5f1c15d2e29bd75e09b47a1 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:02:45 +0100 Subject: [PATCH 04/44] test(telegram): write the bidi rune of the startup test as an escape d72453aa put two raw U+202E runes in the test source (an editing tool turned the \u202e escape into the rune). Same string at run time; the source no longer holds an invisible reordering character. --- cmd/proxsave/telegram_server_status_body_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/proxsave/telegram_server_status_body_test.go b/cmd/proxsave/telegram_server_status_body_test.go index eeb056f2..c94d2e96 100644 --- a/cmd/proxsave/telegram_server_status_body_test.go +++ b/cmd/proxsave/telegram_server_status_body_test.go @@ -20,7 +20,7 @@ func TestTelegramServerStatusKeepsTheServerBodyOutOfTheWarning(t *testing.T) { page := "\r\n502 Bad Gateway\r\n\r\n" + "

502 Bad Gateway

\r\n
nginx
\r\n\r\n\r\n" + strings.Repeat("\n", 80) + - "\x1b[2J\x1b[H‮FAKE" + "\x1b[2J\x1b[H\u202eFAKE" for _, tc := range []struct { status int @@ -63,7 +63,7 @@ func TestTelegramServerStatusKeepsTheServerBodyOutOfTheWarning(t *testing.T) { if !strings.Contains(log, tc.warning) { t.Fatalf("HTTP %d: no WARNING line %q; log:\n%s", tc.status, tc.warning, log) } - for _, raw := range []string{"\x1b", "‮", "\r", "\n", "\r\n"} { + for _, raw := range []string{"\x1b", "\u202e", "\r", "\n", "\r\n"} { if strings.Contains(log, raw) { t.Fatalf("HTTP %d: raw server body (%q) in the log:\n%s", tc.status, raw, log) } From 20241efd67360c8cf25d6587b1d83c6b7dd83dbd Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:03:57 +0100 Subject: [PATCH 05/44] fix(webhook): name the status, not the endpoint's answer, in failure lines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A failed endpoint's answer was printed raw in the visible line of every attempt (4 with the defaults): a proxy's 502 page made 38 physical lines with its escape and bidi bytes, and the read had no limit, so an 8 MiB answer made an 8 MiB WARNING. Measured side effects: a line over 1 MiB stops the run log re-parse (exit 0 instead of 1), and an answer line shaped like "[ts] ERROR" was counted as a backup error (exit 4). The 5xx line also said "will retry" on the last attempt. The answer is now read up to 8 KiB and shown only in the DEBUG "Response body" line, with the endpoint's credentials masked, on one line of printable characters, cut to 200. The errors no longer carry it. Approved texts: ERROR ❌ Bad request (HTTP 400) ERROR ❌ Forbidden (HTTP 403) WARNING ⚠️ Rate limited (HTTP 429), attempt 1/4 WARNING ⚠️ Server error (HTTP 502), attempt 1/4 WARNING ⚠️ Unexpected status (HTTP 405), attempt 1/4 Found while fixing audit finding notify-health-7: same raw server body in a visible line, at a sibling site. --- internal/notify/webhook.go | 40 +++++--- internal/notify/webhook_response_body_test.go | 93 +++++++++++++++++++ 2 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 internal/notify/webhook_response_body_test.go diff --git a/internal/notify/webhook.go b/internal/notify/webhook.go index 1f6f764d..1f1cc088 100644 --- a/internal/notify/webhook.go +++ b/internal/notify/webhook.go @@ -14,9 +14,23 @@ import ( "github.com/tis24dev/proxsave/internal/config" "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/serverbot" "github.com/tis24dev/proxsave/internal/types" ) +// webhookMaxResponseBytes bounds the read of an endpoint's answer: only a DEBUG excerpt +// of it is ever shown, and a proxy page or a misbehaving endpoint can be megabytes. +const webhookMaxResponseBytes = 8 << 10 + +// webhookBodyExcerpt is the endpoint's answer as the DEBUG line shows it: the endpoint's +// own credentials masked first (a page can echo the URL, and the cut below could split +// one so the logger would no longer recognise it), then printable characters only, on +// one line, cut to 200. +func webhookBodyExcerpt(body []byte, endpoint config.WebhookEndpoint) string { + masked := logging.RedactSecrets(string(body), endpoint.URL, endpoint.Auth.Token, endpoint.Auth.Pass, endpoint.Auth.Secret) + return (&serverbot.Response{Body: []byte(masked)}).Snippet(200) +} + // WebhookNotifier sends notifications to configured webhook endpoints type WebhookNotifier struct { config *config.WebhookConfig @@ -390,7 +404,7 @@ func (w *WebhookNotifier) sendToEndpoint(ctx context.Context, endpoint config.We // Read response body w.logger.Debug("Reading response body...") - body, err := io.ReadAll(resp.Body) + body, err := io.ReadAll(io.LimitReader(resp.Body, webhookMaxResponseBytes)) closeErr := resp.Body.Close() if err != nil { @@ -409,11 +423,7 @@ func (w *WebhookNotifier) sendToEndpoint(ctx context.Context, endpoint config.We w.logger.Debug("Received HTTP %d in %dms", resp.StatusCode, requestDuration.Milliseconds()) if len(body) > 0 { - if len(body) > 500 { - w.logger.Debug("Response body (%d bytes, truncated): %s...", len(body), string(body[:500])) - } else { - w.logger.Debug("Response body (%d bytes): %s", len(body), string(body)) - } + w.logger.Debug("Response body (%d bytes read): %s", len(body), webhookBodyExcerpt(body, endpoint)) } else { w.logger.Debug("Response body: empty") } @@ -428,8 +438,8 @@ func (w *WebhookNotifier) sendToEndpoint(ctx context.Context, endpoint config.We case resp.StatusCode == 400: // Bad request - don't retry - w.logger.Error("❌ Bad request (HTTP 400): %s", string(body)) - return fmt.Errorf("bad request (HTTP 400): %s", string(body)) + w.logger.Error("❌ Bad request (HTTP 400)") + return fmt.Errorf("bad request (HTTP 400)") case resp.StatusCode == 401: // Authentication failed - don't retry @@ -438,8 +448,8 @@ func (w *WebhookNotifier) sendToEndpoint(ctx context.Context, endpoint config.We case resp.StatusCode == 403: // Forbidden - don't retry - w.logger.Error("❌ Forbidden (HTTP 403): %s", string(body)) - return fmt.Errorf("forbidden (HTTP 403): %s", string(body)) + w.logger.Error("❌ Forbidden (HTTP 403)") + return fmt.Errorf("forbidden (HTTP 403)") case resp.StatusCode == 404: // Not found - don't retry @@ -448,7 +458,7 @@ func (w *WebhookNotifier) sendToEndpoint(ctx context.Context, endpoint config.We case resp.StatusCode == 429: // Rate limit - retry with longer delay - w.logger.Warning("⚠️ Rate limited (HTTP 429): %s", string(body)) + w.logger.Warning("⚠️ Rate limited (HTTP 429), attempt %d/%d", attempt+1, maxRetries+1) if attempt < maxRetries { w.logger.Debug("Waiting 10 seconds before retry due to rate limiting...") if err := sleepWithContext(ctx, 10*time.Second); err != nil { @@ -460,14 +470,14 @@ func (w *WebhookNotifier) sendToEndpoint(ctx context.Context, endpoint config.We case resp.StatusCode >= 500: // Server error - retry - w.logger.Warning("⚠️ Server error (HTTP %d), will retry: %s", resp.StatusCode, string(body)) - lastErr = fmt.Errorf("server error (HTTP %d): %s", resp.StatusCode, string(body)) + w.logger.Warning("⚠️ Server error (HTTP %d), attempt %d/%d", resp.StatusCode, attempt+1, maxRetries+1) + lastErr = fmt.Errorf("server error (HTTP %d)", resp.StatusCode) continue default: // Unexpected status - retry - w.logger.Warning("⚠️ Unexpected status (HTTP %d): %s", resp.StatusCode, string(body)) - lastErr = fmt.Errorf("unexpected status (HTTP %d): %s", resp.StatusCode, string(body)) + w.logger.Warning("⚠️ Unexpected status (HTTP %d), attempt %d/%d", resp.StatusCode, attempt+1, maxRetries+1) + lastErr = fmt.Errorf("unexpected status (HTTP %d)", resp.StatusCode) continue } } diff --git a/internal/notify/webhook_response_body_test.go b/internal/notify/webhook_response_body_test.go new file mode 100644 index 00000000..7ffbf643 --- /dev/null +++ b/internal/notify/webhook_response_body_test.go @@ -0,0 +1,93 @@ +package notify + +import ( + "bytes" + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/config" + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// An endpoint's answer was printed raw in the visible line of every failed attempt: +// a proxy page split each WARNING over many lines with its escape and bidi bytes, the +// read had no limit (an 8 MiB answer made an 8 MiB WARNING), and the 5xx line said +// "will retry" on the last attempt too. The lines now name the status and the attempt; +// the answer is read up to 8 KiB and shown only at DEBUG, on one line, cut to 200. +func TestWebhookFailureLinesNameTheStatusNotTheAnswer(t *testing.T) { + page := "\r\n502 Bad Gateway\r\n\r\n" + + "

502 Bad Gateway

\r\n
nginx
\r\n\r\n\r\n" + + "\x1b[2J\u202e" + strings.Repeat("\n", 512*1024) + + for _, tc := range []struct { + status int + lines []string + }{ + {http.StatusBadGateway, []string{ + "WARNING ⚠️ Server error (HTTP 502), attempt 1/2\n", + "WARNING ⚠️ Server error (HTTP 502), attempt 2/2\n", + }}, + {http.StatusMethodNotAllowed, []string{ + "WARNING ⚠️ Unexpected status (HTTP 405), attempt 1/2\n", + "WARNING ⚠️ Unexpected status (HTTP 405), attempt 2/2\n", + }}, + {http.StatusBadRequest, []string{"ERROR ❌ Bad request (HTTP 400)\n"}}, + {http.StatusForbidden, []string{"ERROR ❌ Forbidden (HTTP 403)\n"}}, + } { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(tc.status) + _, _ = w.Write([]byte(page)) + })) + cfg := config.WebhookConfig{ + Enabled: true, + DefaultFormat: "generic", + Timeout: 30, + MaxRetries: 1, + RetryDelay: 1, + Endpoints: []config.WebhookEndpoint{{ + Name: "ops", + URL: server.URL, + Format: "generic", + Method: "POST", + Auth: config.WebhookAuth{Type: "none"}, + }}, + } + var out bytes.Buffer + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&out) + notifier, err := NewWebhookNotifier(&cfg, logger) + if err != nil { + t.Fatalf("NewWebhookNotifier: %v", err) + } + sendErr := notifier.sendToEndpoint(context.Background(), cfg.Endpoints[0], createTestNotificationData()) + server.Close() + if sendErr == nil { + t.Fatalf("HTTP %d: nil error, want the failure", tc.status) + } + + log := out.String() + for _, want := range tc.lines { + if !strings.Contains(log, want) { + t.Fatalf("HTTP %d: no line %q; log:\n%.3000s", tc.status, want, log) + } + } + for _, raw := range []string{"\x1b", "\u202e", "\r", "\n"} { + if strings.Contains(log, raw) || strings.Contains(sendErr.Error(), raw) { + t.Fatalf("HTTP %d: raw answer (%q) in the log or the error %q; log:\n%.3000s", tc.status, raw, sendErr, log) + } + } + if len(log) > 16<<10 { + t.Fatalf("HTTP %d: %d bytes of log for one failed endpoint, the answer was not bounded", tc.status, len(log)) + } + for line := range strings.SplitSeq(strings.TrimSuffix(log, "\n"), "\n") { + if !strings.HasPrefix(line, "[") { + t.Fatalf("HTTP %d: the answer split a line, %q has no prefix", tc.status, line) + } + } + } +} From ed68c42ccb7fbe4238133b47a38ac774c542033f Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:10:44 +0100 Subject: [PATCH 06/44] fix(upgrade): name GitHub's status, not its answer, in the upgrade error A non-200 answer from GitHub went raw into the ERROR line of proxsave --upgrade (and the session log of the dashboard's Run upgrade): a proxy's 502 page made 8 lines with its escape and bidi bytes, for the release API and for the asset downloads alike. The error now names the status with Go's own status text, plus, for the release API, the message of GitHub's JSON error, which is where its rate limit says why. The answer itself goes to a DEBUG line, on one line of printable characters, cut to 200. Approved texts: failed to fetch latest release: status 502 Bad Gateway failed to fetch latest release: status 403 Forbidden: API rate limit exceeded for . (...) Upgrade failed: failed to download archive: download failed with status 502 Bad Gateway Upgrade failed: failed to download archive: download failed with status 403 Forbidden Found while fixing audit finding notify-health-7: same raw server body in a visible line, at a sibling site. --- cmd/proxsave/main_update.go | 5 ++ cmd/proxsave/upgrade.go | 42 +++++++++- cmd/proxsave/upgrade_github_status_test.go | 92 ++++++++++++++++++++++ 3 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 cmd/proxsave/upgrade_github_status_test.go diff --git a/cmd/proxsave/main_update.go b/cmd/proxsave/main_update.go index af9b96c3..bfb5e128 100644 --- a/cmd/proxsave/main_update.go +++ b/cmd/proxsave/main_update.go @@ -3,6 +3,7 @@ package main import ( "context" + "errors" "fmt" "strconv" "strings" @@ -79,6 +80,10 @@ func checkForUpdates(ctx context.Context, logger *logging.Logger, currentVersion latestTag, latestVersion, releaseBody, err := fetchLatestRelease(checkCtx) if err != nil { + var statusErr *githubStatusError + if errors.As(err, &statusErr) { + logger.Debug("Update check: latest release response body=%q", statusErr.excerpt) + } logger.Debug("Update check skipped: GitHub unreachable: %v", err) return &UpdateInfo{ NewVersion: false, diff --git a/cmd/proxsave/upgrade.go b/cmd/proxsave/upgrade.go index b5b8d0de..e348f2bd 100644 --- a/cmd/proxsave/upgrade.go +++ b/cmd/proxsave/upgrade.go @@ -28,6 +28,7 @@ import ( "github.com/tis24dev/proxsave/internal/identity" "github.com/tis24dev/proxsave/internal/logging" "github.com/tis24dev/proxsave/internal/safeexec" + "github.com/tis24dev/proxsave/internal/serverbot" "github.com/tis24dev/proxsave/internal/types" buildinfo "github.com/tis24dev/proxsave/internal/version" ) @@ -228,6 +229,10 @@ func upgradeAcquireBinary(ctx context.Context, args *cli.Args, bootstrap *loggin logging.DebugStepBootstrap(bootstrap, "upgrade workflow", "fetching latest release info") tag, latestVersion, _, err := fetchLatestRelease(ctx) if err != nil { + var statusErr *githubStatusError + if errors.As(err, &statusErr) { + logging.DebugStepBootstrap(bootstrap, "upgrade workflow", "latest release response body=%q", statusErr.excerpt) + } // Four of fetchLatestRelease's five errors open with "failed to ...", so a // wrapper repeated the verb. The fifth, "empty tag_name in latest release // response", does not name the operation: on that shape the cause stands @@ -671,6 +676,37 @@ func resolveReleaseTarget(goos, goarch string) (string, string, error) { return osName, "amd64", nil } +// githubStatusError is a non-200 answer from GitHub. Its text is the status, plus the +// message of GitHub's JSON error (the API's rate limit says why there) when asked for; +// excerpt is the answer itself, for a DEBUG line only: a proxy in front of GitHub can +// answer with a multi-line HTML page holding control bytes. +type githubStatusError struct { + code int + message string + excerpt string +} + +func newGitHubStatusError(code int, body []byte, withMessage bool) *githubStatusError { + e := &githubStatusError{code: code, excerpt: (&serverbot.Response{Body: body}).Snippet(200)} + if withMessage { + var apiErr struct { + Message string `json:"message"` + } + if json.Unmarshal(body, &apiErr) == nil { + e.message = strings.TrimSpace((&serverbot.Response{Body: []byte(apiErr.Message)}).Snippet(200)) + } + } + return e +} + +func (e *githubStatusError) Error() string { + text := strings.TrimSpace(fmt.Sprintf("status %d %s", e.code, http.StatusText(e.code))) + if e.message != "" { + text += ": " + e.message + } + return text +} + // fetchLatestRelease returns (tag, version, body, err) for the latest GitHub release. // body is the raw release description (used to surface the release notes); it is remote- // controlled, so every consumer that displays it MUST sanitize it first. @@ -691,7 +727,7 @@ func fetchLatestRelease(ctx context.Context) (string, string, string, error) { if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(io.LimitReader(resp.Body, 4*1024)) - return "", "", "", fmt.Errorf("failed to fetch latest release: status %d, body: %s", resp.StatusCode, string(body)) + return "", "", "", fmt.Errorf("failed to fetch latest release: %w", newGitHubStatusError(resp.StatusCode, body, true)) } var info releaseInfo @@ -802,7 +838,9 @@ func downloadFile(ctx context.Context, url string, root *os.Root, name string, b logging.DebugStepBootstrap(bootstrap, "upgrade download", "status=%s", resp.Status) if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(io.LimitReader(resp.Body, 2*1024)) - return fmt.Errorf("download failed with status %d: %s", resp.StatusCode, string(body)) + statusErr := newGitHubStatusError(resp.StatusCode, body, false) + logging.DebugStepBootstrap(bootstrap, "upgrade download", "response body=%q", statusErr.excerpt) + return fmt.Errorf("download failed with %w", statusErr) } out, err := root.Create(name) diff --git a/cmd/proxsave/upgrade_github_status_test.go b/cmd/proxsave/upgrade_github_status_test.go new file mode 100644 index 00000000..d135e1c4 --- /dev/null +++ b/cmd/proxsave/upgrade_github_status_test.go @@ -0,0 +1,92 @@ +package main + +import ( + "bytes" + "context" + "net/http" + "net/http/httptest" + "net/url" + "os" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +type rewriteToServer struct { + target *url.URL + base http.RoundTripper +} + +func (r rewriteToServer) RoundTrip(req *http.Request) (*http.Response, error) { + req = req.Clone(req.Context()) + req.URL.Scheme = r.target.Scheme + req.URL.Host = r.target.Host + return r.base.RoundTrip(req) +} + +// A non-200 answer from GitHub went raw into the upgrade's ERROR line: a proxy's 502 +// page made 8 lines with its escape and bidi bytes. The line now names the status, plus +// the message of GitHub's JSON error for the release API (its rate limit says why there); +// the answer itself goes to a DEBUG line, on one line of printable characters. +func TestUpgradeErrorsNameTheGitHubStatusNotTheAnswer(t *testing.T) { + page := "\r\n502 Bad Gateway\r\n\r\n" + + "

502 Bad Gateway

\r\n
nginx
\r\n\r\n\r\n\x1b[2J\u202e" + rateLimit := `{"message":"API rate limit exceeded for 203.0.113.7. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)","documentation_url":"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"}` + + for _, tc := range []struct { + status int + body string + release string + download string + }{ + {http.StatusBadGateway, page, + "failed to fetch latest release: status 502 Bad Gateway", + "download failed with status 502 Bad Gateway"}, + {http.StatusForbidden, rateLimit, + "failed to fetch latest release: status 403 Forbidden: API rate limit exceeded for 203.0.113.7. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)", + "download failed with status 403 Forbidden"}, + } { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(tc.status) + _, _ = w.Write([]byte(tc.body)) + })) + target, _ := url.Parse(server.URL) + prev := http.DefaultTransport + http.DefaultTransport = rewriteToServer{target: target, base: prev} + _, _, _, releaseErr := fetchLatestRelease(context.Background()) + http.DefaultTransport = prev + + var out bytes.Buffer + bootstrap := logging.NewBootstrapLogger() + bootstrap.SetConsoleQuiet(true) + mirror := logging.New(types.LogLevelDebug, false) + mirror.SetOutput(&out) + bootstrap.SetMirrorLogger(mirror) + bootstrap.SetLevel(types.LogLevelDebug) + root, err := os.OpenRoot(t.TempDir()) + if err != nil { + t.Fatal(err) + } + downloadErr := downloadFile(context.Background(), server.URL+"/proxsave.tar.gz", root, "proxsave.tar.gz", bootstrap) + _ = root.Close() + server.Close() + + if releaseErr == nil || releaseErr.Error() != tc.release { + t.Fatalf("HTTP %d: release error %q, want %q", tc.status, releaseErr, tc.release) + } + if downloadErr == nil || downloadErr.Error() != tc.download { + t.Fatalf("HTTP %d: download error %q, want %q", tc.status, downloadErr, tc.download) + } + log := out.String() + if !strings.Contains(log, "response body=") { + t.Fatalf("HTTP %d: no DEBUG line with the answer; log:\n%s", tc.status, log) + } + for _, raw := range []string{"\x1b", "\u202e", "\r"} { + if strings.Contains(log, raw) { + t.Fatalf("HTTP %d: raw answer (%q) in the log:\n%s", tc.status, raw, log) + } + } + } +} From 6ec4b391b4ee0dc37321c893e1c25a48b95d8b04 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:11:42 +0100 Subject: [PATCH 07/44] fix(webhook): mask the URL's path, query and header values in the answer excerpt 20241efd masked the endpoint URL whole in the DEBUG excerpt of an endpoint's answer (automated security review of that commit): an error page echoes the requested path alone, and a webhook URL often carries its token in a path segment; query values and header values can be credentials too. Those are now masked as well, before the cut to 200 characters. DEBUG only. --- internal/notify/webhook.go | 21 ++++++-- internal/notify/webhook_response_body_test.go | 49 +++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/internal/notify/webhook.go b/internal/notify/webhook.go index 1f1cc088..ffb721b3 100644 --- a/internal/notify/webhook.go +++ b/internal/notify/webhook.go @@ -23,11 +23,24 @@ import ( const webhookMaxResponseBytes = 8 << 10 // webhookBodyExcerpt is the endpoint's answer as the DEBUG line shows it: the endpoint's -// own credentials masked first (a page can echo the URL, and the cut below could split -// one so the logger would no longer recognise it), then printable characters only, on -// one line, cut to 200. +// own credentials masked first, then printable characters only, on one line, cut to +// 200. Masked before the cut, which could split a credential so the logger would no +// longer recognise it. Besides the auth values, the URL's path segments, query values +// and header values are masked: a webhook URL often carries its token in the path, and +// an error page can echo the requested path alone. func webhookBodyExcerpt(body []byte, endpoint config.WebhookEndpoint) string { - masked := logging.RedactSecrets(string(body), endpoint.URL, endpoint.Auth.Token, endpoint.Auth.Pass, endpoint.Auth.Secret) + secrets := []string{endpoint.URL, endpoint.Auth.Token, endpoint.Auth.Pass, endpoint.Auth.Secret} + if u, err := url.Parse(endpoint.URL); err == nil { + secrets = append(secrets, u.Path, u.RawQuery) + secrets = append(secrets, strings.Split(u.Path, "/")...) + for _, values := range u.Query() { + secrets = append(secrets, values...) + } + } + for _, value := range endpoint.Headers { + secrets = append(secrets, value) + } + masked := logging.RedactSecrets(string(body), secrets...) return (&serverbot.Response{Body: []byte(masked)}).Snippet(200) } diff --git a/internal/notify/webhook_response_body_test.go b/internal/notify/webhook_response_body_test.go index 7ffbf643..1f4b6153 100644 --- a/internal/notify/webhook_response_body_test.go +++ b/internal/notify/webhook_response_body_test.go @@ -91,3 +91,52 @@ func TestWebhookFailureLinesNameTheStatusNotTheAnswer(t *testing.T) { } } } + +// The DEBUG excerpt masked the endpoint URL whole, but an error page echoes the +// requested path alone, and a webhook URL often carries its token in a path segment; +// query values and header values can be credentials too. +func TestWebhookAnswerExcerptMasksTheEndpointCredentials(t *testing.T) { + const pathToken = "TokenSegmentAbcdef9" + const queryValue = "QueryValue12345" + const headerValue = "HeaderValue98765" + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte("The requested URL " + r.URL.Path + "?" + r.URL.RawQuery + + " was not found (X-Key: " + r.Header.Get("X-Key") + ")")) + })) + defer server.Close() + + cfg := config.WebhookConfig{ + Enabled: true, + DefaultFormat: "generic", + Timeout: 30, + RetryDelay: 1, + Endpoints: []config.WebhookEndpoint{{ + Name: "ops", + URL: server.URL + "/api/webhooks/123456/" + pathToken + "?key=" + queryValue, + Format: "generic", + Method: "POST", + Headers: map[string]string{"X-Key": headerValue}, + Auth: config.WebhookAuth{Type: "none"}, + }}, + } + var out bytes.Buffer + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&out) + notifier, err := NewWebhookNotifier(&cfg, logger) + if err != nil { + t.Fatalf("NewWebhookNotifier: %v", err) + } + if err := notifier.sendToEndpoint(context.Background(), cfg.Endpoints[0], createTestNotificationData()); err == nil { + t.Fatalf("nil error, want the 404") + } + log := out.String() + if !strings.Contains(log, "Response body (") { + t.Fatalf("no DEBUG line with the answer; log:\n%s", log) + } + for _, secret := range []string{pathToken, queryValue, headerValue} { + if strings.Contains(log, secret) { + t.Fatalf("endpoint credential %q in the log:\n%s", secret, log) + } + } +} From b464991c23123a978b90dcc4b7cc09f685d9f5b0 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:16:39 +0100 Subject: [PATCH 08/44] fix(notify): keep the server's answer out of Telegram and Gotify errors Three senders put a non-2xx answer raw into their error, printed at DEBUG twice (by the sender and by the adapter's failure detail): the bot server's get-chat-id at send time (up to 8 KiB), api.telegram.org in personal mode (no read limit at all) and the Gotify server (2 KiB). A proxy page split those lines with its escape and bidi bytes. No visible line carried it: the adapter's WARNING is generic. The errors now name the status only. The answer goes to one DEBUG line of printable characters, cut to 200, with the bot or Gotify token masked before the cut; api.telegram.org is read up to 8 KiB. No visible text changes. Found while fixing audit finding notify-health-7: same raw server body, at sibling sites. --- internal/notify/gotify.go | 7 +- internal/notify/server_answer_debug_test.go | 112 ++++++++++++++++++++ internal/notify/telegram.go | 11 +- 3 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 internal/notify/server_answer_debug_test.go diff --git a/internal/notify/gotify.go b/internal/notify/gotify.go index 7ca27cb4..8b6dcc97 100644 --- a/internal/notify/gotify.go +++ b/internal/notify/gotify.go @@ -12,6 +12,7 @@ import ( "time" "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/serverbot" ) // GotifyConfig holds configuration for Gotify notifications. @@ -151,7 +152,11 @@ func (g *GotifyNotifier) Send(ctx context.Context, data *NotificationData) (*Not respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 2048)) if resp.StatusCode < 200 || resp.StatusCode >= 300 { - err = fmt.Errorf("gotify returned HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(respBody))) + // The answer goes to a DEBUG line only, token masked before the cut: a proxy page + // can be multi-line HTML with control bytes. + masked := logging.RedactSecrets(string(respBody), g.config.Token) + g.logger.Debug("Gotify answer (HTTP %d): %s", resp.StatusCode, (&serverbot.Response{Body: []byte(masked)}).Snippet(200)) + err = fmt.Errorf("gotify returned HTTP %d", resp.StatusCode) g.logger.Debug("Gotify send failed (surfaced once by the notification adapter): %v", err) result.Success = false result.Error = err diff --git a/internal/notify/server_answer_debug_test.go b/internal/notify/server_answer_debug_test.go new file mode 100644 index 00000000..d4ea3ede --- /dev/null +++ b/internal/notify/server_answer_debug_test.go @@ -0,0 +1,112 @@ +package notify + +import ( + "bytes" + "context" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// proxyPage is a reverse proxy's 502 page with an escape sequence and a bidi rune, as +// the bot server, api.telegram.org or a Gotify server can answer through a proxy. +const proxyPage = "\r\n502 Bad Gateway\r\n\r\n" + + "

502 Bad Gateway

\r\n
nginx
\r\n\r\n\r\n\x1b[2J\u202e" + +// assertAnswerOnlyAtDebug checks that a server's answer stayed out of the error and +// reached the log once, at DEBUG, on one line of printable characters. +func assertAnswerOnlyAtDebug(t *testing.T, site string, log string, err error, wantErr string, secrets ...string) { + t.Helper() + if err == nil || err.Error() != wantErr { + t.Fatalf("%s: error %q, want %q", site, err, wantErr) + } + if !strings.Contains(log, "502 Bad Gateway") { + t.Fatalf("%s: no DEBUG line with the answer; log:\n%s", site, log) + } + for _, raw := range append([]string{"\x1b", "\u202e", "\r"}, secrets...) { + if strings.Contains(log, raw) { + t.Fatalf("%s: %q in the log:\n%s", site, raw, log) + } + } + for line := range strings.SplitSeq(strings.TrimSuffix(log, "\n"), "\n") { + if !strings.HasPrefix(line, "[") { + t.Fatalf("%s: the answer split a line, %q has no prefix; log:\n%s", site, line, log) + } + } +} + +// A non-2xx answer went raw, multi-line and unbounded for api.telegram.org, into the +// error of three senders, printed at DEBUG twice (the sender and the adapter). The +// errors now name the status only; the answer is one DEBUG line, cut to 200. +func TestSenderErrorsKeepTheServerAnswerAtDebug(t *testing.T) { + const botToken = "123456:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + const gotifyToken = "GotifyAppTokenAbc123" + + newLogger := func(out *bytes.Buffer) *logging.Logger { + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(out) + return logger + } + + t.Run("telegram personal", func(t *testing.T) { + var out bytes.Buffer + notifier, err := NewTelegramNotifier(TelegramConfig{ + Enabled: true, Mode: TelegramModePersonal, BotToken: botToken, ChatID: "123456", + }, newLogger(&out)) + if err != nil { + t.Fatal(err) + } + notifier.client = &http.Client{Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) { + return &http.Response{ + StatusCode: http.StatusBadGateway, + Body: io.NopCloser(strings.NewReader(proxyPage + " " + req.URL.Path + strings.Repeat("x", 1<<20))), + Header: make(http.Header), + }, nil + })} + sendErr := notifier.sendToTelegram(context.Background(), botToken, "123456", "text") + if sendErr != nil { + notifier.logger.Debug("Telegram failure detail: %v", sendErr) + } + assertAnswerOnlyAtDebug(t, "telegram personal", out.String(), sendErr, "telegram api returned status 502", botToken) + }) + + t.Run("telegram centralized", func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadGateway) + _, _ = w.Write([]byte(proxyPage)) + })) + defer server.Close() + var out bytes.Buffer + notifier, err := NewTelegramNotifier(TelegramConfig{ + Enabled: true, Mode: TelegramModeCentralized, ServerAPIHost: server.URL, ServerID: "1234567890123456", + }, newLogger(&out)) + if err != nil { + t.Fatal(err) + } + _, _, fetchErr := notifier.fetchCentralizedCredentials(context.Background()) + assertAnswerOnlyAtDebug(t, "telegram centralized", out.String(), fetchErr, "unexpected status 502") + }) + + t.Run("gotify", func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadGateway) + _, _ = w.Write([]byte(proxyPage + " token=" + r.URL.Query().Get("token") + " " + r.Header.Get("X-Gotify-Key"))) + })) + defer server.Close() + var out bytes.Buffer + notifier, err := NewGotifyNotifier(GotifyConfig{Enabled: true, ServerURL: server.URL, Token: gotifyToken}, newLogger(&out)) + if err != nil { + t.Fatal(err) + } + result, err := notifier.Send(context.Background(), createTestNotificationData()) + if err != nil { + t.Fatal(err) + } + assertAnswerOnlyAtDebug(t, "gotify", out.String(), result.Error, "gotify returned HTTP 502", gotifyToken) + }) +} diff --git a/internal/notify/telegram.go b/internal/notify/telegram.go index dedde5f1..53674710 100644 --- a/internal/notify/telegram.go +++ b/internal/notify/telegram.go @@ -371,7 +371,8 @@ func (t *TelegramNotifier) fetchCentralizedCredentials(ctx context.Context) (str case 422: return "", "", fmt.Errorf("invalid SERVER_ID (HTTP 422)") default: - return "", "", fmt.Errorf("unexpected status %d: %s", resp.Status, string(body)) + t.logger.Debug("Telegram: get-chat-id answer (HTTP %d): %s", resp.Status, resp.Snippet(200)) + return "", "", fmt.Errorf("unexpected status %d", resp.Status) } } @@ -627,8 +628,12 @@ func (t *TelegramNotifier) sendToTelegram(ctx context.Context, botToken, chatID, // Check response if resp.StatusCode != 200 { - body, _ := io.ReadAll(resp.Body) - return fmt.Errorf("telegram api returned status %d: %s", resp.StatusCode, string(body)) + // The answer goes to a DEBUG line only, read up to 8 KiB, token masked before + // the cut: a proxy page can be megabytes of multi-line HTML, and can echo the URL. + body, _ := io.ReadAll(io.LimitReader(resp.Body, 8192)) + masked := logging.RedactSecrets(string(body), botToken) + t.logger.Debug("Telegram: api answer (HTTP %d): %s", resp.StatusCode, (&serverbot.Response{Body: []byte(masked)}).Snippet(200)) + return fmt.Errorf("telegram api returned status %d", resp.StatusCode) } return nil From e03cf931af43b36dc96730bfb7f4f44a604f7993 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:18:16 +0100 Subject: [PATCH 09/44] fix(webhook): mask userinfo, escaped forms and header words in the answer excerpt Second round of the automated security review on the DEBUG excerpt of an endpoint's answer: the masking missed the URL's userinfo, the path segments as sent (percent-encoded), the raw query values, the basic-auth header made from AUTH_USER and AUTH_PASS, and a token inside a header value ("Bearer "). Those are masked too, before the cut. DEBUG only. Values under 6 characters stay unmasked, as everywhere in the logger. --- internal/notify/webhook.go | 27 ++++++++++++++---- internal/notify/webhook_response_body_test.go | 28 ++++++++++++------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/internal/notify/webhook.go b/internal/notify/webhook.go index ffb721b3..0869a7e8 100644 --- a/internal/notify/webhook.go +++ b/internal/notify/webhook.go @@ -25,20 +25,37 @@ const webhookMaxResponseBytes = 8 << 10 // webhookBodyExcerpt is the endpoint's answer as the DEBUG line shows it: the endpoint's // own credentials masked first, then printable characters only, on one line, cut to // 200. Masked before the cut, which could split a credential so the logger would no -// longer recognise it. Besides the auth values, the URL's path segments, query values -// and header values are masked: a webhook URL often carries its token in the path, and -// an error page can echo the requested path alone. +// longer recognise it. Besides the auth values (and the basic-auth header they make), +// the URL's userinfo, path segments (decoded and as sent), query values (decoded and +// raw) and header values (whole and word by word, for "Bearer ") are masked: a +// webhook URL often carries its token in the path, and an error page can echo the +// requested path or a header alone. The logger never masks values under 6 characters. func webhookBodyExcerpt(body []byte, endpoint config.WebhookEndpoint) string { - secrets := []string{endpoint.URL, endpoint.Auth.Token, endpoint.Auth.Pass, endpoint.Auth.Secret} + auth := endpoint.Auth + secrets := []string{endpoint.URL, auth.Token, auth.Pass, auth.Secret} + if auth.Pass != "" { + secrets = append(secrets, base64.StdEncoding.EncodeToString([]byte(auth.User+":"+auth.Pass))) + } if u, err := url.Parse(endpoint.URL); err == nil { - secrets = append(secrets, u.Path, u.RawQuery) + if u.User != nil { + password, _ := u.User.Password() + secrets = append(secrets, u.User.Username(), password, u.User.String()) + } + secrets = append(secrets, u.Path, u.EscapedPath(), u.RawQuery) secrets = append(secrets, strings.Split(u.Path, "/")...) + secrets = append(secrets, strings.Split(u.EscapedPath(), "/")...) for _, values := range u.Query() { secrets = append(secrets, values...) } + for _, pair := range strings.FieldsFunc(u.RawQuery, func(r rune) bool { return r == '&' || r == ';' }) { + if _, value, ok := strings.Cut(pair, "="); ok { + secrets = append(secrets, value) + } + } } for _, value := range endpoint.Headers { secrets = append(secrets, value) + secrets = append(secrets, strings.Fields(value)...) } masked := logging.RedactSecrets(string(body), secrets...) return (&serverbot.Response{Body: []byte(masked)}).Snippet(200) diff --git a/internal/notify/webhook_response_body_test.go b/internal/notify/webhook_response_body_test.go index 1f4b6153..eb73b792 100644 --- a/internal/notify/webhook_response_body_test.go +++ b/internal/notify/webhook_response_body_test.go @@ -3,6 +3,7 @@ package notify import ( "bytes" "context" + "encoding/base64" "net/http" "net/http/httptest" "strings" @@ -93,19 +94,25 @@ func TestWebhookFailureLinesNameTheStatusNotTheAnswer(t *testing.T) { } // The DEBUG excerpt masked the endpoint URL whole, but an error page echoes the -// requested path alone, and a webhook URL often carries its token in a path segment; -// query values and header values can be credentials too. +// requested path or a header alone, and a webhook URL often carries its token in a path +// segment; query values, header values, the URL's userinfo and the basic-auth header +// can be credentials too. func TestWebhookAnswerExcerptMasksTheEndpointCredentials(t *testing.T) { - const pathToken = "TokenSegmentAbcdef9" + const pathToken = "Tok%41enSegment99" const queryValue = "QueryValue12345" - const headerValue = "HeaderValue98765" + const rawQueryValue = "Raw%2BValue777" + const bearer = "HeaderBearerTok1234" + const urlPassword = "UrlPassw0rd9" + const basicPass = "BasicPassw0rd8" server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) - _, _ = w.Write([]byte("The requested URL " + r.URL.Path + "?" + r.URL.RawQuery + - " was not found (X-Key: " + r.Header.Get("X-Key") + ")")) + _, _ = w.Write([]byte("The requested URL " + r.URL.EscapedPath() + " (" + r.URL.Path + ")?" + r.URL.RawQuery + + " was not found (X-Key: " + r.Header.Get("X-Key") + ", Authorization: " + r.Header.Get("Authorization") + ")")) })) defer server.Close() + endpointURL := strings.Replace(server.URL, "http://", "http://hookuser:"+urlPassword+"@", 1) + + "/api/webhooks/123456/" + pathToken + "?key=" + queryValue + "&raw=" + rawQueryValue cfg := config.WebhookConfig{ Enabled: true, DefaultFormat: "generic", @@ -113,11 +120,11 @@ func TestWebhookAnswerExcerptMasksTheEndpointCredentials(t *testing.T) { RetryDelay: 1, Endpoints: []config.WebhookEndpoint{{ Name: "ops", - URL: server.URL + "/api/webhooks/123456/" + pathToken + "?key=" + queryValue, + URL: endpointURL, Format: "generic", Method: "POST", - Headers: map[string]string{"X-Key": headerValue}, - Auth: config.WebhookAuth{Type: "none"}, + Headers: map[string]string{"X-Key": "Bearer " + bearer}, + Auth: config.WebhookAuth{Type: "basic", User: "admin", Pass: basicPass}, }}, } var out bytes.Buffer @@ -134,7 +141,8 @@ func TestWebhookAnswerExcerptMasksTheEndpointCredentials(t *testing.T) { if !strings.Contains(log, "Response body (") { t.Fatalf("no DEBUG line with the answer; log:\n%s", log) } - for _, secret := range []string{pathToken, queryValue, headerValue} { + basic := base64.StdEncoding.EncodeToString([]byte("admin:" + basicPass)) + for _, secret := range []string{pathToken, "TokAenSegment99", queryValue, rawQueryValue, "Raw+Value777", bearer, urlPassword, basicPass, basic} { if strings.Contains(log, secret) { t.Fatalf("endpoint credential %q in the log:\n%s", secret, log) } From 55c8a1ae4a3380b6907834331cbc16664a20b14a Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:26:04 +0100 Subject: [PATCH 10/44] fix(webhook): mask the userinfo basic-auth form and bare query pairs in the excerpt Third round of the automated security review on the DEBUG excerpt of an endpoint's answer: the basic-auth header made from the URL's userinfo, and query pairs with no '=' or a credential as key, were not masked. They are now, before the cut. DEBUG only. --- internal/notify/webhook.go | 19 ++++++++++--------- internal/notify/webhook_response_body_test.go | 9 ++++++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/internal/notify/webhook.go b/internal/notify/webhook.go index 0869a7e8..dc43ad6c 100644 --- a/internal/notify/webhook.go +++ b/internal/notify/webhook.go @@ -25,11 +25,12 @@ const webhookMaxResponseBytes = 8 << 10 // webhookBodyExcerpt is the endpoint's answer as the DEBUG line shows it: the endpoint's // own credentials masked first, then printable characters only, on one line, cut to // 200. Masked before the cut, which could split a credential so the logger would no -// longer recognise it. Besides the auth values (and the basic-auth header they make), -// the URL's userinfo, path segments (decoded and as sent), query values (decoded and -// raw) and header values (whole and word by word, for "Bearer ") are masked: a -// webhook URL often carries its token in the path, and an error page can echo the -// requested path or a header alone. The logger never masks values under 6 characters. +// longer recognise it. Besides the auth values, the forms an error page can echo are +// masked: the basic-auth header made from AUTH_USER and AUTH_PASS or from the URL's +// userinfo, the userinfo itself, the path segments (decoded and as sent; a webhook URL +// often carries its token there), the query pairs (decoded and raw, key and value) and +// the header values (whole and word by word, for "Bearer "). The logger never +// masks values under 6 characters. func webhookBodyExcerpt(body []byte, endpoint config.WebhookEndpoint) string { auth := endpoint.Auth secrets := []string{endpoint.URL, auth.Token, auth.Pass, auth.Secret} @@ -39,7 +40,8 @@ func webhookBodyExcerpt(body []byte, endpoint config.WebhookEndpoint) string { if u, err := url.Parse(endpoint.URL); err == nil { if u.User != nil { password, _ := u.User.Password() - secrets = append(secrets, u.User.Username(), password, u.User.String()) + secrets = append(secrets, u.User.Username(), password, u.User.String(), + base64.StdEncoding.EncodeToString([]byte(u.User.Username()+":"+password))) } secrets = append(secrets, u.Path, u.EscapedPath(), u.RawQuery) secrets = append(secrets, strings.Split(u.Path, "/")...) @@ -48,9 +50,8 @@ func webhookBodyExcerpt(body []byte, endpoint config.WebhookEndpoint) string { secrets = append(secrets, values...) } for _, pair := range strings.FieldsFunc(u.RawQuery, func(r rune) bool { return r == '&' || r == ';' }) { - if _, value, ok := strings.Cut(pair, "="); ok { - secrets = append(secrets, value) - } + key, value, _ := strings.Cut(pair, "=") + secrets = append(secrets, pair, key, value) } } for _, value := range endpoint.Headers { diff --git a/internal/notify/webhook_response_body_test.go b/internal/notify/webhook_response_body_test.go index eb73b792..5762fa81 100644 --- a/internal/notify/webhook_response_body_test.go +++ b/internal/notify/webhook_response_body_test.go @@ -107,12 +107,13 @@ func TestWebhookAnswerExcerptMasksTheEndpointCredentials(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) _, _ = w.Write([]byte("The requested URL " + r.URL.EscapedPath() + " (" + r.URL.Path + ")?" + r.URL.RawQuery + - " was not found (X-Key: " + r.Header.Get("X-Key") + ", Authorization: " + r.Header.Get("Authorization") + ")")) + " was not found (X-Key: " + r.Header.Get("X-Key") + ", Authorization: " + r.Header.Get("Authorization") + + ", userinfo: " + base64.StdEncoding.EncodeToString([]byte("hookuser:"+urlPassword)) + ")")) })) defer server.Close() endpointURL := strings.Replace(server.URL, "http://", "http://hookuser:"+urlPassword+"@", 1) + - "/api/webhooks/123456/" + pathToken + "?key=" + queryValue + "&raw=" + rawQueryValue + "/api/webhooks/123456/" + pathToken + "?key=" + queryValue + "&raw=" + rawQueryValue + "&BareQueryToken42" cfg := config.WebhookConfig{ Enabled: true, DefaultFormat: "generic", @@ -142,7 +143,9 @@ func TestWebhookAnswerExcerptMasksTheEndpointCredentials(t *testing.T) { t.Fatalf("no DEBUG line with the answer; log:\n%s", log) } basic := base64.StdEncoding.EncodeToString([]byte("admin:" + basicPass)) - for _, secret := range []string{pathToken, "TokAenSegment99", queryValue, rawQueryValue, "Raw+Value777", bearer, urlPassword, basicPass, basic} { + urlBasic := base64.StdEncoding.EncodeToString([]byte("hookuser:" + urlPassword)) + for _, secret := range []string{pathToken, "TokAenSegment99", queryValue, rawQueryValue, "Raw+Value777", "BareQueryToken42", + bearer, urlPassword, basicPass, basic, urlBasic} { if strings.Contains(log, secret) { t.Fatalf("endpoint credential %q in the log:\n%s", secret, log) } From 93c29670637cd6d1665bd446beb0311a1dad6b70 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:31:13 +0100 Subject: [PATCH 11/44] fix(orchestrator): count the run log past a line longer than 1 MiB ParseLogCounts, whose counts set the exit code, the healthchecks ping and the metrics, read the run log through a bufio.Scanner capped at 1 MiB and never checked its error: a longer line stopped the parse without a word, so that line and every line after it went uncounted. Measured: an ERROR after a 2 MiB WARNING left the exit code at 1 instead of 4; with the long line first the run exited 0 and the healthchecks ping was green, while the footer, which reads the logger's own counters, showed the warnings. Present since the first Go port (3808e6c6). Lines are now read with a bufio.Reader of any length: the first 64 KiB of each is classified (messages are cut to 300 bytes anyway) and the rest is skipped. A read error that is not the end of the file still ends the walk with what was read so far, as before. No visible text. Found while verifying the webhook fix (raw 8 MiB answer in one line). --- internal/orchestrator/log_parser.go | 44 +++++++++++++++---- .../orchestrator/log_parser_long_line_test.go | 42 ++++++++++++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 internal/orchestrator/log_parser_long_line_test.go diff --git a/internal/orchestrator/log_parser.go b/internal/orchestrator/log_parser.go index 1a191f90..e7a65259 100644 --- a/internal/orchestrator/log_parser.go +++ b/internal/orchestrator/log_parser.go @@ -3,6 +3,8 @@ package orchestrator import ( "bufio" "cmp" + "errors" + "io" "os" "slices" "strings" @@ -35,18 +37,14 @@ func ParseLogCounts(logPath string, categoryLimit int) (categories []notify.LogC } defer func() { _ = file.Close() }() - scanner := bufio.NewScanner(file) - buf := make([]byte, 0, 64*1024) - scanner.Buffer(buf, 1024*1024) - categoryMap := make(map[string]*notify.LogCategory) errorCount = 0 warningCount = 0 - for scanner.Scan() { - entryType, message := classifyLogLine(scanner.Text()) + forEachLogLine(file, func(line string) { + entryType, message := classifyLogLine(line) if entryType == "" || message == "" { - continue + return } switch entryType { @@ -62,7 +60,7 @@ func ParseLogCounts(logPath string, categoryLimit int) (categories []notify.LogC label, example := splitCategoryAndExample(message) if label == "" { - continue + return } // A notify-error displays as ERROR even though it is counted separately. @@ -85,7 +83,7 @@ func ParseLogCounts(logPath string, categoryLimit int) (categories []notify.LogC Example: example, } } - } + }) if len(categoryMap) == 0 { return nil, errorCount, warningCount, notifyCount @@ -139,6 +137,34 @@ func logCategorySymbolRank(label string) int { } } +// logLineClassifyBytes is how much of one physical line ParseLogCounts classifies: the +// level and the start of the message are all it needs (messages are cut to 300 bytes, +// labels and examples to 120), and the rest of a longer line is skipped, not held. +const logLineClassifyBytes = 64 * 1024 + +// forEachLogLine calls fn with the first logLineClassifyBytes of every line of r, of any +// length, without its line ending. The bufio.Scanner it replaces stopped at its 1 MiB +// token limit without a word: the long line and every line after it went uncounted, so +// an ERROR logged after it left the exit code and the healthchecks ping at success. A +// read error that is not the end of the file ends the walk, as it did before: what was +// read so far is counted. +func forEachLogLine(r io.Reader, fn func(line string)) { + reader := bufio.NewReaderSize(r, logLineClassifyBytes) + for { + chunk, err := reader.ReadSlice('\n') + line := strings.TrimSuffix(strings.TrimSuffix(string(chunk), "\n"), "\r") + for errors.Is(err, bufio.ErrBufferFull) { + _, err = reader.ReadSlice('\n') + } + if len(chunk) > 0 { + fn(line) + } + if err != nil { + return + } + } +} + // classifyLogLine extracts the entry type and message from a log line // Supports both Go format ("[2025-11-14 10:30:45] WARNING message") and Bash format ("[WARNING] message") func classifyLogLine(line string) (entryType, message string) { diff --git a/internal/orchestrator/log_parser_long_line_test.go b/internal/orchestrator/log_parser_long_line_test.go new file mode 100644 index 00000000..13a1ca17 --- /dev/null +++ b/internal/orchestrator/log_parser_long_line_test.go @@ -0,0 +1,42 @@ +package orchestrator + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// The run log re-parse read lines through a bufio.Scanner capped at 1 MiB and never +// checked its error: a longer line stopped the parse, so it and every line after it went +// uncounted, and an ERROR logged after it left the exit code and the healthchecks ping +// at success. Every line is now read, of any length. +func TestParseLogCountsReadsPastALineLongerThanOneMiB(t *testing.T) { + for _, tc := range []struct { + name string + content string + errors int + warns int + }{ + {"long line in the middle", + "[2026-11-02 10:00:00] WARNING first warning\n" + + "[2026-11-02 10:00:01] WARNING long " + strings.Repeat("x", 2<<20) + "\n" + + "[2026-11-02 10:00:02] ERROR after the long line\r\n" + + "[2026-11-02 10:00:03] WARNING last line without a newline", + 1, 3}, + {"long line first", + "[2026-11-02 10:00:00] WARNING long " + strings.Repeat("x", 2<<20) + "\n" + + "[2026-11-02 10:00:01] WARNING after the long line\n", + 0, 2}, + } { + logPath := filepath.Join(t.TempDir(), "backup.log") + if err := os.WriteFile(logPath, []byte(tc.content), 0o600); err != nil { + t.Fatal(err) + } + categories, errorCount, warningCount, _ := ParseLogCounts(logPath, 0) + if errorCount != tc.errors || warningCount != tc.warns { + t.Fatalf("%s: errors=%d warnings=%d, want errors=%d warnings=%d (categories %+v)", + tc.name, errorCount, warningCount, tc.errors, tc.warns, categories) + } + } +} From 9dce35930da3a34e7728ffba434123a8b405bae1 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:35:00 +0100 Subject: [PATCH 12/44] fix(config): read "$$" in backup.env as a literal "$" Every string value of backup.env goes through os.Expand, and a '$' could not be written at all: quotes are stripped before the expansion, a backslash stays and the name after it is still looked up, and "$$" was consumed whole. A webhook password "p$ssw0rd" reached the endpoint as "p" on every notification, with no line saying the value had been altered; an HMAC secret or token with a '$' broke the same way. "$$" is now a literal '$', in every value (no per-variable exception, as decided for paths). Only values holding "$$" change meaning, and those always expanded to nothing before. Approved docs texts in CONFIGURATION.md: the personal-scripts paragraph ("`$$` is a literal `$`", "Write `$$` where the path really contains a `$`."), the path resolution note, and a new line under the per-endpoint webhook block. Audit finding ui-config-5 (audit-prod-go-2026-10-26). --- docs/CONFIGURATION.md | 13 ++++-- internal/config/config.go | 7 +++ internal/config/config_dollar_escape_test.go | 48 ++++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 internal/config/config_dollar_escape_test.go diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 859a2f04..0112914e 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -313,10 +313,11 @@ arguments, pipes and redirections in the value are not interpreted. Two characte as in every other value in this file. An unquoted `#` truncates the value, so quote a path that contains one. A `$` is expanded and no form of quoting suppresses it, not double quotes, not single quotes, not a backslash: the name is looked up in this file first, then in the daemon's -own environment, and a name neither defines is replaced by nothing, silently. `$$`, `$@`, `$*`, -`$!`, `$?`, `$-`, `$0` to `$9` and a `${` with no closing brace are consumed the same way. Only -a `$` at the end of the value, or one before a character that is neither a letter, a digit, an -underscore nor one of those, survives. Rename the script rather than fighting the expansion. +own environment, and a name neither defines is replaced by nothing, silently. `$$` is a literal +`$`. `$@`, `$*`, `$!`, `$?`, `$-`, `$0` to `$9` and a `${` with no closing brace are consumed the +same way as an unknown name. Only a `$` at the end of the value, or one before a character that +is neither a letter, a digit, an underscore nor one of those, survives. Write `$$` where the path +really contains a `$`. These scripts run only under the daemon, never under a manual `proxsave --backup` or a cron-mode run. Their output is discarded and their exit status does not change the @@ -684,7 +685,7 @@ Retention deletes a run log together with its archive. A run log with no archive (a dry run, a failed run) is deleted once it is older than the oldest backup kept at that destination. -**Path resolution**: `${BASE_DIR}` expands automatically from the installed `proxsave` executable path. Scalar string values also support `$VAR` / `${VAR}` expansion (config keys first, then environment variables), but `BASE_DIR` itself is not configurable from `backup.env` or the parent environment. +**Path resolution**: `${BASE_DIR}` expands automatically from the installed `proxsave` executable path. Scalar string values also support `$VAR` / `${VAR}` expansion (config keys first, then environment variables; `$$` is a literal `$`), but `BASE_DIR` itself is not configurable from `backup.env` or the parent environment. --- @@ -1486,6 +1487,8 @@ WEBHOOK_DISCORD_ALERTS_AUTH_SECRET= # HMAC secret key WEBHOOK_DISCORD_ALERTS_PRIORITY=0 ``` +A `$` in these values is expanded as in every other value of this file: write `$$` for a literal `$`, as in a password or secret that contains one. + **Supported formats**: - **discord**: Discord webhook JSON format - **slack**: Slack incoming webhook format diff --git a/internal/config/config.go b/internal/config/config.go index 569aa738..c69fefa5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1307,8 +1307,15 @@ func newConfigVarExpander(raw map[string]string, baseDir string) *configVarExpan } } +// expand resolves $VAR and ${VAR} in s. "$$" is a literal "$": os.Expand has no escape +// and hands it over as the shell name "$", and with no form of quoting able to protect a +// "$", a credential that holds one (a webhook password, an HMAC secret) could not be +// written at all. func (e *configVarExpander) expand(s string) string { return os.Expand(s, func(key string) string { + if key == "$" { + return "$" + } return e.resolve(key) }) } diff --git a/internal/config/config_dollar_escape_test.go b/internal/config/config_dollar_escape_test.go new file mode 100644 index 00000000..9ef5005d --- /dev/null +++ b/internal/config/config_dollar_escape_test.go @@ -0,0 +1,48 @@ +package config + +import ( + "os" + "path/filepath" + "testing" +) + +// A '$' could not be written in any backup.env value: quotes are stripped before the +// expansion, a backslash stays and the name after it is still looked up, and "$$" was +// consumed whole, so a webhook password "p$ssw0rd" reached the endpoint as "p". "$$" is +// now a literal '$', in every value. +func TestConfigDoubleDollarIsALiteralDollar(t *testing.T) { + envPath := filepath.Join(t.TempDir(), "backup.env") + content := `WEBHOOK_ENABLED=true +WEBHOOK_ENDPOINTS=ops +WEBHOOK_OPS_URL=https://hooks.example.invalid/x?k=a$$b +WEBHOOK_OPS_AUTH_TYPE=basic +WEBHOOK_OPS_AUTH_USER=admin +WEBHOOK_OPS_AUTH_PASS='p$$ssw0rd' +WEBHOOK_OPS_AUTH_SECRET="ab$$$$cd" +WEBHOOK_OPS_AUTH_TOKEN=tok$$ +WEBHOOK_OPS_HEADERS=X-Key:v$$al +` + if err := os.WriteFile(envPath, []byte(content), 0o600); err != nil { + t.Fatal(err) + } + cfg, err := LoadConfigWithBaseDir(envPath, t.TempDir()) + if err != nil { + t.Fatalf("LoadConfigWithBaseDir: %v", err) + } + wh := cfg.BuildWebhookConfig() + if len(wh.Endpoints) != 1 { + t.Fatalf("endpoints = %d, want 1", len(wh.Endpoints)) + } + ep := wh.Endpoints[0] + for _, tc := range []struct{ name, got, want string }{ + {"URL", ep.URL, "https://hooks.example.invalid/x?k=a$b"}, + {"AUTH_PASS", ep.Auth.Pass, "p$ssw0rd"}, + {"AUTH_SECRET", ep.Auth.Secret, "ab$$cd"}, + {"AUTH_TOKEN", ep.Auth.Token, "tok$"}, + {"HEADERS", ep.Headers["X-Key"], "v$al"}, + } { + if tc.got != tc.want { + t.Errorf("%s = %q, want %q", tc.name, tc.got, tc.want) + } + } +} From 26f9010729b077cfb6b23b686c0cd0315d8936c9 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Mon, 2 Nov 2026 22:36:39 +0100 Subject: [PATCH 13/44] style(upgrade): use errors.AsType for the GitHub status error Lint (modernize): errors.As with a declared target becomes errors.AsType in the two callers added by ed68c42c, and the startup Telegram test ranges over strings.SplitSeq. --- cmd/proxsave/main_update.go | 3 +-- cmd/proxsave/telegram_server_status_body_test.go | 2 +- cmd/proxsave/upgrade.go | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/cmd/proxsave/main_update.go b/cmd/proxsave/main_update.go index bfb5e128..04fa5e98 100644 --- a/cmd/proxsave/main_update.go +++ b/cmd/proxsave/main_update.go @@ -80,8 +80,7 @@ func checkForUpdates(ctx context.Context, logger *logging.Logger, currentVersion latestTag, latestVersion, releaseBody, err := fetchLatestRelease(checkCtx) if err != nil { - var statusErr *githubStatusError - if errors.As(err, &statusErr) { + if statusErr, ok := errors.AsType[*githubStatusError](err); ok { logger.Debug("Update check: latest release response body=%q", statusErr.excerpt) } logger.Debug("Update check skipped: GitHub unreachable: %v", err) diff --git a/cmd/proxsave/telegram_server_status_body_test.go b/cmd/proxsave/telegram_server_status_body_test.go index c94d2e96..ffa6cb25 100644 --- a/cmd/proxsave/telegram_server_status_body_test.go +++ b/cmd/proxsave/telegram_server_status_body_test.go @@ -68,7 +68,7 @@ func TestTelegramServerStatusKeepsTheServerBodyOutOfTheWarning(t *testing.T) { t.Fatalf("HTTP %d: raw server body (%q) in the log:\n%s", tc.status, raw, log) } } - for _, line := range strings.Split(strings.TrimSuffix(log, "\n"), "\n") { + for line := range strings.SplitSeq(strings.TrimSuffix(log, "\n"), "\n") { if !strings.HasPrefix(line, "[") { t.Fatalf("HTTP %d: the body split a line, %q has no prefix; log:\n%s", tc.status, line, log) } diff --git a/cmd/proxsave/upgrade.go b/cmd/proxsave/upgrade.go index e348f2bd..2ab173dd 100644 --- a/cmd/proxsave/upgrade.go +++ b/cmd/proxsave/upgrade.go @@ -229,8 +229,7 @@ func upgradeAcquireBinary(ctx context.Context, args *cli.Args, bootstrap *loggin logging.DebugStepBootstrap(bootstrap, "upgrade workflow", "fetching latest release info") tag, latestVersion, _, err := fetchLatestRelease(ctx) if err != nil { - var statusErr *githubStatusError - if errors.As(err, &statusErr) { + if statusErr, ok := errors.AsType[*githubStatusError](err); ok { logging.DebugStepBootstrap(bootstrap, "upgrade workflow", "latest release response body=%q", statusErr.excerpt) } // Four of fetchLatestRelease's five errors open with "failed to ...", so a From acc66aec2038727bd83e136df58061d899d77e05 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 11:00:00 +0100 Subject: [PATCH 14/44] fix(storage): find a path's mount by device, so a symlinked destination gets its target's filesystem Filesystem detection matched the path string against /proc/mounts and never followed a symlink. A BACKUP_PATH, SECONDARY_PATH or log path that is a link into an NFS root_squash share was seen as the filesystem holding the link (ext4, with ownership): the security check then tried to fix the share's owner, refused to chown the symlink with an ERROR, and with the defaults every run stopped at exit 14 before the backup (measured on pve-test). Primary and secondary also warned "permissions not set" on every run. The mount is now found by device: the st_dev of the path (the stat follows links) against /proc/self/mountinfo, the deepest entry holding the resolved path. A device no mountinfo entry carries (btrfs subvolumes report their own) falls back to the path match, on the resolved path. Measured on pve-test after the fix: "nfs4 (no ownership)", the security check skips the share, 0 errors. The Filesystem line names the target when the path is reached through a link. Approved text: Filesystem: nfs4 (no ownership) [network] [mount: /mnt/s6nfs] [link to: /mnt/s6nfs/proxsave] Audit finding storage-6 (audit-prod-go-2026-10-26). --- cmd/proxsave/filesystem_link_line_test.go | 26 ++++++ cmd/proxsave/runtime_helpers.go | 7 +- internal/storage/filesystem.go | 92 ++++++++++++++++--- internal/storage/filesystem_symlink_test.go | 98 +++++++++++++++++++++ internal/storage/filesystem_test.go | 6 +- internal/storage/storage.go | 2 + 6 files changed, 216 insertions(+), 15 deletions(-) create mode 100644 cmd/proxsave/filesystem_link_line_test.go create mode 100644 internal/storage/filesystem_symlink_test.go diff --git a/cmd/proxsave/filesystem_link_line_test.go b/cmd/proxsave/filesystem_link_line_test.go new file mode 100644 index 00000000..1e1ba628 --- /dev/null +++ b/cmd/proxsave/filesystem_link_line_test.go @@ -0,0 +1,26 @@ +package main + +import ( + "testing" + + "github.com/tis24dev/proxsave/internal/storage" +) + +// A storage path reached through a symlink names the link's target on its +// Filesystem line, after the mount. +func TestFilesystemLineNamesTheLinkTarget(t *testing.T) { + info := &storage.FilesystemInfo{ + Type: storage.FilesystemType("nfs4"), + IsNetworkFS: true, + MountPoint: "/mnt/s6nfs", + LinkTarget: "/mnt/s6nfs/proxsave", + } + want := "nfs4 (no ownership) [network] [mount: /mnt/s6nfs] [link to: /mnt/s6nfs/proxsave]" + if got := formatFilesystemDetail(info); got != want { + t.Fatalf("formatFilesystemDetail = %q, want %q", got, want) + } + info.LinkTarget = "" + if got := formatFilesystemDetail(info); got != "nfs4 (no ownership) [network] [mount: /mnt/s6nfs]" { + t.Fatalf("without a link: %q", got) + } +} diff --git a/cmd/proxsave/runtime_helpers.go b/cmd/proxsave/runtime_helpers.go index fe5e1301..83d1ed25 100644 --- a/cmd/proxsave/runtime_helpers.go +++ b/cmd/proxsave/runtime_helpers.go @@ -415,7 +415,12 @@ func formatFilesystemDetail(info *storage.FilesystemInfo) string { mount := info.MountPoint mount = cmp.Or(mount, "unknown") - return fmt.Sprintf("%s (%s)%s [mount: %s]", info.Type, ownership, network, mount) + link := "" + if info.LinkTarget != "" { + link = " [link to: " + info.LinkTarget + "]" + } + + return fmt.Sprintf("%s (%s)%s [mount: %s]%s", info.Type, ownership, network, mount, link) } // logStoragePath opens a storage-init block: the path line, then the configuration diff --git a/internal/storage/filesystem.go b/internal/storage/filesystem.go index be857f70..82cda4e7 100644 --- a/internal/storage/filesystem.go +++ b/internal/storage/filesystem.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strconv" "strings" + "syscall" "time" "github.com/tis24dev/proxsave/internal/logging" @@ -77,17 +78,17 @@ func NewFilesystemDetector(logger *logging.Logger, opts ...DetectorOption) *File // This function logs the detected filesystem type in real-time func (d *FilesystemDetector) DetectFilesystem(ctx context.Context, path string) (*FilesystemInfo, error) { // Ensure path exists. Bounded so a dead/stale mount does not wedge here. - if _, err := safefs.Stat(ctx, path, d.ioTimeout); err != nil { + pathInfo, err := safefs.Stat(ctx, path, d.ioTimeout) + if err != nil { return nil, fmt.Errorf("path does not exist: %s: %w", path, err) } // Get mount point for this path - var mountPoint string - var err error + var mountPoint, linkTarget string if d.mountPointLookup != nil { mountPoint, err = d.mountPointLookup(path) } else { - mountPoint, err = d.getMountPoint(path) + mountPoint, linkTarget, err = d.getMountPoint(ctx, path, pathInfo) } if err != nil { return nil, fmt.Errorf("failed to get mount point for %s: %w", path, err) @@ -112,6 +113,7 @@ func (d *FilesystemDetector) DetectFilesystem(ctx context.Context, path string) IsNetworkFS: fsType.IsNetworkFilesystem(), MountPoint: mountPoint, Device: device, + LinkTarget: linkTarget, } // Log the detected filesystem in real-time (live output) @@ -158,30 +160,94 @@ func (d *FilesystemDetector) logFilesystemInfo(info *FilesystemInfo) { network = " [network]" } + link := "" + if info.LinkTarget != "" { + link = " [link to: " + info.LinkTarget + "]" + } + // Log in format: "Path: /path/to/backup -> Filesystem: ext4 (supports ownership)" (debug-level, detailed summary printed elsewhere) - d.logger.Debug("Path: %s -> Filesystem: %s (%s)%s [mount: %s]", + d.logger.Debug("Path: %s -> Filesystem: %s (%s)%s [mount: %s]%s", info.Path, info.Type, ownership, network, info.MountPoint, + link, ) } -// getMountPoint finds the mount point for a given path -func (d *FilesystemDetector) getMountPoint(path string) (string, error) { - // Read /proc/mounts to find mount points - data, err := os.ReadFile("/proc/mounts") +// getMountPoint finds the mount point that holds path, and where path resolves when a +// symlink is on the way ("" when none is). The mount is found by device: the st_dev of +// path (the stat follows links) against /proc/self/mountinfo. Matching the path string +// against /proc/mounts gave a BACKUP_PATH that is a link into an NFS share the type of +// the filesystem holding the link: ext4 with ownership, so the security check tried to +// chown the root_squash share and the run stopped with exit 14. A device no mountinfo +// entry carries (btrfs subvolumes report their own) falls back to the deepest mount +// whose path holds the resolved path. +func (d *FilesystemDetector) getMountPoint(ctx context.Context, path string, pathInfo os.FileInfo) (mountPoint, linkTarget string, err error) { + absPath, err := filepath.Abs(path) if err != nil { - return "", err + absPath = path + } + resolved, rerr := safefs.Run(ctx, "resolve symlinks", absPath, d.ioTimeout, func() (string, error) { + return filepath.EvalSymlinks(absPath) + }) + if rerr != nil { + d.logger.Debug("Filesystem detection: cannot resolve %s: %v; matching it as written", absPath, rerr) + resolved = absPath + } + if resolved != absPath { + linkTarget = resolved } - absPath, err := filepath.Abs(path) + if st, ok := pathInfo.Sys().(*syscall.Stat_t); ok { + if data, merr := os.ReadFile("/proc/self/mountinfo"); merr == nil { + devID := deviceID(uint64(st.Dev)) + if mp, found := mountPointForDevice(strings.Split(string(data), "\n"), devID, resolved); found { + d.logger.Debug("Filesystem detection: %s is on device %s, mounted at %s", path, devID, mp) + return mp, linkTarget, nil + } + d.logger.Debug("Filesystem detection: no mountinfo entry for device %s of %s; matching %s by path", devID, path, resolved) + } else { + d.logger.Debug("Filesystem detection: cannot read /proc/self/mountinfo: %v; matching %s by path", merr, resolved) + } + } + + data, err := os.ReadFile("/proc/mounts") if err != nil { - absPath = path + return "", "", err } + return bestMountPointFor(strings.Split(string(data), "\n"), resolved), linkTarget, nil +} - return bestMountPointFor(strings.Split(string(data), "\n"), absPath), nil +// deviceID renders a device number as mountinfo's "major:minor", with glibc's +// major()/minor() split of the Linux dev_t. +func deviceID(dev uint64) string { + major := (dev>>8)&0xfff | (dev>>32)&^uint64(0xfff) + minor := dev&0xff | (dev>>12)&^uint64(0xff) + return fmt.Sprintf("%d:%d", major, minor) +} + +// mountPointForDevice picks, among the /proc/self/mountinfo lines of device devID, the +// deepest mount point that holds resolved; when none does (a bind mount reached through +// another path), the last entry of the device. found is false when no line has devID. +func mountPointForDevice(lines []string, devID, resolved string) (mountPoint string, found bool) { + best, last := "", "" + for _, line := range lines { + fields := strings.Fields(line) + if len(fields) < 5 || fields[2] != devID { + continue + } + mp := unescapeOctal(fields[4]) + last = mp + if (mp == "/" || resolved == mp || strings.HasPrefix(resolved, mp+"/")) && len(mp) > len(best) { + best = mp + } + } + if best != "" { + return best, true + } + return last, last != "" } // bestMountPointFor picks the deepest mount point owning absPath from /proc/mounts diff --git a/internal/storage/filesystem_symlink_test.go b/internal/storage/filesystem_symlink_test.go new file mode 100644 index 00000000..2d8a4fb0 --- /dev/null +++ b/internal/storage/filesystem_symlink_test.go @@ -0,0 +1,98 @@ +package storage + +import ( + "context" + "os" + "path/filepath" + "runtime" + "syscall" + "testing" +) + +// A BACKUP_PATH reached through a symlink got the type of the filesystem holding the +// link: the mount was looked up by matching the path string against /proc/mounts. On +// a link into an NFS root_squash share that read ext4 with ownership, and the security +// check stopped every run with exit 14. The mount is now found by device, so the link +// gets its target's filesystem, and the target is recorded for the Filesystem line. +func TestDetectFilesystemFollowsASymlinkToAnotherFilesystem(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip("depends on /proc/self/mountinfo") + } + linkDir := t.TempDir() + var target string + for _, base := range []string{"/dev/shm", "/run", "/tmp"} { + dir, err := os.MkdirTemp(base, "proxsave-fs-link-") + if err != nil { + continue + } + if deviceOf(t, dir) != deviceOf(t, linkDir) { + target = dir + break + } + _ = os.Remove(dir) + } + if target == "" { + t.Skip("no writable directory on a filesystem other than the test's temp dir") + } + t.Cleanup(func() { _ = os.RemoveAll(target) }) + link := filepath.Join(linkDir, "backup") + if err := os.Symlink(target, link); err != nil { + t.Fatal(err) + } + + detector := NewFilesystemDetector(newTestLogger()) + direct, err := detector.DetectFilesystem(context.Background(), target) + if err != nil { + t.Fatalf("DetectFilesystem(%s): %v", target, err) + } + viaLink, err := detector.DetectFilesystem(context.Background(), link) + if err != nil { + t.Fatalf("DetectFilesystem(%s): %v", link, err) + } + if viaLink.Type != direct.Type || viaLink.MountPoint != direct.MountPoint || viaLink.Device != direct.Device { + t.Fatalf("through the link: type=%s mount=%s device=%s; the target is type=%s mount=%s device=%s", + viaLink.Type, viaLink.MountPoint, viaLink.Device, direct.Type, direct.MountPoint, direct.Device) + } + want, _ := filepath.EvalSymlinks(target) + if viaLink.LinkTarget != want || direct.LinkTarget != "" { + t.Fatalf("LinkTarget: via link %q (want %q), direct %q (want empty)", viaLink.LinkTarget, want, direct.LinkTarget) + } +} + +func deviceOf(t *testing.T, path string) uint64 { + t.Helper() + fi, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + return uint64(fi.Sys().(*syscall.Stat_t).Dev) +} + +func TestMountPointForDevicePicksTheMountHoldingTheResolvedPath(t *testing.T) { + lines := []string{ + "22 1 0:21 / /proc rw,nosuid - proc proc rw", + "25 1 253:1 / / rw,relatime - ext4 /dev/mapper/pve-root rw", + "60 25 0:53 / /mnt/pve/nas rw,relatime - nfs4 10.0.0.5:/export rw", + "61 25 0:53 /proxsave /var/lib/lxc/101/rootfs/mnt/backup rw - nfs4 10.0.0.5:/export rw", + "62 25 0:60 / /mnt/space\\040dir rw - tmpfs tmpfs rw", + } + for _, tc := range []struct { + dev, resolved, want string + found bool + }{ + {"0:53", "/mnt/pve/nas/proxsave", "/mnt/pve/nas", true}, + {"0:53", "/var/lib/lxc/101/rootfs/mnt/backup/x", "/var/lib/lxc/101/rootfs/mnt/backup", true}, + {"0:53", "/elsewhere", "/var/lib/lxc/101/rootfs/mnt/backup", true}, + {"0:60", "/mnt/space dir/proxsave", "/mnt/space dir", true}, + {"253:1", "/var/lib/proxsave/backup", "/", true}, + {"0:99", "/var/lib/proxsave/backup", "", false}, + } { + got, found := mountPointForDevice(lines, tc.dev, tc.resolved) + if got != tc.want || found != tc.found { + t.Errorf("mountPointForDevice(%s, %s) = %q, %v; want %q, %v", tc.dev, tc.resolved, got, found, tc.want, tc.found) + } + } + if got := deviceID(uint64(253<<8 | 1)); got != "253:1" { + t.Errorf("deviceID = %q, want 253:1", got) + } +} diff --git a/internal/storage/filesystem_test.go b/internal/storage/filesystem_test.go index 1383b79f..2657bf50 100644 --- a/internal/storage/filesystem_test.go +++ b/internal/storage/filesystem_test.go @@ -135,7 +135,11 @@ func TestFilesystemDetectorGetMountPoint_PicksProcForProcPaths(t *testing.T) { t.Skip("this test depends on /proc mounts") } detector := NewFilesystemDetector(newTestLogger()) - mp, err := detector.getMountPoint("/proc/self") + fi, err := os.Stat("/proc/self") + if err != nil { + t.Fatal(err) + } + mp, _, err := detector.getMountPoint(context.Background(), "/proc/self", fi) if err != nil { t.Fatalf("getMountPoint error: %v", err) } diff --git a/internal/storage/storage.go b/internal/storage/storage.go index e94840a3..b053eb76 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -50,6 +50,8 @@ type FilesystemInfo struct { IsNetworkFS bool MountPoint string Device string + // LinkTarget is where Path resolves when a symlink is on the way, "" otherwise. + LinkTarget string } // BackupLocation represents a location where backups are stored From 62d5b52378c11e8e19051937ddec3c79660f5af9 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 11:11:40 +0100 Subject: [PATCH 15/44] fix(cli): refuse --dry-run with the modes that ignored it; --upgrade-config previews Six modes return before the runtime, where --dry-run is honoured, and none of them read the flag, so they wrote for real while the operator asked for a simulation. Measured on pve-test: --upgrade-config and --upgrade-config-json rewrote backup.env, --newkey replaced the recipient, --new-install deleted backup/, configs/ and log/. --upgrade-config --dry-run now runs the existing --upgrade-config-dry-run preview. The other five are refused with exit 2, like --restore and --upgrade. Approved texts: --dry-run is not supported with --install or --new-install: the installer always writes the installation. --dry-run is not supported with --newkey: it always replaces the encryption recipient. --dry-run is not supported with --decrypt: it always writes the decrypted archive. --dry-run is not supported with --upgrade-config-json: use --upgrade-config-dry-run to preview the changes. docs/CLI_REFERENCE.md lists the refused modes and the preview (approved). --upgrade calls --upgrade-config-json without --dry-run: unaffected. Audit finding cmd-main-5 (audit-prod-go-2026-10-26). --- cmd/proxsave/dryrun_ignored_modes_test.go | 76 +++++++++++++++++++++++ cmd/proxsave/main_config_modes.go | 3 +- cmd/proxsave/main_modes.go | 27 ++++++++ docs/CLI_REFERENCE.md | 10 +-- 4 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 cmd/proxsave/dryrun_ignored_modes_test.go diff --git a/cmd/proxsave/dryrun_ignored_modes_test.go b/cmd/proxsave/dryrun_ignored_modes_test.go new file mode 100644 index 00000000..f0f225a4 --- /dev/null +++ b/cmd/proxsave/dryrun_ignored_modes_test.go @@ -0,0 +1,76 @@ +package main + +import ( + "bytes" + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/cli" + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// Six modes returned before the runtime, where --dry-run is honoured, and wrote for +// real: --newkey replaced the recipient, --new-install deleted backup/, configs/ and +// log/, --upgrade-config and --upgrade-config-json rewrote backup.env. Five are now +// refused; --upgrade-config --dry-run runs the preview that already exists. +func TestDryRunIsRefusedWithTheModesThatIgnoredIt(t *testing.T) { + for _, tc := range []struct { + args cli.Args + want string + }{ + {cli.Args{Install: true, DryRun: true}, "--dry-run is not supported with --install or --new-install: the installer always writes the installation."}, + {cli.Args{NewInstall: true, DryRun: true}, "--dry-run is not supported with --install or --new-install: the installer always writes the installation."}, + {cli.Args{ForceNewKey: true, DryRun: true}, "--dry-run is not supported with --newkey: it always replaces the encryption recipient."}, + {cli.Args{Decrypt: true, DryRun: true}, "--dry-run is not supported with --decrypt: it always writes the decrypted archive."}, + {cli.Args{UpgradeConfigJSON: true, DryRun: true}, "--dry-run is not supported with --upgrade-config-json: use --upgrade-config-dry-run to preview the changes."}, + } { + messages := validateModeCompatibility(&tc.args) + if len(messages) != 1 || messages[0] != tc.want { + t.Errorf("%+v: messages %q, want [%q]", tc.args, messages, tc.want) + } + } + for _, args := range []cli.Args{ + {Install: true}, {NewInstall: true}, {ForceNewKey: true}, {Decrypt: true}, {UpgradeConfigJSON: true}, + {UpgradeConfig: true, DryRun: true}, + } { + for _, m := range validateModeCompatibility(&args) { + if strings.Contains(m, "--dry-run is not supported") { + t.Errorf("a dry-run guard fired on %+v: %s", args, m) + } + } + } +} + +func TestUpgradeConfigWithDryRunRunsThePreview(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "backup.env") + before := []byte("BACKUP_PATH=/var/lib/proxsave/backup\n") + if err := os.WriteFile(configPath, before, 0o600); err != nil { + t.Fatal(err) + } + var out bytes.Buffer + bootstrap := logging.NewBootstrapLogger() + bootstrap.SetConsoleQuiet(true) + mirror := logging.New(types.LogLevelDebug, false) + mirror.SetOutput(&out) + bootstrap.SetMirrorLogger(mirror) + + args := &cli.Args{UpgradeConfig: true, DryRun: true, ConfigPath: configPath} + code, handled := runUpgradeConfigDryMode(context.Background(), args, bootstrap, "") + if !handled || code != types.ExitSuccess.Int() { + t.Fatalf("handled=%v code=%d, want the preview to run; log:\n%s", handled, code, out.String()) + } + after, err := os.ReadFile(configPath) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(after, before) { + t.Fatalf("backup.env changed under --dry-run:\n%s", after) + } + if !strings.Contains(out.String(), "Dry run only: no files were modified.") { + t.Fatalf("no preview line; log:\n%s", out.String()) + } +} diff --git a/cmd/proxsave/main_config_modes.go b/cmd/proxsave/main_config_modes.go index 787bf131..d9f2f3c9 100644 --- a/cmd/proxsave/main_config_modes.go +++ b/cmd/proxsave/main_config_modes.go @@ -158,7 +158,8 @@ func reportConfigFindingsTheMergeLeaves(bootstrap *logging.BootstrapLogger, conf } func runUpgradeConfigDryMode(_ context.Context, args *cli.Args, bootstrap *logging.BootstrapLogger, _ string) (int, bool) { - if !args.UpgradeConfigDry { + // --upgrade-config --dry-run is this preview: the merge itself never read the flag. + if !args.UpgradeConfigDry && (!args.UpgradeConfig || !args.DryRun) { return types.ExitSuccess.Int(), false } logging.DebugStepBootstrap(bootstrap, "main run", "mode=upgrade-config-dry") diff --git a/cmd/proxsave/main_modes.go b/cmd/proxsave/main_modes.go index f8e10545..97e8f10b 100644 --- a/cmd/proxsave/main_modes.go +++ b/cmd/proxsave/main_modes.go @@ -71,6 +71,7 @@ func validateModeCompatibility(args *cli.Args) []string { validateInstallCompatibility, validateUpgradeCompatibility, validateRestoreCompatibility, + validateDryRunCompatibility, validateDaemonCompatibility, } { if messages := rule(args); len(messages) > 0 { @@ -196,6 +197,32 @@ func validateRestoreCompatibility(args *cli.Args) []string { return nil } +// validateDryRunCompatibility refuses --dry-run with the modes that never read it, for +// the reason given for --upgrade above. They returned before the runtime, where the +// flag is honoured, and wrote for real: --newkey replaced the recipient, --new-install +// deleted backup/, configs/ and log/, --upgrade-config-json rewrote backup.env. +// --upgrade-config is not refused: with --dry-run it runs its existing preview +// (runUpgradeConfigDryMode), which is what the flag asks for. +func validateDryRunCompatibility(args *cli.Args) []string { + if !args.DryRun { + return nil + } + var messages []string + if args.Install || args.NewInstall { + messages = append(messages, "--dry-run is not supported with --install or --new-install: the installer always writes the installation.") + } + if args.ForceNewKey { + messages = append(messages, "--dry-run is not supported with --newkey: it always replaces the encryption recipient.") + } + if args.Decrypt { + messages = append(messages, "--dry-run is not supported with --decrypt: it always writes the decrypted archive.") + } + if args.UpgradeConfigJSON { + messages = append(messages, "--dry-run is not supported with --upgrade-config-json: use --upgrade-config-dry-run to preview the changes.") + } + return messages +} + func validateDaemonCompatibility(args *cli.Args) []string { daemonFlags := 0 label := "" diff --git a/docs/CLI_REFERENCE.md b/docs/CLI_REFERENCE.md index 0e4a49a2..7f0c8fb2 100644 --- a/docs/CLI_REFERENCE.md +++ b/docs/CLI_REFERENCE.md @@ -23,9 +23,11 @@ the combinations that would contradict each other rather than picking one silent of `--daemon`, `--daemon-setup`, `--daemon-remove`, `--daemon-status` at a time and none of them with another mode; `--install` not with `--new-install` or `--upgrade`; `--cleanup-guards` and `--support` each with their own short list of incompatible modes; and -`--dry-run` refused with `--upgrade`, `--upgrade-finalize`, `--daemon`, `--daemon-setup` and -`--daemon-remove`, none of which have ever honoured it, and with `--restore`, which cannot run -without modifying the system (`DRY_RUN=true` refuses a restore too). Any other two operation +`--dry-run` refused with `--upgrade`, `--upgrade-finalize`, `--daemon`, `--daemon-setup`, +`--daemon-remove`, `--install`, `--new-install`, `--newkey`, `--decrypt` and +`--upgrade-config-json`, none of which have ever honoured it, and with `--restore`, which cannot +run without modifying the system (`DRY_RUN=true` refuses a restore too); `--upgrade-config +--dry-run` runs the `--upgrade-config-dry-run` preview. Any other two operation modes in one run are refused as well, for example `--restore` with `--decrypt`. The run stops with the reason printed. Modifiers (`--config`, `--log-level`, `--cli`) do combine with the mode they apply to. @@ -86,7 +88,7 @@ means the flag has no menu row. | `--help` | `-h` | - | Show help message | | `--version` | `-v` | - | Display version information | | `--config ` | `-c` | - | Path to configuration file. Passing it suppresses the dashboard, like any other flag | -| `--dry-run` | `-n` | - | Simulate a backup without creating or uploading archives; diagnostic logs are still written. Refused with `--upgrade`, `--upgrade-finalize`, `--daemon`, `--daemon-setup`, `--daemon-remove` and `--restore` | +| `--dry-run` | `-n` | - | Simulate a backup without creating or uploading archives; diagnostic logs are still written. Refused with `--upgrade`, `--upgrade-finalize`, `--daemon`, `--daemon-setup`, `--daemon-remove`, `--restore`, `--install`, `--new-install`, `--newkey`, `--decrypt` and `--upgrade-config-json`; with `--upgrade-config` it runs the `--upgrade-config-dry-run` preview | | `--log-level ` | `-l` | - | Set log level (debug\|info\|warning\|error\|critical) | | `--cli` | - | - | Force CLI mode instead of TUI (only for: --install, --new-install, --newkey, --decrypt, --restore) | | `--install` | - | Install > Edit install | Interactive installation wizard | From c96df24b5c63e03207ef0c9b776499dee1c1393a Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 11:23:53 +0100 Subject: [PATCH 16/44] fix(install): read the crontab back before removing the daemon on a switch to cron The install's cron path wrote the cron line, which reports its failures only as WARNINGs, and then removed the daemon unit whenever one was installed. With a crontab that refused the new table (Debian cron rejects the whole table for one bad line) or no crontab binary, the host ended with no daemon and no cron line, under a green "Installation completed". --daemon-remove already reads the crontab back (5098c4d2); the install path never got it. The crontab is now read back after the write. Without the proxsave line the daemon is kept, so it keeps scheduling the backup, and the install fails; a first cron install without the line fails too. Approved texts: WARNING daemon: the cron entry could not be written, so the daemon service was left in place and keeps scheduling the backup. ERROR the cron entry could not be written ERROR the cron entry could not be written, so no scheduler is installed followed by the existing "Installation failed" banner. Audit finding cmd-main-7 (audit-prod-go-2026-10-26). --- .../install_cron_switch_readback_test.go | 113 ++++++++++++++++++ cmd/proxsave/install_runtime.go | 12 ++ 2 files changed, 125 insertions(+) create mode 100644 cmd/proxsave/install_cron_switch_readback_test.go diff --git a/cmd/proxsave/install_cron_switch_readback_test.go b/cmd/proxsave/install_cron_switch_readback_test.go new file mode 100644 index 00000000..06b1d62d --- /dev/null +++ b/cmd/proxsave/install_cron_switch_readback_test.go @@ -0,0 +1,113 @@ +package main + +import ( + "bytes" + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// Switching an install to cron removed the daemon unit without reading the crontab +// back, and the cron write reports its failures only as WARNINGs: with a crontab that +// refused the new table the host ended with no scheduler under "Installation +// completed". The crontab is now read back: without the line the daemon is kept and +// the install fails; a first cron install without the line fails too. +func TestInstallCronSwitchReadsTheCrontabBack(t *testing.T) { + for _, tc := range []struct { + name string + daemon bool + writeOK bool + wantErr string + wantUnit bool + wantWarn bool + }{ + {"daemon host, cron refused", true, false, "the cron entry could not be written", true, true}, + {"first cron install, cron refused", false, false, "the cron entry could not be written, so no scheduler is installed", false, false}, + {"daemon host, cron written", true, true, "", false, false}, + } { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + bin := filepath.Join(dir, "fakebin") + if err := os.MkdirAll(bin, 0o755); err != nil { + t.Fatal(err) + } + table := filepath.Join(dir, "crontab-table") + if err := os.WriteFile(table, []byte("0 4 * * * /root/scripts/other.sh\n"), 0o600); err != nil { + t.Fatal(err) + } + write := "cat > /dev/null\necho \"errors in crontab file, can't install.\" >&2\nexit 1\n" + if tc.writeOK { + write = "cat > \"" + table + "\"\nexit 0\n" + } + crontab := "#!/bin/sh\nif [ \"$1\" = \"-l\" ]; then cat \"" + table + "\"; exit 0; fi\n" + write + sysLog := filepath.Join(dir, "systemctl.log") + systemctl := "#!/bin/sh\necho \"$@\" >> \"" + sysLog + "\"\nexit 0\n" + if err := os.WriteFile(filepath.Join(bin, "crontab"), []byte(crontab), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(bin, "systemctl"), []byte(systemctl), 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", bin+string(os.PathListSeparator)+"/usr/bin:/bin") + + globals := filepath.Join(dir, "global") + if err := os.MkdirAll(globals, 0o755); err != nil { + t.Fatal(err) + } + prevGlobals := globalEntrypointDirs + globalEntrypointDirs = []string{globals} + t.Cleanup(func() { globalEntrypointDirs = prevGlobals }) + + unit := filepath.Join(dir, "proxsave.service") + if tc.daemon { + if err := os.WriteFile(unit, []byte("[Unit]\n"), 0o600); err != nil { + t.Fatal(err) + } + } + prevUnit := daemonUnitPath + daemonUnitPath = unit + t.Cleanup(func() { daemonUnitPath = prevUnit }) + + executable := filepath.Join(dir, "proxsave") + if err := os.WriteFile(executable, []byte("binary"), 0o700); err != nil { + t.Fatal(err) + } + cfgPath := filepath.Join(dir, "backup.env") + if err := os.WriteFile(cfgPath, []byte("BASE_DIR="+dir+"\nSCHEDULER_MODE=cron\nHEALTHCHECK_ENABLED=false\n"), 0o600); err != nil { + t.Fatal(err) + } + + prevLogger := logging.GetDefaultLogger() + var out bytes.Buffer + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&out) + logging.SetDefaultLogger(logger) + t.Cleanup(func() { logging.SetDefaultLogger(prevLogger) }) + + p := newInstallRuntimePreparation(cfgPath, dir, ExecInfo{ExecPath: executable}, logging.NewBootstrapLogger()) + p.cronSchedule = "0 2 * * *" + err := p.Finalize(context.Background()) + + gotErr := "" + if err != nil { + gotErr = err.Error() + } + if gotErr != tc.wantErr { + t.Fatalf("Finalize error %q, want %q; log:\n%s", gotErr, tc.wantErr, out.String()) + } + _, unitErr := os.Stat(unit) + if (unitErr == nil) != tc.wantUnit { + t.Fatalf("daemon unit present=%v, want %v; log:\n%s", unitErr == nil, tc.wantUnit, out.String()) + } + warn := "WARNING daemon: the cron entry could not be written, so the daemon service was left in place and keeps scheduling the backup." + if strings.Contains(out.String(), warn) != tc.wantWarn { + t.Fatalf("WARNING shown=%v, want %v; log:\n%s", !tc.wantWarn, tc.wantWarn, out.String()) + } + }) + } +} diff --git a/cmd/proxsave/install_runtime.go b/cmd/proxsave/install_runtime.go index 3b825a25..f4b4fced 100644 --- a/cmd/proxsave/install_runtime.go +++ b/cmd/proxsave/install_runtime.go @@ -96,6 +96,18 @@ func newInstallRuntimePreparation(configPath, baseDir string, execInfo ExecInfo, if err := ctx.Err(); err != nil { return err } + // The cron write reports its failures only as WARNINGs, so the crontab is read + // back: removing the daemon over a cron line that is not there left the host + // with no scheduler at all, under "Installation completed". + present, verified := canonicalCronLinePresent(ctx) + logging.Debug("install: cron entry after the write: present=%v verified=%v", present, verified) + if !present || !verified { + if daemonUnitInstalled() { + logging.Warning("daemon: the cron entry could not be written, so the daemon service was left in place and keeps scheduling the backup.") + return errors.New("the cron entry could not be written") + } + return errors.New("the cron entry could not be written, so no scheduler is installed") + } if daemonUnitInstalled() { if err := removeDaemonService(ctx, bootstrap); err != nil { logging.Warning("daemon: a previous daemon unit could not be removed (possible double execution): %v", err) From 6439cd8d93065aba914ab35473996587263d6c83 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 11:38:00 +0100 Subject: [PATCH 17/44] fix(storage): set the mode of local cloud copies on a filesystem without ownership With CLOUD_REMOTE as a local directory on a filesystem without ownership (NFS root_squash, CIFS), the backup set and the log rclone copied there kept rclone's 0644, while the Secondary's copies on the same mount ended 0600 (log 0640); the archive is unencrypted by default. docs/CLOUD_STORAGE.md promises the Secondary's owner and mode. The chmod was skipped together with the chown, though it needs no ownership. On such a filesystem the owner is still left alone, but each file of the backup set takes its source file's mode, as the Secondary's copy does, and the log takes the Secondary log's 0640. A failure stays at DEBUG. No visible line changes. Measured on pve-test with real rclone on an NFS root_squash share: 644 before, 600 after. Two tests that pinned the 0644 now expect the new modes. Audit finding storage-7 (audit-prod-go-2026-10-26). --- internal/orchestrator/extensions.go | 4 +- .../orchestrator/storage_attempts_e2e_test.go | 5 +- internal/storage/cloud.go | 52 ++++++++++++++----- internal/storage/cloud_local_test.go | 14 +++-- internal/storage/cloud_round6_test.go | 9 ++-- 5 files changed, 58 insertions(+), 26 deletions(-) diff --git a/internal/orchestrator/extensions.go b/internal/orchestrator/extensions.go index 9d7f25fe..bfeeb778 100644 --- a/internal/orchestrator/extensions.go +++ b/internal/orchestrator/extensions.go @@ -779,8 +779,8 @@ func (o *Orchestrator) copyLogToCloud(ctx context.Context, sourcePath, destPath } } // rclone writes the log 0644. In a local CLOUD_REMOTE directory it gets the mode the - // Secondary log copy creates (copyFile, 0640), owner unchanged, on a filesystem that - // takes ownership; the log is there either way. + // Secondary log copy creates (copyFile, 0640), owner unchanged; the log is there + // either way. if _, err := client.SetLocalLogMode(ctx, destPath, o.cloudFilesystemInfo()); err != nil { return &logPermissionsError{err: err, checksumNotVerified: checksumNotVerified} } diff --git a/internal/orchestrator/storage_attempts_e2e_test.go b/internal/orchestrator/storage_attempts_e2e_test.go index 631101f3..4f3a0456 100644 --- a/internal/orchestrator/storage_attempts_e2e_test.go +++ b/internal/orchestrator/storage_attempts_e2e_test.go @@ -604,7 +604,8 @@ func registerCloudInitFS(o *Orchestrator, info *storage.FilesystemInfo) { } // Step [8], local directory: the log mode follows the detection made at storage init. -// A filesystem without ownership, or no cloud target detected, leaves rclone's 0644. +// With or without ownership the log gets the Secondary log's 0640; no cloud target +// detected leaves rclone's 0644. func TestDispatchLogFileCloudLocalLogModeReusesInitDetection(t *testing.T) { script := "#!/bin/sh\n" + "case \"$1\" in\n" + @@ -617,7 +618,7 @@ func TestDispatchLogFileCloudLocalLogModeReusesInitDetection(t *testing.T) { mode os.FileMode }{ {"ownership", &storage.FilesystemInfo{Type: storage.FilesystemExt4, SupportsOwnership: true}, 0o640}, - {"no ownership", &storage.FilesystemInfo{Type: storage.FilesystemFAT32}, 0o644}, + {"no ownership", &storage.FilesystemInfo{Type: storage.FilesystemFAT32}, 0o640}, {"no cloud target", nil, 0o644}, } { t.Run(tc.name, func(t *testing.T) { diff --git a/internal/storage/cloud.go b/internal/storage/cloud.go index b5c51cd2..179b5c7d 100644 --- a/internal/storage/cloud.go +++ b/internal/storage/cloud.go @@ -228,17 +228,18 @@ func (c *CloudStorage) ensureLocalDir(ctx context.Context) error { } // SetLocalLogMode gives a log rclone copied into a local CLOUD_REMOTE directory the mode -// the Secondary log copy creates (0640, owner unchanged), on a filesystem that takes -// ownership, like the backups. fsInfo is what the storage initialization detected for +// the Secondary log copy creates (0640, owner unchanged), on a filesystem without +// ownership too: the chmod needs no ownership, and rclone's 0644 left the log readable +// by every user while the Secondary copy on the same mount was 0640. fsInfo is what the storage initialization detected for // the CLOUD_REMOTE directory: it is reused, nothing is detected (or probed) again here. // applied is false when there is nothing to set (the remote form, a remote destination, -// no detection, or a filesystem without ownership). +// or no detection). func (c *CloudStorage) SetLocalLogMode(ctx context.Context, logFile string, fsInfo *FilesystemInfo) (applied bool, err error) { if c == nil || c.localDir == "" || !filepath.IsAbs(logFile) { return false, nil } - if fsInfo == nil || !fsInfo.SupportsOwnership { - c.logger.Debug("Cloud log copy: mode of %s left as written (ownership not supported or filesystem unknown at storage init: %+v)", logFile, fsInfo) + if fsInfo == nil { + c.logger.Debug("Cloud log copy: mode of %s left as written (filesystem unknown at storage init)", logFile) return false, nil } c.logger.Debug("Cloud log copy: setting mode 0640 on %s (%s, detected at storage init)", logFile, fsInfo.Type) @@ -249,6 +250,28 @@ func (c *CloudStorage) SetLocalLogMode(ctx context.Context, logFile string, fsIn return true, nil } +// setLocalModesFromSource gives each file of the backup set copied to destFile the mode +// of the same file next to sourceFile. A failure stays at DEBUG, as on the Secondary. +func (c *CloudStorage) setLocalModesFromSource(ctx context.Context, sourceFile, destFile string) { + timeout := c.fsIoTimeout() + for _, dest := range buildBackupCandidatePaths(destFile, true) { + if _, err := safefs.Stat(ctx, dest, timeout); err != nil { + continue + } + src := filepath.Join(filepath.Dir(sourceFile), filepath.Base(dest)) + srcInfo, err := safefs.Stat(ctx, src, timeout) + if err != nil { + c.logger.Debug("Cloud Storage: permissions - no source mode for %s: %v", filepath.Base(dest), err) + continue + } + mode := srcInfo.Mode().Perm() + c.logger.Debug("Cloud Storage: permissions - owner left as is for %s (filesystem without ownership), mode %o from the source", filepath.Base(dest), mode) + if err := safefs.Chmod(ctx, dest, mode, timeout); err != nil { + c.logger.Debug("Cloud Storage: permissions - cannot set the mode of %s: %v", filepath.Base(dest), err) + } + } +} + // DetectionFailure is why the last detection of the local directory fell back to an // unknown filesystem, nil when it did not (and always nil for the remote form). func (c *CloudStorage) DetectionFailure() error { @@ -266,12 +289,15 @@ func (c *CloudStorage) NotCheckedInDryRun() bool { // CLOUD_REMOTE directory the owner and mode the Secondary gives its copies // (setBackupSetPermissions: 0600 root:root, or 0640 BACKUP_USER:BACKUP_GROUP with // SET_BACKUP_PERMISSIONS). rclone writes them 0644. Nothing happens for the remote -// form, or on a filesystem without ownership. -func (c *CloudStorage) setLocalPermissions(ctx context.Context, destFile string, issues *storeIssueRecorder) { - if c.localDir == "" || c.localFS == nil || !c.localFS.SupportsOwnership { - if c.localDir != "" { - c.logger.Debug("Cloud Storage: permissions - skipped for %s (filesystem without ownership)", filepath.Base(destFile)) - } +// form. On a filesystem without ownership the owner is left alone but each file still +// takes its source file's mode, as the Secondary's copy does there: the 0644 left an +// archive, unencrypted by default, readable by every user of the share. +func (c *CloudStorage) setLocalPermissions(ctx context.Context, sourceFile, destFile string, issues *storeIssueRecorder) { + if c.localDir == "" || c.localFS == nil { + return + } + if !c.localFS.SupportsOwnership { + c.setLocalModesFromSource(ctx, sourceFile, destFile) return } detector := c.fsDetector @@ -1229,7 +1255,7 @@ func (c *CloudStorage) Store(ctx context.Context, backupFile string, metadata *t // The archive is in the local directory: like the Secondary, the sidecar // failure comes first, then owner and mode of what was copied. issues.add(StoreIssueSidecarNotSaved) - c.setLocalPermissions(ctx, remoteFile, issues) + c.setLocalPermissions(ctx, backupFile, remoteFile, issues) } if primaryFailed && !errors.As(err, &verifyErr) { c.removeFailedUpload(ctx, filename, remoteFile) @@ -1247,7 +1273,7 @@ func (c *CloudStorage) Store(ctx context.Context, backupFile string, metadata *t c.logger.Debug("Cloud storage: upload and verification completed for %s", filename) c.logger.Debug("✓ Cloud Storage: File uploaded") - c.setLocalPermissions(ctx, remoteFile, issues) + c.setLocalPermissions(ctx, backupFile, remoteFile, issues) if count := c.countBackups(ctx); count >= 0 { c.logger.Debug("Cloud storage: current backups detected after upload: %d", count) diff --git a/internal/storage/cloud_local_test.go b/internal/storage/cloud_local_test.go index e1c1711b..a0fcdff3 100644 --- a/internal/storage/cloud_local_test.go +++ b/internal/storage/cloud_local_test.go @@ -651,15 +651,19 @@ func TestCloudLocalFormSidecarFailureThenPermissions(t *testing.T) { } } -// On a filesystem without ownership the Secondary sets nothing, and neither does the -// local form; the remote form never does. -func TestCloudLocalFormNoOwnershipSetsNothing(t *testing.T) { +// On a filesystem without ownership the Secondary's copy keeps its source's mode, and +// so does the local form now: rclone's 0644 left an archive, unencrypted by default, +// readable by every user of the share. The owner is left alone and nothing is printed; +// the remote form never sets anything. +func TestCloudLocalFormNoOwnershipSetsTheSourceMode(t *testing.T) { cs, buf, archive, root := newLocalFormStore(t, "", false) if err := cs.Store(context.Background(), archive, nil); err != nil { t.Fatalf("Store: %v", err) } - if st, err := os.Stat(filepath.Join(root, filepath.Base(archive))); err != nil || st.Mode().Perm() != 0o644 { - t.Fatalf("mode = %v (%v), want rclone's 0644 left alone", st.Mode().Perm(), err) + for _, name := range []string{filepath.Base(archive), filepath.Base(archive) + ".sha256", filepath.Base(archive) + ".metadata"} { + if st, err := os.Stat(filepath.Join(root, name)); err != nil || st.Mode().Perm() != 0o600 { + t.Fatalf("%s: mode = %v (%v), want the source's 0600", name, st.Mode().Perm(), err) + } } if got := visibleOf(buf.String()); len(got) != 0 || len(cs.LastStoreIssues()) != 0 { t.Fatalf("no ownership: nothing to print or record, got %v %v", got, cs.LastStoreIssues()) diff --git a/internal/storage/cloud_round6_test.go b/internal/storage/cloud_round6_test.go index 00033fbb..f3445ba5 100644 --- a/internal/storage/cloud_round6_test.go +++ b/internal/storage/cloud_round6_test.go @@ -74,9 +74,10 @@ func TestCloudLocalStoreRecreatesTheDirectory(t *testing.T) { } } -// The log copied into a local directory gets 0640 only on a filesystem that takes -// ownership (as for the backups), judged on the detection made at storage init: no -// second detection, no ownership probe in the log directory. +// The log copied into a local directory gets 0640, the Secondary log's mode, on a +// filesystem with or without ownership (rclone's 0644 was left on one without), judged +// on the detection made at storage init: no second detection, no ownership probe in +// the log directory. func TestCloudSetLocalLogModeFollowsOwnership(t *testing.T) { dir := filepath.Join(t.TempDir(), "cloud") logDir := filepath.Join(dir, "proxsave", "log") @@ -91,7 +92,7 @@ func TestCloudSetLocalLogModeFollowsOwnership(t *testing.T) { mode os.FileMode }{ {"ownership", &FilesystemInfo{Type: FilesystemExt4, SupportsOwnership: true}, true, 0o640}, - {"no ownership", &FilesystemInfo{Type: FilesystemFAT32}, false, 0o644}, + {"no ownership", &FilesystemInfo{Type: FilesystemFAT32}, true, 0o640}, {"no detection", nil, false, 0o644}, } { if err := os.WriteFile(logFile, []byte("log"), 0o644); err != nil { From 5cca77f4ead768ba0267ec22b57e7e4591495c39 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 11:44:38 +0100 Subject: [PATCH 18/44] fix(storage): fail closed and refuse symlinks when setting local cloud copy modes Automated security review of 6439cd8d: when the source mode could not be read the copy was left at rclone's 0644, and the chmod followed a symlink, in a directory that can be a share other hosts write to, so a link swapped in for a copy could redirect a root chmod to another file. The copy now gets 0600 when its source mode is unknown, and both the backup-set chmod and the log's 0640 go through safefs.Lchmod, which refuses a symlink at the final component. Failures stay at DEBUG, as on the Secondary. No visible line changes. Audit finding storage-7 (audit-prod-go-2026-10-26). --- internal/storage/cloud.go | 20 +++++++----- internal/storage/cloud_local_modes_test.go | 38 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 internal/storage/cloud_local_modes_test.go diff --git a/internal/storage/cloud.go b/internal/storage/cloud.go index 179b5c7d..3b6f8b99 100644 --- a/internal/storage/cloud.go +++ b/internal/storage/cloud.go @@ -243,7 +243,9 @@ func (c *CloudStorage) SetLocalLogMode(ctx context.Context, logFile string, fsIn return false, nil } c.logger.Debug("Cloud log copy: setting mode 0640 on %s (%s, detected at storage init)", logFile, fsInfo.Type) - if err := safefs.Chmod(ctx, logFile, 0o640, c.fsIoTimeout()); err != nil { + // Lchmod: the directory can be a share other hosts write to, and a symlink swapped + // in for the log must not redirect a root chmod to another file. + if err := safefs.Lchmod(ctx, logFile, 0o640, c.fsIoTimeout()); err != nil { c.logger.Debug("Cloud log copy: cannot set the mode of %s: %v", logFile, err) return false, err } @@ -251,7 +253,9 @@ func (c *CloudStorage) SetLocalLogMode(ctx context.Context, logFile string, fsIn } // setLocalModesFromSource gives each file of the backup set copied to destFile the mode -// of the same file next to sourceFile. A failure stays at DEBUG, as on the Secondary. +// of the same file next to sourceFile, 0600 when that mode cannot be read, so a copy is +// never left at rclone's 0644. The chmod refuses a symlink: the directory can be a share +// other hosts write to. A failure stays at DEBUG, as on the Secondary. func (c *CloudStorage) setLocalModesFromSource(ctx context.Context, sourceFile, destFile string) { timeout := c.fsIoTimeout() for _, dest := range buildBackupCandidatePaths(destFile, true) { @@ -259,14 +263,14 @@ func (c *CloudStorage) setLocalModesFromSource(ctx context.Context, sourceFile, continue } src := filepath.Join(filepath.Dir(sourceFile), filepath.Base(dest)) - srcInfo, err := safefs.Stat(ctx, src, timeout) - if err != nil { - c.logger.Debug("Cloud Storage: permissions - no source mode for %s: %v", filepath.Base(dest), err) - continue + mode := os.FileMode(0o600) + if srcInfo, err := safefs.Stat(ctx, src, timeout); err == nil { + mode = srcInfo.Mode().Perm() + } else { + c.logger.Debug("Cloud Storage: permissions - no source mode for %s, using %o: %v", filepath.Base(dest), mode, err) } - mode := srcInfo.Mode().Perm() c.logger.Debug("Cloud Storage: permissions - owner left as is for %s (filesystem without ownership), mode %o from the source", filepath.Base(dest), mode) - if err := safefs.Chmod(ctx, dest, mode, timeout); err != nil { + if err := safefs.Lchmod(ctx, dest, mode, timeout); err != nil { c.logger.Debug("Cloud Storage: permissions - cannot set the mode of %s: %v", filepath.Base(dest), err) } } diff --git a/internal/storage/cloud_local_modes_test.go b/internal/storage/cloud_local_modes_test.go new file mode 100644 index 00000000..fd1a99de --- /dev/null +++ b/internal/storage/cloud_local_modes_test.go @@ -0,0 +1,38 @@ +package storage + +import ( + "context" + "os" + "path/filepath" + "testing" +) + +// Without ownership the local cloud copies take their source's mode; a copy whose +// source mode cannot be read gets 0600, never rclone's 0644, and a symlink in the +// destination (a share other hosts write to) is not followed by the root chmod. +func TestCloudLocalModesFailClosedAndRefuseSymlinks(t *testing.T) { + cs, _, archive, root := newLocalFormStore(t, "", false) + dest := filepath.Join(root, filepath.Base(archive)) + if err := os.WriteFile(dest, []byte("copy"), 0o644); err != nil { + t.Fatal(err) + } + outside := filepath.Join(t.TempDir(), "outside") + if err := os.WriteFile(outside, []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.Symlink(outside, dest+".sha256"); err != nil { + t.Fatal(err) + } + if err := os.Remove(archive); err != nil { // source mode unreadable + t.Fatal(err) + } + + cs.setLocalModesFromSource(context.Background(), archive, dest) + + if st, err := os.Stat(dest); err != nil || st.Mode().Perm() != 0o600 { + t.Fatalf("archive copy mode = %v (%v), want 0600 when the source mode is unknown", st.Mode().Perm(), err) + } + if st, err := os.Stat(outside); err != nil || st.Mode().Perm() != 0o644 { + t.Fatalf("symlink target mode = %v (%v), want 0644 untouched", st.Mode().Perm(), err) + } +} From c8025d5595c9388c24ca4c2672ce41f9831d8538 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 16:39:47 +0100 Subject: [PATCH 19/44] fix(permissions): leave a disabled secondary alone in SET_BACKUP_PERMISSIONS With SET_BACKUP_PERMISSIONS=true every run, --install and --upgrade walked SECONDARY_PATH and SECONDARY_LOG_PATH recursively even with SECONDARY_ENABLED=false: the loader keeps both paths, and the pass chowned to BACKUP_USER and re-moded every entry of a directory the backup no longer uses (measured on pve-test: 11 of 11 entries, dirs 0755 to 0750). Every other consumer checks the flag; the Bash version gated this step on it too. config.ActiveSecondaryPaths returns the two paths only when the secondary is enabled, and the pass uses it. The test that built a secondary without SecondaryEnabled now sets it. Approved docs texts: SECURITY.md "every run walks `BACKUP_PATH` and `LOG_PATH`, plus `SECONDARY_PATH` and `SECONDARY_LOG_PATH` when `SECONDARY_ENABLED=true`", CONFIGURATION.md "(when `SECONDARY_ENABLED=true`)". Audit finding cmd-main-11 (audit-prod-go-2026-10-26). --- cmd/proxsave/permissions.go | 7 +++- cmd/proxsave/permissions_backup_files_test.go | 2 +- .../permissions_disabled_secondary_test.go | 40 +++++++++++++++++++ docs/CONFIGURATION.md | 4 +- docs/SECURITY.md | 4 +- internal/config/config.go | 11 +++++ 6 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 cmd/proxsave/permissions_disabled_secondary_test.go diff --git a/cmd/proxsave/permissions.go b/cmd/proxsave/permissions.go index 36aaafd2..013cc6f5 100644 --- a/cmd/proxsave/permissions.go +++ b/cmd/proxsave/permissions.go @@ -57,11 +57,14 @@ func applyBackupPermissions(ctx context.Context, cfg *config.Config, logger *log timeout := fsIoTimeoutDuration(cfg) + // A disabled secondary keeps its configured paths, and walking them chowned a + // directory the backup no longer uses (the Bash version gated this on the flag). + secondaryPath, secondaryLogPath := cfg.ActiveSecondaryPaths() dirs := []string{ strings.TrimSpace(cfg.BackupPath), strings.TrimSpace(cfg.LogPath), - strings.TrimSpace(cfg.SecondaryPath), - strings.TrimSpace(cfg.SecondaryLogPath), + strings.TrimSpace(secondaryPath), + strings.TrimSpace(secondaryLogPath), } // Bounded + dry-run-aware detector (parity with the security preflight): a diff --git a/cmd/proxsave/permissions_backup_files_test.go b/cmd/proxsave/permissions_backup_files_test.go index 737c367a..0b348230 100644 --- a/cmd/proxsave/permissions_backup_files_test.go +++ b/cmd/proxsave/permissions_backup_files_test.go @@ -81,7 +81,7 @@ func TestApplyBackupPermissionsOpensPreviousBackupFilesToGroup(t *testing.T) { } cfg := &config.Config{BackupUser: usr, BackupGroup: grp, BackupPath: backupDir, - SecondaryPath: secondaryDir, LogPath: logDir, FsIoTimeoutSeconds: 30} + SecondaryEnabled: true, SecondaryPath: secondaryDir, LogPath: logDir, FsIoTimeoutSeconds: 30} logger, _ := bufLogger() if err := applyBackupPermissions(context.Background(), cfg, logger, false); err != nil { t.Fatalf("applyBackupPermissions: %v", err) diff --git a/cmd/proxsave/permissions_disabled_secondary_test.go b/cmd/proxsave/permissions_disabled_secondary_test.go new file mode 100644 index 00000000..52989666 --- /dev/null +++ b/cmd/proxsave/permissions_disabled_secondary_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/tis24dev/proxsave/internal/config" +) + +// SET_BACKUP_PERMISSIONS walked SECONDARY_PATH and SECONDARY_LOG_PATH with the secondary +// disabled: the loader keeps both paths, and the pass chowned and re-moded a directory +// the backup no longer uses. A disabled secondary is now left alone. +func TestBackupPermissionsLeaveADisabledSecondaryAlone(t *testing.T) { + usr, grp := currentUserGroup(t) + root := t.TempDir() + backupDir := filepath.Join(root, "backup") + secondaryDir := filepath.Join(root, "secondary") + for _, d := range []string{backupDir, secondaryDir} { + if err := os.MkdirAll(d, 0o755); err != nil { + t.Fatal(err) + } + } + if err := os.Chmod(secondaryDir, 0o755); err != nil { + t.Fatal(err) + } + cfg := &config.Config{BackupUser: usr, BackupGroup: grp, BackupPath: backupDir, + SecondaryEnabled: false, SecondaryPath: secondaryDir, SecondaryLogPath: secondaryDir, FsIoTimeoutSeconds: 30} + logger, _ := bufLogger() + if err := applyBackupPermissions(context.Background(), cfg, logger, false); err != nil { + t.Fatalf("applyBackupPermissions: %v", err) + } + if st, err := os.Stat(secondaryDir); err != nil || st.Mode().Perm() != 0o755 { + t.Fatalf("disabled secondary mode = %v (%v), want 0755 untouched", st.Mode().Perm(), err) + } + if st, err := os.Stat(backupDir); err != nil || st.Mode().Perm() != 0o750 { + t.Fatalf("backup dir mode = %v (%v), want 0750 (the pass still runs there)", st.Mode().Perm(), err) + } +} diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 0112914e..baa901c5 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -565,8 +565,8 @@ When `SET_BACKUP_PERMISSIONS=true`, the system applies Bash-compatible ownership - Recursively changes owner:group for: - `BACKUP_PATH` (primary backup directory) - `LOG_PATH` (primary log directory) - - `SECONDARY_PATH` (if configured) - - `SECONDARY_LOG_PATH` (if configured) + - `SECONDARY_PATH` (when `SECONDARY_ENABLED=true`) + - `SECONDARY_LOG_PATH` (when `SECONDARY_ENABLED=true`) - Uses `BACKUP_USER:BACKUP_GROUP` as the target owner - Does NOT touch binary files, config files, or system paths diff --git a/docs/SECURITY.md b/docs/SECURITY.md index 20135276..67375d38 100644 --- a/docs/SECURITY.md +++ b/docs/SECURITY.md @@ -300,8 +300,8 @@ a key added by a newer release appears with its default instead of staying absen | `SET_BACKUP_PERMISSIONS` | `false` | when true, the run chowns the backup/log dirs to `BACKUP_USER:BACKUP_GROUP`, opens the backup files to that group (`0640`), and the preflight then skips the dirs' owner **and mode** checks | `SET_BACKUP_PERMISSIONS` is not a passive opt-out. When it is true, every run walks -`BACKUP_PATH`, `LOG_PATH`, `SECONDARY_PATH` and `SECONDARY_LOG_PATH` **recursively, before -the preflight**, sets every entry to `BACKUP_USER:BACKUP_GROUP`, sets directories to +`BACKUP_PATH` and `LOG_PATH`, plus `SECONDARY_PATH` and `SECONDARY_LOG_PATH` when +`SECONDARY_ENABLED=true`, **recursively, before the preflight**, sets every entry to `BACKUP_USER:BACKUP_GROUP`, sets directories to `0750` and the files of a backup set (the archive or bundle and its `.sha256`, `.metadata` and `.manifest.json`) to `0640`; every other file, logs included, keeps its mode. The storage step of the same run then gives the backup it has just written on `BACKUP_PATH` diff --git a/internal/config/config.go b/internal/config/config.go index c69fefa5..fe38f5b0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1531,6 +1531,17 @@ func normalizeList(values []string) []string { } // BuildWebhookConfig constructs a complete webhook configuration with all endpoints +// ActiveSecondaryPaths returns SECONDARY_PATH and SECONDARY_LOG_PATH when the secondary +// is enabled, and two empty strings when it is not. The loader keeps both paths with +// SECONDARY_ENABLED=false, so a pass that walks, chowns or creates them must ask here: +// a disabled secondary is not the operator's to have ProxSave touch. +func (c *Config) ActiveSecondaryPaths() (dataPath, logPath string) { + if c == nil || !c.SecondaryEnabled { + return "", "" + } + return c.SecondaryPath, c.SecondaryLogPath +} + func (c *Config) BuildWebhookConfig() *WebhookConfig { endpoints := []WebhookEndpoint{} From 9d5abcb07ad19590a21f842c417c9ce518367072 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 16:49:07 +0100 Subject: [PATCH 20/44] fix(security): neither create nor check the paths of a disabled secondary The security preflight of every run, --install and --upgrade listed SECONDARY_PATH and SECONDARY_LOG_PATH whatever SECONDARY_ENABLED said. The loader keeps both paths when the secondary is off, so the preflight created a missing one at 0755 root:root (on the root filesystem when its mount was absent, hidden later under the mount), and with AUTO_FIX_PERMISSIONS re-owned an existing one to root:root 0755 (measured on pve-test). The Bash version created them only when the secondary was enabled. Both sites now use config.ActiveSecondaryPaths. Two tests that built a secondary without SecondaryEnabled set it. Approved docs text, SECURITY.md: "`SECONDARY_PATH` and `SECONDARY_LOG_PATH` are checked, and created when missing, only when `SECONDARY_ENABLED=true`." Audit finding platform-3 (audit-prod-go-2026-10-26). --- docs/SECURITY.md | 4 +- internal/security/disabled_secondary_test.go | 39 ++++++++++++++++++++ internal/security/security.go | 13 +++++-- internal/security/security_test.go | 2 + 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 internal/security/disabled_secondary_test.go diff --git a/docs/SECURITY.md b/docs/SECURITY.md index 67375d38..233066b4 100644 --- a/docs/SECURITY.md +++ b/docs/SECURITY.md @@ -217,7 +217,9 @@ ever widening. `identity/.server_identity`, the AGE recipient file, every `secure_account/*.json`, and the PBS token files when they exist: `secure_account/pbs_token`, `secure_account/pbs_api_token` and `/root/.pbs-token`), -directories at `0700`/`0755`, all owned `root:root`. The exception is `BACKUP_PATH`, +directories at `0700`/`0755`, all owned `root:root`. `SECONDARY_PATH` and +`SECONDARY_LOG_PATH` are checked, and created when missing, only when +`SECONDARY_ENABLED=true`. The exception is `BACKUP_PATH`, `LOG_PATH`, `SECONDARY_PATH` and `SECONDARY_LOG_PATH`: their owner **and** mode checks are skipped entirely when `SET_BACKUP_PERMISSIONS=true` (see below) or when the path sits on a filesystem without Unix ownership. With `AUTO_FIX_PERMISSIONS` diff --git a/internal/security/disabled_secondary_test.go b/internal/security/disabled_secondary_test.go new file mode 100644 index 00000000..7d65d8ab --- /dev/null +++ b/internal/security/disabled_secondary_test.go @@ -0,0 +1,39 @@ +package security + +import ( + "context" + "os" + "path/filepath" + "testing" + "time" + + "github.com/tis24dev/proxsave/internal/config" +) + +// The preflight created and re-owned SECONDARY_PATH and SECONDARY_LOG_PATH with the +// secondary disabled: the loader keeps both paths, so a missing mount point became a +// directory on the root filesystem. A disabled secondary is now neither created nor +// checked. +func TestVerifyDirectoriesLeavesADisabledSecondaryAlone(t *testing.T) { + root := t.TempDir() + secondary := filepath.Join(root, "usb", "proxsave") + secondaryLog := filepath.Join(root, "usb", "proxsave-log") + cfg := &config.Config{ + BackupPath: filepath.Join(root, "backup"), + LogPath: filepath.Join(root, "log"), + SecondaryEnabled: false, + SecondaryPath: secondary, + SecondaryLogPath: secondaryLog, + } + c := &Checker{logger: newSecurityTestLogger(), cfg: cfg, result: &Result{}, fsTimeout: 30 * time.Second} + c.verifyDirectories(context.Background()) + + for _, p := range []string{secondary, secondaryLog, filepath.Dir(secondary)} { + if _, err := os.Stat(p); !os.IsNotExist(err) { + t.Fatalf("%s exists after the preflight (err=%v): a disabled secondary was created", p, err) + } + } + if _, err := os.Stat(cfg.BackupPath); err != nil { + t.Fatalf("BACKUP_PATH not created: %v", err) + } +} diff --git a/internal/security/security.go b/internal/security/security.go index d29f8f97..21bdeeeb 100644 --- a/internal/security/security.go +++ b/internal/security/security.go @@ -631,6 +631,10 @@ func (c *Checker) verifySecureAccountFiles(ctx context.Context) { } func (c *Checker) verifyDirectories(ctx context.Context) { + // A disabled secondary keeps its configured paths: checking them created the + // directories (on the root filesystem when the mount was absent) and chowned them + // root:root 0755 for a destination the backup does not use. + secondaryPath, secondaryLogPath := c.cfg.ActiveSecondaryPaths() dirs := []struct { path string perm os.FileMode @@ -638,8 +642,8 @@ func (c *Checker) verifyDirectories(ctx context.Context) { }{ {c.cfg.BackupPath, 0o755, true}, {c.cfg.LogPath, 0o755, true}, - {c.cfg.SecondaryPath, 0o755, true}, - {c.cfg.SecondaryLogPath, 0o755, true}, + {secondaryPath, 0o755, true}, + {secondaryLogPath, 0o755, true}, {c.cfg.LockPath, 0o755, false}, {c.cfg.SecureAccount, 0o700, false}, {filepath.Join(c.cfg.BaseDir, "identity"), 0o700, false}, @@ -1009,11 +1013,12 @@ func (c *Checker) shouldSkipOwnershipChecks(path string) bool { } target := filepath.Clean(path) + secondaryPath, secondaryLogPath := c.cfg.ActiveSecondaryPaths() paths := []string{ c.cfg.BackupPath, c.cfg.LogPath, - c.cfg.SecondaryPath, - c.cfg.SecondaryLogPath, + secondaryPath, + secondaryLogPath, } for _, candidate := range paths { diff --git a/internal/security/security_test.go b/internal/security/security_test.go index bb89136b..f9aec3aa 100644 --- a/internal/security/security_test.go +++ b/internal/security/security_test.go @@ -1187,6 +1187,7 @@ func TestShouldSkipOwnershipChecks(t *testing.T) { SetBackupPermissions: tc.setBackupPerms, BackupPath: tc.backupPath, LogPath: tc.logPath, + SecondaryEnabled: true, SecondaryPath: tc.secondaryPath, SecondaryLogPath: tc.secondaryLogPath, }, @@ -2070,6 +2071,7 @@ func TestVerifyDirectoriesWithAllPaths(t *testing.T) { BaseDir: tmpDir, BackupPath: filepath.Join(tmpDir, "backup"), LogPath: filepath.Join(tmpDir, "log"), + SecondaryEnabled: true, SecondaryPath: filepath.Join(tmpDir, "secondary"), SecondaryLogPath: filepath.Join(tmpDir, "secondary_log"), LockPath: filepath.Join(tmpDir, "lock"), From 0afdc5ea111bbf91ea8e69cd163254bde298ba95 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 16:56:27 +0100 Subject: [PATCH 21/44] fix(install): the permissions footer names the backup paths it skipped applyBackupPermissions returns nil on every skip, so the install and upgrade footers printed "permissions and ownership normalized correctly (including backup paths)" when BACKUP_USER/BACKUP_GROUP were empty or could not be resolved, or a path could not be read, detected or changed. The TUI install hides the pass's WARNINGs, so nothing on its screen told the operator. Its "warning" branch was dead. The pass now reports what it left undone; the backup run keeps its contract (no error). The footer reads it. Approved texts: Permissions: WARNING (non blocking) - permissions and ownership normalized; backup paths skipped: with the reasons "BACKUP_USER/BACKUP_GROUP are empty", "BACKUP_USER/ BACKUP_GROUP cannot be resolved", " could not be read", "the filesystem of could not be detected", " could not be changed" (joined by "; "). A documented skip (filesystem without ownership, missing directory) drops the "(including backup paths)" suffix: "permissions and ownership normalized correctly". The dead branch and its never-printed texts are gone. Audit finding cmd-main-6 (audit-prod-go-2026-10-26). --- cmd/proxsave/permissions.go | 46 ++++++++++++---- cmd/proxsave/permissions_footer_skip_test.go | 56 ++++++++++++++++++++ 2 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 cmd/proxsave/permissions_footer_skip_test.go diff --git a/cmd/proxsave/permissions.go b/cmd/proxsave/permissions.go index 013cc6f5..5403233a 100644 --- a/cmd/proxsave/permissions.go +++ b/cmd/proxsave/permissions.go @@ -39,18 +39,35 @@ func fsIoTimeoutDuration(cfg *config.Config) time.Duration { // - It only touches backup/log paths (not binaries/config files). // - Failures are logged as warnings but do not abort the backup. func applyBackupPermissions(ctx context.Context, cfg *config.Config, logger *logging.Logger, dryRun bool) error { + applyBackupPermissionsOutcome(ctx, cfg, logger, dryRun) + return nil +} + +// backupPermissionsOutcome is what the pass left undone. skipped names each backup path +// it could not set, one reason per WARNING it logged; documentedSkip is true when a path +// was left alone for a documented reason (no Unix ownership, missing directory). The +// install and upgrade footers read it: the pass returns no error, and the footer said +// "normalized correctly (including backup paths)" over every skip. +type backupPermissionsOutcome struct { + skipped []string + documentedSkip bool +} + +func applyBackupPermissionsOutcome(ctx context.Context, cfg *config.Config, logger *logging.Logger, dryRun bool) (outcome backupPermissionsOutcome) { backupUser := strings.TrimSpace(cfg.BackupUser) backupGroup := strings.TrimSpace(cfg.BackupGroup) if backupUser == "" || backupGroup == "" { logger.Warning("SET_BACKUP_PERMISSIONS=true but BACKUP_USER/BACKUP_GROUP are empty; skipping permission adjustments") - return nil + outcome.skipped = append(outcome.skipped, "BACKUP_USER/BACKUP_GROUP are empty") + return outcome } uid, gid, err := resolveUserGroupIDs(backupUser, backupGroup) if err != nil { // Log and skip rather than aborting logger.Warning("Failed to resolve BACKUP_USER/BACKUP_GROUP (%s:%s): %v", backupUser, backupGroup, err) - return nil + outcome.skipped = append(outcome.skipped, "BACKUP_USER/BACKUP_GROUP cannot be resolved") + return outcome } logger.Info("Applying backup permissions (SET_BACKUP_PERMISSIONS=true) for user:group %s:%s", backupUser, backupGroup) @@ -82,6 +99,7 @@ func applyBackupPermissions(ctx context.Context, cfg *config.Config, logger *log } if strings.Contains(dir, ":") && !isLocalPath(dir) { logger.Info("Permissions: skipping remote cloud path (ownership unsupported): %s", dir) + outcome.documentedSkip = true continue } @@ -90,15 +108,19 @@ func applyBackupPermissions(ctx context.Context, cfg *config.Config, logger *log switch { case errors.Is(err, safefs.ErrTimeout): logger.Warning("Permissions: stat of %s timed out after %s; skipping (dead/stale mount?)", dir, timeout) + outcome.skipped = append(outcome.skipped, dir+" could not be read") case os.IsNotExist(err): logger.Skip("Permissions: directory does not exist: %s", dir) + outcome.documentedSkip = true default: logger.Warning("Permissions: failed to stat %s (skipping): %v", dir, err) + outcome.skipped = append(outcome.skipped, dir+" could not be read") } continue } if !info.IsDir() { logger.Skip("Permissions: path is not a directory, skipping: %s", dir) + outcome.documentedSkip = true continue } @@ -109,19 +131,22 @@ func applyBackupPermissions(ctx context.Context, cfg *config.Config, logger *log } else { logger.Warning("Permissions: failed to detect filesystem for %s; skipping chown/chmod: %v", dir, err) } + outcome.skipped = append(outcome.skipped, "the filesystem of "+dir+" could not be detected") continue } if fsInfo.Type.ShouldAutoExclude() || !fsInfo.SupportsOwnership { logger.Info("Permissions: skipping chown/chmod for %s (filesystem %s does not support Unix ownership)", dir, fsInfo.Type) + outcome.documentedSkip = true continue } logger.Debug("Applying permissions on path: %s (uid=%d,gid=%d)", dir, uid, gid) if err := applyDirOwnershipRecursive(ctx, dir, uid, gid, timeout, dryRun, logger); err != nil { logger.Warning("Failed to apply permissions on %s: %v", dir, err) + outcome.skipped = append(outcome.skipped, dir+" could not be changed") } } - return nil + return outcome } func resolveUserGroupIDs(userName, groupName string) (int, int, error) { @@ -289,16 +314,17 @@ func fixPermissionsAfterInstall(ctx context.Context, configPath, baseDir string, bootstrap.Info("Finalizing installation: applying backup directory permissions") } // Install/upgrade finalization always mutates (dryRun=false); it exists to - // leave the environment consistent and has no dry-run mode of its own. - if err := applyBackupPermissions(ctx, cfg, logger, false); err != nil { - if bootstrap != nil { - bootstrap.Warning("Post-install: backup permission adjustment failed (ignored): %v", err) - } + // leave the environment consistent and has no dry-run mode of its own. The + // reasons go in the line itself: the TUI does not show the pass's WARNINGs. + outcome := applyBackupPermissionsOutcome(ctx, cfg, logger, false) + logger.Debug("Post-install: backup permissions skipped=%q documentedSkip=%v", outcome.skipped, outcome.documentedSkip) + switch { + case len(outcome.skipped) > 0: if status != "error" { status = "warning" - message = "permissions normalized with warnings for backup paths (non-blocking, see log)" + message = "permissions and ownership normalized; backup paths skipped: " + strings.Join(outcome.skipped, "; ") } - } else if status == "ok" { + case status == "ok" && !outcome.documentedSkip: message = "permissions and ownership normalized correctly (including backup paths)" } } diff --git a/cmd/proxsave/permissions_footer_skip_test.go b/cmd/proxsave/permissions_footer_skip_test.go new file mode 100644 index 00000000..cea4cb93 --- /dev/null +++ b/cmd/proxsave/permissions_footer_skip_test.go @@ -0,0 +1,56 @@ +package main + +import ( + "context" + "io" + "os" + "path/filepath" + "testing" + + "github.com/tis24dev/proxsave/internal/config" + "github.com/tis24dev/proxsave/internal/logging" +) + +// The install and upgrade footers said "permissions and ownership normalized correctly +// (including backup paths)" whatever the backup-path pass did: it returns no error on +// any skip. The footer now names what was skipped, in the line, since the TUI does not +// show the pass's WARNINGs. +func TestPermissionsFooterNamesTheSkippedBackupPaths(t *testing.T) { + usr, grp := currentUserGroup(t) + for _, tc := range []struct { + name, user, wantStatus, wantMessage string + }{ + {"unknown user", "proxsave-no-such-user-cm6", "warning", "permissions and ownership normalized; backup paths skipped: BACKUP_USER/BACKUP_GROUP cannot be resolved"}, + {"empty user", "", "warning", "permissions and ownership normalized; backup paths skipped: BACKUP_USER/BACKUP_GROUP are empty"}, + {"resolvable user", usr, "ok", "permissions and ownership normalized correctly (including backup paths)"}, + } { + t.Run(tc.name, func(t *testing.T) { + base := t.TempDir() + for _, d := range []string{"backup", "log", "configs"} { + if err := os.MkdirAll(filepath.Join(base, d), 0o755); err != nil { + t.Fatal(err) + } + } + cfgPath := filepath.Join(base, "configs", "backup.env") + env := "BASE_DIR=" + base + "\nBACKUP_PATH=" + filepath.Join(base, "backup") + "\nLOG_PATH=" + filepath.Join(base, "log") + + "\nSET_BACKUP_PERMISSIONS=true\nBACKUP_USER=" + tc.user + "\nBACKUP_GROUP=" + grp + "\nSECONDARY_ENABLED=false\n" + if err := os.WriteFile(cfgPath, []byte(env), 0o600); err != nil { + t.Fatal(err) + } + status, message := fixPermissionsAfterInstall(context.Background(), cfgPath, base, logging.NewBootstrapLogger(), io.Discard) + if status != tc.wantStatus || message != tc.wantMessage { + t.Fatalf("status=%q message=%q, want %q %q", status, message, tc.wantStatus, tc.wantMessage) + } + }) + } +} + +func TestBackupPermissionsOutcomeMarksADocumentedSkip(t *testing.T) { + usr, grp := currentUserGroup(t) + cfg := &config.Config{BackupUser: usr, BackupGroup: grp, BackupPath: filepath.Join(t.TempDir(), "missing"), FsIoTimeoutSeconds: 30} + logger, _ := bufLogger() + outcome := applyBackupPermissionsOutcome(context.Background(), cfg, logger, false) + if !outcome.documentedSkip || len(outcome.skipped) != 0 { + t.Fatalf("missing BACKUP_PATH: outcome %+v, want a documented skip and nothing skipped", outcome) + } +} From 6127f5c060e6b1af8caf6f523d8d9497138ac3cb Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 17:22:33 +0100 Subject: [PATCH 22/44] fix(support): a failed support email gets its own WARNING MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the support email could not be sent the operator saw only the adapter's generic "❌ Email: notification failed", nothing naming the support email. Its CRITICAL line and yellow banner were dead code: the notification adapter returns nil for every delivery failure by design, and the outcome is in NotifyResults. The dead branch is gone; the failure branch logs, after the adapter's line, the approved text: WARNING Support mode: FAILED to send support email to The exit code does not change (the support email runs after it is set). Audit finding notify-health-2 (audit-prod-go-2026-10-26). --- internal/support/support.go | 16 +++++------ .../support_email_failure_line_test.go | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 internal/support/support_email_failure_line_test.go diff --git a/internal/support/support.go b/internal/support/support.go index 18e223c2..7c8e4dc2 100644 --- a/internal/support/support.go +++ b/internal/support/support.go @@ -268,16 +268,14 @@ func SendEmail(ctx context.Context, cfg *config.Config, logger *logging.Logger, } adapter := orchestrator.NewNotificationAdapter(emailNotifier, logger) - if err := adapter.Notify(ctx, stats); err != nil { - logging.Critical("Support mode: FAILED to send support email: %v", err) - fmt.Println("\033[33m⚠️ CRITICAL: Support email failed to send!\033[0m") - return - } - - // Notify keeps delivery failures non-fatal and records the actual outcome in - // NotifyResults. A nil error alone does not confirm an MTA handoff. + // Notify keeps delivery failures non-fatal (it returns nil for them) and records the + // actual outcome in NotifyResults. The adapter's own WARNING names only the Email + // channel, so the failure of the support email gets its own line after it. + notifyErr := adapter.Notify(ctx, stats) outcome := stats.NotifyResults[emailNotifier.Name()] - if outcome != "ok" && outcome != "warning" { + if notifyErr != nil || (outcome != "ok" && outcome != "warning") { + logging.Debug("Support mode: support email outcome=%q err=%v", outcome, notifyErr) + logging.Warning("Support mode: FAILED to send support email to %s", recipient) return } diff --git a/internal/support/support_email_failure_line_test.go b/internal/support/support_email_failure_line_test.go new file mode 100644 index 00000000..ac4ff664 --- /dev/null +++ b/internal/support/support_email_failure_line_test.go @@ -0,0 +1,28 @@ +package support + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/notify" + "github.com/tis24dev/proxsave/internal/orchestrator" + "github.com/tis24dev/proxsave/internal/types" +) + +// A failed support email showed only the adapter's "❌ Email: notification failed": +// its CRITICAL branch waited for an error the adapter never returns. The failure now +// gets its own WARNING, naming the support email and its recipient. +func TestSupportEmailFailureHasItsOwnWarning(t *testing.T) { + logger, output := captureSupportEmailFlow(t) + delivery := &supportDeliveryStub{fakeNotifier{enabled: true, result: ¬ify.NotificationResult{Success: false, Method: "email-sendmail", Error: errors.New("sendmail rejected message: exit status 75")}}} + newEmailNotifier = func(notify.EmailConfig, types.ProxmoxType, *logging.Logger) (notify.Notifier, error) { + return delivery, nil + } + SendEmail(context.Background(), nil, logger, types.ProxmoxVE, &orchestrator.BackupStats{Hostname: "pve-test"}, Meta{}, "") + if !strings.Contains(output.String(), "WARNING Support mode: FAILED to send support email to maintainer@example.invalid\n") { + t.Fatalf("no support failure line; log:\n%s", output.String()) + } +} From e165ccc664848d5fe8361a660185e4656ff63f92 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 17:22:33 +0100 Subject: [PATCH 23/44] fix(email): count queued messages, not lines with an address The mail queue count took every mailq line with an "@": postfix prints the sender on the queue-ID line and each recipient on its own, so 3 queued messages read "7 message(s)" (measured on pve-test, postfix 3.10.5), the "increased from X to Y" line was off the same way, and "Large queue detected" fired at 6 real messages instead of 11. The first Go version's comment already meant queue-ID lines. The count is now one per queue-ID line; output with none (exim prints the age first) keeps the line count. Only the numbers inside the existing lines change. Two tests that pinned 4 for a 2-message fixture now expect 2. Audit finding notify-health-4 (audit-prod-go-2026-10-26). --- internal/notify/email_maillog.go | 18 +++++++++--- internal/notify/email_mailq_test.go | 9 +++--- internal/notify/email_queue_count_test.go | 34 +++++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 internal/notify/email_queue_count_test.go diff --git a/internal/notify/email_maillog.go b/internal/notify/email_maillog.go index 8d154231..4e6fb4ea 100644 --- a/internal/notify/email_maillog.go +++ b/internal/notify/email_maillog.go @@ -199,19 +199,28 @@ type mailQueueEntry struct { line string } -// parseMailQueue reads mailq output: the count checkMailQueue reports (lines with an -// "@", the header and footer left out) and the entries with a line that names the -// recipient, in queue order, each once. +// parseMailQueue reads mailq output: the count checkMailQueue reports and the entries +// with a line that names the recipient, in queue order, each once. The count is one per +// queue-ID line, the start of each message: counting every line with an "@" counted the +// sender and each recipient, about twice the real number on postfix. Output with no +// queue-ID line (exim prints the age first) keeps that line count. func parseMailQueue(output, recipient string) (count int, entries []mailQueueEntry) { if strings.Contains(output, "Mail queue is empty") { return 0, nil } lowerRecipient := strings.ToLower(strings.TrimSpace(recipient)) var currentID string + idLines, addressLines := 0, 0 seen := map[string]bool{} + defer func() { + count = addressLines + if idLines > 0 { + count = idLines + } + }() for line := range strings.SplitSeq(output, "\n") { if len(line) > 10 && strings.Contains(line, "@") && !strings.Contains(line, "Mail queue") && !strings.Contains(line, "Total requests") { - count++ + addressLines++ } trimmed := strings.TrimSpace(line) if trimmed == "" { @@ -220,6 +229,7 @@ func parseMailQueue(output, recipient string) (count int, entries []mailQueueEnt fields := strings.Fields(trimmed) if len(fields) > 0 && mailQueueIDLineRegex.MatchString(fields[0]) { currentID = strings.TrimSuffix(strings.TrimSuffix(fields[0], "*"), "!") + idLines++ continue } if currentID != "" && lowerRecipient != "" && !seen[currentID] && strings.Contains(strings.ToLower(trimmed), lowerRecipient) { diff --git a/internal/notify/email_mailq_test.go b/internal/notify/email_mailq_test.go index 3209c86d..bf4bc8cf 100644 --- a/internal/notify/email_mailq_test.go +++ b/internal/notify/email_mailq_test.go @@ -48,8 +48,9 @@ DEF456! 4321 Mon Jan 1 00:00:01 sender2@example.com if err != nil { t.Fatalf("checkMailQueue() error=%v", err) } - if count != 4 { - t.Fatalf("checkMailQueue()=%d want 4", count) + // Two messages ("in 2 Requests"): one per queue-ID line, not per address line. + if count != 2 { + t.Fatalf("checkMailQueue()=%d want 2", count) } } @@ -72,8 +73,8 @@ DEF456! 4321 Mon Jan 1 00:00:01 sender2@example.com if err != nil { t.Fatalf("checkMailQueueFor() error=%v", err) } - if count != 4 || len(entries) != 1 || entries[0].id != "ABC123" || entries[0].line != "admin@example.com" { - t.Fatalf("checkMailQueueFor() = %d, %+v; want 4 and ABC123 for admin@example.com", count, entries) + if count != 2 || len(entries) != 1 || entries[0].id != "ABC123" || entries[0].line != "admin@example.com" { + t.Fatalf("checkMailQueueFor() = %d, %+v; want 2 and ABC123 for admin@example.com", count, entries) } } diff --git a/internal/notify/email_queue_count_test.go b/internal/notify/email_queue_count_test.go new file mode 100644 index 00000000..f43a77de --- /dev/null +++ b/internal/notify/email_queue_count_test.go @@ -0,0 +1,34 @@ +package notify + +import "testing" + +// Postfix prints the sender on the queue-ID line and each recipient on its own line, so +// counting every line with an "@" gave 7 for 3 queued messages (measured on pve-test). +// The count is one per queue-ID line; output without one (exim) keeps the line count. +func TestParseMailQueueCountsMessagesNotAddressLines(t *testing.T) { + postfix := `-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient------- +C65F548FA0 612 Mon Nov 3 10:01:02 root@pve.lan.local +(connect to 192.0.2.1[192.0.2.1]:25: Connection timed out) + a@example.invalid + +CDB2A48FA3 612 Mon Nov 3 10:01:03 root@pve.lan.local +(connect to 192.0.2.1[192.0.2.1]:25: Connection timed out) + b@example.invalid + +BE71448FA1* 640 Mon Nov 3 10:01:04 root@pve.lan.local + c1@example.invalid + c2@example.invalid + +-- 0 Kbytes in 3 Requests. +` + if count, _ := parseMailQueue(postfix, ""); count != 3 { + t.Fatalf("postfix: count = %d, want 3", count) + } + exim := `25m 1.2K 1qABCD-000123-AB + a@example.invalid + +` + if count, _ := parseMailQueue(exim, ""); count != 2 { + t.Fatalf("exim (no queue-ID line): count = %d, want the address-line count 2", count) + } +} From 64b7379e46d7d0b502e5778e6e756e17a38bc542 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 17:22:33 +0100 Subject: [PATCH 24/44] fix(email): read PBS user.cfg as section-config in the recipient fallback On a PBS-only host the recipient fallback parsed /etc/proxmox-backup/user.cfg as PVE's colon lines, but PBS writes it as section-config ("user: root@pam" then indented "email "), so the email of root@pam was never found there when proxmox-backup-manager failed (measured with pve-test's PBS 4.2.5 file). A section-config reader now handles the PBS file; PVE keeps its colon parser. When the CLI already read the user without an email, the file is no longer read (it holds none either). No INFO or WARNING changes. Approved docs line, TROUBLESHOOTING.md: grep -A5 '^user: root@pam' /etc/proxmox-backup/user.cfg Audit finding notify-health-5 (audit-prod-go-2026-10-26). --- docs/TROUBLESHOOTING.md | 2 +- internal/notify/email.go | 46 ++++++++++++++++++++++- internal/notify/email_pbs_usercfg_test.go | 35 +++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 internal/notify/email_pbs_usercfg_test.go diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md index ee9e60f8..4a20b9e5 100644 --- a/docs/TROUBLESHOOTING.md +++ b/docs/TROUBLESHOOTING.md @@ -896,7 +896,7 @@ grep -n '^user:root@pam:' /etc/pve/user.cfg proxmox-backup-manager user list --output-format json # PBS (config file) -grep -n '^user:root@pam:' /etc/proxmox-backup/user.cfg +grep -A5 '^user: root@pam' /etc/proxmox-backup/user.cfg ``` ##### If `EMAIL_DELIVERY_METHOD=sendmail` diff --git a/internal/notify/email.go b/internal/notify/email.go index feca7099..22d4c972 100644 --- a/internal/notify/email.go +++ b/internal/notify/email.go @@ -615,12 +615,16 @@ func (e *EmailNotifier) detectRecipientPBS(ctx context.Context, targetUserID str e.logger.Debug("Recipient auto-detection: resolved via proxmox-backup-manager for %s", targetUserID) return email, nil } + if errors.Is(err, errRecipientWithoutEmail) { + // The CLI read the user: the file holds no email either. + return "", fmt.Errorf("recipient auto-detection failed for %s (pbs): proxmox-backup-manager: %w", targetUserID, err) + } if err != nil { failures = append(failures, fmt.Sprintf("proxmox-backup-manager: %v", err)) } // Last resort: parse /etc/proxmox-backup/user.cfg directly. - email, err = e.detectRecipientViaUserCfg(pbsUserCfgPath, targetUserID) + email, err = e.detectRecipientViaPBSUserCfg(pbsUserCfgPath, targetUserID) if err == nil && strings.TrimSpace(email) != "" { e.logger.Debug("Recipient auto-detection: resolved via user.cfg for %s (path=%s)", targetUserID, pbsUserCfgPath) return email, nil @@ -689,7 +693,7 @@ func (e *EmailNotifier) detectRecipientViaUserListCLI(ctx context.Context, cmdNa } email := strings.TrimSpace(u.Email) if email == "" { - return "", fmt.Errorf("%s user exists but has no email configured (%s)", targetUserID, cmdName) + return "", fmt.Errorf("%s %w (%s)", targetUserID, errRecipientWithoutEmail, cmdName) } e.logger.Debug("Recipient auto-detection: %s returned an email for %s: %s", cmdName, targetUserID, redactEmail(email)) return email, nil @@ -698,6 +702,44 @@ func (e *EmailNotifier) detectRecipientViaUserListCLI(ctx context.Context, cmdNa return "", fmt.Errorf("%s user not found in Proxmox configuration (%s)", targetUserID, cmdName) } +// errRecipientWithoutEmail is a user that exists with no email address set. +var errRecipientWithoutEmail = errors.New("user exists but has no email configured") + +// detectRecipientViaPBSUserCfg reads PBS's user.cfg, which is section-config, not PVE's +// colon lines: a "user: " header, then indented " " lines. Read as +// colon lines it never matched, so the email was never found there. +func (e *EmailNotifier) detectRecipientViaPBSUserCfg(cfgPath string, targetUserID string) (string, error) { + data, err := os.ReadFile(cfgPath) + if err != nil { + return "", fmt.Errorf("failed to read %s: %w", cfgPath, err) + } + inTarget, found := false, false + for line := range strings.SplitSeq(string(data), "\n") { + trimmed := strings.TrimSpace(line) + if trimmed == "" || strings.HasPrefix(trimmed, "#") { + continue + } + if line[0] != ' ' && line[0] != '\t' { + kind, id, ok := strings.Cut(trimmed, ":") + inTarget = ok && strings.TrimSpace(kind) == "user" && strings.TrimSpace(id) == targetUserID + found = found || inTarget + continue + } + if !inTarget { + continue + } + if key, value, ok := strings.Cut(trimmed, " "); ok && key == "email" && strings.TrimSpace(value) != "" { + email := strings.TrimSpace(value) + e.logger.Debug("Recipient auto-detection: user.cfg contains an email for %s: %s", targetUserID, redactEmail(email)) + return email, nil + } + } + if found { + return "", fmt.Errorf("%s %w (user.cfg)", targetUserID, errRecipientWithoutEmail) + } + return "", fmt.Errorf("%s user not found in %s", targetUserID, cfgPath) +} + func (e *EmailNotifier) detectRecipientViaUserCfg(cfgPath string, targetUserID string) (string, error) { data, err := os.ReadFile(cfgPath) if err != nil { diff --git a/internal/notify/email_pbs_usercfg_test.go b/internal/notify/email_pbs_usercfg_test.go new file mode 100644 index 00000000..dc75aaf9 --- /dev/null +++ b/internal/notify/email_pbs_usercfg_test.go @@ -0,0 +1,35 @@ +package notify + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// PBS writes user.cfg as section-config; the fallback read it as PVE's colon lines and +// never found root@pam's email (the bytes below are pve-test's PBS 4.2.5 file). +func TestPBSUserCfgFallbackReadsSectionConfig(t *testing.T) { + notifier, err := NewEmailNotifier(EmailConfig{Enabled: true, DeliveryMethod: EmailDeliverySendmail}, types.ProxmoxBS, logging.New(types.LogLevelDebug, false)) + if err != nil { + t.Fatal(err) + } + dir := t.TempDir() + withEmail := filepath.Join(dir, "with-email.cfg") + if err := os.WriteFile(withEmail, []byte("user: root@pam\n\tcomment Superuser\n\temail proxsave-probe@example.invalid\n\nuser: other@pbs\n\temail other@example.invalid\n"), 0o600); err != nil { + t.Fatal(err) + } + if email, err := notifier.detectRecipientViaPBSUserCfg(withEmail, "root@pam"); err != nil || email != "proxsave-probe@example.invalid" { + t.Fatalf("with email: %q, %v", email, err) + } + stock := filepath.Join(dir, "stock.cfg") + if err := os.WriteFile(stock, []byte("user: root@pam\n\tcomment Superuser\n"), 0o600); err != nil { + t.Fatal(err) + } + if _, err := notifier.detectRecipientViaPBSUserCfg(stock, "root@pam"); err == nil || !strings.Contains(err.Error(), "exists but has no email") { + t.Fatalf("stock file: err %v, want the user without email", err) + } +} From 6c300a4fd13efcfdb47e6141df65c77102e1d9c1 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 18:08:13 +0100 Subject: [PATCH 25/44] fix(cli): stop offering the removed proxmox-backup alias The run, install and upgrade footers and the missing-config hint still named "proxmox-backup" as an alias. Install and upgrade delete that entrypoint since 37e9e420 and nothing recreates it, so an operator following the hint got "command not found" (on pve-test only proxmox-backup-client/-manager/-debug exist). The texts were reworded later (7c2b8509, 90338af1) and kept the alias. Approved texts: proxsave - Open the interactive dashboard (runs the backup directly when non-interactive, e.g. cron) Run 'proxsave --install' to create a new configuration The footer line now sits in the same column as the other commands. Audit finding cmd-main-8 (audit-prod-go-2026-10-26). --- cmd/proxsave/config_helpers.go | 2 +- cmd/proxsave/footer_alias_test.go | 30 ++++++++++++++++++++++++++++++ cmd/proxsave/install.go | 2 +- cmd/proxsave/main_footer.go | 2 +- cmd/proxsave/upgrade.go | 2 +- 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 cmd/proxsave/footer_alias_test.go diff --git a/cmd/proxsave/config_helpers.go b/cmd/proxsave/config_helpers.go index 67bd99fe..0d41d99a 100644 --- a/cmd/proxsave/config_helpers.go +++ b/cmd/proxsave/config_helpers.go @@ -39,7 +39,7 @@ func ensureConfigExists(path string, logger configStatusLogger) error { // No automatic migration or template creation: require explicit user action. logger.Warning("Configuration file not found: %s", path) - logger.Warning("Run 'proxsave --install' (alias: proxmox-backup --install) to create a new configuration") + logger.Warning("Run 'proxsave --install' to create a new configuration") return fmt.Errorf("configuration file is required to continue") } diff --git a/cmd/proxsave/footer_alias_test.go b/cmd/proxsave/footer_alias_test.go new file mode 100644 index 00000000..f79c0a74 --- /dev/null +++ b/cmd/proxsave/footer_alias_test.go @@ -0,0 +1,30 @@ +package main + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// The footers and the missing-config hint offered "proxmox-backup" as an alias, but +// install and upgrade delete that entrypoint (37e9e420) and nothing recreates it: an +// operator following the hint got "command not found". +func TestNoTextOffersTheRemovedProxmoxBackupAlias(t *testing.T) { + files, err := filepath.Glob("*.go") + if err != nil { + t.Fatal(err) + } + for _, f := range files { + if strings.HasSuffix(f, "_test.go") { + continue + } + data, err := os.ReadFile(f) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(data), "alias: proxmox-backup") { + t.Errorf("%s still offers the removed proxmox-backup alias", f) + } + } +} diff --git a/cmd/proxsave/install.go b/cmd/proxsave/install.go index b58f2a51..7cb623c3 100644 --- a/cmd/proxsave/install.go +++ b/cmd/proxsave/install.go @@ -476,7 +476,7 @@ func installFooterBody(installErr error, configPath, baseDir, telegramCode, perm fmt.Println("https://github.com/sponsors/tis24dev") fmt.Println() fmt.Println("Commands:") - fmt.Println(" proxsave (alias: proxmox-backup) - Open the interactive dashboard (runs the backup directly when non-interactive, e.g. cron)") + fmt.Println(" proxsave - Open the interactive dashboard (runs the backup directly when non-interactive, e.g. cron)") fmt.Println(" --backup - Run a backup now (what bare proxsave does when non-interactive)") fmt.Println(" --help - Show all options") fmt.Println(" --dry-run - Test without changes") diff --git a/cmd/proxsave/main_footer.go b/cmd/proxsave/main_footer.go index f51c5561..7a6e0740 100644 --- a/cmd/proxsave/main_footer.go +++ b/cmd/proxsave/main_footer.go @@ -285,7 +285,7 @@ func printFinalSummaryCommands() { fmt.Println("https://github.com/sponsors/tis24dev") fmt.Println() fmt.Println("Commands:") - fmt.Println(" proxsave (alias: proxmox-backup) - Open the interactive dashboard (runs the backup directly when non-interactive, e.g. cron)") + fmt.Println(" proxsave - Open the interactive dashboard (runs the backup directly when non-interactive, e.g. cron)") fmt.Println(" --backup - Run a backup now (what bare proxsave does when non-interactive)") fmt.Println(" --help - Show all options") fmt.Println(" --dry-run - Test without changes") diff --git a/cmd/proxsave/upgrade.go b/cmd/proxsave/upgrade.go index 2ab173dd..d158898f 100644 --- a/cmd/proxsave/upgrade.go +++ b/cmd/proxsave/upgrade.go @@ -1171,7 +1171,7 @@ func upgradeFooterBody(upgradeErr error, version, configPath, baseDir, telegramC fmt.Println() fmt.Println("Commands:") - fmt.Println(" proxsave (alias: proxmox-backup) - Open the interactive dashboard (runs the backup directly when non-interactive, e.g. cron)") + fmt.Println(" proxsave - Open the interactive dashboard (runs the backup directly when non-interactive, e.g. cron)") fmt.Println(" --backup - Run a backup now (what bare proxsave does when non-interactive)") fmt.Println(" --upgrade - Update proxsave binary to latest release (also adds missing keys to backup.env)") fmt.Println(" --install - Re-run interactive installation/setup") From 94bfd31eb7e95a977b8c0ded5910ffba1cd75842 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 18:11:21 +0100 Subject: [PATCH 26/44] fix(upgrade): exit 130 on an interruption, 0 on an unanswered prompt, 1 when GitHub fails --upgrade exited 2, documented as "Invalid or missing configuration", when the release check could not reach GitHub and when the confirmation got Ctrl+C, SIGTERM, end of input or the idle timeout, while a failed download exited 1. A wrapper reading $? saw a configuration error for a network outage or a Ctrl+C. Now (approved): Ctrl+C or SIGTERM during the check or at the prompt exits 130 with no ERROR line, as backup and restore; end of input or the idle timeout at the prompt prints the existing "Upgrade cancelled by user; no changes were made." and exits 0; GitHub not answering prints its ERROR and exits 1, like a failed download. Docs unchanged: they already give 130 for Ctrl+C/SIGTERM. Audit finding cmd-main-10 (audit-prod-go-2026-10-26). --- cmd/proxsave/upgrade.go | 19 +++++- cmd/proxsave/upgrade_exit_codes_test.go | 79 +++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 cmd/proxsave/upgrade_exit_codes_test.go diff --git a/cmd/proxsave/upgrade.go b/cmd/proxsave/upgrade.go index d158898f..f450a330 100644 --- a/cmd/proxsave/upgrade.go +++ b/cmd/proxsave/upgrade.go @@ -26,6 +26,7 @@ import ( "github.com/tis24dev/proxsave/internal/cli" "github.com/tis24dev/proxsave/internal/config" "github.com/tis24dev/proxsave/internal/identity" + "github.com/tis24dev/proxsave/internal/input" "github.com/tis24dev/proxsave/internal/logging" "github.com/tis24dev/proxsave/internal/safeexec" "github.com/tis24dev/proxsave/internal/serverbot" @@ -236,8 +237,14 @@ func upgradeAcquireBinary(ctx context.Context, args *cli.Args, bootstrap *loggin // wrapper repeated the verb. The fifth, "empty tag_name in latest release // response", does not name the operation: on that shape the cause stands // bare. Known cost. + if ctx.Err() != nil { + // Ctrl+C or SIGTERM during the check: an interrupted run, not a + // configuration error (exit 2 said "Invalid or missing configuration"). + return "", "", exitCodeInterrupted, true, err + } bootstrap.Error("%v", err) - return "", "", types.ExitConfigError.Int(), true, err + // GitHub not answering is a failed run, like a failed download (exit 1). + return "", "", types.ExitGenericError.Int(), true, err } logging.DebugStepBootstrap(bootstrap, "upgrade workflow", "current=%s latest=%s", currentVersion, latestVersion) @@ -259,6 +266,16 @@ func upgradeAcquireBinary(ctx context.Context, args *cli.Args, bootstrap *loggin reader := bufio.NewReader(os.Stdin) confirmed, promptErr := promptYesNo(ctx, reader, "Do you want to download and install this version now? (backup.env will be updated with any missing keys; a backup will be created) [y/N]: ", false) if promptErr != nil { + switch { + case ctx.Err() != nil: + // Ctrl+C or SIGTERM at the prompt: exit 130, as backup and restore. + return "", "", exitCodeInterrupted, true, promptErr + case errors.Is(promptErr, input.ErrInputAborted): + // End of input or the idle timeout: nothing was chosen, nothing changed. + logging.DebugStepBootstrap(bootstrap, "upgrade workflow", "prompt ended without an answer: %v", promptErr) + bootstrap.Println("Upgrade cancelled by user; no changes were made.") + return "", "", types.ExitSuccess.Int(), true, nil + } bootstrap.Error("%v", promptErr) return "", "", types.ExitConfigError.Int(), true, promptErr } diff --git a/cmd/proxsave/upgrade_exit_codes_test.go b/cmd/proxsave/upgrade_exit_codes_test.go new file mode 100644 index 00000000..b594249a --- /dev/null +++ b/cmd/proxsave/upgrade_exit_codes_test.go @@ -0,0 +1,79 @@ +package main + +import ( + "bytes" + "context" + "net/http" + "net/http/httptest" + "net/url" + "os" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/cli" + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// --upgrade exited 2, "configuration error", when GitHub did not answer and when the +// confirmation got Ctrl+C, SIGTERM, end of input or the idle timeout. Now: an +// interruption exits 130 as backup and restore do, a prompt left without an answer is +// a cancel (exit 0), and an unreachable GitHub is a failed run (exit 1), like a failed +// download. +func TestUpgradeExitCodesForAbortsAndFetchFailures(t *testing.T) { + for _, tc := range []struct { + name string + status int + cancel bool + wantCode int + wantLine string + }{ + {"github 502", http.StatusBadGateway, false, types.ExitGenericError.Int(), "failed to fetch latest release: status 502 Bad Gateway"}, + {"interrupted during the check", http.StatusOK, true, exitCodeInterrupted, ""}, + {"end of input at the prompt", http.StatusOK, false, types.ExitSuccess.Int(), "Upgrade cancelled by user; no changes were made."}, + } { + t.Run(tc.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(tc.status) + _, _ = w.Write([]byte(`{"tag_name":"v99.0.0","body":""}`)) + })) + defer server.Close() + target, _ := url.Parse(server.URL) + prevTransport := http.DefaultTransport + http.DefaultTransport = rewriteToServer{target: target, base: prevTransport} + defer func() { http.DefaultTransport = prevTransport }() + + stdinR, stdinW, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + _ = stdinW.Close() + prevStdin := os.Stdin + os.Stdin = stdinR + defer func() { os.Stdin = prevStdin; _ = stdinR.Close() }() + + var out bytes.Buffer + bootstrap := logging.NewBootstrapLogger() + bootstrap.SetConsoleQuiet(true) + mirror := logging.New(types.LogLevelDebug, false) + mirror.SetOutput(&out) + bootstrap.SetMirrorLogger(mirror) + + ctx, cancel := context.WithCancel(context.Background()) + if tc.cancel { + cancel() + } + defer cancel() + _, _, code, terminal, _ := upgradeAcquireBinary(ctx, &cli.Args{Upgrade: true}, bootstrap, t.TempDir(), "0.1.0") + if !terminal || code != tc.wantCode { + t.Fatalf("terminal=%v code=%d, want %d; log:\n%s", terminal, code, tc.wantCode, out.String()) + } + if tc.wantLine != "" && !strings.Contains(out.String(), tc.wantLine) { + t.Fatalf("no line %q; log:\n%s", tc.wantLine, out.String()) + } + if tc.cancel && strings.Contains(out.String(), "ERROR") { + t.Fatalf("an interruption printed an ERROR; log:\n%s", out.String()) + } + }) + } +} From ba3fd4ffa5efe26910c8f8ffbabda8518179a902 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 20:09:20 +0100 Subject: [PATCH 27/44] fix(install): an incomplete post-install dry-run is no longer "no unused components" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The post-install audit runner swallows every exit status, and the collector never read its own context: a dry-run killed by Esc or by the 90 s bound, or one that stopped early (exit 16, a backup already running), returned no suggestions and no error, shown as the green "✓ NO UNUSED COMPONENTS" (TUI) or "No unused components detected" (CLI). That check is what keeps a stock PBS green by offering to disable the optional collectors. The collector now returns an error for a cancelled or timed-out dry-run and for an exit status other than 0 or 1. Approved texts: a dry-run stopped with Esc shows "Status: NOT CHECKED" with "The dry-run was cancelled before it finished."; the others keep the existing CHECK FAILED screen and CLI WARNING with "post-install audit dry-run failed: context deadline exceeded" or "...: exit code 16". Audit finding ui-config-1 (audit-prod-go-2026-10-26). --- internal/installer/audit.go | 11 ++++- .../installer/audit_incomplete_dryrun_test.go | 49 +++++++++++++++++++ internal/ui/flows/install/audit.go | 7 +++ .../ui/flows/install/audit_cancel_test.go | 34 +++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 internal/installer/audit_incomplete_dryrun_test.go create mode 100644 internal/ui/flows/install/audit_cancel_test.go diff --git a/internal/installer/audit.go b/internal/installer/audit.go index 5915f842..32525dcd 100644 --- a/internal/installer/audit.go +++ b/internal/installer/audit.go @@ -91,10 +91,19 @@ func CollectPostInstallDisableSuggestions(ctx context.Context, execPath, configP auditCtx, cancel := context.WithTimeout(ctx, 90*time.Second) defer cancel() - output, _, err := postInstallAuditRunner(auditCtx, execPath, configPath) + output, exitCode, err := postInstallAuditRunner(auditCtx, execPath, configPath) if err != nil { return nil, err } + // The runner swallows every exit status, so a dry-run killed by a cancel or by the + // 90 s bound, or one that stopped early (exit 16: a backup already running), read as + // a clean "no unused components". Exit 1 is the warnings the hints are parsed from. + if ctxErr := auditCtx.Err(); ctxErr != nil { + return nil, fmt.Errorf("post-install audit dry-run failed: %w", ctxErr) + } + if exitCode != 0 && exitCode != 1 { + return nil, fmt.Errorf("post-install audit dry-run failed: exit code %d", exitCode) + } issueLines := extractIssueLinesFromProxsaveOutput(output) return extractDisableSuggestionsFromIssueLines(issueLines, allowed, configValues), nil diff --git a/internal/installer/audit_incomplete_dryrun_test.go b/internal/installer/audit_incomplete_dryrun_test.go new file mode 100644 index 00000000..74d22cba --- /dev/null +++ b/internal/installer/audit_incomplete_dryrun_test.go @@ -0,0 +1,49 @@ +package installer + +import ( + "context" + "os" + "path/filepath" + "testing" +) + +// The runner swallows every exit status, so a dry-run killed by a cancel or the 90 s +// bound, or one that stopped early (exit 16), returned no suggestions and no error: a +// clean "no unused components". Those now come back as errors; exit 1 stays the normal +// case the hints are parsed from. +func TestPostInstallAuditReportsAnIncompleteDryRun(t *testing.T) { + cfgPath := filepath.Join(t.TempDir(), "backup.env") + if err := os.WriteFile(cfgPath, []byte("BACKUP_CLUSTER_CONFIG=true\n"), 0o600); err != nil { + t.Fatal(err) + } + oldRunner := postInstallAuditRunner + t.Cleanup(func() { postInstallAuditRunner = oldRunner }) + + for _, tc := range []struct { + name string + code int + cancel bool + wantErr string + }{ + {"exit 16", 16, false, "post-install audit dry-run failed: exit code 16"}, + {"cancelled", 1, true, "post-install audit dry-run failed: context canceled"}, + {"warnings (exit 1)", 1, false, ""}, + } { + postInstallAuditRunner = func(ctx context.Context, execPath, configPath string) (string, int, error) { + return "", tc.code, nil + } + ctx, cancel := context.WithCancel(context.Background()) + if tc.cancel { + cancel() + } + _, err := CollectPostInstallDisableSuggestions(ctx, "/fake/proxsave", cfgPath) + cancel() + got := "" + if err != nil { + got = err.Error() + } + if got != tc.wantErr { + t.Errorf("%s: error %q, want %q", tc.name, got, tc.wantErr) + } + } +} diff --git a/internal/ui/flows/install/audit.go b/internal/ui/flows/install/audit.go index 1bc382fd..9437cfb0 100644 --- a/internal/ui/flows/install/audit.go +++ b/internal/ui/flows/install/audit.go @@ -82,6 +82,13 @@ func RunPostInstallAudit(ctx context.Context, session *shell.Session, execPath, suggestions = found return nil }) + if errors.Is(collectErr, context.Canceled) { + // The operator stopped the dry-run: nothing was checked, nothing failed. + result.CollectErr = collectErr + showAuditResult(ctx, session, "Post-install check", orchestrator.HealthcheckSetupLevelNeutral, + "NOT CHECKED", "The dry-run was cancelled before it finished.", backToMenu) + return result, nil + } if collectErr != nil { result.CollectErr = collectErr showAuditResult(ctx, session, "Post-install check", orchestrator.HealthcheckSetupLevelWarn, diff --git a/internal/ui/flows/install/audit_cancel_test.go b/internal/ui/flows/install/audit_cancel_test.go new file mode 100644 index 00000000..38953078 --- /dev/null +++ b/internal/ui/flows/install/audit_cancel_test.go @@ -0,0 +1,34 @@ +package install + +import ( + "context" + "fmt" + "testing" + + "github.com/tis24dev/proxsave/internal/installer" +) + +// A dry-run stopped by the operator showed the green "✓ NO UNUSED COMPONENTS". It now +// shows NOT CHECKED with the reason; a failed or timed-out dry-run keeps CHECK FAILED. +func TestRunPostInstallAuditCancelledDryRunIsNotChecked(t *testing.T) { + d := newDriver(t) + origCollect := auditCollect + auditCollect = func(ctx context.Context, execPath, cfgPath string) ([]installer.PostInstallAuditSuggestion, error) { + return nil, fmt.Errorf("post-install audit dry-run failed: %w", context.Canceled) + } + t.Cleanup(func() { auditCollect = origCollect }) + + done := make(chan installer.PostInstallAuditResult, 1) + go func() { + res, _ := RunPostInstallAudit(t.Context(), d.session, "/fake/proxsave", "/fake/backup.env", false) + done <- res + }() + d.waitScreen("Post-install check") + d.keys("enter") + d.waitText("NOT CHECKED") + d.waitText("The dry-run was cancelled before it finished.") + d.keys("enter") + if res := <-done; res.CollectErr == nil { + t.Fatalf("the cancelled dry-run must be recorded on CollectErr, got %+v", res) + } +} From 3423786a43fa9ac4b1b94b3fc488bbd2462c19ee Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 20:12:32 +0100 Subject: [PATCH 28/44] fix(install): Ctrl+C on the TUI Healthchecks step aborts the install On the TUI install, Ctrl+C at the Healthchecks selector came back from the step as "check skipped" (nil: the step also serves the dashboard, where that is right) and on its spinner as the task's own context.Canceled. The install then ran its finalization on the closed session, ended "Installation failed" (exit 2) and did not restore backup.env, while the CLI treats the same Ctrl+C as an abort with the restore. 3774deaf meant every optional step to abort on Ctrl+C. After the step, a closed session is now the operator's Ctrl+C: the driver reports it as the other steps do. Existing texts, as approved ("like the CLI"): "Healthcheck setup aborted (interrupted); stopping the install before finalization", the "Installation aborted" banner, exit 0, backup.env restored. As on the CLI, the scheduler reconciliation before the step stays done. Audit finding ui-config-2 (audit-prod-go-2026-10-26). --- .../install_closed_session_abort_test.go | 34 +++++++++++++++++++ cmd/proxsave/install_step_abort.go | 14 ++++++++ cmd/proxsave/install_tui.go | 5 +++ 3 files changed, 53 insertions(+) create mode 100644 cmd/proxsave/install_closed_session_abort_test.go diff --git a/cmd/proxsave/install_closed_session_abort_test.go b/cmd/proxsave/install_closed_session_abort_test.go new file mode 100644 index 00000000..c53c032e --- /dev/null +++ b/cmd/proxsave/install_closed_session_abort_test.go @@ -0,0 +1,34 @@ +package main + +import ( + "context" + "errors" + "testing" + + "github.com/tis24dev/proxsave/internal/logging" +) + +// Ctrl+C on the TUI Healthchecks step came back as "check skipped" (nil) or as the +// spinner's context.Canceled, so the install went on into finalization on a closed +// session and ended "Installation failed" (exit 2) without restoring backup.env. A +// closed session after the step is now the abort the other steps already report. +func TestClosedSessionAfterAStepAbortsTheInstall(t *testing.T) { + closed := make(chan struct{}) + close(closed) + open := make(chan struct{}) + bootstrap := logging.NewBootstrapLogger() + bootstrap.SetConsoleQuiet(true) + + for _, stepErr := range []error{nil, context.Canceled} { + err := abortInstallOnOptionalStep(context.Background(), bootstrap, "Healthcheck setup", closedSessionIsAbort(closed, stepErr, bootstrap)) + if !isInstallAbortedError(err) { + t.Fatalf("closed session, step err %v: got %v, want the aborted-install error", stepErr, err) + } + } + if err := abortInstallOnOptionalStep(context.Background(), bootstrap, "Healthcheck setup", closedSessionIsAbort(open, nil, bootstrap)); err != nil { + t.Fatalf("open session, skipped step: got %v, want the install to continue", err) + } + if err := closedSessionIsAbort(open, errors.New("relay unreachable"), bootstrap); err == nil || err.Error() != "relay unreachable" { + t.Fatalf("open session keeps the step error, got %v", err) + } +} diff --git a/cmd/proxsave/install_step_abort.go b/cmd/proxsave/install_step_abort.go index 8cb1effc..a3c22bd4 100644 --- a/cmd/proxsave/install_step_abort.go +++ b/cmd/proxsave/install_step_abort.go @@ -57,3 +57,17 @@ func abortInstallOnOptionalStep(ctx context.Context, bootstrap *logging.Bootstra logBootstrapWarning(bootstrap, "%s aborted (interrupted); stopping the install before finalization", step) return wrapInstallError(errInteractiveAborted) } + +// closedSessionIsAbort turns a step result into shell.ErrClosed when the session is +// already closed, which on the TUI install means the operator pressed Ctrl+C: a step +// that maps Ctrl+C to "skipped" (or to its task's context.Canceled) would otherwise let +// the install run its finalization on a dead session. +func closedSessionIsAbort(done <-chan struct{}, stepErr error, bootstrap *logging.BootstrapLogger) error { + select { + case <-done: + logging.DebugStepBootstrap(bootstrap, "install workflow (tui)", "session closed during the step (step err=%v)", stepErr) + return shell.ErrClosed + default: + return stepErr + } +} diff --git a/cmd/proxsave/install_tui.go b/cmd/proxsave/install_tui.go index 2f4a1d61..9cd8f4ae 100644 --- a/cmd/proxsave/install_tui.go +++ b/cmd/proxsave/install_tui.go @@ -296,6 +296,11 @@ func runInstallTUI(ctx context.Context, configPath string, bootstrap *logging.Bo // (self) verify the pasted alive URL is reachable. Eligibility is decided solely // by RunHealthcheckSetup (re-reads the written config); Shown=false with no UI otherwise. hcRes, hcErr := flowinstall.RunHealthcheckSetup(ctx, session, baseDir, configPath, false, installer.HealthcheckSetupOptions{BeforeBootstrap: runtimePreparation.PrepareCaptured}) + // The step maps Ctrl+C to "check skipped" (right for the dashboard, which also + // runs it), and a Ctrl+C on its spinner to the task's own context.Canceled: either + // way the install carried on into finalization on a closed session and ended as + // "Installation failed". A closed session here is the operator's Ctrl+C. + hcErr = closedSessionIsAbort(session.Done(), hcErr, bootstrap) if abortErr := abortInstallOnOptionalStep(ctx, bootstrap, "Healthcheck setup", hcErr); abortErr != nil { return abortErr } From 73537fdf28a30caa43fc7bf836c198347a38ca4c Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 21:46:56 +0100 Subject: [PATCH 29/44] fix(daemon): follow a symlinked backup.env in the daemon-mode writes The daemon-mode writes of backup.env (SCHEDULER_MODE, the schedule adoption) go through a read and an atomic write rooted at the file's parent, which refuse a final component that is an absolute or escaping symlink ("openat backup.env: path escapes from parent"), while the loader and the upgrade merge follow it. On such a link the switch to the daemon removed the cron line, lost its schedule (the daemon ran at 02:00), warned and then reported the daemon enabled, and the dashboard showed a green INSTALLED over a backup.env still saying cron (measured on pve-test). The link is now resolved first and the write roots at the target's parent, as resolveSystemCronPath does for /etc/cron.d; the link stays a link. An adoption that cannot read backup.env, for any cause, now gets the WARNING block a failed write already had, with the approved reason "backup.env could not be read" (it was DEBUG only while the cron line was removed anyway). Docs unchanged: they still advise a regular file. Audit finding cmd-daemon-2 (audit-prod-go-2026-10-26). --- cmd/proxsave/backup_env_symlink_write_test.go | 75 +++++++++++++++++++ cmd/proxsave/daemon_setup.go | 28 ++++++- cmd/proxsave/schedule_helpers.go | 13 +++- 3 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 cmd/proxsave/backup_env_symlink_write_test.go diff --git a/cmd/proxsave/backup_env_symlink_write_test.go b/cmd/proxsave/backup_env_symlink_write_test.go new file mode 100644 index 00000000..d3de0f1a --- /dev/null +++ b/cmd/proxsave/backup_env_symlink_write_test.go @@ -0,0 +1,75 @@ +package main + +import ( + "bytes" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// A configs/backup.env that is an absolute or escaping symlink was refused by the +// daemon-mode writes ("path escapes from parent") while the loader followed it: the +// switch to the daemon removed the cron line and recorded nothing. The write now +// follows the link to its target, and the link stays a link. +func TestSetBackupEnvKeysFollowsASymlinkedConfig(t *testing.T) { + for _, tc := range []struct { + name string + link func(target, configs string) string + }{ + {"absolute link outside", func(target, _ string) string { return target }}, + {"relative link escaping", func(target, configs string) string { + rel, _ := filepath.Rel(configs, target) + return rel + }}, + } { + t.Run(tc.name, func(t *testing.T) { + root := t.TempDir() + target := filepath.Join(root, "etc", "proxsave", "backup.env") + configs := filepath.Join(root, "base", "configs") + for _, d := range []string{filepath.Dir(target), configs} { + if err := os.MkdirAll(d, 0o755); err != nil { + t.Fatal(err) + } + } + if err := os.WriteFile(target, []byte("SCHEDULER_MODE=cron\n"), 0o600); err != nil { + t.Fatal(err) + } + link := filepath.Join(configs, "backup.env") + if err := os.Symlink(tc.link(target, configs), link); err != nil { + t.Fatal(err) + } + if err := setBackupEnvKeys(link, map[string]string{"SCHEDULER_MODE": "daemon"}); err != nil { + t.Fatalf("setBackupEnvKeys: %v", err) + } + data, err := os.ReadFile(target) + if err != nil || !strings.Contains(string(data), "SCHEDULER_MODE=daemon") { + t.Fatalf("target = %q (%v), want SCHEDULER_MODE=daemon", data, err) + } + if fi, err := os.Lstat(link); err != nil || fi.Mode()&os.ModeSymlink == 0 { + t.Fatalf("the link was replaced: %v %v", fi.Mode(), err) + } + }) + } +} + +// The adoption that cannot read backup.env logged only DEBUG while the cron line was +// removed anyway; it is now the WARNING a failed write already gets. +func TestScheduleAdoptionWarnsWhenBackupEnvCannotBeRead(t *testing.T) { + var out bytes.Buffer + bootstrap := logging.NewBootstrapLogger() + bootstrap.SetConsoleQuiet(true) + mirror := logging.New(types.LogLevelDebug, false) + mirror.SetOutput(&out) + bootstrap.SetMirrorLogger(mirror) + adoptSchedulerTimeForDaemon(filepath.Join(t.TempDir(), "missing", "backup.env"), []string{"30 21 * * * /usr/local/bin/proxsave"}, bootstrap) + log := out.String() + for _, want := range []string{"Backup schedule: not adopted from cron entry", "backup.env could not be read"} { + if !strings.Contains(log, want) { + t.Fatalf("missing %q; log:\n%s", want, log) + } + } +} diff --git a/cmd/proxsave/daemon_setup.go b/cmd/proxsave/daemon_setup.go index 123b8106..ad1cc165 100644 --- a/cmd/proxsave/daemon_setup.go +++ b/cmd/proxsave/daemon_setup.go @@ -5,6 +5,8 @@ import ( "context" "errors" "fmt" + "os" + "path/filepath" "slices" "strings" "time" @@ -720,11 +722,35 @@ func maybeAutoMigrateDaemon(ctx context.Context, configPath, baseDir, execToken logBootstrapInfo(bootstrap, "Daemon mode enabled: %s is active. %s", daemonUnitName, cronRemovalClause(cronOutcome)) } +// resolveConfigFileLink returns the file a symlinked backup.env points at, or path +// itself. The confined read and the atomic write root at the parent directory and refuse +// a final component that is an absolute or escaping symlink ("path escapes from +// parent"), while the loader and the upgrade merge follow it: on such a link the daemon +// switch removed the cron line and recorded nothing. Rooting at the target's parent +// keeps the confinement, as resolveSystemCronPath does for /etc/cron.d. A path that is +// not a symlink, or whose target is not a regular file, is returned unchanged. +func resolveConfigFileLink(path string) string { + info, err := os.Lstat(path) + if err != nil || info.Mode()&os.ModeSymlink == 0 { + return path + } + resolved, err := filepath.EvalSymlinks(path) + if err != nil { + return path + } + if target, err := os.Stat(resolved); err != nil || !target.Mode().IsRegular() { + return path + } + return resolved +} + // setBackupEnvKeys reads backup.env, applies the given key=value edits (replacing // or appending each), and writes it back atomically. utils.SetEnvValue preserves // inline comments and ordering. func setBackupEnvKeys(configPath string, kv map[string]string) error { - // Operator-configured path (same trust level as the install/upgrade writers). + // Operator-configured path (same trust level as the install/upgrade writers, which + // follow a symlinked backup.env too). + configPath = resolveConfigFileLink(configPath) data, err := safefs.ReadFileUnderRoot(configPath) if err != nil { return err diff --git a/cmd/proxsave/schedule_helpers.go b/cmd/proxsave/schedule_helpers.go index 035c3eb2..842482a0 100644 --- a/cmd/proxsave/schedule_helpers.go +++ b/cmd/proxsave/schedule_helpers.go @@ -109,6 +109,7 @@ const ( scheduleAdoptedOutcome = theme.SymbolSuccess + " Backup schedule: adopted from cron entry" scheduleWriteFailedOutcome = theme.SymbolWarning + " Backup schedule: not adopted from cron entry" scheduleWriteFailedWhy = "backup.env could not be written" + scheduleReadFailedWhy = "backup.env could not be read" cronRemovalHeader = "Removing proxsave cron entry..." cronRemovedOutcome = theme.SymbolSuccess + " Proxsave cron entry: removed" cronNotRemovedOutcome = theme.SymbolWarning + " Proxsave cron entry: not removed, remove it by hand to avoid a double backup" @@ -756,9 +757,17 @@ func adoptSchedulerTimeForDaemon(configPath string, lines []string, bootstrap *l if !reading.Found { return } - data, err := safefs.ReadFileUnderRoot(configPath) + data, err := safefs.ReadFileUnderRoot(resolveConfigFileLink(configPath)) if err != nil { - logBootstrapDebug(bootstrap, "schedule adopt: config unreadable file=%s error=%v, nothing adopted", configPath, err) + // The cron line is removed after this either way, so a schedule that cannot be + // adopted is the same WARNING as one that cannot be written. + logScheduleLines(bootstrap, scheduleBlock{ + Header: scheduleAdoptHeader, + Debug: append(reading.debugLines(), fmt.Sprintf("schedule adopt: config unreadable file=%s error=%v, nothing adopted", configPath, err)), + Why: scheduleReadFailedWhy, + Outcome: scheduleWriteFailedOutcome, + Warn: true, + }.lines()) return } prefill := installer.DeriveInstallWizardPrefill(string(data)) From 0223d489c1f9948399381ca0fbdf2a0224d30a72 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Tue, 3 Nov 2026 22:32:39 +0100 Subject: [PATCH 30/44] fix(cron): see an /etc cron line with an assignment in front of the command The system-crontab extractor (/etc/crontab, /etc/cron.d) returned the command field as it was, so "0 2 * * * root TZ=UTC /usr/local/bin/proxsave" (or PATH=/LANG= in front) was invisible to the four checks that use it: the --upgrade retrofit refusal, the daemon setup warning, the --daemon-remove advisory and the schedule seed. A retrofitted daemon then ran beside a live /etc schedule. The user-crontab extractor steps over such assignments since 3eac58c4; its own comment promised the two "can never drift". Both now take the command word through one helper, cronCommandWordAt(fields, n), so the step-over (and its guard for an assignment it cannot lex whole) is shared. No new text: the existing lines appear for these hosts. /etc stays read-only to ProxSave. The comment that said cron does not read /etc/timezone and that TZ= is the ordinary way to pin a job's hour is corrected: cron(8) fires every job in its single timezone from /etc/timezone; TZ= only sets the job's environment (approved). Audit finding cmd-daemon-4 (audit-prod-go-2026-10-26). --- cmd/proxsave/cron_indirect_refs.go | 15 ++++-------- cmd/proxsave/runtime_helpers.go | 26 +++++++++++++-------- cmd/proxsave/system_cron_env_prefix_test.go | 26 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 cmd/proxsave/system_cron_env_prefix_test.go diff --git a/cmd/proxsave/cron_indirect_refs.go b/cmd/proxsave/cron_indirect_refs.go index 2c46dfe2..c03f506e 100644 --- a/cmd/proxsave/cron_indirect_refs.go +++ b/cmd/proxsave/cron_indirect_refs.go @@ -2076,8 +2076,9 @@ func resolveSystemCronPath(path string) string { // MM HH DOM MON DOW USER COMMAND -> field 7 is the command // @daily USER COMMAND -> field 3 is the command // -// Comments and environment assignments are rejected exactly as in the user format, so -// a line like "MAILTO=root" is never mistaken for a command. +// Comments and environment assignment lines are rejected exactly as in the user format, +// so a line like "MAILTO=root" is never mistaken for a command, and an assignment in front +// of the command is stepped over as there (cronCommandWordAt). func systemCronCommandToken(line string) string { trimmed := strings.TrimSpace(line) if trimmed == "" || strings.HasPrefix(trimmed, "#") { @@ -2088,15 +2089,9 @@ func systemCronCommandToken(line string) string { return "" } if strings.HasPrefix(fields[0], "@") { - if len(fields) >= 3 { - return fields[2] - } - return "" - } - if len(fields) <= 6 { - return "" + return cronCommandWordAt(fields, 2) } - return fields[6] + return cronCommandWordAt(fields, 6) } // warnIndirectProxsaveCronOnDaemonInstall reports, without touching anything, every diff --git a/cmd/proxsave/runtime_helpers.go b/cmd/proxsave/runtime_helpers.go index 83d1ed25..4776006f 100644 --- a/cmd/proxsave/runtime_helpers.go +++ b/cmd/proxsave/runtime_helpers.go @@ -1241,23 +1241,29 @@ func cronCommandToken(line string) string { } if strings.HasPrefix(first, "@") { - if len(fields) >= 2 { - return cronCommandWord(fields[1:]) - } - return "" + return cronCommandWordAt(fields, 1) } + return cronCommandWordAt(fields, 5) +} - if len(fields) <= 5 { +// cronCommandWordAt is the command word of a crontab line whose command starts at +// field n: 5 in the user format (1 after an @nickname), 6 in the system format (2), one +// later for its USER field. Both extractors go through it: the system one took the field +// as it was, so "root TZ=UTC /usr/local/bin/proxsave" in /etc/cron.d was invisible to the +// duplicate-schedule checks while the same job in the root crontab was seen. +func cronCommandWordAt(fields []string, n int) string { + if len(fields) <= n { return "" } - return cronCommandWord(fields[5:]) + return cronCommandWord(fields[n:]) } // cronCommandWord picks the token to judge out of the words that follow the schedule. -// cron accepts VAR=value in front of a job's command, and an operator writes one there -// more often than it looks: cron does not read /etc/timezone, so "TZ=" in front of a -// backup job is the ordinary way to pin its hour. Without this the whole line was -// invisible - to the detector, to the removal, and to the schedule adoption alike. +// cron accepts VAR=value in front of a job's command (TZ=, PATH=, LANG=): the shell runs +// it as an assignment for that job only. It does not move the job's hour - cron(8) fires +// every job in its own single timezone, from /etc/timezone - but it hides the command +// behind it. Without this the whole line was invisible - to the detector, to the +// removal, and to the schedule adoption alike. // // It is deliberately ADDITIVE, because four of this function's callers WRITE the root // crontab and a token invented here is a line ProxSave deletes. Every line that answers diff --git a/cmd/proxsave/system_cron_env_prefix_test.go b/cmd/proxsave/system_cron_env_prefix_test.go new file mode 100644 index 00000000..08fd9686 --- /dev/null +++ b/cmd/proxsave/system_cron_env_prefix_test.go @@ -0,0 +1,26 @@ +package main + +import "testing" + +// The system-crontab extractor took the command field as it was, so a /etc/cron.d line +// with an assignment in front of the command ("root TZ=UTC /usr/local/bin/proxsave") +// was invisible to the duplicate-schedule checks, while the same job in the root +// crontab was seen (3eac58c4). Both extractors now share the step-over. +func TestSystemCronCommandTokenStepsOverAssignments(t *testing.T) { + for _, tc := range []struct{ system, user, want string }{ + {"0 2 * * * root TZ=UTC /usr/local/bin/proxsave", "0 2 * * * TZ=UTC /usr/local/bin/proxsave", "/usr/local/bin/proxsave"}, + {"@daily root TZ=Europe/Rome /usr/local/bin/proxsave --backup", "@daily TZ=Europe/Rome /usr/local/bin/proxsave --backup", "/usr/local/bin/proxsave"}, + {"0 2 * * * root PATH=/usr/sbin:/usr/bin LANG=C /usr/local/bin/proxsave", "0 2 * * * PATH=/usr/sbin:/usr/bin LANG=C /usr/local/bin/proxsave", "/usr/local/bin/proxsave"}, + {"0 2 * * * root /usr/local/bin/proxsave", "0 2 * * * /usr/local/bin/proxsave", "/usr/local/bin/proxsave"}, + {`0 2 * * * root MSG=a\ proxsave /usr/bin/rsync`, `0 2 * * * MSG=a\ proxsave /usr/bin/rsync`, `MSG=a\`}, + {"MAILTO=root", "MAILTO=root", ""}, + {"0 2 * * * root", "0 2 * * *", ""}, + } { + if got := systemCronCommandToken(tc.system); got != tc.want { + t.Errorf("systemCronCommandToken(%q) = %q, want %q", tc.system, got, tc.want) + } + if got := cronCommandToken(tc.user); got != tc.want { + t.Errorf("cronCommandToken(%q) = %q, want %q", tc.user, got, tc.want) + } + } +} From 9a6473b57d8bb8e01fb7e4450b8e707cf981ab03 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Wed, 4 Nov 2026 08:56:41 +0100 Subject: [PATCH 31/44] fix(logging): mark lost run-log lines and count them in the exit code A write to the run log that failed for any reason but a timeout (disk full, EIO, a read-only remount, a closed fd) was dropped without a word and the sink stayed armed. The run log - copied to the secondary and the cloud, attached to the email - silently lost its WARNING and ERROR lines, and the exit code and the healthchecks ping, computed by re-reading that file, read a clean run while the footer counted the issues (measured on a full tmpfs: file 0 bytes, exit 0). Only the timeout path had been decided (F11-04: strikes, marker). Now (approved): the first lost line of a series prints, on stdout and counted as a warning, WARNING log file write failed (); continuing on stdout, lines are missing from the on-disk log and the first line that lands afterwards is preceded in the file by [] WARNING log file writes failed (); lines missing from the on-disk log here The sink stays armed, as the strike rule wants for a passing stall. When any line of the run missed the file (a write error, a timeout, the sink disabled), the re-parse takes the higher of the file's counts and the logger's own, which also closes the same hole on the timeout path. Audit finding platform-5 (audit-prod-go-2026-10-26). --- internal/logging/log_write_gap_test.go | 59 +++++++++++++++ internal/logging/logger.go | 72 ++++++++++++++++--- internal/orchestrator/extensions.go | 10 +++ .../log_incomplete_counts_test.go | 39 ++++++++++ 4 files changed, 171 insertions(+), 9 deletions(-) create mode 100644 internal/logging/log_write_gap_test.go create mode 100644 internal/orchestrator/log_incomplete_counts_test.go diff --git a/internal/logging/log_write_gap_test.go b/internal/logging/log_write_gap_test.go new file mode 100644 index 00000000..d99de736 --- /dev/null +++ b/internal/logging/log_write_gap_test.go @@ -0,0 +1,59 @@ +package logging + +import ( + "bytes" + "io" + "os" + "path/filepath" + "strings" + "syscall" + "testing" + + "github.com/tis24dev/proxsave/internal/types" +) + +// A log-file write error other than a timeout (disk full, EIO, read-only) was dropped +// without a word: the run log lost its WARNING and ERROR lines and nothing said so. The +// first lost line now prints a WARNING on stdout (counted), and the first line that +// lands afterwards is preceded by a marker naming how many are missing. +func TestLogFileWriteErrorsAreMarked(t *testing.T) { + path := filepath.Join(t.TempDir(), "run.log") + var out bytes.Buffer + logger := New(types.LogLevelDebug, false) + logger.SetOutput(&out) + if err := logger.OpenLogFile(path); err != nil { + t.Fatal(err) + } + failing := 2 + prev := fileWrite + fileWrite = func(w io.Writer, s string) (int, error) { + if failing > 0 { + failing-- + return 0, syscall.ENOSPC + } + return prev(w, s) + } + t.Cleanup(func() { fileWrite = prev }) + + logger.Warning("first lost") + logger.Error("second lost") + logger.Info("lands") + _ = logger.CloseLogFile() + + stdout := out.String() + if n := strings.Count(stdout, "log file write failed (no space left on device); continuing on stdout, lines are missing from the on-disk log"); n != 1 { + t.Fatalf("stdout WARNING count = %d, want 1; stdout:\n%s", n, stdout) + } + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + file := string(data) + marker := "log file writes failed (no space left on device); 2 lines missing from the on-disk log here" + if !strings.Contains(file, marker) || strings.Index(file, marker) > strings.Index(file, "lands") { + t.Fatalf("file lacks the marker before the next line; file:\n%s", file) + } + if !logger.FileIncomplete() || logger.WarningCount() != 2 || logger.ErrorCount() != 1 { + t.Fatalf("incomplete=%v warnings=%d errors=%d, want true 2 1", logger.FileIncomplete(), logger.WarningCount(), logger.ErrorCount()) + } +} diff --git a/internal/logging/logger.go b/internal/logging/logger.go index edc4b3b1..cd37db98 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -54,8 +54,14 @@ type Logger struct { // only after logSinkTimeoutStrikes strikes so a single transient stall does not // permanently truncate the on-disk log. A successful write resets it. Guarded by mu. consecutiveLogTimeouts int - warningCount int64 - errorCount int64 + // fileWriteGap counts the lines lost to write errors since the last one that landed, + // fileWriteGapCause is the error that opened the gap, fileIncomplete stays true once + // any line of the run missed the file (see writeOnceLocked). Guarded by mu. + fileWriteGap int + fileWriteGapCause error + fileIncomplete bool + warningCount int64 + errorCount int64 // notifyCount tracks NOTIFY-ERR lines: notification/communication failures that // display as errors but are warning-weight for the run status (never escalate the // exit code / gauge to error). Kept separate from errorCount so a notify failure @@ -192,6 +198,7 @@ func (l *Logger) OpenLogFile(logPath string) error { l.logFile = file l.fileSinkDisabled = false + l.fileWriteGap, l.fileWriteGapCause, l.fileIncomplete = 0, nil, false // A fresh mount must start with a clean strike count: OpenLogFile is the only // re-enable path, so any stale count from a prior disable would otherwise let // a single new timeout re-latch the sink and defeat the N-strike protection. @@ -220,24 +227,71 @@ func (l *Logger) openFileBounded(logPath string) (*os.File, error) { // non-timeout outcome resets the streak. Caller MUST hold l.mu and have checked // logFile != nil && !fileSinkDisabled. func (l *Logger) writeFileLocked(line string) { + if l.fileWriteGap > 0 { + // The first line that lands after lost ones says how many are missing above it. + marker := fmt.Sprintf("[%s] %-8s log file writes failed (%v); %d lines missing from the on-disk log here\n", + time.Now().Format(l.timeFormat), types.LogLevelWarning.String(), l.fileWriteGapCause, l.fileWriteGap) + if !l.writeOnceLocked(marker) { + l.fileWriteGap++ // this line is lost too + return + } + l.fileWriteGap = 0 + } + if !l.writeOnceLocked(line) { + l.fileWriteGap++ + } +} + +// writeOnceLocked writes s to the log file and reports whether it landed. A timeout is a +// strike (see writeFileLocked) and is not a gap: the third one disables the sink with its +// own marker. Any other error (disk full, EIO, a read-only remount) was dropped without a +// word, so the run log - copied to the secondary and the cloud, attached to the email - +// lost its WARNING and ERROR lines and the exit code computed from it read clean. The +// first one of a series now says so on stdout, counted as a warning. +func (l *Logger) writeOnceLocked(s string) bool { f := l.logFile // local: a disable nils l.logFile, the abandoned goroutine keeps f + var err error if l.ioTimeout <= 0 { - _, _ = fileWrite(f, line) // opt-out / legacy: unbounded - return + _, err = fileWrite(f, s) // opt-out / legacy: unbounded + } else { + wt := min(l.ioTimeout, logWriteTimeoutCap) + _, err = safefs.Run(context.Background(), "logwrite", f.Name(), wt, func() (int, error) { + return fileWrite(f, s) + }) } - wt := min(l.ioTimeout, logWriteTimeoutCap) - _, err := safefs.Run(context.Background(), "logwrite", f.Name(), wt, func() (int, error) { - return fileWrite(f, line) - }) if err != nil && errors.Is(err, safefs.ErrTimeout) { l.consecutiveLogTimeouts++ + l.fileIncomplete = true if l.consecutiveLogTimeouts >= logSinkTimeoutStrikes { l.disableFileSinkLocked(err) } - return + return true // not a gap: the strikes and the disable marker account for it } // Any non-timeout outcome (a clean write or a non-timeout error) breaks the streak. l.consecutiveLogTimeouts = 0 + if err == nil { + return true + } + l.fileIncomplete = true + if l.fileWriteGap == 0 { + l.fileWriteGapCause = err + timestamp := time.Now().Format(l.timeFormat) + warn := fmt.Sprintf("log file write failed (%v); continuing on stdout, lines are missing from the on-disk log", err) + // stdout only - never route this back through the failing file sink. + _, _ = fmt.Fprintf(l.output, "[%s] %-8s %s\n", timestamp, types.LogLevelWarning.String(), warn) + l.warningCount++ + l.issueLines = append(l.issueLines, fmt.Sprintf("[%s] %-8s %s", timestamp, types.LogLevelWarning.String(), warn)) + } + return false +} + +// FileIncomplete reports whether lines of this run never reached the log file (a write +// error, a write timeout, or the sink disabled): the counts parsed back from that file +// then miss them. +func (l *Logger) FileIncomplete() bool { + l.mu.Lock() + defer l.mu.Unlock() + return l.fileIncomplete || l.fileSinkDisabled } // disableFileSinkLocked turns the file sink off after a timed-out write so every diff --git a/internal/orchestrator/extensions.go b/internal/orchestrator/extensions.go index bfeeb778..fca94525 100644 --- a/internal/orchestrator/extensions.go +++ b/internal/orchestrator/extensions.go @@ -336,6 +336,16 @@ func (o *Orchestrator) refreshLogIssuesFromFile(stats *BackupStats, includeCateg // No cap: the notifications list every problem; the chat webhooks keep their own // short list (webhook_payloads.go). categories, errorCount, warningCount, notifyCount := ParseLogCounts(stats.LogFilePath, 0) + // A file that lost lines (write errors, timeouts, a disabled sink) undercounts: the + // exit code and the healthchecks ping then read a clean run. Take the higher of the + // file's counts and the logger's own. + if o.logger != nil && o.logger.FileIncomplete() { + o.logger.Debug("Run log incomplete: counts file errors=%d warnings=%d notify=%d, logger errors=%d warnings=%d notify=%d", + errorCount, warningCount, notifyCount, o.logger.ErrorCount(), o.logger.WarningCount(), o.logger.NotifyCount()) + errorCount = max(errorCount, int(o.logger.ErrorCount())) + warningCount = max(warningCount, int(o.logger.WarningCount())) + notifyCount = max(notifyCount, int(o.logger.NotifyCount())) + } stats.ErrorCount = errorCount stats.WarningCount = warningCount stats.NotifyCount = notifyCount diff --git a/internal/orchestrator/log_incomplete_counts_test.go b/internal/orchestrator/log_incomplete_counts_test.go new file mode 100644 index 00000000..f3605dd8 --- /dev/null +++ b/internal/orchestrator/log_incomplete_counts_test.go @@ -0,0 +1,39 @@ +package orchestrator + +import ( + "bytes" + "os" + "path/filepath" + "testing" + + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// The exit code and the healthchecks ping come from the counts parsed back from the run +// log; when lines never reached it (here a full disk), the parse read a clean run while +// the logger had counted the issues. An incomplete file now yields the higher counts. +func TestRefreshLogIssuesUsesTheLoggerCountsWhenTheFileIsIncomplete(t *testing.T) { + if _, err := os.Stat("/dev/full"); err != nil { + t.Skip("/dev/full not available") + } + var out bytes.Buffer + logger := logging.New(types.LogLevelInfo, false) + logger.SetOutput(&out) + if err := logger.OpenLogFile("/dev/full"); err != nil { + t.Skipf("cannot open /dev/full as a log: %v", err) + } + logger.Warning("lost warning") + logger.Error("lost error") + + parsed := filepath.Join(t.TempDir(), "run.log") + if err := os.WriteFile(parsed, nil, 0o600); err != nil { + t.Fatal(err) + } + o := &Orchestrator{logger: logger} + stats := &BackupStats{LogFilePath: parsed} + o.refreshLogIssuesFromFile(stats, false) + if stats.ErrorCount < 1 || stats.WarningCount < 2 { + t.Fatalf("errors=%d warnings=%d, want at least the logger's 1 and 2 (lost warning + the write-failure WARNING)", stats.ErrorCount, stats.WarningCount) + } +} From 61425db5010599475fcc9d4329656652c34758fd Mon Sep 17 00:00:00 2001 From: tis24dev Date: Wed, 4 Nov 2026 09:07:49 +0100 Subject: [PATCH 32/44] fix(config): default CLOUD_LOG_PATH to a relative path, like the backups With the template defaults (CLOUD_REMOTE_PATH=/proxsave/backup, CLOUD_LOG_PATH=/proxsave/log) on an sftp or ftp remote the backups went to ~/proxsave/backup (the leading "/" of the backup path is dropped, decided 2026-10-04 so existing sftp backups do not move) and the logs to /proxsave/log at the server root; a non-root login got "Log not copied to cloud" (measured on pve-test with real rclone over sftp). On S3/Drive the two forms are the same. As approved, nothing moves for existing installs: the template (and so the install wizard's default) becomes CLOUD_LOG_PATH=proxsave/log, and the comment and docs say that a leading "/" is kept here: backup.env: # - With a leading /: /proxsave/log (on sftp, ftp and local remotes: an absolute path on the server, not under the home directory) CLOUD_STORAGE.md: Unlike `CLOUD_REMOTE_PATH`, a leading `/` is kept: on an `sftp` or `ftp` remote `/proxsave/log` is an absolute path on the server, not under the login user's home directory. The install characterization goldens change only by that comment and the prompt default; a test name that said the slash is "stripped" while asserting it is kept now says "kept". Audit finding storage-9 (audit-prod-go-2026-10-26). --- cmd/proxsave/install.go | 8 ++++--- .../install_characterization/edit_noop.env | 2 +- .../fresh_decline_all.env | 2 +- .../fresh_enable_all.env | 2 +- .../fresh_enable_all.transcript | 2 +- docs/CLOUD_STORAGE.md | 2 +- .../config/template_cloud_log_path_test.go | 24 +++++++++++++++++++ internal/config/templates/backup.env | 4 ++-- internal/orchestrator/helpers_test.go | 2 +- 9 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 internal/config/template_cloud_log_path_test.go diff --git a/cmd/proxsave/install.go b/cmd/proxsave/install.go index 7cb623c3..bd8c1560 100644 --- a/cmd/proxsave/install.go +++ b/cmd/proxsave/install.go @@ -628,9 +628,11 @@ func collectInstallWizardDataCLI(ctx context.Context, reader *bufio.Reader, prom // defence schedulerEngineDefault / healthcheckModeDefault / cronTimeDefault // already apply to their keys; this one was missed. // - // CLOUD_LOG_PATH is deliberately NOT cleared: "/proxsave/log" is a real path - // inside whatever remote is chosen, listed by the template's own comment as an - // accepted form, so it is a usable default rather than a stand-in. + // CLOUD_LOG_PATH is deliberately NOT cleared: the template's "proxsave/log" is a + // real path inside whatever remote is chosen, so it is a usable default rather + // than a stand-in. It has no leading "/": unlike CLOUD_REMOTE_PATH that one is + // kept, and on sftp/ftp it put the logs at the server root, away from the backups + // in the login home. prefill.CloudRemote = "" } diff --git a/cmd/proxsave/testdata/install_characterization/edit_noop.env b/cmd/proxsave/testdata/install_characterization/edit_noop.env index afb4be04..3ecab87b 100644 --- a/cmd/proxsave/testdata/install_characterization/edit_noop.env +++ b/cmd/proxsave/testdata/install_characterization/edit_noop.env @@ -173,7 +173,7 @@ CLOUD_REMOTE=myremote:pbs-backups # rclone remote NAME (from "rclone config") CLOUD_REMOTE_PATH=/proxsave/backup # Optional subdirectory within the remote # Cloud log storage path: # - Recommended (same remote): path-only (remote name is taken from CLOUD_REMOTE), no trailing slash: proxsave/log -# - New style (also ok): /proxsave/log +# - With a leading /: /proxsave/log (on sftp, ftp and local remotes: an absolute path on the server, not under the home directory) # - Legacy / different remote: explicit remote:path: myremote:proxsave/log (or myremote:/proxsave/log) CLOUD_LOG_PATH=myremote:/logs CLOUD_UPLOAD_MODE=parallel # sequential | parallel (parallel uses a worker pool for associated/bundle files) diff --git a/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env b/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env index 40977590..a0120ffc 100644 --- a/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env +++ b/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env @@ -173,7 +173,7 @@ CLOUD_REMOTE= # rclone remote NAME (from "rclone config") CLOUD_REMOTE_PATH=/proxsave/backup # Optional subdirectory within the remote # Cloud log storage path: # - Recommended (same remote): path-only (remote name is taken from CLOUD_REMOTE), no trailing slash: proxsave/log -# - New style (also ok): /proxsave/log +# - With a leading /: /proxsave/log (on sftp, ftp and local remotes: an absolute path on the server, not under the home directory) # - Legacy / different remote: explicit remote:path: myremote:proxsave/log (or myremote:/proxsave/log) CLOUD_LOG_PATH= CLOUD_UPLOAD_MODE=parallel # sequential | parallel (parallel uses a worker pool for associated/bundle files) diff --git a/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env b/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env index 358da7a1..c5aa15e8 100644 --- a/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env +++ b/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env @@ -173,7 +173,7 @@ CLOUD_REMOTE=myremote:pbs-backups # rclone remote NAME (from "rclone config") CLOUD_REMOTE_PATH=/proxsave/backup # Optional subdirectory within the remote # Cloud log storage path: # - Recommended (same remote): path-only (remote name is taken from CLOUD_REMOTE), no trailing slash: proxsave/log -# - New style (also ok): /proxsave/log +# - With a leading /: /proxsave/log (on sftp, ftp and local remotes: an absolute path on the server, not under the home directory) # - Legacy / different remote: explicit remote:path: myremote:proxsave/log (or myremote:/proxsave/log) CLOUD_LOG_PATH=myremote:/logs CLOUD_UPLOAD_MODE=parallel # sequential | parallel (parallel uses a worker pool for associated/bundle files) diff --git a/cmd/proxsave/testdata/install_characterization/fresh_enable_all.transcript b/cmd/proxsave/testdata/install_characterization/fresh_enable_all.transcript index bfe41386..03076bc9 100644 --- a/cmd/proxsave/testdata/install_characterization/fresh_enable_all.transcript +++ b/cmd/proxsave/testdata/install_characterization/fresh_enable_all.transcript @@ -8,7 +8,7 @@ For direct network access without mounting, use cloud storage (rclone) instead. Enable secondary backup path? [y/N]: Secondary backup path (SECONDARY_PATH): Secondary log path (SECONDARY_LOG_PATH): --- Cloud storage (rclone) --- Remember to configure rclone manually before enabling cloud backups. -Enable cloud backups? [y/N]: Rclone remote for backups (e.g. myremote:pbs-backups): Rclone remote for logs (e.g. myremote:/logs): [/proxsave/log] +Enable cloud backups? [y/N]: Rclone remote for backups (e.g. myremote:pbs-backups): Rclone remote for logs (e.g. myremote:/logs): [proxsave/log] --- Firewall rules --- Enable collection of firewall rules (e.g., iptables/nftables). (You can change this later in backup.env via BACKUP_FIREWALL_RULES) diff --git a/docs/CLOUD_STORAGE.md b/docs/CLOUD_STORAGE.md index d9f2584c..a820a362 100644 --- a/docs/CLOUD_STORAGE.md +++ b/docs/CLOUD_STORAGE.md @@ -854,7 +854,7 @@ ProxSave supports both "new style" (path-only) and "legacy style" (`remote:path` - `CLOUD_REMOTE_PATH` should be a **path inside the remote** (no remote prefix). Use **no trailing slash**. A leading `/` is accepted and dropped: the path is always relative to the remote's root, which for an `sftp` remote is the login user's home directory. For a folder on this host, set `CLOUD_REMOTE` to its absolute path instead (for example `CLOUD_REMOTE=/mnt/backup`). -- `CLOUD_LOG_PATH` should be a **folder path** for logs. When logs are stored on the **same remote**, prefer **path-only** here too (no remote prefix). Use `otherremote:/path` only if logs must go to a different remote than `CLOUD_REMOTE`. +- `CLOUD_LOG_PATH` should be a **folder path** for logs. When logs are stored on the **same remote**, prefer **path-only** here too (no remote prefix). Use `otherremote:/path` only if logs must go to a different remote than `CLOUD_REMOTE`. Unlike `CLOUD_REMOTE_PATH`, a leading `/` is kept: on an `sftp` or `ftp` remote `/proxsave/log` is an absolute path on the server, not under the login user's home directory. **Examples (same remote):** ```bash diff --git a/internal/config/template_cloud_log_path_test.go b/internal/config/template_cloud_log_path_test.go new file mode 100644 index 00000000..a1520933 --- /dev/null +++ b/internal/config/template_cloud_log_path_test.go @@ -0,0 +1,24 @@ +package config + +import ( + "strings" + "testing" +) + +// The template defaulted CLOUD_LOG_PATH to "/proxsave/log" next to +// CLOUD_REMOTE_PATH=/proxsave/backup. The backup path drops its leading "/" (decided +// 2026-10-04, so sftp backups stay in the login home); the log path keeps it, so on an +// sftp or ftp remote the logs went to the server root, away from the backups, and a +// non-root login could not write there. The default is now relative. +func TestTemplateCloudLogPathIsRelative(t *testing.T) { + for _, line := range strings.Split(DefaultEnvTemplate(), "\n") { + if value, ok := strings.CutPrefix(line, "CLOUD_LOG_PATH="); ok { + value = strings.TrimSpace(strings.SplitN(value, "#", 2)[0]) + if value != "proxsave/log" { + t.Fatalf("template CLOUD_LOG_PATH = %q, want proxsave/log", value) + } + return + } + } + t.Fatal("no CLOUD_LOG_PATH line in the template") +} diff --git a/internal/config/templates/backup.env b/internal/config/templates/backup.env index e2c03f0a..26d320f8 100644 --- a/internal/config/templates/backup.env +++ b/internal/config/templates/backup.env @@ -173,9 +173,9 @@ CLOUD_REMOTE=GoogleDrive # rclone remote NAME (from "rclone config") CLOUD_REMOTE_PATH=/proxsave/backup # Optional subdirectory within the remote # Cloud log storage path: # - Recommended (same remote): path-only (remote name is taken from CLOUD_REMOTE), no trailing slash: proxsave/log -# - New style (also ok): /proxsave/log +# - With a leading /: /proxsave/log (on sftp, ftp and local remotes: an absolute path on the server, not under the home directory) # - Legacy / different remote: explicit remote:path: myremote:proxsave/log (or myremote:/proxsave/log) -CLOUD_LOG_PATH=/proxsave/log +CLOUD_LOG_PATH=proxsave/log CLOUD_UPLOAD_MODE=parallel # sequential | parallel (parallel uses a worker pool for associated/bundle files) CLOUD_PARALLEL_MAX_JOBS=2 # Parallel workers when CLOUD_UPLOAD_MODE=parallel CLOUD_PARALLEL_VERIFICATION=true # true = also verify each associated file in parallel diff --git a/internal/orchestrator/helpers_test.go b/internal/orchestrator/helpers_test.go index 47e3796e..603faada 100644 --- a/internal/orchestrator/helpers_test.go +++ b/internal/orchestrator/helpers_test.go @@ -374,7 +374,7 @@ func TestResolveCloudPath(t *testing.T) { // New style WITH leading slash {"new style simple", "/logs", "gdrive", "gdrive:/logs"}, {"new style subpath", "/proxmox-backup/log", "gdrive", "gdrive:/proxmox-backup/log"}, - {"new style with leading slash stripped", "/logs", "gdrive", "gdrive:/logs"}, + {"new style with leading slash kept", "/logs", "gdrive", "gdrive:/logs"}, {"new style remote has legacy path", "/logs", "gdrive:base", "gdrive:/logs"}, // New style WITHOUT leading slash (rclone accepts both formats) From d16e280e96282b10c0367bba9c54f44721aa574f Mon Sep 17 00:00:00 2001 From: tis24dev Date: Wed, 4 Nov 2026 09:08:28 +0100 Subject: [PATCH 33/44] style(config): range over strings.SplitSeq in the template test Lint (modernize) on the test added by 61425db5. --- internal/config/template_cloud_log_path_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/config/template_cloud_log_path_test.go b/internal/config/template_cloud_log_path_test.go index a1520933..b982e97a 100644 --- a/internal/config/template_cloud_log_path_test.go +++ b/internal/config/template_cloud_log_path_test.go @@ -11,7 +11,7 @@ import ( // sftp or ftp remote the logs went to the server root, away from the backups, and a // non-root login could not write there. The default is now relative. func TestTemplateCloudLogPathIsRelative(t *testing.T) { - for _, line := range strings.Split(DefaultEnvTemplate(), "\n") { + for line := range strings.SplitSeq(DefaultEnvTemplate(), "\n") { if value, ok := strings.CutPrefix(line, "CLOUD_LOG_PATH="); ok { value = strings.TrimSpace(strings.SplitN(value, "#", 2)[0]) if value != "proxsave/log" { From c554c45d2d149c856e745341c0d553e5c92d8ce9 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Wed, 4 Nov 2026 18:03:33 +0100 Subject: [PATCH 34/44] fix(decrypt): honour --log-level like backup and restore --decrypt set its session logger from DEBUG_LEVEL only and never read --log-level: with --log-level debug its log had no DEBUG line (measured on pve-test; --restore wrote 154), and --log-level error still printed the INFO lines. The docs and --help give the flag no mode exception. Decrypt now resolves the level with resolveRunLogLevel, the backup and restore rule (approved): DEBUG_LEVEL is the base, --log-level overrides it. No new text; the decrypt log gains the DEBUG "main run: log_level=" those runs already write. The interactive-stdin check gets a test seam. Audit finding cmd-main-9 (audit-prod-go-2026-10-26). --- cmd/proxsave/decrypt_cmd.go | 13 +++++-- cmd/proxsave/decrypt_log_level_test.go | 50 ++++++++++++++++++++++++++ cmd/proxsave/main_modes.go | 2 +- 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 cmd/proxsave/decrypt_log_level_test.go diff --git a/cmd/proxsave/decrypt_cmd.go b/cmd/proxsave/decrypt_cmd.go index 38caf3cd..51256069 100644 --- a/cmd/proxsave/decrypt_cmd.go +++ b/cmd/proxsave/decrypt_cmd.go @@ -6,13 +6,18 @@ import ( "os" "strings" + "github.com/tis24dev/proxsave/internal/cli" "github.com/tis24dev/proxsave/internal/config" "github.com/tis24dev/proxsave/internal/logging" "github.com/tis24dev/proxsave/internal/orchestrator" ) // runDecryptWorkflowOnly executes the decrypt workflow without initializing the backup orchestrator. -func runDecryptWorkflowOnly(ctx context.Context, configPath string, bootstrap *logging.BootstrapLogger, version string, useCLI bool) error { +// decryptStdinCheck is ensureInteractiveStdin, a seam for tests. +var decryptStdinCheck = ensureInteractiveStdin + +func runDecryptWorkflowOnly(ctx context.Context, args *cli.Args, bootstrap *logging.BootstrapLogger, version string, useCLI bool) error { + configPath := args.ConfigPath // Print version/banner header (mirrored later into the decrypt log via bootstrap.Flush). bootstrap.Println("===========================================") bootstrap.Println(" ProxSave") @@ -27,7 +32,7 @@ func runDecryptWorkflowOnly(ctx context.Context, configPath string, bootstrap *l return err } - if err := ensureInteractiveStdin(); err != nil { + if err := decryptStdinCheck(); err != nil { return err } @@ -39,7 +44,9 @@ func runDecryptWorkflowOnly(ctx context.Context, configPath string, bootstrap *l _ = os.Setenv("BASE_DIR", cfg.BaseDir) - logLevel := cfg.DebugLevel + // The backup and restore rule: DEBUG_LEVEL is the base and --log-level overrides it. + // Decrypt read DEBUG_LEVEL only, so --log-level was ignored here alone. + logLevel := resolveRunLogLevel(args, cfg, bootstrap) logger, sessionLogPath, closeSessionLog, err := logging.StartSessionLogger("decrypt", logLevel, cfg.UseColor) if err != nil { logger = logging.New(logLevel, cfg.UseColor) diff --git a/cmd/proxsave/decrypt_log_level_test.go b/cmd/proxsave/decrypt_log_level_test.go new file mode 100644 index 00000000..b9ae753e --- /dev/null +++ b/cmd/proxsave/decrypt_log_level_test.go @@ -0,0 +1,50 @@ +package main + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/cli" + "github.com/tis24dev/proxsave/internal/config" + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// --decrypt read DEBUG_LEVEL only, so --log-level was ignored there alone (measured on +// pve-test: no DEBUG line with --log-level debug, 154 for --restore). It now follows +// the backup and restore rule: DEBUG_LEVEL as the base, --log-level overriding it. +func TestDecryptHonoursLogLevel(t *testing.T) { + base := t.TempDir() + cfgPath := filepath.Join(base, "configs", "backup.env") + if err := os.MkdirAll(filepath.Dir(cfgPath), 0o755); err != nil { + t.Fatal(err) + } + env := strings.Replace(config.DefaultEnvTemplate(), "DEBUG_LEVEL=", "DEBUG_LEVEL=standard #", 1) + if err := os.WriteFile(cfgPath, []byte(env), 0o600); err != nil { + t.Fatal(err) + } + stdinR, stdinW, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + _ = stdinW.Close() + prevStdin := os.Stdin + os.Stdin = stdinR + prevLogger := logging.GetDefaultLogger() + t.Cleanup(func() { os.Stdin = prevStdin; _ = stdinR.Close(); logging.SetDefaultLogger(prevLogger) }) + + prevCheck := decryptStdinCheck + decryptStdinCheck = func() error { return nil } + t.Cleanup(func() { decryptStdinCheck = prevCheck }) + + bootstrap := logging.NewBootstrapLogger() + bootstrap.SetConsoleQuiet(true) + runErr := runDecryptWorkflowOnly(context.Background(), &cli.Args{ConfigPath: cfgPath, LogLevel: types.LogLevelDebug}, bootstrap, "test", true) + t.Logf("decrypt returned %v", runErr) + if got := logging.GetDefaultLogger().GetLevel(); got != types.LogLevelDebug { + t.Fatalf("decrypt logger level = %v, want DEBUG from --log-level", got) + } +} diff --git a/cmd/proxsave/main_modes.go b/cmd/proxsave/main_modes.go index 97e8f10b..48253232 100644 --- a/cmd/proxsave/main_modes.go +++ b/cmd/proxsave/main_modes.go @@ -425,7 +425,7 @@ func runDecryptOnlyMode(ctx context.Context, args *cli.Args, bootstrap *logging. } decryptCLI := modeUseCLI(args) logging.DebugStepBootstrap(bootstrap, "main run", "mode=decrypt cli=%v", decryptCLI) - if err := runDecryptWorkflowOnly(ctx, args.ConfigPath, bootstrap, toolVersion, decryptCLI); err != nil { + if err := runDecryptWorkflowOnly(ctx, args, bootstrap, toolVersion, decryptCLI); err != nil { if errors.Is(err, orchestrator.ErrDecryptAborted) { bootstrap.Info("Decrypt workflow aborted by user") return types.ExitSuccess.Int(), true From 66efca850922cc0c77042fc8f90806694a32168d Mon Sep 17 00:00:00 2001 From: tis24dev Date: Wed, 4 Nov 2026 18:06:21 +0100 Subject: [PATCH 35/44] fix(config): stop reading the Bash-era DISABLE_COLORS The loader still read DISABLE_COLORS and let it override USE_COLOR, from the file and the environment, while the integrity check reported "DISABLE_COLORS is not a known variable and is ignored": an operator who deleted the line as told got the colors back. 935fcc51 removed the legacy names from the loader and missed this one; no Go writer ever wrote it, so it comes only from a copied Bash env. As approved, it is no longer read: the integrity line becomes true and USE_COLOR decides alone. Three comments that named it as a live knob now name USE_COLOR. Audit finding ui-config-3 (audit-prod-go-2026-10-26). --- internal/config/config.go | 9 ++++----- internal/config/disable_colors_test.go | 24 ++++++++++++++++++++++++ internal/logging/bootstrap.go | 2 +- internal/ui/shell/router.go | 2 +- internal/ui/shell/session.go | 2 +- 5 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 internal/config/disable_colors_test.go diff --git a/internal/config/config.go b/internal/config/config.go index fe38f5b0..fc1c0dcb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -574,11 +574,10 @@ func (c *Config) parseGeneralSettings() { c.ProfilingEnabled = c.getBool("PROFILING_ENABLED", true) c.DebugLevel = c.getLogLevel("DEBUG_LEVEL", types.LogLevelInfo) - if disableColors, ok := c.raw["DISABLE_COLORS"]; ok { - c.UseColor = !utils.ParseBool(disableColors) - } else { - c.UseColor = c.getBool("USE_COLOR", true) - } + // DISABLE_COLORS, the Bash-era name, is no longer read: 935fcc51 dropped the legacy + // names from the loader and missed this one, so the integrity check called it unknown + // and ignored while it still overrode USE_COLOR, from the file and the environment. + c.UseColor = c.getBool("USE_COLOR", true) c.ColorizeStepLogs = c.getBool("COLORIZE_STEP_LOGS", true) && c.UseColor } diff --git a/internal/config/disable_colors_test.go b/internal/config/disable_colors_test.go new file mode 100644 index 00000000..b49d1946 --- /dev/null +++ b/internal/config/disable_colors_test.go @@ -0,0 +1,24 @@ +package config + +import ( + "os" + "path/filepath" + "testing" +) + +// The loader still read DISABLE_COLORS, a Bash-era name, and let it override +// USE_COLOR, while the integrity check reported it unknown and ignored: deleting the +// line as told brought the colors back. It is no longer read (approved). +func TestDisableColorsNoLongerOverridesUseColor(t *testing.T) { + envPath := filepath.Join(t.TempDir(), "backup.env") + if err := os.WriteFile(envPath, []byte("USE_COLOR=true\nDISABLE_COLORS=true\n"), 0o600); err != nil { + t.Fatal(err) + } + cfg, err := LoadConfigWithBaseDir(envPath, t.TempDir()) + if err != nil { + t.Fatalf("LoadConfigWithBaseDir: %v", err) + } + if !cfg.UseColor { + t.Fatalf("UseColor = false: DISABLE_COLORS still overrides USE_COLOR=true") + } +} diff --git a/internal/logging/bootstrap.go b/internal/logging/bootstrap.go index 6853eb92..cd98a9cb 100644 --- a/internal/logging/bootstrap.go +++ b/internal/logging/bootstrap.go @@ -17,7 +17,7 @@ const bootstrapTimeFormat = "2006-01-02 15:04:05" // consoleUseColor decides whether the bootstrap console prefix carries ANSI // color for the given target stream. The main Logger's color is config-driven -// (USE_COLOR/DISABLE_COLORS), but the bootstrap runs before config is loaded, +// (USE_COLOR), but the bootstrap runs before config is loaded, // so it falls back to the conventional rule: color only on a real terminal and // only when NO_COLOR is unset. func consoleUseColor(f *os.File) bool { diff --git a/internal/ui/shell/router.go b/internal/ui/shell/router.go index 42ace386..0028c412 100644 --- a/internal/ui/shell/router.go +++ b/internal/ui/shell/router.go @@ -127,7 +127,7 @@ func (m rootModel) View() tea.View { v.MouseMode = tea.MouseModeCellMotion if m.cfg.UseColor { // Only repaint the terminal background when color is enabled: - // with DISABLE_COLORS the OSC 11 repaint would contradict the + // with USE_COLOR=false the OSC 11 repaint would contradict the // monochrome contract. v.BackgroundColor = theme.Background } diff --git a/internal/ui/shell/session.go b/internal/ui/shell/session.go index 2bf069fc..8625a303 100644 --- a/internal/ui/shell/session.go +++ b/internal/ui/shell/session.go @@ -44,7 +44,7 @@ type Config struct { // same data the tview ScreenSpec carried. Optional. ConfigPath string BuildSig string - // UseColor mirrors the USE_COLOR/DISABLE_COLORS config knobs. When + // UseColor mirrors the USE_COLOR config knob. When // false the program renders monochrome (layout preserved). UseColor bool From c5b363f32f527ea2a929ca37622f2b20b3bc4b90 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Wed, 4 Nov 2026 18:06:37 +0100 Subject: [PATCH 36/44] docs(daemon): say a personal script sees its name as /proc/self/fd/3 Personal scripts are executed from a pinned descriptor, /proc/self/fd/3, so a swapped path cannot change what runs (f882c8f8). For an interpreted script that also sets $0, BASH_SOURCE, Python's __file__ and Perl's $FindBin::Bin to /proc/self/fd/3: dirname "$0" finds nothing next to the script, silently. The docs tell the operator to give the script a shebang and never said so. Behaviour unchanged (approved, docs only). The approved sentence, in DAEMON.md after "give it a shebang" and in CONFIGURATION.md's personal-scripts paragraph: An interpreted script sees its own name as `/proc/self/fd/3`, not as the configured path: `$0`, `BASH_SOURCE`, Python's `__file__` and Perl's `$FindBin::Bin` all carry it, so `dirname "$0"` points at `/proc/self/fd`. To find files next to the script, resolve it first: `readlink -f "$0"`, `os.path.realpath(__file__)` or `$FindBin::RealBin`. Audit finding cmd-main-4 (audit-prod-go-2026-10-26). --- docs/CONFIGURATION.md | 2 +- docs/DAEMON.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index baa901c5..42c71acc 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -317,7 +317,7 @@ own environment, and a name neither defines is replaced by nothing, silently. `$ `$`. `$@`, `$*`, `$!`, `$?`, `$-`, `$0` to `$9` and a `${` with no closing brace are consumed the same way as an unknown name. Only a `$` at the end of the value, or one before a character that is neither a letter, a digit, an underscore nor one of those, survives. Write `$$` where the path -really contains a `$`. +really contains a `$`. An interpreted script sees its own name as `/proc/self/fd/3`, not as the configured path: `$0`, `BASH_SOURCE`, Python's `__file__` and Perl's `$FindBin::Bin` all carry it, so `dirname "$0"` points at `/proc/self/fd`. To find files next to the script, resolve it first: `readlink -f "$0"`, `os.path.realpath(__file__)` or `$FindBin::RealBin`. These scripts run only under the daemon, never under a manual `proxsave --backup` or a cron-mode run. Their output is discarded and their exit status does not change the diff --git a/docs/DAEMON.md b/docs/DAEMON.md index 7a2d66da..9a6d78d5 100644 --- a/docs/DAEMON.md +++ b/docs/DAEMON.md @@ -444,7 +444,7 @@ not the backup's, so no run log, recap, notification, healthchecks ping or metri log ProxSave is writing at that moment, the second the installation, and between them they are the only way a script could learn about the run or write into ProxSave's log. Nothing is added, so there is no run id either. The script runs as the daemon does, as `root`, in the - daemon's working directory. Make it executable and give it a shebang. + daemon's working directory. Make it executable and give it a shebang. An interpreted script sees its own name as `/proc/self/fd/3`, not as the configured path: `$0`, `BASH_SOURCE`, Python's `__file__` and Perl's `$FindBin::Bin` all carry it, so `dirname "$0"` points at `/proc/self/fd`. To find files next to the script, resolve it first: `readlink -f "$0"`, `os.path.realpath(__file__)` or `$FindBin::RealBin`. - **A stop does not queue behind either script.** A `systemctl stop` or `systemctl restart` landing while the run is in flight still starts `PERSONAL_SCRIPT_POST_RUN`, but does not wait for it: the daemon's whole teardown budget is the unit's stop timeout, 90 seconds on a stock From 9b777e06bf77cacc9a0093832facb263f0b837e1 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Wed, 4 Nov 2026 19:51:39 +0100 Subject: [PATCH 37/44] fix(storage): classify CephFS as a network filesystem the ownership probe decides The filesystem classifier had no case for CephFS: the kernel client ("ceph") fell to unknown and ceph-fuse ("fuse.ceph-fuse") to the FUSE rule, both without ownership. On a common PVE destination SET_BACKUP_PERMISSIONS was never applied, the backups stayed root-only, and the security preflight skipped the owner and mode checks of those paths. CephFS takes POSIX ownership unless the client cap carries root_squash. Both clients are now "ceph", a network filesystem, so the existing ownership write probe decides (approved, root_squash included). The lines read "Filesystem: ceph (supports ownership) [network] [mount: ...]" or "ceph (no ownership)" under root_squash; with SET_BACKUP_PERMISSIONS=false the preflight now enforces its checks on CephFS directories as on ext4 (approved side effect). Approved docs line: "POSIX-capable NFS and CephFS mounts requiring specific ownership". pve-test has no Ceph cluster; the classifier and the probe path are tested with the detector's seams. Audit finding storage-5 (audit-prod-go-2026-10-26). --- docs/CONFIGURATION.md | 2 +- internal/storage/filesystem.go | 7 +++++ internal/storage/filesystem_ceph_test.go | 36 ++++++++++++++++++++++++ internal/storage/storage.go | 7 +++-- 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 internal/storage/filesystem_ceph_test.go diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 42c71acc..bc010388 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -597,7 +597,7 @@ When `SET_BACKUP_PERMISSIONS=true`, the system applies Bash-compatible ownership - Migration from legacy Bash version - Multi-user environments requiring specific ownership - Shared backup storage with group access -- POSIX-capable NFS mounts requiring specific ownership +- POSIX-capable NFS and CephFS mounts requiring specific ownership **Example**: ```bash diff --git a/internal/storage/filesystem.go b/internal/storage/filesystem.go index 82cda4e7..90c9dd85 100644 --- a/internal/storage/filesystem.go +++ b/internal/storage/filesystem.go @@ -393,6 +393,13 @@ func (d *FilesystemDetector) testOwnershipSupport(ctx context.Context, path stri // parseFilesystemType converts a filesystem type string to FilesystemType func parseFilesystemType(fsTypeStr string) FilesystemType { fsTypeStr = strings.ToLower(fsTypeStr) + // CephFS had no case: the kernel client ("ceph") fell to unknown and ceph-fuse to + // the FUSE rule, both "no ownership", so backup permissions were never applied on a + // common PVE destination that takes POSIX ownership. + switch fsTypeStr { + case "ceph", "fuse.ceph-fuse", "fuse.ceph": + return FilesystemCeph + } if strings.HasPrefix(fsTypeStr, "fuse.") { return FilesystemFUSE } diff --git a/internal/storage/filesystem_ceph_test.go b/internal/storage/filesystem_ceph_test.go new file mode 100644 index 00000000..01299a68 --- /dev/null +++ b/internal/storage/filesystem_ceph_test.go @@ -0,0 +1,36 @@ +package storage + +import ( + "context" + "testing" +) + +// CephFS had no case: the kernel client fell to unknown and ceph-fuse to FUSE, both +// without ownership, so SET_BACKUP_PERMISSIONS was never applied there. Both are now +// "ceph", a network filesystem whose ownership the write probe decides (root_squash +// is a client cap away). +func TestCephFSIsANetworkFilesystemTheProbeDecides(t *testing.T) { + for _, s := range []string{"ceph", "fuse.ceph-fuse", "fuse.ceph"} { + if got := parseFilesystemType(s); got != FilesystemCeph { + t.Fatalf("parseFilesystemType(%q) = %q, want ceph", s, got) + } + } + if !FilesystemCeph.IsNetworkFilesystem() || FilesystemCeph.ShouldAutoExclude() { + t.Fatalf("ceph: network=%v autoExclude=%v, want true false", FilesystemCeph.IsNetworkFilesystem(), FilesystemCeph.ShouldAutoExclude()) + } + for _, probe := range []bool{true, false} { + d := NewFilesystemDetector(newTestLogger()) + d.mountPointLookup = func(string) (string, error) { return "/mnt/pve/cephfs", nil } + d.filesystemTypeLookup = func(context.Context, string) (FilesystemType, string, error) { + return FilesystemCeph, "10.0.0.1:/", nil + } + d.ownershipSupportTest = func(context.Context, string) bool { return probe } + info, err := d.DetectFilesystem(context.Background(), t.TempDir()) + if err != nil { + t.Fatal(err) + } + if info.SupportsOwnership != probe { + t.Fatalf("probe %v: SupportsOwnership = %v", probe, info.SupportsOwnership) + } + } +} diff --git a/internal/storage/storage.go b/internal/storage/storage.go index b053eb76..351275ad 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -35,6 +35,9 @@ const ( // Network filesystems (need testing) FilesystemNFS FilesystemType = "nfs" FilesystemNFS4 FilesystemType = "nfs4" + // FilesystemCeph is CephFS, kernel client or ceph-fuse: POSIX ownership unless the + // client cap carries root_squash, so the network probe decides. + FilesystemCeph FilesystemType = "ceph" FilesystemCIFS FilesystemType = "cifs" FilesystemSMB FilesystemType = "smb" @@ -320,7 +323,7 @@ func (f FilesystemType) SupportsUnixOwnership() bool { case FilesystemFAT32, FilesystemFAT, FilesystemExFAT, FilesystemNTFS, FilesystemFUSE: return false - case FilesystemNFS, FilesystemNFS4, FilesystemCIFS, FilesystemSMB: + case FilesystemNFS, FilesystemNFS4, FilesystemCIFS, FilesystemSMB, FilesystemCeph: // Network filesystems need runtime testing return false default: @@ -331,7 +334,7 @@ func (f FilesystemType) SupportsUnixOwnership() bool { // IsNetworkFilesystem returns true if the filesystem is network-based func (f FilesystemType) IsNetworkFilesystem() bool { switch f { - case FilesystemNFS, FilesystemNFS4, FilesystemCIFS, FilesystemSMB: + case FilesystemNFS, FilesystemNFS4, FilesystemCIFS, FilesystemSMB, FilesystemCeph: return true default: return false From 0f32add2ec13034b514e940b13c897d8fd05e346 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Wed, 4 Nov 2026 19:57:31 +0100 Subject: [PATCH 38/44] fix(backup): skip dedup when the run's exclusions would drop its manifest Deduplication replaces duplicate files with symlinks and records them in var/lib/proxsave-info/dedup_manifest.json, the only input that turns them back into files on restore. The archiver applies the run's exclusion patterns afterwards and drops a match silently: with an operator pattern such as *.json, *manifest* or /var/lib/** the archive shipped the symlinks without the manifest, and a restore brought the duplicates back as links (dangling on a selective restore) with no error. The template and docs patterns do not match. Dedup now checks the manifest path and each directory above it against the run's exclusions first (as writeBackupMetadata does for its file) and, on a match, is skipped for that run. Approved INFO line: Deduplication skipped: the dedup manifest var/lib/proxsave-info/dedup_manifest.json matches exclusion pattern "Deduplication completed" is then not printed. Audit finding backup-7 (audit-prod-go-2026-10-26). --- .../backup/dedup_manifest_excluded_test.go | 58 +++++++++++++++++++ internal/backup/optimizations.go | 25 ++++++++ internal/orchestrator/backup_run_helpers.go | 6 +- internal/orchestrator/backup_run_phases.go | 2 +- 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 internal/backup/dedup_manifest_excluded_test.go diff --git a/internal/backup/dedup_manifest_excluded_test.go b/internal/backup/dedup_manifest_excluded_test.go new file mode 100644 index 00000000..4f866eef --- /dev/null +++ b/internal/backup/dedup_manifest_excluded_test.go @@ -0,0 +1,58 @@ +package backup + +import ( + "bytes" + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/types" +) + +// An operator exclusion that matched the dedup manifest (*.json, *manifest*, +// /var/lib/**) dropped it from the archive while the dedup symlinks shipped: on restore +// the duplicates came back as links, or dangling ones, with no error. Dedup is now +// skipped for such a run, with an INFO line naming the pattern. +func TestDedupIsSkippedWhenTheManifestWouldBeExcluded(t *testing.T) { + for _, tc := range []struct { + patterns []string + wantSymlink bool + }{ + {nil, true}, + {[]string{"*.json"}, false}, + {[]string{"/var/lib/**"}, false}, + } { + root := t.TempDir() + for _, user := range []string{"alice", "bob"} { + dir := filepath.Join(root, "home", user) + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, ".profile"), []byte("same content for both users\n"), 0o644); err != nil { + t.Fatal(err) + } + } + var out bytes.Buffer + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&out) + cfg := OptimizationConfig{EnableDeduplication: true, ExcludePatterns: tc.patterns} + if _, err := ApplyOptimizations(context.Background(), logger, root, cfg); err != nil { + t.Fatalf("%v: ApplyOptimizations: %v", tc.patterns, err) + } + links := 0 + for _, user := range []string{"alice", "bob"} { + if fi, err := os.Lstat(filepath.Join(root, "home", user, ".profile")); err == nil && fi.Mode()&os.ModeSymlink != 0 { + links++ + } + } + if (links > 0) != tc.wantSymlink { + t.Fatalf("%v: dedup symlinks=%d, want symlink=%v; log:\n%s", tc.patterns, links, tc.wantSymlink, out.String()) + } + if !tc.wantSymlink && !strings.Contains(out.String(), "Deduplication skipped: the dedup manifest var/lib/proxsave-info/dedup_manifest.json matches exclusion pattern "+tc.patterns[0]) { + t.Fatalf("%v: no skip line; log:\n%s", tc.patterns, out.String()) + } + } +} diff --git a/internal/backup/optimizations.go b/internal/backup/optimizations.go index 3d2ec217..f4f9ac27 100644 --- a/internal/backup/optimizations.go +++ b/internal/backup/optimizations.go @@ -45,6 +45,25 @@ type OptimizationConfig struct { EnableDeduplication bool EnablePrefilter bool PrefilterMaxFileSizeBytes int64 + // ExcludePatterns are the run's archive exclusions (see dedupManifestExcluded). + ExcludePatterns []string +} + +// dedupManifestExcluded returns the exclusion pattern that would keep the dedup +// manifest, or a directory above it, out of the archive, "" when none does. The +// archiver drops a matching path silently, so an operator pattern such as *.json or +// /var/lib/** shipped the dedup symlinks without the manifest that turns them back +// into files on restore: the duplicates came back as links, or dangling ones. +func dedupManifestExcluded(root string, patterns []string) string { + if len(patterns) == 0 { + return "" + } + for rel := DedupManifestRelPath; rel != "." && rel != "/"; rel = filepath.Dir(rel) { + if excluded, pattern := FindExcludeMatch(patterns, filepath.Join(root, rel), root, ""); excluded { + return pattern + } + } + return "" } // Enabled returns true if at least one optimization is active. @@ -73,6 +92,12 @@ func ApplyOptimizations(ctx context.Context, logger *logging.Logger, root string logger.Info("Running backup optimizations (dedup=%v prefilter=%v)", cfg.EnableDeduplication, cfg.EnablePrefilter) + if cfg.EnableDeduplication { + if pattern := dedupManifestExcluded(root, cfg.ExcludePatterns); pattern != "" { + logger.Info("Deduplication skipped: the dedup manifest %s matches exclusion pattern %s", DedupManifestRelPath, pattern) + cfg.EnableDeduplication = false + } + } if cfg.EnableDeduplication { logger.Debug("Starting deduplication stage") dups, reclaimed, err := deduplicateFiles(ctx, logger, root) diff --git a/internal/orchestrator/backup_run_helpers.go b/internal/orchestrator/backup_run_helpers.go index 05fe06c5..14130528 100644 --- a/internal/orchestrator/backup_run_helpers.go +++ b/internal/orchestrator/backup_run_helpers.go @@ -163,7 +163,7 @@ func (o *Orchestrator) logBackupCollectionSummary(collStats *backup.CollectionSt collStats.DirsCreated) } -func (o *Orchestrator) applyBackupOptimizations(ctx context.Context, tempDir string) (backup.OptimizationResult, error) { +func (o *Orchestrator) applyBackupOptimizations(ctx context.Context, tempDir string, excludePatterns []string) (backup.OptimizationResult, error) { if !o.optimizationCfg.Enabled() { o.logger.Debug("Skipping optimization step (all features disabled)") return backup.OptimizationResult{}, nil @@ -171,7 +171,9 @@ func (o *Orchestrator) applyBackupOptimizations(ctx context.Context, tempDir str fmt.Println() o.logger.Step("Backup optimizations on collected data") - res, err := backup.ApplyOptimizations(ctx, o.logger, tempDir, o.optimizationCfg) + optCfg := o.optimizationCfg + optCfg.ExcludePatterns = excludePatterns + res, err := backup.ApplyOptimizations(ctx, o.logger, tempDir, optCfg) if err != nil { // ApplyOptimizations only returns an error for an unsafe deduplication state // (a tree that would lose fidelity on restore); abort rather than ship it. diff --git a/internal/orchestrator/backup_run_phases.go b/internal/orchestrator/backup_run_phases.go index 99993a68..6603ed17 100644 --- a/internal/orchestrator/backup_run_phases.go +++ b/internal/orchestrator/backup_run_phases.go @@ -300,7 +300,7 @@ func (o *Orchestrator) collectBackupData(run *backupRunContext, workspace *backu return err } - optResult, err := o.applyBackupOptimizations(run.ctx, workspace.tempDir) + optResult, err := o.applyBackupOptimizations(run.ctx, workspace.tempDir, run.collectorConfig.ExcludePatterns) if err != nil { return err } From c95ac3f40375f48ef08b0097a3424e05fe457969 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Wed, 4 Nov 2026 22:38:20 +0100 Subject: [PATCH 39/44] chore(whatsnew): add the 0.46.1 entry No new features since v0.46.0: the single generic fixes line. --- internal/whatsnew/registry.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/whatsnew/registry.go b/internal/whatsnew/registry.go index 41ff34dd..1288bd71 100644 --- a/internal/whatsnew/registry.go +++ b/internal/whatsnew/registry.go @@ -307,6 +307,12 @@ var notes = []Note{ "This release includes a collection of fixes and corrections.", }, }, + { + Version: "0.46.1", + Lines: []string{ + "This release includes a collection of fixes and corrections.", + }, + }, } // LookupNotes returns the notes for versions in the half-open range (from, to], ascending by From 30826e83ecdcd6cff8025c5a7b8c92461bd7e518 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Fri, 13 Nov 2026 13:40:42 +0100 Subject: [PATCH 40/44] feat(healthchecks): send an initial email for the monitoring portal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue 347: in centralized mode the portal account of a host has an address that receives no mail, so the portal's confirmation code never arrives and the operator can never set a password or change the email. The relay can now mail a confirmation link to a real address and, on the click, move the host's project into the account of that address (relay commits b9e8739 and 5878b10, deployed on 2026-11-13). HEALTHCHECK_ACCOUNT_INITIAL_EMAIL (backup.env, empty by default: nothing changes while it is empty, also after an upgrade) carries that address. The daemon sends it in the X-Proxsave-Account-Initial-Email header of the config poll at start and of the poll before each scheduled run, never in the query (the relay's access log records it) and never in any log. The schedule negotiation's polls do not carry it. The variable is read again before each run, so a hand edit reaches the relay without a restart; when the notify policy is already confirmed, that poll still goes out for the address, without the notify level block. The relay answers account_initial_email {state, reason}. Applied or rejected is final for that address: the daemon stops sending it (in memory: after a restart it is sent once more and the relay answers the same without asking the hc host). A different address is sent again. The journal block, texts approved by the maintainer: Applying monitoring portal email... then " Waiting for the confirmation link sent by mail" or " Daily mail limit reached, retried at the next run" and "⚠ Monitoring portal email: not applied" (INFO); "✓ Monitoring portal email: applied"; or, rejected, the reason (" Not a usable email address", " An address of the ProxSave HC Server", " 3 confirmation mails expired; set another address") and "⚠ Monitoring portal email: not applied" at WARNING. An older relay that does not answer about it gets no visible line. For a project the operator owns, the relay sends the portal's sign-in page instead of a magic link, with login_kind "portal". The install, --install --cli and dashboard Healthchecks screens then say "Your monitoring portal:" and "Sign in with your email: use "Email Me a Link" on the sign-in page.", or "Sign in with your email and the password you set." Every other case renders as before: two new tests pin today's box and CLI block. whatsnew: new 0.47.0 entry. Docs: CONFIGURATION.md and HEALTHCHECKS.md (the portal section no longer tells a host without an email to set a password from the link). The install characterization goldens and the template variable count change by the one new template line. GitNexus: buildHealthcheckPrompt and ClassifyHealthcheckSetupResult HIGH risk (install, install --cli, dashboard), told to the maintainer before the edit. Tests: go test ./... passes with umask 022; with this shell's umask 0002 four personal-script tests fail, on dev as well. --- cmd/proxsave/config_integrity_test.go | 2 +- cmd/proxsave/daemon.go | 24 +- cmd/proxsave/daemon_initial_email.go | 104 ++++++ cmd/proxsave/daemon_initial_email_test.go | 306 ++++++++++++++++++ cmd/proxsave/daemon_notify_policy.go | 30 +- cmd/proxsave/healthcheck_portal_cli_test.go | 45 +++ cmd/proxsave/healthcheck_setup_cli.go | 8 + .../install_characterization/edit_noop.env | 1 + .../fresh_decline_all.env | 1 + .../fresh_enable_all.env | 1 + docs/CONFIGURATION.md | 1 + docs/HEALTHCHECKS.md | 14 +- internal/config/config.go | 25 +- internal/config/scheduler_healthcheck_test.go | 2 +- internal/config/templates/backup.env | 1 + internal/health/config.go | 50 ++- internal/health/config_checks_test.go | 4 +- internal/health/delivery_test.go | 4 +- internal/health/schedule.go | 8 +- internal/health/schedule_test.go | 46 ++- .../orchestrator/healthcheck_setup_check.go | 6 +- .../healthcheck_setup_classify.go | 23 +- .../orchestrator/healthcheck_setup_test.go | 23 ++ internal/serverbot/client.go | 5 + internal/serverbot/client_test.go | 27 ++ internal/serverbot/request.go | 22 +- internal/ui/flows/install/healthcheck.go | 27 +- .../flows/install/healthcheck_notify_test.go | 4 +- .../install/healthcheck_portal_copy_test.go | 58 ++++ .../flows/install/healthcheck_sensors_test.go | 6 +- internal/ui/flows/install/healthcheck_test.go | 10 +- internal/whatsnew/registry.go | 6 + 32 files changed, 823 insertions(+), 71 deletions(-) create mode 100644 cmd/proxsave/daemon_initial_email.go create mode 100644 cmd/proxsave/daemon_initial_email_test.go create mode 100644 cmd/proxsave/healthcheck_portal_cli_test.go create mode 100644 internal/ui/flows/install/healthcheck_portal_copy_test.go diff --git a/cmd/proxsave/config_integrity_test.go b/cmd/proxsave/config_integrity_test.go index d667a9f3..86cea352 100644 --- a/cmd/proxsave/config_integrity_test.go +++ b/cmd/proxsave/config_integrity_test.go @@ -264,7 +264,7 @@ func TestTheDebugBlockExplainsEveryDecisionItMade(t *testing.T) { // which write form won, since the two resolve in opposite ways fmt.Sprintf("CUSTOM_BACKUP_PATHS assigned on lines %d, %d; line %d wins (block form: it replaces everything before it)", customPathLines[0], customPathLines[1], customPathLines[1]), // the template's own two halves, so the counts above can be reconstructed - "template assigns 191 variables and documents", + "template assigns 192 variables and documents", } { if !strings.Contains(logged, want) { t.Fatalf("missing %q in:\n%s", want, logged) diff --git a/cmd/proxsave/daemon.go b/cmd/proxsave/daemon.go index bb7a77b9..64ad3c29 100644 --- a/cmd/proxsave/daemon.go +++ b/cmd/proxsave/daemon.go @@ -145,6 +145,11 @@ type daemon struct { notifyWant *notifyPolicy notifyApplied *notifyPolicy notifyPolls uint64 // config polls that reached the relay; guarded by mu + // initialEmailWant is HEALTHCHECK_ACCOUNT_INITIAL_EMAIL as last read by refreshNotifyPolicy + // (nil: d.cfg's). initialEmailDone is the address the relay gave a final answer for + // (applied or rejected): it is not sent again while the variable holds it. Both guarded by mu. + initialEmailWant *string + initialEmailDone string // pollMu makes the daemon send one /api/healthcheck/config poll at a time (pollCentralized // holds it across the request and its resends). The relay provisions under a per-server // lock it does not wait on: a second poll that arrives while the first is provisioning @@ -2113,7 +2118,9 @@ func (d *daemon) buildReporter(ctx context.Context) *health.Reporter { // centralized p := d.pollCentralized(ctx, "", "") d.afterFailedPoll(p) - return d.reporterFromPoll(p, false) + r := d.reporterFromPoll(p, false) + d.logInitialEmailOutcome(p) + return r } // afterFailedPoll clears the relay secret a poll was refused with. A DEFINITIVE rejection means @@ -2200,6 +2207,9 @@ type configPoll struct { secret string // the exact secret sent, the comparand of a rejected-secret clear frequency cron.Frequency // the schedule frequency sent notify notifyPolicy // the notify policy whose ack cfg carries + // initialEmailSent says the poll carried HEALTHCHECK_ACCOUNT_INITIAL_EMAIL; cfg then holds the + // relay's answer in AccountInitialEmail. The address itself is never kept here. + initialEmailSent bool } // relaySecret is the relay secret on disk or, when there is none yet, one from a THROTTLED, @@ -2245,6 +2255,12 @@ func (d *daemon) pollCentralizedTo(ctx context.Context, logger *logging.Logger, // checks back to daily on a poll with channels and no frequency. want := d.wantedNotifyPolicy() freq := d.wantedFrequency() + // The portal's initial email rides the start poll and the poll before each run, the two that + // report it (logInitialEmailOutcome); the schedule negotiation's polls do not carry it. + initialEmail := "" + if op != "schedule" { + initialEmail = d.wantedInitialEmail() + } fetch := func(want notifyPolicy) (health.CentralizedConfig, int, error) { plog := health.PollLog{} if op != "" { @@ -2252,13 +2268,15 @@ func (d *daemon) pollCentralizedTo(ctx context.Context, logger *logging.Logger, URLDetail: fmt.Sprintf("frequency=%s notify_on=%s channels=%s", freq, want.notifyOn, want.channelList())} } return health.FetchCentralizedConfigPoll(ctx, nil, d.cfg.ServerAPIHost, d.cfg.ServerID, secret, - want.channels, want.notifyOn, string(freq), plog) + want.channels, want.notifyOn, string(freq), initialEmail, plog) } cfg, status, err := fetch(want) - p := configPoll{sent: true, status: status, cfg: cfg, err: err, secret: secret, frequency: freq, notify: want} + p := configPoll{sent: true, status: status, cfg: cfg, err: err, secret: secret, frequency: freq, notify: want, + initialEmailSent: initialEmail != ""} if err != nil { return p } + d.recordInitialEmailAck(initialEmail, p.cfg.AccountInitialEmail) // A poll that sent a policy no longer wanted may have reached the relay after a newer one // and put the old policy back there: send the wanted one again, so the relay's last word is // always the policy wanted now. Every answer is recorded, the last resend's included: the diff --git a/cmd/proxsave/daemon_initial_email.go b/cmd/proxsave/daemon_initial_email.go new file mode 100644 index 00000000..30782d5d --- /dev/null +++ b/cmd/proxsave/daemon_initial_email.go @@ -0,0 +1,104 @@ +package main + +import ( + "strings" + + "github.com/tis24dev/proxsave/internal/config" + "github.com/tis24dev/proxsave/internal/health" + "github.com/tis24dev/proxsave/internal/logging" + "github.com/tis24dev/proxsave/internal/ui/theme" +) + +// The initial email of the monitoring portal (issue 347). HEALTHCHECK_ACCOUNT_INITIAL_EMAIL is sent +// to the relay on the start poll and on the poll before each scheduled run, until the relay gives +// a final answer for that address (applied or rejected); a different address is sent again. The +// relay mails a confirmation link; the click moves this server's project into the account of the +// address. The address is never logged. + +// wantedInitialEmail is the address the next poll sends: "" when none is set, healthchecks is not +// centralized, or the relay already answered for good about this address. +func (d *daemon) wantedInitialEmail() string { + if d.cfg == nil || !d.cfg.HealthcheckEnabled || d.cfg.HealthcheckMode != config.HealthcheckModeCentralized { + return "" + } + d.mu.Lock() + defer d.mu.Unlock() + want := d.cfg.HealthcheckAccountInitialEmail + if d.initialEmailWant != nil { + want = *d.initialEmailWant + } + want = strings.TrimSpace(want) + if want == "" || want == d.initialEmailDone { + return "" + } + return want +} + +// recordInitialEmailAck keeps a final answer for sent, so that address is not sent again. +func (d *daemon) recordInitialEmailAck(sent string, ack *health.AccountInitialEmailAck) { + if sent == "" || !ack.Final() { + return + } + d.mu.Lock() + d.initialEmailDone = sent + d.mu.Unlock() +} + +// setInitialEmailWant records the address as backup.env holds it now (refreshNotifyPolicy). +func (d *daemon) setInitialEmailWant(value string) { + d.mu.Lock() + d.initialEmailWant = &value + d.mu.Unlock() +} + +// logInitialEmailOutcome is the "Applying monitoring portal email..." block of a poll that sent +// the address: DEBUG evidence, then the outcome. Nothing visible when the poll failed or the relay +// did not answer about it (an older relay): the next poll sends it again. +func (d *daemon) logInitialEmailOutcome(p configPoll) { + if !p.initialEmailSent { + return + } + logger := logging.GetDefaultLogger() + logging.DebugStep(logger, "initial email", "read HEALTHCHECK_ACCOUNT_INITIAL_EMAIL set=true source=%s", d.cfg.ConfigPath) + if p.err != nil { + logging.DebugStep(logger, "initial email", "no answer %s", pollFailure(p)) + return + } + ack := p.cfg.AccountInitialEmail + if ack == nil { + logging.DebugStep(logger, "initial email", "ack missing") + return + } + logging.DebugStep(logger, "initial email", "ack state=%s reason=%s", ack.State, ack.Reason) + logging.Info("Applying monitoring portal email...") + switch ack.State { + case "applied": + logging.Info("%s Monitoring portal email: applied", theme.SymbolSuccess) + case "rejected": + if why := initialEmailRejection(ack.Reason); why != "" { + logging.Info(" %s", why) + } + logging.Warning("%s Monitoring portal email: not applied", theme.SymbolWarning) + default: + if ack.Reason == "limit" { + logging.Info(" Daily mail limit reached, retried at the next run") + } else { + logging.Info(" Waiting for the confirmation link sent by mail") + } + logging.Info("%s Monitoring portal email: not applied", theme.SymbolWarning) + } +} + +// initialEmailRejection is the operator's reason for a rejected address; "" for a reason this +// client does not know (the DEBUG line above has it). +func initialEmailRejection(reason string) string { + switch reason { + case "invalid": + return "Not a usable email address" + case "reserved": + return "An address of the ProxSave HC Server" + case "unconfirmed": + return "3 confirmation mails expired; set another address" + } + return "" +} diff --git a/cmd/proxsave/daemon_initial_email_test.go b/cmd/proxsave/daemon_initial_email_test.go new file mode 100644 index 00000000..157c5944 --- /dev/null +++ b/cmd/proxsave/daemon_initial_email_test.go @@ -0,0 +1,306 @@ +package main + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + "github.com/tis24dev/proxsave/internal/config" + "github.com/tis24dev/proxsave/internal/identity" +) + +const testInitialEmail = "operator@example.org" + +// emailRelay is a fake config endpoint that records the initial-email header of every poll and +// answers it with ack (nil: no answer about it, as an older relay). +type emailRelay struct { + mu sync.Mutex + headers []string + queries []string + ack map[string]string + applied bool // the notify policy ack +} + +func (e *emailRelay) handler(w http.ResponseWriter, r *http.Request) { + q := r.URL.Query() + e.mu.Lock() + e.headers = append(e.headers, r.Header.Get("X-Proxsave-Account-Initial-Email")) + e.queries = append(e.queries, r.URL.RawQuery) + ack, applied := e.ack, e.applied + e.mu.Unlock() + body := map[string]any{ + "mode": "centralized", + "alive_ping_url": "https://hc.invalid/ping/alive", + "backup_ping_url": "https://hc.invalid/ping/backup", + "notify_policy": map[string]any{ + "contract_version": 1, "requested": q.Get("notify_on"), "applied": applied, "channels": []string{"telegram"}, + }, + } + if ack != nil && r.Header.Get("X-Proxsave-Account-Initial-Email") != "" { + body["account_initial_email"] = ack + } + _ = json.NewEncoder(w).Encode(body) +} + +func (e *emailRelay) sent() []string { + e.mu.Lock() + defer e.mu.Unlock() + return append([]string(nil), e.headers...) +} + +func (e *emailRelay) answer(ack map[string]string) { + e.mu.Lock() + e.ack = ack + e.mu.Unlock() +} + +// emailDaemon is a centralized daemon whose backup.env holds body and whose start config held +// HEALTHCHECK_ACCOUNT_INITIAL_EMAIL=start. +func emailDaemon(t *testing.T, relay *emailRelay, start, body string) *daemon { + t.Helper() + for _, key := range []string{"NOTIFY_ON", "TELEGRAM_ENABLED", "TELEGRAM_ENABLE", "EMAIL_ENABLED", "EMAIL_ENABLE", + "GOTIFY_ENABLED", "GOTIFY_ENABLE", "WEBHOOK_ENABLED", "WEBHOOK_ENABLE", "HEALTHCHECK_ACCOUNT_INITIAL_EMAIL"} { + t.Setenv(key, "") + } + base := t.TempDir() + if err := identity.PersistNotifySecret(context.Background(), base, relayTestSecret, nil); err != nil { + t.Fatalf("seed secret: %v", err) + } + path := filepath.Join(base, "backup.env") + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatalf("write backup.env: %v", err) + } + server := httptest.NewServer(http.HandlerFunc(relay.handler)) + t.Cleanup(server.Close) + return &daemon{ + cfg: &config.Config{ + ConfigPath: path, + BaseDir: base, + HealthcheckEnabled: true, + HealthcheckMode: config.HealthcheckModeCentralized, + HealthcheckAccountInitialEmail: start, + ServerID: "123456789012", + ServerAPIHost: server.URL, + NotifyOn: config.NotifyOnWarning, + TelegramEnabled: true, + }, + now: time.Now, + } +} + +const emailEnv = "HEALTHCHECK_ENABLED=true\nHEALTHCHECK_MODE=centralized\nNOTIFY_ON=warning\nTELEGRAM_ENABLED=true\n" + +// infoLines are the log lines above DEBUG, as the operator reads them: without the timestamp and +// the level column ("INFO ", "WARNING "), indentation kept. +func infoLines(buf string) []string { + var out []string + for _, line := range strings.Split(buf, "\n") { + i := strings.Index(line, "] ") + if i < 0 || len(line) < i+2+9 { + continue + } + level, msg := strings.TrimSpace(line[i+2:i+2+9]), line[i+2+9:] + if level == "INFO" || level == "WARNING" { + out = append(out, msg) + } + } + return out +} + +func startPoll(d *daemon) configPoll { + p := d.pollCentralized(context.Background(), "", "") + d.logInitialEmailOutcome(p) + return p +} + +func TestNoInitialEmailSendsNoHeader(t *testing.T) { + relay := &emailRelay{ack: map[string]string{"state": "pending"}} + d := emailDaemon(t, relay, "", emailEnv) + buf := captureDaemonDebug(t) + startPoll(d) + if got := relay.sent(); len(got) != 1 || got[0] != "" { + t.Fatalf("headers = %q, want one poll without it", got) + } + if strings.Contains(buf.String(), "portal email") { + t.Fatalf("a block without the variable:\n%s", buf) + } +} + +func TestPendingAnswerKeepsSendingAndSaysWhy(t *testing.T) { + relay := &emailRelay{ack: map[string]string{"state": "pending"}} + d := emailDaemon(t, relay, testInitialEmail, emailEnv) + buf := captureDaemonDebug(t) + startPoll(d) + startPoll(d) + if got := relay.sent(); len(got) != 2 || got[0] != testInitialEmail || got[1] != testInitialEmail { + t.Fatalf("headers = %q", got) + } + want := []string{"Applying monitoring portal email...", " Waiting for the confirmation link sent by mail", + "⚠ Monitoring portal email: not applied"} + if got := infoLines(buf.String()); strings.Join(got, "|") != strings.Join(append(want, want...), "|") { + t.Fatalf("lines:\n%s", strings.Join(got, "\n")) + } + for _, q := range relay.queries { + if strings.Contains(q, "example.org") { + t.Fatalf("the address is in the query: %s", q) + } + } + if strings.Contains(buf.String(), testInitialEmail) { + t.Fatalf("the address reached the log:\n%s", buf) + } +} + +func TestLimitAnswerSaysTheLimit(t *testing.T) { + relay := &emailRelay{ack: map[string]string{"state": "pending", "reason": "limit"}} + d := emailDaemon(t, relay, testInitialEmail, emailEnv) + buf := captureDaemonDebug(t) + startPoll(d) + want := "Applying monitoring portal email...| Daily mail limit reached, retried at the next run|⚠ Monitoring portal email: not applied" + if got := strings.Join(infoLines(buf.String()), "|"); got != want { + t.Fatalf("lines: %s", got) + } +} + +func TestAppliedAnswerIsSaidOnceAndTheAddressIsNotSentAgain(t *testing.T) { + relay := &emailRelay{ack: map[string]string{"state": "applied"}} + d := emailDaemon(t, relay, testInitialEmail, emailEnv) + buf := captureDaemonDebug(t) + startPoll(d) + startPoll(d) + if got := relay.sent(); len(got) != 2 || got[0] != testInitialEmail || got[1] != "" { + t.Fatalf("headers = %q, want the address once", got) + } + want := "Applying monitoring portal email...|✓ Monitoring portal email: applied" + if got := strings.Join(infoLines(buf.String()), "|"); got != want { + t.Fatalf("lines: %s", got) + } +} + +func TestRejectedAnswersSayWhyAtWarning(t *testing.T) { + for reason, why := range map[string]string{ + "invalid": " Not a usable email address", + "reserved": " An address of the ProxSave HC Server", + "unconfirmed": " 3 confirmation mails expired; set another address", + } { + relay := &emailRelay{ack: map[string]string{"state": "rejected", "reason": reason}} + d := emailDaemon(t, relay, testInitialEmail, emailEnv) + buf := captureDaemonDebug(t) + startPoll(d) + startPoll(d) + want := "Applying monitoring portal email...|" + why + "|⚠ Monitoring portal email: not applied" + if got := strings.Join(infoLines(buf.String()), "|"); got != want { + t.Errorf("%s: lines %s", reason, got) + } + if !strings.Contains(buf.String(), "WARNING") { + t.Errorf("%s: the outcome is not a WARNING:\n%s", reason, buf) + } + if got := relay.sent(); got[1] != "" { + t.Errorf("%s: sent again after a final answer: %q", reason, got) + } + } +} + +func TestAnUnknownReasonHasNoWhyLine(t *testing.T) { + relay := &emailRelay{ack: map[string]string{"state": "rejected", "reason": "something_new"}} + d := emailDaemon(t, relay, testInitialEmail, emailEnv) + buf := captureDaemonDebug(t) + startPoll(d) + want := "Applying monitoring portal email...|⚠ Monitoring portal email: not applied" + if got := strings.Join(infoLines(buf.String()), "|"); got != want { + t.Fatalf("lines: %s", got) + } + if !strings.Contains(buf.String(), "reason=something_new") { + t.Fatalf("the DEBUG evidence lacks the reason:\n%s", buf) + } +} + +func TestAnOlderRelayIsSilentAndAskedAgain(t *testing.T) { + relay := &emailRelay{} + d := emailDaemon(t, relay, testInitialEmail, emailEnv) + buf := captureDaemonDebug(t) + startPoll(d) + startPoll(d) + if got := infoLines(buf.String()); len(got) != 0 { + t.Fatalf("visible lines without an answer: %q", got) + } + if got := relay.sent(); got[1] != testInitialEmail { + t.Fatalf("not asked again: %q", got) + } +} + +func TestScheduleNegotiationPollsNeverCarryTheAddress(t *testing.T) { + relay := &emailRelay{ack: map[string]string{"state": "pending"}} + d := emailDaemon(t, relay, testInitialEmail, emailEnv) + d.pollCentralizedTo(context.Background(), nil, "schedule", "") + if got := relay.sent(); len(got) != 1 || got[0] != "" { + t.Fatalf("headers = %q", got) + } +} + +func TestSelfModeNeverSendsIt(t *testing.T) { + d := &daemon{cfg: &config.Config{HealthcheckEnabled: true, HealthcheckMode: config.HealthcheckModeSelf, + HealthcheckAccountInitialEmail: testInitialEmail}} + if got := d.wantedInitialEmail(); got != "" { + t.Fatalf("self mode wants %q", got) + } + d.cfg.HealthcheckMode, d.cfg.HealthcheckEnabled = config.HealthcheckModeCentralized, false + if got := d.wantedInitialEmail(); got != "" { + t.Fatalf("disabled healthchecks want %q", got) + } +} + +// Before a run, with the notify policy already confirmed: the address still goes out, on a poll +// without the notify level block, and its block follows. +func TestBeforeARunTheAddressGoesOutEvenWithAConfirmedPolicy(t *testing.T) { + relay := &emailRelay{applied: true, ack: map[string]string{"state": "pending"}} + d := emailDaemon(t, relay, "", emailEnv+"HEALTHCHECK_ACCOUNT_INITIAL_EMAIL="+testInitialEmail+"\n") + d.notifyApplied = ¬ifyPolicy{notifyOn: config.NotifyOnWarning, channels: []string{"telegram"}} + buf := captureDaemonDebug(t) + d.refreshNotifyPolicy(context.Background()) + if got := relay.sent(); len(got) != 1 || got[0] != testInitialEmail { + t.Fatalf("headers = %q", got) + } + want := "Applying monitoring portal email...| Waiting for the confirmation link sent by mail|⚠ Monitoring portal email: not applied" + if got := strings.Join(infoLines(buf.String()), "|"); got != want { + t.Fatalf("lines: %s", got) + } +} + +// A confirmed policy and no address due: no request, as before. +func TestBeforeARunNothingIsSentWithoutAnAddressDue(t *testing.T) { + relay := &emailRelay{applied: true, ack: map[string]string{"state": "applied"}} + d := emailDaemon(t, relay, testInitialEmail, emailEnv+"HEALTHCHECK_ACCOUNT_INITIAL_EMAIL="+testInitialEmail+"\n") + d.notifyApplied = ¬ifyPolicy{notifyOn: config.NotifyOnWarning, channels: []string{"telegram"}} + captureDaemonDebug(t) + startPoll(d) // applied: final for this address + d.refreshNotifyPolicy(context.Background()) // still the same address: nothing due + if got := relay.sent(); len(got) != 1 { + t.Fatalf("polls = %q, want only the first", got) + } +} + +// A hand edit to another address reaches the relay before the next run, the policy block first. +func TestBeforeARunAChangedAddressIsSentAfterTheNotifyBlock(t *testing.T) { + relay := &emailRelay{applied: false, ack: map[string]string{"state": "rejected", "reason": "unconfirmed"}} + d := emailDaemon(t, relay, testInitialEmail, emailEnv+"HEALTHCHECK_ACCOUNT_INITIAL_EMAIL=other@example.org\n") + captureDaemonDebug(t) + startPoll(d) + relay.answer(map[string]string{"state": "pending"}) + buf := captureDaemonDebug(t) + d.refreshNotifyPolicy(context.Background()) + if got := relay.sent(); got[len(got)-1] != "other@example.org" { + t.Fatalf("headers = %q", got) + } + lines := infoLines(buf.String()) + if len(lines) < 4 || lines[0] != "Applying notify level..." || + lines[len(lines)-3] != "Applying monitoring portal email..." { + t.Fatalf("order:\n%s", strings.Join(lines, "\n")) + } +} diff --git a/cmd/proxsave/daemon_notify_policy.go b/cmd/proxsave/daemon_notify_policy.go index 0dac0688..230e931a 100644 --- a/cmd/proxsave/daemon_notify_policy.go +++ b/cmd/proxsave/daemon_notify_policy.go @@ -89,19 +89,26 @@ func (d *daemon) refreshNotifyPolicy(ctx context.Context) { return } want := notifyPolicy{notifyOn: negotiatedNotifyOn(fresh), channels: enabledNotifyChannels(fresh)} + d.setInitialEmailWant(fresh.HealthcheckAccountInitialEmail) d.mu.Lock() d.notifyWant = &want applied := d.notifyApplied != nil && d.notifyApplied.equal(want) d.mu.Unlock() logger := logging.GetDefaultLogger() - if applied { + // A confirmed policy needs no request, unless the portal's initial email is still to be sent: + // that poll then goes out without the notify level block. + emailDue := d.wantedInitialEmail() != "" + if applied && !emailDue { logging.DebugStep(logger, "notify policy", "before run notify_on=%s channels=%s already confirmed, not sent", want.notifyOn, want.channelList()) return } - block := true - if secret, _ := identity.LoadNotifySecret(d.cfg.BaseDir); strings.TrimSpace(secret) == "" { + block := !applied + if !block { + logging.DebugStep(logger, "notify policy", "before run notify_on=%s channels=%s already confirmed, sent for the initial email", + want.notifyOn, want.channelList()) + } else if secret, _ := identity.LoadNotifySecret(d.cfg.BaseDir); strings.TrimSpace(secret) == "" { block = false logging.DebugStep(logger, "notify policy", "before run notify_on=%s channels=%s, no relay secret on disk", want.notifyOn, want.channelList()) @@ -143,16 +150,19 @@ func (d *daemon) refreshNotifyPolicy(ctx context.Context) { case <-timer.C: case <-ctx.Done(): } - if !block { - return - } if !answered { - logging.DebugStep(logger, "notify policy", "no answer within %s", wait) - d.logNotifyLevelOutcome(fresh.NotifyOn, want, reachNone) + if block { + logging.DebugStep(logger, "notify policy", "no answer within %s", wait) + d.logNotifyLevelOutcome(fresh.NotifyOn, want, reachNone) + } return } - d.logNotifyPolicyAnswer(result) - d.logNotifyLevelOutcome(fresh.NotifyOn, want, result.answer()) + if block { + d.logNotifyPolicyAnswer(result) + d.logNotifyLevelOutcome(fresh.NotifyOn, want, result.answer()) + } + // After the notify level block, never inside it. + d.logInitialEmailOutcome(result) } // logNotifyPolicyRead is the DEBUG evidence of what the notify level block sends: NOTIFY_ON as diff --git a/cmd/proxsave/healthcheck_portal_cli_test.go b/cmd/proxsave/healthcheck_portal_cli_test.go new file mode 100644 index 00000000..55cf0ad2 --- /dev/null +++ b/cmd/proxsave/healthcheck_portal_cli_test.go @@ -0,0 +1,45 @@ +package main + +import ( + "testing" + + "github.com/tis24dev/proxsave/internal/orchestrator" +) + +// The --install --cli portal block as it was before the portal sign-in page existed (issue 347). +func TestPrintHealthcheckPortalOfTodayIsUnchanged(t *testing.T) { + cases := []struct { + name string + st orchestrator.HealthcheckSetupState + want string + }{ + {"magic-link", orchestrator.HealthcheckSetupState{LoginURL: "https://hc.proxsave.dev/l/abc"}, + "\nYour monitoring portal (single-use link, valid ~1h):\n https://hc.proxsave.dev/l/abc\n" + + "Open it to set a password and configure alert channels (email, etc.).\n\n"}, + {"password fallback", orchestrator.HealthcheckSetupState{PortalURL: "https://hc.proxsave.dev/accounts/login/", + PortalLogin: "1@proxsave.dev"}, + "\nYour monitoring portal:\n https://hc.proxsave.dev/accounts/login/\n" + + "Sign in as 1@proxsave.dev with the password you set.\n\n"}, + {"nothing", orchestrator.HealthcheckSetupState{}, ""}, + } + for _, c := range cases { + if got := captureStdout(t, func() { printHealthcheckPortal(c.st) }); got != c.want { + t.Errorf("%s:\ngot %q\nwant %q", c.name, got, c.want) + } + } +} + +// The operator owns the project (issue 347): the sign-in page, with the approved sign-in line. +func TestPrintHealthcheckPortalForTheSignInPage(t *testing.T) { + page := "https://hc.proxsave.dev/accounts/login/" + for passwordSet, line := range map[bool]string{ + false: `Sign in with your email: use "Email Me a Link" on the sign-in page.`, + true: "Sign in with your email and the password you set.", + } { + st := orchestrator.HealthcheckSetupState{LoginURL: page, LoginIsPortalPage: true, LoginPasswordSet: passwordSet} + want := "\nYour monitoring portal:\n " + page + "\n" + line + "\n\n" + if got := captureStdout(t, func() { printHealthcheckPortal(st) }); got != want { + t.Errorf("password=%t:\ngot %q\nwant %q", passwordSet, got, want) + } + } +} diff --git a/cmd/proxsave/healthcheck_setup_cli.go b/cmd/proxsave/healthcheck_setup_cli.go index 7974150a..cb5baf68 100644 --- a/cmd/proxsave/healthcheck_setup_cli.go +++ b/cmd/proxsave/healthcheck_setup_cli.go @@ -150,6 +150,14 @@ func runHealthcheckSetupCLI(ctx context.Context, reader *bufio.Reader, baseDir, func printHealthcheckPortal(st orchestrator.HealthcheckSetupState) { if link := strings.TrimSpace(st.LoginURL); link != "" { fmt.Println() + if st.LoginIsPortalPage { + // The operator owns the project (issue 347): the sign-in page, not a single-use link. + fmt.Println("Your monitoring portal:") + fmt.Printf(" %s\n", link) + fmt.Println(orchestrator.HealthcheckPortalPageSignIn(st.LoginPasswordSet)) + fmt.Println() + return + } fmt.Println("Your monitoring portal (single-use link, valid ~1h):") fmt.Printf(" %s\n", link) fmt.Println("Open it to set a password and configure alert channels (email, etc.).") diff --git a/cmd/proxsave/testdata/install_characterization/edit_noop.env b/cmd/proxsave/testdata/install_characterization/edit_noop.env index 3ecab87b..2be22700 100644 --- a/cmd/proxsave/testdata/install_characterization/edit_noop.env +++ b/cmd/proxsave/testdata/install_characterization/edit_noop.env @@ -479,6 +479,7 @@ MAX_RUN_DURATION=1h # daemon watchdog: hard timeout for one backup; o # ---------------------------------------------------------------------- HEALTHCHECK_ENABLED=false # report service-alive heartbeat + per-run outcome to a healthchecks monitor HEALTHCHECK_MODE=off # centralized = ping URLs fetched from the proxsave_server (zero setup, reuses the Telegram identity); self = your own healthchecks/SaaS +HEALTHCHECK_ACCOUNT_INITIAL_EMAIL= # centralized: email for your monitoring portal; applied once after you confirm the mail, then change it in the portal HEALTHCHECK_HEARTBEAT_INTERVAL=5m # service-alive ping interval HEALTHCHECK_SEND_LOG=true # POST the log tail on a warning/error outcome HEALTHCHECK_ALIVE_URL= # self: the FULL service-alive ping URL you paste (e.g. https://hc-ping.com/); centralized: fallback when ProxSave HC Server is unreachable diff --git a/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env b/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env index a0120ffc..3a6a347a 100644 --- a/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env +++ b/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env @@ -479,6 +479,7 @@ MAX_RUN_DURATION=1h # daemon watchdog: hard timeout for one backup; o # ---------------------------------------------------------------------- HEALTHCHECK_ENABLED=true # report service-alive heartbeat + per-run outcome to a healthchecks monitor HEALTHCHECK_MODE=centralized # centralized = ping URLs fetched from the proxsave_server (zero setup, reuses the Telegram identity); self = your own healthchecks/SaaS +HEALTHCHECK_ACCOUNT_INITIAL_EMAIL= # centralized: email for your monitoring portal; applied once after you confirm the mail, then change it in the portal HEALTHCHECK_HEARTBEAT_INTERVAL=5m # service-alive ping interval HEALTHCHECK_SEND_LOG=true # POST the log tail on a warning/error outcome HEALTHCHECK_ALIVE_URL= # self: the FULL service-alive ping URL you paste (e.g. https://hc-ping.com/); centralized: fallback when ProxSave HC Server is unreachable diff --git a/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env b/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env index c5aa15e8..36518c3c 100644 --- a/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env +++ b/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env @@ -479,6 +479,7 @@ MAX_RUN_DURATION=1h # daemon watchdog: hard timeout for one backup; o # ---------------------------------------------------------------------- HEALTHCHECK_ENABLED=true # report service-alive heartbeat + per-run outcome to a healthchecks monitor HEALTHCHECK_MODE=self # centralized = ping URLs fetched from the proxsave_server (zero setup, reuses the Telegram identity); self = your own healthchecks/SaaS +HEALTHCHECK_ACCOUNT_INITIAL_EMAIL= # centralized: email for your monitoring portal; applied once after you confirm the mail, then change it in the portal HEALTHCHECK_HEARTBEAT_INTERVAL=5m # service-alive ping interval HEALTHCHECK_SEND_LOG=true # POST the log tail on a warning/error outcome HEALTHCHECK_ALIVE_URL= # self: the FULL service-alive ping URL you paste (e.g. https://hc-ping.com/); centralized: fallback when ProxSave HC Server is unreachable diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index bc010388..821f4fb7 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -353,6 +353,7 @@ running a backup. The keys are: ```bash HEALTHCHECK_ENABLED=false # true with the daemon (--daemon-setup / --upgrade); back to false by --daemon-remove HEALTHCHECK_MODE=centralized # centralized (fetch URLs from the server) | self +HEALTHCHECK_ACCOUNT_INITIAL_EMAIL= # centralized: your email for the monitoring portal, applied once after you confirm the mail HEALTHCHECK_HEARTBEAT_INTERVAL=5m HEALTHCHECK_UPDATE_INTERVAL=5m HEALTHCHECK_SEND_LOG=true diff --git a/docs/HEALTHCHECKS.md b/docs/HEALTHCHECKS.md index 88f2666d..d03947bc 100644 --- a/docs/HEALTHCHECKS.md +++ b/docs/HEALTHCHECKS.md @@ -308,7 +308,8 @@ ProxSave shows you how to reach it in three places: What it shows depends on whether you have given yourself a portal password yet. **Before you have a password**, you get a **single-use login link**, valid for about an -hour. Open it and **set a password**. That is what turns the link into an account you +hour. Open it to reach the portal. To set a password, first give the portal your email +(see below). That is what turns the link into an account you can log into later, and it is the point at which you can configure alert channels, email and the rest, so the monitor can reach you when a check goes down. Until then the server mints a fresh link every time, so a link that expired is never a problem: just @@ -318,6 +319,17 @@ open the dashboard check again. portal's own address plus the identity you sign in with. That identity is an **email address**, not a username. Sign in there with the password you chose. +**Your email.** The account the server makes for a host has an address that receives no +mail, and the portal mails a code before it lets you set a password or change the email. +Set `HEALTHCHECK_ACCOUNT_INITIAL_EMAIL` in `backup.env` to your address: the daemon sends +it to the server, which mails a confirmation link to it, valid 15 minutes. Press Confirm +on that page and this host's checks move into the portal account of that address, made +then if you have none. Hosts with the same address share one account, one project each. +From then on you sign in with your email, by password or with "Email Me a Link", and +change the address from the portal: the variable is not used again for this host. The +server sends at most three mails for one address; after that only a different address +starts again. + The exact wording differs a little per surface. The log epilogue uses `Healthchecks Portal:` for both states, the link and the sign-in address alike, and adds a `Healthchecks Login:` line only in the second. The run screen distinguishes them diff --git a/internal/config/config.go b/internal/config/config.go index fc1c0dcb..8142b7ea 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -263,16 +263,20 @@ type Config struct { PersonalScriptPostRun string // Healthchecks connector (dead-man switch + backup outcome), used by the daemon. - HealthcheckEnabled bool - HealthcheckMode string // "centralized" (fetch ping-urls from the server) | "self" - HealthcheckHeartbeatInterval time.Duration - HealthcheckSendLog bool - HealthcheckAliveURL string // centralized cache (auto-filled from the server) - HealthcheckBackupURL string // centralized cache (auto-filled from the server) - HealthcheckPingEndpoint string // self mode: ping base - HealthcheckPingKey string // self mode: optional ping key for slug URLs - HealthcheckAliveID string // self mode: UUID or slug of the service-alive check - HealthcheckBackupID string // self mode: UUID or slug of the backup-outcome check + HealthcheckEnabled bool + HealthcheckMode string // "centralized" (fetch ping-urls from the server) | "self" + // HealthcheckAccountInitialEmail is the operator's address for the monitoring portal + // (centralized only, issue 347): sent to the relay until it is applied or refused, then + // changed from the portal. Empty sends nothing. + HealthcheckAccountInitialEmail string + HealthcheckHeartbeatInterval time.Duration + HealthcheckSendLog bool + HealthcheckAliveURL string // centralized cache (auto-filled from the server) + HealthcheckBackupURL string // centralized cache (auto-filled from the server) + HealthcheckPingEndpoint string // self mode: ping base + HealthcheckPingKey string // self mode: optional ping key for slug URLs + HealthcheckAliveID string // self mode: UUID or slug of the service-alive check + HealthcheckBackupID string // self mode: UUID or slug of the backup-outcome check // Fase 1 updates sensor (daemon): whether a newer release is available. HealthcheckUpdatesURL string // self mode: full updates check ping URL (optional) HealthcheckUpdatesID string // self mode: UUID or slug of the updates check @@ -953,6 +957,7 @@ func (c *Config) parsePersonalScriptSettings() { func (c *Config) parseHealthcheckSettings() { c.HealthcheckEnabled = c.getBool("HEALTHCHECK_ENABLED", false) c.HealthcheckMode = normalizeHealthcheckMode(c.getString("HEALTHCHECK_MODE", HealthcheckModeCentralized)) + c.HealthcheckAccountInitialEmail = strings.TrimSpace(c.getString("HEALTHCHECK_ACCOUNT_INITIAL_EMAIL", "")) c.HealthcheckHeartbeatInterval = c.getDuration("HEALTHCHECK_HEARTBEAT_INTERVAL", 5*time.Minute) c.HealthcheckSendLog = c.getBool("HEALTHCHECK_SEND_LOG", true) c.HealthcheckAliveURL = strings.TrimSpace(c.getString("HEALTHCHECK_ALIVE_URL", "")) diff --git a/internal/config/scheduler_healthcheck_test.go b/internal/config/scheduler_healthcheck_test.go index afdff50b..d8179ee5 100644 --- a/internal/config/scheduler_healthcheck_test.go +++ b/internal/config/scheduler_healthcheck_test.go @@ -250,7 +250,7 @@ func TestRealTemplateContainsNewKeys(t *testing.T) { for _, key := range []string{ "SCHEDULER_MODE=", "SCHEDULER_FREQUENCY=", "SCHEDULER_WEEKDAY=", "SCHEDULER_MONTHDAY=", "SCHEDULER_TIME=", "MAX_RUN_DURATION=", - "HEALTHCHECK_ENABLED=", "HEALTHCHECK_MODE=", "HEALTHCHECK_HEARTBEAT_INTERVAL=", + "HEALTHCHECK_ENABLED=", "HEALTHCHECK_MODE=", "HEALTHCHECK_ACCOUNT_INITIAL_EMAIL=", "HEALTHCHECK_HEARTBEAT_INTERVAL=", "HEALTHCHECK_SEND_LOG=", "HEALTHCHECK_ALIVE_URL=", "HEALTHCHECK_BACKUP_URL=", "HEALTHCHECK_PING_ENDPOINT=", "HEALTHCHECK_PING_KEY=", "HEALTHCHECK_ALIVE_ID=", "HEALTHCHECK_BACKUP_ID=", diff --git a/internal/config/templates/backup.env b/internal/config/templates/backup.env index 26d320f8..249564fe 100644 --- a/internal/config/templates/backup.env +++ b/internal/config/templates/backup.env @@ -479,6 +479,7 @@ MAX_RUN_DURATION=1h # daemon watchdog: hard timeout for one backup; o # ---------------------------------------------------------------------- HEALTHCHECK_ENABLED=false # report service-alive heartbeat + per-run outcome to a healthchecks monitor HEALTHCHECK_MODE=centralized # centralized = ping URLs fetched from the proxsave_server (zero setup, reuses the Telegram identity); self = your own healthchecks/SaaS +HEALTHCHECK_ACCOUNT_INITIAL_EMAIL= # centralized: email for your monitoring portal; applied once after you confirm the mail, then change it in the portal HEALTHCHECK_HEARTBEAT_INTERVAL=5m # service-alive ping interval HEALTHCHECK_SEND_LOG=true # POST the log tail on a warning/error outcome HEALTHCHECK_ALIVE_URL= # self: the FULL service-alive ping URL you paste (e.g. https://hc-ping.com/); centralized: fallback when ProxSave HC Server is unreachable diff --git a/internal/health/config.go b/internal/health/config.go index 624855e4..85154db3 100644 --- a/internal/health/config.go +++ b/internal/health/config.go @@ -63,6 +63,14 @@ type CentralizedConfig struct { // infer it from an empty LoginURL: a failed mint looks identical and would tell a // password-less operator to sign in with a password they never chose. PasswordSet *bool `json:"password_set"` + // LoginKind is LoginKindPortal when LoginURL is the portal's plain sign-in page rather than a + // single-use magic-link: the relay sends it for a project the operator owns (an initial + // email applied, or a project moved to an account of theirs). "" for a magic-link or an + // older relay. + LoginKind string `json:"login_kind,omitempty"` + // AccountInitialEmail is the relay's answer to HEALTHCHECK_ACCOUNT_INITIAL_EMAIL on a poll + // that sent it; nil otherwise or from an older relay. + AccountInitialEmail *AccountInitialEmailAck `json:"account_initial_email,omitempty"` // Checks carries additive, OPTIONAL per-sensor ping URLs beyond the two frozen // alive/backup keys (Fase 1: {"updates":""}). An old server omits it; the // completeness check below still requires only alive+backup, so a new client against @@ -115,7 +123,7 @@ func FetchCentralizedConfig(ctx context.Context, client *http.Client, serverAPIH } func fetchConfig(ctx context.Context, client *http.Client, serverAPIHost, serverID, secret string, includeLogin bool) (CentralizedConfig, error) { - cfg, _, err := fetchConfigPoll(ctx, client, serverAPIHost, serverID, secret, includeLogin, nil, nil, PollLog{}) + cfg, _, err := fetchConfigPoll(ctx, client, serverAPIHost, serverID, secret, includeLogin, nil, nil, "", PollLog{}) return cfg, err } @@ -132,7 +140,7 @@ type PollLog struct { // and also returns the HTTP status of the answer: 0 when // there was none (the error is then the transport's), the relay's status otherwise, whatever the // error. With plog.Logger an answer other than 200 adds an excerpt of its body, secret masked. -func fetchConfigPoll(ctx context.Context, client *http.Client, serverAPIHost, serverID, secret string, includeLogin bool, channels []string, extra url.Values, plog PollLog) (CentralizedConfig, int, error) { +func fetchConfigPoll(ctx context.Context, client *http.Client, serverAPIHost, serverID, secret string, includeLogin bool, channels []string, extra url.Values, initialEmail string, plog PollLog) (CentralizedConfig, int, error) { q := url.Values{"server_id": {serverID}} maps.Copy(q, extra) if includeLogin { @@ -150,15 +158,17 @@ func fetchConfigPoll(ctx context.Context, client *http.Client, serverAPIHost, se // bounded read, error redaction) is the shared serverbot brick; the endpoint // vocabulary below (status map + typed errors + completeness check) stays here. resp, err := serverbot.New(serverAPIHost, client, plog.Logger).Do(ctx, serverbot.Request{ - Method: http.MethodGet, - Path: "/api/healthcheck/config", - Query: q, - Secret: secret, - Timeout: fetchTimeout, - MaxBytes: 8192, - LogOperation: plog.Operation, - LogPrefix: plog.Prefix, - LogURLDetail: plog.URLDetail, + Method: http.MethodGet, + Path: "/api/healthcheck/config", + Query: q, + Secret: secret, + Timeout: fetchTimeout, + MaxBytes: 8192, + // The address travels in a header and never in LogURLDetail: never logged. + AccountInitialEmail: initialEmail, + LogOperation: plog.Operation, + LogPrefix: plog.Prefix, + LogURLDetail: plog.URLDetail, }) if err != nil { // Transport failure (build/dial/read), already URL-stripped + secret-masked by @@ -210,3 +220,21 @@ func fetchConfigPoll(ctx context.Context, client *http.Client, serverAPIHost, se return CentralizedConfig{}, resp.Status, fmt.Errorf("healthcheck config: HTTP %d", resp.Status) } } + +// LoginKindPortal is CentralizedConfig.LoginKind for a login URL that is the portal's sign-in page. +const LoginKindPortal = "portal" + +// AccountInitialEmailAck is the relay's answer about HEALTHCHECK_ACCOUNT_INITIAL_EMAIL (issue 347): +// State is "pending" (a confirmation mail is out, or none could go yet), "applied" (the address +// now signs in to the portal; the variable is not needed any more) or "rejected", with Reason +// "invalid", "reserved" or "unconfirmed". A pending answer may carry Reason "limit". +type AccountInitialEmailAck struct { + State string `json:"state"` + Reason string `json:"reason,omitempty"` +} + +// Final reports whether the relay will answer the same for this address from now on: the daemon +// then stops sending it. +func (a *AccountInitialEmailAck) Final() bool { + return a != nil && (a.State == "applied" || a.State == "rejected") +} diff --git a/internal/health/config_checks_test.go b/internal/health/config_checks_test.go index 7be2fa27..f1634b83 100644 --- a/internal/health/config_checks_test.go +++ b/internal/health/config_checks_test.go @@ -71,7 +71,7 @@ func TestFetchWithChannelsSendsQuery(t *testing.T) { defer srv.Close() // A populated non-nil slice -> channels=email,telegram (comma-joined, order preserved). - cfg, _, err := FetchCentralizedConfigPoll(context.Background(), srv.Client(), srv.URL, "1", "s", []string{"email", "telegram"}, "always", "", PollLog{}) + cfg, _, err := FetchCentralizedConfigPoll(context.Background(), srv.Client(), srv.URL, "1", "s", []string{"email", "telegram"}, "always", "", "", PollLog{}) if err != nil { t.Fatalf("FetchCentralizedConfigPoll: %v", err) } @@ -87,7 +87,7 @@ func TestFetchWithChannelsSendsQuery(t *testing.T) { } // An EMPTY (non-nil) slice -> the explicit "none" sentinel (pause all), never empty value. - if _, _, err := FetchCentralizedConfigPoll(context.Background(), srv.Client(), srv.URL, "1", "s", []string{}, "always", "", PollLog{}); err != nil { + if _, _, err := FetchCentralizedConfigPoll(context.Background(), srv.Client(), srv.URL, "1", "s", []string{}, "always", "", "", PollLog{}); err != nil { t.Fatalf("FetchCentralizedConfigPoll(empty): %v", err) } if got := gotQuery.Get("channels"); got != "none" { diff --git a/internal/health/delivery_test.go b/internal/health/delivery_test.go index 8df0fbdc..3a272745 100644 --- a/internal/health/delivery_test.go +++ b/internal/health/delivery_test.go @@ -170,7 +170,7 @@ func TestFetchCentralizedConfigPollSendsTheContract(t *testing.T) { })) defer srv.Close() - cfg, _, err := FetchCentralizedConfigPoll(context.Background(), srv.Client(), srv.URL, "1", "s", []string{"email", "telegram"}, "failure", "", PollLog{}) + cfg, _, err := FetchCentralizedConfigPoll(context.Background(), srv.Client(), srv.URL, "1", "s", []string{"email", "telegram"}, "failure", "", "", PollLog{}) if err != nil { t.Fatalf("FetchCentralizedConfigPoll: %v", err) } @@ -182,7 +182,7 @@ func TestFetchCentralizedConfigPollSendsTheContract(t *testing.T) { } // No channel enabled is sent as the explicit "none", never as an empty value a proxy could drop. - if _, _, err := FetchCentralizedConfigPoll(context.Background(), srv.Client(), srv.URL, "1", "s", []string{}, "warning", "", PollLog{}); err != nil { + if _, _, err := FetchCentralizedConfigPoll(context.Background(), srv.Client(), srv.URL, "1", "s", []string{}, "warning", "", "", PollLog{}); err != nil { t.Fatalf("FetchCentralizedConfigPoll: %v", err) } if gotChannels != "none" || gotNotifyOn != "warning" { diff --git a/internal/health/schedule.go b/internal/health/schedule.go index 85bcf6ad..5f45ad2e 100644 --- a/internal/health/schedule.go +++ b/internal/health/schedule.go @@ -31,13 +31,15 @@ func (a *ScheduleAck) Confirms(frequency string) bool { // contract-1 notify policy (hc_contract, notify_on and the channel set) plus ?frequency, so the relay // gives the backup check and the periodic notify checks that period and acks it in Schedule. It // also returns the HTTP status of the answer (0 when there was none) and logs the transport -// stages in DEBUG as plog says. An empty frequency is not sent. -func FetchCentralizedConfigPoll(ctx context.Context, client *http.Client, serverAPIHost, serverID, secret string, channels []string, notifyOn, frequency string, plog PollLog) (CentralizedConfig, int, error) { +// stages in DEBUG as plog says. An empty frequency is not sent. A non-empty initialEmail +// (HEALTHCHECK_ACCOUNT_INITIAL_EMAIL) rides the X-Proxsave-Account-Initial-Email header and the +// relay answers it in AccountInitialEmail. +func FetchCentralizedConfigPoll(ctx context.Context, client *http.Client, serverAPIHost, serverID, secret string, channels []string, notifyOn, frequency, initialEmail string, plog PollLog) (CentralizedConfig, int, error) { extra := url.Values{"hc_contract": {"1"}, "notify_on": {notifyOn}} if frequency != "" { extra.Set("frequency", frequency) } - return fetchConfigPoll(ctx, client, serverAPIHost, serverID, secret, false, channels, extra, plog) + return fetchConfigPoll(ctx, client, serverAPIHost, serverID, secret, false, channels, extra, initialEmail, plog) } // ScheduleStateSchemaVersion is the version of the .schedule_state.json layout. diff --git a/internal/health/schedule_test.go b/internal/health/schedule_test.go index 893c21e8..61af5a23 100644 --- a/internal/health/schedule_test.go +++ b/internal/health/schedule_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "os" + "strings" "testing" ) @@ -38,7 +39,7 @@ func TestFetchCentralizedConfigPollSendsFrequencyAndReturnsTheStatus(t *testing. })) defer srv.Close() - cfg, st, err := FetchCentralizedConfigPoll(context.Background(), nil, srv.URL, "1", "s", []string{"email"}, "warning", "weekly", PollLog{}) + cfg, st, err := FetchCentralizedConfigPoll(context.Background(), nil, srv.URL, "1", "s", []string{"email"}, "warning", "weekly", "", PollLog{}) if err != nil || st != http.StatusOK || gotFreq != "weekly" || gotContract != "1" { t.Fatalf("poll = %v, status %d, frequency=%q hc_contract=%q", err, st, gotFreq, gotContract) } @@ -47,7 +48,7 @@ func TestFetchCentralizedConfigPollSendsFrequencyAndReturnsTheStatus(t *testing. } status = http.StatusServiceUnavailable - if _, st, err := FetchCentralizedConfigPoll(context.Background(), nil, srv.URL, "1", "s", nil, "warning", "weekly", PollLog{}); err == nil || st != http.StatusServiceUnavailable { + if _, st, err := FetchCentralizedConfigPoll(context.Background(), nil, srv.URL, "1", "s", nil, "warning", "weekly", "", PollLog{}); err == nil || st != http.StatusServiceUnavailable { t.Fatalf("503 poll = %v, status %d; want an error and 503", err, st) } } @@ -79,3 +80,44 @@ func TestScheduleStateRoundTripIsRootOnly(t *testing.T) { t.Fatal("a corrupt state read without error") } } + +// The portal's initial email (issue 347): its header only when set, never in the query, and the +// relay's answer and login_kind parsed. +func TestFetchCentralizedConfigPollCarriesTheInitialEmail(t *testing.T) { + var header, query string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + header, query = r.Header.Get("X-Proxsave-Account-Initial-Email"), r.URL.RawQuery + _, _ = w.Write([]byte(`{"alive_ping_url":"a","backup_ping_url":"b","login_kind":"portal",` + + `"account_initial_email":{"state":"rejected","reason":"unconfirmed"}}`)) + })) + defer srv.Close() + cfg, _, err := FetchCentralizedConfigPoll(context.Background(), nil, srv.URL, "1", "s", nil, "warning", "", "operator@example.org", PollLog{}) + if err != nil || header != "operator@example.org" || strings.Contains(query, "example.org") { + t.Fatalf("err=%v header=%q query=%q", err, header, query) + } + ack := cfg.AccountInitialEmail + if ack == nil || ack.State != "rejected" || ack.Reason != "unconfirmed" || !ack.Final() || cfg.LoginKind != LoginKindPortal { + t.Fatalf("ack=%+v login_kind=%q", ack, cfg.LoginKind) + } + if _, _, err := FetchCentralizedConfigPoll(context.Background(), nil, srv.URL, "1", "s", nil, "warning", "", "", PollLog{}); err != nil || header != "" { + t.Fatalf("without the address: err=%v header=%q", err, header) + } +} + +func TestAccountInitialEmailAckFinal(t *testing.T) { + for _, c := range []struct { + ack *AccountInitialEmailAck + want bool + }{ + {nil, false}, + {&AccountInitialEmailAck{State: "pending"}, false}, + {&AccountInitialEmailAck{State: "pending", Reason: "limit"}, false}, + {&AccountInitialEmailAck{State: "applied"}, true}, + {&AccountInitialEmailAck{State: "rejected", Reason: "invalid"}, true}, + {&AccountInitialEmailAck{State: "something_new"}, false}, + } { + if got := c.ack.Final(); got != c.want { + t.Errorf("%+v: Final()=%t", c.ack, got) + } + } +} diff --git a/internal/orchestrator/healthcheck_setup_check.go b/internal/orchestrator/healthcheck_setup_check.go index 4c08363d..613f4ab9 100644 --- a/internal/orchestrator/healthcheck_setup_check.go +++ b/internal/orchestrator/healthcheck_setup_check.go @@ -33,7 +33,10 @@ type HealthcheckCheckResult struct { PortalURL string PortalLogin string PasswordSet *bool - Reachable bool + // LoginKind is the server's login_kind: health.LoginKindPortal when LoginURL is the portal's + // sign-in page (the operator owns the project), "" for a single-use magic-link. + LoginKind string + Reachable bool Daemon health.Diagnosis // daemon liveness/transmission diagnosed from the status file DaemonRead bool // false only if the status file existed but could not be read @@ -135,6 +138,7 @@ func CheckHealthcheckConnection(ctx context.Context, serverAPIHost, serverID, ba res.PortalURL = cfg.PortalURL res.PortalLogin = cfg.PortalLogin res.PasswordSet = cfg.PasswordSet + res.LoginKind = cfg.LoginKind if cfg.AliveURL != "" { if perr := healthcheckSetupPing(ctx, cfg.AliveURL); perr != nil { res.Err = perr diff --git a/internal/orchestrator/healthcheck_setup_classify.go b/internal/orchestrator/healthcheck_setup_classify.go index 8794eaad..4cc5622f 100644 --- a/internal/orchestrator/healthcheck_setup_classify.go +++ b/internal/orchestrator/healthcheck_setup_classify.go @@ -44,8 +44,13 @@ type HealthcheckSetupState struct { // clear TrustedLoginURL, the identity SanitizePortalLogin. PortalURL string PortalLogin string - Verified bool // provisioning ready AND monitor reachable -> Continue/return allowed - Fatal bool // another check cannot help -> do NOT offer Check again + // LoginIsPortalPage says LoginURL is the portal's sign-in page, not a single-use link: the + // operator owns the project and signs in with their email (issue 347). LoginPasswordSet says + // the server confirmed they also have a password. Both false for a magic-link. + LoginIsPortalPage bool + LoginPasswordSet bool + Verified bool // provisioning ready AND monitor reachable -> Continue/return allowed + Fatal bool // another check cannot help -> do NOT offer Check again } // ClassifyHealthcheckSetupResult maps a HealthcheckCheckResult to display state. The @@ -61,6 +66,10 @@ func ClassifyHealthcheckSetupResult(res HealthcheckCheckResult) HealthcheckSetup // trust/sanitize gates) lives in serverbot.TrustedPortalFallback, shared with the // run epilogue's storePortalFallback so the two surfaces cannot drift. st.PortalURL, st.PortalLogin = serverbot.TrustedPortalFallback(res.PortalURL, res.PortalLogin, res.PasswordSet, defaultServerAPIHost) + if st.LoginURL != "" && res.LoginKind == health.LoginKindPortal { + st.LoginIsPortalPage = true + st.LoginPasswordSet = res.PasswordSet != nil && *res.PasswordSet + } // 1) Hard provisioning blockers: monitoring cannot operate until fixed -> override. switch { @@ -242,3 +251,13 @@ func ClassifyHealthcheckSetupSkip(res HealthcheckSetupBootstrap) HealthcheckSetu } return st } + +// HealthcheckPortalPageSignIn is the line under the portal's sign-in page when the operator owns the +// project (HealthcheckSetupState.LoginIsPortalPage, issue 347): sign in with the email, with the +// password when there is one, else with an emailed link. Shared by the TUI and --install --cli. +func HealthcheckPortalPageSignIn(passwordSet bool) string { + if passwordSet { + return "Sign in with your email and the password you set." + } + return `Sign in with your email: use "Email Me a Link" on the sign-in page.` +} diff --git a/internal/orchestrator/healthcheck_setup_test.go b/internal/orchestrator/healthcheck_setup_test.go index 3c2b1e3e..1d882c85 100644 --- a/internal/orchestrator/healthcheck_setup_test.go +++ b/internal/orchestrator/healthcheck_setup_test.go @@ -487,3 +487,26 @@ func TestClassifyHealthcheckBehindDefaultsABlankReason(t *testing.T) { t.Fatalf("BEHIND message = %q, want it to contain %q", msg, want) } } + +// The portal's sign-in page (issue 347): marked only with login_kind portal and a trusted URL. +func TestClassifyHealthcheckSetupResultPortalPage(t *testing.T) { + yes, no := true, false + page := "https://hc.proxsave.dev/accounts/login/" + for _, c := range []struct { + name string + res HealthcheckCheckResult + page, passwordSet bool + }{ + {"portal page without password", HealthcheckCheckResult{Reachable: true, LoginURL: page, LoginKind: health.LoginKindPortal, PasswordSet: &no}, true, false}, + {"portal page with password", HealthcheckCheckResult{Reachable: true, LoginURL: page, LoginKind: health.LoginKindPortal, PasswordSet: &yes}, true, true}, + {"portal page, password unknown", HealthcheckCheckResult{Reachable: true, LoginURL: page, LoginKind: health.LoginKindPortal}, true, false}, + {"magic link", HealthcheckCheckResult{Reachable: true, LoginURL: "https://hc.proxsave.dev/l/abc", PasswordSet: &no}, false, false}, + {"unknown kind", HealthcheckCheckResult{Reachable: true, LoginURL: page, LoginKind: "other"}, false, false}, + {"foreign host dropped", HealthcheckCheckResult{Reachable: true, LoginURL: "https://evil.example/accounts/login/", LoginKind: health.LoginKindPortal}, false, false}, + } { + st := ClassifyHealthcheckSetupResult(c.res) + if st.LoginIsPortalPage != c.page || st.LoginPasswordSet != c.passwordSet { + t.Errorf("%s: page=%t password=%t", c.name, st.LoginIsPortalPage, st.LoginPasswordSet) + } + } +} diff --git a/internal/serverbot/client.go b/internal/serverbot/client.go index f77b49c0..aacd6240 100644 --- a/internal/serverbot/client.go +++ b/internal/serverbot/client.go @@ -22,6 +22,8 @@ const ( serverAuthHeader = "X-Server-Auth" provisionHeader = "X-Proxsave-Provision" notifyIDHeader = "X-Notify-Id" + // initialEmailHeader carries HEALTHCHECK_ACCOUNT_INITIAL_EMAIL to the relay (issue 347). + initialEmailHeader = "X-Proxsave-Account-Initial-Email" defaultTimeout = 5 * time.Second defaultMaxBytes = 8192 @@ -135,6 +137,9 @@ func (c *Client) Do(ctx context.Context, req Request) (*Response, error) { if req.NotifyID != "" { httpReq.Header.Set(notifyIDHeader, req.NotifyID) } + if req.AccountInitialEmail != "" { + httpReq.Header.Set(initialEmailHeader, req.AccountInitialEmail) + } if req.Body != nil { httpReq.Header.Set("Content-Type", "application/json") } diff --git a/internal/serverbot/client_test.go b/internal/serverbot/client_test.go index 089ae953..36b9b765 100644 --- a/internal/serverbot/client_test.go +++ b/internal/serverbot/client_test.go @@ -252,3 +252,30 @@ func TestDoLoggerNeverEmitsBodyOrSecret(t *testing.T) { t.Errorf("expected a debug line naming the path, got: %s", out) } } + +// The portal's initial email (issue 347) rides its header only when set, and is never logged. +func TestDoInitialEmailHeaderOnlyWhenSetAndNeverLogged(t *testing.T) { + var got []string + var buf bytes.Buffer + lg := logging.New(types.LogLevelDebug, false) + lg.SetOutput(&buf) + c := New("https://bot.example", &http.Client{Transport: rtFunc(func(r *http.Request) (*http.Response, error) { + got = append(got, r.Header.Get("X-Proxsave-Account-Initial-Email")) + if strings.Contains(r.URL.String(), "example.org") { + t.Errorf("the address is in the URL: %s", r.URL) + } + return stubResp(200, "{}"), nil + })}, lg) + for _, email := range []string{"operator@example.org", ""} { + if _, err := c.Do(context.Background(), Request{Path: "/api/healthcheck/config", Secret: "s", + AccountInitialEmail: email, LogOperation: "poll", LogURLDetail: "frequency=daily"}); err != nil { + t.Fatalf("Do: %v", err) + } + } + if len(got) != 2 || got[0] != "operator@example.org" || got[1] != "" { + t.Fatalf("headers = %q", got) + } + if strings.Contains(buf.String(), "example.org") { + t.Fatalf("the address reached the log:\n%s", buf.String()) + } +} diff --git a/internal/serverbot/request.go b/internal/serverbot/request.go index c6bea40f..d282cfcc 100644 --- a/internal/serverbot/request.go +++ b/internal/serverbot/request.go @@ -13,15 +13,19 @@ import ( // Client adds no endpoint knowledge. Secret == "" omits X-Server-Auth (the pre-auth // get-chat-id call). Provision and NotifyID are caller-gated and never defaulted on. type Request struct { - Method string // http.MethodGet / http.MethodPost ("" -> GET) - Path string // "/api/notify", "/api/get-chat-id", ... (leading slash, no host) - Query url.Values // .Encode() escapes every value; caller sets server_id etc. - Secret string // != "" -> X-Server-Auth; "" -> header omitted - Provision bool // true -> X-Proxsave-Provision:"1" - NotifyID string // != "" -> X-Notify-Id (serverbot never mints one) - Body any // != nil -> json.Marshal + Content-Type: application/json - Timeout time.Duration // per-request ctx cap; 0 -> 5s - MaxBytes int64 // response read cap; 0 -> 8192 + Method string // http.MethodGet / http.MethodPost ("" -> GET) + Path string // "/api/notify", "/api/get-chat-id", ... (leading slash, no host) + Query url.Values // .Encode() escapes every value; caller sets server_id etc. + Secret string // != "" -> X-Server-Auth; "" -> header omitted + Provision bool // true -> X-Proxsave-Provision:"1" + NotifyID string // != "" -> X-Notify-Id (serverbot never mints one) + // AccountInitialEmail != "" -> X-Proxsave-Account-Initial-Email: the operator's address for + // the monitoring portal (issue 347). A header, never the query: the relay's access log + // records the query. Never logged here either. + AccountInitialEmail string + Body any // != nil -> json.Marshal + Content-Type: application/json + Timeout time.Duration // per-request ctx cap; 0 -> 5s + MaxBytes int64 // response read cap; 0 -> 8192 // LogOperation is the DEBUG operation the call's transport stages are logged under, e.g. // "notifications init"; "" -> "serverbot". Only read when the Client has a logger. LogOperation string diff --git a/internal/ui/flows/install/healthcheck.go b/internal/ui/flows/install/healthcheck.go index c790d3dc..71d19f7d 100644 --- a/internal/ui/flows/install/healthcheck.go +++ b/internal/ui/flows/install/healthcheck.go @@ -89,6 +89,7 @@ func RunHealthcheckSetup(ctx context.Context, session *shell.Session, baseDir, c statusExplanation := "Choose Check to verify the monitoring connection." statusLevel := orchestrator.HealthcheckSetupLevelNeutral // pre-check: yellow, no symbol magicLink := "" + linkPortalPage, linkPasswordSet := false, false portalURL, portalLogin := "", "" var sensors []health.SensorRow // per-sensor rows, populated after each Check var notifyBlock *healthcheckNotifyBlock // NOTIFY_ON lines, populated after each Check @@ -152,9 +153,11 @@ func RunHealthcheckSetup(ctx context.Context, session *shell.Session, baseDir, c // and already prefers the portal for the current check. if link := strings.TrimSpace(st.LoginURL); link != "" { magicLink = link + linkPortalPage, linkPasswordSet = st.LoginIsPortalPage, st.LoginPasswordSet result.MagicLinkSeen = true } else if portal := strings.TrimSpace(st.PortalURL); portal != "" { magicLink = "" + linkPortalPage, linkPasswordSet = false, false portalURL = portal portalLogin = strings.TrimSpace(st.PortalLogin) } @@ -174,7 +177,7 @@ func RunHealthcheckSetup(ctx context.Context, session *shell.Session, baseDir, c } } } - prompt := buildHealthcheckPrompt(selfMode, magicLink, portalURL, portalLogin, statusKeyword, statusExplanation, statusLevel, sensors, notifyBlock) + prompt := buildHealthcheckPrompt(selfMode, magicLink, portalURL, portalLogin, statusKeyword, statusExplanation, statusLevel, sensors, notifyBlock, portalLinkCopy{portalPage: linkPortalPage, passwordSet: linkPasswordSet}) items := make([]components.SelectorItem[healthcheckAction], 0, 3) if !result.LastFatal && (result.Verified || result.CheckAttempts < orchestrator.HealthcheckSetupMaxVerificationAttempts) { @@ -233,7 +236,14 @@ func RunHealthcheckSetup(ctx context.Context, session *shell.Session, baseDir, c // click. portalURL/portalLogin are the fallback the server sends instead once the user // has their own password. Every value is already sanitized upstream // (serverbot.TrustedLoginURL / SanitizePortalLogin). -func buildHealthcheckPrompt(selfMode bool, magicLink, portalURL, portalLogin, keyword, explanation string, level orchestrator.HealthcheckSetupLevel, sensors []health.SensorRow, notify *healthcheckNotifyBlock) string { +// portalLinkCopy says how the box presents magicLink: the zero value is a single-use magic-link; +// portalPage is the portal's sign-in page for an operator who owns the project (issue 347). +type portalLinkCopy struct { + portalPage bool + passwordSet bool +} + +func buildHealthcheckPrompt(selfMode bool, magicLink, portalURL, portalLogin, keyword, explanation string, level orchestrator.HealthcheckSetupLevel, sensors []health.SensorRow, notify *healthcheckNotifyBlock, linkCopy portalLinkCopy) string { var b strings.Builder b.WriteString(theme.Text.Render("Backup monitoring (healthchecks) is enabled for this host.")) b.WriteString("\n") @@ -248,7 +258,18 @@ func buildHealthcheckPrompt(selfMode bool, magicLink, portalURL, portalLogin, ke } b.WriteString("\n\n") - if magicLink != "" { + if magicLink != "" && linkCopy.portalPage { + box := lipgloss.NewStyle(). + Border(lipgloss.RoundedBorder()). + BorderForeground(theme.Orange). + Padding(0, 1). + Render(theme.Subtle.Render("Your monitoring portal:") + + "\n" + theme.Emphasis.Render(magicLink)) + b.WriteString(box) + b.WriteString("\n") + b.WriteString(theme.Subtle.Render(orchestrator.HealthcheckPortalPageSignIn(linkCopy.passwordSet))) + b.WriteString("\n\n") + } else if magicLink != "" { box := lipgloss.NewStyle(). Border(lipgloss.RoundedBorder()). BorderForeground(theme.Orange). diff --git a/internal/ui/flows/install/healthcheck_notify_test.go b/internal/ui/flows/install/healthcheck_notify_test.go index dbac6455..1825415f 100644 --- a/internal/ui/flows/install/healthcheck_notify_test.go +++ b/internal/ui/flows/install/healthcheck_notify_test.go @@ -84,7 +84,7 @@ func TestHealthcheckNotifyBlockTexts(t *testing.T) { func TestHealthcheckPromptPlacesTheNotificationsBlockAboveTheSensors(t *testing.T) { sensors := []health.SensorRow{{Name: "proxsave-alive", State: "ok", Level: health.SensorOk}} nb := &healthcheckNotifyBlock{Setting: "NOTIFY_ON=failure", Current: "always (Healthchecks not configured)"} - v := ansi.Strip(buildHealthcheckPrompt(false, "", "", "", "WORKING", "It is reporting.", orchestrator.HealthcheckSetupLevelOk, sensors, nb)) + v := ansi.Strip(buildHealthcheckPrompt(false, "", "", "", "WORKING", "It is reporting.", orchestrator.HealthcheckSetupLevelOk, sensors, nb, portalLinkCopy{})) want := "It is reporting.\n\nNotifications:\nSetting: NOTIFY_ON=failure\nCurrent: always (Healthchecks not configured)\n\nSensors:\n" if !strings.Contains(v, want) { @@ -96,7 +96,7 @@ func TestHealthcheckPromptPlacesTheNotificationsBlockAboveTheSensors(t *testing. } } - if v := ansi.Strip(buildHealthcheckPrompt(false, "", "", "", "NOT CHECKED", "Choose Check.", orchestrator.HealthcheckSetupLevelNeutral, nil, nil)); strings.Contains(v, "Notifications:") { + if v := ansi.Strip(buildHealthcheckPrompt(false, "", "", "", "NOT CHECKED", "Choose Check.", orchestrator.HealthcheckSetupLevelNeutral, nil, nil, portalLinkCopy{})); strings.Contains(v, "Notifications:") { t.Fatalf("no block before a Check:\n%s", v) } } diff --git a/internal/ui/flows/install/healthcheck_portal_copy_test.go b/internal/ui/flows/install/healthcheck_portal_copy_test.go new file mode 100644 index 00000000..e1af603c --- /dev/null +++ b/internal/ui/flows/install/healthcheck_portal_copy_test.go @@ -0,0 +1,58 @@ +package install + +import ( + "strings" + "testing" + + "github.com/charmbracelet/x/ansi" + "github.com/tis24dev/proxsave/internal/orchestrator" +) + +// The portal box as it was before the portal sign-in page existed (issue 347): a magic-link, and the +// sign-in page once a password is set. These renders must not change. +const ( + boxMagicLink = `╭──────────────────────────────────────────────────────╮ +│ Your monitoring portal (single-use link, valid ~1h): │ +│ https://hc.proxsave.dev/l/abc │ +╰──────────────────────────────────────────────────────╯ +Open it to set a password and configure alert channels.` + boxPasswordFallback = `╭─────────────────────────────────────────╮ +│ Your monitoring portal: │ +│ https://hc.proxsave.dev/accounts/login/ │ +│ Login: 1@proxsave.dev │ +╰─────────────────────────────────────────╯ +Sign in with the password you set.` +) + +func TestPortalBoxOfTodayIsUnchanged(t *testing.T) { + magic := ansi.Strip(buildHealthcheckPrompt(false, "https://hc.proxsave.dev/l/abc", "", "", "WORKING", "ok", + orchestrator.HealthcheckSetupLevelOk, nil, nil, portalLinkCopy{})) + if !strings.Contains(magic, boxMagicLink) { + t.Fatalf("magic-link box changed:\n%s", magic) + } + fallback := ansi.Strip(buildHealthcheckPrompt(false, "", "https://hc.proxsave.dev/accounts/login/", "1@proxsave.dev", + "WORKING", "ok", orchestrator.HealthcheckSetupLevelOk, nil, nil, portalLinkCopy{})) + if !strings.Contains(fallback, boxPasswordFallback) { + t.Fatalf("password fallback box changed:\n%s", fallback) + } +} + +// The operator owns the project (issue 347): the sign-in page, with the approved sign-in line. +func TestPortalBoxForTheSignInPage(t *testing.T) { + page := "https://hc.proxsave.dev/accounts/login/" + box := `╭─────────────────────────────────────────╮ +│ Your monitoring portal: │ +│ https://hc.proxsave.dev/accounts/login/ │ +╰─────────────────────────────────────────╯ +` + for passwordSet, line := range map[bool]string{ + false: `Sign in with your email: use "Email Me a Link" on the sign-in page.`, + true: "Sign in with your email and the password you set.", + } { + v := ansi.Strip(buildHealthcheckPrompt(false, page, "", "", "WORKING", "ok", orchestrator.HealthcheckSetupLevelOk, + nil, nil, portalLinkCopy{portalPage: true, passwordSet: passwordSet})) + if !strings.Contains(v, box+line+"\n") || strings.Contains(v, "single-use") { + t.Errorf("password=%t:\n%s", passwordSet, v) + } + } +} diff --git a/internal/ui/flows/install/healthcheck_sensors_test.go b/internal/ui/flows/install/healthcheck_sensors_test.go index 87946a9f..702c17ff 100644 --- a/internal/ui/flows/install/healthcheck_sensors_test.go +++ b/internal/ui/flows/install/healthcheck_sensors_test.go @@ -20,7 +20,7 @@ func TestBuildHealthcheckPromptSensors(t *testing.T) { {Name: "proxsave-updates", Level: health.SensorError, State: "update available", Age: "1m ago"}, {Name: "proxsave-neutral", Level: health.SensorNeutral, State: "no data"}, } - v := buildHealthcheckPrompt(false, "", "", "", "WORKING", "It is reporting.", orchestrator.HealthcheckSetupLevelOk, sensors, nil) + v := buildHealthcheckPrompt(false, "", "", "", "WORKING", "It is reporting.", orchestrator.HealthcheckSetupLevelOk, sensors, nil, portalLinkCopy{}) plain := ansi.Strip(v) if !strings.Contains(plain, "Sensors:") { @@ -49,7 +49,7 @@ func TestBuildHealthcheckPromptSensors(t *testing.T) { } // No sensors -> no "Sensors:" block (pre-check state). - none := ansi.Strip(buildHealthcheckPrompt(false, "", "", "", "NOT CHECKED", "Choose Check.", orchestrator.HealthcheckSetupLevelNeutral, nil, nil)) + none := ansi.Strip(buildHealthcheckPrompt(false, "", "", "", "NOT CHECKED", "Choose Check.", orchestrator.HealthcheckSetupLevelNeutral, nil, nil, portalLinkCopy{})) if strings.Contains(none, "Sensors:") { t.Fatalf("no sensors must render no Sensors block:\n%s", none) } @@ -66,7 +66,7 @@ func TestBuildHealthcheckPromptSanitizesInjection(t *testing.T) { } v := buildHealthcheckPrompt(false, "", "", "", "UNREACHABLE", "probe failed: \x1b[2J\x07connection refused\x1b]0;evil\x07", - orchestrator.HealthcheckSetupLevelError, sensors, nil) + orchestrator.HealthcheckSetupLevelError, sensors, nil, portalLinkCopy{}) for _, bad := range []string{"\x1b]0;", "\x07", "\x9b", "\x1b[2J", "pwned", "evil"} { if strings.Contains(v, bad) { t.Fatalf("healthcheck prompt leaks injected sequence %q:\n%q", bad, v) diff --git a/internal/ui/flows/install/healthcheck_test.go b/internal/ui/flows/install/healthcheck_test.go index 1be5162a..c66d9205 100644 --- a/internal/ui/flows/install/healthcheck_test.go +++ b/internal/ui/flows/install/healthcheck_test.go @@ -82,7 +82,7 @@ func TestBuildHealthcheckPrompt(t *testing.T) { link := "https://hc.proxsave.dev/l/Nr4vAebz5b" // Ok level: green "✓ WORKING", the explanation on its own line, the link boxed. - v := buildHealthcheckPrompt(false, link, "", "", "WORKING", "It is reporting.", orchestrator.HealthcheckSetupLevelOk, nil, nil) + v := buildHealthcheckPrompt(false, link, "", "", "WORKING", "It is reporting.", orchestrator.HealthcheckSetupLevelOk, nil, nil, portalLinkCopy{}) if !strings.Contains(ansi.Strip(v), "✓ WORKING") { t.Fatalf("working keyword missing: %q", ansi.Strip(v)) } @@ -97,7 +97,7 @@ func TestBuildHealthcheckPrompt(t *testing.T) { } // Error level: red "✗ REJECTED". - f := buildHealthcheckPrompt(false, link, "", "", "REJECTED", "bad creds", orchestrator.HealthcheckSetupLevelError, nil, nil) + f := buildHealthcheckPrompt(false, link, "", "", "REJECTED", "bad creds", orchestrator.HealthcheckSetupLevelError, nil, nil, portalLinkCopy{}) if !strings.Contains(ansi.Strip(f), "✗ REJECTED") { t.Fatalf("error keyword missing: %q", ansi.Strip(f)) } @@ -106,7 +106,7 @@ func TestBuildHealthcheckPrompt(t *testing.T) { } // Warn level (a real post-check warning): yellow "⚠ NOT RUNNING" (with the triangle). - w := buildHealthcheckPrompt(false, link, "", "", "NOT RUNNING", "daemon down", orchestrator.HealthcheckSetupLevelWarn, nil, nil) + w := buildHealthcheckPrompt(false, link, "", "", "NOT RUNNING", "daemon down", orchestrator.HealthcheckSetupLevelWarn, nil, nil, portalLinkCopy{}) if !strings.Contains(ansi.Strip(w), "⚠ NOT RUNNING") { t.Fatalf("warn keyword missing: %q", ansi.Strip(w)) } @@ -115,7 +115,7 @@ func TestBuildHealthcheckPrompt(t *testing.T) { } // Neutral level (pre-check): yellow "NOT CHECKED" with NO triangle - like upgrade/telegram. - nn := buildHealthcheckPrompt(false, link, "", "", "NOT CHECKED", "Choose Check.", orchestrator.HealthcheckSetupLevelNeutral, nil, nil) + nn := buildHealthcheckPrompt(false, link, "", "", "NOT CHECKED", "Choose Check.", orchestrator.HealthcheckSetupLevelNeutral, nil, nil, portalLinkCopy{}) if !strings.Contains(ansi.Strip(nn), "NOT CHECKED") { t.Fatalf("neutral keyword missing: %q", ansi.Strip(nn)) } @@ -127,7 +127,7 @@ func TestBuildHealthcheckPrompt(t *testing.T) { } // No link -> no box; the explanation still renders verbatim. - n := ansi.Strip(buildHealthcheckPrompt(false, "", "", "", "NOT CHECKED", "Choose Check.", orchestrator.HealthcheckSetupLevelNeutral, nil, nil)) + n := ansi.Strip(buildHealthcheckPrompt(false, "", "", "", "NOT CHECKED", "Choose Check.", orchestrator.HealthcheckSetupLevelNeutral, nil, nil, portalLinkCopy{})) if strings.Contains(n, "╭") { t.Fatalf("no link must render no box: %q", n) } diff --git a/internal/whatsnew/registry.go b/internal/whatsnew/registry.go index 1288bd71..81139f46 100644 --- a/internal/whatsnew/registry.go +++ b/internal/whatsnew/registry.go @@ -313,6 +313,12 @@ var notes = []Note{ "This release includes a collection of fixes and corrections.", }, }, + { + Version: "0.47.0", + Lines: []string{ + "You can now set an email for your monitoring portal, so you can sign in and set a password there.", + }, + }, } // LookupNotes returns the notes for versions in the half-open range (from, to], ascending by From f2732afc4752a257fb2c20ea86766cc67890bb57 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Fri, 13 Nov 2026 13:49:30 +0100 Subject: [PATCH 41/44] test(healthchecks): pin that the setup check keeps the relay's login_kind --- internal/orchestrator/healthcheck_setup_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/orchestrator/healthcheck_setup_test.go b/internal/orchestrator/healthcheck_setup_test.go index 1d882c85..9ca9dd70 100644 --- a/internal/orchestrator/healthcheck_setup_test.go +++ b/internal/orchestrator/healthcheck_setup_test.go @@ -213,6 +213,18 @@ func TestCheckHealthcheckConnection(t *testing.T) { } }) + t.Run("the portal sign-in page keeps its login_kind", func(t *testing.T) { + healthcheckSetupFetch = func(ctx context.Context, client *http.Client, host, id, secret string, includeLogin bool) (health.CentralizedConfig, error) { + return health.CentralizedConfig{AliveURL: "https://a", BackupURL: "https://b", + LoginURL: "https://hc.proxsave.dev/accounts/login/", LoginKind: health.LoginKindPortal}, nil + } + healthcheckSetupPing = func(ctx context.Context, aliveURL string) error { return nil } + res := CheckHealthcheckConnection(context.Background(), "https://h", "id", t.TempDir(), time.Minute) + if res.LoginKind != health.LoginKindPortal { + t.Fatalf("login_kind lost: %+v", res) + } + }) + t.Run("fetch error propagates, no login", func(t *testing.T) { healthcheckSetupFetch = func(ctx context.Context, client *http.Client, host, id, secret string, includeLogin bool) (health.CentralizedConfig, error) { return health.CentralizedConfig{}, health.ErrHCNotReady From 8464d06616d30d9b5d1035e40f9077188a097f7c Mon Sep 17 00:00:00 2001 From: tis24dev Date: Fri, 13 Nov 2026 13:54:13 +0100 Subject: [PATCH 42/44] fix(healthchecks): send the initial email at daemon start too At start the daemon polls the relay only through the schedule negotiation, whose attempts never carry the address, so the first block appeared only before the next scheduled run (found on pve1 in the live test). A poll of its own now follows the start blocks while an address is due; none goes out otherwise. The new start test fails without this change. --- cmd/proxsave/daemon_initial_email_test.go | 43 +++++++++++++++++++++ cmd/proxsave/daemon_schedule_negotiation.go | 8 ++++ 2 files changed, 51 insertions(+) diff --git a/cmd/proxsave/daemon_initial_email_test.go b/cmd/proxsave/daemon_initial_email_test.go index 157c5944..89d58ea5 100644 --- a/cmd/proxsave/daemon_initial_email_test.go +++ b/cmd/proxsave/daemon_initial_email_test.go @@ -304,3 +304,46 @@ func TestBeforeARunAChangedAddressIsSentAfterTheNotifyBlock(t *testing.T) { t.Fatalf("order:\n%s", strings.Join(lines, "\n")) } } + +// At daemon start the schedule negotiation does the polling; the address rides its own poll after +// the start blocks (found live on pve1: the start path never used the poll that carried it). +func TestAtStartTheAddressGoesOutAfterTheStartBlocks(t *testing.T) { + var mu sync.Mutex + var headers []string + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := r.Header.Get("X-Proxsave-Account-Initial-Email") + mu.Lock() + headers = append(headers, h) + mu.Unlock() + body := relayBody(r.URL.Query(), true, true) + if h != "" { + body["account_initial_email"] = map[string]string{"state": "pending"} + } + _ = json.NewEncoder(w).Encode(body) + })) + t.Cleanup(server.Close) + d := negotiationDaemon(t, server.URL, "weekly", "mon", "1", "02:00") + d.cfg.HealthcheckAccountInitialEmail = testInitialEmail + got := strings.Join(startLines(t, d), "\n") + block := "INFO Applying monitoring portal email...\nINFO Waiting for the confirmation link sent by mail\n" + + "INFO ⚠ Monitoring portal email: not applied" + i, j := strings.Index(got, "INFO ✓ Notify level: applied"), strings.Index(got, block) + if i < 0 || j < i { + t.Fatalf("block missing or before the notify level block:\n%s", got) + } + mu.Lock() + defer mu.Unlock() + if len(headers) != 2 || headers[0] != "" || headers[1] != testInitialEmail { + t.Fatalf("headers = %q: want the schedule attempt without it, then its own poll", headers) + } +} + +// Without an address due the start sends nothing more than before. +func TestAtStartNoExtraPollWithoutAnAddress(t *testing.T) { + relay := newScheduleRelay(t, nil) + d := negotiationDaemon(t, relay.server.URL, "weekly", "mon", "1", "02:00") + startLines(t, d) + if polls := relay.sent(); len(polls) != 1 { + t.Fatalf("polls = %d, want 1", len(polls)) + } +} diff --git a/cmd/proxsave/daemon_schedule_negotiation.go b/cmd/proxsave/daemon_schedule_negotiation.go index 86b1c92f..94c279d6 100644 --- a/cmd/proxsave/daemon_schedule_negotiation.go +++ b/cmd/proxsave/daemon_schedule_negotiation.go @@ -376,6 +376,14 @@ func (d *daemon) startScheduleNegotiation(ctx context.Context, reading scheduleR if !haveGood { d.logPingURLsNotRefreshed(last, why) } + + // The portal's initial email (issue 347) rides its own poll, after the start blocks, only while + // one is due: the schedule attempts above never carry it. + if d.wantedInitialEmail() != "" { + p := d.pollCentralized(ctx, "", "") + d.afterFailedPoll(p) + d.logInitialEmailOutcome(p) + } return sent } From dd2677887860eaa334766eff73387179bebba8f1 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Fri, 13 Nov 2026 14:09:46 +0100 Subject: [PATCH 43/44] fix(healthchecks): line-per-fact texts for the portal sign-in line and the docs Reviewed with the maintainer on 2026-11-13: under the portal's sign-in page the line is now "Sign in: your email, then "Email Me a Link"" or "Sign in: your email and password"; the HEALTHCHECKS.md "Your email." paragraph becomes three steps and four "Label: fact" points. --- cmd/proxsave/healthcheck_portal_cli_test.go | 4 ++-- docs/HEALTHCHECKS.md | 21 ++++++++++++------- .../healthcheck_setup_classify.go | 4 ++-- .../install/healthcheck_portal_copy_test.go | 4 ++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cmd/proxsave/healthcheck_portal_cli_test.go b/cmd/proxsave/healthcheck_portal_cli_test.go index 55cf0ad2..44df0558 100644 --- a/cmd/proxsave/healthcheck_portal_cli_test.go +++ b/cmd/proxsave/healthcheck_portal_cli_test.go @@ -33,8 +33,8 @@ func TestPrintHealthcheckPortalOfTodayIsUnchanged(t *testing.T) { func TestPrintHealthcheckPortalForTheSignInPage(t *testing.T) { page := "https://hc.proxsave.dev/accounts/login/" for passwordSet, line := range map[bool]string{ - false: `Sign in with your email: use "Email Me a Link" on the sign-in page.`, - true: "Sign in with your email and the password you set.", + false: `Sign in: your email, then "Email Me a Link"`, + true: "Sign in: your email and password", } { st := orchestrator.HealthcheckSetupState{LoginURL: page, LoginIsPortalPage: true, LoginPasswordSet: passwordSet} want := "\nYour monitoring portal:\n " + page + "\n" + line + "\n\n" diff --git a/docs/HEALTHCHECKS.md b/docs/HEALTHCHECKS.md index d03947bc..cb389033 100644 --- a/docs/HEALTHCHECKS.md +++ b/docs/HEALTHCHECKS.md @@ -321,14 +321,19 @@ address**, not a username. Sign in there with the password you chose. **Your email.** The account the server makes for a host has an address that receives no mail, and the portal mails a code before it lets you set a password or change the email. -Set `HEALTHCHECK_ACCOUNT_INITIAL_EMAIL` in `backup.env` to your address: the daemon sends -it to the server, which mails a confirmation link to it, valid 15 minutes. Press Confirm -on that page and this host's checks move into the portal account of that address, made -then if you have none. Hosts with the same address share one account, one project each. -From then on you sign in with your email, by password or with "Email Me a Link", and -change the address from the portal: the variable is not used again for this host. The -server sends at most three mails for one address; after that only a different address -starts again. +To use your own address: + +1. Set `HEALTHCHECK_ACCOUNT_INITIAL_EMAIL` in `backup.env`. +2. The daemon sends it to the server, which mails a confirmation link (valid 15 minutes). +3. Press Confirm: this host's checks move into the portal account of that address, + made if missing. + +Then: + +- Same address on several hosts: one account, one project per host. +- Sign in: your email, then "Email Me a Link" or your password. +- Change the address: from the portal; the variable is not used again for this host. +- Mails per address: at most three; after that only a different address starts again. The exact wording differs a little per surface. The log epilogue uses `Healthchecks Portal:` for both states, the link and the sign-in address alike, and diff --git a/internal/orchestrator/healthcheck_setup_classify.go b/internal/orchestrator/healthcheck_setup_classify.go index 4cc5622f..e3afc3d2 100644 --- a/internal/orchestrator/healthcheck_setup_classify.go +++ b/internal/orchestrator/healthcheck_setup_classify.go @@ -257,7 +257,7 @@ func ClassifyHealthcheckSetupSkip(res HealthcheckSetupBootstrap) HealthcheckSetu // password when there is one, else with an emailed link. Shared by the TUI and --install --cli. func HealthcheckPortalPageSignIn(passwordSet bool) string { if passwordSet { - return "Sign in with your email and the password you set." + return "Sign in: your email and password" } - return `Sign in with your email: use "Email Me a Link" on the sign-in page.` + return `Sign in: your email, then "Email Me a Link"` } diff --git a/internal/ui/flows/install/healthcheck_portal_copy_test.go b/internal/ui/flows/install/healthcheck_portal_copy_test.go index e1af603c..c5eb525e 100644 --- a/internal/ui/flows/install/healthcheck_portal_copy_test.go +++ b/internal/ui/flows/install/healthcheck_portal_copy_test.go @@ -46,8 +46,8 @@ func TestPortalBoxForTheSignInPage(t *testing.T) { ╰─────────────────────────────────────────╯ ` for passwordSet, line := range map[bool]string{ - false: `Sign in with your email: use "Email Me a Link" on the sign-in page.`, - true: "Sign in with your email and the password you set.", + false: `Sign in: your email, then "Email Me a Link"`, + true: "Sign in: your email and password", } { v := ansi.Strip(buildHealthcheckPrompt(false, page, "", "", "WORKING", "ok", orchestrator.HealthcheckSetupLevelOk, nil, nil, portalLinkCopy{portalPage: true, passwordSet: passwordSet})) From f8faf9efab4e0a075a7899261a1388229c3bded7 Mon Sep 17 00:00:00 2001 From: tis24dev Date: Fri, 13 Nov 2026 15:06:02 +0100 Subject: [PATCH 44/44] feat(install): ask the HC initial account email with the ProxSave HC Server Issue 347, decided with the maintainer on 2026-11-13. The install wizard asks HEALTHCHECK_ACCOUNT_INITIAL_EMAIL right under the Healthchecks mode, and it is required with the centralized mode: - TUI: a row "HC initial account email" under "Healthchecks", always shown, editable only with the daemon and the ProxSave HC Server (dimmed otherwise, like Notify level with Healthchecks off), prefilled with the stored value; - --install --cli: "HC initial account email []: " right after "centralized", repeated until the answer is usable; - messages: "Required with ProxSave HC Server" for an empty value, "Not a usable email address" for a minimal form check (one "@", a dotted domain, printable characters without spaces); the relay checks it again; - the value is written only with the centralized mode; with off, your own server or the cron engine the stored value is kept. The upgrade merge adds the variable empty to an existing backup.env, under HEALTHCHECK_MODE with its template comment, and keeps every operator value: a new test measures it. Install tests now type the address where the form or the CLI asks it; the fresh_decline_all goldens gain the question and the written line. GitNexus: CollectWizardData, ApplyInstallData and DeriveInstallWizardPrefill HIGH risk (install flows), told to the maintainer before the edit. Tests: go test ./... passes with umask 022; 3 targeted mutations caught. --- cmd/proxsave/install.go | 28 ++++++++++ cmd/proxsave/install_characterization_test.go | 3 +- cmd/proxsave/install_test.go | 25 ++++++++- .../fresh_decline_all.env | 2 +- .../fresh_decline_all.transcript | 2 +- internal/config/upgrade_initial_email_test.go | 43 +++++++++++++++ internal/installer/install_data.go | 37 ++++++++++++- .../install_data_initial_email_test.go | 53 +++++++++++++++++++ internal/ui/flows/install/install.go | 15 +++++- internal/ui/flows/install/install_test.go | 37 ++++++++----- 10 files changed, 226 insertions(+), 19 deletions(-) create mode 100644 internal/config/upgrade_initial_email_test.go create mode 100644 internal/installer/install_data_initial_email_test.go diff --git a/cmd/proxsave/install.go b/cmd/proxsave/install.go index bd8c1560..e71c58f3 100644 --- a/cmd/proxsave/install.go +++ b/cmd/proxsave/install.go @@ -727,6 +727,13 @@ func collectInstallWizardDataCLI(ctx context.Context, reader *bufio.Reader, prom return nil, err } data.HealthcheckMode = hcMode + if hcMode == "centralized" { + email, err := promptHealthcheckInitialEmail(ctx, reader, prefill.HealthcheckAccountInitialEmail) + if err != nil { + return nil, err + } + data.HealthcheckAccountInitialEmail = email + } } // Notify level (NOTIFY_ON): asked only with a Healthchecks monitor. Without one any level below @@ -1570,3 +1577,24 @@ func clearImmutableAttributesWithContext(ctx context.Context, target string, boo } return nil } + +// promptHealthcheckInitialEmail asks the portal's initial email (issue 347), required with the +// centralized mode: Enter keeps the stored value, and the question repeats until the address passes +// installer.ValidateHealthcheckInitialEmail, whose message is printed first. +func promptHealthcheckInitialEmail(ctx context.Context, reader *bufio.Reader, def string) (string, error) { + for { + raw, err := promptOptional(ctx, reader, fmt.Sprintf("HC initial account email [%s]: ", def)) + if err != nil { + return "", err + } + value := strings.TrimSpace(raw) + if value == "" { + value = strings.TrimSpace(def) + } + if verr := installer.ValidateHealthcheckInitialEmail(value); verr != nil { + fmt.Println(verr.Error()) + continue + } + return value, nil + } +} diff --git a/cmd/proxsave/install_characterization_test.go b/cmd/proxsave/install_characterization_test.go index 3707eaae..7080e78c 100644 --- a/cmd/proxsave/install_characterization_test.go +++ b/cmd/proxsave/install_characterization_test.go @@ -101,7 +101,8 @@ func TestInstallWizardCharacterization_FreshDeclineAll(t *testing.T) { // toggle declined via bare Enter; default scheduler (daemon), default // healthchecks mode (centralized, daemon-only prompt), default notify level // (warning, asked only with a monitor), + run-at accepted. - run := runWizardCharacterization(t, "", strings.Repeat("\n", 11)) + // The HC initial account email is required with centralized: typed after the mode answer. + run := runWizardCharacterization(t, "", strings.Repeat("\n", 8)+"ops@example.org\n"+strings.Repeat("\n", 3)) if run.err != nil { t.Fatalf("wizard error: %v", run.err) } diff --git a/cmd/proxsave/install_test.go b/cmd/proxsave/install_test.go index b543a9bc..d9ea32ce 100644 --- a/cmd/proxsave/install_test.go +++ b/cmd/proxsave/install_test.go @@ -800,7 +800,8 @@ func TestRunConfigWizardCLIReturnsCronSchedule(t *testing.T) { // 6 toggle declines, empty scheduler-engine answer (defaults to daemon on a // fresh install), empty healthcheck-mode answer (daemon-only prompt, defaults // to centralized), empty notify-level answer (defaults to warning), then the run-at time. - reader := bufio.NewReader(strings.NewReader("n\nn\nn\nn\nn\nn\n\n\n\n\n03:15\n")) + // The HC initial account email follows the centralized mode answer (required there). + reader := bufio.NewReader(strings.NewReader("n\nn\nn\nn\nn\nn\n\n\nops@example.org\n\n\n03:15\n")) var result installConfigResult var err error @@ -1522,3 +1523,25 @@ func TestRunPostInstallAuditCLIListsTheDisabledVariablesInOrder(t *testing.T) { t.Fatalf("summary does not list the variables in order; want %q in:\n%s", want, out) } } + +// The CLI asks the HC initial account email after centralized, repeats on an empty or unusable +// answer with the approved message, and keeps the stored value on Enter. +func TestPromptHealthcheckInitialEmail(t *testing.T) { + var got string + out := captureStdout(t, func() { + v, err := promptHealthcheckInitialEmail(context.Background(), bufio.NewReader(strings.NewReader("\nnope\nops@example.org\n")), "") + if err != nil { + t.Fatalf("prompt: %v", err) + } + got = v + }) + if got != "ops@example.org" || + !strings.Contains(out, "HC initial account email []: Required with ProxSave HC Server\n") || + !strings.Contains(out, "Not a usable email address\n") { + t.Fatalf("value %q, transcript:\n%s", got, out) + } + kept, err := promptHealthcheckInitialEmail(context.Background(), bufio.NewReader(strings.NewReader("\n")), "kept@example.org") + if err != nil || kept != "kept@example.org" { + t.Fatalf("Enter must keep the stored value: %q %v", kept, err) + } +} diff --git a/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env b/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env index 3a6a347a..cb5f4710 100644 --- a/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env +++ b/cmd/proxsave/testdata/install_characterization/fresh_decline_all.env @@ -479,7 +479,7 @@ MAX_RUN_DURATION=1h # daemon watchdog: hard timeout for one backup; o # ---------------------------------------------------------------------- HEALTHCHECK_ENABLED=true # report service-alive heartbeat + per-run outcome to a healthchecks monitor HEALTHCHECK_MODE=centralized # centralized = ping URLs fetched from the proxsave_server (zero setup, reuses the Telegram identity); self = your own healthchecks/SaaS -HEALTHCHECK_ACCOUNT_INITIAL_EMAIL= # centralized: email for your monitoring portal; applied once after you confirm the mail, then change it in the portal +HEALTHCHECK_ACCOUNT_INITIAL_EMAIL=ops@example.org # centralized: email for your monitoring portal; applied once after you confirm the mail, then change it in the portal HEALTHCHECK_HEARTBEAT_INTERVAL=5m # service-alive ping interval HEALTHCHECK_SEND_LOG=true # POST the log tail on a warning/error outcome HEALTHCHECK_ALIVE_URL= # self: the FULL service-alive ping URL you paste (e.g. https://hc-ping.com/); centralized: fallback when ProxSave HC Server is unreachable diff --git a/cmd/proxsave/testdata/install_characterization/fresh_decline_all.transcript b/cmd/proxsave/testdata/install_characterization/fresh_decline_all.transcript index 19578431..c930c971 100644 --- a/cmd/proxsave/testdata/install_characterization/fresh_decline_all.transcript +++ b/cmd/proxsave/testdata/install_characterization/fresh_decline_all.transcript @@ -27,7 +27,7 @@ Scheduler engine: daemon (resident, hang watchdog + healthchecks) or cron [daemo off no monitoring centralized ProxSave HC Server (zero setup, reuses the Telegram identity) self your own healthchecks/SaaS server (you paste the ping URLs next) -Healthchecks monitoring: off, centralized, or self [centralized]: +Healthchecks monitoring: off, centralized, or self [centralized]: HC initial account email []: --- Notify level --- always every run warning warnings and failures diff --git a/internal/config/upgrade_initial_email_test.go b/internal/config/upgrade_initial_email_test.go new file mode 100644 index 00000000..8e41984e --- /dev/null +++ b/internal/config/upgrade_initial_email_test.go @@ -0,0 +1,43 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// An install from before issue 347 has no HEALTHCHECK_ACCOUNT_INITIAL_EMAIL: the upgrade merge adds it, +// empty, with its template comment, under HEALTHCHECK_MODE, and keeps every value the operator set. +func TestUpgradeAddsTheInitialEmailVariableEmpty(t *testing.T) { + var kept []string + for _, line := range strings.Split(DefaultEnvTemplate(), "\n") { + if strings.HasPrefix(line, "HEALTHCHECK_ACCOUNT_INITIAL_EMAIL=") { + continue + } + if strings.HasPrefix(line, "HEALTHCHECK_MODE=") { + line = "HEALTHCHECK_MODE=self" + } + kept = append(kept, line) + } + dir := t.TempDir() + path := filepath.Join(dir, "backup.env") + if err := os.WriteFile(path, []byte(strings.Join(kept, "\n")), 0o600); err != nil { + t.Fatal(err) + } + if _, err := UpgradeConfigFileWithBaseDir(path, dir); err != nil { + t.Fatalf("upgrade: %v", err) + } + raw, _ := os.ReadFile(path) + got := string(raw) + want := "HEALTHCHECK_ACCOUNT_INITIAL_EMAIL= # centralized: email for your monitoring portal; applied once after you confirm the mail, then change it in the portal" + if !strings.Contains(got, want) { + t.Fatalf("variable not added as in the template:\n%s", got) + } + if !strings.Contains(got, "HEALTHCHECK_MODE=self") { + t.Fatalf("an operator value was changed") + } + if i, j := strings.Index(got, "HEALTHCHECK_MODE=self"), strings.Index(got, want); j < i { + t.Fatalf("added before HEALTHCHECK_MODE") + } +} diff --git a/internal/installer/install_data.go b/internal/installer/install_data.go index ca54ded7..3e27ee70 100644 --- a/internal/installer/install_data.go +++ b/internal/installer/install_data.go @@ -56,7 +56,9 @@ type InstallWizardPrefill struct { SchedulerWeekday string // SCHEDULER_WEEKDAY as stored (empty when absent) SchedulerMonthDay string // SCHEDULER_MONTHDAY as stored (empty when absent) HealthcheckMode string // "off" | "centralized" | "self" (empty on a fresh/pre-daemon config) - NotifyOn string // "always" | "warning" | "failure"; empty when absent or not valid + // HealthcheckAccountInitialEmail is HEALTHCHECK_ACCOUNT_INITIAL_EMAIL as stored ("" when absent). + HealthcheckAccountInitialEmail string + NotifyOn string // "always" | "warning" | "failure"; empty when absent or not valid } // InstallWizardData holds the collected installation data. @@ -89,7 +91,10 @@ type InstallWizardData struct { EnableEncryption bool SchedulerMode string // "cron" | "daemon" HealthcheckMode string // "off" | "centralized" | "self"; empty with daemon -> backward-compat centralized-on - NotifyOn string // Notify level: "always" | "warning" | "failure"; empty -> NOTIFY_ON left as stored + // HealthcheckAccountInitialEmail is the portal's initial email (issue 347), required with the + // centralized mode and written only then; with any other mode the stored value is kept. + HealthcheckAccountInitialEmail string + NotifyOn string // Notify level: "always" | "warning" | "failure"; empty -> NOTIFY_ON left as stored } // HealthcheckSelfParams holds the full ping URLs the self-mode params screen @@ -386,6 +391,9 @@ func ApplyInstallData(baseTemplate string, data *InstallWizardData) (string, err template = setEnvValue(template, "HEALTHCHECK_ENABLED", "true") template = setEnvValue(template, "HEALTHCHECK_MODE", "centralized") clearURLs() + if email := strings.TrimSpace(data.HealthcheckAccountInitialEmail); email != "" { + template = setEnvValue(template, "HEALTHCHECK_ACCOUNT_INITIAL_EMAIL", email) + } } // Notify level (NOTIFY_ON). Below always it applies only with a Healthchecks monitor, so without @@ -486,6 +494,8 @@ func DeriveInstallWizardPrefill(baseTemplate string) InstallWizardPrefill { out.HealthcheckMode = "off" } + out.HealthcheckAccountInitialEmail = strings.TrimSpace(readTemplateString(values, "HEALTHCHECK_ACCOUNT_INITIAL_EMAIL")) + if raw := readTemplateString(values, "NOTIFY_ON"); strings.TrimSpace(raw) != "" { if level := config.NormalizeNotifyOn(raw); config.IsValidNotifyOn(level) { out.NotifyOn = level @@ -562,3 +572,26 @@ func installEmailDeliveryMethodOrDefault(method string) string { return "relay" } } + +// ValidateHealthcheckInitialEmail checks the address the install wizard asks with the centralized +// Healthchecks mode (issue 347): required, then a minimal form check (one "@", a dotted domain, +// printable characters without spaces). The ProxSave HC Server checks it again. +func ValidateHealthcheckInitialEmail(value string) error { + v := strings.TrimSpace(value) + if v == "" { + return errors.New("Required with ProxSave HC Server") + } + local, domain, ok := strings.Cut(v, "@") + usable := ok && local != "" && !strings.Contains(domain, "@") && strings.Contains(domain, ".") && + !strings.HasPrefix(domain, ".") && !strings.HasSuffix(domain, ".") && !strings.Contains(domain, "..") && + len(v) <= 254 + for _, r := range v { + if r <= ' ' || r > '~' { + usable = false + } + } + if !usable { + return errors.New("Not a usable email address") + } + return nil +} diff --git a/internal/installer/install_data_initial_email_test.go b/internal/installer/install_data_initial_email_test.go new file mode 100644 index 00000000..aa2dc16f --- /dev/null +++ b/internal/installer/install_data_initial_email_test.go @@ -0,0 +1,53 @@ +package installer + +import ( + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/config" +) + +func TestValidateHealthcheckInitialEmail(t *testing.T) { + for v, want := range map[string]string{ + "ops@example.org": "", + " ops@example.org ": "", + "": "Required with ProxSave HC Server", + " ": "Required with ProxSave HC Server", + "ops": "Not a usable email address", + "@example.org": "Not a usable email address", + "a@b@example.org": "Not a usable email address", + "a@localhost": "Not a usable email address", + "a@.example.org": "Not a usable email address", + "a@example.org.": "Not a usable email address", + "a@example..org": "Not a usable email address", + "a b@example.org": "Not a usable email address", + "à@example.org": "Not a usable email address", + } { + err := ValidateHealthcheckInitialEmail(v) + got := "" + if err != nil { + got = err.Error() + } + if got != want { + t.Errorf("%q: got %q, want %q", v, got, want) + } + } +} + +// Written with the centralized mode only; every other mode keeps the stored value (maintainer, 2026-11-13). +func TestApplyInstallDataWritesTheInitialEmailOnlyWhenCentralized(t *testing.T) { + base := SetEnvValueInTemplate(config.DefaultEnvTemplate(), "HEALTHCHECK_ACCOUNT_INITIAL_EMAIL", "kept@example.org") + for mode, want := range map[string]string{"centralized": "new@example.org", "self": "kept@example.org", "off": "kept@example.org"} { + out, err := ApplyInstallData(base, &InstallWizardData{SchedulerMode: "daemon", HealthcheckMode: mode, + HealthcheckAccountInitialEmail: "new@example.org"}) + if err != nil { + t.Fatalf("%s: %v", mode, err) + } + if got := DeriveInstallWizardPrefill(out).HealthcheckAccountInitialEmail; got != want { + t.Errorf("%s: stored %q, want %q", mode, got, want) + } + if !strings.Contains(out, "HEALTHCHECK_ACCOUNT_INITIAL_EMAIL="+want) { + t.Errorf("%s: line missing", mode) + } + } +} diff --git a/internal/ui/flows/install/install.go b/internal/ui/flows/install/install.go index 452cecb9..fb673171 100644 --- a/internal/ui/flows/install/install.go +++ b/internal/ui/flows/install/install.go @@ -217,6 +217,16 @@ func CollectWizardData(ctx context.Context, session *shell.Session, baseTemplate OptionIndex: hcIndex, Active: func() bool { return schedulerValues[scheduler.OptionIndex] == "daemon" }, } + // HC initial account email (HEALTHCHECK_ACCOUNT_INITIAL_EMAIL, issue 347): always shown, editable and + // required only with the daemon and the ProxSave HC Server; prefilled with the stored value. + hcInitialEmail := &components.FormField{ + Label: "HC initial account email", + Description: "ProxSave HC Server: your email for the monitoring portal. Confirmed by mail, applied once.", + Kind: components.FieldText, + Text: prefill.HealthcheckAccountInitialEmail, + Active: func() bool { return healthcheck.Active() && hcValues[healthcheck.OptionIndex] == "centralized" }, + Validate: installer.ValidateHealthcheckInitialEmail, + } // Notify level (NOTIFY_ON). Active only with a Healthchecks monitor (daemon engine, mode not // Off): without one any level below every run has no effect, and the data block writes always. // Fresh/Overwrite defaults to Warnings and failures; editing an existing config defaults to its @@ -300,7 +310,7 @@ func CollectWizardData(ctx context.Context, session *shell.Session, baseTemplate fields := []*components.FormField{ secondary, secondaryPath, secondaryLog, cloud, cloudRemote, cloudLog, - firewall, telegram, email, method, encryption, scheduler, healthcheck, notifyLevel, + firewall, telegram, email, method, encryption, scheduler, healthcheck, hcInitialEmail, notifyLevel, frequency, weekday, monthDay, cronField, } if _, err := shell.Ask(ctx, session, components.NewFormGrid( @@ -363,6 +373,9 @@ func CollectWizardData(ctx context.Context, session *shell.Session, baseTemplate // Healthchecks require the daemon (the sole pinger); cron forces the mode off. if data.SchedulerMode == "daemon" { data.HealthcheckMode = hcValues[healthcheck.OptionIndex] + if data.HealthcheckMode == "centralized" { + data.HealthcheckAccountInitialEmail = strings.TrimSpace(hcInitialEmail.Text) + } } else { data.HealthcheckMode = "off" } diff --git a/internal/ui/flows/install/install_test.go b/internal/ui/flows/install/install_test.go index 903600ae..6d079cfa 100644 --- a/internal/ui/flows/install/install_test.go +++ b/internal/ui/flows/install/install_test.go @@ -153,11 +153,16 @@ func TestCollectWizardDataDeclineAll(t *testing.T) { }() // Single aligned form: inactive dependent rows are skipped. Fresh install - // defaults to the daemon, so the Healthchecks and Notify level rows are active too: - // Enter through the 11 active rows (Frequency included; Weekday and Day of month are - // inactive on daily) reaches Continue; the final Enter submits. + // defaults to the daemon, so the Healthchecks, HC initial account email and Notify + // level rows are active too: Enter through the 12 active rows (Frequency included; + // Weekday and Day of month are inactive on daily) reaches Continue; the final Enter + // submits. The email, required with the ProxSave HC Server, is typed on its row (9th). d.waitScreen("Configuration") - for range 12 { + for range 8 { + d.keys("enter") + } + d.typeText("ops@example.org") + for range 5 { d.keys("enter") } @@ -176,6 +181,9 @@ func TestCollectWizardDataDeclineAll(t *testing.T) { if data.NotifyOn != "warning" { t.Fatalf("fresh install must default Notify level to warning, got %q", data.NotifyOn) } + if data.HealthcheckAccountInitialEmail != "ops@example.org" { + t.Fatalf("HC initial account email = %q", data.HealthcheckAccountInitialEmail) + } if data.NotificationMode != "none" { t.Fatalf("notification mode = %q, want none", data.NotificationMode) } @@ -766,13 +774,14 @@ func TestCollectWizardDataNotifyLevel(t *testing.T) { template = installer.SetEnvValueInTemplate(template, "SCHEDULER_MODE", "daemon") template = installer.SetEnvValueInTemplate(template, "HEALTHCHECK_ENABLED", "true") template = installer.SetEnvValueInTemplate(template, "HEALTHCHECK_MODE", "centralized") + template = installer.SetEnvValueInTemplate(template, "HEALTHCHECK_ACCOUNT_INITIAL_EMAIL", "ops@example.org") return installer.SetEnvValueInTemplate(template, "NOTIFY_ON", level) } t.Run("prefill_failure_roundtrip", func(t *testing.T) { d := newDriver(t) resCh := collectWizardAsync(t, d, daemonCentralized("failure")) d.waitScreen("Configuration") - d.keys("down down down down down down down down down down down down enter") + d.keys("down down down down down down down down down down down down down enter") // the stored email prefills its row res := <-resCh if res.err != nil || res.data.NotifyOn != "failure" { t.Fatalf("notify=%q err=%v, want the stored failure kept", res.data.NotifyOn, res.err) @@ -782,9 +791,11 @@ func TestCollectWizardDataNotifyLevel(t *testing.T) { d := newDriver(t) resCh := collectWizardAsync(t, d, "") d.waitScreen("Configuration") - d.keys("down down down down down down down down") // 8 downs -> Notify level row - d.keys("right") // warning -> failure - d.keys("down down down enter") // Frequency, Run at, Continue, submit + d.keys("down down down down down down down down") // 8 downs -> HC initial account email (required) + d.typeText("ops@example.org") + d.keys("down") // -> Notify level row + d.keys("right") // warning -> failure + d.keys("down down down enter") // Frequency, Run at, Continue, submit res := <-resCh if res.err != nil || res.data.NotifyOn != "failure" { t.Fatalf("notify=%q err=%v, want failure", res.data.NotifyOn, res.err) @@ -797,10 +808,12 @@ func TestCollectWizardDataScheduleWeekly(t *testing.T) { d := newDriver(t) resCh := collectWizardAsync(t, d, "") d.waitScreen("Configuration") - d.keys("down down down down down down down down down") // 9 downs -> Frequency row - d.keys("right") // daily -> weekly - d.keys("down right right right right") // Weekday: mon -> fri - d.keys("down down enter") // Run at, Continue, submit + d.keys("down down down down down down down down") // 8 downs -> HC initial account email (required) + d.typeText("ops@example.org") + d.keys("down down") // -> Frequency row + d.keys("right") // daily -> weekly + d.keys("down right right right right") // Weekday: mon -> fri + d.keys("down down enter") // Run at, Continue, submit res := <-resCh if res.err != nil { t.Fatalf("unexpected error: %v", res.err)