diff --git a/DESIGN.md b/DESIGN.md index 0afc955..f4bf9ab 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -310,7 +310,8 @@ used again. - tip is the held SHA, or was asked before and the question expired or was skipped, or its check failed, or it was refused, or it failed `max_failed_tries` times: nothing to do. `pullgate status NAME - --ask-again` clears this mark. + --ask-again` clears this mark, and also closes an open question for + the tip (section 13). - tip is not a descendant of the applied SHA (an older commit, a force push, a rewritten branch) and `allow_older` is false: refuse. Send one notice for this SHA and mark it refused. When `allow_older` is true, @@ -350,7 +351,11 @@ name order and skips names that start with `.`. Kinds: - `reply`: `question`, `text`: a reply to that question, or a new message when the listener does not know the question. -A file the listener cannot parse is removed and logged. If Telegram refuses +A file the listener cannot read or parse is removed and logged. The owner +also gets one notice per round for such files: "an outbox message of +instance NAME could not be read and was removed", with the command +`pullgate status NAME --ask-again`. The notice shows no part of the file, +because it may be a question that the owner never saw. If Telegram refuses a message, the listener tries again on the next round, and drops it after one day with a log line. @@ -649,7 +654,12 @@ closed after a cut. the held SHA, the pause file and an apply in progress. Flags: - `--ask-again`: clear the marks of the last fetched tip (expired, skipped, - check failed, refused, failed, held), so the next run asks again; + check failed, refused, failed, held), so the next run asks again. If a + question for the tip is open (for example its message was lost), root + removes it from its state, then asks the listener to close its message. + The next run asks again with a new question ID and a new code. An + approval for the old code finds no question and does nothing, even one + that the listener wrote before the close; - `--pause`: write the pause file; - `--resume`: remove the pause file and clear an apply in progress. diff --git a/README.md b/README.md index 69a0d42..46e89ed 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,10 @@ sections 5 and 11. `pullgate status NAME` shows the state. As root: `pullgate status NAME --pause` stops the instance, `pullgate status NAME --resume` starts it again, and -`pullgate status NAME --ask-again` asks about the tip again. +`pullgate status NAME --ask-again` asks about the tip again. If a question +for the tip is still open (for example its Telegram message was lost), it +closes that question too: the next run sends a new question, and the Apply +button of the old one no longer works. ## Example 1: a config file and a service reload diff --git a/THREAT-MODEL.md b/THREAT-MODEL.md index 6798375..ec0dcc1 100644 --- a/THREAT-MODEL.md +++ b/THREAT-MODEL.md @@ -254,7 +254,12 @@ tap on a closed or expired question, writes nothing. The listener also keeps the last 200 update IDs it handled and ignores an update with one of them, a tap or a `/check` alike. Root removes a question from its state before it acts, so a replayed approval finds no question. -Root checks the time limit itself. A tap on a superseded question that the +Root checks the time limit itself. `--ask-again` removes an open question +for the tip from root's state (its message may be lost), so a tap on it, +even one already written, finds no question; the new question has a new +code. A question lost in the outbox is not silent: the listener tells the +owner when it removes an outbox file that it cannot read, and names the +instance, but shows no part of the file. A tap on a superseded question that the listener took before it handled the close still applies the old SHA (rule 2); the forward-only check still holds then. diff --git a/cmd/pullgate/main.go b/cmd/pullgate/main.go index 02f01c3..c4e4730 100644 --- a/cmd/pullgate/main.go +++ b/cmd/pullgate/main.go @@ -29,6 +29,8 @@ commands: run NAME fetch, check, and ask (the timer runs it, as root) apply NAME act on approvals (the path unit runs it, as root) status NAME [--ask-again] [--pause] [--resume] + --ask-again: ask about the last tip again; it also closes + an open question for it, so the next run sends a new one listen [--config FILE] the Telegram listener (its own user) version` diff --git a/internal/gate/gate_test.go b/internal/gate/gate_test.go index bd9dcc6..f8b16ff 100644 --- a/internal/gate/gate_test.go +++ b/internal/gate/gate_test.go @@ -229,6 +229,134 @@ func TestSkip(t *testing.T) { _ = a } +// TestAskAgainReplacesALostQuestion: the message of an open question was +// lost (code6x/pullgate#22). --ask-again closes it, the next run asks +// again with a new question, and an approval for the old code does not +// apply. +func TestAskAgainReplacesALostQuestion(t *testing.T) { + f := newFixture(t, ``, "") + tip := f.commit("first", "one", nil) + if r := f.run(); r != Asked { + t.Fatalf("run: %s", r) + } + old := f.ask() // read and dropped: as if the listener lost it + if r := f.run(); r != Nothing { + t.Fatalf("an open question is not asked again by a plain run: %s", r) + } + var out strings.Builder + if err := f.env.Status(&out, StatusFlags{AskAgain: true}); err != nil { + t.Fatal(err) + } + if !strings.Contains(out.String(), "question: none") { + t.Fatalf("--ask-again must close the open question:\n%s", out.String()) + } + ms := f.outbox() + if len(ms) != 1 || ms[0].Kind != spool.KindEdit || ms[0].Question != old.Question || !strings.Contains(ms[0].Text, "closed") { + t.Fatalf("--ask-again must close the old message: %+v", ms) + } + if r := f.run(); r != Asked { + t.Fatalf("the run after --ask-again must ask: %s", r) + } + nq := f.ask() + if nq.Question == old.Question || nq.Code == old.Code { + t.Fatalf("the new question must have a new ID and code: old %d, new %d", old.Question, nq.Question) + } + if q := f.state().Open(); q == nil || q.ID != nq.Question || q.SHA != tip { + t.Fatalf("the new question must be open: %+v", q) + } + // An Apply for the old question applies nothing. + f.tap(old.Code, "apply") + if r := f.apply(); r == Applied || f.state().Applied != "" { + t.Fatalf("an Apply for the closed question must not apply: %s", r) + } + if q := f.state().Open(); q == nil || q.ID != nq.Question { + t.Fatal("the new question must stay open") + } + // The new one still works. + f.tap(nq.Code, "apply") + if r := f.apply(); r != Applied || f.state().Applied != tip { + t.Fatalf("an Apply for the new question applies: %s", r) + } +} + +// TestAskAgainDropsAnApprovalWrittenBefore: a tap on the lost question +// that reached the inbox before --ask-again finds no question. +func TestAskAgainDropsAnApprovalWrittenBefore(t *testing.T) { + f := newFixture(t, ``, "") + f.commit("first", "one", nil) + f.run() + old := f.ask() + f.tap(old.Code, "apply") + if err := f.env.Status(new(strings.Builder), StatusFlags{AskAgain: true}); err != nil { + t.Fatal(err) + } + if r := f.apply(); r == Applied || f.state().Applied != "" { + t.Fatalf("an approval written before --ask-again must not apply: %s", r) + } + if r := f.run(); r != Asked { + t.Fatalf("the next run asks again: %s", r) + } +} + +// TestAskAgainInAutoMode: an auto question for the tip is removed too, with +// no edit (it has no message), and the old auto approval applies nothing. +func TestAskAgainInAutoMode(t *testing.T) { + f := newFixture(t, `mode = "auto"`, "") + tip := f.commit("first", "one", nil) + if r := f.run(); r != AutoQueued { + t.Fatalf("run: %s", r) + } + autoCode := func() string { + b, _ := os.ReadFile(f.env.Dir.AutoFile()) + return strings.TrimSpace(string(b)) + } + oldAuto := autoCode() + if oldAuto == "" { + t.Fatal("no auto approval file") + } + if err := f.env.Status(new(strings.Builder), StatusFlags{AskAgain: true}); err != nil { + t.Fatal(err) + } + if f.state().Open() != nil { + t.Fatal("--ask-again must remove the open auto question") + } + if ms := f.outbox(); len(ms) != 0 { + t.Fatalf("an auto question has no message to edit: %+v", ms) + } + if r := f.apply(); r == Applied || f.state().Applied != "" { + t.Fatalf("the old auto approval must not apply: %s", r) + } + if r := f.run(); r != AutoQueued || autoCode() == oldAuto || autoCode() == "" { + t.Fatalf("the next run queues a new auto approval: %s", r) + } + if r := f.apply(); r != Applied || f.state().Applied != tip { + t.Fatalf("the new auto approval applies: %s", r) + } +} + +// TestAskAgainKeepsAnOpenQuestionForAnotherSHA: --ask-again acts only on +// the tip; an open question for another SHA is left alone. +func TestAskAgainKeepsAnOpenQuestionForAnotherSHA(t *testing.T) { + f := newFixture(t, ``, "") + f.commit("first", "one", nil) + f.run() + q := f.ask() + st := f.state() + st.LastTip = strings.Repeat("b", 40) + if err := f.env.Dir.Save(st); err != nil { + t.Fatal(err) + } + if err := f.env.Status(new(strings.Builder), StatusFlags{AskAgain: true}); err != nil { + t.Fatal(err) + } + if o := f.state().Open(); o == nil || o.ID != q.Question { + t.Fatal("an open question for another SHA must stay open") + } + if ms := f.outbox(); len(ms) != 0 { + t.Fatalf("no edit for a question that stays: %+v", ms) + } +} + func TestExpiredApprovalIsIgnored(t *testing.T) { f := newFixture(t, ``, "[limits]\napproval_ttl = \"1h\"\n") f.commit("first", "one", nil) diff --git a/internal/gate/status.go b/internal/gate/status.go index b8c8bf5..458aed2 100644 --- a/internal/gate/status.go +++ b/internal/gate/status.go @@ -36,6 +36,7 @@ func (e *Env) Status(w io.Writer, f StatusFlags) error { return err } changed := false + var closed *state.Question if f.Resume { if err := e.Dir.Resume(); err != nil { return err @@ -50,6 +51,7 @@ func (e *Env) Status(w io.Writer, f StatusFlags) error { if st.Held == st.LastTip { st.Held = "" } + closed = forgetOpen(st, st.LastTip) changed = true } if changed { @@ -57,6 +59,12 @@ func (e *Env) Status(w io.Writer, f StatusFlags) error { return err } } + if closed != nil && !closed.Auto { + // After the save: the listener forgets the code only when root + // has forgotten it too. + e.edit(closed.ID, "This question for "+code(closed.SHA)+" is closed: the owner ran --ask-again. The next run asks again.") + _, _ = fmt.Fprintf(w, "The open question %d for %s is closed. The next run asks again.\n", closed.ID, closed.SHA) + } show := func(k, v string) { _, _ = fmt.Fprintf(w, "%-12s %s\n", k+":", v) } show("instance", e.Cfg.Name) show("mode", e.Cfg.Mode) @@ -85,11 +93,25 @@ func (e *Env) Status(w io.Writer, f StatusFlags) error { return nil } +// forgetOpen removes the open question for sha from the state and returns +// it, so that the next run asks again with a new question and a new code. +// Its message may never have reached the owner (a lost outbox file). +// Unlike a superseded question, it does not stay in the list: an approval +// for its code, even one written before this, finds no question and is +// ignored (fail closed). +func forgetOpen(st *state.State, sha string) *state.Question { + for i := range st.Questions { + if q := st.Questions[i]; q.Open && q.SHA == sha { + st.Remove(i) + return &q + } + } + return nil +} + func orNone(s string) string { if s == "" { return "none" } return s } - -var _ = state.MarkAsked diff --git a/internal/listener/listener.go b/internal/listener/listener.go index 5fe01a0..5b85255 100644 --- a/internal/listener/listener.go +++ b/internal/listener/listener.go @@ -195,10 +195,23 @@ instances: l.Log.Warn("read the outbox", "instance", inst, "err", err) continue } + told := false for _, f := range files { if f.Err != nil { + if err := d.Done(f.Name); err != nil { + // Not removed: say nothing to the owner, or every + // round would send the same notice. + l.Log.Warn("bad outbox file, could not remove it", "instance", inst, "file", f.Name, "err", f.Err, "remove_err", err) + continue + } l.Log.Warn("bad outbox file, removed", "instance", inst, "file", f.Name, "err", f.Err) - _ = d.Done(f.Name) + if !told { + // The text cannot be read, but the owner must know that + // a message (maybe a question) is lost. One notice per + // pass is enough, also for many bad files. + told = true + l.badOutboxNotice(ctx, inst) + } continue } err := l.send(ctx, inst, f.Message) @@ -226,6 +239,17 @@ instances: } } +// badOutboxNotice tells the owner that an outbox file of inst could not be +// read and was removed. It shows no part of the file. A failed send is +// only logged: the file is gone either way. +func (l *Listener) badOutboxNotice(ctx context.Context, inst string) { + name := html.EscapeString(inst) + text := "pullgate: an outbox message of instance " + name + " could not be read and was removed. It may have been a question. To ask again, run as root: pullgate status " + name + " --ask-again" + if _, err := l.TG.SendMessage(ctx, l.Cfg.ChatID, text, nil, 0); err != nil { + l.Log.Warn("send the notice about a bad outbox file", "instance", inst, "err", err) + } +} + func (l *Listener) send(ctx context.Context, inst string, m spool.Message) error { chat := l.Cfg.ChatID switch m.Kind { diff --git a/internal/listener/listener_test.go b/internal/listener/listener_test.go index 60c85bb..715797b 100644 --- a/internal/listener/listener_test.go +++ b/internal/listener/listener_test.go @@ -198,6 +198,64 @@ func TestReplyAndNotice(t *testing.T) { } } +// TestBadOutboxFileTellsTheOwner: an outbox file that the listener cannot +// read is removed, and the owner gets one notice that names the instance +// and the command to ask again (code6x/pullgate#22). +func TestBadOutboxFileTellsTheOwner(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("root can read a file with mode 0") + } + f := newL(t) + out := f.d.Outbox() + if err := os.WriteFile(filepath.Join(out, "0-a-unreadable"), []byte(`{"kind":"notice","text":"secret text"}`), 0o000); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(out, "0-b-broken"), []byte("not json"), 0o600); err != nil { + t.Fatal(err) + } + _ = f.d.Send(os.Getegid(), spool.Message{Kind: spool.KindNotice, Text: "good notice"}) + f.l.Outbox(context.Background()) + ms := f.tg.Messages() + if len(ms) != 2 { + t.Fatalf("want one notice for the bad files and the good message, got %+v", ms) + } + n := ms[0].Text + if !strings.Contains(n, ""+inst+"") || !strings.Contains(n, "pullgate status "+inst+" --ask-again") || !strings.Contains(n, "removed") { + t.Fatalf("the notice must name the instance and the command: %q", n) + } + if strings.Contains(n, "secret") || strings.Contains(n, "json") || strings.Contains(n, "0-a-unreadable") { + t.Fatalf("the notice must not show the file: %q", n) + } + if ms[1].Text != "good notice" { + t.Fatalf("the good message must still be sent: %+v", ms[1]) + } + if left, _ := os.ReadDir(out); len(left) != 0 { + t.Fatalf("the bad files must be removed: %v", left) + } + // A pass with no bad file sends no notice. + f.l.Outbox(context.Background()) + if len(f.tg.Messages()) != 2 { + t.Fatal("no new notice without a bad file") + } +} + +// TestBadOutboxEntryThatStaysSendsNoNotice: an outbox entry that cannot +// be removed (a directory with a file in it) gets no "removed" notice, so +// the chat is not filled with one notice per round. +func TestBadOutboxEntryThatStaysSendsNoNotice(t *testing.T) { + f := newL(t) + dir := filepath.Join(f.d.Outbox(), "0-dir") + if err := os.MkdirAll(filepath.Join(dir, "x"), 0o700); err != nil { + t.Fatal(err) + } + for range 3 { + f.l.Outbox(context.Background()) + } + if ms := f.tg.Messages(); len(ms) != 0 { + t.Fatalf("no notice for an entry that was not removed: %+v", ms) + } +} + func TestTokenNeverInErrors(t *testing.T) { c := telegram.New("http://127.0.0.1:1", token) _, err := c.SendMessage(context.Background(), 1, "x", nil, 0)