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/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/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/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/cron_migration_test.go b/cmd/proxsave/cron_migration_test.go index 7cf542cb..a5a22772 100644 --- a/cmd/proxsave/cron_migration_test.go +++ b/cmd/proxsave/cron_migration_test.go @@ -312,6 +312,87 @@ func TestRepointLegacyCronLines(t *testing.T) { if changed2 || out2[0] != "0 5 * * * /usr/local/bin/proxsave --backup" { t.Errorf("no legacy line -> changed=false, unchanged; got changed=%v out=%v", changed2, out2) } + + // The command field is repointed, never the first occurrence of the legacy path on the + // line: a preceding assignment or a later argument naming that path stays byte-identical. + rows := []struct { + name string + line string + want string + changed bool + }{ + { + name: "assignment value holding the legacy path before the command", + line: "0 5 * * * TAG=/usr/local/bin/proxmox-backup.old /usr/local/bin/proxmox-backup --backup", + want: "0 5 * * * TAG=/usr/local/bin/proxmox-backup.old /usr/local/bin/proxsave --backup", + changed: true, + }, + { + name: "quoted assignment and quoted command", + line: `0 5 * * * TAG='/usr/local/bin/proxmox-backup.old' "/usr/local/bin/proxmox-backup" --backup`, + want: `0 5 * * * TAG='/usr/local/bin/proxmox-backup.old' "/usr/local/bin/proxsave" --backup`, + changed: true, + }, + { + name: "nickname schedule with an assignment", + line: "@daily TAG=/usr/local/bin/proxmox-backup.old /usr/local/bin/proxmox-backup --backup", + want: "@daily TAG=/usr/local/bin/proxmox-backup.old /usr/local/bin/proxsave --backup", + changed: true, + }, + { + name: "irregular whitespace is preserved", + line: "0 5 * * * TZ=UTC\t/usr/local/bin/proxmox-backup --backup", + want: "0 5 * * * TZ=UTC\t/usr/local/bin/proxsave --backup", + changed: true, + }, + { + name: "argument naming the legacy path is left alone", + line: "0 5 * * * /usr/local/bin/proxmox-backup --log /usr/local/bin/proxmox-backup.log", + want: "0 5 * * * /usr/local/bin/proxsave --log /usr/local/bin/proxmox-backup.log", + changed: true, + }, + { + // Pins current behaviour: an assignment whose value is the legacy binary answers + // as the command word itself, so the exact-equality guard skips the line. + name: "assignment naming the legacy binary is not repointed", + line: "0 5 * * * A=/usr/local/bin/proxmox-backup /usr/local/bin/proxmox-backup --backup", + want: "0 5 * * * A=/usr/local/bin/proxmox-backup /usr/local/bin/proxmox-backup --backup", + changed: false, + }, + } + for _, tc := range rows { + got, gotChanged := repointLegacyCronLines([]string{tc.line}) + if gotChanged != tc.changed || got[0] != tc.want { + t.Errorf("%s:\n line %q\n got %q changed=%v\n want %q changed=%v", + tc.name, tc.line, got[0], gotChanged, tc.want, tc.changed) + } + } +} + +// cronFieldOffset finds the first whitespace-separated field equal to the word, splitting +// like strings.Fields, and never a substring inside a longer field. +func TestCronFieldOffset(t *testing.T) { + cases := []struct { + line string + word string + want int + }{ + {"0 5 * * * /usr/local/bin/proxmox-backup --backup", "/usr/local/bin/proxmox-backup", 10}, + {"0 5 * * * TAG=/usr/local/bin/proxmox-backup.old /usr/local/bin/proxmox-backup", "/usr/local/bin/proxmox-backup", 48}, + {"0 5 * * * /x/proxmox-backup.log", "/x/proxmox-backup", -1}, + {"a\tb\u00a0c d", "c", 5}, + {"a\tb\u00a0c d", "d", 8}, + {" lead", "lead", 2}, + {"word", "word", 0}, + {"a b", "", -1}, + {"", "a", -1}, + {"ab abc", "abc", 3}, + } + for _, tc := range cases { + if got := cronFieldOffset(tc.line, tc.word); got != tc.want { + t.Errorf("cronFieldOffset(%q, %q) = %d, want %d", tc.line, tc.word, got, tc.want) + } + } } // The cron line pins the run to the config the daemon or the install used when it is diff --git a/cmd/proxsave/daemon.go b/cmd/proxsave/daemon.go index bb7a77b9..7fb092ff 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 @@ -158,6 +163,9 @@ type daemon struct { pollMu sync.Mutex // notifyRefreshWaitOverride replaces notifyPolicyRefreshWait in tests. notifyRefreshWaitOverride time.Duration + // beforeRunPollFinished, when set (tests), is called once a before-run poll's goroutine + // has closed its done channel (refreshNotifyPolicy). + beforeRunPollFinished func() // confirmedCadence is the seam the relay negotiation of SCHEDULER_FREQUENCY plugs into // (todo points 17-22 and 39-40): in centralized mode it returns the cadence in effect for // the configured one, i.e. the one the ProxSave HC Server has confirmed. nil means nothing @@ -780,7 +788,7 @@ func (d *daemon) runOnce(parentCtx context.Context) bool { // Before the start ping, so its bounded wait is not counted in the run duration the remote // monitor measures, and before getReporter, so a renegotiated channel set is the one pinged. - d.refreshNotifyPolicy(parentCtx) + lateEmail := d.refreshNotifyPolicy(parentCtx) r := d.getReporter() rid := health.NewRunID() d.reportBestEffort("start", false, func() error { return d.startPing(parentCtx, r, rid) }) @@ -850,6 +858,9 @@ func (d *daemon) runOnce(parentCtx context.Context) bool { return false } logging.Info("daemon: backup finished (rid=%s exit=%d)", rid, code) + // A portal initial email answer that missed the bounded wait before the run is shown here, + // in the same run, or not at all (it is sent again before the next run). + d.logLateInitialEmailOutcome(lateEmail) // A run whose child actually RAN can lift an inherited abandon degrade: the daemon launched // a child, it took the backup lock the orphan used to hold, and it was reaped. That is what // the marker's claim is checked against -- not a skip (ExitBackupSkipped, handled above, is @@ -2113,7 +2124,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 +2213,10 @@ 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 + // initialEmail is the HEALTHCHECK_ACCOUNT_INITIAL_EMAIL the poll carried ("" when none); cfg + // then holds the relay's answer in AccountInitialEmail. It is kept only so that the answer is + // recorded once it is logged (logInitialEmailOutcome), and never logged. + initialEmail string } // relaySecret is the relay secret on disk or, when there is none yet, one from a THROTTLED, @@ -2245,6 +2262,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,10 +2275,11 @@ 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, + initialEmail: initialEmail} if err != nil { return p } diff --git a/cmd/proxsave/daemon_cron_reporting_test.go b/cmd/proxsave/daemon_cron_reporting_test.go index c82935a1..818d6854 100644 --- a/cmd/proxsave/daemon_cron_reporting_test.go +++ b/cmd/proxsave/daemon_cron_reporting_test.go @@ -184,6 +184,25 @@ func cronModeFixture(t *testing.T) (*config.Config, string) { return &config.Config{BaseDir: dir, SchedulerTime: "02:00"}, configPath } +// revertCronLine is the canonical line a cron write at SCHEDULER_TIME=02:00 produces. +const revertCronLine = "00 02 * * * /usr/local/bin/proxsave --backup" + +// cronWriteLands stubs a cron write that landed: the writer reports revertCronLine as written, +// and the crontab read back holds the extra lines followed by it. applyCronMode removes the +// daemon only after reading its own line back, so every test of what happens AFTER that gate +// has to start here. It returns the schedule the writer was asked for. +func cronWriteLands(t *testing.T, extra ...string) *string { + t.Helper() + schedule := new(string) + migrateLegacyCronEntriesFn = func(_ context.Context, _, _ string, _ *logging.BootstrapLogger, s, _ string) (string, bool) { + *schedule = s + return revertCronLine, true + } + lines := append(append([]string(nil), extra...), revertCronLine) + crontabReadLinesFn = func(context.Context) ([]string, error) { return lines, nil } + return schedule +} + // A revert must ALWAYS leave the host scheduled. This branch used to withhold the canonical // cron line whenever the detector saw an operator command that might run ProxSave, on the // reasoning that its worst case was "the host keeps the proxsave line it already had". That @@ -202,12 +221,8 @@ func TestApplyCronModeAlwaysWritesTheCronLineAndReportsTheFinding(t *testing.T) cfg, configPath := cronModeFixture(t) const wrapper = "30 02 * * * /usr/local/sbin/proxsave-nas-guard" - crontabReadLinesFn = func(context.Context) ([]string, error) { return []string{wrapper}, nil } + migrated := cronWriteLands(t, wrapper) wrapperCronLinesFn = func([]string) []string { return []string{wrapper} } - migrated := "" - migrateLegacyCronEntriesFn = func(_ context.Context, _, _ string, _ *logging.BootstrapLogger, schedule, _ string) { - migrated = schedule - } var report cronRevertReport seen := captureConsole(t, func() { @@ -218,8 +233,8 @@ func TestApplyCronModeAlwaysWritesTheCronLineAndReportsTheFinding(t *testing.T) } }) - if migrated != "00 02 * * *" { - t.Fatalf("the canonical cron line must be written whatever the detector found, got %q", migrated) + if *migrated != "00 02 * * *" { + t.Fatalf("the canonical cron line must be written whatever the detector found, got %q", *migrated) } // One problem, one WARNING, and it carries the count so it stands alone under // DEBUG_LEVEL=warning. The finding itself is INFO below it. @@ -259,14 +274,8 @@ func TestApplyCronModeAlwaysWritesTheCronLineAndReportsTheFinding(t *testing.T) func TestApplyCronModeStillWritesTheCronLineWithoutAWrapper(t *testing.T) { cfg, configPath := cronModeFixture(t) - crontabReadLinesFn = func(context.Context) ([]string, error) { - return []string{"0 6 * * * /usr/bin/rsync /a /b"}, nil - } + migrated := cronWriteLands(t, "0 6 * * * /usr/bin/rsync /a /b") wrapperCronLinesFn = func([]string) []string { return nil } - migrated := "" - migrateLegacyCronEntriesFn = func(_ context.Context, _, _ string, _ *logging.BootstrapLogger, schedule, _ string) { - migrated = schedule - } crontabWriteLinesFn = func(context.Context, []string) error { t.Error("without a wrapper the crontab is rewritten by migrateLegacyCronEntries, not here") return nil @@ -280,20 +289,20 @@ func TestApplyCronModeStillWritesTheCronLineWithoutAWrapper(t *testing.T) { // migrateLegacyCronEntries' OWN fallback for an empty schedule, which is exactly the // value this test must not accept: it would pass even if applyCronMode stopped passing // the configured time at all. - if migrated != "00 02 * * *" { - t.Fatalf("a host with no wrapper must still get its cron line at SCHEDULER_TIME, got %q", migrated) + if *migrated != "00 02 * * *" { + t.Fatalf("a host with no wrapper must still get its cron line at SCHEDULER_TIME, got %q", *migrated) } } -// Fail-open: an unreadable crontab must be treated as "no wrapper" and keep today's append. -// Being scheduled twice is recoverable; being unscheduled is silent data loss. +// An unreadable crontab after the write is a write nobody can confirm, and the revert removes +// the daemon only over a cron line it has read back. So the write is still ATTEMPTED - the +// unreadable read is no reason to skip it - but the revert then stops: the daemon stays, the +// configuration stays, and the caller gets errCronRevertCronNotWritten. // -// The second half is the #298 follow-up. A ProxSave schedule under /etc is an INDEPENDENT -// fact: it is read from files, not from `crontab -l`, so a failed crontab read is no -// reason to stay quiet about it. It still does not gate the append - nothing does - which -// is why this fail-open rule needed no splitting: the two facts are reported separately -// because they are separately true, not weighed against each other. -func TestApplyCronModeFallsBackToTheAppendWhenTheCrontabIsUnreadable(t *testing.T) { +// Nothing past the gate runs either. The wrapper detector reads the same unreadable crontab, and +// the /etc advisory says the host is reverting to cron, which it is not: emitting it over a +// daemon left in place would make its title the lie. +func TestApplyCronModeKeepsTheDaemonWhenTheCrontabIsUnreadable(t *testing.T) { cfg, configPath := cronModeFixture(t) crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, errors.New("crontab -l boom") } @@ -301,24 +310,33 @@ func TestApplyCronModeFallsBackToTheAppendWhenTheCrontabIsUnreadable(t *testing. t.Error("the detector must not be consulted when the crontab could not be read") return nil } - systemCronConsulted := false systemCronProxsaveRefsFn = func() []indirectCronRef { - systemCronConsulted = true + t.Error("the /etc advisory must not be emitted on a revert that was abandoned") return nil } migrated := false - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) { + migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) (string, bool) { migrated = true + return revertCronLine, true + } + removeDaemonServiceFn = func(context.Context, *logging.BootstrapLogger) error { + t.Error("the daemon must not be removed over a cron line nobody could read back") + return nil } - if _, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", nil); err != nil { - t.Fatalf("applyCronMode: %v", err) + report, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", nil) + if !errors.Is(err, errCronRevertCronNotWritten) { + t.Fatalf("want errCronRevertCronNotWritten, got %v", err) } if !migrated { - t.Fatal("an unreadable crontab must fall back to writing the cron line, never to leaving the host unscheduled") + t.Fatal("an unreadable crontab is no reason to skip the cron write") + } + if report.CronVerified || report.CronScheduled || !report.CronWritten || report.ModeRecorded { + t.Errorf("the report must say: written by the writer, not verified, not scheduled, mode not recorded; got %+v", report) } - if !systemCronConsulted { - t.Fatal("a failed `crontab -l` says nothing about /etc: the system-cron advisory must still run, or the host is told its revert restored the only schedule when a second one is sitting in /etc/cron.d") + data, _ := os.ReadFile(configPath) + if !strings.Contains(string(data), "SCHEDULER_MODE=daemon") { + t.Fatalf("an abandoned revert must leave the configuration untouched:\n%s", data) } } @@ -339,7 +357,7 @@ func TestApplyCronModeFallsBackToTheAppendWhenTheCrontabIsUnreadable(t *testing. func TestApplyCronModeStillWritesTheCronLineWhenSystemCronAlsoSchedulesIt(t *testing.T) { cfg, configPath := cronModeFixture(t) - crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, nil } + migrated := cronWriteLands(t) wrapperCronLinesFn = func([]string) []string { return nil } systemCronProxsaveRefsFn = func() []indirectCronRef { return []indirectCronRef{{ @@ -349,10 +367,6 @@ func TestApplyCronModeStillWritesTheCronLineWhenSystemCronAlsoSchedulesIt(t *tes Source: "/etc/cron.d/proxsave-guard", }} } - migrated := "" - migrateLegacyCronEntriesFn = func(_ context.Context, _, _ string, _ *logging.BootstrapLogger, schedule, _ string) { - migrated = schedule - } crontabWriteLinesFn = func(context.Context, []string) error { t.Error("a /etc finding must not send the revert down the removal path: it is not a wrapper this host's crontab owns") return nil @@ -361,8 +375,8 @@ func TestApplyCronModeStillWritesTheCronLineWhenSystemCronAlsoSchedulesIt(t *tes if _, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", nil); err != nil { t.Fatalf("applyCronMode: %v", err) } - if migrated != "00 02 * * *" { - t.Fatalf("a ProxSave schedule under /etc must NOT cancel the revert's own cron line (that would leave the host unscheduled if the finding was wrong or the entry is later deleted), got %q", migrated) + if *migrated != "00 02 * * *" { + t.Fatalf("a ProxSave schedule under /etc must NOT cancel the revert's own cron line (that would leave the host unscheduled if the finding was wrong or the entry is later deleted), got %q", *migrated) } } @@ -377,9 +391,8 @@ func TestApplyCronModeStillWritesTheCronLineWhenSystemCronAlsoSchedulesIt(t *tes func TestApplyCronModeEmitsTheSystemCronAdvisory(t *testing.T) { cfg, configPath := cronModeFixture(t) - crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, nil } + cronWriteLands(t) wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} systemCronProxsaveRefsFn = func() []indirectCronRef { return []indirectCronRef{{ Line: "17 02 * * * root /usr/local/sbin/proxsave-nas-guard", @@ -416,9 +429,8 @@ func TestApplyCronModeEmitsTheSystemCronAdvisory(t *testing.T) { func TestApplyCronModeAdvisoryUsesOneWarning(t *testing.T) { cfg, configPath := cronModeFixture(t) - crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, nil } + cronWriteLands(t) wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} // TWO findings, so a per-line WARNING cannot be mistaken for the one-warning shape. systemCronProxsaveRefsFn = func() []indirectCronRef { return []indirectCronRef{ @@ -540,9 +552,8 @@ func TestMaybeAutoMigrateDaemonReportsWhatTheRemovalDid(t *testing.T) { func TestApplyCronModeReportsWhetherTheModeWasRecorded(t *testing.T) { t.Run("write lands: the mode is reported as recorded", func(t *testing.T) { cfg, configPath := cronModeFixture(t) - crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, nil } + cronWriteLands(t) wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} report, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", nil) if err != nil { @@ -555,9 +566,8 @@ func TestApplyCronModeReportsWhetherTheModeWasRecorded(t *testing.T) { t.Run("write fails: the report says the mode is NOT recorded", func(t *testing.T) { cfg, configPath := cronModeFixture(t) - crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, nil } + cronWriteLands(t) wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} // A directory where the file is expected: every write fails, nothing else does. if err := os.Remove(configPath); err != nil { t.Fatal(err) @@ -576,138 +586,156 @@ func TestApplyCronModeReportsWhetherTheModeWasRecorded(t *testing.T) { }) } -// A revert that could not write the crontab must not report success. migrateLegacyCronEntries is -// void and has four early returns that write nothing - unknown exec path, no binary to point at, -// `crontab -l` failing, `crontab -` failing - and applyCronMode cannot see any of them, then -// removes the daemon unit anyway. A host with a broken or absent cron ended with no unit and no -// cron line, at exit 0. +// A revert that could not write the crontab must not remove the daemon. migrateLegacyCronEntries +// has four early returns that write nothing - unknown exec path, no binary to point at, +// `crontab -l` failing, `crontab -` failing - and applyCronMode used to drop its verdict and +// remove the daemon unit anyway. A host with a broken or absent cron ended with no unit and no +// cron line. // -// The check READS THE CRONTAB BACK rather than trusting a return value, for the same reason -// cronRemovalOutcome.Verified exists: what matters is whether the line is there, not whether the -// writer thought it wrote one. +// The gate needs the writer's verdict AND the very line it wrote read back from the crontab, for +// the same reason cronRemovalOutcome.Verified exists: what matters is whether the line is there, +// not whether the writer thought it wrote one. And it has to be THAT line: an older proxsave line +// with another schedule, left by a write that failed, does not schedule what the configuration +// says. On every failure nothing else happens: no teardown, no config write, and the caller gets +// errCronRevertCronNotWritten. func TestApplyCronModeReportsWhetherTheCronLineExists(t *testing.T) { - t.Run("the line is there afterwards", func(t *testing.T) { - cfg, configPath := cronModeFixture(t) - wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} - crontabReadLinesFn = func(context.Context) ([]string, error) { - return []string{"00 02 * * * /usr/local/bin/proxsave --backup"}, nil - } - - report, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", nil) - if err != nil { - t.Fatalf("applyCronMode: %v", err) - } - if !report.CronScheduled { - t.Error("a canonical proxsave line is present, so the revert is scheduled") - } - }) - - t.Run("the write silently did nothing", func(t *testing.T) { + t.Run("the line is there afterwards: the revert goes through", func(t *testing.T) { cfg, configPath := cronModeFixture(t) wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} - crontabReadLinesFn = func(context.Context) ([]string, error) { - return []string{"0 6 * * * /usr/bin/rsync /a /b"}, nil - } - - report, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", nil) - if err != nil { - t.Fatalf("a failed cron write must not fail the call: the daemon is already gone, got %v", err) - } - if report.CronScheduled { - t.Error("no proxsave line is present, so the revert must not report a schedule") - } - }) - - // "Could not read" is not "there is none", and the report has to keep them apart. The screen - // built on this field denied a schedule outright, which on an unreadable crontab is a - // measurement nobody took - the same shape of claim as the one canonicalCronLinePresent's own - // comment refuses to make in the other direction. - t.Run("the crontab cannot be read: not scheduled AND not verified", func(t *testing.T) { - cfg, configPath := cronModeFixture(t) - wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} - crontabReadLinesFn = func(context.Context) ([]string, error) { - return nil, errors.New(`exec: "crontab": executable file not found in $PATH`) + cronWriteLands(t) + removed := false + removeDaemonServiceFn = func(context.Context, *logging.BootstrapLogger) error { + removed = true + return nil } report, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", nil) if err != nil { t.Fatalf("applyCronMode: %v", err) } - if report.CronScheduled { - t.Error("an unreadable crontab is not evidence of a schedule") + if !report.CronWritten || !report.CronScheduled || !report.CronVerified { + t.Errorf("the line was written and read back, so the report must say so, got %+v", report) } - if report.CronVerified { - t.Error("an unreadable crontab is not evidence of the absence of one either") + if !removed { + t.Error("with the cron line in place the daemon must be removed") } }) - t.Run("the crontab was read: verified either way", func(t *testing.T) { - for _, lines := range [][]string{ - {"00 02 * * * /usr/local/bin/proxsave --backup"}, - {"0 6 * * * /usr/bin/rsync /a /b"}, - } { + for _, tc := range []struct { + name string + written string + ok bool + crontab []string + readErr error + wantVerified bool + }{ + { + name: "the writer reports a failure, even with a proxsave line in the crontab", + written: "", + ok: false, + crontab: []string{revertCronLine}, + wantVerified: true, + }, + { + name: "the write silently did nothing", + written: revertCronLine, + ok: true, + crontab: []string{"0 6 * * * /usr/bin/rsync /a /b"}, + wantVerified: true, + }, + { + name: "an older proxsave line with another schedule does not pass", + written: revertCronLine, + ok: true, + crontab: []string{"30 04 * * * /usr/local/bin/proxsave --backup"}, + wantVerified: true, + }, + { + // "Could not read" is not "there is none", and the report keeps them apart, but + // neither is a line read back: the daemon stays. + name: "the crontab cannot be read back", + written: revertCronLine, + ok: true, + readErr: errors.New(`exec: "crontab": executable file not found in $PATH`), + wantVerified: false, + }, + } { + t.Run(tc.name, func(t *testing.T) { cfg, configPath := cronModeFixture(t) - wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} - crontabReadLinesFn = func(context.Context) ([]string, error) { return lines, nil } + migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) (string, bool) { + return tc.written, tc.ok + } + crontabReadLinesFn = func(context.Context) ([]string, error) { return tc.crontab, tc.readErr } + wrapperCronLinesFn = func([]string) []string { + t.Error("an abandoned revert must not reach the wrapper advisory") + return nil + } + systemCronProxsaveRefsFn = func() []indirectCronRef { + t.Error("an abandoned revert must not reach the /etc advisory") + return nil + } + removeDaemonServiceFn = func(context.Context, *logging.BootstrapLogger) error { + t.Error("the daemon must not be removed without the cron line read back") + return nil + } + before, err := os.ReadFile(configPath) + if err != nil { + t.Fatal(err) + } report, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", nil) - if err != nil { - t.Fatalf("applyCronMode: %v", err) + if !errors.Is(err, errCronRevertCronNotWritten) { + t.Fatalf("want errCronRevertCronNotWritten, got %v", err) } - if !report.CronVerified { - t.Errorf("the crontab was read, so the answer is a measurement, got %+v for %v", report, lines) + if report.CronScheduled { + t.Error("the line written is not in the crontab, so the report must not claim a schedule") } - } - }) - - t.Run("the crontab cannot be read: fail closed", func(t *testing.T) { - cfg, configPath := cronModeFixture(t) - wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} - crontabReadLinesFn = func(context.Context) ([]string, error) { - return nil, errors.New(`exec: "crontab": executable file not found in $PATH`) - } - - report, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", nil) - if err != nil { - t.Fatalf("applyCronMode: %v", err) - } - // A host with no crontab binary at all is exactly the host this exists to catch, and it - // is also the host where nothing can be verified. Claiming a schedule would be the lie. - if report.CronScheduled { - t.Error("an unreadable crontab is not evidence of a schedule") - } - }) + if report.CronVerified != tc.wantVerified { + t.Errorf("CronVerified = %v, want %v", report.CronVerified, tc.wantVerified) + } + if report.CronWritten != tc.ok { + t.Errorf("CronWritten = %v, want the writer's own verdict %v", report.CronWritten, tc.ok) + } + if report.ModeRecorded { + t.Error("nothing was written to the configuration, so the report must not say it was") + } + after, _ := os.ReadFile(configPath) + if !bytes.Equal(before, after) { + t.Errorf("setBackupEnvKeys must not run on an abandoned revert:\nbefore:\n%s\nafter:\n%s", before, after) + } + }) + } } // The exit code of --daemon-remove is the only thing a script or a cron job sees. A revert that -// could not write the cron entry leaves the host with no daemon and no schedule, so reporting -// success there is the same defect as issue #298 in its most expensive form: the operator's -// automation records a clean revert over a host that stopped backing up. +// could not write the cron entry is abandoned with the daemon left in place, and it has to exit +// non-zero with the reason on screen: the operator asked for cron and did not get it. // // runDaemonRemove is drivable: applyCronMode reaches the crontab and the unit only through seams. func TestRunDaemonRemoveExitCodeFollowsTheCronEntry(t *testing.T) { for _, tc := range []struct { - name string - crontab []string - wantCode int - wantInLog string + name string + crontab []string + wantCode int + wantInLog []string + wantRemoved bool }{ { - name: "the cron entry is there afterwards: success", - crontab: []string{"00 02 * * * /usr/local/bin/proxsave --backup"}, - wantCode: types.ExitSuccess.Int(), - wantInLog: "Daemon removed", + name: "the cron entry is there afterwards: success", + crontab: []string{revertCronLine}, + wantCode: types.ExitSuccess.Int(), + wantInLog: []string{"Daemon removed"}, + wantRemoved: true, }, { - name: "no cron entry afterwards: failure", - crontab: []string{"0 6 * * * /usr/bin/rsync /a /b"}, - wantCode: types.ExitGenericError.Int(), - wantInLog: "Cron write failed.", + name: "no cron entry afterwards: failure, daemon left in place", + crontab: []string{"0 6 * * * /usr/bin/rsync /a /b"}, + wantCode: types.ExitGenericError.Int(), + wantInLog: []string{ + "daemon: the cron entry could not be written, so the daemon service was left in place and keeps scheduling the backup.", + "daemon-remove failed: the cron entry could not be written", + }, + wantRemoved: false, }, } { t.Run(tc.name, func(t *testing.T) { @@ -726,8 +754,14 @@ func TestRunDaemonRemoveExitCodeFollowsTheCronEntry(t *testing.T) { systemCronProxsaveRefsFn = origSystem }) restartVerifyBackupRunning = func(string) bool { return false } - removeDaemonServiceFn = func(context.Context, *logging.BootstrapLogger) error { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} + removed := false + removeDaemonServiceFn = func(context.Context, *logging.BootstrapLogger) error { + removed = true + return nil + } + migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) (string, bool) { + return revertCronLine, true + } wrapperCronLinesFn = func([]string) []string { return nil } systemCronProxsaveRefsFn = func() []indirectCronRef { return nil } crontabReadLinesFn = func(context.Context) ([]string, error) { return tc.crontab, nil } @@ -755,8 +789,13 @@ func TestRunDaemonRemoveExitCodeFollowsTheCronEntry(t *testing.T) { if code != tc.wantCode { t.Errorf("exit code = %d, want %d, out=%q", code, tc.wantCode, buf.String()) } - if !strings.Contains(buf.String(), tc.wantInLog) { - t.Errorf("the operator must read %q, out=%q", tc.wantInLog, buf.String()) + for _, want := range tc.wantInLog { + if !strings.Contains(buf.String(), want) { + t.Errorf("the operator must read %q, out=%q", want, buf.String()) + } + } + if removed != tc.wantRemoved { + t.Errorf("daemon removed = %v, want %v", removed, tc.wantRemoved) } }) } @@ -767,45 +806,54 @@ func TestRunDaemonRemoveExitCodeFollowsTheCronEntry(t *testing.T) { // a host where that write did not land the sentence was simply false, and it was false in the // reassuring direction: it told the operator a schedule existed next to the unmanaged ones when // none did. +// +// A write that did not land no longer reaches the duplicate warning at all: the revert stops at +// the cron gate with the daemon in place, so the only WARNING is the one saying so. func TestApplyCronModeDuplicateWarningStatesTheCrontabItRead(t *testing.T) { const wrapper = "30 02 * * * /usr/local/sbin/nas-guard" for _, tc := range []struct { name string crontab []string + wantErr error want string - absent string + absent []string }{ { name: "the entry is there: say so", - crontab: []string{wrapper, "00 02 * * * /usr/local/bin/proxsave --backup"}, + crontab: []string{wrapper, revertCronLine}, want: "The cron entry is in the crontab", - absent: "could be written", + absent: []string{"could be written", "left in place"}, }, { - name: "the entry is not there: do not claim it", + name: "the entry is not there: no duplicate claim, the daemon stays", crontab: []string{wrapper}, - want: "no proxsave cron entry could be written", - absent: "is in the crontab", + wantErr: errCronRevertCronNotWritten, + want: "daemon: the cron entry could not be written, so the daemon service was left in place and keeps scheduling the backup.", + absent: []string{"is in the crontab", "unmanaged cron line(s)", "no proxsave cron entry could be written"}, }, } { t.Run(tc.name, func(t *testing.T) { cfg, configPath := cronModeFixture(t) crontabReadLinesFn = func(context.Context) ([]string, error) { return tc.crontab, nil } wrapperCronLinesFn = func([]string) []string { return []string{wrapper} } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} + migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) (string, bool) { + return revertCronLine, true + } seen := captureConsole(t, func() { - if _, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", logging.NewBootstrapLogger()); err != nil { - t.Fatalf("applyCronMode: %v", err) + if _, err := applyCronMode(context.Background(), cfg, configPath, "/usr/local/bin/proxsave", logging.NewBootstrapLogger()); !errors.Is(err, tc.wantErr) { + t.Fatalf("applyCronMode: got %v, want %v", err, tc.wantErr) } }) if !strings.Contains(seen, tc.want) { t.Errorf("the WARNING must state %q, out=%q", tc.want, seen) } - if strings.Contains(seen, tc.absent) { - t.Errorf("the WARNING must not state %q, out=%q", tc.absent, seen) + for _, absent := range tc.absent { + if strings.Contains(seen, absent) { + t.Errorf("the output must not state %q, out=%q", absent, seen) + } } }) } @@ -815,9 +863,8 @@ func TestApplyCronModeDuplicateWarningStatesTheCrontabItRead(t *testing.T) { func TestApplyCronModeStaysSilentWithoutASystemCronFinding(t *testing.T) { cfg, configPath := cronModeFixture(t) - crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, nil } + cronWriteLands(t) wrapperCronLinesFn = func([]string) []string { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} systemCronProxsaveRefsFn = func() []indirectCronRef { return nil } seen := captureConsole(t, func() { diff --git a/cmd/proxsave/daemon_initial_email.go b/cmd/proxsave/daemon_initial_email.go new file mode 100644 index 00000000..1fdeab2c --- /dev/null +++ b/cmd/proxsave/daemon_initial_email.go @@ -0,0 +1,139 @@ +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. It is +// called only once that answer is logged: an answer the operator never saw (a before-run poll +// that came back only after the run's backup had finished) leaves the address due, to be sent +// again next run. +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.initialEmail == "" { + 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) + } + d.recordInitialEmailAck(p.initialEmail, ack) +} + +// logLateInitialEmailOutcome is the initial email block of a before-run poll that did not answer +// within the bounded wait (refreshNotifyPolicy), looked at once, right after the run's "daemon: +// backup finished" line and without waiting. An answer arrived by then is logged there, once, and +// a final one is recorded. One still missing is dropped: nothing visible, and the address, still +// due, goes out again on the poll before the next run, so a dropped poll is never logged later +// and never logged twice. The block is skipped when another poll (a reporter refresh during the +// backup) already brought a final answer for the address and logged it. +func (d *daemon) logLateInitialEmailOutcome(late *beforeRunPoll) { + if late == nil { + return + } + logger := logging.GetDefaultLogger() + select { + case <-late.done: + default: + logging.DebugStep(logger, "initial email", "before-run poll still unanswered after the backup, sent again before the next run") + return + } + sent := late.result.initialEmail + if sent == "" { + logging.DebugStep(logger, "initial email", "before-run poll answered after the wait, without the address") + return + } + if d.wantedInitialEmail() != sent { + logging.DebugStep(logger, "initial email", "before-run poll answered after the wait, a final answer was already logged") + return + } + logging.DebugStep(logger, "initial email", "before-run poll answered after the wait") + d.logInitialEmailOutcome(late.result) +} + +// 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..e8f90200 --- /dev/null +++ b/cmd/proxsave/daemon_initial_email_test.go @@ -0,0 +1,536 @@ +package main + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "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 + release chan struct{} // when set, every answer waits until it is closed (releaseAnswers) + once sync.Once +} + +// releaseAnswers closes release once, however many times it is called: by a test that lets a held +// answer through, and by the cleanup emailDaemon registers. +func (e *emailRelay) releaseAnswers() { + if e.release != nil { + e.once.Do(func() { close(e.release) }) + } +} + +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, release := e.ack, e.applied, e.release + e.mu.Unlock() + if release != nil { + <-release + } + 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) + // Registered after server.Close, so it runs before it (cleanups run last in, first out): a test + // that fails while a request is held on release ends at once instead of waiting for the held + // handler, and leaves no poll still running into a later -count run. + t.Cleanup(relay.releaseAnswers) + 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.SplitSeq(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) + } +} + +// waitForRequests waits until relay has recorded n requests, failing at a deadline instead of +// hanging. The fake records a request on arrival, before it waits on release: a delayed answer is +// released only once its request is known to be in flight. +func waitForRequests(t *testing.T, relay *emailRelay, n int) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for len(relay.sent()) < n { + if time.Now().After(deadline) { + t.Fatalf("relay requests = %q, want %d within the deadline", relay.sent(), n) + } + time.Sleep(time.Millisecond) + } +} + +// waitForSignal waits for one value on ch, failing at a deadline instead of hanging. +func waitForSignal(t *testing.T, ch <-chan struct{}, what string) { + t.Helper() + select { + case <-ch: + case <-time.After(5 * time.Second): + t.Fatalf("%s: not within the deadline", what) + } +} + +// lateEmailDaemon is emailDaemon with the address in backup.env, the notify policy confirmed, a +// 50 ms bounded wait before the run, and scheduled runs whose child runs hook before it starts. +// finished receives one value each time a before-run poll's goroutine is done. +func lateEmailDaemon(t *testing.T, relay *emailRelay) (d *daemon, finished chan struct{}) { + t.Helper() + d = emailDaemon(t, relay, "", emailEnv+"HEALTHCHECK_ACCOUNT_INITIAL_EMAIL="+testInitialEmail+"\n") + d.notifyApplied = ¬ifyPolicy{notifyOn: config.NotifyOnWarning, channels: []string{"telegram"}} + d.notifyRefreshWaitOverride = 50 * time.Millisecond + d.cfg.BackupEnabled, d.cfg.MaxRunDuration = true, time.Minute + finished = make(chan struct{}, 8) + d.beforeRunPollFinished = func() { finished <- struct{}{} } + return d, finished +} + +// childAfter is a backup child that exits 0, built after hook ran. +func childAfter(hook func()) func(ctx context.Context) *exec.Cmd { + return func(ctx context.Context) *exec.Cmd { + hook() + return exec.CommandContext(ctx, "/bin/sh", "-c", "exit 0") + } +} + +// runLines are the visible lines of the log, with the run id and timeout cut from the daemon's +// run lines so they compare across runs. +func runLines(logs func() []string) []string { + var out []string + for _, line := range logs() { + if !strings.HasPrefix(line, "INFO ") && !strings.HasPrefix(line, "WARNING ") { + continue + } + if i := strings.Index(line, " (rid="); i >= 0 { + line = line[:i] + } + out = append(out, line) + } + return out +} + +const lateRejectionBlock = "INFO Applying monitoring portal email...|INFO Not a usable email address|" + + "WARNING ⚠ Monitoring portal email: not applied" + +// A before-run answer that comes back after the bounded wait, while the backup runs, is shown in +// the same run, right after "daemon: backup finished", once; the next run sends nothing. +func TestALateBeforeRunAnswerIsLoggedAfterTheBackupFinished(t *testing.T) { + relay := &emailRelay{applied: true, ack: map[string]string{"state": "rejected", "reason": "invalid"}, + release: make(chan struct{})} + d, finished := lateEmailDaemon(t, relay) + logs := captureDaemonLog(t) // a locked sink: the late poll writes to it after the wait + d.newBackupCmd = childAfter(func() { + waitForRequests(t, relay, 1) + relay.releaseAnswers() // the late answer arrives during the backup + waitForSignal(t, finished, "the before-run poll") + }) + d.runOnce(context.Background()) + d.newBackupCmd = childAfter(func() {}) + d.runOnce(context.Background()) // the final answer was logged: nothing due + + if got := relay.sent(); len(got) != 1 || got[0] != testInitialEmail { + t.Fatalf("headers = %q, want the address once", got) + } + run := "INFO daemon: launching backup|INFO daemon: backup finished" + want := run + "|" + lateRejectionBlock + "|" + run + if got := strings.Join(runLines(logs), "|"); got != want { + t.Fatalf("lines:\n%s", strings.Join(runLines(logs), "\n")) + } +} + +// A late answer whose address another poll already answered for good during the backup (a +// reporter refresh) is not logged a second time after "daemon: backup finished". +func TestALateBeforeRunAnswerAlreadyLoggedIsNotLoggedAgain(t *testing.T) { + relay := &emailRelay{applied: true, ack: map[string]string{"state": "rejected", "reason": "invalid"}, + release: make(chan struct{})} + d, finished := lateEmailDaemon(t, relay) + logs := captureDaemonLog(t) + d.newBackupCmd = childAfter(func() { + waitForRequests(t, relay, 1) + relay.releaseAnswers() + waitForSignal(t, finished, "the before-run poll") + startPoll(d) // another poll carrying the address, answered and logged + }) + d.runOnce(context.Background()) + + if got := relay.sent(); len(got) != 2 || got[0] != testInitialEmail || got[1] != testInitialEmail { + t.Fatalf("headers = %q, want the address twice", got) + } + want := lateRejectionBlock + "|INFO daemon: launching backup|INFO daemon: backup finished" + if got := strings.Join(runLines(logs), "|"); got != want { + t.Fatalf("lines:\n%s", strings.Join(runLines(logs), "\n")) + } +} + +// An answer still missing when the backup has finished shows nothing in that run; the address, +// still due, goes out again before the next run, whose answer is logged once. +func TestABeforeRunAnswerMissingAfterTheBackupIsSentAgainNextRun(t *testing.T) { + relay := &emailRelay{applied: true, ack: map[string]string{"state": "rejected", "reason": "invalid"}, + release: make(chan struct{})} + d, finished := lateEmailDaemon(t, relay) + logs := captureDaemonLog(t) + d.newBackupCmd = childAfter(func() { waitForRequests(t, relay, 1) }) + d.runOnce(context.Background()) + run := "INFO daemon: launching backup|INFO daemon: backup finished" + if got := strings.Join(runLines(logs), "|"); got != run { + t.Fatalf("first run lines:\n%s", strings.Join(runLines(logs), "\n")) + } + + relay.releaseAnswers() // its request is in flight (recorded above): the answer comes after the run + waitForSignal(t, finished, "the first before-run poll") + d.notifyRefreshWaitOverride = 5 * time.Second + d.newBackupCmd = childAfter(func() {}) + d.runOnce(context.Background()) + + if got := relay.sent(); len(got) != 2 || got[0] != testInitialEmail || got[1] != testInitialEmail { + t.Fatalf("headers = %q, want the address twice", got) + } + want := run + "|" + lateRejectionBlock + "|" + run + if got := strings.Join(runLines(logs), "|"); got != want { + t.Fatalf("lines:\n%s", strings.Join(runLines(logs), "\n")) + } +} + +// A run interrupted by a shutdown never reaches "daemon: backup finished": the late answer is not +// shown, and the address stays due. +func TestALateBeforeRunAnswerIsNotShownOnShutdown(t *testing.T) { + relay := &emailRelay{applied: true, ack: map[string]string{"state": "rejected", "reason": "invalid"}, + release: make(chan struct{})} + d, finished := lateEmailDaemon(t, relay) + logs := captureDaemonLog(t) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + d.newBackupCmd = childAfter(func() { + waitForRequests(t, relay, 1) + relay.releaseAnswers() + waitForSignal(t, finished, "the before-run poll") + cancel() + }) + d.runOnce(ctx) + + if got := strings.Join(runLines(logs), "|"); got != "INFO daemon: launching backup" { + t.Fatalf("lines:\n%s", strings.Join(runLines(logs), "\n")) + } + if got := d.wantedInitialEmail(); got != testInitialEmail { + t.Fatalf("address due = %q, want it still due", 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")) + } +} + +// 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_notify_policy.go b/cmd/proxsave/daemon_notify_policy.go index 0dac0688..99d8dc8d 100644 --- a/cmd/proxsave/daemon_notify_policy.go +++ b/cmd/proxsave/daemon_notify_policy.go @@ -80,28 +80,39 @@ const notifyPolicyResends = 2 // Journal (todo point 42): a policy already confirmed is one DEBUG line and no request; one that // is sent is the "Applying notify level..." block, applied or pending. With no relay secret on // disk the request is still attempted, as before, but with no block: that case has none. -func (d *daemon) refreshNotifyPolicy(ctx context.Context) { +// +// It returns the poll when that poll carried the portal's initial email and did not answer within +// the bounded wait (nil otherwise): runOnce looks at it once, without waiting, right after the +// run's "daemon: backup finished" line (logLateInitialEmailOutcome). +func (d *daemon) refreshNotifyPolicy(ctx context.Context) *beforeRunPoll { if d.cfg == nil || !d.cfg.HealthcheckEnabled || d.cfg.HealthcheckMode != config.HealthcheckModeCentralized { - return + return nil } fresh, err := config.LoadConfigWithBaseDir(d.cfg.ConfigPath, d.cfg.BaseDir) if err != nil { - return + return nil } 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 + return nil } - 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()) @@ -112,11 +123,15 @@ func (d *daemon) refreshNotifyPolicy(ctx context.Context) { } // The poll runs on its own goroutine because it can reach the relay-secret flock, which - // honours no deadline. result is written before done is closed and read only after it is. - var result configPoll - done := make(chan struct{}) + // honours no deadline. + poll := &beforeRunPoll{done: make(chan struct{})} go func() { - defer close(done) + defer func() { + close(poll.done) + if hook := d.beforeRunPollFinished; hook != nil { + hook() + } + }() op := "" if block { op = "notify policy" @@ -128,7 +143,7 @@ func (d *daemon) refreshNotifyPolicy(ctx context.Context) { if r := d.reporterFromPoll(p, block); r != nil { d.setReporter(r) } - result = p + poll.result = p }() wait := notifyPolicyRefreshWait if d.notifyRefreshWaitOverride > 0 { @@ -138,21 +153,37 @@ func (d *daemon) refreshNotifyPolicy(ctx context.Context) { defer timer.Stop() answered := false select { - case <-done: + case <-poll.done: answered = true 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) - return + if block { + logging.DebugStep(logger, "notify policy", "no answer within %s", wait) + d.logNotifyLevelOutcome(fresh.NotifyOn, want, reachNone) + } + if !emailDue { + return nil + } + // Its initial email answer, if any, is looked at once the backup has finished. + logging.DebugStep(logger, "initial email", "no answer within %s, looked at again after the backup", wait) + return poll } - d.logNotifyPolicyAnswer(result) - d.logNotifyLevelOutcome(fresh.NotifyOn, want, result.answer()) + if block { + d.logNotifyPolicyAnswer(poll.result) + d.logNotifyLevelOutcome(fresh.NotifyOn, want, poll.result.answer()) + } + // After the notify level block, never inside it. + d.logInitialEmailOutcome(poll.result) + return nil +} + +// beforeRunPoll is the config poll refreshNotifyPolicy sends before a run, on its own goroutine: +// result is written before done is closed and read only after it is. +type beforeRunPoll struct { + done chan struct{} + result configPoll } // logNotifyPolicyRead is the DEBUG evidence of what the notify level block sends: NOTIFY_ON as 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 } diff --git a/cmd/proxsave/daemon_setup.go b/cmd/proxsave/daemon_setup.go index 123b8106..61deeda3 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" @@ -72,15 +74,11 @@ func runDaemonRemove(rt *appRuntime) int { logging.Error("daemon-remove aborted: the configuration could not be read; the daemon was NOT removed.") return types.ExitGenericError.Int() } + // errCronRevertCronNotWritten lands here too, after applyCronMode's WARNING saying the + // daemon was left in place: the revert was abandoned before anything changed. logging.Error("daemon-remove failed: %v", err) return types.ExitGenericError.Int() } - if !revert.CronScheduled { - // Short on purpose: the reason is already on screen, as the WARNING - // migrateLegacyCronEntries emits at the point it gave up. - logging.Error("Cron write failed.") - return types.ExitGenericError.Int() - } logging.Info("Daemon removed: reverted to the cron scheduler. %s", cronModeRecordClause(revert)) return types.ExitSuccess.Int() } @@ -106,6 +104,22 @@ func canonicalCronLinePresent(ctx context.Context) (present, verified bool) { return false, true } +// cronLineReadBack reads the root crontab and reports whether it holds line (spaces around it +// aside), and SEPARATELY whether it could look at all, as canonicalCronLinePresent does. +func cronLineReadBack(ctx context.Context, line string) (present, verified bool) { + lines, err := crontabReadLinesFn(ctx) + if err != nil { + return false, false + } + want := strings.TrimSpace(line) + for _, got := range lines { + if want != "" && strings.TrimSpace(got) == want { + return true, true + } + } + return false, true +} + // cronModeRecordClause renders whether the revert's config write landed, as one standalone // sentence both front-ends use. // @@ -336,7 +350,10 @@ func installVerifyVerdict(rv RestartVerifyResult) (orchestrator.HealthcheckSetup // ordering, what the cron removal reports, and whether the --upgrade retrofit // migrated at all - without touching the real crontab or the systemd unit. var ( - removeDaemonServiceFn = removeDaemonService + removeDaemonServiceFn = removeDaemonService + // It returns the line migrateLegacyCronEntries wrote and whether it believes the write + // landed: applyCronMode reads that exact line back (cronLineReadBack) and removes the daemon + // only when both agree, the same gate the install path applies. migrateLegacyCronEntriesFn = migrateLegacyCronEntries // crontabReadLinesFn lets a test feed a synthetic crontab to the SCHEDULER_TIME // seeding (seedSchedulerTimeFromCrontab), to the cron removal @@ -396,6 +413,11 @@ var ( // real backup lock path) could not be read; fail-closed so a backup on a custom LOCK_PATH is // never killed blindly. errDaemonTeardownConfigUnreadable = errors.New("the configuration could not be read; the daemon was not removed") + // errCronRevertCronNotWritten abandons a daemon revert because the cron entry that has to + // replace the daemon could not be written or read back. Nothing was changed: the daemon + // keeps scheduling the backup and the configuration still records the daemon engine. The + // dashboard keys on it to tell this outcome from a teardown that failed half-way. + errCronRevertCronNotWritten = errors.New("the cron entry could not be written") ) // existingWrapperCronFallback returns the crontab lines that ALREADY schedule ProxSave @@ -456,14 +478,21 @@ type cronRevertReport struct { // CronScheduled is not a measurement, and a caller must not turn it into one in either // direction. CronVerified bool - // CronScheduled is true only when a canonical proxsave cron line is READ BACK from the - // crontab after the write. migrateLegacyCronEntries is void and has four early returns that - // write nothing - unknown exec path, no binary to point at, `crontab -l` failing, `crontab - // -` failing - and none of them is visible to this function, which then removes the daemon - // unit anyway. A host with a broken or absent cron ended with no unit and no cron line at - // exit 0, and the run that would have noticed is the backup that was never scheduled. + // CronWritten is migrateLegacyCronEntries' own verdict on its write. It has four early + // returns that write nothing - unknown exec path, no binary to point at, `crontab -l` + // failing, `crontab -` failing - and each of them reports false here. + CronWritten bool + // CronScheduled is true only when the very line migrateLegacyCronEntries wrote is READ BACK + // from the crontab. An older proxsave line left by a failed write, with another schedule or + // another --config, does not count. + // + // applyCronMode removes the daemon only when CronWritten, CronVerified and CronScheduled are + // all true; otherwise it returns errCronRevertCronNotWritten with the daemon and the + // configuration untouched. So on every report that comes back with a nil error, or with a + // teardown error, the three are true. Removing the unit over a write that did not land used + // to leave a host with a broken or absent cron with no scheduler at all, at exit 0. // - // Read back rather than returned, for the reason cronRemovalOutcome.Verified exists: what + // Read back rather than trusted, for the reason cronRemovalOutcome.Verified exists: what // matters is whether the line is there, not whether the writer believed it wrote one. CronScheduled bool // ModeRecorded is true only when SCHEDULER_MODE=cron actually reached the config file. @@ -483,7 +512,9 @@ type cronRevertReport struct { // --upgrade from re-migrating a host that had just chosen cron. That key is gone: the retrofit // now honours SCHEDULER_MODE because it is PRESENT, so the mode written here is itself the // record, and nothing has to contradict it. The cron fallback is established first so a teardown -// failure never leaves the host unscheduled with a stale mode=daemon (F09-06). +// failure never leaves the host unscheduled with a stale mode=daemon (F09-06). When that cron +// entry cannot be written and read back, the revert stops there with errCronRevertCronNotWritten: +// no advisory, no config write, no teardown, and the daemon keeps scheduling the backup. // // "Make sure a cron schedule exists" is not always "append one": on a host already // scheduled through an operator wrapper, appending would produce a SECOND nightly backup. @@ -543,8 +574,28 @@ func applyCronMode(ctx context.Context, cfg *config.Config, configPath, execToke // host's only schedule on that is the wrong side of the project's own ranking, stated at // cron_indirect_refs.go and again for the /etc habitat below: a double schedule is a // recoverable annoyance the operator can see, an unscheduled host is silent data loss. - migrateLegacyCronEntriesFn(ctx, cfg.BaseDir, execToken, bootstrap, configCronSchedule(cfg), cfg.ConfigPath) - cronScheduled, cronVerified := canonicalCronLinePresent(ctx) + written, cronWritten := migrateLegacyCronEntriesFn(ctx, cfg.BaseDir, execToken, bootstrap, configCronSchedule(cfg), cfg.ConfigPath) + // The writer reports its failures only as WARNINGs, so the crontab is read back, and the line + // read back must be the one just written: an older proxsave line left by a failed write + // (another schedule or --config) does not schedule what the configuration says. This is the + // gate the install path applies (installRuntimePreparation.configureCron), and it sits before + // anything else changes: before the advisories, before SCHEDULER_MODE=cron is recorded and + // before the daemon unit is removed. Removing the daemon over a cron line that is not there + // left the host with no scheduler at all, and the backup that would have noticed is the one + // that was never scheduled. + cronScheduled, cronVerified := cronLineReadBack(ctx, written) + logging.Debug("daemon-remove: cron entry after the write: written=%v present=%v verified=%v line=%q", cronWritten, cronScheduled, cronVerified, written) + if !cronWritten || !cronScheduled || !cronVerified { + // Nothing has been changed yet, so the honest outcome is "nothing changed": the daemon + // keeps scheduling the backup and the configuration keeps saying so. The /etc advisory + // is not emitted either: its title says the host is reverting to cron, and it is not. + logBootstrapWarning(bootstrap, "daemon: the cron entry could not be written, so the daemon service was left in place and keeps scheduling the backup.") + return cronRevertReport{ + CronWritten: cronWritten, + CronScheduled: cronScheduled, + CronVerified: cronVerified, + }, errCronRevertCronNotWritten + } var unmanagedAdvisory []string if wrappers := existingWrapperCronFallback(ctx); len(wrappers) > 0 { unmanagedAdvisory = append(unmanagedAdvisory, fmt.Sprintf("Reverting to cron: %d unmanaged crontab line(s) also appear to schedule ProxSave:", len(wrappers))) @@ -557,14 +608,9 @@ func applyCronMode(ctx context.Context, cfg *config.Config, configPath, execToke // The count lives in the WARNING itself: DEBUG_LEVEL=warning hides the lines above it, // and what has to survive is that this host may now run the backup more than once. // - // It states the crontab as it was READ BACK, not the write it hoped for. cronScheduled - // above already established whether the line is there, and claiming a write that did not - // land is the defect this whole area keeps producing. - if cronScheduled { - logBootstrapWarning(bootstrap, "The cron entry is in the crontab and %d unmanaged cron line(s) were left untouched, so this host may run the backup more than once.", len(wrappers)) - } else { - logBootstrapWarning(bootstrap, "%d unmanaged cron line(s) were left untouched and no proxsave cron entry could be written, so tonight the host runs whatever those lines do.", len(wrappers)) - } + // "The cron entry is in the crontab" is a fact here, not a hope: the gate above has + // read the line back, and a revert whose line did not land never reaches this branch. + logBootstrapWarning(bootstrap, "The cron entry is in the crontab and %d unmanaged cron line(s) were left untouched, so this host may run the backup more than once.", len(wrappers)) } // HEALTHCHECK_ENABLED=false is the exact MIRROR of the write applyDaemonMode makes on @@ -627,6 +673,7 @@ func applyCronMode(ctx context.Context, cfg *config.Config, configPath, execToke return cronRevertReport{ SystemCronAdvisory: systemCronScheduleAdvisory(refs), UnmanagedAdvisory: unmanagedAdvisory, + CronWritten: cronWritten, CronScheduled: cronScheduled, CronVerified: cronVerified, ModeRecorded: modeRecorded, @@ -720,11 +767,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/daemon_setup_test.go b/cmd/proxsave/daemon_setup_test.go index 902966b6..3d1c065a 100644 --- a/cmd/proxsave/daemon_setup_test.go +++ b/cmd/proxsave/daemon_setup_test.go @@ -114,8 +114,9 @@ func TestApplyCronMode_PersistsCronModeBeforeTeardown(t *testing.T) { migrateLegacyCronEntriesFn = origMigrate }) migrateCalled := false - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) { + migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) (string, bool) { migrateCalled = true + return revertCronLine, true } removeDaemonServiceFn = func(context.Context, *logging.BootstrapLogger) error { return errors.New("teardown boom") @@ -123,14 +124,15 @@ func TestApplyCronMode_PersistsCronModeBeforeTeardown(t *testing.T) { // applyCronMode now consults the crontab before deciding whether to append a cron line // (#298). Without this stub the test would shell out to the host's real `crontab -l`, // making its verdict depend on whatever the machine running the suite has scheduled. - // An empty crontab is the "no wrapper" case, i.e. the append path this test asserts. + // A crontab holding only the line just written is the "no wrapper" case, i.e. the append + // path this test asserts, and the read-back the revert now requires before the teardown. origRead := crontabReadLinesFn origSystem := systemCronProxsaveRefsFn t.Cleanup(func() { crontabReadLinesFn = origRead systemCronProxsaveRefsFn = origSystem }) - crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, nil } + crontabReadLinesFn = func(context.Context) ([]string, error) { return []string{revertCronLine}, nil } // Same reason, second habitat: applyCronMode also reports a ProxSave schedule found in // /etc/crontab or /etc/cron.d, and systemCronPaths points at the REAL /etc. Unstubbed // this test would read the developer's own system cron - and pass there anyway. @@ -172,8 +174,9 @@ func TestApplyCronModeDefersWhileBackupRunning(t *testing.T) { return nil } migrateCalled := false - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) { + migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) (string, bool) { migrateCalled = true + return revertCronLine, true } dir := t.TempDir() @@ -216,7 +219,9 @@ func TestApplyCronModeProceedsWhenIdle(t *testing.T) { removeCalled = true return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} + migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) (string, bool) { + return revertCronLine, true + } // See TestApplyCronMode_PersistsCronModeBeforeTeardown: applyCronMode now reads the // crontab AND the system cron habitat (#298), and either left unstubbed would make this // test depend on the host it runs on. @@ -226,7 +231,7 @@ func TestApplyCronModeProceedsWhenIdle(t *testing.T) { crontabReadLinesFn = origRead systemCronProxsaveRefsFn = origSystem }) - crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, nil } + crontabReadLinesFn = func(context.Context) ([]string, error) { return []string{revertCronLine}, nil } systemCronProxsaveRefsFn = func() []indirectCronRef { return nil } dir := t.TempDir() @@ -278,7 +283,9 @@ func TestRunDaemonRemoveDefersWhenBackupRunning(t *testing.T) { removeCalled = true return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} + migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) (string, bool) { + return revertCronLine, true + } dir := t.TempDir() configPath := filepath.Join(dir, "backup.env") @@ -326,11 +333,13 @@ func TestApplyCronModeRollsBackHealthcheckEnabled(t *testing.T) { }) restartVerifyBackupRunning = func(string) bool { return false } // idle removeDaemonServiceFn = func(context.Context, *logging.BootstrapLogger) error { return nil } - migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) {} + migrateLegacyCronEntriesFn = func(context.Context, string, string, *logging.BootstrapLogger, string, string) (string, bool) { + return revertCronLine, true + } // This test is about the env write, not the crontab: pin the "ordinary host, no - // wrapper" shape so applyCronMode takes its normal append path and never touches the - // real `crontab -l` of the machine running the suite. - crontabReadLinesFn = func(context.Context) ([]string, error) { return nil, nil } + // wrapper" shape, with the line just written read back, so applyCronMode takes its normal + // append path and never touches the real `crontab -l` of the machine running the suite. + crontabReadLinesFn = func(context.Context) ([]string, error) { return []string{revertCronLine}, nil } wrapperCronLinesFn = func([]string) []string { return nil } systemCronProxsaveRefsFn = func() []indirectCronRef { return nil } diff --git a/cmd/proxsave/dashboard.go b/cmd/proxsave/dashboard.go index 9daaa363..d42bf429 100644 --- a/cmd/proxsave/dashboard.go +++ b/cmd/proxsave/dashboard.go @@ -718,10 +718,12 @@ func runDashboardDaemonAdmin(ctx context.Context, session *shell.Session, instal "DEFERRED - BACKUP RUNNING", "Daemon service: NOT removed, a backup is in progress.\nTry again once the backup finishes.") return } - // A failed TEARDOWN is the only revert failure that arrives with a populated report, and - // the generic screen below discards it: the duplicate-schedule finding, the /etc advisory - // and the config-write fact were all lost on the one path whose logger is muted for the - // whole operation and never flushed. + // A failed TEARDOWN arrives with a populated report, and the generic screen below + // discards it: the duplicate-schedule finding, the /etc advisory and the config-write + // fact were all lost on the one path whose logger is muted for the whole operation and + // never flushed. A revert abandoned because the cron entry could not be written + // (errCronRevertCronNotWritten) arrives with the read-back facts and goes to the same + // renderer, which tells the two apart by the sentinel. // // The guard keys on the SENTINEL rather than on the report looking empty, because // applyCronMode's two sentinels abort before anything is written and hand back a ZERO @@ -792,6 +794,8 @@ func runDashboardDaemonAdmin(ctx context.Context, session *shell.Session, instal // teardownErr is nil on every path that got as far as removing the daemon service. When it is // set, everything the report established BEFORE the teardown is still true and is stated; // everything the failed teardown killed is dropped. See the arm itself for which is which. +// When it is errCronRevertCronNotWritten there was no teardown at all: the revert stopped at the +// cron write, and the screen says the daemon was left in place. func buildCronRevertScreen(revert cronRevertReport, teardownErr error) (orchestrator.HealthcheckSetupLevel, string, string) { // One fact per LINE, each behind the label that names what it is about, in the same order on // every screen. The daemon-status screen already reads this way ("Scheduler mode:", "Daemon @@ -812,11 +816,29 @@ func buildCronRevertScreen(revert cronRevertReport, teardownErr error) (orchestr } found := len(revert.UnmanagedAdvisory) > 0 || len(revert.SystemCronAdvisory) > 0 + // THE REVERT WAS ABANDONED before anything changed: the cron entry that has to replace the + // daemon was not written, or was not read back. applyCronMode returned before the advisories, + // the config write and the teardown, so the daemon keeps scheduling the backup and the + // configuration still records the daemon engine (ModeRecorded is false). + // + // The cron line states what is known. A write the writer itself reported as failed is "NOT + // written" whatever the read-back says; a write it believed landed but that the crontab could + // not confirm is "could not be checked"; a readable crontab without the line is "NOT written". + if errors.Is(teardownErr, errCronRevertCronNotWritten) { + cron = "Cron entry: NOT written." + if revert.CronWritten && !revert.CronVerified { + cron = "Cron entry: could not be checked, the crontab was unreadable." + } + lines = append(lines, "Daemon service: left in place, it keeps scheduling the backup.", cron, config) + return orchestrator.HealthcheckSetupLevelError, "DAEMON NOT REMOVED", appendCronRevertAdvisories(strings.Join(lines, "\n"), revert) + } + // A FAILED TEARDOWN. removeDaemonServiceFn is the last thing applyCronMode does, so // everything the report carries was established before it and none of it is invalidated by - // it: the cron line was read back from the crontab, the wrapper lines were read from the - // crontab ProxSave never edits, the config write already returned, and the /etc scan runs - // after the teardown by design. What the failure DOES kill is the claim that the service was + // it: the cron line was read back from the crontab (applyCronMode reaches the teardown only + // after that read-back succeeded, so the line is always there), the wrapper lines were read + // from the crontab ProxSave never edits, the config write already returned, and the /etc scan + // runs after the teardown by design. What the failure DOES kill is the claim that the service was // removed, and that is the only line this arm rewrites. // // The host is left with a unit file still on disk (os.Remove failed) or deleted but not @@ -827,18 +849,15 @@ func buildCronRevertScreen(revert cronRevertReport, teardownErr error) (orchestr // class this area keeps producing. See backup_notifications.go on a failed teardown leaving a // live, transmitting daemon on a host whose config already reads cron. if teardownErr != nil { + // Two schedulers at the same minute: the daemon runs at SCHEDULER_TIME and that is the + // time the cron line carries. The per-run lock makes the loser exit 16, i.e. a failed + // backup reported every night - the third duplicate source in issue #298. The duplicate + // takes the keyword because it is what changes tonight. There is no "cron entry not + // written" variant here: a revert whose line was not read back stops at the cron gate + // above and never reaches the teardown. level := orchestrator.HealthcheckSetupLevelError - keyword := "DAEMON NOT REMOVED" - if revert.CronScheduled { - // Two schedulers at the same minute: the daemon runs at SCHEDULER_TIME and that is - // the time the cron line carries. The per-run lock makes the loser exit 16, i.e. a - // failed backup reported every night - the third duplicate source in issue #298. - // The duplicate takes the keyword because it is what changes tonight. - keyword = "DAEMON NOT REMOVED - DUPLICATE SCHEDULE" - cron = "Cron entry: in the crontab, so the backup may run twice." - } else { - cron = "Cron entry: NOT written." - } + keyword := "DAEMON NOT REMOVED - DUPLICATE SCHEDULE" + cron = "Cron entry: in the crontab, so the backup may run twice." lines = append(lines, "Daemon service: NOT removed, may still be running.", // Indented on its own line: it is an external tool's message, not a sentence of ours, @@ -866,30 +885,9 @@ func buildCronRevertScreen(revert cronRevertReport, teardownErr error) (orchestr level = orchestrator.HealthcheckSetupLevelWarn keyword = "REVERTED - DUPLICATE SCHEDULE" } - // Nothing scheduling the backup outranks everything else, so it is decided last. - // - // CronScheduled is an assertion about the ROOT crontab alone: canonicalCronLinePresent reads - // crontabReadLinesFn and nothing else, and the unmanaged entries it does not match are not - // proxsave-named. So with anything listed below, the host may well still be scheduled - and - // it is the same host where ProxSave could neither write its own line nor remove the other - // ones. The denial is stated only when there is nothing underneath to contradict it. - if !revert.CronScheduled { - level = orchestrator.HealthcheckSetupLevelError - keyword = "NO SCHEDULE" - cron = "Cron entry: NOT written, nothing is scheduling the backup." - if found { - keyword = "CRON ENTRY NOT WRITTEN" - cron = "Cron entry: NOT written." - } - // An unreadable crontab reaches here with CronScheduled false, and the denial above is - // then a measurement nobody took: the write may well have landed. The level stays Error - // because a host that cannot be checked may equally be a host with nothing scheduled, - // and that is the expensive one. - if !revert.CronVerified { - keyword = "CRON ENTRY NOT CHECKED" - cron = "Cron entry: could not be checked, the crontab was unreadable." - } - } + // There is no "nothing scheduled" arm here any more: applyCronMode reaches the teardown only + // after reading its own cron line back, so a revert that comes back without an error always + // left that line in the crontab. lines = append(lines, daemon, cron, config) if len(revert.UnmanagedAdvisory) > 0 { lines = append(lines, "Check your crons to remove duplication.") diff --git a/cmd/proxsave/dashboard_test.go b/cmd/proxsave/dashboard_test.go index 361f1a91..c1142078 100644 --- a/cmd/proxsave/dashboard_test.go +++ b/cmd/proxsave/dashboard_test.go @@ -1105,36 +1105,6 @@ func TestDashboardDaemonRevertWarnsOnADuplicateSchedule(t *testing.T) { wantKeyword: "REVERTED - DUPLICATE SCHEDULE", wantText: "1 entry(ies) in /etc unchanged", }, - { - // A cron entry that could not be written takes the level, because it is the worst - // thing that happened. It does NOT take the old headline: with an unmanaged line - // listed below, "nothing is scheduling the backup" would deny what the screen goes - // on to show, so the headline states the certain fact instead. - name: "unwritten entry takes the level, not the denial", - report: cronRevertReport{CronScheduled: false, CronVerified: true, ModeRecorded: true, UnmanagedAdvisory: []string{"1 unmanaged crontab line(s) also appear to schedule ProxSave:", " - 30 02 * * * /usr/local/sbin/nas-guard"}}, - wantLevel: orchestrator.HealthcheckSetupLevelError, - wantKeyword: "CRON ENTRY NOT WRITTEN", - wantText: "Cron entry: NOT written.", - }, - { - // With nothing listed underneath, the denial is true and stays. - name: "nothing left at all: the denial stands", - report: cronRevertReport{CronScheduled: false, CronVerified: true, ModeRecorded: true}, - wantLevel: orchestrator.HealthcheckSetupLevelError, - wantKeyword: "NO SCHEDULE", - wantText: "nothing is scheduling the backup", - }, - { - // ...and it stands only there. An unreadable crontab reaches this branch with the - // same false, but nobody measured it: the write may well have landed. The level stays - // Error because a host that cannot be checked may equally be one with nothing - // scheduled, and that is the expensive reading. - name: "the crontab could not be read: no denial, only the fact", - report: cronRevertReport{CronScheduled: false, CronVerified: false, ModeRecorded: true}, - wantLevel: orchestrator.HealthcheckSetupLevelError, - wantKeyword: "CRON ENTRY NOT CHECKED", - wantText: "Cron entry: could not be checked, the crontab was unreadable.", - }, } { t.Run(tc.name, func(t *testing.T) { installDashboardGates(t, true, true) @@ -1188,9 +1158,12 @@ func TestDashboardDaemonRevertWarnsOnADuplicateSchedule(t *testing.T) { } } -// The daemon is gone and nothing replaced it: that is the one revert outcome that is an ERROR, -// not a warning, and it must outrank every other clause on the screen. -func TestDashboardDaemonRevertReportsAnUnscheduledHost(t *testing.T) { +// A revert whose cron entry could not be written is abandoned before anything changes, and the +// dashboard has to say so on the screen that is its only channel: the logger is muted for the +// whole operation. The daemon line must not borrow the teardown failure's wording ("NOT removed, +// may still be running"): nobody tried to remove it, and it is certainly still scheduling the +// backup. The title and the keyword of the failure screen stay, because both are true. +func TestDashboardDaemonRevertKeepsTheDaemonWhenTheCronEntryIsNotWritten(t *testing.T) { installDashboardGates(t, true, true) origCfg, origShow := daemonStatusLoadConfig, showDaemonResultScreenFn t.Cleanup(func() { daemonStatusLoadConfig, showDaemonResultScreenFn = origCfg, origShow }) @@ -1198,16 +1171,17 @@ func TestDashboardDaemonRevertReportsAnUnscheduledHost(t *testing.T) { return &config.Config{SchedulerMode: "daemon"}, nil } daemonApplyCronMode = func(context.Context, *config.Config, string, string, *logging.BootstrapLogger) (cronRevertReport, error) { - return cronRevertReport{CronScheduled: false, CronVerified: true, ModeRecorded: true}, nil + return cronRevertReport{CronWritten: true, CronVerified: true}, errCronRevertCronNotWritten } type shown struct { + title string level orchestrator.HealthcheckSetupLevel keyword string text string } ch := make(chan shown, 1) - showDaemonResultScreenFn = func(_ context.Context, _ *shell.Session, _ string, level orchestrator.HealthcheckSetupLevel, kw, explanation string) { - ch <- shown{level, kw, explanation} + showDaemonResultScreenFn = func(_ context.Context, _ *shell.Session, title string, level orchestrator.HealthcheckSetupLevel, kw, explanation string) { + ch <- shown{title, level, kw, explanation} } driver := installDashboardSessionSeam(t) @@ -1229,129 +1203,27 @@ func TestDashboardDaemonRevertReportsAnUnscheduledHost(t *testing.T) { t.Fatal("dashboard did not resolve") } - if got.level != orchestrator.HealthcheckSetupLevelError { - t.Errorf("an unscheduled host is an error, level = %v", got.level) - } - if !strings.Contains(got.text, "nothing is scheduling the backup") { - t.Errorf("the screen must say nothing is scheduling the backup, got:\n%s", got.text) - } - if strings.Contains(got.text, "Cron entry: in the crontab") { - t.Errorf("the success wording must not survive next to it, got:\n%s", got.text) - } -} - -// The screen is the ONLY channel on this path: the logger is muted for the whole operation and -// never flushed, so a fact the screen drops is a fact the operator never gets. The unscheduled -// branch used to REPLACE the message rather than compose it, which threw away the config-write -// clause with it. On the CLI the same host reads both, because applyCronMode's own warning is -// still on screen there. -func TestDashboardDaemonRevertKeepsEveryFactOnTheUnscheduledScreen(t *testing.T) { - installDashboardGates(t, true, true) - origCfg, origShow := daemonStatusLoadConfig, showDaemonResultScreenFn - t.Cleanup(func() { daemonStatusLoadConfig, showDaemonResultScreenFn = origCfg, origShow }) - daemonStatusLoadConfig = func(string, string) (*config.Config, error) { - return &config.Config{SchedulerMode: "daemon"}, nil - } - daemonApplyCronMode = func(context.Context, *config.Config, string, string, *logging.BootstrapLogger) (cronRevertReport, error) { - return cronRevertReport{CronScheduled: false, CronVerified: true, ModeRecorded: false}, nil + if got.title != "Daemon disable failed" { + t.Errorf("title = %q, want the failure screen's own title", got.title) } - shown := make(chan string, 1) - showDaemonResultScreenFn = func(_ context.Context, _ *shell.Session, _ string, _ orchestrator.HealthcheckSetupLevel, kw, explanation string) { - shown <- kw + "\n" + explanation - } - - driver := installDashboardSessionSeam(t) - res := driver.spawn(&cli.Args{}) - driver.waitScreen("Dashboard") - driver.keys("down down down down down down down down down enter") // Disable daemon - - var msg string - select { - case msg = <-shown: - case <-time.After(uitest.Deadline(60 * time.Second)): - t.Fatal("the revert never reached the result screen") - } - driver.waitScreen("Dashboard") - driver.keys("esc") - select { - case <-res: - case <-time.After(uitest.Deadline(60 * time.Second)): - t.Fatal("dashboard did not resolve") - } - - if !strings.Contains(msg, "NO SCHEDULE") { - t.Errorf("nothing scheduling the backup is still the headline, got:\n%s", msg) - } - if !strings.Contains(msg, "nothing is scheduling the backup") { - t.Errorf("the screen must state the unscheduled host, got:\n%s", msg) - } - // The second fact, which has no other channel here. - if !strings.Contains(msg, "Configuration: NOT updated") { - t.Errorf("the screen must also state that the configuration was not written, got:\n%s", msg) - } -} - -// CronScheduled is an assertion about the ROOT crontab alone: canonicalCronLinePresent reads -// crontabReadLinesFn and nothing else. So "nothing is scheduling the backup" is false on a host -// whose proxsave entry lives under /etc, and that is exactly the host where ProxSave could not -// write its own line and cannot remove the other one either - the screen said nothing was -// scheduled and then listed, three lines below, the entry that schedules it. -func TestDashboardDaemonRevertDoesNotDenyAScheduleItJustListed(t *testing.T) { - advisory := []string{ - "Reverting to cron: 1 possible ProxSave cron line(s) under /etc:", - " - 0 5 * * * root /usr/local/bin/proxsave --backup [/etc/cron.d/proxsave]", - "ProxSave owns the root crontab only and never edits files it did not place, 1 entry(ies) in /etc unchanged", - } - installDashboardGates(t, true, true) - origCfg, origShow := daemonStatusLoadConfig, showDaemonResultScreenFn - t.Cleanup(func() { daemonStatusLoadConfig, showDaemonResultScreenFn = origCfg, origShow }) - daemonStatusLoadConfig = func(string, string) (*config.Config, error) { - return &config.Config{SchedulerMode: "daemon"}, nil - } - daemonApplyCronMode = func(context.Context, *config.Config, string, string, *logging.BootstrapLogger) (cronRevertReport, error) { - return cronRevertReport{CronScheduled: false, CronVerified: true, ModeRecorded: true, SystemCronAdvisory: advisory}, nil - } - shown := make(chan string, 1) - showDaemonResultScreenFn = func(_ context.Context, _ *shell.Session, _ string, _ orchestrator.HealthcheckSetupLevel, kw, explanation string) { - shown <- kw + "\n" + explanation - } - - driver := installDashboardSessionSeam(t) - res := driver.spawn(&cli.Args{}) - driver.waitScreen("Dashboard") - driver.keys("down down down down down down down down down enter") // Disable daemon - - var msg string - select { - case msg = <-shown: - case <-time.After(uitest.Deadline(60 * time.Second)): - t.Fatal("the revert never reached the result screen") - } - driver.waitScreen("Dashboard") - driver.keys("esc") - select { - case <-res: - case <-time.After(uitest.Deadline(60 * time.Second)): - t.Fatal("dashboard did not resolve") - } - - if strings.Contains(msg, "nothing is scheduling the backup") { - t.Errorf("the screen may not deny a schedule it lists below, got:\n%s", msg) + if got.level != orchestrator.HealthcheckSetupLevelError { + t.Errorf("level = %v, want Error", got.level) } - // What IS certain, and what the operator has to act on. - if !strings.Contains(msg, "Cron entry: NOT written.") { - t.Errorf("the screen must still state that the cron entry was not written, got:\n%s", msg) + if got.keyword != "DAEMON NOT REMOVED" { + t.Errorf("keyword = %q, want DAEMON NOT REMOVED", got.keyword) } - if !strings.Contains(msg, advisory[1]) { - t.Errorf("the /etc finding must still be listed, got:\n%s", msg) + const want = "Daemon service: left in place, it keeps scheduling the backup.\n" + + "Cron entry: NOT written.\n" + + "Configuration: NOT updated, it still records the daemon engine." + if got.text != want { + t.Errorf("the screen must read exactly:\n%s\ngot:\n%s", want, got.text) } } // The screen is the only channel on this path, and it carried the /etc findings as rendered lines -// while carrying the root-crontab ones as a bare COUNT. So the unscheduled branch, which composes -// its own text, had nothing to list for them and the count went with it: the sentence pointed at -// an entry "below" that was never printed. The two habitats are now carried the same way, which -// is also how the CLI has always printed them. +// while carrying the root-crontab ones as a bare COUNT: a sentence pointing at an entry "below" +// had nothing to list for one of them. The two habitats are now carried the same way, which is +// also how the CLI has always printed them. func TestDashboardDaemonRevertListsBothHabitats(t *testing.T) { unmanaged := []string{ "1 unmanaged crontab line(s) also appear to schedule ProxSave:", @@ -1369,7 +1241,8 @@ func TestDashboardDaemonRevertListsBothHabitats(t *testing.T) { } daemonApplyCronMode = func(context.Context, *config.Config, string, string, *logging.BootstrapLogger) (cronRevertReport, error) { return cronRevertReport{ - CronScheduled: false, + CronWritten: true, + CronScheduled: true, CronVerified: true, ModeRecorded: true, UnmanagedAdvisory: unmanaged, @@ -1404,35 +1277,14 @@ func TestDashboardDaemonRevertListsBothHabitats(t *testing.T) { t.Fatal("dashboard did not resolve") } - // The headline states the fact that is certain. NO SCHEDULE would deny two schedules the - // screen lists underneath it. - if got.keyword != "CRON ENTRY NOT WRITTEN" { - t.Errorf("keyword = %q, want CRON ENTRY NOT WRITTEN", got.keyword) + if got.keyword != "REVERTED - DUPLICATE SCHEDULE" { + t.Errorf("keyword = %q, want REVERTED - DUPLICATE SCHEDULE", got.keyword) } for _, want := range append(append([]string{}, unmanaged...), etc...) { if !strings.Contains(got.text, want) { t.Errorf("the screen must list %q, got:\n%s", want, got.text) } } - // And with nothing left to schedule the host, the denial is true and stays. - daemonApplyCronMode = func(context.Context, *config.Config, string, string, *logging.BootstrapLogger) (cronRevertReport, error) { - return cronRevertReport{CronScheduled: false, CronVerified: true, ModeRecorded: true}, nil - } - driver2 := installDashboardSessionSeam(t) - res2 := driver2.spawn(&cli.Args{}) - driver2.waitScreen("Dashboard") - driver2.keys("down down down down down down down down down enter") - select { - case got = <-ch: - case <-time.After(uitest.Deadline(60 * time.Second)): - t.Fatal("the second revert never reached the result screen") - } - driver2.waitScreen("Dashboard") - driver2.keys("esc") - <-res2 - if got.keyword != "NO SCHEDULE" { - t.Errorf("with nothing listed the denial is true: keyword = %q, want NO SCHEDULE", got.keyword) - } } // A failed install leaves the host in one of two states and the screen used to name neither. @@ -1731,17 +1583,6 @@ func TestCronRevertScreenOnAFailedTeardown(t *testing.T) { // exactly what this failure disproves. wantSilent: []string{"while no daemon is installed"}, }, - { - name: "the cron entry was not written either", - revert: cronRevertReport{ModeRecorded: true}, - err: teardown, - wantLevel: orchestrator.HealthcheckSetupLevelError, - wantKeyword: "DAEMON NOT REMOVED", - wantSays: []string{"Daemon service: NOT removed, may still be running.", "Cron entry: NOT written."}, - // The old wording closed with "the daemon that is still there may be the only thing - // still scheduling the backup", which denies whatever the advisories below list. - wantSilent: []string{"nothing is scheduling the backup", "may run twice", "only thing still scheduling"}, - }, { name: "both habitats still have to be listed", revert: cronRevertReport{CronScheduled: true, ModeRecorded: true, UnmanagedAdvisory: unmanaged, SystemCronAdvisory: etc}, @@ -1760,14 +1601,6 @@ func TestCronRevertScreenOnAFailedTeardown(t *testing.T) { wantSays: []string{"Daemon service: removed.", "Cron entry: in the crontab."}, wantSilent: []string{"NOT removed"}, }, - { - name: "success, nothing scheduling", - revert: cronRevertReport{ModeRecorded: true, CronVerified: true}, - wantLevel: orchestrator.HealthcheckSetupLevelError, - wantKeyword: "NO SCHEDULE", - wantSays: []string{"nothing is scheduling the backup"}, - wantSilent: []string{"NOT removed"}, - }, { name: "success with an /etc finding", revert: cronRevertReport{CronScheduled: true, ModeRecorded: true, SystemCronAdvisory: etc}, @@ -1799,6 +1632,56 @@ func TestCronRevertScreenOnAFailedTeardown(t *testing.T) { } } +// A revert abandoned at the cron gate never reached the teardown, so its screen may not borrow the +// teardown's daemon line, and its cron line states what is known: a write the writer reported as +// failed is NOT written whatever the crontab shows; a write it believed landed but that could not +// be read back is unchecked; a readable crontab without the line is NOT written. Nothing past the +// gate ran, so no advisory and no config record can be on it. +func TestCronRevertScreenWhenTheCronEntryIsNotWritten(t *testing.T) { + const daemonLine = "Daemon service: left in place, it keeps scheduling the backup." + const configLine = "Configuration: NOT updated, it still records the daemon engine." + for _, tc := range []struct { + name string + revert cronRevertReport + wantCron string + }{ + { + name: "the writer reported a failure", + revert: cronRevertReport{CronWritten: false, CronVerified: true}, + wantCron: "Cron entry: NOT written.", + }, + { + name: "the writer reported a failure and the crontab is unreadable", + revert: cronRevertReport{CronWritten: false, CronVerified: false}, + wantCron: "Cron entry: NOT written.", + }, + { + name: "the line is not in a readable crontab", + revert: cronRevertReport{CronWritten: true, CronVerified: true}, + wantCron: "Cron entry: NOT written.", + }, + { + name: "the crontab could not be read back", + revert: cronRevertReport{CronWritten: true, CronVerified: false}, + wantCron: "Cron entry: could not be checked, the crontab was unreadable.", + }, + } { + t.Run(tc.name, func(t *testing.T) { + level, keyword, msg := buildCronRevertScreen(tc.revert, errCronRevertCronNotWritten) + if level != orchestrator.HealthcheckSetupLevelError { + t.Errorf("level = %v, want Error", level) + } + if keyword != "DAEMON NOT REMOVED" { + t.Errorf("keyword = %q, want DAEMON NOT REMOVED", keyword) + } + want := daemonLine + "\n" + tc.wantCron + "\n" + configLine + if msg != want { + t.Errorf("the screen must read exactly:\n%s\ngot:\n%s", want, msg) + } + }) + } +} + // The wiring. The renderer above can be perfect and the operator still see one line, because the // generic failure arm showed opErr.Error() and returned before the report was ever read. This // test drives the real dashboard action and asserts on what reaches the only channel open here. 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/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/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/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/healthcheck_portal_cli_test.go b/cmd/proxsave/healthcheck_portal_cli_test.go new file mode 100644 index 00000000..44df0558 --- /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: 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" + 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/install.go b/cmd/proxsave/install.go index b58f2a51..e91ad619 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") @@ -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 = "" } @@ -725,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 @@ -774,12 +783,14 @@ func runEncryptionSetupIfNeeded(ctx context.Context, configPath string, enableEn return nil } -func runPostInstallSymlinksAndCron(ctx context.Context, baseDir string, execInfo ExecInfo, bootstrap *logging.BootstrapLogger, cronSchedule, configPath string) { +// runPostInstallSymlinksAndCron returns the cron entry written and ok=true once crontab accepted +// it (migrateLegacyCronEntries). +func runPostInstallSymlinksAndCron(ctx context.Context, baseDir string, execInfo ExecInfo, bootstrap *logging.BootstrapLogger, cronSchedule, configPath string) (written string, ok bool) { done := logging.DebugStartBootstrap(bootstrap, "post-install setup", "base=%s", baseDir) defer func() { done(nil) }() if err := runPostInstallEntrypoints(ctx, execInfo, bootstrap); err != nil { - return + return "", false } // Ensure a cron entry for the Go binary: preserve an entry that already @@ -789,7 +800,7 @@ func runPostInstallSymlinksAndCron(ctx context.Context, baseDir string, execInfo cronSchedule = resolveCronScheduleFromEnv() } logging.DebugStepBootstrap(bootstrap, "post-install setup", "migrating cron entries") - migrateLegacyCronEntries(ctx, baseDir, execInfo.ExecPath, bootstrap, cronSchedule, configPath) + return migrateLegacyCronEntries(ctx, baseDir, execInfo.ExecPath, bootstrap, cronSchedule, configPath) } // runPostInstallEntrypoints is shared by the early daemon and late cron paths. @@ -1568,3 +1579,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_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_cron_switch_readback_test.go b/cmd/proxsave/install_cron_switch_readback_test.go new file mode 100644 index 00000000..409e0e4f --- /dev/null +++ b/cmd/proxsave/install_cron_switch_readback_test.go @@ -0,0 +1,124 @@ +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. The line read back +// must be the one written: an older proxsave line left by a refused write, or a line with +// another --config, does not count. +func TestInstallCronSwitchReadsTheCrontabBack(t *testing.T) { + const olderLine = "0 5 * * * /usr/local/bin/proxsave --backup\n" + const otherConfig = "0 2 * * * /usr/local/bin/proxsave --backup --config /etc/other/backup.env\n" + for _, tc := range []struct { + name string + daemon bool + writeOK bool + wantErr string + wantUnit bool + wantWarn bool + before string // proxsave lines already in the crontab + readBack string // when set, the table crontab installs whatever it was given + }{ + {"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, "", ""}, + {"daemon host, cron refused, an older proxsave line left", true, false, "the cron entry could not be written", true, true, olderLine, ""}, + {"daemon host, another --config read back", true, true, "the cron entry could not be written", true, true, "", otherConfig}, + } { + 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"+tc.before), 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" + } + if tc.readBack != "" { + write = "cat > /dev/null\nprintf '%s' '" + tc.readBack + "' > \"" + 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..9562863d 100644 --- a/cmd/proxsave/install_runtime.go +++ b/cmd/proxsave/install_runtime.go @@ -92,10 +92,24 @@ func newInstallRuntimePreparation(configPath, baseDir string, execInfo ExecInfo, if err := ctx.Err(); err != nil { return err } - runPostInstallSymlinksAndCron(ctx, baseDir, execInfo, bootstrap, schedule, configPath) + written, ok := runPostInstallSymlinksAndCron(ctx, baseDir, execInfo, bootstrap, schedule, configPath) 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". The line read back + // must be the one just written: an older proxsave line left by a failed write + // (another schedule or --config) does not count. + present, verified := cronLineReadBack(ctx, written) + logging.Debug("install: cron entry after the write: written=%v present=%v verified=%v", ok, present, verified) + if !ok || !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) 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_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/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 } diff --git a/cmd/proxsave/main.go b/cmd/proxsave/main.go index e54018c2..90c78bcb 100644 --- a/cmd/proxsave/main.go +++ b/cmd/proxsave/main.go @@ -10,7 +10,7 @@ import ( ) const ( - goRuntimeMinVersion = "1.26.10" + goRuntimeMinVersion = "1.26.11" networkPreflightTimeout = 2 * time.Second bytesPerMegabyte int64 = 1024 * 1024 defaultDirPerm = 0o755 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_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/main_modes.go b/cmd/proxsave/main_modes.go index f8e10545..48253232 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 := "" @@ -398,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 diff --git a/cmd/proxsave/main_update.go b/cmd/proxsave/main_update.go index af9b96c3..04fa5e98 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,9 @@ func checkForUpdates(ctx context.Context, logger *logging.Logger, currentVersion latestTag, latestVersion, releaseBody, err := fetchLatestRelease(checkCtx) if err != nil { + 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) return &UpdateInfo{ NewVersion: false, diff --git a/cmd/proxsave/permissions.go b/cmd/proxsave/permissions.go index 36aaafd2..96adda94 100644 --- a/cmd/proxsave/permissions.go +++ b/cmd/proxsave/permissions.go @@ -8,6 +8,7 @@ import ( "io/fs" "os" "os/user" + "path/filepath" "strconv" "strings" "time" @@ -39,29 +40,49 @@ 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) 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 @@ -79,46 +100,78 @@ 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 } - info, err := safefs.Stat(ctx, dir, timeout) + // The bounded walk Lstats its root and never follows a symlink, so a configured + // directory that is a symlink was walked as a leaf: only the link was chowned. + // The configured root alone is resolved; symlinks inside it stay unfollowed. + // A failure to resolve it is a failure to read it, reported as the stat is. + root, err := safefs.Run(ctx, "evalsymlinks", dir, timeout, func() (string, error) { + return filepath.EvalSymlinks(dir) + }) + var info fs.FileInfo + if err == nil { + info, err = safefs.Stat(ctx, root, timeout) + } if err != nil { 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 } - fsInfo, err := fsDetector.DetectFilesystem(ctx, dir) + if root != dir { + logger.Debug("Permissions: %s resolves to %s; detecting and walking the resolved directory", dir, root) + } + + fsInfo, err := fsDetector.DetectFilesystem(ctx, root) if err != nil { if errors.Is(err, safefs.ErrTimeout) { logger.Warning("Permissions: filesystem detection on %s timed out after %s; skipping chown/chmod (dead/stale mount?)", dir, timeout) } 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 { + // A dry run walks nothing, and its line keeps naming the configured path. + walkRoot := root + if dryRun { + walkRoot = dir + } + logger.Debug("Applying permissions on path: %s (uid=%d,gid=%d)", walkRoot, uid, gid) + failed, err := applyDirOwnershipRecursive(ctx, walkRoot, uid, gid, timeout, dryRun, logger) + switch { + case err != nil: logger.Warning("Failed to apply permissions on %s: %v", dir, err) + outcome.skipped = append(outcome.skipped, dir+" could not be changed") + case failed > 0: + logger.Warning("Failed to apply permissions on %s: %d entries could not be changed", dir, failed) + outcome.skipped = append(outcome.skipped, dir+" could not be changed") } } - return nil + return outcome } func resolveUserGroupIDs(userName, groupName string) (int, int, error) { @@ -153,17 +206,39 @@ func resolveUserGroupIDs(userName, groupName string) (int, int, error) { // by FS_IO_TIMEOUT (filepath.WalkDir's internal readdir/lstat would otherwise // block forever in an uninterruptible syscall on a dead/stale mount). In dry-run // it mutates nothing, matching the read-only dry-run security preflight. -func applyDirOwnershipRecursive(ctx context.Context, root string, uid, gid int, timeout time.Duration, dryRun bool, logger *logging.Logger) error { +// +// An entry whose chown or chmod failed does not stop the walk; failed is the count of +// such entries. err is the error that stopped the walk itself (a cancelled context). +func applyDirOwnershipRecursive(ctx context.Context, root string, uid, gid int, timeout time.Duration, dryRun bool, logger *logging.Logger) (failed int, err error) { if dryRun { logger.Info("DRY RUN: would recursively set ownership %d:%d, 0750 directory perms and 0640 backup file perms on %s", uid, gid, root) - return nil + return 0, nil } logger.Debug("Walking directory tree for permissions: %s (uid=%d,gid=%d)", root, uid, gid) - return safefs.WalkBounded(ctx, root, timeout, func(path string, d fs.DirEntry, walkErr error) error { - return applyOwnershipWalkEntry(ctx, path, d, walkErr, uid, gid, timeout, logger) + var failures walkFailures + err = safefs.WalkBounded(ctx, root, timeout, func(path string, d fs.DirEntry, walkErr error) error { + return applyOwnershipWalkEntry(ctx, path, d, walkErr, uid, gid, timeout, logger, &failures) }) + logger.Debug("Permission walk on %s finished: %d entries could not be changed, walk error: %v", root, failures.count, err) + return failures.count, err +} + +// walkFailures counts the walked entries that could not be changed, each once. The walk +// reports a directory it could not read right after visiting that directory, so a +// directory whose chmod failed and whose read then failed is the same entry, counted once. +type walkFailures struct { + count int + lastPath string +} + +func (f *walkFailures) add(path string) { + if f.count > 0 && f.lastPath == path { + return + } + f.count++ + f.lastPath = path } // applyOwnershipWalkEntry applies bounded ownership/permissions to a single walked @@ -171,19 +246,28 @@ func applyDirOwnershipRecursive(ctx context.Context, root string, uid, gid int, // bounded walk) is logged and SKIPPED (return nil) rather than propagated: // returning it would abort the walk, leaving every not-yet-visited entry with its // old ownership. One unreadable/stale subtree must not block fixing the rest of -// the backup tree. -func applyOwnershipWalkEntry(ctx context.Context, path string, d fs.DirEntry, walkErr error, uid, gid int, timeout time.Duration, logger *logging.Logger) error { +// the backup tree, but it was not changed either, so it is counted in failed like an +// entry whose chown or chmod failed. Each entry is counted once. +func applyOwnershipWalkEntry(ctx context.Context, path string, d fs.DirEntry, walkErr error, uid, gid int, timeout time.Duration, logger *logging.Logger, failed *walkFailures) error { if walkErr != nil { - logger.Debug("permission walk: skipping %s: %v", path, walkErr) + logger.Debug("permission walk: skipping %s, its subtree is not changed: %v", path, walkErr) + failed.add(path) return nil } + entryFailed := false + defer func() { + if entryFailed { + failed.add(path) + } + }() // Lchown (not Chown): bounded, and never follows a symlink to chown a target // outside the backup tree (the walk itself never descends symlinks). For // regular files and directories it is identical to chown. if err := safefs.Lchown(ctx, path, uid, gid, timeout); err != nil { - // Do not stop on chown errors (including timeouts); just log at debug level. + // Do not stop on chown errors (including timeouts); log at debug level and count. logger.Debug("chown failed on %s: %v", path, err) + entryFailed = true } if d != nil && d.IsDir() { @@ -197,6 +281,7 @@ func applyOwnershipWalkEntry(ctx context.Context, path string, d fs.DirEntry, wa // mode) both bounds the call and structurally avoids the G302 finding. if err := safefs.Lchmod(ctx, path, 0o750, timeout); err != nil { logger.Debug("chmod failed on %s: %v", path, err) + entryFailed = true } return nil } @@ -208,6 +293,7 @@ func applyOwnershipWalkEntry(ctx context.Context, path string, d fs.DirEntry, wa if d != nil && d.Type().IsRegular() && storage.IsBackupSetFile(d.Name()) { if err := safefs.Lchmod(ctx, path, backup.SharedArtifactFilePerm, timeout); err != nil { logger.Debug("chmod failed on %s: %v", path, err) + entryFailed = true } } return nil @@ -286,16 +372,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_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/cmd/proxsave/permissions_footer_skip_test.go b/cmd/proxsave/permissions_footer_skip_test.go new file mode 100644 index 00000000..7869427b --- /dev/null +++ b/cmd/proxsave/permissions_footer_skip_test.go @@ -0,0 +1,87 @@ +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) + } +} + +// A backup path whose walk left an entry unchanged is a skipped path: the footer is a +// warning that names it, never "normalized correctly (including backup paths)". +func TestPermissionsFooterNamesABackupPathWithAFailedEntry(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("root opens a 0000 file, so its chmod does not fail") + } + usr, grp := currentUserGroup(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) + } + } + // The chmod of a backup set file opens it first; a 0000 file cannot be opened by + // its non-root owner, so that entry is the one the walk cannot change. + if err := os.WriteFile(filepath.Join(base, "backup", "pve1-backup-20261001-010101.tar.xz"), []byte("x"), 0o000); 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=" + usr + "\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) + wantMessage := "permissions and ownership normalized; backup paths skipped: " + filepath.Join(base, "backup") + " could not be changed" + if status != "warning" || message != wantMessage { + t.Fatalf("status=%q message=%q, want %q %q", status, message, "warning", wantMessage) + } +} diff --git a/cmd/proxsave/permissions_symlink_root_test.go b/cmd/proxsave/permissions_symlink_root_test.go new file mode 100644 index 00000000..730d6d1d --- /dev/null +++ b/cmd/proxsave/permissions_symlink_root_test.go @@ -0,0 +1,54 @@ +package main + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/tis24dev/proxsave/internal/config" +) + +// The bounded walk Lstats its root and never follows a symlink, so a BACKUP_PATH that +// is a symlink to a directory was walked as a leaf: the link was chowned, the directory +// behind it kept its modes, and the footer still said "normalized correctly". The +// configured root is now resolved before the walk; symlinks inside stay unfollowed. +func TestApplyBackupPermissionsFollowsASymlinkedRoot(t *testing.T) { + usr, grp := currentUserGroup(t) + base := t.TempDir() + target := filepath.Join(base, "target") + sub := filepath.Join(target, "sub") + outside := filepath.Join(base, "outside") + for _, d := range []string{target, sub, outside} { + if err := os.Mkdir(d, 0o700); err != nil { + t.Fatal(err) + } + } + file := filepath.Join(target, "pve1-backup-20261001-010101.tar.xz") + if err := os.WriteFile(file, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + if err := os.Symlink(outside, filepath.Join(target, "inner-link")); err != nil { + t.Fatal(err) + } + link := filepath.Join(base, "backup") + if err := os.Symlink(target, link); err != nil { + t.Fatal(err) + } + + cfg := &config.Config{BackupUser: usr, BackupGroup: grp, BackupPath: link, FsIoTimeoutSeconds: 30} + logger, buf := bufLogger() + outcome := applyBackupPermissionsOutcome(context.Background(), cfg, logger, false) + if len(outcome.skipped) != 0 || outcome.documentedSkip { + t.Fatalf("outcome %+v, want nothing skipped; log:\n%s", outcome, buf.String()) + } + for p, want := range map[string]os.FileMode{target: 0o750, sub: 0o750, file: 0o640, outside: 0o700} { + info, err := os.Stat(p) + if err != nil { + t.Fatal(err) + } + if info.Mode().Perm() != want { + t.Errorf("%s: mode %o, want %o", p, info.Mode().Perm(), want) + } + } +} diff --git a/cmd/proxsave/permissions_walk_audited_test.go b/cmd/proxsave/permissions_walk_audited_test.go index 217b3154..f315ba84 100644 --- a/cmd/proxsave/permissions_walk_audited_test.go +++ b/cmd/proxsave/permissions_walk_audited_test.go @@ -1,13 +1,16 @@ package main import ( + "bytes" "context" "errors" "io" "os" "path/filepath" + "strings" "testing" + "github.com/tis24dev/proxsave/internal/config" "github.com/tis24dev/proxsave/internal/logging" "github.com/tis24dev/proxsave/internal/types" ) @@ -27,10 +30,51 @@ func permWalkTestLogger() *logging.Logger { func TestApplyOwnershipWalkEntry_TraversalErrorIsSkippedNotAborted(t *testing.T) { // A non-nil walkErr (with a nil DirEntry, as WalkDir passes on read failures) - // must return nil so the walk continues to the rest of the tree. - if err := applyOwnershipWalkEntry(context.Background(), "/some/unreadable/subdir", nil, errors.New("permission denied"), 0, 0, 0, permWalkTestLogger()); err != nil { + // must return nil so the walk continues to the rest of the tree, and the subtree + // it could not read is counted as an entry that could not be changed. + var failed walkFailures + if err := applyOwnershipWalkEntry(context.Background(), "/some/unreadable/subdir", nil, errors.New("permission denied"), 0, 0, 0, permWalkTestLogger(), &failed); err != nil { t.Fatalf("a traversal error must be skipped (return nil) so the walk continues, got %v", err) } + if failed.count != 1 { + t.Fatalf("an unread subtree was counted %d times, want 1", failed.count) + } +} + +// A directory whose chmod failed is reported again when its read fails: one entry. +func TestWalkFailuresCountsAnEntryOnce(t *testing.T) { + var failed walkFailures + for _, p := range []string{"/b/sub", "/b/sub", "/b/other", "/b/sub"} { + failed.add(p) + } + if failed.count != 3 { + t.Fatalf("count = %d, want 3 (the repeated report of /b/sub right after it is one entry)", failed.count) + } +} + +// A subdirectory the walk cannot read leaves its subtree unchanged: the pass reports +// it, so the footer is not "normalized correctly" over a subtree never visited. +func TestApplyBackupPermissions_UnreadableSubdirIsReported(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("root reads a 0000 directory") + } + dir := t.TempDir() + sub := filepath.Join(dir, "sub") + if err := os.Mkdir(sub, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(sub, "file"), []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + if err := os.Chmod(sub, 0o000); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chmod(sub, 0o700) }) + + failed, err := applyDirOwnershipRecursive(context.Background(), dir, os.Getuid(), os.Getgid(), 0, false, permWalkTestLogger()) + if err != nil || failed != 1 { + t.Fatalf("failed = %d, err = %v, want the unreadable subdirectory counted once and no walk error", failed, err) + } } func TestApplyOwnershipWalkEntry_NormalEntryReturnsNil(t *testing.T) { @@ -45,7 +89,41 @@ func TestApplyOwnershipWalkEntry_NormalEntryReturnsNil(t *testing.T) { } // chown to our own uid/gid is a harmless no-op; the entry must process cleanly. - if err := applyOwnershipWalkEntry(context.Background(), file, entries[0], nil, os.Getuid(), os.Getgid(), 0, permWalkTestLogger()); err != nil { + var failed walkFailures + if err := applyOwnershipWalkEntry(context.Background(), file, entries[0], nil, os.Getuid(), os.Getgid(), 0, permWalkTestLogger(), &failed); err != nil { t.Fatalf("a normal entry must return nil, got %v", err) } + if failed.count != 0 { + t.Fatalf("a clean entry was counted as failed (%d)", failed.count) + } +} + +// An entry whose chown fails no longer vanishes into a DEBUG line: the walk goes on over +// every entry and the pass reports how many could not be changed, so the install footer +// is not "normalized correctly" over them. +func TestApplyBackupPermissions_FailedEntriesAreReported(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("root can chown to uid 0") + } + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "file"), []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + failed, err := applyDirOwnershipRecursive(context.Background(), dir, 0, 0, 0, false, permWalkTestLogger()) + if err != nil || failed != 2 { + t.Fatalf("failed = %d, err = %v, want both entries (the directory and its file) counted and no walk error", failed, err) + } + + var out bytes.Buffer + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&out) + cfg := &config.Config{BackupUser: "root", BackupGroup: "root", BackupPath: dir} + outcome := applyBackupPermissionsOutcome(context.Background(), cfg, logger, false) + want := "Failed to apply permissions on " + dir + ": 2 entries could not be changed" + if !strings.Contains(out.String(), "WARNING "+want+"\n") { + t.Fatalf("missing WARNING %q in:\n%s", want, out.String()) + } + if len(outcome.skipped) != 1 || outcome.skipped[0] != dir+" could not be changed" { + t.Fatalf("skipped = %q", outcome.skipped) + } } diff --git a/cmd/proxsave/personal_scripts_audited_test.go b/cmd/proxsave/personal_scripts_audited_test.go index 15d5fcc5..0b24893b 100644 --- a/cmd/proxsave/personal_scripts_audited_test.go +++ b/cmd/proxsave/personal_scripts_audited_test.go @@ -764,6 +764,11 @@ func parkedParentOpens(t *testing.T) int { stacks := string(buf[:runtime.Stack(buf, true)]) parked := 0 for goroutine := range strings.SplitSeq(stacks, "\n\n") { + // Only the personal-script probe: every safefs read of a concurrent test also passes through + // os.OpenRoot for an instant, and counting those made the baseline one too high at random. + if !strings.Contains(goroutine, "openPersonalScriptForExecution") { + continue + } if strings.Contains(goroutine, "os.openRootNolog") || strings.Contains(goroutine, "os.OpenRoot(") { parked++ } diff --git a/cmd/proxsave/runtime_helpers.go b/cmd/proxsave/runtime_helpers.go index fe5e1301..887814ce 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 @@ -896,11 +901,14 @@ func removeLegacyEntrypoint(dest string, bootstrap *logging.BootstrapLogger) { logBootstrapInfo(bootstrap, "Removed legacy 'proxmox-backup' entrypoint: %s", dest) } -func migrateLegacyCronEntries(ctx context.Context, baseDir, execPath string, bootstrap *logging.BootstrapLogger, cronSchedule, configPath string) { +// migrateLegacyCronEntries rewrites the root crontab with a single proxsave entry at cronSchedule +// and returns that entry with ok=true once crontab accepted the table; "" and false when it gave +// up (each early return logs its WARNING) or the write failed. +func migrateLegacyCronEntries(ctx context.Context, baseDir, execPath string, bootstrap *logging.BootstrapLogger, cronSchedule, configPath string) (written string, ok bool) { execPath = strings.TrimSpace(execPath) if execPath == "" { logBootstrapWarning(bootstrap, "Unable to update cron entry: executable path is unknown") - return + return "", false } newCommandToken := "/usr/local/bin/proxsave" @@ -911,7 +919,7 @@ func migrateLegacyCronEntries(ctx context.Context, baseDir, execPath string, boo fallback := strings.TrimSpace(execPath) if fallback == "" { logBootstrapWarning(bootstrap, "Unable to locate Go binary for cron migration") - return + return "", false } logBootstrapInfo(bootstrap, "proxsave symlink not found; falling back to %s for cron entries", fallback) newCommandToken = fallback @@ -940,11 +948,12 @@ func migrateLegacyCronEntries(ctx context.Context, baseDir, execPath string, boo return err } // DELIBERATELY NOT safeexec.ApplyWaitDelay, unlike readCron above. See the note on - // crontabWriteLines in daemon_setup.go for the measurements; the stakes are highest - // here. This writer's error is DISCARDED (migrateLegacyCronEntries is void), and - // applyCronMode calls it to establish the cron fallback and then removes the daemon - // unit unconditionally. A bound that killed a working descendant here would leave - // the host with no cron line AND no daemon, silently. + // crontabWriteLines in daemon_setup.go for the measurements. This writer establishes + // the cron fallback that replaces the daemon on install and on --daemon-remove. Both + // callers read the written line back and keep the daemon when it is not there, so a + // bound that killed a working descendant here would no longer strand the host + // unscheduled, but it would abandon a revert or an install whose write may well have + // landed. cmd.Stdin = strings.NewReader(content) output, err := cmd.CombinedOutput() if err != nil { @@ -956,7 +965,7 @@ func migrateLegacyCronEntries(ctx context.Context, baseDir, execPath string, boo current, err := readCron() if err != nil { logBootstrapWarning(bootstrap, "Unable to inspect existing cron entries: %v", err) - return + return "", false } normalized := strings.ReplaceAll(current, "\r\n", "\n") @@ -986,10 +995,11 @@ func migrateLegacyCronEntries(ctx context.Context, baseDir, execPath string, boo newCron := strings.Join(updatedLines, "\n") + "\n" if err := writeCron(newCron); err != nil { logBootstrapWarning(bootstrap, "Failed to update cron entries: %v", err) - return + return "", false } logBootstrapDebug(bootstrap, "Reinstalled proxsave cron entry at schedule %s: %s", schedule, newCommandToken) + return updatedLines[len(updatedLines)-1], true } // dropLegacyBashCronLines removes crontab lines whose command is the old Bash @@ -1104,26 +1114,62 @@ func repointLegacyCronLines(lines []string) ([]string, bool) { out := make([]string, len(lines)) changed := false for i, line := range lines { - if strings.Trim(cronCommandToken(line), "\"'") == legacy { - // cronCommandToken already excluded comments and env LINES, and a schedule field - // is never a path, so the command is the first occurrence of legacy on the line. - // A leading assignment does not change that: it is stepped over and the command - // still holds the first occurrence. + tok := cronCommandToken(line) + if strings.Trim(tok, "\"'") == legacy { + // Replace the legacy path inside the command field itself, not its first + // occurrence on the line. A leading assignment is stepped over by + // cronCommandToken, so "TAG=/usr/local/bin/proxmox-backup.old + // /usr/local/bin/proxmox-backup --backup" reaches this branch with the legacy + // path appearing first inside TAG: replacing the first occurrence rewrote TAG + // and left the command on the symlink removed right after. The command field + // is located by cronFieldOffset: a schedule field is never a path, and an + // assignment contains '=' while the token does not, so no preceding field can + // equal it. Schedule, assignments, whitespace and arguments are byte-preserved. // - // The assignment form is deliberately NOT repointed. The guard above is exact - // equality, so "BIN=/usr/local/bin/proxmox-backup $BIN --backup" fails it and the - // line is left alone: cronCommandWord answers with the whole assignment WORD for - // that shape, never with the path inside it. That is a missed cosmetic repoint, - // not damage - this whole function is best-effort, and its caller says so. - out[i] = strings.Replace(line, legacy, canonical, 1) - changed = true - continue + // The assignment form is NOT repointed. The guard above is exact equality, so + // "BIN=/usr/local/bin/proxmox-backup $BIN --backup" fails it and the line is + // left alone: cronCommandWord answers with the whole assignment WORD for that + // shape, never with the path inside it. That line keeps pointing at the legacy + // symlink, which the upgrade removes right after this repoint, so it is left + // orphaned. + if at := cronFieldOffset(line, tok); at >= 0 { + out[i] = line[:at] + strings.Replace(tok, legacy, canonical, 1) + line[at+len(tok):] + changed = true + continue + } } out[i] = line } return out, changed } +// cronFieldOffset returns the byte offset in line of the first whitespace-separated +// field equal to word, splitting exactly as strings.Fields does (unicode.IsSpace), or -1 +// when no field equals it. It lets a caller edit one field in place while every other +// byte of the line, whitespace included, stays as it was. +func cronFieldOffset(line, word string) int { + if word == "" { + return -1 + } + start := -1 + for i, r := range line { + if unicode.IsSpace(r) { + if start >= 0 && line[start:i] == word { + return start + } + start = -1 + continue + } + if start < 0 { + start = i + } + } + if start >= 0 && line[start:] == word { + return start + } + return -1 +} + // repointLegacyCronEntries reads the crontab, repoints legacy proxmox-backup entries, and // writes back only if something changed. Best-effort: a crontab read/write failure logs and // continues (a cosmetic repoint must never fail the upgrade). @@ -1236,23 +1282,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/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)) 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) + } + } +} 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..ffa6cb25 --- /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\u202eFAKE" + + 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", "\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) + } + } + 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/testdata/install_characterization/edit_noop.env b/cmd/proxsave/testdata/install_characterization/edit_noop.env index afb4be04..2be22700 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) @@ -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 40977590..cb5f4710 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) @@ -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=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/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env b/cmd/proxsave/testdata/install_characterization/fresh_enable_all.env index 358da7a1..36518c3c 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) @@ -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/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/cmd/proxsave/upgrade.go b/cmd/proxsave/upgrade.go index b5b8d0de..f450a330 100644 --- a/cmd/proxsave/upgrade.go +++ b/cmd/proxsave/upgrade.go @@ -26,8 +26,10 @@ 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" "github.com/tis24dev/proxsave/internal/types" buildinfo "github.com/tis24dev/proxsave/internal/version" ) @@ -228,12 +230,21 @@ 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 { + 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 // 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) @@ -255,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 } @@ -671,6 +692,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 +743,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 +854,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) @@ -1134,7 +1188,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") 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()) + } + }) + } +} 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) + } + } + } +} 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 | 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/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 859a2f04..821f4fb7 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 `$`. 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 @@ -352,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 @@ -564,8 +566,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 @@ -596,7 +598,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 @@ -684,7 +686,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 +1488,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/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 diff --git a/docs/DEVELOPER_GUIDE.md b/docs/DEVELOPER_GUIDE.md index 0b0370ee..e8729287 100644 --- a/docs/DEVELOPER_GUIDE.md +++ b/docs/DEVELOPER_GUIDE.md @@ -22,7 +22,7 @@ Guide for contributing to Proxsave, including development setup, coding guidelin This guide covers the implementation and contributor workflow. For the operator view of collection, archives and destinations, read [How a backup runs](BACKUP_GUIDE.md#how-a-backup-runs). For routine operations, use the [dashboard guide](DASHBOARD.md) **Technology stack**: -- **Language**: Go 1.26.10 or later (the floor `go.mod` sets and `goRuntimeMinVersion` enforces at startup) +- **Language**: Go 1.26.11 or later (the floor `go.mod` sets and `goRuntimeMinVersion` enforces at startup) - **Dependencies**: See `go.mod` for complete list - **Build system**: Makefile + Go modules - **Compression**: gzip, bzip2, xz, lzma, zstd @@ -36,7 +36,7 @@ This guide covers the implementation and contributor workflow. For the operator ### Prerequisites ```bash -# Go 1.26.10 or later +# Go 1.26.11 or later go version # Build tools diff --git a/docs/HEALTHCHECKS.md b/docs/HEALTHCHECKS.md index 88f2666d..cb389033 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,22 @@ 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. +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 adds a `Healthchecks Login:` line only in the second. The run screen distinguishes them diff --git a/docs/INSTALL.md b/docs/INSTALL.md index b89a8155..0f41cc0a 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -114,7 +114,7 @@ Build from source for development or an architecture without a published release ### Prerequisites -Install Git, make, and a Go toolchain satisfying `go.mod` (currently Go 1.26.10). Choose the Go download for your host architecture and verify it according to the Go project's instructions. Install rclone only if you plan to use cloud storage. Do not extract a new Go toolchain over an existing `/usr/local/go` tree. +Install Git, make, and a Go toolchain satisfying `go.mod` (currently Go 1.26.11). Choose the Go download for your host architecture and verify it according to the Go project's instructions. Install rclone only if you plan to use cloud storage. Do not extract a new Go toolchain over an existing `/usr/local/go` tree. ### Building from Source diff --git a/docs/SECURITY.md b/docs/SECURITY.md index 20135276..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` @@ -300,8 +302,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/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/go.mod b/go.mod index ee548fe7..ff1f3aa7 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/tis24dev/proxsave -go 1.26.10 +go 1.26.11 require ( charm.land/bubbles/v2 v2.2.1 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/config/config.go b/internal/config/config.go index 569aa738..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 @@ -574,11 +578,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 } @@ -954,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", "")) @@ -1307,8 +1311,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) }) } @@ -1524,6 +1535,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{} 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) + } + } +} 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/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/template_cloud_log_path_test.go b/internal/config/template_cloud_log_path_test.go new file mode 100644 index 00000000..b982e97a --- /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.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" { + 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..249564fe 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 @@ -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/config/upgrade_initial_email_test.go b/internal/config/upgrade_initial_email_test.go new file mode 100644 index 00000000..5159d788 --- /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.SplitSeq(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/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/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/installer/install_data.go b/internal/installer/install_data.go index ca54ded7..9561cdac 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,34 @@ 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 or "$": loading backup.env expands $NAME and ${NAME}, so an +// address with "$" would not be the one saved). The ProxSave HC Server checks it again. +func ValidateHealthcheckInitialEmail(value string) error { + v := strings.TrimSpace(value) + if v == "" { + return wizardMessage("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 > '~' || r == '$' { + usable = false + } + } + if !usable { + return wizardMessage("Not a usable email address") + } + return nil +} + +// wizardMessage is a validation answer the install wizard shows to the operator as it is (the TUI under +// the field, --install --cli before asking again): an approved sentence, capitalized, never wrapped into +// another error. +type wizardMessage string + +func (m wizardMessage) Error() string { return string(m) } 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..18a61d4a --- /dev/null +++ b/internal/installer/install_data_initial_email_test.go @@ -0,0 +1,55 @@ +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", + "ops$TEAM@example.org": "Not a usable email address", + "ops@exa$mple.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/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/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/notify/email.go b/internal/notify/email.go index feca7099..79daa056 100644 --- a/internal/notify/email.go +++ b/internal/notify/email.go @@ -20,6 +20,7 @@ import ( "github.com/tis24dev/proxsave/internal/logging" "github.com/tis24dev/proxsave/internal/safeexec" + "github.com/tis24dev/proxsave/internal/safefs" "github.com/tis24dev/proxsave/internal/types" ) @@ -615,12 +616,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 +694,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,8 +703,48 @@ 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) { + // Through an *os.Root on the file's directory, as every other config read of the repo (safefs). + data, err := safefs.ReadFileUnderRoot(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) + // Through an *os.Root on the file's directory, as every other config read of the repo (safefs). + data, err := safefs.ReadFileUnderRoot(cfgPath) if err != nil { return "", fmt.Errorf("failed to read %s: %w", cfgPath, err) } @@ -1274,7 +1319,8 @@ func (e *EmailNotifier) buildEmailMessage(recipient, subject, htmlBody, textBody if attachLog { // Try to read log file; on failure, fall back to plain multipart/alternative logPath := strings.TrimSpace(data.LogFilePath) - content, err := os.ReadFile(logPath) + // Through an *os.Root on the log's directory, as the user.cfg reads above (safefs). + content, err := safefs.ReadFileUnderRoot(logPath) if err != nil { e.logger.Warning("Failed to read log file for email attachment (%s): %v", logPath, err) attachLog = false diff --git a/internal/notify/email_delivery_methods_test.go b/internal/notify/email_delivery_methods_test.go index 939efc44..e774b542 100644 --- a/internal/notify/email_delivery_methods_test.go +++ b/internal/notify/email_delivery_methods_test.go @@ -3,6 +3,7 @@ package notify import ( "bytes" "context" + "encoding/base64" "io" "net/http" "net/http/httptest" @@ -744,3 +745,59 @@ func TestEmailNotifierSendPMFLogsAMissingRecipient(t *testing.T) { t.Fatalf("log missing %q:\n%s", want, log.String()) } } + +// The log attached to the mail is read through safefs (an *os.Root on its directory): +// a regular log is attached with its content, and a path that is not a regular file +// (a directory, a missing file) fails as os.ReadFile did, the mail falling back to +// multipart/alternative with the read failure in the WARNING. +func TestEmailNotifierBuildEmailMessage_LogAttachmentReadThroughSafefs(t *testing.T) { + dir := t.TempDir() + logPath := filepath.Join(dir, "backup.log") + if err := os.WriteFile(logPath, []byte("log contents"), 0o600); err != nil { + t.Fatalf("write log: %v", err) + } + subdir := filepath.Join(dir, "a-directory.log") + if err := os.Mkdir(subdir, 0o700); err != nil { + t.Fatalf("mkdir: %v", err) + } + for _, tc := range []struct { + name, path string + attached bool + }{ + {"regular", logPath, true}, + {"directory", subdir, false}, + {"missing", filepath.Join(dir, "missing.log"), false}, + } { + t.Run(tc.name, func(t *testing.T) { + var buf strings.Builder + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&buf) + notifier, err := NewEmailNotifier(EmailConfig{ + Enabled: true, + DeliveryMethod: EmailDeliverySendmail, + From: "no-reply@proxmox.example.com", + AttachLogFile: true, + }, types.ProxmoxBS, logger) + if err != nil { + t.Fatalf("NewEmailNotifier() error = %v", err) + } + data := createTestNotificationData() + data.LogFilePath = tc.path + + msg, _, _ := notifier.buildEmailMessage("admin@example.com", "subject", "html", "text", data) + if tc.attached { + encoded := base64.StdEncoding.EncodeToString([]byte("log contents")) + if !strings.Contains(msg, "Content-Disposition: attachment") || !strings.Contains(msg, encoded) { + t.Fatalf("expected the log attached with its content, got:\n%s", msg) + } + return + } + if strings.Contains(msg, "Content-Type: multipart/mixed") || !strings.Contains(msg, "Content-Type: multipart/alternative") { + t.Fatalf("expected multipart/alternative fallback, got:\n%s", msg) + } + if !strings.Contains(buf.String(), "Failed to read log file for email attachment ("+tc.path+")") { + t.Fatalf("missing the read failure warning in:\n%s", buf.String()) + } + }) + } +} diff --git a/internal/notify/email_maillog.go b/internal/notify/email_maillog.go index 8d154231..6eb3cb99 100644 --- a/internal/notify/email_maillog.go +++ b/internal/notify/email_maillog.go @@ -199,27 +199,38 @@ 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 == "" { + // sendmail ends its listing with "Total requests: N", whose "Total" reads as a queue ID. + if trimmed == "" || strings.HasPrefix(trimmed, "Total requests") { continue } 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_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) + } +} diff --git a/internal/notify/email_queue_count_test.go b/internal/notify/email_queue_count_test.go new file mode 100644 index 00000000..1bdb87d2 --- /dev/null +++ b/internal/notify/email_queue_count_test.go @@ -0,0 +1,53 @@ +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) + } +} + +// sendmail ends its listing with "Total requests: N": its first field, "Total", reads as a +// queue ID, so one queued message counted as two. +func TestParseMailQueueSkipsTheSendmailFooter(t *testing.T) { + sendmail := ` /var/spool/mqueue (1 request) +-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient----------- +5AD3H1sK012345 612 Mon Nov 3 10:01 + (Deferred: Connection timed out with example.invalid.) + + Total requests: 1 +` + count, entries := parseMailQueue(sendmail, "a@example.invalid") + if count != 1 { + t.Fatalf("sendmail: count = %d, want 1", count) + } + if len(entries) != 1 || entries[0].id != "5AD3H1sK012345" { + t.Fatalf("sendmail: entries = %+v, want the one message with its real queue ID", entries) + } +} 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 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), }, "" } diff --git a/internal/notify/webhook.go b/internal/notify/webhook.go index 1f6f764d..dc43ad6c 100644 --- a/internal/notify/webhook.go +++ b/internal/notify/webhook.go @@ -14,9 +14,54 @@ 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, 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 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} + if auth.Pass != "" { + secrets = append(secrets, base64.StdEncoding.EncodeToString([]byte(auth.User+":"+auth.Pass))) + } + 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(), + base64.StdEncoding.EncodeToString([]byte(u.User.Username()+":"+password))) + } + 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 == ';' }) { + key, value, _ := strings.Cut(pair, "=") + secrets = append(secrets, pair, key, 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) +} + // WebhookNotifier sends notifications to configured webhook endpoints type WebhookNotifier struct { config *config.WebhookConfig @@ -390,7 +435,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 +454,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 +469,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 +479,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 +489,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 +501,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..5762fa81 --- /dev/null +++ b/internal/notify/webhook_response_body_test.go @@ -0,0 +1,153 @@ +package notify + +import ( + "bytes" + "context" + "encoding/base64" + "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) + } + } + } +} + +// The DEBUG excerpt masked the endpoint URL whole, but an error page echoes the +// 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 = "Tok%41enSegment99" + const queryValue = "QueryValue12345" + 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.EscapedPath() + " (" + r.URL.Path + ")?" + r.URL.RawQuery + + " 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 + "&BareQueryToken42" + cfg := config.WebhookConfig{ + Enabled: true, + DefaultFormat: "generic", + Timeout: 30, + RetryDelay: 1, + Endpoints: []config.WebhookEndpoint{{ + Name: "ops", + URL: endpointURL, + Format: "generic", + Method: "POST", + Headers: map[string]string{"X-Key": "Bearer " + bearer}, + Auth: config.WebhookAuth{Type: "basic", User: "admin", Pass: basicPass}, + }}, + } + 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) + } + basic := base64.StdEncoding.EncodeToString([]byte("admin:" + basicPass)) + 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) + } + } +} 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 } diff --git a/internal/orchestrator/extensions.go b/internal/orchestrator/extensions.go index 9d7f25fe..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 @@ -779,8 +789,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/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..e3afc3d2 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: your email and password" + } + return `Sign in: your email, then "Email Me a Link"` +} diff --git a/internal/orchestrator/healthcheck_setup_test.go b/internal/orchestrator/healthcheck_setup_test.go index 3c2b1e3e..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 @@ -487,3 +499,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/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) 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) + } +} 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) + } + } +} diff --git a/internal/orchestrator/storage_attempts_e2e_test.go b/internal/orchestrator/storage_attempts_e2e_test.go index 631101f3..2452a0d7 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) { @@ -648,3 +649,50 @@ func TestDispatchLogFileCloudLocalLogModeReusesInitDetection(t *testing.T) { }) } } + +// Step [8], CLOUD_REMOTE a local directory: the log chmod (SetLocalLogMode, inside +// copyLogToCloud) only ever sees a destination inside the CLOUD_REMOTE directory. An +// absolute CLOUD_LOG_PATH is placed under CLOUD_REMOTE, never at that absolute path on +// the host; a CLOUD_LOG_PATH that resolves outside it is refused before the upload, so +// neither rclone nor the chmod runs. +func TestDispatchLogFileCloudLogChmodStaysInsideTheShare(t *testing.T) { + for _, tc := range []struct { + name, logPath string + uploaded bool + }{ + {"absolute", "/var/log/proxsave", true}, + {"absolute with traversal", "/../../outside", false}, + {"relative traversal", "../outside", false}, + } { + t.Run(tc.name, func(t *testing.T) { + var buf bytes.Buffer + logger := logging.New(types.LogLevelInfo, false) + logger.SetOutput(&buf) + root := filepath.Join(t.TempDir(), "cloud") + cfg := &config.Config{CloudEnabled: true, CloudRemote: root, CloudLogPath: tc.logPath, FsIoTimeoutSeconds: 30} + o := &Orchestrator{logger: logger, cfg: cfg} + var dests []string + o.copyLogToCloudFn = func(_ context.Context, _, destPath string) error { + dests = append(dests, destPath) + return nil + } + src := writeSrcLog(t) + if err := o.dispatchLogFile(context.Background(), src); err != nil { + t.Fatalf("dispatchLogFile: %v", err) + } + if !tc.uploaded { + if len(dests) != 0 { + t.Fatalf("upload (and its chmod) ran for a refused CLOUD_LOG_PATH: %v", dests) + } + if !strings.Contains(buf.String(), "CLOUD_LOG_PATH: outside "+root) { + t.Fatalf("missing the refusal in:\n%s", buf.String()) + } + return + } + want := filepath.Join(root, tc.logPath, filepath.Base(src)) + if len(dests) != 1 || dests[0] != want { + t.Fatalf("upload destinations = %v, want [%s] inside %s", dests, want, root) + } + }) + } +} diff --git a/internal/safefs/confined_test.go b/internal/safefs/confined_test.go index 0c05d0eb..6e3123dd 100644 --- a/internal/safefs/confined_test.go +++ b/internal/safefs/confined_test.go @@ -1,11 +1,13 @@ package safefs import ( + "context" "errors" "os" "path/filepath" "syscall" "testing" + "time" ) func TestReadFileUnderRootReadsFile(t *testing.T) { @@ -221,3 +223,50 @@ func TestReadFileInRootDetectsLoop(t *testing.T) { t.Fatal("expected error for symlink loop") } } + +// LchmodInRoot changes a file under root, and refuses what would leave it: a parent directory +// replaced by a symlink to another directory (Lchmod followed it), a symlink at the final +// component (as Lchmod), and a path above root. +func TestLchmodInRoot(t *testing.T) { + ctx := context.Background() + root := t.TempDir() + outside := t.TempDir() + victim := filepath.Join(outside, "f") + for _, p := range []string{filepath.Join(root, "sub", "f"), victim} { + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + } + if err := os.Symlink(outside, filepath.Join(root, "swapped")); err != nil { + t.Fatal(err) + } + if err := os.Symlink("f", filepath.Join(root, "sub", "link")); err != nil { + t.Fatal(err) + } + + if err := LchmodInRoot(ctx, root, filepath.Join(root, "sub", "f"), 0o600, time.Second); err != nil { + t.Fatalf("a file under root: %v", err) + } + if st, _ := os.Stat(filepath.Join(root, "sub", "f")); st.Mode().Perm() != 0o600 { + t.Fatalf("mode = %v, want 0600", st.Mode().Perm()) + } + for _, p := range []string{ + filepath.Join(root, "swapped", "f"), // a parent symlinked out of root + filepath.Join(root, "sub", "link"), // a final-component symlink, even inside root + filepath.Join(root, "..", filepath.Base(outside), "f"), + victim, + } { + if err := LchmodInRoot(ctx, root, p, 0o640, time.Second); err == nil { + t.Errorf("%s: changed, want refused", p) + } + } + if st, _ := os.Stat(victim); st.Mode().Perm() != 0o644 { + t.Fatalf("the file outside root went to %v, want 0644 untouched", st.Mode().Perm()) + } + if st, _ := os.Lstat(filepath.Join(root, "sub", "f")); st.Mode().Perm() != 0o600 { + t.Fatalf("the link target changed through the link: %v", st.Mode().Perm()) + } +} diff --git a/internal/safefs/safefs.go b/internal/safefs/safefs.go index 376fd6ac..d94f274f 100644 --- a/internal/safefs/safefs.go +++ b/internal/safefs/safefs.go @@ -7,6 +7,8 @@ import ( "io/fs" "math" "os" + "path/filepath" + "strings" "syscall" "time" ) @@ -302,6 +304,58 @@ func Lchmod(ctx context.Context, path string, mode os.FileMode, timeout time.Dur return err } +// LchmodInRoot is Lchmod confined to root, for a path in a directory other hosts can write to: +// every directory component of path is resolved through an *os.Root on root, so a symlink that +// leaves root (or an absolute one) is refused there too, not only at the final component. The +// final component is opened with O_NOFOLLOW, as Lchmod does, and the mode is applied with fchmod +// on that descriptor. path is absolute under root, or relative to it. Bounded like Lchmod. +func LchmodInRoot(ctx context.Context, root, path string, mode os.FileMode, timeout time.Duration) error { + _, err := runLimited(ctx, timeout, &TimeoutError{Op: "lchmod", Path: path, Timeout: effectiveTimeout(ctx, timeout)}, func() (struct{}, error) { + return struct{}{}, lchmodInRoot(root, path, mode) + }) + return err +} + +// lchmodInRoot opens the parent directory of path through os.Root (os.Root.OpenFile follows a +// final-component symlink that stays inside root even with O_NOFOLLOW, so the file itself is +// opened with openat on that directory, O_NOFOLLOW honoured) and fchmods it. +func lchmodInRoot(root, path string, mode os.FileMode) error { + rel := filepath.Clean(path) + if filepath.IsAbs(rel) { + r, err := filepath.Rel(filepath.Clean(root), rel) + if err != nil { + return &os.PathError{Op: "lchmod", Path: path, Err: err} + } + rel = r + } + if rel == ".." || strings.HasPrefix(rel, "../") { + return &os.PathError{Op: "lchmod", Path: path, Err: errors.New("path escapes from root")} + } + r, err := os.OpenRoot(root) + if err != nil { + return err + } + defer func() { _ = r.Close() }() + dir, base := filepath.Split(rel) + if dir == "" { + dir = "." + } + parent, err := r.OpenFile(dir, os.O_RDONLY|syscall.O_DIRECTORY|syscall.O_CLOEXEC, 0) + if err != nil { + return err + } + defer func() { _ = parent.Close() }() + fd, err := syscall.Openat(int(parent.Fd()), base, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0) + if err != nil { + return &os.PathError{Op: "openat", Path: path, Err: err} + } + defer func() { _ = syscall.Close(fd) }() // fchmod already applied; a close error on the fd is not meaningful + if err := syscall.Fchmod(fd, chmodSyscallMode(mode)); err != nil { + return &os.PathError{Op: "fchmod", Path: path, Err: err} + } + return nil +} + // lchmodNoFollow opens path with O_NOFOLLOW (a symlink final component fails with // ELOOP) and fchmods the resulting descriptor, so a symlink swapped in after an // earlier stat cannot redirect the chmod to another target. O_RDONLY (0) opens 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"), 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/storage/cloud.go b/internal/storage/cloud.go index 2f68d32a..73f36e7c 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) @@ -225,27 +228,75 @@ 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) - if err := safefs.Chmod(ctx, logFile, 0o640, c.fsIoTimeout()); err != nil { + // Confined to the CLOUD_REMOTE directory: it can be a share other hosts write to, and a + // symlink swapped in for the log or one of its parent directories must not redirect a + // root chmod to another file. A log outside it is confined to its own directory. + root := c.localModeRoot() + if !pathWithin(filepath.Clean(logFile), root) { + root = filepath.Dir(logFile) + } + if err := safefs.LchmodInRoot(ctx, root, logFile, 0o640, c.fsIoTimeout()); err != nil { c.logger.Debug("Cloud log copy: cannot set the mode of %s: %v", logFile, err) return false, err } return true, nil } +// setLocalModesFromSource gives each file of the backup set copied to destFile the mode +// 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 and stays inside the CLOUD_REMOTE +// directory: the directory can be a share other hosts write to. A mode that cannot be set +// is reported as the ownership branch reports it (logPermissionsFailure, then +// StoreIssuePermissionsNotSet in issues). +func (c *CloudStorage) setLocalModesFromSource(ctx context.Context, sourceFile, destFile string, issues *storeIssueRecorder) { + timeout := c.fsIoTimeout() + for _, dest := range buildBackupCandidatePaths(destFile, true) { + if _, err := safefs.Stat(ctx, dest, timeout); err != nil { + if !errors.Is(err, os.ErrNotExist) { + c.logger.Debug("Cloud Storage: permissions - cannot read %s, mode not set: %v", filepath.Base(dest), err) + } + continue + } + src := filepath.Join(filepath.Dir(sourceFile), filepath.Base(dest)) + 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) + } + 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.LchmodInRoot(ctx, c.localModeRoot(), dest, mode, timeout); err != nil { + c.logger.Debug("Cloud Storage: permissions - cannot set the mode of %s: %v", filepath.Base(dest), err) + logPermissionsFailure(c.logger, dest, err) + issues.add(StoreIssuePermissionsNotSet) + } + } +} + +// localModeRoot is the directory the local chmods are confined to: the CLOUD_REMOTE +// directory, or the backup directory for a store built without it. +func (c *CloudStorage) localModeRoot() string { + if c.localRoot != "" { + return c.localRoot + } + return c.localDir +} + // 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 { @@ -263,12 +314,20 @@ 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. A FAT-family +// filesystem has no Unix permissions: nothing is set there and nothing is reported. +func (c *CloudStorage) setLocalPermissions(ctx context.Context, sourceFile, destFile string, issues *storeIssueRecorder) { + if c.localDir == "" || c.localFS == nil { + return + } + if !c.localFS.SupportsOwnership { + if c.localFS.Type.hasNoUnixModes() { + c.logger.Debug("Cloud Storage: permissions - not set on the copies of %s (%s has no Unix permissions)", filepath.Base(destFile), c.localFS.Type) + return } + c.setLocalModesFromSource(ctx, sourceFile, destFile, issues) return } detector := c.fsDetector @@ -488,6 +547,12 @@ func NewCloudStorage(cfg *config.Config, logger *logging.Logger, writtenHostname host := resolveRetentionHostname() serverID := types.NormalizeServerID(cfg.ServerID) logRetentionServerIdentity(logger, "Cloud storage", serverID) + flagSecrets := rcloneFlagSecrets(cfg.RcloneFlags) + if logger != nil { + for _, secret := range flagSecrets { + logger.RegisterSecret(secret) + } + } return &CloudStorage{ config: cfg, logger: logger, @@ -504,6 +569,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(), @@ -511,6 +577,32 @@ func NewCloudStorage(cfg *config.Config, logger *logging.Logger, writtenHostname }, nil } +// 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 '=' 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 { + value := tok + if strings.HasPrefix(tok, "--") { + var ok bool + if _, value, ok = strings.Cut(tok, "="); !ok { + continue + } + } + 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 func (c *CloudStorage) Name() string { return "Cloud Storage (rclone)" @@ -1193,7 +1285,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) @@ -1211,7 +1303,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) @@ -1670,7 +1762,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_local_modes_test.go b/internal/storage/cloud_local_modes_test.go new file mode 100644 index 00000000..3adabc02 --- /dev/null +++ b/internal/storage/cloud_local_modes_test.go @@ -0,0 +1,152 @@ +package storage + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/tis24dev/proxsave/internal/config" +) + +// 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. The mode +// not set on it is reported as the ownership branch reports its own; a clean set records +// nothing. +func TestCloudLocalModesFailClosedAndRefuseSymlinks(t *testing.T) { + clean, cleanLog, cleanArchive, cleanRoot := newLocalFormStore(t, "", false) + for _, suffix := range []string{"", ".sha256", ".metadata"} { + if err := os.WriteFile(filepath.Join(cleanRoot, filepath.Base(cleanArchive)+suffix), []byte("copy"), 0o644); err != nil { + t.Fatal(err) + } + } + cleanIssues := &storeIssueRecorder{} + clean.setLocalModesFromSource(context.Background(), cleanArchive, filepath.Join(cleanRoot, filepath.Base(cleanArchive)), cleanIssues) + if got := cleanIssues.list(); len(got) != 0 || strings.Contains(cleanLog.String(), "Permissions failed") { + t.Fatalf("clean set: issues %v, log:\n%s", got, cleanLog.String()) + } + + cs, buf, 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) + } + + issues := &storeIssueRecorder{} + cs.setLocalModesFromSource(context.Background(), archive, dest, issues) + + 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) + } + if got := issues.list(); len(got) != 1 || got[0] != StoreIssuePermissionsNotSet { + t.Fatalf("issues = %v, want permissions not set", got) + } + if want := " Permissions failed: " + filepath.Base(dest) + ".sha256: "; !strings.Contains(buf.String(), want) { + t.Fatalf("missing %q in:\n%s", want, buf.String()) + } +} + +// A parent directory of the copy replaced by a symlink to a directory outside the CLOUD_REMOTE +// directory: the root chmod does not reach the file there (Lchmod only refused a symlink at the +// final component). +func TestCloudLocalModesStayInsideTheCloudDirectory(t *testing.T) { + srcDir := t.TempDir() + archive := filepath.Join(srcDir, "node-backup-20261003-100000.tar.zst") + if err := os.WriteFile(archive, []byte("archive"), 0o600); err != nil { + t.Fatal(err) + } + root := filepath.Join(t.TempDir(), "cloud") + if err := os.MkdirAll(root, 0o700); err != nil { + t.Fatal(err) + } + outside := t.TempDir() + victim := filepath.Join(outside, filepath.Base(archive)) + if err := os.WriteFile(victim, []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.Symlink(outside, filepath.Join(root, "sub")); err != nil { + t.Fatal(err) + } + cs, err := NewCloudStorage(&config.Config{CloudEnabled: true, CloudRemote: root, CloudRemotePath: "sub"}, newTestLogger(), "") + if err != nil { + t.Fatalf("NewCloudStorage: %v", err) + } + + issues := &storeIssueRecorder{} + cs.setLocalModesFromSource(context.Background(), archive, filepath.Join(root, "sub", filepath.Base(archive)), issues) + + if st, err := os.Stat(victim); err != nil || st.Mode().Perm() != 0o644 { + t.Fatalf("file outside the cloud directory: mode %v (%v), want 0644 untouched", st.Mode().Perm(), err) + } + if got := issues.list(); len(got) != 1 || got[0] != StoreIssuePermissionsNotSet { + t.Fatalf("issues = %v, want permissions not set", got) + } +} + +// A FAT-family CLOUD_REMOTE directory has no Unix permissions: the copies are not +// chmodded and nothing is reported, where a chmod would fail. Another filesystem without +// ownership keeps the chmod and reports the copy whose mode could not be set. +func TestCloudLocalModesSkippedOnFAT(t *testing.T) { + for _, tc := range []struct { + fsType FilesystemType + skipped bool + wantMode os.FileMode + }{ + {FilesystemFAT32, true, 0o644}, + {FilesystemFAT, true, 0o644}, + {FilesystemMSDOS, true, 0o644}, + {FilesystemExFAT, true, 0o644}, + {FilesystemCIFS, false, 0o600}, + {FilesystemNTFS, false, 0o600}, + } { + t.Run(string(tc.fsType), func(t *testing.T) { + cs, buf, archive, root := newLocalFormStore(t, "", false) + cs.localFS.Type = tc.fsType + 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) + } + // A symlinked sidecar makes the chmod fail where it runs. + if err := os.Symlink(outside, dest+".sha256"); err != nil { + t.Fatal(err) + } + + issues := &storeIssueRecorder{} + cs.setLocalPermissions(context.Background(), archive, dest, issues) + + if st, err := os.Stat(dest); err != nil || st.Mode().Perm() != tc.wantMode { + t.Fatalf("archive copy mode = %v (%v), want %o", st.Mode().Perm(), err, tc.wantMode) + } + got := issues.list() + failed := strings.Contains(buf.String(), "Permissions failed") + if tc.skipped { + if len(got) != 0 || failed { + t.Fatalf("FAT: issues %v, log:\n%s", got, buf.String()) + } + return + } + if len(got) != 1 || got[0] != StoreIssuePermissionsNotSet || !failed { + t.Fatalf("issues = %v, want permissions not set; log:\n%s", got, buf.String()) + } + }) + } +} 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_rclone_flags_secret_test.go b/internal/storage/cloud_rclone_flags_secret_test.go new file mode 100644 index 00000000..93d96fd4 --- /dev/null +++ b/internal/storage/cloud_rclone_flags_secret_test.go @@ -0,0 +1,136 @@ +package storage + +import ( + "bytes" + "context" + "errors" + "strconv" + "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) + } + } +} + +// 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) + } + } +} diff --git a/internal/storage/cloud_round6_test.go b/internal/storage/cloud_round6_test.go index 00033fbb..5daf37ed 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 { @@ -131,3 +132,35 @@ func TestCloudSetLocalLogModeFollowsOwnership(t *testing.T) { t.Fatalf("the remote form sets nothing: %v %v", applied, err) } } + +// A parent directory of the log replaced by a symlink to a directory outside the CLOUD_REMOTE +// directory: the mode is not set, and the file there keeps its own. +func TestCloudSetLocalLogModeStaysInsideTheCloudDirectory(t *testing.T) { + dir := filepath.Join(t.TempDir(), "cloud") + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatalf("mkdir: %v", err) + } + outside := t.TempDir() + if err := os.MkdirAll(filepath.Join(outside, "log"), 0o755); err != nil { + t.Fatalf("mkdir: %v", err) + } + victim := filepath.Join(outside, "log", "backup-node-20261003-100000.log") + if err := os.WriteFile(victim, []byte("log"), 0o644); err != nil { + t.Fatalf("write: %v", err) + } + if err := os.Symlink(outside, filepath.Join(dir, "proxsave")); err != nil { + t.Fatalf("symlink: %v", err) + } + cs, err := NewCloudStorage(&config.Config{CloudEnabled: true, CloudRemote: dir}, newTestLogger(), "") + if err != nil { + t.Fatalf("NewCloudStorage: %v", err) + } + ext4 := &FilesystemInfo{Type: FilesystemExt4, SupportsOwnership: true} + logFile := filepath.Join(dir, "proxsave", "log", filepath.Base(victim)) + if applied, err := cs.SetLocalLogMode(context.Background(), logFile, ext4); err == nil || applied { + t.Fatalf("applied=%v err=%v, want the chmod refused", applied, err) + } + if st, err := os.Stat(victim); err != nil || st.Mode().Perm() != 0o644 { + t.Fatalf("file outside the cloud directory: mode %v (%v), want 0644 untouched", st.Mode().Perm(), err) + } +} diff --git a/internal/storage/filesystem.go b/internal/storage/filesystem.go index be857f70..278dde88 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 @@ -327,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 } @@ -356,6 +429,8 @@ func parseFilesystemType(fsTypeStr string) FilesystemType { return FilesystemFAT32 case "fat", "fat16": return FilesystemFAT + case "msdos": + return FilesystemMSDOS case "exfat": return FilesystemExFAT case "ntfs", "ntfs-3g": 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/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..ddf34a83 100644 --- a/internal/storage/filesystem_test.go +++ b/internal/storage/filesystem_test.go @@ -54,6 +54,8 @@ func TestParseFilesystemType_CoversKnownAndUnknownTypes(t *testing.T) { {"fat32", FilesystemFAT32}, {"fat", FilesystemFAT}, {"fat16", FilesystemFAT}, + {"msdos", FilesystemMSDOS}, + {"MSDOS", FilesystemMSDOS}, {"exfat", FilesystemExFAT}, {"ntfs", FilesystemNTFS}, {"ntfs-3g", FilesystemNTFS}, @@ -76,6 +78,32 @@ func TestParseFilesystemType_CoversKnownAndUnknownTypes(t *testing.T) { } } +// An msdos mount has its own type and behaves like fat for every user of the type: no +// ownership, excluded from chown/chmod, and no mode set on the Cloud and Secondary copies. +// vfat keeps its own type with the same behaviour. +func TestMsdosBehavesLikeFAT(t *testing.T) { + if got := parseFilesystemType("msdos"); got != FilesystemMSDOS || string(got) != "msdos" { + t.Fatalf("parseFilesystemType(msdos) = %q, want %q", got, "msdos") + } + if got := parseFilesystemType("vfat"); got != FilesystemFAT32 || string(got) != "vfat" { + t.Fatalf("parseFilesystemType(vfat) = %q, want %q", got, "vfat") + } + for _, fs := range []FilesystemType{FilesystemMSDOS, FilesystemFAT32} { + if got, want := fs.hasNoUnixModes(), FilesystemFAT.hasNoUnixModes(); got != want || !got { + t.Fatalf("%q hasNoUnixModes() = %v, want %v (as fat)", fs, got, want) + } + if got, want := fs.ShouldAutoExclude(), FilesystemFAT.ShouldAutoExclude(); got != want || !got { + t.Fatalf("%q ShouldAutoExclude() = %v, want %v (as fat)", fs, got, want) + } + if got, want := fs.SupportsUnixOwnership(), FilesystemFAT.SupportsUnixOwnership(); got != want || got { + t.Fatalf("%q SupportsUnixOwnership() = %v, want %v (as fat)", fs, got, want) + } + if got, want := fs.IsNetworkFilesystem(), FilesystemFAT.IsNetworkFilesystem(); got != want || got { + t.Fatalf("%q IsNetworkFilesystem() = %v, want %v (as fat)", fs, got, want) + } + } +} + func TestUnescapeOctal(t *testing.T) { cases := []struct { in string @@ -135,7 +163,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/secondary.go b/internal/storage/secondary.go index c7682c2d..928d00d2 100644 --- a/internal/storage/secondary.go +++ b/internal/storage/secondary.go @@ -211,7 +211,17 @@ func (s *SecondaryStorage) Store(ctx context.Context, backupFile string, metadat s.logger.Debug("Secondary Storage: Start copy...") s.logger.Debug("Copying backup to secondary storage: %s -> %s", filepath.Base(sourceFile), s.basePath) - if err := s.copyFile(ctx, sourceFile, destFile); err != nil { + // Without ownership the mode copyFile mirrors from the source is the one each copy keeps: + // a copy whose mode could not be set is reported below, where the ownership branch + // reports its own. + type modeFailure struct { + path string + err error + } + var modeFailures []modeFailure + modeFailed := func(path string, err error) { modeFailures = append(modeFailures, modeFailure{path, err}) } + + if err := s.copyFile(ctx, sourceFile, destFile, modeFailed); err != nil { s.logger.Debug("Secondary Storage: copy - failed for %s: %v", filepath.Base(sourceFile), err) s.logger.Info(" Copy failed: %s", safefs.SystemErrorText(err)) return &StorageError{ @@ -239,7 +249,7 @@ func (s *SecondaryStorage) Store(ctx context.Context, backupFile string, metadat } destAssocFile := filepath.Join(s.basePath, filepath.Base(srcFile)) - if err := s.copyFile(ctx, srcFile, destAssocFile); err != nil { + if err := s.copyFile(ctx, srcFile, destAssocFile, modeFailed); err != nil { s.logger.Debug("Secondary Storage: copy - failed for an associated file: %v", err) s.logger.Info(" Sidecar failed: %s: %s", filepath.Base(srcFile), safefs.SystemErrorText(err)) issues.add(StoreIssueSidecarNotSaved) @@ -262,6 +272,11 @@ func (s *SecondaryStorage) Store(ctx context.Context, backupFile string, metadat logPermissionsFailure(s.logger, path, err) issues.add(StoreIssuePermissionsNotSet) }) + } else if s.fsInfo != nil { + for _, f := range modeFailures { + logPermissionsFailure(s.logger, f.path, f.err) + issues.add(StoreIssuePermissionsNotSet) + } } s.logger.Debug("✓ Secondary Storage: File copied") @@ -302,8 +317,14 @@ func (s *SecondaryStorage) countBackups(ctx context.Context) int { // cleanup and never turns a committed copy into a reported failure. var secondaryCloseSourceFile = func(f *os.File) error { return f.Close() } -// copyFile copies a file using Go's io.Copy -func (s *SecondaryStorage) copyFile(ctx context.Context, src, dest string) (err error) { +// secondaryChmod is the chmod copyFile mirrors the source mode with; a seam so a test can +// make it fail. +var secondaryChmod = safefs.Chmod + +// copyFile copies a file using Go's io.Copy. modeFailed, when not nil, is told about a +// copy that was committed without the source mode (the chmod failed). On a FAT-family +// filesystem the source mode is not mirrored: there are no Unix permissions to set. +func (s *SecondaryStorage) copyFile(ctx context.Context, src, dest string, modeFailed func(path string, err error)) (err error) { if err := ctx.Err(); err != nil { return err } @@ -393,8 +414,11 @@ func (s *SecondaryStorage) copyFile(ctx context.Context, src, dest string) (err return fmt.Errorf("failed to close temporary file %s: %w", tempName, closeErr) } - if err := safefs.Chmod(ctx, tempName, sourceInfo.Mode(), to); err != nil { - s.logger.Debug("Secondary storage: unable to mirror permissions on %s: %v", tempName, err) + var modeErr error + if s.fsInfo != nil && s.fsInfo.Type.hasNoUnixModes() { + s.logger.Debug("Secondary storage: mode of %s not mirrored (%s has no Unix permissions)", filepath.Base(dest), s.fsInfo.Type) + } else if modeErr = secondaryChmod(ctx, tempName, sourceInfo.Mode(), to); modeErr != nil { + s.logger.Debug("Secondary storage: unable to mirror permissions on %s: %v", tempName, modeErr) } if _, err := safefs.Run(ctx, "secondary-chtimes", tempName, to, func() (struct{}, error) { return struct{}{}, os.Chtimes(tempName, sourceInfo.ModTime(), sourceInfo.ModTime()) @@ -406,6 +430,9 @@ func (s *SecondaryStorage) copyFile(ctx context.Context, src, dest string) (err return fmt.Errorf("failed to finalize copy to %s: %w", dest, err) } tempName = "" + if modeErr != nil && modeFailed != nil { + modeFailed(dest, modeErr) + } elapsed := time.Since(start) var rateStr string diff --git a/internal/storage/secondary_test.go b/internal/storage/secondary_test.go index 7fc4da4c..93ba7078 100644 --- a/internal/storage/secondary_test.go +++ b/internal/storage/secondary_test.go @@ -436,7 +436,7 @@ func TestSecondaryStorage_CopyFile_SourceCloseErrorAfterCommitStillSucceeds(t *t } dest := filepath.Join(t.TempDir(), "node-backup.tar.zst") - if err := s.copyFile(context.Background(), src, dest); err != nil { + if err := s.copyFile(context.Background(), src, dest, nil); err != nil { t.Fatalf("copyFile must succeed despite a source-close error after a durable rename; got %v", err) } @@ -456,12 +456,12 @@ func TestSecondaryStorage_CopyFile_CoversErrorBranches(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() - if err := storage.copyFile(ctx, "a", "b"); !errors.Is(err, context.Canceled) { + if err := storage.copyFile(ctx, "a", "b", nil); !errors.Is(err, context.Canceled) { t.Fatalf("copyFile canceled err=%v want context.Canceled", err) } // Missing source -> stat error. - if err := storage.copyFile(context.Background(), filepath.Join(t.TempDir(), "missing"), filepath.Join(t.TempDir(), "dest")); err == nil { + if err := storage.copyFile(context.Background(), filepath.Join(t.TempDir(), "missing"), filepath.Join(t.TempDir(), "dest"), nil); err == nil { t.Fatalf("expected error for missing source") } @@ -475,7 +475,7 @@ func TestSecondaryStorage_CopyFile_CoversErrorBranches(t *testing.T) { if err := os.WriteFile(src, []byte("data"), 0o600); err != nil { t.Fatalf("WriteFile: %v", err) } - if err := storage.copyFile(context.Background(), src, filepath.Join(destDirFile, "out")); err == nil { + if err := storage.copyFile(context.Background(), src, filepath.Join(destDirFile, "out"), nil); err == nil { t.Fatalf("expected error for invalid destination directory") } @@ -484,7 +484,7 @@ func TestSecondaryStorage_CopyFile_CoversErrorBranches(t *testing.T) { if err := os.MkdirAll(srcDir, 0o700); err != nil { t.Fatalf("MkdirAll: %v", err) } - if err := storage.copyFile(context.Background(), srcDir, filepath.Join(t.TempDir(), "out")); err == nil { + if err := storage.copyFile(context.Background(), srcDir, filepath.Join(t.TempDir(), "out"), nil); err == nil { t.Fatalf("expected error when reading from directory source") } @@ -494,7 +494,7 @@ func TestSecondaryStorage_CopyFile_CoversErrorBranches(t *testing.T) { if err := os.MkdirAll(renameDest, 0o700); err != nil { t.Fatalf("MkdirAll: %v", err) } - if err := storage.copyFile(context.Background(), src, renameDest); err == nil { + if err := storage.copyFile(context.Background(), src, renameDest, nil); err == nil { t.Fatalf("expected error when renaming over existing directory") } @@ -510,7 +510,7 @@ func TestSecondaryStorage_CopyFile_CoversErrorBranches(t *testing.T) { if err := os.WriteFile(srcFile, []byte("data"), 0o600); err != nil { t.Fatalf("WriteFile: %v", err) } - if err := storage.copyFile(context.Background(), srcFile, filepath.Join(unwritable, "out")); err == nil { + if err := storage.copyFile(context.Background(), srcFile, filepath.Join(unwritable, "out"), nil); err == nil { t.Fatalf("expected error when CreateTemp cannot write to dest dir") } } @@ -1093,7 +1093,7 @@ func TestSecondaryStorage_CopyFile_RespectsSourcePermissionsAndChtimesBestEffort } dest := filepath.Join(t.TempDir(), "dest") - if err := storage.copyFile(context.Background(), src, dest); err != nil { + if err := storage.copyFile(context.Background(), src, dest, nil); err != nil { t.Fatalf("copyFile error: %v", err) } st, err := os.Stat(dest) @@ -1104,3 +1104,105 @@ func TestSecondaryStorage_CopyFile_RespectsSourcePermissionsAndChtimesBestEffort t.Fatalf("dest perm=%#o want %#o", st.Mode().Perm(), 0o640) } } + +// Without ownership the mode copyFile mirrors from the source is the one the copy keeps: a +// chmod that failed there is reported as the ownership branch reports its own (the fact +// line, then StoreIssuePermissionsNotSet); a clean copy records nothing. +func TestSecondaryStorage_Store_ModeNotSetWithoutOwnershipIsReported(t *testing.T) { + for _, tc := range []struct { + name string + chmodErr error + }{ + {"clean", nil}, + {"chmod failed", &os.PathError{Op: "chmod", Path: "x", Err: fs.ErrPermission}}, + } { + t.Run(tc.name, func(t *testing.T) { + if tc.chmodErr != nil { + prev := secondaryChmod + secondaryChmod = func(context.Context, string, os.FileMode, time.Duration) error { return tc.chmodErr } + t.Cleanup(func() { secondaryChmod = prev }) + } + var buf strings.Builder + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&buf) + destDir := t.TempDir() + storage, _ := NewSecondaryStorage(&config.Config{SecondaryEnabled: true, SecondaryPath: destDir}, logger, "") + storage.fsInfo = &FilesystemInfo{Type: FilesystemCIFS, SupportsOwnership: false} + backupFile := filepath.Join(t.TempDir(), "node-backup-20240102-030405.tar.zst") + if err := os.WriteFile(backupFile, []byte("data"), 0o600); err != nil { + t.Fatalf("WriteFile: %v", err) + } + + if err := storage.Store(context.Background(), backupFile, &types.BackupMetadata{}); err != nil { + t.Fatalf("Store error: %v", err) + } + + issues := storage.LastStoreIssues() + line := " Permissions failed: " + filepath.Base(backupFile) + ": permission denied\n" + if tc.chmodErr == nil { + if len(issues) != 0 || strings.Contains(buf.String(), "Permissions failed") { + t.Fatalf("clean copy: issues %v, log:\n%s", issues, buf.String()) + } + return + } + if len(issues) != 1 || issues[0] != StoreIssuePermissionsNotSet { + t.Fatalf("issues = %v, want permissions not set", issues) + } + if !strings.Contains(buf.String(), line) { + t.Fatalf("missing %q in:\n%s", line, buf.String()) + } + }) + } +} + +// A FAT-family Secondary has no Unix permissions: copyFile does not chmod the copies and +// Store records nothing. Another filesystem without ownership keeps the chmod and reports +// a copy whose mode could not be set. +func TestSecondaryStorage_Store_ModeSkippedOnFAT(t *testing.T) { + for _, tc := range []struct { + fsType FilesystemType + skipped bool + }{ + {FilesystemFAT32, true}, + {FilesystemFAT, true}, + {FilesystemMSDOS, true}, + {FilesystemExFAT, true}, + {FilesystemCIFS, false}, + {FilesystemNTFS, false}, + } { + t.Run(string(tc.fsType), func(t *testing.T) { + calls := 0 + prev := secondaryChmod + secondaryChmod = func(context.Context, string, os.FileMode, time.Duration) error { + calls++ + return &os.PathError{Op: "chmod", Path: "x", Err: fs.ErrPermission} + } + t.Cleanup(func() { secondaryChmod = prev }) + + var buf strings.Builder + logger := logging.New(types.LogLevelDebug, false) + logger.SetOutput(&buf) + storage, _ := NewSecondaryStorage(&config.Config{SecondaryEnabled: true, SecondaryPath: t.TempDir()}, logger, "") + storage.fsInfo = &FilesystemInfo{Type: tc.fsType, SupportsOwnership: false} + backupFile := filepath.Join(t.TempDir(), "node-backup-20240102-030405.tar.zst") + if err := os.WriteFile(backupFile, []byte("data"), 0o600); err != nil { + t.Fatal(err) + } + if err := storage.Store(context.Background(), backupFile, &types.BackupMetadata{}); err != nil { + t.Fatalf("Store error: %v", err) + } + + issues := storage.LastStoreIssues() + failed := strings.Contains(buf.String(), "Permissions failed") + if tc.skipped { + if calls != 0 || len(issues) != 0 || failed { + t.Fatalf("FAT: chmod calls %d, issues %v, log:\n%s", calls, issues, buf.String()) + } + return + } + if calls == 0 || len(issues) != 1 || issues[0] != StoreIssuePermissionsNotSet || !failed { + t.Fatalf("chmod calls %d, issues %v, want permissions not set; log:\n%s", calls, issues, buf.String()) + } + }) + } +} diff --git a/internal/storage/storage.go b/internal/storage/storage.go index e94840a3..86479095 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -28,6 +28,9 @@ const ( // Filesystems that do NOT support Unix ownership FilesystemFAT32 FilesystemType = "vfat" FilesystemFAT FilesystemType = "fat" + // FilesystemMSDOS is the kernel's FAT driver without long names: FAT-family, treated + // like FilesystemFAT everywhere. + FilesystemMSDOS FilesystemType = "msdos" FilesystemExFAT FilesystemType = "exfat" FilesystemNTFS FilesystemType = "ntfs" FilesystemFUSE FilesystemType = "fuse" @@ -35,6 +38,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" @@ -50,6 +56,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 @@ -315,10 +323,10 @@ func (f FilesystemType) SupportsUnixOwnership() bool { FilesystemXFS, FilesystemBtrfs, FilesystemZFS, FilesystemJFS, FilesystemReiserFS, FilesystemOverlay, FilesystemTmpfs: return true - case FilesystemFAT32, FilesystemFAT, FilesystemExFAT, + case FilesystemFAT32, FilesystemFAT, FilesystemMSDOS, FilesystemExFAT, FilesystemNTFS, FilesystemFUSE: return false - case FilesystemNFS, FilesystemNFS4, FilesystemCIFS, FilesystemSMB: + case FilesystemNFS, FilesystemNFS4, FilesystemCIFS, FilesystemSMB, FilesystemCeph: // Network filesystems need runtime testing return false default: @@ -326,10 +334,22 @@ func (f FilesystemType) SupportsUnixOwnership() bool { } } +// hasNoUnixModes reports a FAT-family filesystem (vfat, fat, msdos, exfat): it has no Unix +// permissions, the mount options give every file its mode, so there is no mode for a +// chmod to set on a copy. +func (f FilesystemType) hasNoUnixModes() bool { + switch f { + case FilesystemFAT32, FilesystemFAT, FilesystemMSDOS, FilesystemExFAT: + return true + default: + return false + } +} + // 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 @@ -340,7 +360,7 @@ func (f FilesystemType) IsNetworkFilesystem() bool { // from ownership operations (incompatible filesystems like FAT32/CIFS) func (f FilesystemType) ShouldAutoExclude() bool { switch f { - case FilesystemFAT32, FilesystemFAT, FilesystemExFAT, FilesystemNTFS, FilesystemFUSE, FilesystemCIFS, FilesystemSMB: + case FilesystemFAT32, FilesystemFAT, FilesystemMSDOS, FilesystemExFAT, FilesystemNTFS, FilesystemFUSE, FilesystemCIFS, FilesystemSMB: return true default: return false diff --git a/internal/storage/storage_step1_timeout_test.go b/internal/storage/storage_step1_timeout_test.go index 3bdd811f..346bd2e8 100644 --- a/internal/storage/storage_step1_timeout_test.go +++ b/internal/storage/storage_step1_timeout_test.go @@ -77,7 +77,7 @@ func TestSecondaryCopyFileLeafOpenTimeout(t *testing.T) { } done := make(chan error, 1) - go func() { done <- s.copyFile(t.Context(), fifo, filepath.Join(dir, "dst")) }() + go func() { done <- s.copyFile(t.Context(), fifo, filepath.Join(dir, "dst"), nil) }() select { case err := <-done: if !errors.Is(err, safefs.ErrTimeout) { @@ -104,7 +104,7 @@ func TestSecondaryCopyFileHealthy(t *testing.T) { t.Fatalf("NewSecondaryStorage: %v", err) } dst := filepath.Join(dir, "dst") - if err := s.copyFile(context.Background(), src, dst); err != nil { + if err := s.copyFile(context.Background(), src, dst, nil); err != nil { t.Fatalf("copyFile: %v", err) } data, err := os.ReadFile(dst) diff --git a/internal/storage/storage_step2_timeout_test.go b/internal/storage/storage_step2_timeout_test.go index 2e7fa027..8cb92e49 100644 --- a/internal/storage/storage_step2_timeout_test.go +++ b/internal/storage/storage_step2_timeout_test.go @@ -49,7 +49,7 @@ func TestSecondaryCopyFileMidStreamWedge(t *testing.T) { dst := filepath.Join(dir, "dst") done := make(chan error, 1) - go func() { done <- s.copyFile(t.Context(), fifo, dst) }() + go func() { done <- s.copyFile(t.Context(), fifo, dst, nil) }() select { case err := <-done: if !errors.Is(err, safefs.ErrTimeout) { @@ -82,7 +82,7 @@ func TestSecondaryCopyFileHealthyLarge(t *testing.T) { t.Fatalf("NewSecondaryStorage: %v", err) } dst := filepath.Join(dir, "dst") - if err := s.copyFile(context.Background(), src, dst); err != nil { + if err := s.copyFile(context.Background(), src, dst, nil); err != nil { t.Fatalf("copyFile: %v", err) } got, err := os.ReadFile(dst) 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()) + } +} 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) + } +} 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..c5eb525e --- /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: 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})) + 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/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) 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 diff --git a/internal/whatsnew/registry.go b/internal/whatsnew/registry.go index 41ff34dd..6f78ca07 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.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