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/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/docs/cmd/policy-change-show.md b/docs/cmd/policy-change-show.md index e00d934..6bf7f16 100644 --- a/docs/cmd/policy-change-show.md +++ b/docs/cmd/policy-change-show.md @@ -46,6 +46,14 @@ 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. + +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. @@ -53,7 +61,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/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/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= 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) { 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() diff --git a/internal/cmd/policy/change_preview.go b/internal/cmd/policy/change_preview.go index 2387fa2..9391833 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" @@ -19,12 +20,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,22 +94,13 @@ 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)) } 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", @@ -135,6 +129,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 @@ -213,25 +233,49 @@ 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 { + overrideID := op.GetCreate().GetOverrideId() + if head != nil { + overrideID = head.GetOverrideId() + } + item := changeItem{Where: ruleOperationPlace(overrideID), 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) } 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_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_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..5d1eb4e 100644 --- a/internal/cmd/policy/change_test.go +++ b/internal/cmd/policy/change_test.go @@ -2,9 +2,11 @@ package policy import ( "context" + "strings" "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" @@ -143,6 +145,108 @@ 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 + 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", 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", 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", newValue: `"604800s"`, + wantCell: "- -> 7d", + wantPlain: "change\toverride \"payments\"\tpackage_admission.cooldown.min_age\t-\t168h\tTIGHTENS\t", + }, + } + 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.oldValue) + c.SetNewValue(tt.newValue) + c.SetInheritedValue(tt.inherited) + c.SetDirection(policyv1.ChangeDirection_CHANGE_DIRECTION_TIGHTENS) + 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) + plain := (&changeShowResult{Preview: got}).RenderPlain() + assert.Equal(t, tt.wantPlain, plain[strings.LastIndex(plain, "\n")+1:]) + }) + } +} + +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) @@ -429,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/errors_test.go b/internal/cmd/policy/errors_test.go index d42deb4..ca1a791 100644 --- a/internal/cmd/policy/errors_test.go +++ b/internal/cmd/policy/errors_test.go @@ -267,6 +267,19 @@ 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() } + // 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() + } 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 +290,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 +301,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 +311,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 +320,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 +330,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: 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", + 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: fixFailOn, 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: fixFailOn, 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 +386,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") 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") } diff --git a/internal/cmd/policy/rule_show.go b/internal/cmd/policy/rule_show.go index 5792c19..366a186 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,21 @@ 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}, }) } +// 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 { + 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 +119,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..16b6582 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") @@ -386,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") 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())),