From 664d875103425daba611bd7ff3cd05f016eb20d1 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 11:46:49 +0000 Subject: [PATCH 01/11] Bump the SafeDep API SDK to the 20261106062459-79f98cf26185 build The policy API now sends the inherited value of a path change, the head rule of a rule operation preview and the update revision of a rule. The CLI needs this build to read these fields. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 08c5e11..3aef120 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.26.2 require ( buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.12-20260709200747-435963d16310.1 - buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261104174030-9113dd6a8ea6.1 - buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261104174030-9113dd6a8ea6.2 + buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261106062459-79f98cf26185.1 + buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261106062459-79f98cf26185.2 github.com/charmbracelet/huh v1.0.0 github.com/charmbracelet/lipgloss v1.1.0 github.com/cli/browser v1.3.0 diff --git a/go.sum b/go.sum index 65baad8..22a6bf3 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,9 @@ buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.12-20260709200747-435963d16310.1 h1:6nlcxMOui23ZRVAfJM451duu79P1npA5JRdZqMilrrQ= buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.12-20260709200747-435963d16310.1/go.mod h1:TCt1lluMFnctISJXvkIQ4x3ABrPuUKCWKyjKdkJNBpw= -buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261104174030-9113dd6a8ea6.1 h1:oKI0awI3q9Ft7eISQzAknDpHx9gDUrFa9Qe4/5+1d+g= -buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261104174030-9113dd6a8ea6.1/go.mod h1:hpgi3VWa8hReWhVtNpPAjWsU4TPf+lYozGgjrHggWhw= -buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261104174030-9113dd6a8ea6.2 h1:zpGu3bDWbHdBkUgeSt45/O/38JbZMEvn55nEf30u5qg= -buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261104174030-9113dd6a8ea6.2/go.mod h1:LfbopTmazyAppwhYT2vdRDLDH8ZTm4QNbp9yFjGRbHs= +buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261106062459-79f98cf26185.1 h1:S0e3r4qsk7fVC2kEP+WDvLG2s4MmuNWFOMZcsF0mc5s= +buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261106062459-79f98cf26185.1/go.mod h1:C0+UQn8fXxnhC1ifFxYYMFbXoxcS9wmInwIxbApfsx0= +buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261106062459-79f98cf26185.2 h1:gef9XLbPemfcQJ96qCb0CQPaAXUjX/Jo8iC80gaEOZ0= +buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261106062459-79f98cf26185.2/go.mod h1:LfbopTmazyAppwhYT2vdRDLDH8ZTm4QNbp9yFjGRbHs= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= From 4d52ee8159096d9f94ae4302d6075a66f20b397a Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 11:47:16 +0000 Subject: [PATCH 02/11] Keep a decimal duration as it is in a refused request DocumentDurations matched "604800s" inside "0.604800s" because a word boundary is also between a dot and a digit. The message then showed "0.7d", which is not the value of the server. The pattern now captures the character before the number and excludes a dot and a word character. The replacement keeps that character. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- internal/cloudpolicy/docpath.go | 10 +++++++--- internal/cloudpolicy/docpath_test.go | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/internal/cloudpolicy/docpath.go b/internal/cloudpolicy/docpath.go index 3679040..ad3bd73 100644 --- a/internal/cloudpolicy/docpath.go +++ b/internal/cloudpolicy/docpath.go @@ -134,7 +134,10 @@ func joinFieldPath(segments []pathSegment) string { } // wholeSeconds matches a protovalidate duration in seconds, as "31536000s". -var wholeSeconds = regexp.MustCompile(`\b([0-9]+)s\b`) +// RE2 has no lookbehind, so the first group captures the character before +// the number. It excludes a word character and a dot, so the fraction of +// "0.604800s" does not match. +var wholeSeconds = regexp.MustCompile(`(^|[^\w.])([0-9]+)s\b`) // DocumentDurations writes each duration in text that is a whole number of // days in the form of the document notes, as "31536000s" to "365d". @@ -142,10 +145,11 @@ var wholeSeconds = regexp.MustCompile(`\b([0-9]+)s\b`) // compare with the "from 0s to 365d" note of the file. func DocumentDurations(text string) string { return wholeSeconds.ReplaceAllStringFunc(text, func(m string) string { - n, err := strconv.ParseInt(strings.TrimSuffix(m, "s"), 10, 64) + parts := wholeSeconds.FindStringSubmatch(m) + n, err := strconv.ParseInt(parts[2], 10, 64) if err != nil || n == 0 || n%(24*60*60) != 0 || n > int64(time.Duration(1<<62)/time.Second) { return m } - return humanDuration(time.Duration(n) * time.Second) + return parts[1] + humanDuration(time.Duration(n)*time.Second) }) } diff --git a/internal/cloudpolicy/docpath_test.go b/internal/cloudpolicy/docpath_test.go index eccb628..8c37456 100644 --- a/internal/cloudpolicy/docpath_test.go +++ b/internal/cloudpolicy/docpath_test.go @@ -44,6 +44,8 @@ func TestDocumentDurations(t *testing.T) { {"must be at most 604800s", "must be at most 7d"}, {"must be at most 3600s", "must be at most 3600s"}, {"must be at most 1.5s", "must be at most 1.5s"}, + {"must be at most 0.604800s", "must be at most 0.604800s"}, + {"604800s or 86400s", "7d or 1d"}, } for _, tt := range tests { t.Run(tt.in, func(t *testing.T) { From 8e50eba824977175981a6c9e5e663dfc73501214 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 11:49:05 +0000 Subject: [PATCH 03/11] Show the inherited value on an empty side of a field change A field change with an empty side showed "-", as in "scan_gate.fail_on - -> medium", although the default policy gave the field the value high. The reader could not see the value that applied. The preview now shows the inherited value that the server sends on the empty side. It marks the value "(default)" for the policy base and "(inherited)" for a policy override, as the notice email does. The JSON and plain output carry the exact inherited value. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- docs/cmd/policy-change-show.md | 8 ++++- internal/cmd/policy/change_preview.go | 41 +++++++++++++++++------- internal/cmd/policy/change_render.go | 2 +- internal/cmd/policy/change_test.go | 46 +++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 13 deletions(-) diff --git a/docs/cmd/policy-change-show.md b/docs/cmd/policy-change-show.md index e00d934..9c39046 100644 --- a/docs/cmd/policy-change-show.md +++ b/docs/cmd/policy-change-show.md @@ -46,6 +46,11 @@ know what each tool did before. A selector edit always loosens, because it can move subjects to another policy override. The impact counts only projects: the pull request check enforces `scan_gate.fail_on` and does not support the cooldown. +When the change sets a field that its layer did not set before, or unsets a +field, the empty side shows the value that the lower layers give the field, as +`high (default) -> medium` for the policy base and `medium -> high (inherited)` +for a policy override. A side shows `-` when no lower layer sets the field. + A stale change shows `STALE: sync it before you publish` in the first line. A published or deleted change shows no preview. @@ -53,7 +58,8 @@ published or deleted change shows no preview. `json` holds `change` and `preview` (head revision, stale, direction, changes, matched counts, coverage, warnings). `plain` writes `keyvalue` lines and one -`changewherewhatoldnewdirection` line for each change. +`changewherewhatoldnewdirectioninherited` line for +each change. `inherited` is the value of the lower layers on an empty side. Values in `plain` and `json` are exact, as `168h`, and a selector is the protojson that the server sends. The table humanizes them, as `7d` and `custom/criticality=high`. diff --git a/internal/cmd/policy/change_preview.go b/internal/cmd/policy/change_preview.go index 2387fa2..5bc1121 100644 --- a/internal/cmd/policy/change_preview.go +++ b/internal/cmd/policy/change_preview.go @@ -19,12 +19,14 @@ import ( // `change show` and `change publish` print. // changeItem is one row of "What changes": a field, a policy override or a -// package rule operation. +// package rule operation. Inherited is the value that the lower layers give +// a field on a side that this layer does not set. type changeItem struct { Where string `json:"where"` What string `json:"what"` Old string `json:"old,omitempty"` New string `json:"new,omitempty"` + Inherited string `json:"inherited,omitempty"` Direction string `json:"direction"` oldHuman string newHuman string @@ -91,16 +93,7 @@ func newPreviewView(res *policyv1.PreviewPolicyChangeResponse) *previewView { Warnings: []warningView{}, } for _, c := range res.GetChanges() { - where := "base" - if c.GetLayer() == recordv1.Layer_LAYER_OVERRIDE { - where = fmt.Sprintf("override %q", c.GetOverrideName()) - } - oldValue, oldHuman := cloudpolicy.FormatJSONValue(c.GetPath(), c.GetOldValue()) - newValue, newHuman := cloudpolicy.FormatJSONValue(c.GetPath(), c.GetNewValue()) - v.Items = append(v.Items, changeItem{ - Where: where, What: c.GetPath(), Old: oldValue, New: newValue, - Direction: directionToken(c.GetDirection()), oldHuman: oldHuman, newHuman: newHuman, - }) + v.Items = append(v.Items, pathChangeItem(c)) } for _, c := range res.GetOverrideChanges() { v.Items = append(v.Items, overrideChangeItem(c)) @@ -135,6 +128,32 @@ func newPreviewView(res *policyv1.PreviewPolicyChangeResponse) *previewView { return v } +// pathChangeItem turns one change to a field into a row. A side that the +// layer does not set shows the value that the lower layers give it, with a +// mark, so the reader sees the value that applies and knows that this layer +// does not set it. +func pathChangeItem(c *policyv1.PathChange) changeItem { + where, mark := "base", "(default)" + if c.GetLayer() == recordv1.Layer_LAYER_OVERRIDE { + where, mark = fmt.Sprintf("override %q", c.GetOverrideName()), "(inherited)" + } + item := changeItem{Where: where, What: c.GetPath(), Direction: directionToken(c.GetDirection())} + item.Old, item.oldHuman = cloudpolicy.FormatJSONValue(c.GetPath(), c.GetOldValue()) + item.New, item.newHuman = cloudpolicy.FormatJSONValue(c.GetPath(), c.GetNewValue()) + if c.GetInheritedValue() == "" { + return item + } + var inheritedHuman string + item.Inherited, inheritedHuman = cloudpolicy.FormatJSONValue(c.GetPath(), c.GetInheritedValue()) + if c.GetOldValue() == "" { + item.oldHuman = inheritedHuman + " " + mark + } + if c.GetNewValue() == "" { + item.newHuman = inheritedHuman + " " + mark + } + return item +} + // overrideChangeValue gives the human form of a value of an override change. // The server writes a selector as protojson. The table and the publish // warning show it in the selector syntax of the CLI, as in diff --git a/internal/cmd/policy/change_render.go b/internal/cmd/policy/change_render.go index c05131b..ed7091f 100644 --- a/internal/cmd/policy/change_render.go +++ b/internal/cmd/policy/change_render.go @@ -68,7 +68,7 @@ func (r *changeShowResult) RenderPlain() string { items = p.Items } for _, it := range items { - b.WriteString("\n" + tui.PlainRow("change", it.Where, it.What, it.Old, it.New, it.Direction)) + b.WriteString("\n" + tui.PlainRow("change", it.Where, it.What, it.Old, it.New, it.Direction, it.Inherited)) } if p := r.Preview; p != nil { for _, w := range p.Warnings { diff --git a/internal/cmd/policy/change_test.go b/internal/cmd/policy/change_test.go index c86e3fe..b033180 100644 --- a/internal/cmd/policy/change_test.go +++ b/internal/cmd/policy/change_test.go @@ -143,6 +143,52 @@ func TestRunChangeShow_PreviewsAnOpenChange(t *testing.T) { assert.Contains(t, plain, "change\toverride \"payments\"\tselector\t{\"matchLabels\":{\"custom/team\":\"pay\"}}\t", "plain keeps the value of the server") } +func TestNewPreviewView_InheritedValue(t *testing.T) { + tests := []struct { + name string + layer recordv1.Layer + path string + old, new string + inherited string + wantCell string + wantInherited string + }{ + { + name: "base sets a field for the first time", layer: recordv1.Layer_LAYER_BASE, + path: "scan_gate.fail_on", new: "SEVERITY_MEDIUM", inherited: "SEVERITY_HIGH", + wantCell: "high (default) -> medium", wantInherited: "high", + }, + { + name: "override unsets a field", layer: recordv1.Layer_LAYER_OVERRIDE, + path: "package_admission.cooldown.min_age", old: `"86400s"`, inherited: `"604800s"`, + wantCell: "1d -> 7d (inherited)", wantInherited: "168h", + }, + { + name: "no lower layer sets the field", layer: recordv1.Layer_LAYER_OVERRIDE, + path: "package_admission.cooldown.min_age", new: `"604800s"`, + wantCell: "- -> 7d", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &policyv1.PathChange{} + c.SetPath(tt.path) + c.SetLayer(tt.layer) + c.SetOverrideName("payments") + c.SetOldValue(tt.old) + c.SetNewValue(tt.new) + c.SetInheritedValue(tt.inherited) + res := &policyv1.PreviewPolicyChangeResponse{} + res.SetChanges([]*policyv1.PathChange{c}) + + got := newPreviewView(res) + require.Len(t, got.Items, 1) + assert.Equal(t, tt.wantCell, got.Items[0].changeCell()) + assert.Equal(t, tt.wantInherited, got.Items[0].Inherited) + }) + } +} + func TestRunChangeShow_Warnings(t *testing.T) { client := mocks.NewPolicyServiceClientMock(t) client.EXPECT().GetPolicyChange(mock.Anything, mock.Anything).Return(changeResponse(openChange("18")), nil) From 32557ed1a503d99b0d5a38e1254984cb4c4cdb63 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 11:50:06 +0000 Subject: [PATCH 04/11] Name the head rule of a rule update or delete in the preview A preview showed an update or a delete of a package rule by its id only, as "delete r_68e23zskkh". An admin could not see what the rule selected before they published a rollback. The preview now reads the head rule that the server sends and shows its effect, selector and id, as "delete trust pkg:npm/lodash (r_68e23zskkh)". The row also names the policy override that the head rule attaches to. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- docs/cmd/policy-change-show.md | 3 ++ internal/cmd/policy/change_preview.go | 48 +++++++++++++++++++------- internal/cmd/policy/change_test.go | 49 +++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/docs/cmd/policy-change-show.md b/docs/cmd/policy-change-show.md index 9c39046..6bf7f16 100644 --- a/docs/cmd/policy-change-show.md +++ b/docs/cmd/policy-change-show.md @@ -51,6 +51,9 @@ field, the empty side shows the value that the lower layers give the field, as `high (default) -> medium` for the policy base and `medium -> high (inherited)` for a policy override. A side shows `-` when no lower layer sets the field. +An update or a delete of a package rule names the rule at head by its effect, +selector and id, as `delete trust pkg:npm/lodash (r_68e23zskkh)`. + A stale change shows `STALE: sync it before you publish` in the first line. A published or deleted change shows no preview. diff --git a/internal/cmd/policy/change_preview.go b/internal/cmd/policy/change_preview.go index 5bc1121..7af719e 100644 --- a/internal/cmd/policy/change_preview.go +++ b/internal/cmd/policy/change_preview.go @@ -7,6 +7,7 @@ import ( "time" messagescontroltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/controltower/v1" + corev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/policy/core/v1" recordv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/policy/record/v1" policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" "google.golang.org/protobuf/encoding/protojson" @@ -99,7 +100,7 @@ func newPreviewView(res *policyv1.PreviewPolicyChangeResponse) *previewView { v.Items = append(v.Items, overrideChangeItem(c)) } for _, op := range res.GetPackageRuleOperations() { - item := ruleOperationItem(op.GetOperation()) + item := ruleOperationItem(op.GetOperation(), op.GetHeadRule()) item.Direction = directionToken(op.GetDirection()) if op.GetOperation().HasCreate() { item.New = fmt.Sprintf("matches %d packages, seen on %d endpoints in 30d", @@ -232,25 +233,48 @@ func overrideChangeItem(c *policyv1.OverrideChange) changeItem { func ruleOperationItems(ops []*policyv1.PackageRuleOperation) []changeItem { out := make([]changeItem, 0, len(ops)) for _, op := range ops { - out = append(out, ruleOperationItem(op)) + out = append(out, ruleOperationItem(op, nil)) } return out } -func ruleOperationItem(op *policyv1.PackageRuleOperation) changeItem { - item := changeItem{Where: "package rules", oldHuman: "-", newHuman: "-"} +// ruleOperationItem turns one package rule operation into a row. head is +// the rule at head for an update or a delete. Only a preview has it. With +// head the row names what the rule selects, as the notice of the server +// does. Without it the row names the rule id only. +func ruleOperationItem(op *policyv1.PackageRuleOperation, head *policyv1.PackageRuleWithAttributes) changeItem { + item := changeItem{Where: ruleOperationPlace(op.GetCreate().GetOverrideId()), oldHuman: "-", newHuman: "-"} switch { case op.HasCreate(): - rule := op.GetCreate().GetRule() - item.What = fmt.Sprintf("create %s %s", - tui.EnumToken(rule.GetEffect().String(), effectPrefix), selectorText(rule.GetSelector())) - if id := op.GetCreate().GetOverrideId(); id != "" { - item.Where = "package rules of override " + id - } + item.What = "create " + ruleText(op.GetCreate().GetRule()) case op.HasUpdate(): - item.What = "update " + op.GetUpdate().GetRuleId() + item.What = "update " + headRuleText(op.GetUpdate().GetRuleId(), head) case op.HasDelete(): - item.What = "delete " + op.GetDelete().GetRuleId() + item.What = "delete " + headRuleText(op.GetDelete().GetRuleId(), head) + } + if head != nil { + item.Where = ruleOperationPlace(head.GetOverrideId()) } return item } + +func ruleOperationPlace(overrideID string) string { + if overrideID == "" { + return "package rules" + } + return "package rules of override " + overrideID +} + +// ruleText names a rule by its effect and selector, as "trust pkg:npm/evil". +func ruleText(rule *corev1.PackageRule) string { + return tui.EnumToken(rule.GetEffect().String(), effectPrefix) + " " + selectorText(rule.GetSelector()) +} + +// headRuleText names the rule of an update or a delete, as +// "trust pkg:npm/evil (r_0123456789)". +func headRuleText(id string, head *policyv1.PackageRuleWithAttributes) string { + if head == nil { + return id + } + return ruleText(head.GetRule()) + " (" + id + ")" +} diff --git a/internal/cmd/policy/change_test.go b/internal/cmd/policy/change_test.go index b033180..a0ff4cc 100644 --- a/internal/cmd/policy/change_test.go +++ b/internal/cmd/policy/change_test.go @@ -5,6 +5,7 @@ import ( "testing" errorv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/error/v1" + corev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/policy/core/v1" recordv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/policy/record/v1" policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" "github.com/safedep/dry/usefulerror" @@ -189,6 +190,54 @@ func TestNewPreviewView_InheritedValue(t *testing.T) { } } +func TestNewPreviewView_RuleOperationNamesTheHeadRule(t *testing.T) { + update := &policyv1.PackageRuleUpdate{} + update.SetRuleId("r_77e") + updateOp := &policyv1.PackageRuleOperation{} + updateOp.SetUpdate(update) + del := &policyv1.PackageRuleDelete{} + del.SetRuleId("r_80b") + deleteOp := &policyv1.PackageRuleOperation{} + deleteOp.SetDelete(del) + + tests := []struct { + name string + op *policyv1.PackageRuleOperation + head *policyv1.PackageRuleWithAttributes + wantWhere string + wantWhat string + }{ + { + name: "delete of a base rule", op: deleteOp, + head: ruleWithAttributes("r_80b", corev1.Effect_EFFECT_TRUST, ""), + wantWhere: "package rules", wantWhat: "delete trust pkg:npm/leftpad (r_80b)", + }, + { + name: "update of an override rule", op: updateOp, + head: ruleWithAttributes("r_77e", corev1.Effect_EFFECT_BLOCK, "ov_pay"), + wantWhere: "package rules of override ov_pay", wantWhat: "update block pkg:npm/leftpad (r_77e)", + }, + { + name: "rule that is not active at head", op: deleteOp, + wantWhere: "package rules", wantWhat: "delete r_80b", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + preview := &policyv1.PackageRuleOperationPreview{} + preview.SetOperation(tt.op) + preview.SetHeadRule(tt.head) + res := &policyv1.PreviewPolicyChangeResponse{} + res.SetPackageRuleOperations([]*policyv1.PackageRuleOperationPreview{preview}) + + got := newPreviewView(res) + require.Len(t, got.Items, 1) + assert.Equal(t, tt.wantWhere, got.Items[0].Where) + assert.Equal(t, tt.wantWhat, got.Items[0].What) + }) + } +} + func TestRunChangeShow_Warnings(t *testing.T) { client := mocks.NewPolicyServiceClientMock(t) client.EXPECT().GetPolicyChange(mock.Anything, mock.Anything).Return(changeResponse(openChange("18")), nil) From 3a89c19bdc47ebdd20a09196bbbd6f35ed631261 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 11:50:47 +0000 Subject: [PATCH 05/11] Show the revision of the last update of a package rule The server now keeps added_revision as the revision that first created a rule, and sends updated_revision for the last update. rule show gave only the added revision, so an admin could not see that a later revision changed the rule. rule show now writes "added in revision 17, updated in revision 23" when an update changed the rule. The JSON and plain output carry the updated revision. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- docs/cmd/policy-rule-list.md | 2 +- docs/cmd/policy-rule-show.md | 3 +++ internal/cmd/policy/rule_show.go | 15 +++++++++++++-- internal/cmd/policy/rule_test.go | 8 ++++++++ internal/cmd/policy/rule_view.go | 2 ++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/docs/cmd/policy-rule-list.md b/docs/cmd/policy-rule-list.md index aaa1486..ed3d2c5 100644 --- a/docs/cmd/policy-rule-list.md +++ b/docs/cmd/policy-rule-list.md @@ -43,7 +43,7 @@ r_80b pkg:npm/leftpad trust malware FP, S-71 web ## Output `json` holds `rules` (rule id, purl, vers, effect, scopes, reason, ticket, -requester, override id, `attaches_to`, author, added and removed revision, times) +requester, override id, `attaches_to`, author, added, updated and removed revision, times) and `next_page_token`. `plain` writes a tab-separated header and one row for each rule. The table humanizes the expiry. diff --git a/docs/cmd/policy-rule-show.md b/docs/cmd/policy-rule-show.md index bbee260..50fb5d8 100644 --- a/docs/cmd/policy-rule-show.md +++ b/docs/cmd/policy-rule-show.md @@ -28,6 +28,9 @@ Created by carol Revisions added in revision 48 · active ``` +After an update of the rule, `Revisions` also names the revision of the last +update, as `added in revision 48, updated in revision 52 · active`. + ## Authentication Requires a control-plane OAuth session. Run `safedep auth login` first. diff --git a/internal/cmd/policy/rule_show.go b/internal/cmd/policy/rule_show.go index 5792c19..b10fc30 100644 --- a/internal/cmd/policy/rule_show.go +++ b/internal/cmd/policy/rule_show.go @@ -25,7 +25,7 @@ func ruleShowCmd(a *app.App) *cobra.Command { Use: "show RULE", Short: "Show one package rule", Long: "Show one package rule: its selector, effect, trust scopes, reason, ticket, requester, the " + - "policy override that it attaches to, its expiry, and the revisions that added and removed it. " + + "policy override that it attaches to, its expiry, and the revisions that added, updated and removed it. " + "A block message of a tool cites the rule id.", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -89,10 +89,20 @@ func (r *ruleShowResult) table(now time.Time) string { {"Attaches to", v.AttachesTo}, {"Expires", expires}, {"Created by", cloudpolicy.EmptyDash(v.CreatedBy)}, - {"Revisions", fmt.Sprintf("added in revision %d%s%s", v.AddedRevision, sep, removed)}, + {"Revisions", v.revisions() + sep + removed}, }) } +// revisions names the revision that added the rule, and the revision of its +// last update when an update changed it later. +func (v ruleView) revisions() string { + text := fmt.Sprintf("added in revision %d", v.AddedRevision) + if v.UpdatedRevision > 0 && v.UpdatedRevision != v.AddedRevision { + text += fmt.Sprintf(", updated in revision %d", v.UpdatedRevision) + } + return text +} + func (r *ruleShowResult) RenderPlain() string { v := r.Rule return strings.Join([]string{ @@ -108,6 +118,7 @@ func (r *ruleShowResult) RenderPlain() string { clitui.PlainRow("expires_at", v.ExpiresAt), clitui.PlainRow("created_by", v.CreatedBy), clitui.PlainRow("added_revision", fmt.Sprint(v.AddedRevision)), + clitui.PlainRow("updated_revision", fmt.Sprint(v.UpdatedRevision)), clitui.PlainRow("removed_revision", fmt.Sprint(v.RemovedRevision)), }, "\n") } diff --git a/internal/cmd/policy/rule_test.go b/internal/cmd/policy/rule_test.go index a15e88c..e66bf9a 100644 --- a/internal/cmd/policy/rule_test.go +++ b/internal/cmd/policy/rule_test.go @@ -316,6 +316,14 @@ func TestRunRuleShow(t *testing.T) { assert.Contains(t, table, "Rule r_9c1") assert.Contains(t, table, "added in revision 40 · active") + updated := ruleWithAttributes("r_9c1", corev1.Effect_EFFECT_BLOCK, "") + updated.SetUpdatedRevision(43) + res.SetRule(updated) + got, err = runRuleShow(context.Background(), client, "r_9c1") + require.NoError(t, err) + assert.Contains(t, got.RenderTable(), "added in revision 40, updated in revision 43 · active") + assert.Contains(t, got.RenderPlain(), "updated_revision\t43") + missing := mocks.NewPolicyServiceClientMock(t) missing.EXPECT().GetPackageRule(mock.Anything, mock.Anything).Return(nil, status.Error(codes.NotFound, "x")) _, err = runRuleShow(context.Background(), missing, "r_x") diff --git a/internal/cmd/policy/rule_view.go b/internal/cmd/policy/rule_view.go index 798fe30..6d70857 100644 --- a/internal/cmd/policy/rule_view.go +++ b/internal/cmd/policy/rule_view.go @@ -28,6 +28,7 @@ type ruleView struct { AttachesTo string `json:"attaches_to"` CreatedBy string `json:"created_by,omitempty"` AddedRevision uint64 `json:"added_revision"` + UpdatedRevision uint64 `json:"updated_revision,omitempty"` RemovedRevision uint64 `json:"removed_revision,omitempty"` ExpiresAt string `json:"expires_at,omitempty"` CreatedAt string `json:"created_at,omitempty"` @@ -53,6 +54,7 @@ func newRuleView(r *policyv1.PackageRuleWithAttributes, names map[string]string) AttachesTo: baseAttachment, CreatedBy: cloudpolicy.ActorName(r.GetCreatedBy()), AddedRevision: r.GetAddedRevision(), + UpdatedRevision: r.GetUpdatedRevision(), RemovedRevision: r.GetRemovedRevision(), ExpiresAt: cloudpolicy.RFC3339(expires), CreatedAt: cloudpolicy.RFC3339(cloudpolicy.TimeOf(r.GetCreatedAt())), From 2d30dca89f37f8c32ea7875fa379d2f306f3addb Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 11:51:27 +0000 Subject: [PATCH 06/11] Show no revision for a tool that sent no policy report endpoint list showed "rev 0" in the POLICY column for a tool that sent no policy report. The state of such a tool is unspecified and its revision is 0, which is not a policy revision. The column now leaves out a tool with an unspecified state and revision 0, as it does for a disabled tool. A tool with an unspecified state and a revision keeps its revision. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- docs/cmd/endpoint-list.md | 3 ++- internal/cmd/endpoint/policy_test.go | 17 +++++++++++------ internal/cmd/endpoint/service.go | 6 ++++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/docs/cmd/endpoint-list.md b/docs/cmd/endpoint-list.md index 6800cf0..b0dbc02 100644 --- a/docs/cmd/endpoint-list.md +++ b/docs/cmd/endpoint-list.md @@ -36,7 +36,8 @@ The `Policy` column shows the applied policy revision that each tool reported: `rev 48` when every tool applies the same revision, or `pmg 48, vet 47`. A tool that applies no cloud policy is not in the column: its state is `disabled`, as for pmg with `cloud.policy: false`, or `none`, when the tool has no policy cache yet and applies its -baseline. The column shows `-` when no tool is left. JSON +baseline. A tool that sent no policy report is not in the column either. The column +shows `-` when no tool is left. JSON output carries `tool_policies` for each endpoint: the tool, its version, its applied policy revision and the state of the policy in the tool. diff --git a/internal/cmd/endpoint/policy_test.go b/internal/cmd/endpoint/policy_test.go index 524de8f..60176ef 100644 --- a/internal/cmd/endpoint/policy_test.go +++ b/internal/cmd/endpoint/policy_test.go @@ -2,6 +2,7 @@ package endpoint import ( "context" + "fmt" "testing" "time" @@ -130,17 +131,21 @@ func TestPolicyCell(t *testing.T) { func TestToolPolicies_StatesWithNoRevisionAreNotInThePolicyCell(t *testing.T) { tests := []struct { - state recordv1.ApplyState - want string + state recordv1.ApplyState + revision uint64 + want string }{ - {recordv1.ApplyState_APPLY_STATE_DISABLED, "-"}, - {recordv1.ApplyState_APPLY_STATE_NONE, "-"}, - {recordv1.ApplyState_APPLY_STATE_STALE, "rev 0"}, + {recordv1.ApplyState_APPLY_STATE_DISABLED, 0, "-"}, + {recordv1.ApplyState_APPLY_STATE_NONE, 0, "-"}, + {recordv1.ApplyState_APPLY_STATE_UNSPECIFIED, 0, "-"}, + {recordv1.ApplyState_APPLY_STATE_UNSPECIFIED, 47, "rev 47"}, + {recordv1.ApplyState_APPLY_STATE_STALE, 0, "rev 0"}, } for _, tt := range tests { - t.Run(tt.state.String(), func(t *testing.T) { + t.Run(fmt.Sprintf("%s rev %d", tt.state, tt.revision), func(t *testing.T) { report := &recordv1.ApplyReport{} report.SetState(tt.state) + report.SetRevision(tt.revision) p := &controltowerv1.EndpointToolPolicy{} p.SetToolName("pmg") p.SetPolicy(report) diff --git a/internal/cmd/endpoint/service.go b/internal/cmd/endpoint/service.go index ba9ee4f..fa49236 100644 --- a/internal/cmd/endpoint/service.go +++ b/internal/cmd/endpoint/service.go @@ -122,8 +122,8 @@ type ToolPolicy struct { State string ReportedAt time.Time // noRevision marks a tool that applies no cloud policy: it is disabled - // in the tool, or the tool has no policy cache yet. Its revision is 0 - // and means nothing. + // in the tool, the tool has no policy cache yet, or the endpoint sent no + // policy report for the tool. Its revision is 0 and means nothing. noRevision bool } type ListEndpoint struct { @@ -346,6 +346,8 @@ func toolPolicies(in []*controltowerv1.EndpointToolPolicy) []ToolPolicy { switch p.GetPolicy().GetState() { case recordv1.ApplyState_APPLY_STATE_DISABLED, recordv1.ApplyState_APPLY_STATE_NONE: tp.noRevision = true + case recordv1.ApplyState_APPLY_STATE_UNSPECIFIED: + tp.noRevision = tp.Revision == 0 } if ts := p.GetReportedAt(); ts != nil { tp.ReportedAt = ts.AsTime().UTC() From d58249d1ed3290e3d43ef9a0558c3936e5f3179b Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 11:53:12 +0000 Subject: [PATCH 07/11] Test that rule create validates the request before it connects No test checked that rule create refuses an invalid request before it connects to the server. The trust expiry test now runs createRules with a trust entry that expires in 91 days. Its connection callback fails the test when createRules calls it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- internal/cmd/policy/rule_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/cmd/policy/rule_test.go b/internal/cmd/policy/rule_test.go index e66bf9a..16b6582 100644 --- a/internal/cmd/policy/rule_test.go +++ b/internal/cmd/policy/rule_test.go @@ -394,13 +394,17 @@ func TestExpandScopes_AllMixedWithANamedScope(t *testing.T) { } } -func TestBuildRuleCreates_TrustExpiryLimit(t *testing.T) { +func TestRuleCreate_TrustExpiryLimit(t *testing.T) { in := ruleCreateInput{Trust: true, Selector: "pkg:pypi/foo@1.0.3", Scopes: []string{"malware"}, Reason: "FP", Expires: "90d"} _, err := buildRuleCreates(in, nil, fixedNow) require.NoError(t, err) in.Expires = "91d" - _, err = buildRuleCreates(in, nil, fixedNow) + connect := func() (policyv1grpc.PolicyServiceClient, error) { + require.Fail(t, "createRules connected to the server before it validated the request") + return nil, nil + } + err = createRules(context.Background(), connect, in, nil, func() time.Time { return fixedNow }) ue, ok := usefulerror.AsUsefulError(err) require.True(t, ok) assert.Contains(t, ue.HumanError(), "a trust entry expires at most 90 days from now") From 67050a57cd1bc89c95e747ac218ec5652dd4c669 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 11:53:12 +0000 Subject: [PATCH 08/11] Assert both requests of a policy edit that the server refuses The test of a refused edit counted the requests but did not read them. Three cases corrected a comment line in the file header and not the field, so the second request sent the refused value again, and the test passed. The test now reads the edited field in the refused request and in the corrected request. The three corrections now edit the field. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- internal/cmd/policy/errors_test.go | 49 ++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/internal/cmd/policy/errors_test.go b/internal/cmd/policy/errors_test.go index d42deb4..6fed4e9 100644 --- a/internal/cmd/policy/errors_test.go +++ b/internal/cmd/policy/errors_test.go @@ -267,6 +267,15 @@ func TestKeptEditError_Help(t *testing.T) { } func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { + minAgeField := func(p *policyv1.Policy) string { + return p.GetBase().GetPackageAdmission().GetCooldown().GetMinAge().AsDuration().String() + } + payName := func(p *policyv1.Policy) string { return p.GetOverrides()[0].GetName() } + payLabels := func(p *policyv1.Policy) string { return fmt.Sprint(p.GetOverrides()[0].GetSelector().GetMatchLabels()) } + ciName := func(p *policyv1.Policy) string { return p.GetOverrides()[1].GetName() } + ciFailOn := func(p *policyv1.Policy) string { + return p.GetOverrides()[1].GetSpec().GetScanGate().GetFailOn().String() + } minAge := func(s string) string { s = uncomment(" # package_admission:", " # cooldown:", " # min_age: 7d")(s) return strings.Replace(s, "min_age: 7d", "min_age: 400d", 1) @@ -277,6 +286,10 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { bad, fix func(string) string violation *errdetails.BadRequest_FieldViolation want string + // field reads the edited field from the settings of a request. sent + // is its value in the refused request and in the corrected request. + field func(*policyv1.Policy) string + sent [2]string }{ { name: "a min_age above the limit", @@ -284,7 +297,9 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { fix: replace("min_age: 400d", "min_age: 30d"), violation: violation(encodedFieldPath("settings", "base", "package_admission", "cooldown", "min_age"), "must be greater than or equal to 0s and less than or equal to 31536000s"), - want: "# ! base.package_admission.cooldown.min_age: must be greater than or equal to 0s and less than or equal to 365d\n", + want: "# ! base.package_admission.cooldown.min_age: must be greater than or equal to 0s and less than or equal to 365d\n", + field: minAgeField, + sent: [2]string{"9600h0m0s", "720h0m0s"}, }, { name: "an override name with an upper case letter", @@ -292,6 +307,8 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { fix: replace("name: Payments", "name: payments-team"), violation: violation(encodedFieldPath("settings", "overrides[0]", "name"), "does not match regex pattern"), want: "# ! overrides[0].name: does not match regex pattern\n", + field: payName, + sent: [2]string{"Payments", "payments-team"}, }, { name: "an unknown selector label prefix", @@ -299,6 +316,8 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { fix: replace("{team/criticality:", "{custom/criticality:"), violation: violation(`settings.overrides[0].selector.match_labels["team/criticality"]`, `unknown label prefix "team/"`), want: "# ! overrides[0].selector.matchLabels[\"team/criticality\"]: unknown label prefix \"team/\"\n", + field: payLabels, + sent: [2]string{"map[team/criticality:high]", "map[custom/criticality:high]"}, }, { name: "a field of the override in the file", @@ -307,42 +326,50 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { fix: replace("name: CI", "name: ci-runners"), violation: violation(encodedFieldPath("settings", "overrides[1]", "name"), "does not match regex pattern"), want: "# ! name: does not match regex pattern\n", + field: ciName, + sent: [2]string{"CI", "ci-runners"}, }, { name: "a spec field of the override in the file", override: "ci", bad: replace("fail_on: critical", "fail_on: high"), - fix: replace("fail_on: high", "fail_on: medium"), + fix: replace("\n fail_on: high", "\n fail_on: medium"), violation: violation(encodedFieldPath("settings", "overrides[1]", "spec", "scan_gate", "fail_on"), "value must be one of the defined enum values"), - want: "# ! spec.scan_gate.fail_on: value must be one of the defined enum values\n", + want: "# ! spec.scan_gate.fail_on: value must be one of the defined enum values\n", + field: ciFailOn, + sent: [2]string{"SEVERITY_HIGH", "SEVERITY_MEDIUM"}, }, { name: "the override in the file as a whole", override: "ci", bad: replace("fail_on: critical", "fail_on: high"), - fix: replace("fail_on: high", "fail_on: medium"), + fix: replace("\n fail_on: high", "\n fail_on: medium"), violation: violation(encodedFieldPath("settings", "overrides[1]"), "the override selects one endpoint"), want: "# ! the override selects one endpoint\n", + field: ciFailOn, + sent: [2]string{"SEVERITY_HIGH", "SEVERITY_MEDIUM"}, }, { name: "another override in an edit of one override", override: "ci", bad: replace("fail_on: critical", "fail_on: high"), - fix: replace("fail_on: high", "fail_on: medium"), + fix: replace("\n fail_on: high", "\n fail_on: medium"), violation: violation(encodedFieldPath("settings", "overrides[0]", "priority"), "must be unique"), want: "# ! overrides[0].priority: must be unique\n", + field: ciFailOn, + sent: [2]string{"SEVERITY_HIGH", "SEVERITY_MEDIUM"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { client := mocks.NewPolicyServiceClientMock(t) client.EXPECT().GetPolicy(mock.Anything, mock.Anything).Return(headResponse(), nil) - calls := 0 + var requests []*policyv1.CreatePolicyChangeRequest client.EXPECT().CreatePolicyChange(mock.Anything, mock.Anything). - RunAndReturn(func(context.Context, *policyv1.CreatePolicyChangeRequest, ...grpc.CallOption) (*policyv1.CreatePolicyChangeResponse, error) { - calls++ - if calls == 1 { + RunAndReturn(func(_ context.Context, r *policyv1.CreatePolicyChangeRequest, _ ...grpc.CallOption) (*policyv1.CreatePolicyChangeResponse, error) { + requests = append(requests, r) + if len(requests) == 1 { return nil, statusWithViolations(codes.InvalidArgument, "service execution failed: "+tt.violation.GetDescription(), tt.violation) } @@ -355,7 +382,9 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { change, err := runEdit(context.Background(), client, ed.open, "", editInput{Override: tt.override}) require.NoError(t, err) assert.Equal(t, "18", change.GetChangeId()) - assert.Equal(t, 2, calls) + require.Len(t, requests, 2) + assert.Equal(t, tt.sent[0], tt.field(requests[0].GetSettings()), "the refused request") + assert.Equal(t, tt.sent[1], tt.field(requests[1].GetSettings()), "the corrected request") require.Len(t, ed.seen, 2) assert.Contains(t, ed.seen[1], tt.want) assert.NotContains(t, ed.seen[1], "rpc error") From f8868a5eedb5efce0d6b8397462efad500c36246 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 12:03:50 +0000 Subject: [PATCH 09/11] Test the inherited column of the plain change line No test read the inherited column of a plain change line. A change that dropped the column passed every test. The inherited value test now checks one full plain change line in each case. The test table also uses the names oldValue and newValue, because new is a Go built-in. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- internal/cmd/policy/change_test.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/internal/cmd/policy/change_test.go b/internal/cmd/policy/change_test.go index a0ff4cc..71d3514 100644 --- a/internal/cmd/policy/change_test.go +++ b/internal/cmd/policy/change_test.go @@ -2,6 +2,7 @@ package policy import ( "context" + "strings" "testing" errorv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/error/v1" @@ -149,25 +150,30 @@ func TestNewPreviewView_InheritedValue(t *testing.T) { name string layer recordv1.Layer path string - old, new string + oldValue string + newValue string inherited string wantCell string wantInherited string + wantPlain string }{ { name: "base sets a field for the first time", layer: recordv1.Layer_LAYER_BASE, - path: "scan_gate.fail_on", new: "SEVERITY_MEDIUM", inherited: "SEVERITY_HIGH", + path: "scan_gate.fail_on", newValue: "SEVERITY_MEDIUM", inherited: "SEVERITY_HIGH", wantCell: "high (default) -> medium", wantInherited: "high", + wantPlain: "change\tbase\tscan_gate.fail_on\t-\tmedium\tTIGHTENS\thigh", }, { name: "override unsets a field", layer: recordv1.Layer_LAYER_OVERRIDE, - path: "package_admission.cooldown.min_age", old: `"86400s"`, inherited: `"604800s"`, + path: "package_admission.cooldown.min_age", oldValue: `"86400s"`, inherited: `"604800s"`, wantCell: "1d -> 7d (inherited)", wantInherited: "168h", + wantPlain: "change\toverride \"payments\"\tpackage_admission.cooldown.min_age\t24h\t-\tTIGHTENS\t168h", }, { name: "no lower layer sets the field", layer: recordv1.Layer_LAYER_OVERRIDE, - path: "package_admission.cooldown.min_age", new: `"604800s"`, - wantCell: "- -> 7d", + path: "package_admission.cooldown.min_age", newValue: `"604800s"`, + wantCell: "- -> 7d", + wantPlain: "change\toverride \"payments\"\tpackage_admission.cooldown.min_age\t-\t168h\tTIGHTENS\t", }, } for _, tt := range tests { @@ -176,9 +182,10 @@ func TestNewPreviewView_InheritedValue(t *testing.T) { c.SetPath(tt.path) c.SetLayer(tt.layer) c.SetOverrideName("payments") - c.SetOldValue(tt.old) - c.SetNewValue(tt.new) + c.SetOldValue(tt.oldValue) + c.SetNewValue(tt.newValue) c.SetInheritedValue(tt.inherited) + c.SetDirection(policyv1.ChangeDirection_CHANGE_DIRECTION_TIGHTENS) res := &policyv1.PreviewPolicyChangeResponse{} res.SetChanges([]*policyv1.PathChange{c}) @@ -186,6 +193,8 @@ func TestNewPreviewView_InheritedValue(t *testing.T) { require.Len(t, got.Items, 1) assert.Equal(t, tt.wantCell, got.Items[0].changeCell()) assert.Equal(t, tt.wantInherited, got.Items[0].Inherited) + plain := (&changeShowResult{Preview: got}).RenderPlain() + assert.Equal(t, tt.wantPlain, plain[strings.LastIndex(plain, "\n")+1:]) }) } } From bf1828d1a8683bad2a261132f6b44414c16e23db Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 12:03:50 +0000 Subject: [PATCH 10/11] Write the loosening warning with no nested parentheses The publish warning put the old and new values in parentheses. An inherited value has its own parentheses, so the warning read "scan_gate.fail_on (high (default) -> low)". A rule operation has no values, so its warning ended in "(- -> -)". The warning now reads "It loosens scan_gate.fail_on from high (default) to low in base". For an item with no old and no new value, it names only the item and its place, as "It loosens delete trust pkg:npm/leftpad (r_80b) in package rules". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- docs/cmd/policy-change-create.md | 2 +- docs/cmd/policy-change-publish.md | 2 +- internal/cmd/policy/change_publish.go | 6 ++- internal/cmd/policy/change_test.go | 67 +++++++++++++++++++++------ internal/cmd/policy/publish_test.go | 2 +- 5 files changed, 62 insertions(+), 17 deletions(-) diff --git a/docs/cmd/policy-change-create.md b/docs/cmd/policy-change-create.md index 3d2d235..541b904 100644 --- a/docs/cmd/policy-change-create.md +++ b/docs/cmd/policy-change-create.md @@ -31,7 +31,7 @@ safedep policy change create --from-revision 46 --capability scan_gate -m "rever ``` ✓ change 20 restores scan_gate from revision 46 · package rules stay at head -! It loosens scan_gate.fail_on (medium -> high) in override "payments" +! It loosens scan_gate.fail_on from medium to high in override "payments" Review it: safedep policy change show 20 Publish it: safedep policy change publish 20 ``` diff --git a/docs/cmd/policy-change-publish.md b/docs/cmd/policy-change-publish.md index 4894a08..e7a10c7 100644 --- a/docs/cmd/policy-change-publish.md +++ b/docs/cmd/policy-change-publish.md @@ -28,7 +28,7 @@ safedep policy change publish 18 -m "payments: fail on medium, 7d cooldown (CUST ``` Change 18 · LOOSENS. Only an Admin can publish it, and Admins and Owners get a notice -! It loosens package_admission.cooldown.min_age (- -> 7d) in override "payments" +! It loosens package_admission.cooldown.min_age from - to 7d in override "payments" ! It changes the selector of policy override "payments" from custom/team=pay to custom/criticality=high. Change 18 loosens the policy. Publish it? [y/N]: y ✓ revision 47 published · LOOSENS diff --git a/internal/cmd/policy/change_publish.go b/internal/cmd/policy/change_publish.go index 1a260bc..217918a 100644 --- a/internal/cmd/policy/change_publish.go +++ b/internal/cmd/policy/change_publish.go @@ -156,7 +156,11 @@ func reportLoosening(p *previewView) { tui.Warning("%s", clitui.EscapeLine(item.sentence)) continue } - tui.Warning("It loosens %s (%s -> %s) in %s", clitui.EscapeLine(item.What), + if item.oldHuman == "-" && item.newHuman == "-" { + tui.Warning("It loosens %s in %s", clitui.EscapeLine(item.What), clitui.EscapeLine(item.Where)) + continue + } + tui.Warning("It loosens %s from %s to %s in %s", clitui.EscapeLine(item.What), clitui.EscapeLine(item.oldHuman), clitui.EscapeLine(item.newHuman), clitui.EscapeLine(item.Where)) } } diff --git a/internal/cmd/policy/change_test.go b/internal/cmd/policy/change_test.go index 71d3514..5d1eb4e 100644 --- a/internal/cmd/policy/change_test.go +++ b/internal/cmd/policy/change_test.go @@ -533,18 +533,59 @@ func TestOverrideChangeItem(t *testing.T) { } } -func TestReportLoosening_AnAddedOverride(t *testing.T) { - stderr := captureStderr(t) - c := &policyv1.OverrideChange{} - c.SetOverrideName("policy-test") - c.SetKind(policyv1.OverrideChangeKind_OVERRIDE_CHANGE_KIND_ADDED) - c.SetNewValue("policy-test") - c.SetDirection(policyv1.ChangeDirection_CHANGE_DIRECTION_LOOSENS) - res := &policyv1.PreviewPolicyChangeResponse{} - res.SetOverrideChanges([]*policyv1.OverrideChange{c}) - res.SetDirection(policyv1.ChangeDirection_CHANGE_DIRECTION_LOOSENS) +func TestReportLoosening(t *testing.T) { + added := &policyv1.OverrideChange{} + added.SetOverrideName("policy-test") + added.SetKind(policyv1.OverrideChangeKind_OVERRIDE_CHANGE_KIND_ADDED) + added.SetNewValue("policy-test") + added.SetDirection(policyv1.ChangeDirection_CHANGE_DIRECTION_LOOSENS) + + field := &policyv1.PathChange{} + field.SetPath("scan_gate.fail_on") + field.SetLayer(recordv1.Layer_LAYER_BASE) + field.SetNewValue("SEVERITY_LOW") + field.SetInheritedValue("SEVERITY_HIGH") + field.SetDirection(policyv1.ChangeDirection_CHANGE_DIRECTION_LOOSENS) + + del := &policyv1.PackageRuleDelete{} + del.SetRuleId("r_80b") + op := &policyv1.PackageRuleOperation{} + op.SetDelete(del) + ruleDelete := &policyv1.PackageRuleOperationPreview{} + ruleDelete.SetOperation(op) + ruleDelete.SetHeadRule(ruleWithAttributes("r_80b", corev1.Effect_EFFECT_TRUST, "")) + ruleDelete.SetDirection(policyv1.ChangeDirection_CHANGE_DIRECTION_LOOSENS) - reportLoosening(newPreviewView(res)) - assert.Contains(t, stderr.String(), `It adds policy override "policy-test".`) - assert.NotContains(t, stderr.String(), "It loosens added") + tests := []struct { + name string + res func(*policyv1.PreviewPolicyChangeResponse) + want string + }{ + { + name: "an added override", + res: func(r *policyv1.PreviewPolicyChangeResponse) { r.SetOverrideChanges([]*policyv1.OverrideChange{added}) }, + want: "It adds policy override \"policy-test\".\n", + }, + { + name: "a field with an inherited value", + res: func(r *policyv1.PreviewPolicyChangeResponse) { r.SetChanges([]*policyv1.PathChange{field}) }, + want: "It loosens scan_gate.fail_on from high (default) to low in base\n", + }, + { + name: "a rule delete", + res: func(r *policyv1.PreviewPolicyChangeResponse) { + r.SetPackageRuleOperations([]*policyv1.PackageRuleOperationPreview{ruleDelete}) + }, + want: "It loosens delete trust pkg:npm/leftpad (r_80b) in package rules\n", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stderr := captureStderr(t) + res := &policyv1.PreviewPolicyChangeResponse{} + tt.res(res) + reportLoosening(newPreviewView(res)) + assert.Contains(t, stderr.String(), tt.want) + }) + } } diff --git a/internal/cmd/policy/publish_test.go b/internal/cmd/policy/publish_test.go index cf053b3..bc67e48 100644 --- a/internal/cmd/policy/publish_test.go +++ b/internal/cmd/policy/publish_test.go @@ -86,7 +86,7 @@ func TestRunChangePublish_ShowsTheDirectionAndEveryWarning(t *testing.T) { out := stderr.String() assert.Contains(t, out, "Change 18 · LOOSENS. Only an Admin can publish it") - assert.Contains(t, out, "It loosens package_admission.cooldown.min_age (- -> 7d) in override \"payments\"") + assert.Contains(t, out, "It loosens package_admission.cooldown.min_age from - to 7d in override \"payments\"") assert.Contains(t, out, "It changes the selector of policy override \"payments\" from custom/team=pay to reported/os=linux.") assert.Contains(t, out, "ENDPOINT_LABEL_LOOSENS payments: a policy override that loosens selects on a label that an endpoint can send") } From 607d88717728250e892434b0e2fae99ced6d1ca3 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 12:03:50 +0000 Subject: [PATCH 11/11] Set the place of a rule operation one time and explain two guards ruleOperationItem set the place of a row and then set it again for a head rule. It now finds the override id first and sets the place one time. The comment on the revisions of a rule now gives the reason for its guard. The edit retry test now says why its fix step anchors on the indent of the field. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- internal/cmd/policy/change_preview.go | 9 +++++---- internal/cmd/policy/errors_test.go | 10 +++++++--- internal/cmd/policy/rule_show.go | 5 +++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/internal/cmd/policy/change_preview.go b/internal/cmd/policy/change_preview.go index 7af719e..9391833 100644 --- a/internal/cmd/policy/change_preview.go +++ b/internal/cmd/policy/change_preview.go @@ -243,7 +243,11 @@ func ruleOperationItems(ops []*policyv1.PackageRuleOperation) []changeItem { // head the row names what the rule selects, as the notice of the server // does. Without it the row names the rule id only. func ruleOperationItem(op *policyv1.PackageRuleOperation, head *policyv1.PackageRuleWithAttributes) changeItem { - item := changeItem{Where: ruleOperationPlace(op.GetCreate().GetOverrideId()), oldHuman: "-", newHuman: "-"} + overrideID := op.GetCreate().GetOverrideId() + if head != nil { + overrideID = head.GetOverrideId() + } + item := changeItem{Where: ruleOperationPlace(overrideID), oldHuman: "-", newHuman: "-"} switch { case op.HasCreate(): item.What = "create " + ruleText(op.GetCreate().GetRule()) @@ -252,9 +256,6 @@ func ruleOperationItem(op *policyv1.PackageRuleOperation, head *policyv1.Package case op.HasDelete(): item.What = "delete " + headRuleText(op.GetDelete().GetRuleId(), head) } - if head != nil { - item.Where = ruleOperationPlace(head.GetOverrideId()) - } return item } diff --git a/internal/cmd/policy/errors_test.go b/internal/cmd/policy/errors_test.go index 6fed4e9..ca1a791 100644 --- a/internal/cmd/policy/errors_test.go +++ b/internal/cmd/policy/errors_test.go @@ -273,6 +273,10 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { payName := func(p *policyv1.Policy) string { return p.GetOverrides()[0].GetName() } payLabels := func(p *policyv1.Policy) string { return fmt.Sprint(p.GetOverrides()[0].GetSelector().GetMatchLabels()) } ciName := func(p *policyv1.Policy) string { return p.GetOverrides()[1].GetName() } + // The file header has the comment line "scan_gate.fail_on: high" of the + // default policy. The fix anchors on the indent of the field, so it edits + // the field and not that comment. + fixFailOn := replace("\n fail_on: high", "\n fail_on: medium") ciFailOn := func(p *policyv1.Policy) string { return p.GetOverrides()[1].GetSpec().GetScanGate().GetFailOn().String() } @@ -333,7 +337,7 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { name: "a spec field of the override in the file", override: "ci", bad: replace("fail_on: critical", "fail_on: high"), - fix: replace("\n fail_on: high", "\n fail_on: medium"), + fix: fixFailOn, violation: violation(encodedFieldPath("settings", "overrides[1]", "spec", "scan_gate", "fail_on"), "value must be one of the defined enum values"), want: "# ! spec.scan_gate.fail_on: value must be one of the defined enum values\n", @@ -344,7 +348,7 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { name: "the override in the file as a whole", override: "ci", bad: replace("fail_on: critical", "fail_on: high"), - fix: replace("\n fail_on: high", "\n fail_on: medium"), + fix: fixFailOn, violation: violation(encodedFieldPath("settings", "overrides[1]"), "the override selects one endpoint"), want: "# ! the override selects one endpoint\n", field: ciFailOn, @@ -354,7 +358,7 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { name: "another override in an edit of one override", override: "ci", bad: replace("fail_on: critical", "fail_on: high"), - fix: replace("\n fail_on: high", "\n fail_on: medium"), + fix: fixFailOn, violation: violation(encodedFieldPath("settings", "overrides[0]", "priority"), "must be unique"), want: "# ! overrides[0].priority: must be unique\n", field: ciFailOn, diff --git a/internal/cmd/policy/rule_show.go b/internal/cmd/policy/rule_show.go index b10fc30..366a186 100644 --- a/internal/cmd/policy/rule_show.go +++ b/internal/cmd/policy/rule_show.go @@ -93,8 +93,9 @@ func (r *ruleShowResult) table(now time.Time) string { }) } -// revisions names the revision that added the rule, and the revision of its -// last update when an update changed it later. +// The server sends updated_revision 0 when no update changed the rule. An +// updated revision equal to the added revision tells the reader nothing new, +// so the row leaves it out. func (v ruleView) revisions() string { text := fmt.Sprintf("added in revision %d", v.AddedRevision) if v.UpdatedRevision > 0 && v.UpdatedRevision != v.AddedRevision {