From 6c02453d61e2deef62213d12f6a1ea88b6bcaaa5 Mon Sep 17 00:00:00 2001 From: Geno Roupsky <185580+groupsky@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:02:21 +0200 Subject: [PATCH] feat: nodkey broker config-diff lists config changes by meaning (#174) It reads two configs with the parser of the start, compares them by meaning, and marks each change that widens access or weakens a control, and each change it does not know as narrowing. --short fits one Telegram message and never drops a widening line silently. Co-Authored-By: Claude Opus 5.5 --- DESIGN.md | 42 ++ THREAT-MODEL.md | 9 + cmd/nodkey/broker.go | 7 +- cmd/nodkey/configdiff.go | 176 ++++++++ cmd/nodkey/configdiff_linux_test.go | 202 ++++++++++ docs/setup.md | 35 ++ internal/config/diff.go | 599 ++++++++++++++++++++++++++++ internal/config/diff_test.go | 301 ++++++++++++++ 8 files changed, 1370 insertions(+), 1 deletion(-) create mode 100644 cmd/nodkey/configdiff.go create mode 100644 cmd/nodkey/configdiff_linux_test.go create mode 100644 internal/config/diff.go create mode 100644 internal/config/diff_test.go diff --git a/DESIGN.md b/DESIGN.md index 24d8373..e102b7d 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -3183,6 +3183,7 @@ and `[standing]` need a restart. nodkey broker [--config FILE] nodkey broker reload [--config FILE] [--timeout DURATION] nodkey broker check [--config FILE] [--files DIR] [--credentials DIR] +nodkey broker config-diff [--short] [--max-chars N] OLD NEW nodkey run [--secret VAR=NAME]... [--grant-token FILE] [--reason TEXT] [--mask | --no-mask] -- COMMAND [ARGS...] nodkey ssh-agent [--socket PATH] nodkey ssh-grant --key NAME --user USER --host NAME... --for DURATION [--reason TEXT] @@ -3342,6 +3343,47 @@ of the output is cleaned with `claim.Line`, so text from the config cannot add a line or change the terminal. It exits 0 when the config passed, 1 with the reason when it did not, and 64 on wrong usage. +`nodkey broker config-diff OLD NEW` lists the changes from one config to +another by what they mean (issue #174), such as for the owner's +confirmation of a config change from git (#102). OLD and NEW are each a +config file, or a directory with `broker.toml` and `known_hosts` (as +`--files`). For a file, `known_hosts` is read from the directory of the +file, by its base name, never from the path in the config: two configs +that name one path would both read the live file, and a new host key +would not show. Both are read with the parser of the start +(`config.ReadBrokerFiles`); no other file is read, also no credential, +and no root or network is needed. `config.Diff` compares the two parsed +configs, so comments, order and spaces are no change, and a list of +clients or hosts is a set. Each change is one line, sorted by group +(the top keys and sections, clients, stores, secrets, SSH keys, +`known_hosts`) and name. A line that starts with `⚠` widens access or +weakens a control: a new client, secret, key, store or host; a new +client of a secret or a key; `ask` towards `never`; a longer `window`; +wider `hours`; a higher or no `max_per_day`; `notify` off (these three +are compared only between two `never` rules; a move to `never` shows +them on its line); more hosts, +or every host, for a key; `preapprove` or `standing` turned on; a new +`[standing]` or a higher limit in it or in `[preapprove]`; fewer +`warn_days`; any change of the approver, the notice chat, the channel, +the store's program, server or files, the item of a secret, the file of +a key, `listen`, `tls_key`, `audit_log`, the `known_hosts` path, the +`[standing]` summary time, or `[updates] check` turned off; a longer +timeout; a new host key. Any other change is a plain line only when the +code knows it narrows access. A field of the config that the code does +not compare by name is still compared (`unknown` in +`internal/config/diff.go`), and its change is marked as "a change that +config-diff does not know". A test fails when a field is in neither +list. Text from a config is quoted and each line cleaned with +`claim.Line`. + +`--short` fits one Telegram message: at most `--max-chars` characters, +counted in UTF-16 code units as Telegram counts them (default 3500, from 200 to 100000). It puts the lines that widen first, +then the others, and cuts with "and N more". When the lines that widen +do not all fit, it gives one line instead: how many there are, and that +the full list must be read. It exits 0 when the configs mean the same, 1 +when they differ, 2 when a config cannot be read or is not valid, and 64 +on wrong usage. + The broker stops on SIGTERM or SIGINT, also during the start, when the polling stops for good (section 7), when its server fails, and when a write to stderr fails (a log line or an audit anchor), such as after the reader diff --git a/THREAT-MODEL.md b/THREAT-MODEL.md index d02b39a..9eb0532 100644 --- a/THREAT-MODEL.md +++ b/THREAT-MODEL.md @@ -1498,6 +1498,15 @@ a check that needs a credential; the reload then keeps the old config. The check is no part of what the broker trusts: the broker checks every config again when it loads it. +`nodkey broker config-diff` (issue #174) shows the owner what a config +change means, before it is applied. Its risk is a change that widens +access but is shown as harmless. So it marks each change that it does +not know as narrowing, and a field of the config that it does not +compare by name is still compared and marked; a test fails when a field +is in neither list. The short form never drops a line that widens: when +they do not all fit, it says how many there are. It reads no credential. +It is no control of the broker: it only helps a person to decide. + ## 7. Assumptions - The broker host is patched, has no agent on it, and only the owner has diff --git a/cmd/nodkey/broker.go b/cmd/nodkey/broker.go index 67cb64e..a2723b8 100644 --- a/cmd/nodkey/broker.go +++ b/cmd/nodkey/broker.go @@ -107,6 +107,7 @@ var errPanicked = errors.New("it panicked; the panic value is not logged") const brokerUsage = `Usage: nodkey broker [--config FILE] nodkey broker reload [--config FILE] [--timeout DURATION] nodkey broker check [--config FILE] [--files DIR] [--credentials DIR] + nodkey broker config-diff [--short] [--max-chars N] OLD NEW Run the broker: the daemon that holds the SSH keys and the access to the secret stores. It asks the approver on Telegram before it hands out a @@ -120,7 +121,8 @@ of the audit log. SIGTERM or SIGINT stops it. So does a failed write to stderr, such as after its reader is gone: it stops with exit code 1. SIGHUP reloads the config without a restart; see "nodkey broker reload --help". To check a config without a start, see "nodkey broker check ---help". +--help". To list the changes between two configs by what they mean, see +"nodkey broker config-diff --help". Exit codes: 0 stopped by SIGTERM or SIGINT, 1 any other error, 64 wrong usage, 76 Telegram reports a polling conflict: the bot token may be @@ -137,6 +139,9 @@ func runBroker(args []string, stdout, stderr io.Writer) int { if len(args) > 0 && args[0] == "check" { return runBrokerCheck(args[1:], stdout, stderr) } + if len(args) > 0 && args[0] == "config-diff" { + return runConfigDiff(args[1:], stdout, stderr) + } // A broken stderr must not end the broker with SIGPIPE in the middle // of a line: the write fails instead, and the broker stops in its // order (issue #73). diff --git a/cmd/nodkey/configdiff.go b/cmd/nodkey/configdiff.go new file mode 100644 index 0000000..40cbe97 --- /dev/null +++ b/cmd/nodkey/configdiff.go @@ -0,0 +1,176 @@ +package main + +import ( + "errors" + "flag" + "fmt" + "io" + "os" + "path/filepath" + "strings" + "unicode/utf16" + + "github.com/code6x/nodkey/internal/claim" + "github.com/code6x/nodkey/internal/config" + "github.com/code6x/nodkey/internal/doctor" +) + +const configDiffUsage = `Usage: nodkey broker config-diff [--short] [--max-chars N] OLD NEW + +List the changes from the broker config OLD to NEW by what they mean: +who gets which secret or SSH key, and with or without a tap. Comments, +order and spaces are not changes. Each change that widens access or +weakens a control starts with a warning sign (⚠). So does each change +that config-diff does not know as one that narrows access. + +OLD and NEW are each a config file, with the known_hosts that it uses +next to it (by the base name of its path in the config), or a directory +with broker.toml and known_hosts (as "nodkey broker check --files DIR"). It reads no +credential, needs no root, and uses no network. + + --short print a short form that fits one Telegram message: the + changes that widen access first, then the rest, cut with + "and N more" + --max-chars N the most characters of the short form (default 3500) + +Exit codes: 0 the configs mean the same, 1 they differ, 2 a file cannot +be read or is not a valid config, 64 wrong usage. +` + +// exitDiffUnreadable is the exit code of nodkey broker config-diff when +// a config cannot be read. +const exitDiffUnreadable = 2 + +// runConfigDiff runs nodkey broker config-diff (issue #174). +func runConfigDiff(args []string, stdout, stderr io.Writer) int { + fs := flag.NewFlagSet("nodkey broker config-diff", flag.ContinueOnError) + fs.SetOutput(io.Discard) + short := fs.Bool("short", false, "the short form") + maxChars := fs.Int("max-chars", 3500, "the most characters of the short form") + if err := fs.Parse(args); err != nil { + if errors.Is(err, flag.ErrHelp) { + _, _ = io.WriteString(stdout, configDiffUsage) + return exitOK + } + return parseErrorCode(stderr, "broker config-diff", err) + } + if fs.NArg() != 2 { + return usageFail(stderr, "broker config-diff", "give OLD and NEW, each a config file or a directory, "+ + "after the options") + } + if *maxChars < minShortChars || *maxChars > 100000 { + return usageFail(stderr, "broker config-diff", fmt.Sprintf("--max-chars must be from %d to 100000", minShortChars)) + } + // ${CREDENTIALS_DIRECTORY} stands for the directory of the unit. No + // file in it is read; only the paths are compared. + if err := os.Setenv("CREDENTIALS_DIRECTORY", "/run/credentials/"+doctor.Unit); err != nil { + _, _ = fmt.Fprintf(stderr, "nodkey broker config-diff: cannot set CREDENTIALS_DIRECTORY: %s\n", err) + return exitDiffUnreadable + } + var cfgs [2]*config.Broker + for i, arg := range fs.Args() { + cfg, err := readDiffArg(arg) + if err != nil { + _, _ = fmt.Fprintf(stderr, "nodkey broker config-diff: %s: %s; %s\n", [2]string{"OLD", "NEW"}[i], + claim.Line(err.Error(), checkLineMax), claim.Line(diffArgHint(arg), checkLineMax)) + return exitDiffUnreadable + } + cfgs[i] = cfg + } + changes := config.Diff(cfgs[0], cfgs[1]) + if len(changes) == 0 { + _, _ = io.WriteString(stdout, "no change\n") + return exitOK + } + lines := make([]string, len(changes)) + widens := make([]bool, len(changes)) + for i, c := range changes { + c.Text = claim.Line(c.Text, checkLineMax) + lines[i], widens[i] = c.String(), c.Widens + } + if *short { + lines = shortForm(lines, widens, *maxChars) + } + _, _ = io.WriteString(stdout, strings.Join(lines, "\n")+"\n") + return exitFailure +} + +// readDiffArg reads a config for config-diff: a file, or a directory +// with broker.toml and known_hosts. It uses the parser of the start +// (config.ReadBrokerFiles), without the checks of the other files. +func readDiffArg(arg string) (*config.Broker, error) { + fi, err := os.Stat(arg) + if err != nil { + return nil, err // a *fs.PathError names the path + } + if fi.IsDir() { + return config.ReadBrokerFiles(filepath.Join(arg, "broker.toml"), arg) + } + // known_hosts comes from the directory of the file, not from the path + // in the config: two configs that name the same path would otherwise + // both read the live file, and a new host key would not show. + return config.ReadBrokerFiles(arg, filepath.Dir(arg)) +} + +// diffArgHint says how to check the config arg that cannot be read. +func diffArgHint(arg string) string { + fi, err := os.Stat(arg) + switch { + case err != nil: + return "give a config file, or a directory with broker.toml and known_hosts, that exists" + case fi.IsDir(): + return fmt.Sprintf("check it with nodkey broker check --files %q (docs/setup.md section 7)", arg) + } + return fmt.Sprintf("check it with nodkey broker check --config %q --files %q (docs/setup.md section 7)", + arg, filepath.Dir(arg)) +} + +// minShortChars is the smallest --max-chars: room for the line that says +// how many changes widen access. +const minShortChars = 200 + +// shortForm cuts lines to at most maxChars characters, with the lines +// that widen access first. When it cuts, the last line says how many +// lines are left out. When the lines that widen do not all fit, it gives +// none of them: one line says how many there are, and that the full list +// must be read. +func shortForm(lines []string, widens []bool, maxChars int) []string { + var wide, rest []string + for i, l := range lines { + if widens[i] { + wide = append(wide, l) + } else { + rest = append(rest, l) + } + } + all := append(append([]string(nil), wide...), rest...) + if size(all) <= maxChars { + return all + } + more := func(n int) string { + return fmt.Sprintf("and %d more; read the full list with nodkey broker config-diff OLD NEW", n) + } + if size(append(append([]string(nil), wide...), more(len(rest)))) > maxChars { + return []string{fmt.Sprintf("⚠ %d changes widen access, and they do not fit here; read the full "+ + "list with nodkey broker config-diff OLD NEW (%d changes in all)", len(wide), len(lines))} + } + out := append([]string(nil), wide...) + for i, l := range rest { + next := append(append([]string(nil), out...), l, more(len(rest)-i-1)) + if size(next) > maxChars { + return append(out, more(len(rest)-i)) + } + out = append(out, l) + } + return out // not reached: all lines did not fit +} + +// size is the number of characters of lines joined with line breaks, as +// Telegram counts them: in UTF-16 code units. +func size(lines []string) int { + n := 0 + for _, l := range lines { + n += len(utf16.Encode([]rune(l))) + 1 + } + return max(n-1, 0) +} diff --git a/cmd/nodkey/configdiff_linux_test.go b/cmd/nodkey/configdiff_linux_test.go new file mode 100644 index 0000000..0aba649 --- /dev/null +++ b/cmd/nodkey/configdiff_linux_test.go @@ -0,0 +1,202 @@ +package main + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "slices" + "strings" + "testing" + "unicode/utf8" + + "golang.org/x/crypto/ssh" +) + +// The tests of nodkey broker config-diff run it in this process. It sets +// CREDENTIALS_DIRECTORY, so they are not parallel (see brokercheck). + +func runDiff(t *testing.T, args ...string) (code int, stdout, stderr string) { + t.Helper() + var out, errOut bytes.Buffer + code = runBroker(append([]string{"config-diff"}, args...), &out, &errOut) + return code, out.String(), errOut.String() +} + +// diffDirs makes a directory "new" next to the config of e, with a copy +// of broker.toml and known_hosts, and returns the old and the new one. +func diffDirs(t *testing.T, e *brokerEnv) (oldDir, newDir string) { + t.Helper() + oldDir = filepath.Dir(e.cfgPath) + newDir = filepath.Join(e.dir, "new") + if err := os.Mkdir(newDir, 0o700); err != nil { + t.Fatal(err) + } + for _, name := range []string{"broker.toml", "known_hosts"} { + data, err := os.ReadFile(filepath.Join(oldDir, name)) //nolint:gosec // G304: the test's own file. + if err != nil { + t.Fatal(err) + } + writeTestFile(t, filepath.Join(newDir, name), data, 0o600) + } + return oldDir, newDir +} + +// TestConfigDiff: the exit codes and the lines of the full form, for +// directories and files. It runs as a user that is not root, without the +// credentials of the config. +func TestConfigDiff(t *testing.T) { + t.Setenv("CREDENTIALS_DIRECTORY", "") + if os.Getuid() == 0 { + t.Skip("runs as root; this test is for a user that is not root") + } + e := newBrokerEnv(t, brokerOptions{}) + oldDir, newDir := diffDirs(t, e) + cfg := filepath.Join(newDir, "broker.toml") + + // Only a comment and spaces: no change. + data, err := os.ReadFile(cfg) //nolint:gosec // G304: the test's own file. + if err != nil { + t.Fatal(err) + } + data0 := slices.Clone(data) + writeTestFile(t, cfg, append([]byte("# a comment\n\n"), bytes.ReplaceAll(data, []byte(" = "), []byte(" = "))...), 0o600) + code, out, errOut := runDiff(t, oldDir, newDir) + if code != exitOK || out != "no change\n" || errOut != "" { + t.Fatalf("no change: exit %d, stdout %q, stderr %q", code, out, errOut) + } + + // A change that widens and one that narrows. + data = bytes.Replace(data, []byte("ask = \"always\"\nhosts = [\"web-1\"]"), []byte("ask = \"always\"\nhosts = []"), 1) + data = bytes.Replace(data, []byte("[secret.db-password]"), []byte("[secret.gone]"), 1) + data = bytes.Replace(data, []byte("ask = \"never\"\nnotify = false"), []byte("ask = \"always\""), 1) + writeTestFile(t, cfg, data, 0o600) + code, out, errOut = runDiff(t, oldDir, newDir) + if code != exitFailure || errOut != "" { + t.Fatalf("a change: exit %d, stdout %q, stderr %q", code, out, errOut) + } + for _, want := range []string{ + "⚠ SSH key \"deploy\": may now log in to every host in known_hosts\n", + " secret \"db-password\": removed\n", + "⚠ secret \"gone\": new secret in store \"vault\", ask = \"always\"\n", + } { + if !strings.Contains(out, want) { + t.Errorf("stdout %q has no %q", out, want) + } + } + // A file for OLD and a directory for NEW: the same. + if code, out2, _ := runDiff(t, e.cfgPath, newDir); code != exitFailure || out2 != out { + t.Errorf("file and directory: exit %d, stdout %q, want %q", code, out2, out) + } + + // Two files: each reads the known_hosts next to it, not the one at + // the path in the config. A new host key in NEW's known_hosts shows. + newKH := filepath.Join(newDir, "known_hosts") + saved, err := os.ReadFile(newKH) //nolint:gosec // G304: the test's own file. + if err != nil { + t.Fatal(err) + } + writeTestFile(t, cfg, data0, 0o600) + if code, out, _ := runDiff(t, e.cfgPath, cfg); code != exitOK || out != "no change\n" { + t.Fatalf("two files, the same: exit %d, stdout %q", code, out) + } + host := newHostSigner(t) + writeTestFile(t, newKH, append(slices.Clone(saved), []byte("db-9 "+string(ssh.MarshalAuthorizedKey(host.PublicKey())))...), 0o600) + if code, out, _ := runDiff(t, e.cfgPath, cfg); code != exitFailure || !strings.Contains(out, "⚠ known_hosts: new host \"db-9\"") { + t.Fatalf("two files, a new host next to NEW: exit %d, stdout %q", code, out) + } + writeTestFile(t, newKH, saved, 0o600) + writeTestFile(t, cfg, data, 0o600) + + // A config that cannot be read, or is not valid: exit 2. + for _, args := range [][]string{ + {filepath.Join(e.dir, "missing"), newDir}, + {oldDir, filepath.Join(newDir, "missing.toml")}, + } { + if code, out, errOut := runDiff(t, args...); code != exitDiffUnreadable || out != "" || + strings.Count(errOut, "\n") != 1 || !strings.Contains(errOut, "that exists") { + t.Errorf("config-diff %q: exit %d, stdout %q, stderr %q", args, code, out, errOut) + } + } + writeTestFile(t, cfg, append(data, "\n[client.bad]\npin = \"not-a-pin\"\n"...), 0o600) + if code, _, errOut := runDiff(t, oldDir, newDir); code != exitDiffUnreadable || !strings.Contains(errOut, "NEW: config") || + !strings.Contains(errOut, "[client.bad] pin") || !strings.Contains(errOut, "nodkey broker check --files") { + t.Errorf("an invalid NEW: exit %d, stderr %q", code, errOut) + } + // known_hosts of NEW comes from its directory. + writeTestFile(t, cfg, data, 0o600) + if err := os.Remove(filepath.Join(newDir, "known_hosts")); err != nil { + t.Fatal(err) + } + if code, _, errOut := runDiff(t, oldDir, newDir); code != exitDiffUnreadable || !strings.Contains(errOut, filepath.Join(newDir, "known_hosts")) { + t.Errorf("NEW without known_hosts: exit %d, stderr %q", code, errOut) + } +} + +// TestConfigDiffUsage: wrong usage exits 64 with one line. +func TestConfigDiffUsage(t *testing.T) { + t.Setenv("CREDENTIALS_DIRECTORY", "") + for _, args := range [][]string{{}, {"a"}, {"a", "b", "c"}, {"--nope", "a", "b"}, {"--max-chars", "10", "a", "b"}} { + code, out, errOut := runDiff(t, args...) + if code != exitUsage || out != "" || strings.Count(errOut, "\n") != 1 || + !strings.HasPrefix(errOut, "nodkey broker config-diff: ") { + t.Errorf("config-diff %q: exit %d, stdout %q, stderr %q", args, code, out, errOut) + } + } + if code, out, _ := runDiff(t, "--help"); code != exitOK || !strings.HasPrefix(out, "Usage: nodkey broker config-diff") { + t.Errorf("--help: exit %d, stdout %q", code, out) + } +} + +// TestConfigDiffShort: the short form keeps every line that widens when +// it cuts, or says that they do not fit, and is never longer than +// --max-chars. +func TestConfigDiffShort(t *testing.T) { + lines := func(n int, wide bool) ([]string, []bool) { + var l []string + var w []bool + for i := range n { + if wide { + l = append(l, fmt.Sprintf("⚠ change %d that widens access, with some more words to fill the line", i)) + } else { + l = append(l, fmt.Sprintf(" change %d that narrows access, with some more words to fill the line", i)) + } + w = append(w, wide) + } + return l, w + } + narrow, nw := lines(40, false) + wide, ww := lines(5, true) + // The narrowing lines come first in the input; the short form puts + // the widening lines first. + all, allW := append(narrow, wide...), append(nw, ww...) + + got := shortForm(all, allW, 100000) + if len(got) != len(all) || got[0] != wide[0] { + t.Fatalf("everything fits: %d lines, first %q", len(got), got[0]) + } + got = shortForm(all, allW, 1000) + if size(got) > 1000 { + t.Errorf("size %d > 1000", size(got)) + } + for i, w := range wide { + if got[i] != w { + t.Fatalf("line %d is %q, want the widening line %q", i, got[i], w) + } + } + last := got[len(got)-1] + if n := len(all) - (len(got) - 1); !strings.HasPrefix(last, fmt.Sprintf("and %d more;", n)) { + t.Errorf("last line %q, want \"and %d more\"", last, n) + } + + many, mw := lines(60, true) + got = shortForm(append(many, narrow...), append(mw, nw...), 1000) + if len(got) != 1 || !strings.HasPrefix(got[0], "⚠ 60 changes widen access, and they do not fit here") || + utf8.RuneCountInString(got[0]) > 1000 { + t.Errorf("widening lines that do not fit: %q", got) + } + // At the smallest --max-chars, the line still fits. + if got = shortForm(append(many, narrow...), append(mw, nw...), minShortChars); size(got) > minShortChars { + t.Errorf("size %d > %d: %q", size(got), minShortChars, got) + } +} diff --git a/docs/setup.md b/docs/setup.md index 811ac02..bea76c7 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -1322,6 +1322,41 @@ nodkey broker check --files /etc/nodkey/new --credentials /etc/nodkey/credential A config that passes the check can still fail at a start or a reload, on a check that needs a credential, such as a key file that does not parse. +### See what a config change means + +`nodkey broker config-diff` lists the changes from one config to another +by what they mean: who gets which secret or key, and with or without a +tap. Comments, order and spaces are not changes. Give it two +directories, each with `broker.toml` and `known_hosts`. You can also give +a config file: then it reads the `known_hosts` next to that file. + +```sh +nodkey broker config-diff /etc/nodkey ./broker-config +``` + +It prints one line per change. A line that starts with `⚠` widens access +or weakens a control, such as a new client, a "never ask" rule, a longer +`window`, or a new host key. A change that the command does not know as +narrowing gets the sign too. Read every such line before you apply the +change. An example: + +```text +⚠ client "build-1": new client +⚠ secret "backup-token": new rule: never ask (ask changed from "always" to "never") + SSH key "deploy": host "web-2" removed +``` + +`--short` gives a short form for one Telegram message, at most +`--max-chars` characters (3500 by default). It puts the lines with `⚠` +first. If they do not all fit, it says how many there are, and you must +read the full list. + +It exits with 0 when the two configs mean the same, 1 when they differ, +and 2 when a config cannot be read or is not valid. It reads no +credential, needs no root, and uses no network. Like `nodkey broker +check --files`, it needs the files to be owned by the user that runs it, +or by root. + ## 8. The systemd unit Copy the example unit diff --git a/internal/config/diff.go b/internal/config/diff.go new file mode 100644 index 0000000..eccc1a8 --- /dev/null +++ b/internal/config/diff.go @@ -0,0 +1,599 @@ +package config + +import ( + "fmt" + "maps" + "reflect" + "slices" + "strconv" + "time" + + "golang.org/x/crypto/ssh" +) + +// The diff of two broker configs by meaning (nodkey broker config-diff, +// issue #174, DESIGN.md section 15). It compares two configs that +// LoadBroker's parser read, not their text: comments, order and spaces do +// not count. A change that widens access or weakens a control is marked. +// So is every change that this code does not know as narrowing: a new +// part of the config is marked until someone decides otherwise here. + +// Change is one change from an old config to a new one. +type Change struct { + // Group sorts the changes: "broker", "client", "store", "secret", + // "key" or "known_hosts". + Group string + // Name is the name of the section in the group, or "" for the top. + Name string + // Text says what changed, in plain words. Text from the config in it + // is quoted with strconv.Quote. + Text string + // Widens is true when the change widens access or weakens a control, + // or is of a kind that Diff does not know as narrowing. + Widens bool +} + +// String is the line of the change: "⚠ " before a change that widens, +// two spaces before one that narrows. +func (c Change) String() string { + if c.Widens { + return "⚠ " + c.Text + } + return " " + c.Text +} + +// groups is the order of the groups in the output. +var groups = []string{"broker", "client", "store", "secret", "key", "known_hosts"} + +// Diff returns the changes from old to next, by group and then by name, +// in a fixed order. No change means the two configs mean the same. +func Diff(old, next *Broker) []Change { + d := &differ{} + d.top(old, next) + d.clients(old, next) + d.stores(old, next) + d.secrets(old, next) + d.keys(old, next) + d.knownHosts(old.KnownHosts, next.KnownHosts) + d.unknown(old, next) + order := func(g string) int { return slices.Index(groups, g) } + slices.SortStableFunc(d.out, func(a, b Change) int { + if x, y := order(a.Group), order(b.Group); x != y { + return x - y + } + switch { + case a.Name < b.Name: + return -1 + case a.Name > b.Name: + return 1 + } + return 0 + }) + return d.out +} + +type differ struct{ out []Change } + +func (d *differ) add(group, name string, widens bool, format string, args ...any) { + d.out = append(d.out, Change{Group: group, Name: name, Text: fmt.Sprintf(format, args...), Widens: widens}) +} + +func q(s string) string { return strconv.Quote(s) } + +func dur(t time.Duration) string { return formatDuration(t) } + +// top compares the top keys and the sections that are not named: +// [telegram], [ntfy], [updates], [preapprove] and [standing]. +func (d *differ) top(old, next *Broker) { + if old.Listen != next.Listen { + d.add("broker", "", true, "listen: changed from %s to %s", q(old.Listen.String()), q(next.Listen.String())) + } + for _, p := range []struct{ key, old, new string }{ + {"tls_key", old.TLSKey, next.TLSKey}, + {"audit_log", old.AuditLog, next.AuditLog}, + {"known_hosts", old.KnownHostsFile, next.KnownHostsFile}, + } { + if p.old != p.new { + d.add("broker", "", true, "%s: changed from %s to %s", p.key, q(p.old), q(p.new)) + } + } + d.channel(old, next) + if old.Updates.Check != next.Updates.Check { + // The check for a newer release tells the owner about security + // fixes: off weakens a control. + d.add("broker", "", !next.Updates.Check, "[updates] check: changed from %t to %t", + old.Updates.Check, next.Updates.Check) + } + o, n := old.Preapprove, next.Preapprove + d.durLimit("[preapprove] max_ahead", o.MaxAhead, n.MaxAhead) + d.durLimit("[preapprove] max_open", o.MaxOpen, n.MaxOpen) + d.intLimit("[preapprove] max_uses", o.MaxUses, n.MaxUses) + d.intLimit("[preapprove] max_grants", o.MaxGrants, n.MaxGrants) + os, ns := old.Standing, next.Standing + switch { + case !os.Enabled && ns.Enabled: + d.add("broker", "", true, "[standing]: new: standing grants are allowed") + case os.Enabled && !ns.Enabled: + d.add("broker", "", false, "[standing]: removed: no standing grants") + case os.Enabled: + d.intLimit("[standing] max_days", os.MaxDays, ns.MaxDays) + d.durLimit("[standing] max_window", os.MaxWindow, ns.MaxWindow) + d.intLimit("[standing] max_uses", os.MaxUses, ns.MaxUses) + d.intLimit("[standing] max_grants", os.MaxGrants, ns.MaxGrants) + if os.WarnDays != ns.WarnDays { + // Fewer days of warning weaken a control. + d.add("broker", "", ns.WarnDays < os.WarnDays, "[standing] warn_days: changed from %d to %d", + os.WarnDays, ns.WarnDays) + } + if os.SummaryDay != ns.SummaryDay || os.SummaryMinute != ns.SummaryMinute { + d.add("broker", "", true, "[standing] summary: changed from %s to %s", + summaryText(os.SummaryDay, os.SummaryMinute), summaryText(ns.SummaryDay, ns.SummaryMinute)) + } + } +} + +func summaryText(day time.Weekday, minute int) string { + return fmt.Sprintf("%q", fmt.Sprintf("%.3s %02d:%02d", day.String(), minute/60, minute%60)) +} + +// durLimit and intLimit add a change of a limit: a higher limit widens. +func (d *differ) durLimit(key string, old, next time.Duration) { + if old != next { + d.add("broker", "", next > old, "%s: changed from %s to %s", key, dur(old), dur(next)) + } +} + +func (d *differ) intLimit(key string, old, next int) { + if old != next { + d.add("broker", "", next > old, "%s: changed from %d to %d", key, old, next) + } +} + +// channel compares the approval channel. Every change of the approver or +// of where the notices go widens, but a shorter timeout. +func (d *differ) channel(old, next *Broker) { + switch { + case old.Ntfy == nil && next.Ntfy != nil: + d.add("broker", "", true, "the approval channel: changed from [telegram] to [ntfy] (server %s)", q(next.Ntfy.Server)) + return + case old.Ntfy != nil && next.Ntfy == nil: + d.add("broker", "", true, "the approval channel: changed from [ntfy] to [telegram] (user_id %d)", next.Telegram.UserID) + return + } + timeouts := func(sec string, ot, os, nt, ns time.Duration) { + if ot != nt { + d.add("broker", "", nt > ot, "%s timeout: changed from %s to %s", sec, dur(ot), dur(nt)) + } + if os != ns { + d.add("broker", "", ns > os, "%s ssh_timeout: changed from %s to %s", sec, dur(os), dur(ns)) + } + } + if old.Ntfy != nil { + o, n := *old.Ntfy, *next.Ntfy + for _, p := range []struct{ key, old, new string }{ + {"server", o.Server, n.Server}, + {"request_topic", o.RequestTopic, n.RequestTopic}, + {"answer_topic", o.AnswerTopic, n.AnswerTopic}, + {"token_file", o.TokenFile, n.TokenFile}, + } { + if p.old != p.new { + d.add("broker", "", true, "[ntfy] %s: changed from %s to %s", p.key, q(p.old), q(p.new)) + } + } + timeouts("[ntfy]", o.Timeout, o.SSHTimeout, n.Timeout, n.SSHTimeout) + return + } + o, n := old.Telegram, next.Telegram + if o.TokenFile != n.TokenFile { + d.add("broker", "", true, "[telegram] token_file: changed from %s to %s", q(o.TokenFile), q(n.TokenFile)) + } + if o.UserID != n.UserID { + d.add("broker", "", true, "[telegram] user_id: the approver changed from %d to %d", o.UserID, n.UserID) + } + if o.NoticeChatID != n.NoticeChatID { + d.add("broker", "", true, "[telegram] notice_chat_id: the notices go to %d, not %d", n.NoticeChatID, o.NoticeChatID) + } + timeouts("[telegram]", o.Timeout, o.SSHTimeout, n.Timeout, n.SSHTimeout) +} + +func (d *differ) clients(old, next *Broker) { + for _, name := range sortedUnion(old.Clients, next.Clients) { + o, inOld := old.Clients[name] + n, inNew := next.Clients[name] + sec := q(sectionName("client", name)) + switch { + case !inOld: + d.add("client", name, true, "client %s: new client", q(name)) + case !inNew: + d.add("client", name, false, "client %s: removed", q(name)) + case o.Pin != n.Pin: + d.add("client", name, true, "client %s: new pin %s (in %s)", q(name), n.Pin, sec) + } + } +} + +func (d *differ) stores(old, next *Broker) { + for _, name := range sortedUnion(old.Stores, next.Stores) { + o, inOld := old.Stores[name] + n, inNew := next.Stores[name] + switch { + case !inOld: + d.add("store", name, true, "store %s: new store of type %s", q(name), q(string(n.Type))) + continue + case !inNew: + d.add("store", name, false, "store %s: removed", q(name)) + continue + } + for _, p := range []struct{ key, old, new string }{ + {"type", string(o.Type), string(n.Type)}, + {"program", o.Program, n.Program}, + {"token_file", o.TokenFile, n.TokenFile}, + {"password_file", o.PasswordFile, n.PasswordFile}, + {"vault", o.Vault, n.Vault}, + {"server", o.Server, n.Server}, + {"client_id", o.ClientID, n.ClientID}, + {"client_secret_file", o.ClientSecretFile, n.ClientSecretFile}, + } { + if p.old != p.new { + d.add("store", name, true, "store %s: %s changed from %s to %s", q(name), p.key, q(p.old), q(p.new)) + } + } + } +} + +func (d *differ) secrets(old, next *Broker) { + for _, name := range sortedUnion(old.Secrets, next.Secrets) { + o, inOld := old.Secrets[name] + n, inNew := next.Secrets[name] + what := "secret " + q(name) + switch { + case !inOld: + d.add("secret", name, true, "%s: new secret in store %s, %s", what, q(n.Store), ruleText(n.Rule)) + for _, c := range n.Clients { + d.add("secret", name, true, "%s: client %s may now ask for it", what, q(c)) + } + continue + case !inNew: + d.add("secret", name, false, "%s: removed", what) + continue + } + for _, p := range []struct{ key, old, new string }{ + {"store", o.Store, n.Store}, + {"id", o.ID, n.ID}, + {"field", o.Field, n.Field}, + {"project", o.Project, n.Project}, + {"environment", o.Environment, n.Environment}, + {"path", o.Path, n.Path}, + {"name", o.SecretName, n.SecretName}, + } { + if p.old != p.new { + d.add("secret", name, true, "%s: now another item: %s changed from %s to %s", what, p.key, q(p.old), q(p.new)) + } + } + d.clientList("secret", name, what, "ask for it", o.Clients, n.Clients) + d.rule("secret", name, what, o.Rule, n.Rule) + } +} + +func (d *differ) keys(old, next *Broker) { + for _, name := range sortedUnion(old.Keys, next.Keys) { + o, inOld := old.Keys[name] + n, inNew := next.Keys[name] + what := "SSH key " + q(name) + switch { + case !inOld: + d.add("key", name, true, "%s: new key, %s, %s", what, hostsText(n.Hosts), ruleText(n.Rule)) + for _, c := range n.Clients { + d.add("key", name, true, "%s: client %s may now use it", what, q(c)) + } + continue + case !inNew: + d.add("key", name, false, "%s: removed", what) + continue + } + if o.File != n.File { + d.add("key", name, true, "%s: file changed from %s to %s", what, q(o.File), q(n.File)) + } + d.clientList("key", name, what, "use it", o.Clients, n.Clients) + switch { + case len(o.Hosts) > 0 && len(n.Hosts) == 0: + d.add("key", name, true, "%s: may now log in to every host in known_hosts", what) + case len(o.Hosts) == 0 && len(n.Hosts) > 0: + d.add("key", name, false, "%s: may now log in only to %s", what, hostsText(n.Hosts)) + default: + for _, h := range n.Hosts { + if !slices.Contains(o.Hosts, h) { + d.add("key", name, true, "%s: new host %s", what, q(h)) + } + } + for _, h := range o.Hosts { + if !slices.Contains(n.Hosts, h) { + d.add("key", name, false, "%s: host %s removed", what, q(h)) + } + } + } + d.rule("key", name, what, o.Rule, n.Rule) + } +} + +func hostsText(hosts []string) string { + if len(hosts) == 0 { + return "every host in known_hosts" + } + s := "hosts" + for _, h := range hosts { + s += " " + q(h) + } + return s +} + +// clientList compares the clients of a secret or a key. verb is "ask +// for it" or "use it". +func (d *differ) clientList(group, name, what, verb string, old, next []string) { + for _, c := range next { + if !slices.Contains(old, c) { + d.add(group, name, true, "%s: client %s may now %s", what, q(c), verb) + } + } + for _, c := range old { + if !slices.Contains(next, c) { + d.add(group, name, false, "%s: client %s may no longer %s", what, q(c), verb) + } + } +} + +// askLevel orders the ask values from the strictest: always, window, +// never. An ask that is none of them is the most open, so a change to it +// widens. +func askLevel(a Ask) int { + switch a { + case AskAlways: + return 0 + case AskWindow: + return 1 + case AskNever: + return 2 + } + return 3 +} + +func ruleText(r Rule) string { + s := "ask = " + q(string(r.Ask)) + if r.Ask == AskWindow { + s += ", window = " + q(dur(r.Window)) + } + if r.Preapprove { + s += ", preapprove = true" + } + if r.Standing { + s += ", standing = true" + } + return s +} + +// rule compares the rules of a secret or a key. +func (d *differ) rule(group, name, what string, o, n Rule) { + if o.Ask != n.Ask { + widens := askLevel(n.Ask) > askLevel(o.Ask) + text := "ask changed from %s to %s" + if n.Ask == AskNever { + text = "new rule: never ask (ask changed from %s to %s)" + } + extra := "" + if n.Ask == AskNever { + extra = neverText(n) + } + d.add(group, name, widens, "%s: "+text+"%s", what, q(string(o.Ask)), q(string(n.Ask)), extra) + } + if o.Window != n.Window && (o.Ask == AskWindow || n.Ask == AskWindow) { + d.add(group, name, n.Window > o.Window, "%s: window changed from %s to %s", what, dur(o.Window), dur(n.Window)) + } + if o.Preapprove != n.Preapprove { + d.add(group, name, n.Preapprove, "%s: preapprove changed from %t to %t", what, o.Preapprove, n.Preapprove) + } + if o.Standing != n.Standing { + d.add(group, name, n.Standing, "%s: standing changed from %t to %t", what, o.Standing, n.Standing) + } + // notify, max_per_day and hours apply only to a "never" rule. When + // only one side is "never", the ask line above says it. + if o.Ask != AskNever || n.Ask != AskNever { + return + } + if o.Notify != n.Notify { + d.add(group, name, !n.Notify, "%s: notify changed from %t to %t", what, o.Notify, n.Notify) + } + if o.MaxPerDay != n.MaxPerDay { + // 0 is no limit. + widens := n.MaxPerDay == 0 || (o.MaxPerDay != 0 && n.MaxPerDay > o.MaxPerDay) + d.add(group, name, widens, "%s: max_per_day changed from %s to %s", what, perDay(o.MaxPerDay), perDay(n.MaxPerDay)) + } + if !reflect.DeepEqual(o.Hours, n.Hours) { + d.add(group, name, wider(o.Hours, n.Hours), "%s: hours changed from %s to %s", what, hoursText(o.Hours), hoursText(n.Hours)) + } +} + +// neverText is the part of a "never" rule after the ask line. +func neverText(r Rule) string { + return fmt.Sprintf(", with notify %t, max_per_day %s, hours %s", r.Notify, perDay(r.MaxPerDay), hoursText(r.Hours)) +} + +func perDay(n int) string { + if n == 0 { + return "no limit" + } + return strconv.Itoa(n) +} + +func hoursText(h *Hours) string { + if h == nil { + return "any time" + } + clock := func(t time.Duration) string { + return fmt.Sprintf("%02d:%02d", int(t/time.Hour), int(t%time.Hour/time.Minute)) + } + return q(clock(h.From) + "-" + clock(h.To)) +} + +// wider reports whether the hours next allow a minute that old does not. +// nil is any time. +func wider(old, next *Hours) bool { + if old == nil { + return false + } + if next == nil { + return true + } + day := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) + for m := range 24 * 60 { + t := day.Add(time.Duration(m) * time.Minute) + if next.Contains(t) && !old.Contains(t) { + return true + } + } + return false +} + +func (d *differ) knownHosts(old, next *KnownHosts) { + names := func(k *KnownHosts) map[string][]ssh.PublicKey { + if k == nil { + return nil + } + return k.byName + } + o, n := names(old), names(next) + for _, host := range sortedUnion(o, n) { + ok := keySet(o[host]) + nk := keySet(n[host]) + switch { + case len(ok) == 0: + d.add("known_hosts", host, true, "known_hosts: new host %s", q(host)) + continue + case len(nk) == 0: + d.add("known_hosts", host, false, "known_hosts: host %s removed", q(host)) + continue + } + for _, k := range slices.Sorted(maps.Keys(nk)) { + if !ok[k] { + d.add("known_hosts", host, true, "known_hosts: host %s has a new host key %s", q(host), k) + } + } + for _, k := range slices.Sorted(maps.Keys(ok)) { + if !nk[k] { + d.add("known_hosts", host, false, "known_hosts: host %s: host key %s removed", q(host), k) + } + } + } +} + +// keySet returns the SHA-256 fingerprints of keys. +func keySet(keys []ssh.PublicKey) map[string]bool { + m := make(map[string]bool, len(keys)) + for _, k := range keys { + m[ssh.FingerprintSHA256(k)] = true + } + return m +} + +func sortedUnion[V any](a, b map[string]V) []string { + names := slices.Collect(maps.Keys(a)) + for name := range b { + if _, ok := a[name]; !ok { + names = append(names, name) + } + } + slices.Sort(names) + return names +} + +// unknown marks a change that the code above does not compare. In copies +// of the two configs, it zeros every field that the code above compares +// (compared), keeps only the names that both configs have, and compares +// the rest. So a field that is added to one of these types later is +// marked as a change that widens, until it is added above and to +// compared. +func (d *differ) unknown(old, next *Broker) { + x, y := *old, *next + x.Clients, y.Clients = common(old.Clients, next.Clients) + x.Stores, y.Stores = common(old.Stores, next.Stores) + x.Secrets, y.Secrets = common(old.Secrets, next.Secrets) + x.Keys, y.Keys = common(old.Keys, next.Keys) + if (x.Ntfy == nil) != (y.Ntfy == nil) { + x.Ntfy, y.Ntfy = nil, nil // a change of the channel is compared above + } + zeroCompared(reflect.ValueOf(&x).Elem()) + zeroCompared(reflect.ValueOf(&y).Elem()) + if !reflect.DeepEqual(x, y) { + d.add("broker", "", true, "a change that config-diff does not know; read the two configs") + } +} + +// compared names the fields of each type that Diff compares, or that are +// not part of the meaning (Path, Sum, Name). A field of these types that +// is not here is compared by unknown. +var compared = map[reflect.Type][]string{ + reflect.TypeFor[Broker](): {"Path", "Sum", "Listen", "TLSKey", "AuditLog", "KnownHostsFile", "KnownHosts"}, + reflect.TypeFor[Telegram](): {"TokenFile", "UserID", "NoticeChatID", "Timeout", "SSHTimeout"}, + reflect.TypeFor[Ntfy](): {"Server", "RequestTopic", "AnswerTopic", "TokenFile", "Timeout", "SSHTimeout"}, + reflect.TypeFor[Updates](): {"Check"}, + reflect.TypeFor[PreapproveLimits](): {"MaxAhead", "MaxOpen", "MaxUses", "MaxGrants"}, + reflect.TypeFor[StandingLimits](): {"Enabled", "MaxDays", "MaxWindow", "MaxUses", "MaxGrants", "WarnDays", + "SummaryDay", "SummaryMinute"}, + reflect.TypeFor[Client](): {"Name", "Pin"}, + reflect.TypeFor[Store](): {"Name", "Type", "Program", "TokenFile", "PasswordFile", "Vault", "Server", "ClientID", + "ClientSecretFile"}, + reflect.TypeFor[Secret](): {"Name", "Store", "ID", "Field", "Project", "Environment", "Path", "SecretName", + "Clients"}, + reflect.TypeFor[Key](): {"Name", "File", "Clients", "Hosts"}, + reflect.TypeFor[Rule](): {"Ask", "Window", "Preapprove", "Standing", "Notify", "MaxPerDay", "Hours"}, +} + +// zeroCompared zeros the fields in compared, in v and in each struct, +// pointer and map below it. v must be settable. Pointers and maps are +// copied first, so the configs themselves do not change. +func zeroCompared(v reflect.Value) { + switch v.Kind() { + case reflect.Struct: + names := compared[v.Type()] + for i := range v.NumField() { + f := v.Field(i) + if !f.CanSet() { + continue // an unexported field: DeepEqual compares it + } + if slices.Contains(names, v.Type().Field(i).Name) { + f.SetZero() + } else { + zeroCompared(f) + } + } + case reflect.Pointer: + if !v.IsNil() { + c := reflect.New(v.Type().Elem()) + c.Elem().Set(v.Elem()) + zeroCompared(c.Elem()) + v.Set(c) + } + case reflect.Map: + if !v.IsNil() { + m := reflect.MakeMapWithSize(v.Type(), v.Len()) + for it := v.MapRange(); it.Next(); { + c := reflect.New(v.Type().Elem()).Elem() + c.Set(it.Value()) + zeroCompared(c) + m.SetMapIndex(it.Key(), c) + } + v.Set(m) + } + } +} + +// common returns copies of a and b with only the names that both have. +// A name that only one has is a new or a removed section, compared above. +func common[V any](a, b map[string]V) (map[string]V, map[string]V) { + x, y := make(map[string]V), make(map[string]V) + for name, v := range a { + if w, ok := b[name]; ok { + x[name], y[name] = v, w + } + } + return x, y +} diff --git a/internal/config/diff_test.go b/internal/config/diff_test.go new file mode 100644 index 0000000..4d352a3 --- /dev/null +++ b/internal/config/diff_test.go @@ -0,0 +1,301 @@ +package config + +import ( + "reflect" + "slices" + "strings" + "testing" + "time" +) + +// diffCase is a change of the valid config and of known_hosts. +type diffCase struct { + name string + old []edit // edits of the old config + new []edit // edits of the new config + hosts func(e *brokerEnv) []string // the lines of the new known_hosts, if set + files func(t *testing.T, e *brokerEnv) // run before the new config is loaded, if set + want string // in a line of the diff +} + +func (c diffCase) run(t *testing.T) []Change { + t.Helper() + e := newBrokerEnv(t) + old, err := loadBroker(e.write(t, c.old...), "") + if err != nil { + t.Fatalf("the old config: %v", err) + } + if c.hosts != nil { + writeFile(t, e.dir, "known_hosts", strings.Join(c.hosts(e), "\n")+"\n", 0o644) + } + if c.files != nil { + c.files(t, e) + } + next, err := loadBroker(e.write(t, append(slices.Clone(c.old), c.new...)...), "") + if err != nil { + t.Fatalf("the new config: %v", err) + } + return Diff(old, next) +} + +func (e *brokerEnv) hostLines(names ...string) []string { + var lines []string + for _, name := range names { + lines = append(lines, knownHostsLine(name, e.hosts[name])) + } + return lines +} + +// findChange returns the change whose line has want. +func findChange(changes []Change, want string) (Change, bool) { + for _, c := range changes { + if strings.Contains(c.Text, want) { + return c, true + } + } + return Change{}, false +} + +const standingTOML = "\n[standing]\n" + +// TestDiffMarksWidening: each kind of change that widens access or +// weakens a control (issue #174) is marked. +func TestDiffMarksWidening(t *testing.T) { + newClient := "[client.new-box]\npin = \"" + testPin("new-box").String() + "\"\n\n[client.backup-box]" + for _, c := range []diffCase{ + {name: "a new client", new: []edit{{"[client.backup-box]", newClient}}, want: `client "new-box": new client`}, + {name: "a secret for another client", + new: []edit{{"clients = [\"laptop\"]\nask = \"window\"\nwindow = \"15m\"\n\n[secret.db", "clients = [\"laptop\", \"backup-box\"]\nask = \"window\"\nwindow = \"15m\"\n\n[secret.db"}}, + want: `secret "github-token": client "backup-box" may now ask for it`}, + {name: "a new secret", new: []edit{{"[key.deploy]", + "[secret.new]\nstore = \"main\"\nid = \"00000000-0000-0000-0000-00000000000c\"\nclients = [\"laptop\"]\nask = \"always\"\n\n[key.deploy]"}}, + want: `secret "new": client "laptop" may now ask for it`}, + {name: "a key for another client", + new: []edit{{"clients = [\"laptop\"]\nask = \"window\"\nwindow = \"15m\"\nhosts", "clients = [\"laptop\", \"backup-box\"]\nask = \"window\"\nwindow = \"15m\"\nhosts"}}, + want: `SSH key "deploy": client "backup-box" may now use it`}, + {name: "a never rule", new: []edit{{"clients = [\"laptop\", \"backup-box\"]\nask = \"always\"", "clients = [\"laptop\"]\nask = \"never\""}}, + want: `secret "db-password": new rule: never ask`}, + {name: "always to window", new: []edit{{"ask = \"always\"\n\n[secret.backup-token]", "ask = \"window\"\nwindow = \"5m\"\n\n[secret.backup-token]"}}, + want: `secret "db-password": ask changed from "always" to "window"`}, + {name: "a longer window", new: []edit{{"window = \"15m\"\n\n[secret.db-password]", "window = \"1h\"\n\n[secret.db-password]"}}, + want: `secret "github-token": window changed from 15m to 1h`}, + {name: "wider hours", new: []edit{{"23:30-00:30", "23:00-00:30"}}, want: `hours changed from "23:30-00:30" to "23:00-00:30"`}, + {name: "no hours", new: []edit{{"hours = \"23:30-00:30\"\n", ""}}, want: `hours changed from "23:30-00:30" to any time`}, + {name: "a higher max_per_day", new: []edit{{"max_per_day = 2", "max_per_day = 3"}}, want: "max_per_day changed from 2 to 3"}, + {name: "no max_per_day", new: []edit{{"max_per_day = 2\n", ""}}, want: "max_per_day changed from 2 to no limit"}, + {name: "notify off", new: []edit{{"hosts = [\"db-1\"]", "hosts = [\"db-1\"]\nnotify = false"}}, + want: `SSH key "backup": notify changed from true to false`}, + {name: "more hosts", new: []edit{{`hosts = ["web-1", "web-2"]`, `hosts = ["web-1", "web-2", "db-1"]`}}, + want: `SSH key "deploy": new host "db-1"`}, + {name: "any host", new: []edit{{"hosts = [\"web-1\", \"web-2\"]\n", ""}}, + want: `SSH key "deploy": may now log in to every host in known_hosts`}, + {name: "preapprove", new: []edit{{"window = \"15m\"\n\n[secret.db-password]", "window = \"15m\"\npreapprove = true\n\n[secret.db-password]"}}, + want: `secret "github-token": preapprove changed from false to true`}, + {name: "standing", old: []edit{{"[client.laptop]", standingTOML + "\n[client.laptop]"}}, + new: []edit{{"window = \"15m\"\n\n[secret.db-password]", "window = \"15m\"\nstanding = true\n\n[secret.db-password]"}}, + want: `secret "github-token": standing changed from false to true`}, + {name: "a [standing] section", new: []edit{{"[client.laptop]", standingTOML + "\n[client.laptop]"}}, + want: "[standing]: new: standing grants are allowed"}, + {name: "a longer [standing] limit", old: []edit{{"[client.laptop]", standingTOML + "\n[client.laptop]"}}, + new: []edit{{standingTOML, standingTOML + "max_days = 120\n"}}, want: "[standing] max_days: changed from 90 to 120"}, + {name: "a wider [preapprove] limit", new: []edit{{"[client.laptop]", "[preapprove]\nmax_uses = 20\n\n[client.laptop]"}}, + want: "[preapprove] max_uses: changed from 10 to 20"}, + {name: "another approver", new: []edit{{"user_id = 123456789", "user_id = 987654321"}}, + want: "[telegram] user_id: the approver changed from 123456789 to 987654321"}, + {name: "another notice chat", new: []edit{{"user_id = 123456789", "user_id = 123456789\nnotice_chat_id = -100123"}}, + want: "[telegram] notice_chat_id"}, + {name: "a longer timeout", new: []edit{{"user_id = 123456789", "user_id = 123456789\ntimeout = \"300s\""}}, + want: "[telegram] timeout: changed from 2m to 5m"}, + {name: "another bot token file", new: []edit{{"/telegram-token", "/bws-token"}}, want: "[telegram] token_file"}, + {name: "a new pin", new: []edit{{"{{pin laptop}}", testPin("other").String()}}, want: `client "laptop": new pin`}, + {name: "another store program", new: []edit{{"{{dir}}/bin/bws", "{{dir}}/bin/bw"}}, want: `store "main": program changed`}, + {name: "another store token", new: []edit{{"/bws-token", "/ssh-backup"}}, want: `store "main": token_file changed`}, + {name: "another store password", new: []edit{{"/bw-master-password", "/ssh-backup"}}, want: `store "vault": password_file changed`}, + {name: "a secret in another store", new: []edit{{"store = \"main\"\nid = \"00000000-0000-0000-0000-000000000001\"", + "store = \"vault\"\nid = \"00000000-0000-0000-0000-000000000001\""}}, + want: `secret "github-token": now another item: store changed from "main" to "vault"`}, + {name: "another item", new: []edit{{"00000000-0000-0000-0000-000000000001", "00000000-0000-0000-0000-000000000009"}}, + want: `secret "github-token": now another item: id changed`}, + {name: "another key file", new: []edit{{"/ssh-deploy", "/bw-master-password"}}, want: `SSH key "deploy": file changed`}, + {name: "listen", new: []edit{{"192.0.2.10:7743", "192.0.2.11:7743"}}, want: `listen: changed from "192.0.2.10:7743" to "192.0.2.11:7743"`}, + {name: "tls_key", new: []edit{{"/broker-tls-key", "/ssh-backup"}}, want: "tls_key: changed"}, + {name: "audit_log", new: []edit{{"/audit.jsonl", "/audit2.jsonl"}}, want: "audit_log: changed"}, + {name: "known_hosts path", new: []edit{{"{{dir}}/known_hosts", "{{dir}}/known_hosts2"}}, + files: func(t *testing.T, e *brokerEnv) { + writeFile(t, e.dir, "known_hosts2", strings.Join(e.hostLines("web-1", "web-2", "db-1"), "\n")+"\n", 0o644) + }, want: "known_hosts: changed"}, + {name: "a new host", hosts: func(e *brokerEnv) []string { + e.hosts["db-2"] = newHostKey(t) + return e.hostLines("web-1", "web-2", "db-1", "db-2") + }, want: `known_hosts: new host "db-2"`}, + {name: "a new host key", hosts: func(e *brokerEnv) []string { + e.hosts["web-1"] = newHostKey(t) + return e.hostLines("web-1", "web-2", "db-1") + }, want: `known_hosts: host "web-1" has a new host key SHA256:`}, + {name: "no update check", new: []edit{{"[client.laptop]", "[updates]\ncheck = false\n\n[client.laptop]"}}, + want: "[updates] check: changed from true to false"}, + } { + t.Run(c.name, func(t *testing.T) { + changes := c.run(t) + got, ok := findChange(changes, c.want) + if !ok || !got.Widens || !strings.HasPrefix(got.String(), "⚠ ") { + t.Fatalf("no marked change with %q; the diff:\n%s", c.want, diffText(changes)) + } + if _, ok := findChange(changes, "does not know"); ok { + t.Errorf("a known change is also an unknown one:\n%s", diffText(changes)) + } + }) + } +} + +// TestDiffNarrowing: a change that narrows access is a plain line. +func TestDiffNarrowing(t *testing.T) { + for _, c := range []diffCase{ + {name: "a client removed", new: []edit{{"\"laptop\", \"backup-box\"", "\"laptop\""}, {"clients = [\"backup-box\"]\nask = \"never\"\nhours", "clients = [\"laptop\"]\nask = \"never\"\nhours"}, + {"clients = [\"backup-box\"]\nask = \"never\"\nhosts", "clients = [\"laptop\"]\nask = \"never\"\nhosts"}, + {"[client.backup-box]\npin = \"{{pin backup-box}}\"\n", ""}}, want: `client "backup-box": removed`}, + {name: "a shorter window", new: []edit{{"window = \"15m\"\n\n[secret.db-password]", "window = \"5m\"\n\n[secret.db-password]"}}, + want: "window changed from 15m to 5m"}, + {name: "never to always", new: []edit{{"ask = \"never\"\nhours = \"23:30-00:30\"\nmax_per_day = 2\nnotify = false", "ask = \"always\""}}, + want: `secret "backup-token": ask changed from "never" to "always"`}, + {name: "fewer hosts", new: []edit{{`hosts = ["web-1", "web-2"]`, `hosts = ["web-1"]`}}, want: `SSH key "deploy": host "web-2" removed`}, + {name: "a lower max_per_day", new: []edit{{"max_per_day = 2", "max_per_day = 1"}}, want: "max_per_day changed from 2 to 1"}, + {name: "narrower hours", new: []edit{{"23:30-00:30", "23:45-00:15"}}, want: "hours changed"}, + {name: "notify on", new: []edit{{"notify = false", "notify = true"}}, want: "notify changed from false to true"}, + {name: "a host removed", hosts: func(e *brokerEnv) []string { return e.hostLines("web-1", "web-2", "db-1") }, + new: []edit{{`hosts = ["web-1", "web-2"]`, `hosts = ["web-1"]`}}, want: `host "web-2" removed`}, + } { + t.Run(c.name, func(t *testing.T) { + changes := c.run(t) + got, ok := findChange(changes, c.want) + if !ok || got.Widens || !strings.HasPrefix(got.String(), " ") { + t.Fatalf("no plain change with %q; the diff:\n%s", c.want, diffText(changes)) + } + }) + } +} + +// TestDiffNeverParts: notify, max_per_day and hours are compared only +// between two "never" rules; a move to "never" shows them on its line. +func TestDiffNeverParts(t *testing.T) { + changes := diffCase{new: []edit{{"ask = \"never\"\nhours = \"23:30-00:30\"\nmax_per_day = 2\nnotify = false", "ask = \"always\""}}}.run(t) + if len(changes) != 1 || changes[0].Widens { + t.Errorf("never to always:\n%s", diffText(changes)) + } + changes = diffCase{new: []edit{{"clients = [\"laptop\", \"backup-box\"]\nask = \"always\"", + "clients = [\"laptop\"]\nask = \"never\"\nmax_per_day = 5\nnotify = false"}}}.run(t) + got, ok := findChange(changes, `new rule: never ask (ask changed from "always" to "never"), with notify false, max_per_day 5, hours any time`) + if !ok || !got.Widens { + t.Errorf("always to never:\n%s", diffText(changes)) + } + if _, ok := findChange(changes, "max_per_day changed"); ok { + t.Errorf("a line for max_per_day:\n%s", diffText(changes)) + } +} + +// TestDiffSameMeaning: comments, order and spaces alone are no change. +func TestDiffSameMeaning(t *testing.T) { + c := diffCase{ + new: []edit{ + {"# A broker config for tests.", "# Another comment.\n\n\n"}, + {"user_id = 123456789", "user_id = 123456789 # the approver"}, + {"\"laptop\", \"backup-box\"", "\"backup-box\",\"laptop\""}, + // [key.backup] moves before [key.deploy]. + {"[key.deploy]", "[key.backup]\nfile = \"${CREDENTIALS_DIRECTORY}/ssh-backup\"\nclients = [\"backup-box\"]\n" + + "ask = \"never\"\nhosts = [\"db-1\"]\n\n[key.deploy]"}, + {"\n[key.backup]\nfile = \"${CREDENTIALS_DIRECTORY}/ssh-backup\"\nclients = [\"backup-box\"]\nask = \"never\"\nhosts = [\"db-1\"]\n", "\n"}, + }, + hosts: func(e *brokerEnv) []string { + return append([]string{"# the hosts", ""}, e.hostLines("db-1", "web-2", "web-1")...) + }, + } + if changes := c.run(t); len(changes) != 0 { + t.Fatalf("the diff:\n%s", diffText(changes)) + } +} + +// TestDiffComparesEveryField: each field of the types of the config is in +// compared, or is a type in compared itself. So a field that is added +// later and not compared by name is found by unknown. +func TestDiffComparesEveryField(t *testing.T) { + for typ, names := range compared { + for i := range typ.NumField() { + f := typ.Field(i) + if slices.Contains(names, f.Name) { + continue + } + ft := f.Type + for ft.Kind() == reflect.Pointer || ft.Kind() == reflect.Map { + ft = ft.Elem() + } + if _, ok := compared[ft]; !ok { + t.Errorf("%s.%s is neither compared by name nor a type in compared; unknown compares it, "+ + "so each of its changes is marked as one that config-diff does not know", typ.Name(), f.Name) + } + } + } +} + +// TestDiffUnknownIsMarked: a change of a field that Diff does not compare +// by name is marked. +func TestDiffUnknownIsMarked(t *testing.T) { + saved := compared[reflect.TypeFor[Telegram]()] + t.Cleanup(func() { compared[reflect.TypeFor[Telegram]()] = saved }) + // As if Telegram had a new field that Diff does not know. + compared[reflect.TypeFor[Telegram]()] = slices.DeleteFunc(slices.Clone(saved), func(s string) bool { return s == "NoticeChatID" }) + changes := diffCase{new: []edit{{"user_id = 123456789", "user_id = 123456789\nnotice_chat_id = -100123"}}}.run(t) + got, ok := findChange(changes, "a change that config-diff does not know") + if !ok || !got.Widens { + t.Fatalf("the diff:\n%s", diffText(changes)) + } + // The same config: no change. + if changes := (diffCase{}).run(t); len(changes) != 0 { + t.Fatalf("the same config:\n%s", diffText(changes)) + } +} + +func diffText(changes []Change) string { + var b strings.Builder + for _, c := range changes { + b.WriteString(c.String() + "\n") + } + return b.String() +} + +// TestDiffNtfy: a change of the channel, or of the ntfy server, a topic or +// the token file, is marked; a shorter timeout is not. +func TestDiffNtfy(t *testing.T) { + tg := reloadBase() + nt := reloadCopy(tg) + nt.Telegram = Telegram{} + nt.Ntfy = &Ntfy{Server: "https://ntfy.example.com", RequestTopic: "a", AnswerTopic: "b", TokenFile: "/t", + Timeout: 2 * time.Minute, SSHTimeout: time.Minute} + for _, c := range []struct { + name string + old, next *Broker + want string + widens bool + }{ + {"to ntfy", tg, nt, "changed from [telegram] to [ntfy]", true}, + {"to telegram", nt, tg, "changed from [ntfy] to [telegram]", true}, + {"another server", nt, ntfyWith(nt, func(n *Ntfy) { n.Server = "https://other.example.com" }), "[ntfy] server", true}, + {"another topic", nt, ntfyWith(nt, func(n *Ntfy) { n.AnswerTopic = "c" }), "[ntfy] answer_topic", true}, + {"another token", nt, ntfyWith(nt, func(n *Ntfy) { n.TokenFile = "/u" }), "[ntfy] token_file", true}, + {"a shorter timeout", nt, ntfyWith(nt, func(n *Ntfy) { n.Timeout = time.Minute }), "[ntfy] timeout", false}, + {"a longer ssh_timeout", nt, ntfyWith(nt, func(n *Ntfy) { n.SSHTimeout = 2 * time.Minute }), "[ntfy] ssh_timeout", true}, + } { + changes := Diff(c.old, c.next) + got, ok := findChange(changes, c.want) + if !ok || got.Widens != c.widens || len(changes) != 1 { + t.Errorf("%s: the diff:\n%s", c.name, diffText(changes)) + } + } +} + +func ntfyWith(b *Broker, f func(*Ntfy)) *Broker { + c := reloadCopy(b) + n := *b.Ntfy + f(&n) + c.Ntfy = &n + return c +}