diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index faff10b..d5c94d7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -105,13 +105,33 @@ jobs: exit 1 fi + units-first-run: + name: first run under the units + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: go.mod + - name: Build + run: CGO_ENABLED=0 go build -trimpath -o "${RUNNER_TEMP}/pullgate" ./cmd/pullgate + - name: The first run and the listener under the real units + # A fresh install with contrib/systemd/ and no drop-in: pullgate run + # under pullgate-run@.service, then pullgate listen under + # pullgate-listen.service against a fake Telegram. A unit setting + # that breaks a run fails here (issue #19). + run: sudo sh test/units/first-run.sh "${RUNNER_TEMP}/pullgate" + lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - name: shellcheck install.sh + - name: shellcheck install.sh and test/units/first-run.sh # A fixed release of ShellCheck, checked by its SHA-256. Dependabot # does not update it. Update it by hand: the version and the SHA-256 # of shellcheck-VERSION.linux.x86_64.tar.xz together. @@ -127,7 +147,7 @@ jobs: echo "${SHELLCHECK_SHA256} $dir/shellcheck.tar.xz" | sha256sum --strict -c - tar -xJf "$dir/shellcheck.tar.xz" -C "$dir" "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --version - "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh install.sh + "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh install.sh test/units/first-run.sh - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: go.mod diff --git a/DESIGN.md b/DESIGN.md index 5132fdf..7ac300c 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -104,7 +104,8 @@ can be less strict; each example sets what its `apply` needs. | `/var/lib/pullgate/NAME/auto-approval` | root, 0600 | the approval that `auto` mode writes (section 13) | | `/var/lib/pullgate/NAME/trees/` | root, 0711 | the trees of one process (section 6) | | `/var/spool/pullgate/` and `/var/spool/pullgate/NAME/` | root, 0755 | the spool | -| `/var/spool/pullgate/NAME/outbox` | root, group listener, 0770 | messages from root to the listener | +| `/var/spool/pullgate/NAME/outbox` | root, group listener, 0770 (no setgid) | messages from root to the listener | +| `/var/spool/pullgate/NAME/outbox/*.json` | root, group listener, 0640 | one message | | `/var/spool/pullgate/NAME/inbox` | listener, 0700 | approvals from the listener to root | | `/var/spool/pullgate/NAME/check` | listener, 0700 | check requests from the listener to root | | `/var/lib/pullgate-listener/state.json` | listener, 0600 | the listener's open questions | @@ -132,6 +133,22 @@ in the same directory. `/var/spool/pullgate` and `NAME/` are owned by root and pass the check of section 6, so the listener cannot swap `outbox`, `inbox` or `check` for a symlink. +**No setgid bit on the outbox.** Root gives each outbox file the group of +the listener user itself: it creates the file, then calls `fchown` on the +open file (group only) and `fchmod` to 0640, all before the rename. So the +listener never sees an outbox file with another group, and no directory +needs the setgid bit. This matters because `pullgate-run@.service` sets +`RestrictSUIDSGID=yes`, which refuses every `chmod` that sets a setuid or +setgid bit. Version 0.1.0 made the outbox `2770`, and its first run failed +under the unit (issue #19). The other choice was to remove +`RestrictSUIDSGID=yes` from the run unit. We keep it: the run unit runs as +root with `CAP_FOWNER`, and it must never make a setuid or setgid file. +`Ensure` also takes the setgid bit off an outbox made by 0.1.0. Removing a +bit is allowed under `RestrictSUIDSGID=yes`. Because root is not in the +listener's group, the `fchown` needs `CAP_CHOWN`: every unit that runs +`pullgate run` or `pullgate apply` must keep it. Without it, root cannot +write to the outbox, and the owner gets no question and no reply. + ## 5. The instance config ```toml @@ -673,6 +690,15 @@ and `pullgate`. They never use the network. They show that the script installs nothing when a check fails (section 16), and that it installs when all checks pass. CI also runs a pinned ShellCheck on the script. +`test/units/first-run.sh` runs on CI as root, on the throwaway runner. It +installs the binary and the units of `contrib/systemd/` as the README +says, with no drop-in. It runs the first `pullgate run` under the real +`pullgate-run@.service`, checks the owners and modes of section 4, then +runs `pullgate listen` under the real `pullgate-listen.service` against a +fake Telegram on `127.0.0.1`, and checks that the listener sent the +question and removed it from the outbox. So a unit setting that breaks a +run or the listener fails CI. + ## 15. Not in version 0.1 - Self-update of pullgate. `install.sh` updates it only when root runs diff --git a/README.md b/README.md index 4d5bbaa..632f7c8 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,22 @@ The run unit sets `MemoryDenyWriteExecute=yes`, which also holds for `check` and `summary`. A command that needs a JIT (for example Node.js) does not work there. +### Update from 0.1.0 + +In 0.1.0 the first `pullgate run` failed under `pullgate-run@.service` +with `chmod .../outbox: operation not permitted` (issue #19). A host may +have a drop-in with `RestrictSUIDSGID=no` for the run unit as a +workaround. Newer versions do not need it. After you update the binary: + +1. Find the drop-in: `systemctl cat pullgate-run@NAME.service` shows each + file and its path. It is in `/etc/systemd/system/pullgate-run@.service.d/` + or in `/etc/systemd/system/pullgate-run@NAME.service.d/`. +2. Remove the line `RestrictSUIDSGID=no`, or the whole file when that is + all it holds. +3. Run `systemctl daemon-reload`. + +The next run takes the setgid bit off the outbox by itself. + ## Development ```sh diff --git a/THREAT-MODEL.md b/THREAT-MODEL.md index df350ce..6798375 100644 --- a/THREAT-MODEL.md +++ b/THREAT-MODEL.md @@ -105,7 +105,10 @@ user can write in the outbox and the inbox. Root never follows a path there: the spool directories above them are root-owned, root opens them with `os.OpenRoot`, and creates files with `O_CREAT|O_EXCL|O_NOFOLLOW` under a random name. So a symlink that the listener plants cannot make root write -or change a file elsewhere. +or change a file elsewhere. Root gives an outbox file the listener's group +with `fchown` on the open file, never through a path, and no spool +directory has the setgid bit. So the run unit keeps `RestrictSUIDSGID=yes` +(DESIGN.md section 4). *Check requests.* The listener can also write check requests at any time, without the owner. A request holds no data: root checks it like an diff --git a/contrib/systemd/pullgate-apply@.service b/contrib/systemd/pullgate-apply@.service index ae83feb..589d820 100644 --- a/contrib/systemd/pullgate-apply@.service +++ b/contrib/systemd/pullgate-apply@.service @@ -1,7 +1,9 @@ # pullgate apply NAME: act on approvals as root (DESIGN.md sections 9, 10). # It runs the owner's apply and health commands. What they need decides # how strict this unit can be, so the sandbox here is light. Add more in a -# drop-in for your instance, for example: +# drop-in for your instance. Keep CAP_CHOWN if you set +# CapabilityBoundingSet=: root needs it to give its replies in the outbox +# the listener's group (DESIGN.md section 4). For example: # systemctl edit pullgate-apply@config-repo.service # The example configs in contrib/examples say what fits them. [Unit] diff --git a/contrib/systemd/pullgate-run@.service b/contrib/systemd/pullgate-run@.service index 48c1344..11a19cc 100644 --- a/contrib/systemd/pullgate-run@.service +++ b/contrib/systemd/pullgate-run@.service @@ -21,7 +21,8 @@ TimeoutStartSec=30min KillMode=control-group # Sandbox. Root needs: CAP_SETUID/CAP_SETGID to run check as check_user, -# CAP_CHOWN/CAP_FOWNER to fix the spool owners, CAP_DAC_OVERRIDE to remove +# CAP_CHOWN/CAP_FOWNER to fix the spool owners and to give each outbox file +# the listener's group (DESIGN.md section 4), CAP_DAC_OVERRIDE to remove # files in the listener's inbox, CAP_KILL to stop a check that runs too long. CapabilityBoundingSet=CAP_SETUID CAP_SETGID CAP_CHOWN CAP_FOWNER CAP_DAC_OVERRIDE CAP_KILL AmbientCapabilities= diff --git a/internal/gate/run.go b/internal/gate/run.go index 345470d..557df50 100644 --- a/internal/gate/run.go +++ b/internal/gate/run.go @@ -41,19 +41,19 @@ const ( func (e *Env) head() string { return message.Head(e.Cfg.Host, e.Cfg.Name) } func (e *Env) notice(text string) { - if err := e.Spool.Send(spool.Message{Kind: spool.KindNotice, Text: e.head() + "\n" + text}); err != nil { + if err := e.Spool.Send(e.Owners.ListenerGID, spool.Message{Kind: spool.KindNotice, Text: e.head() + "\n" + text}); err != nil { e.Log.Error("write a notice to the outbox", "err", err) } } func (e *Env) reply(q int64, text string) { - if err := e.Spool.Send(spool.Message{Kind: spool.KindReply, Question: q, Text: e.head() + "\n" + text}); err != nil { + if err := e.Spool.Send(e.Owners.ListenerGID, spool.Message{Kind: spool.KindReply, Question: q, Text: e.head() + "\n" + text}); err != nil { e.Log.Error("write a reply to the outbox", "err", err) } } func (e *Env) edit(q int64, text string) { - if err := e.Spool.Send(spool.Message{Kind: spool.KindEdit, Question: q, Text: e.head() + "\n" + text}); err != nil { + if err := e.Spool.Send(e.Owners.ListenerGID, spool.Message{Kind: spool.KindEdit, Question: q, Text: e.head() + "\n" + text}); err != nil { e.Log.Error("write an edit to the outbox", "err", err) } } @@ -340,7 +340,7 @@ func (e *Env) decide(ctx context.Context, st *state.State, tip string) (Result, e.Log.Info("auto mode: apply queued", "sha", tip) return AutoQueued, nil } - if err := e.Spool.Send(spool.Message{Kind: spool.KindAsk, Question: nq.ID, Code: c, Expires: nq.Expires, Text: text}); err != nil { + if err := e.Spool.Send(e.Owners.ListenerGID, spool.Message{Kind: spool.KindAsk, Question: nq.ID, Code: c, Expires: nq.Expires, Text: text}); err != nil { return undo(err) } st.AskTimes = append(st.AskTimes, now) diff --git a/internal/listener/listener_test.go b/internal/listener/listener_test.go index 9df4846..60c85bb 100644 --- a/internal/listener/listener_test.go +++ b/internal/listener/listener_test.go @@ -55,7 +55,7 @@ func newL(t *testing.T) *lfix { // ask puts a question in the outbox and lets the listener send it. func (f *lfix) ask(id int64, c string) int64 { f.t.Helper() - if err := f.d.Send(spool.Message{Kind: spool.KindAsk, Question: id, Code: c, Expires: f.now.Add(time.Hour), Text: "pullgate on example-host\nApply this commit?"}); err != nil { + if err := f.d.Send(os.Getegid(), spool.Message{Kind: spool.KindAsk, Question: id, Code: c, Expires: f.now.Add(time.Hour), Text: "pullgate on example-host\nApply this commit?"}); err != nil { f.t.Fatal(err) } f.l.Outbox(context.Background()) @@ -131,7 +131,7 @@ func TestTapsThatDoNotCount(t *testing.T) { f.tap(owner, chat, msg, "a:"+code) }, "closed question": func(f *lfix, msg int64) { - if err := f.d.Send(spool.Message{Kind: spool.KindEdit, Question: 1, Text: "closed"}); err != nil { + if err := f.d.Send(os.Getegid(), spool.Message{Kind: spool.KindEdit, Question: 1, Text: "closed"}); err != nil { f.t.Fatal(err) } f.l.Outbox(context.Background()) @@ -159,7 +159,7 @@ func TestTapsThatDoNotCount(t *testing.T) { func TestExpiredAskIsNotSent(t *testing.T) { f := newL(t) - if err := f.d.Send(spool.Message{Kind: spool.KindAsk, Question: 1, Code: code, Expires: f.now.Add(-time.Minute), Text: "old"}); err != nil { + if err := f.d.Send(os.Getegid(), spool.Message{Kind: spool.KindAsk, Question: 1, Code: code, Expires: f.now.Add(-time.Minute), Text: "old"}); err != nil { t.Fatal(err) } f.l.Outbox(context.Background()) @@ -186,8 +186,8 @@ func TestStateSurvivesRestart(t *testing.T) { func TestReplyAndNotice(t *testing.T) { f := newL(t) msg := f.ask(1, code) - _ = f.d.Send(spool.Message{Kind: spool.KindReply, Question: 1, Text: "applied"}) - _ = f.d.Send(spool.Message{Kind: spool.KindNotice, Text: "notice"}) + _ = f.d.Send(os.Getegid(), spool.Message{Kind: spool.KindReply, Question: 1, Text: "applied"}) + _ = f.d.Send(os.Getegid(), spool.Message{Kind: spool.KindNotice, Text: "notice"}) f.l.Outbox(context.Background()) ms := f.tg.Messages() if len(ms) != 3 || ms[1].ReplyTo != msg || ms[2].Text != "notice" { diff --git a/internal/spool/spool.go b/internal/spool/spool.go index 43e71e8..7735887 100644 --- a/internal/spool/spool.go +++ b/internal/spool/spool.go @@ -89,7 +89,9 @@ const MaxCheck = 64 // Ensure makes the spool of the instance with the owners and modes of // DESIGN.md section 4. Base and Base/NAME belong to root (uid) and must // pass the check of rule 6; outbox and inbox are fixed through an open -// directory, never through a path. +// directory, never through a path. No directory gets the setgid bit: +// pullgate-run@.service has RestrictSUIDSGID=yes, which refuses a chmod +// that sets it. Send gives each outbox file its group itself. func (d Dirs) Ensure(uid uint32, o Owners) error { for _, p := range []string{d.Base, d.inst()} { if err := os.Mkdir(p, 0o755); err != nil && !errors.Is(err, fs.ErrExist) { @@ -114,7 +116,7 @@ func (d Dirs) Ensure(uid uint32, o Owners) error { uid, gid int mode os.FileMode }{ - {"outbox", int(uid), o.ListenerGID, 0o770 | os.ModeSetgid}, + {"outbox", int(uid), o.ListenerGID, 0o770}, {"inbox", o.ListenerUID, o.ListenerGID, 0o700}, {"check", o.ListenerUID, o.ListenerGID, 0o700}, } { @@ -150,8 +152,9 @@ func seqName() string { // writeIn writes data as a new file in dir, through os.Root: a random // temporary name with O_EXCL|O_NOFOLLOW, then a rename in the same dir to -// final (a new sequence name when final is ""). -func writeIn(dir string, data []byte, mode os.FileMode, final string) error { +// final (a new sequence name when final is ""). With gid >= 0 the file gets +// that group before it is renamed, so a reader never sees it with another. +func writeIn(dir string, data []byte, mode os.FileMode, gid int, final string) error { root, err := os.OpenRoot(dir) if err != nil { return fmt.Errorf("open %s: %w", dir, err) @@ -163,6 +166,9 @@ func writeIn(dir string, data []byte, mode os.FileMode, final string) error { return fmt.Errorf("write in %s: %w", dir, err) } _, err = f.Write(data) + if err == nil && gid >= 0 { + err = f.Chown(-1, gid) + } if err == nil { err = f.Chmod(mode) } @@ -185,13 +191,18 @@ func writeIn(dir string, data []byte, mode os.FileMode, final string) error { return nil } -// Send writes a message to the outbox (root). -func (d Dirs) Send(m Message) error { +// Send writes a message to the outbox (root). The file is owned by root, +// with group gid (the group of the listener user, Owners.ListenerGID) and +// mode 0640, so the listener can read it (DESIGN.md section 4). +func (d Dirs) Send(gid int, m Message) error { + if gid < 0 { + return errors.New("write in the outbox: no group for the listener. This is a bug in pullgate") + } b, err := json.Marshal(m) if err != nil { return err } - return writeIn(d.Outbox(), b, 0o640, "") + return writeIn(d.Outbox(), b, 0o640, gid, "") } // WriteApproval writes an approval to the inbox (the listener). @@ -200,13 +211,13 @@ func (d Dirs) WriteApproval(a Approval) error { if err != nil { return err } - return writeIn(d.Inbox(), b, 0o600, "") + return writeIn(d.Inbox(), b, 0o600, -1, "") } // WriteCheck writes a check request (the listener). It always has the same // name, so many requests before root reads them are one file. func (d Dirs) WriteCheck() error { - return writeIn(d.CheckDir(), []byte(CheckContent), 0o600, CheckFile) + return writeIn(d.CheckDir(), []byte(CheckContent), 0o600, -1, CheckFile) } // HasCheck reports whether the check directory holds a file whose name diff --git a/internal/spool/spool_test.go b/internal/spool/spool_test.go index fe5fb17..60dc7e2 100644 --- a/internal/spool/spool_test.go +++ b/internal/spool/spool_test.go @@ -105,7 +105,7 @@ func TestSendRefusesSymlinkedOutboxAndUnsafeSpool(t *testing.T) { if err := os.Symlink(target, filepath.Join(d.Outbox(), "00000000000000000001-x.json")); err != nil { t.Fatal(err) } - if err := d.Send(Message{Kind: KindNotice, Text: "x"}); err != nil { + if err := d.Send(os.Getegid(), Message{Kind: KindNotice, Text: "x"}); err != nil { t.Fatal(err) } if _, err := os.Lstat(target); err == nil { @@ -223,3 +223,103 @@ func TestBadCheckRequests(t *testing.T) { }) } } + +// otherGroup is a group of the test user other than its main group, or +// its main group when it has no other. +func otherGroup(t *testing.T) int { + t.Helper() + gs, err := os.Getgroups() + if err != nil { + t.Fatal(err) + } + for _, g := range gs { + if g != os.Getegid() { + return g + } + } + return os.Getegid() +} + +func ownerMode(t *testing.T, p string) (uid, gid int, mode os.FileMode) { + t.Helper() + fi, err := os.Lstat(p) + if err != nil { + t.Fatal(err) + } + st := fi.Sys().(*syscall.Stat_t) + return int(st.Uid), int(st.Gid), fi.Mode() +} + +// TestOwnersAndModes checks the spool of DESIGN.md section 4: no setgid +// bit anywhere (pullgate-run@.service has RestrictSUIDSGID=yes, issue +// #19), and every outbox file has the listener's group, so the listener +// can read it. +func TestOwnersAndModes(t *testing.T) { + uid, lgid := os.Geteuid(), otherGroup(t) + if lgid == os.Getegid() { + t.Log("the test user has only one group, so this test cannot show that Send sets the group") + } + d := Dirs{Base: filepath.Join(testgit.PrivateDir(t), "spool"), Name: "x"} + // A spool made by v0.1.0 has a setgid outbox: Ensure takes the bit off. + if err := os.MkdirAll(d.Outbox(), 0o700); err != nil { + t.Fatal(err) + } + if err := os.Chmod(d.Outbox(), 0o770|os.ModeSetgid); err != nil { + t.Fatal(err) + } + if err := d.Ensure(uint32(uid), Owners{ListenerUID: uid, ListenerGID: lgid}); err != nil { + t.Fatal(err) + } + for _, c := range []struct { + path string + gid int + mode os.FileMode + }{ + {d.Base, -1, os.ModeDir | 0o755}, + {filepath.Join(d.Base, d.Name), -1, os.ModeDir | 0o755}, + {d.Outbox(), lgid, os.ModeDir | 0o770}, + {d.Inbox(), lgid, os.ModeDir | 0o700}, + {d.CheckDir(), lgid, os.ModeDir | 0o700}, + } { + u, g, m := ownerMode(t, c.path) + if u != uid || (c.gid >= 0 && g != c.gid) || m != c.mode { + t.Errorf("%s: owner %d group %d mode %v, want owner %d group %d mode %v", c.path, u, g, m, uid, c.gid, c.mode) + } + } + if err := d.Send(lgid, Message{Kind: KindNotice, Text: "x"}); err != nil { + t.Fatal(err) + } + fs, err := d.ReadOutbox() + if err != nil || len(fs) != 1 || fs[0].Err != nil || fs[0].Message.Text != "x" { + t.Fatalf("the outbox must hold the message: %+v %v", fs, err) + } + u, g, m := ownerMode(t, filepath.Join(d.Outbox(), fs[0].Name)) + if u != uid || g != lgid || m != 0o640 { + t.Errorf("outbox file: owner %d group %d mode %v, want owner %d group %d mode -rw-r-----", u, g, m, uid, lgid) + } +} + +func TestSendDeny(t *testing.T) { + d := dirs(t) + if err := d.Send(-1, Message{Kind: KindNotice, Text: "x"}); err == nil { + t.Error("a message with no group must be refused") + } + if os.Geteuid() != 0 { + // A group the user is not in: the chown fails, and no file stays. + gs, _ := os.Getgroups() + in := map[int]bool{os.Getegid(): true} + for _, g := range gs { + in[g] = true + } + other := 1 + for in[other] { + other++ + } + if err := d.Send(other, Message{Kind: KindNotice, Text: "x"}); err == nil { + t.Error("a group the chown cannot give must be an error") + } + } + if left, _ := os.ReadDir(d.Outbox()); len(left) != 0 { + t.Fatalf("a refused message must leave no file: %v", left) + } +} diff --git a/test/units/faketg.py b/test/units/faketg.py new file mode 100644 index 0000000..fbe1039 --- /dev/null +++ b/test/units/faketg.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +"""A tiny fake Telegram Bot API for test/units/first-run.sh. + +It listens on 127.0.0.1:PORT. It answers every method with "ok", gives no +updates, and appends the name of each method it gets to LOG, one per line. +It never prints a request, so the test token never shows. + +Usage: python3 -I faketg.py PORT LOG +""" + +import json +import sys +import time +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +PORT = int(sys.argv[1]) +LOG = sys.argv[2] +CHAT = 4242 + + +class Handler(BaseHTTPRequestHandler): + next_id = 1 + + def do_POST(self): + length = int(self.headers.get("Content-Length") or 0) + self.rfile.read(length) + method = self.path.rsplit("/", 1)[-1] + with open(LOG, "a", encoding="utf-8") as f: + f.write(method + "\n") + if method == "getUpdates": + time.sleep(1) + result = [] + elif method == "sendMessage": + result = {"message_id": Handler.next_id, "chat": {"id": CHAT}, "text": ""} + Handler.next_id += 1 + else: + result = True + body = json.dumps({"ok": True, "result": result}).encode() + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def log_message(self, *args): + pass + + +ThreadingHTTPServer(("127.0.0.1", PORT), Handler).serve_forever() diff --git a/test/units/first-run.sh b/test/units/first-run.sh new file mode 100644 index 0000000..78ed1c6 --- /dev/null +++ b/test/units/first-run.sh @@ -0,0 +1,182 @@ +#!/bin/sh +# A fresh install with the units of contrib/systemd/, and the first run. +# +# It runs `pullgate run` under the real pullgate-run@.service and +# `pullgate listen` under the real pullgate-listen.service, with no +# drop-in. Then it checks the owners and modes of DESIGN.md section 4, and +# that the listener read the outbox and sent the question. So a unit +# setting that breaks a run fails here (issue #19). +# +# It changes the system: it installs /usr/local/bin/pullgate, adds users, +# and writes to /etc and /var. Run it only as root, on a machine you throw +# away, such as a CI runner. +# +# Usage: sudo sh test/units/first-run.sh PATH-TO-PULLGATE-BINARY +set -eu + +if [ "$(id -u)" -ne 0 ]; then + echo "first-run.sh: run it as root, on a machine you throw away" >&2 + exit 1 +fi +if [ $# -ne 1 ] || [ ! -f "$1" ]; then + echo "usage: first-run.sh PATH-TO-PULLGATE-BINARY" >&2 + exit 64 +fi + +here="$(cd "$(dirname "$0")" && pwd)" +top="$(cd "$here/../.." && pwd)" +name=config-repo +port=18443 +fail=0 + +err() { + echo "FAIL: $*" >&2 + fail=1 +} + +# The binary, the users and the directories, as in the README. +install -m 0755 -o root -g root "$1" /usr/local/bin/pullgate +id pullgate >/dev/null 2>&1 || useradd --system --no-create-home --user-group --shell /usr/sbin/nologin pullgate +id pullgate-check >/dev/null 2>&1 || useradd --system --no-create-home --user-group --shell /usr/sbin/nologin pullgate-check +install -d -m 0755 -o root -g root /etc/pullgate /var/spool/pullgate +install -d -m 0711 -o root -g root /var/lib/pullgate +install -m 0644 -o root -g root "$top"/contrib/systemd/* /etc/systemd/system/ + +# A local repo with one commit, owned by root. +src=/srv/pullgate-units-test +rm -rf "$src" +install -d -m 0755 -o root -g root "$src" +git init -q --bare "$src/$name.git" +git -C "$src/$name.git" symbolic-ref HEAD refs/heads/main +work="$(mktemp -d)" +printf 'a = 1\n' >"$work/policy.toml" +git -C "$work" init -q +git -C "$work" checkout -q -b main +git -C "$work" add policy.toml +git -C "$work" -c user.name=test -c user.email=test@example.com commit -q -m "first commit" +git -C "$work" push -q "$src/$name.git" main +rm -rf "$work" + +cat >"/etc/pullgate/$name.toml" </dev/null || echo missing)" + if [ "$got" != "$2" ]; then + err "$1: got '$got', want '$2'" + fi +} +spool="/var/spool/pullgate/$name" +want /var/lib/pullgate "root root 711" +want "/var/lib/pullgate/$name" "root root 711" +want "/var/lib/pullgate/$name/repo.git" "root root 700" +want "/var/lib/pullgate/$name/state.json" "root root 600" +want /var/spool/pullgate "root root 755" +want "$spool" "root root 755" +want "$spool/outbox" "root pullgate 770" +want "$spool/inbox" "pullgate pullgate 700" +want "$spool/check" "pullgate pullgate 700" +n=0 +for f in "$spool"/outbox/*.json; do + [ -e "$f" ] || continue + n=$((n + 1)) + want "$f" "root pullgate 640" +done +if [ "$n" -ne 1 ]; then + err "the outbox holds $n messages after the first run, want 1 (the question)" +fi + +# The listener, under the real unit, against a fake Telegram on 127.0.0.1. +printf '123:units-test-token-made-by-the-script\n' >/etc/pullgate/bot-token +chown root:root /etc/pullgate/bot-token +chmod 0600 /etc/pullgate/bot-token +cat >/etc/pullgate/listener.toml </dev/null || true' EXIT +# Wait until the fake Telegram takes connections. +i=0 +until python3 -I -c 'import socket, sys; socket.create_connection(("127.0.0.1", int(sys.argv[1])), 1)' "$port" 2>/dev/null; do + i=$((i + 1)) + if [ "$i" -ge 20 ]; then + echo "FAIL: the fake Telegram did not start on 127.0.0.1:$port" >&2 + exit 1 + fi + sleep 0.5 +done +if ! systemctl start pullgate-listen.service; then + err "the listener did not start under pullgate-listen.service" +fi + +# The listener sends the question, then removes the file from the outbox. +i=0 +while [ "$i" -lt 30 ]; do + if grep -qx sendMessage "$tglog" && [ -z "$(ls -A "$spool/outbox")" ]; then + break + fi + i=$((i + 1)) + sleep 1 +done +journalctl --sync || true +journalctl --no-pager -o cat -u pullgate-listen.service || true +if ! grep -qx sendMessage "$tglog"; then + err "the listener sent no message" +fi +if [ -n "$(ls -A "$spool/outbox")" ]; then + err "the listener did not remove the message from the outbox" +fi +if journalctl --no-pager -o cat -u pullgate-listen.service | grep -q -E 'level=(ERROR|WARN)'; then + err "the listener logged an error or a warning" +fi +if ! systemctl is-active --quiet pullgate-listen.service; then + err "the listener is not running" +fi +systemctl stop pullgate-listen.service + +if [ "$fail" -ne 0 ]; then + exit 1 +fi +echo "ok: the first run and the listener work under the units of contrib/systemd/"