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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/cmd/endpoint-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/policy-change-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/policy-change-publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion docs/cmd/policy-change-show.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,23 @@ 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.

## Output

`json` holds `change` and `preview` (head revision, stale, direction, changes,
matched counts, coverage, warnings). `plain` writes `key<TAB>value` lines and one
`change<TAB>where<TAB>what<TAB>old<TAB>new<TAB>direction` line for each change.
`change<TAB>where<TAB>what<TAB>old<TAB>new<TAB>direction<TAB>inherited` 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`.

Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/policy-rule-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 3 additions & 0 deletions docs/cmd/policy-rule-show.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
10 changes: 7 additions & 3 deletions internal/cloudpolicy/docpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,22 @@ 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".
// protovalidate writes a duration limit in seconds, which a person cannot
// 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)
})
}
2 changes: 2 additions & 0 deletions internal/cloudpolicy/docpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions internal/cmd/endpoint/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package endpoint

import (
"context"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions internal/cmd/endpoint/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
90 changes: 67 additions & 23 deletions internal/cmd/policy/change_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 + ")"
}
6 changes: 5 additions & 1 deletion internal/cmd/policy/change_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/policy/change_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading
Loading