Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3532,6 +3532,22 @@ 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`.

Each line about a new, changed or moved store reference names it (issue
#196): a new `[secret.*]` names its item (and the field for 1password,
when it is not `password`; for infisical, the project, environment,
path and name), a new `[project.*]` its project, and a new `[logins.*]`
its collection. A changed ID is its own line, such as `secret "x": item
changed from …1111aaaa to …2222bbbb`, and a move to another store names
the reference too. IDs are shown by their last 8 characters (`idText`).
IDs are not secret; the short form keeps the line easy to read, and is
enough for the owner to match the item they made. When the short forms
of the old and the new ID are the same (also in another case), or two
item rules of one `[logins.*]` have the same short form, the IDs are
shown in full, so a change always shows. A change of the field,
environment, path or name of a secret names the item too. An ID with a
character that IDs do not have is quoted in full. All these lines are
marked, so `--short` puts them first.

`--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
Expand Down
5 changes: 5 additions & 0 deletions THREAT-MODEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,11 @@ 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.
A writer of the config could keep the name of a secret that the owner
trusts and point it at another item, project or collection. So each
line about a new or changed reference names its ID by the last 8
characters, and a change of only the ID is a line with `⚠` (issue #196).
The owner checks the ID against the item they made.
It is no control of the broker: it only helps a person to decide.

**T43. A website login goes to a page that the client claims.**
Expand Down
31 changes: 30 additions & 1 deletion cmd/nodkey/configdiff_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestConfigDiff(t *testing.T) {
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",
"⚠ secret \"gone\": new secret in store \"vault\", item …00000002, ask = \"always\"\n",
} {
if !strings.Contains(out, want) {
t.Errorf("stdout %q has no %q", out, want)
Expand Down Expand Up @@ -200,3 +200,32 @@ func TestConfigDiffShort(t *testing.T) {
t.Errorf("size %d > %d: %q", size(got), minShortChars, got)
}
}

// TestConfigDiffShortItem: a change of only the item of a secret is a ⚠
// line, and --short puts it first, before the lines that narrow (issue
// #196).
func TestConfigDiffShortItem(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")
data, err := os.ReadFile(cfg) //nolint:gosec // G304: the test's own file.
if err != nil {
t.Fatal(err)
}
// The item of a secret changes (widens), and the "never" secret goes
// to "always" (narrows). The narrowing line is first by name.
data = bytes.Replace(data, []byte(idGitHub), []byte("00000000-0000-0000-0000-00003f9a1c2e"), 1)
data = bytes.Replace(data, []byte("ask = \"never\"\nnotify = false"), []byte("ask = \"always\""), 1)
writeTestFile(t, cfg, data, 0o600)
code, out, errOut := runDiff(t, "--short", "--max-chars", "200", oldDir, newDir)
lines := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
want := "⚠ secret \"github-token\": item changed from …00000001 to …3f9a1c2e"
if code != exitFailure || errOut != "" || len(lines) != 2 || lines[0] != want ||
!strings.HasPrefix(lines[1], " secret \"db-password\"") {
t.Fatalf("exit %d, stdout %q, stderr %q; want the first line %q", code, out, errOut, want)
}
}
11 changes: 11 additions & 0 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -1500,10 +1500,21 @@ change. An example:

```text
⚠ client "build-1": new client
⚠ secret "mail-password": new secret in store "main", item …3f9a1c2e, ask = "always"
⚠ secret "github-token": item changed from …1111aaaa to …2222bbbb
⚠ secret "backup-token": new rule: never ask (ask changed from "always" to "never")
SSH key "deploy": host "web-2" removed
```

A line about a new or changed secret, `[project.*]` or `[logins.*]`
names what it reads in the store: the item ID (or the project ID or
collection ID), shortened to its last 8 characters, such as `…3f9a1c2e`.
Check that it is the item you made. A secret can keep a name that you
trust and read another item: that line is `⚠` too. For a secret in
Infisical, the line names the project ID, the environment, the path and
the name. When two IDs end with the same 8 characters, the line shows
both IDs in full.

`--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
Expand Down
121 changes: 96 additions & 25 deletions internal/config/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"slices"
"strconv"
"strings"
"time"

"golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -250,7 +251,7 @@ func (d *differ) secrets(old, next *Broker) {
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))
d.add("secret", name, true, "%s: new secret in store %s, %s, %s", what, q(n.Store), secretRef(n), ruleText(n.Rule))
for _, c := range n.Clients {
d.add("secret", name, true, "%s: client %s may now ask for it", what, q(c))
}
Expand All @@ -259,17 +260,27 @@ func (d *differ) secrets(old, next *Broker) {
d.add("secret", name, false, "%s: removed", what)
continue
}
if o.Store != n.Store {
d.add("secret", name, true, "%s: now another item: store changed from %s to %s, %s",
what, q(o.Store), q(n.Store), secretRef(n))
}
if o.ID != n.ID {
a, b := idPair(o.ID, n.ID)
d.add("secret", name, true, "%s: item changed from %s to %s", what, a, b)
}
if o.Project != n.Project {
a, b := idPair(o.Project, n.Project)
d.add("secret", name, true, "%s: now another item: project changed from %s to %s", what, a, b)
}
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.add("secret", name, true, "%s: now another item: %s changed from %s to %s, %s", what, p.key,
q(p.old), q(p.new), secretRef(n))
}
}
d.clientList("secret", name, what, "ask for it", o.Clients, n.Clients)
Expand All @@ -288,7 +299,7 @@ func (d *differ) projects(old, next *Broker) {
switch {
case !inOld:
d.add("project", name, true, "%s: new: every secret of project %s in store %s, %s",
what, q(n.Project), q(n.Store), ruleText(n.Rule))
what, idText(n.Project), q(n.Store), ruleText(n.Rule))
for _, c := range n.Clients {
d.add("project", name, true, "%s: client %s may now ask for its secrets", what, q(c))
}
Expand All @@ -297,13 +308,13 @@ func (d *differ) projects(old, next *Broker) {
d.add("project", name, false, "%s: removed", what)
continue
}
for _, p := range []struct{ key, old, new string }{
{"store", o.Store, n.Store},
{"project", o.Project, n.Project},
} {
if p.old != p.new {
d.add("project", name, true, "%s: now other secrets: %s changed from %s to %s", what, p.key, q(p.old), q(p.new))
}
if o.Store != n.Store {
d.add("project", name, true, "%s: now other secrets: store changed from %s to %s, project %s",
what, q(o.Store), q(n.Store), idText(n.Project))
}
if o.Project != n.Project {
a, b := idPair(o.Project, n.Project)
d.add("project", name, true, "%s: now other secrets: project changed from %s to %s", what, a, b)
}
d.clientList("project", name, what, "ask for its secrets", o.Clients, n.Clients)
d.rule("project", name, what, o.Rule, n.Rule)
Expand All @@ -321,32 +332,34 @@ func (d *differ) logins(old, next *Broker) {
switch {
case !inOld:
d.add("logins", name, true, "%s: new website logins of collection %s in store %s, %s",
what, q(n.Collection), q(n.Store), ruleText(n.Rule))
what, idText(n.Collection), q(n.Store), ruleText(n.Rule))
for _, c := range n.Clients {
d.add("logins", name, true, "%s: client %s may now ask for a login", what, q(c))
}
for _, id := range slices.Sorted(maps.Keys(n.Items)) {
d.add("logins", name, true, "%s: item %s has its own rule: %s", what, q(id), ruleText(n.Items[id].Rule))
items := slices.Sorted(maps.Keys(n.Items))
for _, id := range items {
d.add("logins", name, true, "%s: item %s has its own rule: %s", what, itemText(id, items), ruleText(n.Items[id].Rule))
}
continue
case !inNew:
d.add("logins", name, false, "%s: removed", what)
continue
}
for _, p := range []struct{ key, old, new string }{
{"store", o.Store, n.Store},
{"collection", o.Collection, n.Collection},
} {
if p.old != p.new {
d.add("logins", name, true, "%s: now other logins: %s changed from %s to %s", what, p.key, q(p.old), q(p.new))
}
if o.Store != n.Store {
d.add("logins", name, true, "%s: now other logins: store changed from %s to %s, collection %s",
what, q(o.Store), q(n.Store), idText(n.Collection))
}
if o.Collection != n.Collection {
a, b := idPair(o.Collection, n.Collection)
d.add("logins", name, true, "%s: now other logins: collection changed from %s to %s", what, a, b)
}
d.clientList("logins", name, what, "ask for a login", o.Clients, n.Clients)
d.rule("logins", name, what, o.Rule, n.Rule)
for _, id := range sortedUnion(o.Items, n.Items) {
items := sortedUnion(o.Items, n.Items)
for _, id := range items {
oi, inO := o.Items[id]
ni, inN := n.Items[id]
iw := what + " item " + q(id)
iw := what + " item " + itemText(id, items)
switch {
case !inO:
d.add("logins", name, true, "%s: has its own rule now, in place of the collection's: %s", iw, ruleText(ni.Rule))
Expand Down Expand Up @@ -405,6 +418,64 @@ func (d *differ) keys(old, next *Broker) {
}
}

// idText is the ID of an item, a project or a collection in a store,
// shortened to its last 8 characters, such as "…3f9a1c2e" (issue #196).
// That is enough for the owner to match it with the item in the store,
// and too little to be of use alone. An ID of 8 characters or less, or
// with a character that IDs do not have, is quoted in full.
func idText(id string) string {
r := []rune(id)
idChar := func(c rune) bool {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_'
}
if len(r) <= idChars || strings.ContainsFunc(id, func(c rune) bool { return !idChar(c) }) {
return q(id)
}
return "…" + string(r[len(r)-idChars:])
}

// idChars is the number of characters of an ID that idText shows.
const idChars = 8

// idPair is idText of two IDs that differ. When their short forms are
// the same, also in another case, it gives both IDs in full, so that a
// change always shows.
func idPair(old, next string) (string, string) {
a, b := idText(old), idText(next)
if strings.EqualFold(a, b) {
return q(old), q(next)
}
return a, b
}

// itemText is idText of id, one of ids. When another ID in ids has the
// same short form, also in another case, it gives id in full, so that two
// items never look like one.
func itemText(id string, ids []string) string {
for _, other := range ids {
if other != id && strings.EqualFold(idText(other), idText(id)) {
return q(id)
}
}
return idText(id)
}

// secretRef names the item that a [secret.*] section reads: the item's
// ID shortened, and the field for 1password when it is not "password";
// for infisical, the project's ID shortened, the environment, the path
// and the secret's name.
func secretRef(s Secret) string {
if s.ID == "" && s.Project != "" {
return fmt.Sprintf("project %s, environment %s, path %s, name %s",
idText(s.Project), q(s.Environment), q(s.Path), q(s.SecretName))
}
ref := "item " + idText(s.ID)
if s.Field != "" && s.Field != "password" {
ref += ", field " + q(s.Field)
}
return ref
}

func hostsText(hosts []string) string {
if len(hosts) == 0 {
return "every host in known_hosts"
Expand Down
Loading
Loading