From 18da4bb243ae66349e1fb9fde949bd10fec82896 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 18:41:32 +0000 Subject: [PATCH 1/4] chore(deps): bump the SafeDep API SDK for ListPolicyDecisions The policy decision log needs the RPC ListPolicyDecisions of PolicyService. The API build 20261104174030-9113dd6a8ea6 adds it. The regenerated PolicyServiceClient mock gets the new method. The same build moves GitRef to safedep.messages.scm.v1. The CLI does not use GitRef, so no other code changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- go.mod | 4 +- go.sum | 8 +- .../mocks/PolicyServiceClient_mock.go | 83 +++++++++++++++++++ 3 files changed, 89 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index a431329..a54d020 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/safedep/cli go 1.26.2 require ( - buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261031065119-dc85a10c2beb.1 - buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261031065119-dc85a10c2beb.2 + 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 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 44bdf97..65baad8 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-20261031065119-dc85a10c2beb.1 h1:GFuHP+IKmox25L8V2EF4CR3rblReLuKjZCnuOuR7dc4= -buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261031065119-dc85a10c2beb.1/go.mod h1:wVMcwuJdlWZbHcwPPepDJQR1zQDjh/ANlc7Zv0BDfiI= -buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261031065119-dc85a10c2beb.2 h1:1p9CC/GyQ5POTKPtFvl+48Fk+37rQl/zbAOd1PDDabE= -buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261031065119-dc85a10c2beb.2/go.mod h1:LfbopTmazyAppwhYT2vdRDLDH8ZTm4QNbp9yFjGRbHs= +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= 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/mocks/PolicyServiceClient_mock.go b/internal/cloudpolicy/mocks/PolicyServiceClient_mock.go index 4979e96..34b844b 100644 --- a/internal/cloudpolicy/mocks/PolicyServiceClient_mock.go +++ b/internal/cloudpolicy/mocks/PolicyServiceClient_mock.go @@ -1035,6 +1035,89 @@ func (_c *PolicyServiceClientMock_ListPolicyChanges_Call) RunAndReturn(run func( return _c } +// ListPolicyDecisions provides a mock function for the type PolicyServiceClientMock +func (_mock *PolicyServiceClientMock) ListPolicyDecisions(ctx context.Context, in *policyv1.ListPolicyDecisionsRequest, opts ...grpc.CallOption) (*policyv1.ListPolicyDecisionsResponse, error) { + var tmpRet mock.Arguments + if len(opts) > 0 { + tmpRet = _mock.Called(ctx, in, opts) + } else { + tmpRet = _mock.Called(ctx, in) + } + ret := tmpRet + + if len(ret) == 0 { + panic("no return value specified for ListPolicyDecisions") + } + + var r0 *policyv1.ListPolicyDecisionsResponse + var r1 error + if returnFunc, ok := ret.Get(0).(func(context.Context, *policyv1.ListPolicyDecisionsRequest, ...grpc.CallOption) (*policyv1.ListPolicyDecisionsResponse, error)); ok { + return returnFunc(ctx, in, opts...) + } + if returnFunc, ok := ret.Get(0).(func(context.Context, *policyv1.ListPolicyDecisionsRequest, ...grpc.CallOption) *policyv1.ListPolicyDecisionsResponse); ok { + r0 = returnFunc(ctx, in, opts...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*policyv1.ListPolicyDecisionsResponse) + } + } + if returnFunc, ok := ret.Get(1).(func(context.Context, *policyv1.ListPolicyDecisionsRequest, ...grpc.CallOption) error); ok { + r1 = returnFunc(ctx, in, opts...) + } else { + r1 = ret.Error(1) + } + return r0, r1 +} + +// PolicyServiceClientMock_ListPolicyDecisions_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListPolicyDecisions' +type PolicyServiceClientMock_ListPolicyDecisions_Call struct { + *mock.Call +} + +// ListPolicyDecisions is a helper method to define mock.On call +// - ctx context.Context +// - in *policyv1.ListPolicyDecisionsRequest +// - opts ...grpc.CallOption +func (_e *PolicyServiceClientMock_Expecter) ListPolicyDecisions(ctx interface{}, in interface{}, opts ...interface{}) *PolicyServiceClientMock_ListPolicyDecisions_Call { + return &PolicyServiceClientMock_ListPolicyDecisions_Call{Call: _e.mock.On("ListPolicyDecisions", + append([]interface{}{ctx, in}, opts...)...)} +} + +func (_c *PolicyServiceClientMock_ListPolicyDecisions_Call) Run(run func(ctx context.Context, in *policyv1.ListPolicyDecisionsRequest, opts ...grpc.CallOption)) *PolicyServiceClientMock_ListPolicyDecisions_Call { + _c.Call.Run(func(args mock.Arguments) { + var arg0 context.Context + if args[0] != nil { + arg0 = args[0].(context.Context) + } + var arg1 *policyv1.ListPolicyDecisionsRequest + if args[1] != nil { + arg1 = args[1].(*policyv1.ListPolicyDecisionsRequest) + } + var arg2 []grpc.CallOption + var variadicArgs []grpc.CallOption + if len(args) > 2 { + variadicArgs = args[2].([]grpc.CallOption) + } + arg2 = variadicArgs + run( + arg0, + arg1, + arg2..., + ) + }) + return _c +} + +func (_c *PolicyServiceClientMock_ListPolicyDecisions_Call) Return(listPolicyDecisionsResponse *policyv1.ListPolicyDecisionsResponse, err error) *PolicyServiceClientMock_ListPolicyDecisions_Call { + _c.Call.Return(listPolicyDecisionsResponse, err) + return _c +} + +func (_c *PolicyServiceClientMock_ListPolicyDecisions_Call) RunAndReturn(run func(ctx context.Context, in *policyv1.ListPolicyDecisionsRequest, opts ...grpc.CallOption) (*policyv1.ListPolicyDecisionsResponse, error)) *PolicyServiceClientMock_ListPolicyDecisions_Call { + _c.Call.Return(run) + return _c +} + // ListPolicyRevisions provides a mock function for the type PolicyServiceClientMock func (_mock *PolicyServiceClientMock) ListPolicyRevisions(ctx context.Context, in *policyv1.ListPolicyRevisionsRequest, opts ...grpc.CallOption) (*policyv1.ListPolicyRevisionsResponse, error) { var tmpRet mock.Arguments From 233f92bee59b8207170f2ac09d9328b55f937dc6 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 18:49:34 +0000 Subject: [PATCH 2/4] refactor(duration): share the --since and --until time window audit event list turned --since and --until into a time range in its own code. policy decision list needs the same flags with the same checks. The logic now lives in duration.Window, so both commands refuse the same input with the same message. The audit behavior does not change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- internal/cmd/audit/list.go | 18 +++++-------- internal/duration/window.go | 40 +++++++++++++++++++++++++++ internal/duration/window_test.go | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 internal/duration/window.go create mode 100644 internal/duration/window_test.go diff --git a/internal/cmd/audit/list.go b/internal/cmd/audit/list.go index 5078aff..53855d7 100644 --- a/internal/cmd/audit/list.go +++ b/internal/cmd/audit/list.go @@ -2,7 +2,6 @@ package audit import ( "context" - "errors" "fmt" "strings" "time" @@ -95,19 +94,16 @@ func invalidFilter(cause error, help string) error { func newListRequest(in listInput, now time.Time) (*auditv1.ListAuditEventsRequest, error) { filters := &auditv1.ListAuditEventsRequest_Filters{} set := false - if in.Since < 0 || in.Until < 0 { - return nil, invalidFilter(errors.New("--since and --until must not be negative"), "Use a duration such as 7d") + after, before, err := duration.Window(in.Since, in.Until, now) + if err != nil { + return nil, err } - if in.Since > 0 { - filters.SetOccurredAfter(timestamppb.New(now.Add(-in.Since))) + if after != nil { + filters.SetOccurredAfter(timestamppb.New(*after)) set = true } - if in.Until > 0 { - if in.Since > 0 && in.Until >= in.Since { - return nil, invalidFilter(errors.New("--until must be shorter than --since"), - "As in --since 7d --until 1d for the events from 7 days ago to 1 day ago") - } - filters.SetOccurredBefore(timestamppb.New(now.Add(-in.Until))) + if before != nil { + filters.SetOccurredBefore(timestamppb.New(*before)) set = true } if in.Action != "" { diff --git a/internal/duration/window.go b/internal/duration/window.go new file mode 100644 index 0000000..15d8579 --- /dev/null +++ b/internal/duration/window.go @@ -0,0 +1,40 @@ +package duration + +import ( + "errors" + "time" + + "github.com/safedep/dry/usefulerror" +) + +// Window turns the --since and --until flags of a list command into a time +// range. Both are durations before now, and zero means no bound. after is +// nil when since is zero, and before is nil when until is zero. More than +// one list command reads these flags, so they all refuse the same input with +// the same message. +func Window(since, until time.Duration, now time.Time) (after, before *time.Time, err error) { + if since < 0 || until < 0 { + return nil, nil, badWindow(errors.New("--since and --until must not be negative"), "Use a duration such as 7d") + } + if since > 0 && until >= since { + return nil, nil, badWindow(errors.New("--until must be shorter than --since"), + "As in --since 7d --until 1d for the time from 7 days ago to 1 day ago") + } + if since > 0 { + t := now.Add(-since) + after = &t + } + if until > 0 { + t := now.Add(-until) + before = &t + } + return after, before, nil +} + +func badWindow(cause error, help string) error { + return usefulerror.NewUsefulError(). + WithCode(usefulerror.ErrBadRequest). + WithHumanError(cause.Error()). + WithHelp(help). + Wrap(cause) +} diff --git a/internal/duration/window_test.go b/internal/duration/window_test.go new file mode 100644 index 0000000..0b4ecec --- /dev/null +++ b/internal/duration/window_test.go @@ -0,0 +1,46 @@ +package duration + +import ( + "testing" + "time" + + "github.com/safedep/dry/usefulerror" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestWindow(t *testing.T) { + now := time.Date(2026, 11, 4, 12, 0, 0, 0, time.UTC) + day := 24 * time.Hour + tests := []struct { + name string + since time.Duration + until time.Duration + wantAfter *time.Time + wantBefore *time.Time + wantErr string + }{ + {name: "no bounds"}, + {name: "since only", since: 7 * day, wantAfter: new(now.Add(-7 * day))}, + {name: "until only", until: day, wantBefore: new(now.Add(-day))}, + {name: "both", since: 7 * day, until: day, wantAfter: new(now.Add(-7 * day)), wantBefore: new(now.Add(-day))}, + {name: "until not shorter", since: time.Hour, until: time.Hour, wantErr: "--until must be shorter than --since"}, + {name: "negative", since: -time.Hour, wantErr: "must not be negative"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + after, before, err := Window(tt.since, tt.until, now) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + ue, ok := usefulerror.AsUsefulError(err) + require.True(t, ok) + assert.Equal(t, usefulerror.ErrBadRequest, ue.Code()) + return + } + require.NoError(t, err) + assert.Equal(t, tt.wantAfter, after) + assert.Equal(t, tt.wantBefore, before) + }) + } +} From 6c6959cf32a4a8681f2bba0c49f18b5c2636f473 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 18:49:34 +0000 Subject: [PATCH 3/4] feat(policy): add safedep policy decision list An admin who changes a rule, or who sees a blocked install, could not ask which decisions a revision, an override or a rule made, and where. The new command reads ListPolicyDecisions and lists the decisions of pmg and the pull request check, newest first. Each filter of the RPC has a flag: --since, --until, --tool, --outcome, --revision, --rule, --override, --path, --package, --project and --endpoint. --override takes a name or an id and resolves it in the policy at --revision, or at head. --package takes a PURL with no version. Each row shows the time, the tool, the package version, the outcome, the rule or field with its layer or override, the revision, and the place. The revision cell adds the default policy revision when the default policy decided, so a cooldown block shows both. A pmg place is the hostname and the user, with one hint: the CI repository, else the Kubernetes workload, else the agent. A check shows the repository, the pull request number and the link. A scan shows the repository and the ref. The JSON output writes the messages of the server in protojson. The errors map to a missing entitlement, a role that cannot read the policy, a bad page token or filter, and a server without the RPC. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- README.md | 2 + docs/cmd/policy-decision-list.md | 111 ++++++ internal/cmd/policy/cmd.go | 15 +- internal/cmd/policy/decision_list.go | 284 ++++++++++++++ internal/cmd/policy/decision_test.go | 565 +++++++++++++++++++++++++++ internal/cmd/policy/decision_view.go | 253 ++++++++++++ internal/cmd/policy/errors.go | 49 +++ 7 files changed, 1276 insertions(+), 3 deletions(-) create mode 100644 docs/cmd/policy-decision-list.md create mode 100644 internal/cmd/policy/decision_list.go create mode 100644 internal/cmd/policy/decision_test.go create mode 100644 internal/cmd/policy/decision_view.go diff --git a/README.md b/README.md index e82c0bc..7c3faca 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ and drill into package inventory and recent activity. Edit the one policy of your tenant: the policy base, policy overrides that select endpoints and projects by label, and package rules that trust or block packages. Review each policy change before you publish it, and roll back from any policy revision. +See which rule made each decision of pmg and the pull request check, and where. **Projects and labels**: `safedep project` List projects by label selector, set `custom/` labels, and see which policy applies @@ -150,6 +151,7 @@ Push SafeDep malware findings to external security tools. JFrog XRay is supporte | [`safedep policy rule show`](./docs/cmd/policy-rule-show.md) | Show one package rule | | [`safedep policy rule update`](./docs/cmd/policy-rule-update.md) | Change the reason, expiry, ticket or requester of a package rule | | [`safedep policy rule delete`](./docs/cmd/policy-rule-delete.md) | Delete a package rule | +| [`safedep policy decision list`](./docs/cmd/policy-decision-list.md) | List the decisions that the policy made, and where | | [`safedep audit event list`](./docs/cmd/audit-event-list.md) | List audit events of the tenant | | [`safedep query exec`](./docs/cmd/query-exec.md) | Execute a SQL query against SafeDep Cloud | | [`safedep query schema list`](./docs/cmd/query-schema-list.md) | List tables in the query schema | diff --git a/docs/cmd/policy-decision-list.md b/docs/cmd/policy-decision-list.md new file mode 100644 index 0000000..8cc3025 --- /dev/null +++ b/docs/cmd/policy-decision-list.md @@ -0,0 +1,111 @@ +# safedep policy decision list + +List the decisions that pmg and the pull request check made with the policy of +the tenant, newest first. Each row says which policy revision and which package +rule or field decided, and where the decision happened. A decision that did not +use the cloud policy is not in the list. + +## Synopsis + +``` +safedep policy decision list [--since DURATION] [--until DURATION] + [--tool pmg|pull-request-check|vet] + [--outcome blocked|failed|allowed] + [--revision N] [--rule RULE_ID] [--override NAME|ID] + [--path PATH] [--package PURL] + [--project PROJECT_ID] [--endpoint ENDPOINT_ID] + [--limit N] [--page-token TOKEN] [--output table|plain|json] +``` + +## Flags + +| Flag | Description | +|------|-------------| +| `--since ` | Only decisions in this time before now, as `7d` or `12h`. | +| `--until ` | Only decisions older than this time before now, as `1d`. It must be shorter than `--since`. | +| `--tool ` | Only decisions of `pmg`, `pull-request-check` or `vet`. | +| `--outcome ` | Only decisions that `blocked` an install, `failed` a check, or `allowed` a package because a trust entry waived a verdict. | +| `--revision ` | Only decisions that applied this tenant policy revision. | +| `--rule ` | Only decisions that this package rule made. | +| `--override ` | Only decisions that this policy override made: a field that the override set, or a package rule that attaches to it. The command finds the name in the policy at `--revision`, or at head. | +| `--path ` | Only decisions that this field made, by its contract path, as `package_admission.cooldown.min_age`. | +| `--package ` | Only decisions about this package, in all its versions. Give a PURL with no version, as `pkg:npm/lodash`. | +| `--project ` | Only decisions at a pull request or a scan of a repository that is bound to this project. | +| `--endpoint ` | Only decisions that pmg made on this endpoint. | +| `--limit ` | Page size. The server default applies when 0. | +| `--page-token ` | Continuation token from a prior response. | + +Inherits root flags `--output` and `--profile`. + +## Columns + +| Column | Content | +|--------|---------| +| `WHEN` | When the tool made the decision. | +| `TOOL` | `pmg`, `pull-request-check` or `vet`. | +| `PACKAGE` | The ecosystem, the name and the version. | +| `OUTCOME` | `blocked`, `failed` or `allowed`. | +| `DECIDED BY` | The package rule, or the field path, and the layer or the override that set it: `default policy`, `base` or `override NAME`. | +| `REV` | The tenant policy revision. When the default policy decided, also its revision, as `47 · default 12`. | +| `WHERE` | For pmg, the hostname and the user, and one hint: the CI repository, else the Kubernetes namespace and workload, else the agent. For a check, the repository and the pull request number. For a scan, the repository and the branch or the commit. | +| `LINK` | The check run, the pull request or the CI run. The table has this column only when a row has a link. | + +## Examples + +The decisions that one package rule made: + +```bash +safedep policy decision list --rule r_9c1x4kq7dm +``` + +``` +Policy decisions +WHEN TOOL PACKAGE OUTCOME DECIDED BY REV WHERE LINK +5m ago pmg npm/evil@1.0.0 blocked rule r_9c1x4kq7dm 47 dev-laptop (alice) - +5m ago pull-request-check npm/evil@1.0.1 failed rule r_9c1x4kq7dm 47 acme/payments-api#42 https://github.com/acme/payments-api/runs/7 +2 decisions +``` + +The installs that the cooldown blocked in the last day: + +```bash +safedep policy decision list --path package_admission.cooldown.min_age --since 1d +``` + +``` +Policy decisions +WHEN TOOL PACKAGE OUTCOME DECIDED BY REV WHERE +5m ago pmg npm/fresh@0.0.1 blocked package_admission.cooldown.min_age · default policy 47 · default 12 dev-laptop (alice) +1 decision +``` + +The decisions of a policy override at one revision: + +```bash +safedep policy decision list --override payments --revision 47 +``` + +## Output + +`json` holds `decisions`, the `PolicyDecision` messages as the server sends them +in protojson with the field names of the contract, and `next_page_token`. Each +message has the full citation (revisions, digest, path, rule id, override id, +layer) and the full decision point, such as the working directory, the CI branch +and commit, the Kubernetes Pod and the agent of a pmg run. `plain` writes a +tab-separated header and one row for each decision, with exact times and ids. + +## Permissions + +The roles that can read the policy can read the decision log. The tenant needs +policy management. + +## Authentication + +Requires a control-plane OAuth session. Run `safedep auth login` first. + +## Exit codes + +| Code | Meaning | +|------|---------| +| `0` | The page was read, also when it is empty. | +| non-zero | Validation, authentication or the request failed. | diff --git a/internal/cmd/policy/cmd.go b/internal/cmd/policy/cmd.go index 9331ffb..b896e2a 100644 --- a/internal/cmd/policy/cmd.go +++ b/internal/cmd/policy/cmd.go @@ -1,7 +1,7 @@ // Package policy registers the `safedep policy` command tree, the CLI for // policy management in SafeDep Cloud: the policy settings document, policy -// changes, policy revisions and package rules. Every verb needs control -// plane credentials. +// changes, policy revisions, package rules and the decision log. Every +// verb needs control plane credentials. package policy import ( @@ -54,6 +54,15 @@ func Register(root *cobra.Command, a *app.App) { } rule.AddCommand(ruleCreateCmd(a), ruleListCmd(a), ruleShowCmd(a), ruleUpdateCmd(a), ruleDeleteCmd(a)) - parent.AddCommand(change, revision, rule) + decision := &cobra.Command{ + Use: "decision", + Short: "Read the decisions that the policy made", + Long: "Read the decision log: the decisions that pmg and the pull request check made with the policy " + + "of the tenant. Each decision cites the policy revision and the rule or field that decided, and " + + "says where it happened. The roles that can read the policy can read the decision log.", + } + decision.AddCommand(decisionListCmd(a)) + + parent.AddCommand(change, revision, rule, decision) root.AddCommand(parent) } diff --git a/internal/cmd/policy/decision_list.go b/internal/cmd/policy/decision_list.go new file mode 100644 index 0000000..f392725 --- /dev/null +++ b/internal/cmd/policy/decision_list.go @@ -0,0 +1,284 @@ +package policy + +import ( + "context" + "fmt" + "strings" + "time" + + policyv1grpc "buf.build/gen/go/safedep/api/grpc/go/safedep/services/controltower/policy/v1/policyv1grpc" + packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1" + policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" + drypb "github.com/safedep/dry/api/pb" + "github.com/safedep/dry/log" + "github.com/spf13/cobra" + "google.golang.org/protobuf/types/known/timestamppb" + + "github.com/safedep/cli/internal/app" + "github.com/safedep/cli/internal/cloudpolicy" + "github.com/safedep/cli/internal/duration" + "github.com/safedep/cli/internal/paging" + "github.com/safedep/cli/internal/tui" +) + +const ( + decisionToolPrefix = "DECISION_TOOL_" + decisionOutcomePrefix = "DECISION_OUTCOME_" +) + +type decisionListInput struct { + Since time.Duration + Until time.Duration + Tool string + Outcome string + Revision uint64 + Rule string + Override string + Path string + Package string + Project string + Endpoint string + PageSize uint32 + PageToken string +} + +func decisionListCmd(a *app.App) *cobra.Command { + var in decisionListInput + cmd := &cobra.Command{ + Use: "list", + Short: "List the decisions that the policy made, newest first", + Long: "List the decisions that pmg and the pull request check made with the policy of the tenant, " + + "newest first. Each row says which policy revision and which rule or field decided, and where: " + + "the endpoint and the user for pmg, the repository and the pull request for a check. " + + "--since and --until take a duration before now, as in 7d or 12h. --override takes the name " + + "or the id of a policy override. --package takes a PURL with no version, as in pkg:npm/lodash. " + + "The command reads one page. Pass --page-token to read the next page.", + Args: func(cmd *cobra.Command, args []string) error { + if err := cobra.NoArgs(cmd, args); err != nil { + return err + } + _, err := newDecisionFilters(in, "", time.Now()) + return err + }, + RunE: func(cmd *cobra.Command, _ []string) error { + client, err := a.ControlPlane() + if err != nil { + return err + } + res, err := runDecisionList(cmd.Context(), cloudpolicy.NewPolicyClient(client.Connection()), in, time.Now()) + if err != nil { + return err + } + return a.Output.Print(res) + }, + } + f := cmd.Flags() + f.Var(duration.NewValue(&in.Since, 0), "since", "only decisions in this time before now, as in 7d or 12h") + f.Var(duration.NewValue(&in.Until, 0), "until", "only decisions older than this time before now, as in 1d") + f.StringVar(&in.Tool, "tool", "", "filter: the tool that decided ("+strings.Join(decisionToolTokens(), ", ")+")") + f.StringVar(&in.Outcome, "outcome", "", "filter: what the decision did ("+strings.Join(decisionOutcomeTokens(), ", ")+")") + f.Uint64Var(&in.Revision, "revision", 0, "filter: decisions that applied this policy revision") + f.StringVar(&in.Rule, "rule", "", "filter: decisions that this package rule made, by rule id") + f.StringVar(&in.Override, "override", "", "filter: decisions that this policy override made, by name or id") + f.StringVar(&in.Path, "path", "", "filter: decisions that this field made, as in package_admission.cooldown.min_age") + f.StringVar(&in.Package, "package", "", "filter: decisions about this package in all its versions, as in pkg:npm/lodash") + f.StringVar(&in.Project, "project", "", "filter: decisions at a repository that is bound to this project id") + f.StringVar(&in.Endpoint, "endpoint", "", "filter: decisions that pmg made on this endpoint id") + f.Uint32Var(&in.PageSize, "limit", 0, "page size; server default when 0") + f.StringVar(&in.PageToken, "page-token", "", "continuation token from a prior response") + return cmd +} + +func decisionToolTokens() []string { + return tui.EnumTokens(policyv1.DecisionTool_name, decisionToolPrefix) +} + +func decisionOutcomeTokens() []string { + return tui.EnumTokens(policyv1.DecisionOutcome_name, decisionOutcomePrefix) +} + +func parseDecisionEnum(flag, token string, names map[int32]string, prefix string, tokens []string) (int32, error) { + n, ok := tui.ParseEnumToken(names, prefix, token) + if !ok { + return 0, invalidInputError( + fmt.Errorf("unknown --%s value %q: allowed values are %s", flag, token, strings.Join(tokens, ", ")), + "Retry with one of: "+strings.Join(tokens, ", ")) + } + return n, nil +} + +// parseDecisionPackage reads a PURL with no version. The filter matches every +// version of the package, so a version in the PURL would mislead. +func parseDecisionPackage(purl string) (*packagev1.Package, error) { + help := "Give a PURL with no version, as in --package pkg:npm/lodash" + if !strings.HasPrefix(purl, "pkg:") { + return nil, invalidInputError(fmt.Errorf("--package %q is not a PURL", purl), help) + } + parsed, err := drypb.NewPurlPackageVersion(purl) + if err != nil { + return nil, invalidInputError(fmt.Errorf("--package %q: %w", purl, err), help) + } + pv := parsed.PackageVersion() + switch { + case pv.GetPackage().GetEcosystem() == packagev1.Ecosystem_ECOSYSTEM_UNSPECIFIED: + return nil, invalidInputError(fmt.Errorf("--package %q: unknown ecosystem", purl), help) + case pv.GetVersion() != "": + return nil, invalidInputError( + fmt.Errorf("--package %q has a version: the filter matches every version of the package", purl), help) + case pv.GetPackage().GetName() == "": + return nil, invalidInputError(fmt.Errorf("--package %q has no name", purl), help) + } + return pv.GetPackage(), nil +} + +// newDecisionFilters builds the request filters from the flags. overrideID is +// the id that --override resolved to. It returns nil when no filter is set. +func newDecisionFilters( + in decisionListInput, + overrideID string, + now time.Time, +) (*policyv1.ListPolicyDecisionsRequest_Filters, error) { + filters := &policyv1.ListPolicyDecisionsRequest_Filters{} + set := false + after, before, err := duration.Window(in.Since, in.Until, now) + if err != nil { + return nil, err + } + if after != nil { + filters.SetDecidedAfter(timestamppb.New(*after)) + set = true + } + if before != nil { + filters.SetDecidedBefore(timestamppb.New(*before)) + set = true + } + if in.Tool != "" { + n, err := parseDecisionEnum("tool", in.Tool, policyv1.DecisionTool_name, decisionToolPrefix, decisionToolTokens()) + if err != nil { + return nil, err + } + filters.SetTool(policyv1.DecisionTool(n)) + set = true + } + if in.Outcome != "" { + n, err := parseDecisionEnum("outcome", in.Outcome, policyv1.DecisionOutcome_name, decisionOutcomePrefix, + decisionOutcomeTokens()) + if err != nil { + return nil, err + } + filters.SetOutcome(policyv1.DecisionOutcome(n)) + set = true + } + if in.Revision > 0 { + filters.SetRevision(in.Revision) + set = true + } + if in.Package != "" { + pkg, err := parseDecisionPackage(in.Package) + if err != nil { + return nil, err + } + filters.SetPackage(pkg) + set = true + } + for _, text := range []struct { + value string + apply func(string) + }{ + {in.Rule, filters.SetRuleId}, + {overrideID, filters.SetOverrideId}, + {in.Path, filters.SetPath}, + {in.Project, filters.SetProjectId}, + {in.Endpoint, filters.SetEndpointId}, + } { + if text.value != "" { + text.apply(text.value) + set = true + } + } + if !set { + return nil, nil + } + return filters, nil +} + +// runDecisionList reads one page of decisions. It reads the policy only to +// resolve --override and to name the overrides that the decisions cite. It +// reads the policy at --revision when it is set, because the decisions of +// that revision used its overrides. +func runDecisionList( + ctx context.Context, + client policyv1grpc.PolicyServiceClient, + in decisionListInput, + now time.Time, +) (*decisionListResult, error) { + if _, err := newDecisionFilters(in, "", now); err != nil { + return nil, err + } + var snapshot *policyv1.Policy + overrideID := "" + if in.Override != "" { + _, p, err := readPolicyAt(ctx, client, in.Revision) + if err != nil { + return nil, err + } + snapshot = p + o, err := findOverrideByNameOrID(p, in.Override) + if err != nil { + return nil, err + } + overrideID = o.GetOverrideId() + } + filters, err := newDecisionFilters(in, overrideID, now) + if err != nil { + return nil, err + } + req := &policyv1.ListPolicyDecisionsRequest{} + req.SetPagination(paging.NewPaginationRequest(in.PageSize, in.PageToken)) + if filters != nil { + req.SetFilters(filters) + } + res, err := client.ListPolicyDecisions(ctx, req) + if err != nil { + return nil, mapDecisionListError(err) + } + + names := map[string]string{} + if citesOverride(res.GetDecisions()) { + if snapshot == nil { + _, p, err := readPolicyAt(ctx, client, in.Revision) + if err != nil { + // The names only label the output. The ids stay, so the list is + // still correct without them. + log.Warnf("policy decision list: read the override names: %v", err) + } + snapshot = p + } + names = overrideNames(snapshot) + } + return newDecisionListResult(res, names), nil +} + +func citesOverride(decisions []*policyv1.PolicyDecision) bool { + for _, d := range decisions { + if d.GetCitation().GetOverrideId() != "" { + return true + } + } + return false +} + +// findOverrideByNameOrID accepts the id too, because the JSON output of a +// decision gives the id of its override. A name wins over an id. +func findOverrideByNameOrID(p *policyv1.Policy, ref string) (*policyv1.Override, error) { + o, err := findOverride(p, ref) + if err == nil { + return o, nil + } + for _, o := range p.GetOverrides() { + if o.GetOverrideId() == ref { + return o, nil + } + } + return nil, err +} diff --git a/internal/cmd/policy/decision_test.go b/internal/cmd/policy/decision_test.go new file mode 100644 index 0000000..9d2ff69 --- /dev/null +++ b/internal/cmd/policy/decision_test.go @@ -0,0 +1,565 @@ +package policy + +import ( + "context" + "encoding/json" + "strings" + "testing" + "time" + + messagescontroltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/controltower/v1" + errorv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/error/v1" + packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/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" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + "google.golang.org/genproto/googleapis/rpc/errdetails" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/timestamppb" + + "github.com/safedep/cli/internal/cloudpolicy/mocks" +) + +const prSHA = "0123456789abcdef0123456789abcdef01234567" + +func citation(revision, defaultRevision uint64, path, ruleID, overrideID string, layer recordv1.Layer) *recordv1.DecisionCitation { + c := &recordv1.DecisionCitation{} + c.SetRevision(revision) + c.SetDefaultPolicyRevision(defaultRevision) + c.SetPath(path) + c.SetRuleId(ruleID) + c.SetOverrideId(overrideID) + c.SetLayer(layer) + return c +} + +func endpointPoint(hostname, username string) *messagescontroltowerv1.EndpointPoint { + e := &messagescontroltowerv1.EndpointPoint{} + e.SetEndpointId("01JEP0000000000000000000") + if hostname != "" { + e.SetHostname(hostname) + } + if username != "" { + e.SetUsername(username) + } + e.SetInvocationId("inv_1") + return e +} + +func atEndpoint(e *messagescontroltowerv1.EndpointPoint) *messagescontroltowerv1.DecisionPoint { + p := &messagescontroltowerv1.DecisionPoint{} + p.SetEndpoint(e) + return p +} + +func atPullRequest(repo string, number uint32, url string) *messagescontroltowerv1.DecisionPoint { + pr := &messagescontroltowerv1.PullRequestPoint{} + pr.SetPlatform(messagescontroltowerv1.IntegrationType_INTEGRATION_TYPE_GITHUB) + pr.SetRepositoryId("123") + pr.SetRepositoryName(repo) + pr.SetNumber(number) + pr.SetHeadCommitSha(prSHA) + if url != "" { + pr.SetUrl(url) + } + p := &messagescontroltowerv1.DecisionPoint{} + p.SetPullRequest(pr) + return p +} + +func atScan(repo, ref string) *messagescontroltowerv1.DecisionPoint { + s := &messagescontroltowerv1.ScanPoint{} + s.SetRepositoryName(repo) + if ref != "" { + s.SetRef(ref) + } + s.SetCommitSha(prSHA) + s.SetUrl("https://ci.example.com/run/9") + p := &messagescontroltowerv1.DecisionPoint{} + p.SetScan(s) + return p +} + +func decision( + id string, + tool policyv1.DecisionTool, + outcome policyv1.DecisionOutcome, + name, version string, + c *recordv1.DecisionCitation, + point *messagescontroltowerv1.DecisionPoint, +) *policyv1.PolicyDecision { + pkg := &packagev1.Package{} + pkg.SetEcosystem(packagev1.Ecosystem_ECOSYSTEM_NPM) + pkg.SetName(name) + pv := &packagev1.PackageVersion{} + pv.SetPackage(pkg) + pv.SetVersion(version) + d := &policyv1.PolicyDecision{} + d.SetDecisionId(id) + d.SetDecidedAt(timestamppb.New(time.Now().Add(-5 * time.Minute))) + d.SetTool(tool) + d.SetOutcome(outcome) + d.SetPackageVersion(pv) + d.SetCitation(c) + if point != nil { + d.SetDecisionPoint(point) + } + return d +} + +// ruleDecisions are a pmg block and a failed pull request check that one +// package rule made. +func ruleDecisions() []*policyv1.PolicyDecision { + return []*policyv1.PolicyDecision{ + decision("d_pmg", policyv1.DecisionTool_DECISION_TOOL_PMG, policyv1.DecisionOutcome_DECISION_OUTCOME_BLOCKED, + "evil", "1.0.0", citation(47, 12, "", "r_9c1x4kq7dm", "", recordv1.Layer_LAYER_UNSPECIFIED), + atEndpoint(endpointPoint("dev-laptop", "alice"))), + decision("d_pr", policyv1.DecisionTool_DECISION_TOOL_PULL_REQUEST_CHECK, + policyv1.DecisionOutcome_DECISION_OUTCOME_FAILED, "evil", "1.0.1", + citation(47, 12, "", "r_9c1x4kq7dm", "", recordv1.Layer_LAYER_UNSPECIFIED), + atPullRequest("acme/payments-api", 42, "https://github.com/acme/payments-api/runs/7")), + } +} + +func decisionsResponse(next string, decisions ...*policyv1.PolicyDecision) *policyv1.ListPolicyDecisionsResponse { + page := &messagescontroltowerv1.PaginationResponse{} + page.SetNextPageToken(next) + res := &policyv1.ListPolicyDecisionsResponse{} + res.SetDecisions(decisions) + res.SetPagination(page) + return res +} + +func TestNewDecisionFilters(t *testing.T) { + day := 24 * time.Hour + tests := []struct { + name string + in decisionListInput + overrideID string + check func(*testing.T, *policyv1.ListPolicyDecisionsRequest_Filters) + wantErr string + }{ + { + name: "no filters", + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { assert.Nil(t, f) }, + }, + { + name: "time range", + in: decisionListInput{Since: 7 * day, Until: day}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, fixedNow.Add(-7*day), f.GetDecidedAfter().AsTime()) + assert.Equal(t, fixedNow.Add(-day), f.GetDecidedBefore().AsTime()) + }, + }, + { + name: "tool pmg", + in: decisionListInput{Tool: "pmg"}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, policyv1.DecisionTool_DECISION_TOOL_PMG, f.GetTool()) + }, + }, + { + name: "tool pull request check", + in: decisionListInput{Tool: "pull-request-check"}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, policyv1.DecisionTool_DECISION_TOOL_PULL_REQUEST_CHECK, f.GetTool()) + }, + }, + { + name: "outcome", + in: decisionListInput{Outcome: "allowed"}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, policyv1.DecisionOutcome_DECISION_OUTCOME_ALLOWED, f.GetOutcome()) + }, + }, + { + name: "revision", + in: decisionListInput{Revision: 47}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, uint64(47), f.GetRevision()) + }, + }, + { + name: "rule", + in: decisionListInput{Rule: "r_9c1x4kq7dm"}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, "r_9c1x4kq7dm", f.GetRuleId()) + }, + }, + { + name: "override id from the resolved name", + in: decisionListInput{Override: "payments"}, + overrideID: "ov_pay", + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, "ov_pay", f.GetOverrideId()) + }, + }, + { + name: "path", + in: decisionListInput{Path: "package_admission.cooldown.min_age"}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, "package_admission.cooldown.min_age", f.GetPath()) + }, + }, + { + name: "package", + in: decisionListInput{Package: "pkg:npm/%40acme/ui"}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, packagev1.Ecosystem_ECOSYSTEM_NPM, f.GetPackage().GetEcosystem()) + assert.Equal(t, "@acme/ui", f.GetPackage().GetName()) + }, + }, + { + name: "project", + in: decisionListInput{Project: "prj_1"}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, "prj_1", f.GetProjectId()) + }, + }, + { + name: "endpoint", + in: decisionListInput{Endpoint: "01JEP0000000000000000000"}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { + assert.Equal(t, "01JEP0000000000000000000", f.GetEndpointId()) + }, + }, + {name: "unknown tool", in: decisionListInput{Tool: "npm"}, wantErr: "unknown --tool value"}, + {name: "unknown outcome", in: decisionListInput{Outcome: "denied"}, wantErr: "unknown --outcome value"}, + {name: "package with a version", in: decisionListInput{Package: "pkg:npm/lodash@4.17.21"}, wantErr: "has a version"}, + {name: "package not a PURL", in: decisionListInput{Package: "lodash"}, wantErr: "is not a PURL"}, + {name: "package of an unknown ecosystem", in: decisionListInput{Package: "pkg:nope/x"}, wantErr: "unknown ecosystem"}, + {name: "until after since", in: decisionListInput{Since: time.Hour, Until: 2 * time.Hour}, wantErr: "--until must be shorter"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f, err := newDecisionFilters(tt.in, tt.overrideID, fixedNow) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + ue, ok := usefulerror.AsUsefulError(err) + require.True(t, ok) + assert.Equal(t, usefulerror.ErrBadRequest, ue.Code()) + return + } + require.NoError(t, err) + tt.check(t, f) + }) + } +} + +func TestRunDecisionList_RuleListsBothToolsWithThePlace(t *testing.T) { + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.MatchedBy(func(r *policyv1.ListPolicyDecisionsRequest) bool { + return r.GetFilters().GetRuleId() == "r_9c1x4kq7dm" + })).Return(decisionsResponse("", ruleDecisions()...), nil) + + got, err := runDecisionList(context.Background(), client, decisionListInput{Rule: "r_9c1x4kq7dm"}, fixedNow) + require.NoError(t, err) + + table := got.RenderTable() + for _, want := range []string{ + "5m ago", "pmg", "npm/evil@1.0.0", "blocked", "rule r_9c1x4kq7dm", "dev-laptop (alice)", + "pull-request-check", "failed", "acme/payments-api#42", "https://github.com/acme/payments-api/runs/7", + "LINK", "2 decisions", + } { + assert.Contains(t, table, want) + } + plain := strings.Split(got.RenderPlain(), "\n") + require.Len(t, plain, 3) + assert.Contains(t, plain[1], "\td_pmg\tpmg\tblocked\tnpm\tevil\t1.0.0\t47\t12\t\t\tr_9c1x4kq7dm\t\tendpoint\tdev-laptop (alice)\t") + assert.Contains(t, plain[2], "\tpull-request\tacme/payments-api#42\thttps://github.com/acme/payments-api/runs/7") +} + +func TestRunDecisionList_CooldownShowsThePathAndTheDefaultRevision(t *testing.T) { + cooldown := decision("d_cool", policyv1.DecisionTool_DECISION_TOOL_PMG, policyv1.DecisionOutcome_DECISION_OUTCOME_BLOCKED, + "fresh", "0.0.1", citation(47, 12, "package_admission.cooldown.min_age", "", "", recordv1.Layer_LAYER_DEFAULT_POLICY), + atEndpoint(endpointPoint("dev-laptop", "alice"))) + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.MatchedBy(func(r *policyv1.ListPolicyDecisionsRequest) bool { + return r.GetFilters().GetPath() == "package_admission.cooldown.min_age" + })).Return(decisionsResponse("", cooldown), nil) + + got, err := runDecisionList(context.Background(), client, + decisionListInput{Path: "package_admission.cooldown.min_age"}, fixedNow) + require.NoError(t, err) + table := got.RenderTable() + assert.Contains(t, table, "package_admission.cooldown.min_age · default policy") + assert.Contains(t, table, "47 · default 12") + assert.NotContains(t, table, "LINK", "no decision has a link") + assert.Contains(t, got.RenderPlain(), "\t47\t12\tdefault-policy\tpackage_admission.cooldown.min_age\t") +} + +func TestRunDecisionList_Override(t *testing.T) { + overrideDecision := decision("d_ov", policyv1.DecisionTool_DECISION_TOOL_PMG, policyv1.DecisionOutcome_DECISION_OUTCOME_BLOCKED, + "fresh", "0.0.1", citation(47, 12, "package_admission.cooldown.min_age", "", "ov_pay", recordv1.Layer_LAYER_OVERRIDE), + atEndpoint(endpointPoint("dev-laptop", "alice"))) + tests := []struct { + name string + in decisionListInput + expect func(*mocks.PolicyServiceClientMock) + wantID string + wantCell string + wantErr string + }{ + { + name: "by name at head", + in: decisionListInput{Override: "payments"}, + expect: func(c *mocks.PolicyServiceClientMock) { + c.EXPECT().GetPolicy(mock.Anything, mock.Anything).Return(headResponse(), nil).Once() + }, + wantID: "ov_pay", + wantCell: "package_admission.cooldown.min_age · override payments", + }, + { + name: "by id", + in: decisionListInput{Override: "ov_pay"}, + expect: func(c *mocks.PolicyServiceClientMock) { + c.EXPECT().GetPolicy(mock.Anything, mock.Anything).Return(headResponse(), nil).Once() + }, + wantID: "ov_pay", + wantCell: "override payments", + }, + { + name: "by name at the revision", + in: decisionListInput{Override: "payments", Revision: 47}, + expect: func(c *mocks.PolicyServiceClientMock) { + res := &policyv1.GetPolicyRevisionResponse{} + res.SetPolicy(headPolicy()) + c.EXPECT().GetPolicyRevision(mock.Anything, mock.MatchedBy(func(r *policyv1.GetPolicyRevisionRequest) bool { + return r.GetRevision() == 47 + })).Return(res, nil).Once() + }, + wantID: "ov_pay", + wantCell: "override payments", + }, + { + name: "unknown name", + in: decisionListInput{Override: "nope"}, + expect: func(c *mocks.PolicyServiceClientMock) { + c.EXPECT().GetPolicy(mock.Anything, mock.Anything).Return(headResponse(), nil).Once() + }, + wantErr: `no policy override is named "nope"`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client := mocks.NewPolicyServiceClientMock(t) + tt.expect(client) + if tt.wantErr == "" { + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.MatchedBy(func(r *policyv1.ListPolicyDecisionsRequest) bool { + return r.GetFilters().GetOverrideId() == tt.wantID && r.GetFilters().GetRevision() == tt.in.Revision + })).Return(decisionsResponse("", overrideDecision), nil) + } + got, err := runDecisionList(context.Background(), client, tt.in, fixedNow) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + require.NoError(t, err) + assert.Contains(t, got.RenderTable(), tt.wantCell) + assert.Contains(t, got.RenderPlain(), "\tov_pay\t", "plain keeps the id") + }) + } +} + +func TestRunDecisionList_OverrideNameReadFailureKeepsTheID(t *testing.T) { + d := decision("d_ov", policyv1.DecisionTool_DECISION_TOOL_PMG, policyv1.DecisionOutcome_DECISION_OUTCOME_BLOCKED, + "evil", "1.0.0", citation(47, 12, "", "r_1", "ov_gone", recordv1.Layer_LAYER_UNSPECIFIED), + atEndpoint(endpointPoint("dev-laptop", ""))) + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(decisionsResponse("", d), nil) + client.EXPECT().GetPolicy(mock.Anything, mock.Anything).Return(nil, status.Error(codes.Unavailable, "down")) + + got, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + require.NoError(t, err) + assert.Contains(t, got.RenderTable(), "rule r_1 · override ov_gone") +} + +func TestDescribePlace(t *testing.T) { + ci := endpointPoint("runner-7", "runner") + ciContext := &messagescontroltowerv1.EndpointCIContext{} + ciContext.SetRepository("acme/api") + ciContext.SetBranch("main") + ci.SetCi(ciContext) + + pod := endpointPoint("api-7d9f-x2", "") + k8s := &messagescontroltowerv1.EndpointKubernetesContext{} + k8s.SetNamespace("payments") + k8s.SetWorkloadName("api") + pod.SetKubernetes(k8s) + + laptop := endpointPoint("dev-laptop", "alice") + agent := &messagescontroltowerv1.EndpointAgentContext{} + agent.SetAgentName("claude-code") + laptop.SetAgent(agent) + + tests := []struct { + name string + point *messagescontroltowerv1.DecisionPoint + wantKind string + wantPlace string + wantURL string + }{ + {"endpoint", atEndpoint(endpointPoint("dev-laptop", "alice")), placeEndpoint, "dev-laptop (alice)", ""}, + {"endpoint with no hostname", atEndpoint(endpointPoint("", "")), placeEndpoint, "01JEP0000000000000000000", ""}, + {"endpoint in CI", atEndpoint(ci), placeEndpoint, "runner-7 (runner) · ci acme/api", ""}, + {"endpoint in Kubernetes", atEndpoint(pod), placeEndpoint, "api-7d9f-x2 · k8s payments/api", ""}, + {"endpoint with an agent", atEndpoint(laptop), placeEndpoint, "dev-laptop (alice) · agent claude-code", ""}, + { + "pull request", atPullRequest("acme/api", 42, "https://github.com/acme/api/pull/42"), + placePullRequest, "acme/api#42", "https://github.com/acme/api/pull/42", + }, + {"pull request with no link", atPullRequest("acme/api", 42, ""), placePullRequest, "acme/api#42", ""}, + {"scan of a branch", atScan("acme/api", "main"), placeScan, "acme/api@main", "https://ci.example.com/run/9"}, + {"scan of a commit", atScan("acme/api", ""), placeScan, "acme/api@0123456789ab", "https://ci.example.com/run/9"}, + {"no place", nil, "", "", ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + kind, place, url := describePlace(tt.point) + assert.Equal(t, tt.wantKind, kind) + assert.Equal(t, tt.wantPlace, place) + assert.Equal(t, tt.wantURL, url) + }) + } +} + +func TestRunDecisionList_EscapesServerText(t *testing.T) { + d := decision("d_x", policyv1.DecisionTool_DECISION_TOOL_PMG, policyv1.DecisionOutcome_DECISION_OUTCOME_BLOCKED, + "evil\x1b]0;owned\x07", "1.0.0", citation(47, 12, "a\nb", "", "", recordv1.Layer_LAYER_BASE), + atEndpoint(endpointPoint("host\u202e", "mallory\tx"))) + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(decisionsResponse("", d), nil) + + got, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + require.NoError(t, err) + for _, out := range []string{got.RenderTable(), got.RenderPlain()} { + assert.NotContains(t, out, "\x1b") + assert.NotContains(t, out, "\u202e") + assert.Contains(t, out, `evil\x1b]0;owned\x07`) + assert.Contains(t, out, `a\nb`) + assert.Contains(t, out, `mallory\tx`) + } + assert.Len(t, strings.Split(got.RenderPlain(), "\n"), 2, "a line break in a value adds no row") +} + +func TestRunDecisionList_Empty(t *testing.T) { + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(decisionsResponse(""), nil) + + got, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + require.NoError(t, err) + assert.Contains(t, got.RenderTable(), "No policy decisions match") + assert.Len(t, strings.Split(got.RenderPlain(), "\n"), 1, "plain writes only the header") + b, err := got.RenderJSON() + require.NoError(t, err) + assert.JSONEq(t, `{"decisions": []}`, string(b)) +} + +func TestRunDecisionList_Paging(t *testing.T) { + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.MatchedBy(func(r *policyv1.ListPolicyDecisionsRequest) bool { + return r.GetPagination().GetPageSize() == 2 && r.GetPagination().GetPageToken() == "tok_1" && !r.HasFilters() + })).Return(decisionsResponse("tok_2", ruleDecisions()...), nil) + + got, err := runDecisionList(context.Background(), client, decisionListInput{PageSize: 2, PageToken: "tok_1"}, fixedNow) + require.NoError(t, err) + assert.Contains(t, got.RenderTable(), "2 decisions. More available: --page-token tok_2") + b, err := got.RenderJSON() + require.NoError(t, err) + assert.Contains(t, string(b), `"next_page_token": "tok_2"`) +} + +func TestRunDecisionList_JSONHasTheRawMessages(t *testing.T) { + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(decisionsResponse("", ruleDecisions()...), nil) + + got, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + require.NoError(t, err) + b, err := got.RenderJSON() + require.NoError(t, err) + + var out struct { + Decisions []map[string]any `json:"decisions"` + } + require.NoError(t, json.Unmarshal(b, &out)) + require.Len(t, out.Decisions, 2) + pmg := out.Decisions[0] + assert.Equal(t, "d_pmg", pmg["decision_id"]) + assert.Equal(t, "DECISION_TOOL_PMG", pmg["tool"]) + assert.Equal(t, "DECISION_OUTCOME_BLOCKED", pmg["outcome"]) + assert.Equal(t, "r_9c1x4kq7dm", pmg["citation"].(map[string]any)["rule_id"]) + assert.Equal(t, "inv_1", pmg["decision_point"].(map[string]any)["endpoint"].(map[string]any)["invocation_id"]) + pr := out.Decisions[1]["decision_point"].(map[string]any)["pull_request"].(map[string]any) + assert.Equal(t, prSHA, pr["head_commit_sha"]) + assert.Equal(t, "INTEGRATION_TYPE_GITHUB", pr["platform"]) +} + +func TestMapDecisionListError(t *testing.T) { + badToken := usefulerror.NewStatus(codes.InvalidArgument, "the page token is not valid"). + WithDetail(&errdetails.BadRequest{FieldViolations: []*errdetails.BadRequest_FieldViolation{ + {Field: "pagination.page_token", Description: "the page token is not valid"}, + }}).Err() + tests := []struct { + name string + err error + wantCode string + wantHuman string + }{ + { + name: "feature not enabled", + err: usefulerror.NewStatus(codes.PermissionDenied, "no entitlement"). + WithReason(errorv1.ErrorReason_ERROR_REASON_ENTITLEMENT_NOT_AVAILABLE).Err(), + wantCode: usefulerror.ErrMissingEntitlements, + wantHuman: "Policy management is not enabled for this tenant", + }, + { + name: "role cannot read", + err: status.Error(codes.PermissionDenied, "denied"), + wantCode: usefulerror.ErrAuthorizationFailed, + wantHuman: "Your role cannot read the policy decision log", + }, + { + name: "bad page token", + err: badToken, + wantCode: usefulerror.ErrBadRequest, + wantHuman: "The page token is not valid", + }, + { + name: "bad filter", + err: status.Error(codes.InvalidArgument, "decided_after must be before decided_before\x1b[2J"), + wantCode: usefulerror.ErrBadRequest, + wantHuman: `The server refused the filters: decided_after must be before decided_before\x1b[2J`, + }, + { + name: "server without the RPC", + err: status.Error(codes.Unimplemented, "unknown method"), + wantCode: usefulerror.ErrServiceUnavailable, + wantHuman: "SafeDep Cloud does not have the policy decision log yet", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(nil, tt.err) + _, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + ue, ok := usefulerror.AsUsefulError(err) + require.True(t, ok) + assert.Equal(t, tt.wantCode, ue.Code()) + assert.Equal(t, tt.wantHuman, ue.HumanError()) + assert.ErrorIs(t, err, tt.err) + }) + } + + t.Run("other errors keep the cause", func(t *testing.T) { + cause := status.Error(codes.Internal, "boom") + err := mapDecisionListError(cause) + assert.ErrorIs(t, err, cause) + assert.Contains(t, err.Error(), "policy decision list") + }) +} diff --git a/internal/cmd/policy/decision_view.go b/internal/cmd/policy/decision_view.go new file mode 100644 index 0000000..b77d72e --- /dev/null +++ b/internal/cmd/policy/decision_view.go @@ -0,0 +1,253 @@ +package policy + +import ( + "encoding/json" + "fmt" + "strings" + "time" + + messagescontroltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/controltower/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/tui/table" + "google.golang.org/protobuf/encoding/protojson" + + "github.com/safedep/cli/internal/cloudpolicy" + "github.com/safedep/cli/internal/tui" +) + +const ( + placeEndpoint = "endpoint" + placePullRequest = "pull-request" + placeScan = "scan" + + // shortSHA is the length of a commit hash in a table cell. + shortSHA = 12 +) + +// decisionView is one decision as the table and the plain output show it. +// The JSON output does not use it: it writes the messages of the server. +type decisionView struct { + ID string + DecidedAt *time.Time + Tool string + Outcome string + Ecosystem string + Name string + Version string + Revision uint64 + DefaultRev uint64 + Layer recordv1.Layer + Path string + RuleID string + OverrideID string + // override is the name of the override, or its id when the policy + // that the command read does not have it. + override string + PlaceKind string + Place string + URL string +} + +type decisionListResult struct { + decisions []*policyv1.PolicyDecision + views []decisionView + NextPageToken string +} + +func newDecisionListResult(res *policyv1.ListPolicyDecisionsResponse, overrideNames map[string]string) *decisionListResult { + out := &decisionListResult{ + decisions: res.GetDecisions(), + views: make([]decisionView, 0, len(res.GetDecisions())), + NextPageToken: res.GetPagination().GetNextPageToken(), + } + for _, d := range res.GetDecisions() { + out.views = append(out.views, newDecisionView(d, overrideNames)) + } + return out +} + +func newDecisionView(d *policyv1.PolicyDecision, overrideNames map[string]string) decisionView { + c := d.GetCitation() + pv := d.GetPackageVersion() + v := decisionView{ + ID: d.GetDecisionId(), + DecidedAt: cloudpolicy.TimeOf(d.GetDecidedAt()), + Tool: tui.EnumToken(d.GetTool().String(), decisionToolPrefix), + Outcome: tui.EnumToken(d.GetOutcome().String(), decisionOutcomePrefix), + Ecosystem: tui.EnumToken(pv.GetPackage().GetEcosystem().String(), "ECOSYSTEM_"), + Name: pv.GetPackage().GetName(), + Version: pv.GetVersion(), + Revision: c.GetRevision(), + DefaultRev: c.GetDefaultPolicyRevision(), + Layer: c.GetLayer(), + Path: c.GetPath(), + RuleID: c.GetRuleId(), + OverrideID: c.GetOverrideId(), + override: c.GetOverrideId(), + } + if name := overrideNames[v.OverrideID]; name != "" { + v.override = name + } + v.PlaceKind, v.Place, v.URL = describePlace(d.GetDecisionPoint()) + return v +} + +// describePlace names the place of a decision in one short cell. For pmg it +// is the hostname and the user, and one hint of the context: the CI +// repository, else the Kubernetes namespace and workload, else the agent. In +// CI and in Kubernetes the hostname is the name of a runner or a Pod, so the +// repository or the workload is the useful name. One hint keeps the cell +// short. The JSON output has the full context. +func describePlace(p *messagescontroltowerv1.DecisionPoint) (kind, place, url string) { + switch { + case p.HasEndpoint(): + e := p.GetEndpoint() + host := e.GetHostname() + if host == "" { + host = e.GetEndpointId() + } + if e.GetUsername() != "" { + host += " (" + e.GetUsername() + ")" + } + parts := []string{host} + k := e.GetKubernetes() + switch { + case e.GetCi().GetRepository() != "": + parts = append(parts, "ci "+e.GetCi().GetRepository()) + case k.GetNamespace() != "" || k.GetWorkloadName() != "": + parts = append(parts, "k8s "+strings.Trim(k.GetNamespace()+"/"+k.GetWorkloadName(), "/")) + case e.GetAgent().GetAgentName() != "": + parts = append(parts, "agent "+e.GetAgent().GetAgentName()) + } + return placeEndpoint, strings.Join(parts, sep), "" + case p.HasPullRequest(): + pr := p.GetPullRequest() + return placePullRequest, fmt.Sprintf("%s#%d", pr.GetRepositoryName(), pr.GetNumber()), pr.GetUrl() + case p.HasScan(): + s := p.GetScan() + ref := s.GetRef() + if ref == "" { + ref = s.GetCommitSha() + if len(ref) > shortSHA { + ref = ref[:shortSHA] + } + } + return placeScan, s.GetRepositoryName() + "@" + ref, s.GetUrl() + } + return "", "", "" +} + +func (v decisionView) pkg() string { + text := v.Ecosystem + "/" + v.Name + if v.Version != "" { + text += "@" + v.Version + } + return text +} + +// decidedBy names the rule or the field that decided, and the layer or the +// override that set it. +func (v decisionView) decidedBy() string { + var parts []string + switch { + case v.RuleID != "": + parts = append(parts, "rule "+v.RuleID) + case v.Path != "": + parts = append(parts, v.Path) + } + switch { + case v.override != "": + parts = append(parts, "override "+v.override) + case v.Layer == recordv1.Layer_LAYER_DEFAULT_POLICY: + parts = append(parts, "default policy") + case v.Layer == recordv1.Layer_LAYER_BASE: + parts = append(parts, "base") + } + return cloudpolicy.EmptyDash(strings.Join(parts, sep)) +} + +// revisionCell gives the tenant revision, and the default policy revision +// when the default policy decided or the tenant has no revision. +func (v decisionView) revisionCell() string { + var parts []string + if v.Revision > 0 { + parts = append(parts, fmt.Sprint(v.Revision)) + } + if v.DefaultRev > 0 && (v.Layer == recordv1.Layer_LAYER_DEFAULT_POLICY || v.Revision == 0) { + parts = append(parts, fmt.Sprintf("default %d", v.DefaultRev)) + } + return cloudpolicy.EmptyDash(strings.Join(parts, sep)) +} + +func (v decisionView) layer() string { + if v.Layer == recordv1.Layer_LAYER_UNSPECIFIED { + return "" + } + return tui.EnumToken(v.Layer.String(), "LAYER_") +} + +// RenderJSON writes the decisions as the server sent them, in protojson with +// the field names of the contract. +func (r *decisionListResult) RenderJSON() ([]byte, error) { + decisions := make([]json.RawMessage, 0, len(r.decisions)) + for _, d := range r.decisions { + b, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(d) + if err != nil { + return nil, fmt.Errorf("policy decision list: encode decision %s: %w", d.GetDecisionId(), err) + } + decisions = append(decisions, b) + } + return json.MarshalIndent(struct { + Decisions []json.RawMessage `json:"decisions"` + NextPageToken string `json:"next_page_token,omitempty"` + }{decisions, r.NextPageToken}, "", " ") +} + +func (r *decisionListResult) RenderTable() string { + now := time.Now() + withLink := false + for _, v := range r.views { + withLink = withLink || v.URL != "" + } + headers := []string{"WHEN", "TOOL", "PACKAGE", "OUTCOME", "DECIDED BY", "REV", "WHERE"} + if withLink { + headers = append(headers, "LINK") + } + rows := make([][]string, 0, len(r.views)) + for _, v := range r.views { + row := []string{ + humanTimeCell(v.DecidedAt, now), v.Tool, v.pkg(), v.Outcome, v.decidedBy(), v.revisionCell(), + cloudpolicy.EmptyDash(v.Place), + } + if withLink { + row = append(row, cloudpolicy.EmptyDash(v.URL)) + } + rows = append(rows, row) + } + t := table.New(). + Title("Policy decisions"). + Headers(headers...). + Rows(tui.EscapeCells(rows)...). + EmptyMessage("No policy decisions match. Try a wider --since window or fewer filters.") + if len(rows) > 0 { + footer := fmt.Sprintf("%d %s", len(rows), cloudpolicy.Plural(len(rows), "decision", "decisions")) + if r.NextPageToken != "" { + footer += ". More available: --page-token " + r.NextPageToken + } + t = t.Footer(footer) + } + return t.Render() +} + +func (r *decisionListResult) RenderPlain() string { + var b strings.Builder + b.WriteString("decided_at\tdecision_id\ttool\toutcome\tecosystem\tpackage\tversion\trevision\t" + + "default_policy_revision\tlayer\tpath\trule_id\toverride_id\tplace_kind\tplace\turl") + for _, v := range r.views { + b.WriteString("\n" + tui.PlainRow(cloudpolicy.RFC3339(v.DecidedAt), v.ID, v.Tool, v.Outcome, v.Ecosystem, + v.Name, v.Version, fmt.Sprint(v.Revision), fmt.Sprint(v.DefaultRev), v.layer(), v.Path, v.RuleID, + v.OverrideID, v.PlaceKind, v.Place, v.URL)) + } + return b.String() +} diff --git a/internal/cmd/policy/errors.go b/internal/cmd/policy/errors.go index c1f8c6a..5b3e326 100644 --- a/internal/cmd/policy/errors.go +++ b/internal/cmd/policy/errors.go @@ -5,8 +5,11 @@ import ( errorv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/error/v1" "github.com/safedep/dry/usefulerror" + "google.golang.org/genproto/googleapis/rpc/errdetails" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + + "github.com/safedep/cli/internal/tui" ) func newPolicyError(code, humanError, help string, cause error) error { @@ -89,3 +92,49 @@ func mapRuleError(op, ruleID string, err error) error { } return cause } + +// mapDecisionListError gives the recovery for the errors of the decision +// log: a tenant without policy management, a role that cannot read the +// policy, a bad filter or page token, and a server that does not have the +// RPC yet. +func mapDecisionListError(err error) error { + cause := fmt.Errorf("policy decision list: %w", err) + if reason, _ := usefulerror.ReasonOf(err); reason == errorv1.ErrorReason_ERROR_REASON_ENTITLEMENT_NOT_AVAILABLE { + return newPolicyError(usefulerror.ErrMissingEntitlements, + "Policy management is not enabled for this tenant", + "See the plans at https://safedep.io/pricing, or ask SafeDep support to enable it", + cause) + } + st, ok := status.FromError(err) + if !ok { + return cause + } + switch st.Code() { + case codes.PermissionDenied: + return newPolicyError(usefulerror.ErrAuthorizationFailed, + "Your role cannot read the policy decision log", + "Ask an Admin of the tenant for a role that can read the policy", + cause) + case codes.InvalidArgument: + if bad, ok := usefulerror.DetailAs[*errdetails.BadRequest](err); ok { + for _, v := range bad.GetFieldViolations() { + if v.GetField() == "pagination.page_token" { + return newPolicyError(usefulerror.ErrBadRequest, + "The page token is not valid", + "Run the command again without --page-token", + cause) + } + } + } + return newPolicyError(usefulerror.ErrBadRequest, + "The server refused the filters: "+tui.EscapeLine(st.Message()), + "See the filters: safedep policy decision list --help", + cause) + case codes.Unimplemented: + return newPolicyError(usefulerror.ErrServiceUnavailable, + "SafeDep Cloud does not have the policy decision log yet", + "Try again after the next SafeDep Cloud release", + cause) + } + return cause +} From ddaa59ade7a23886d9da431b046fce69ff3b4567 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 18:59:40 +0000 Subject: [PATCH 4/4] fix(policy): accept a deleted override id, keep package names, check lengths A review of policy decision list found these problems: - --override with the id of a deleted override failed with "no policy override is named". A stored decision keeps that id, and the server matches it. The command now sends a value with the shape of an override id ("ov_" and 8 to 64 Crockford base32 characters) as is when the policy does not have it. Other unknown values still fail. - --package let the PURL parser change the name. The parser lowercases a PyPI name and drops the vendor of a Composer package, but the server compares the stored name exactly. The command now takes only the ecosystem from the parser and builds the name from the PURL as written: npm @scope/name, Maven group:artifact, the Go module path, Composer vendor/name. It refuses qualifiers and a subpath. - The command did not check the length limits of the filters. It now refuses a --rule or --override over 64 characters, a --path over 256, a --project over 128, an --endpoint over 100 and a package name over 255, with the flag name in the message. - duration.Window changed the help of audit event list from "for the events" to "for the time". The caller now gives the noun, so the audit text is the same as before. - The command built the filters three times. It now checks the flags once in Args and passes the result to the run. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- docs/cmd/policy-decision-list.md | 21 ++- internal/cmd/audit/list.go | 2 +- internal/cmd/policy/decision_list.go | 183 ++++++++++++++---------- internal/cmd/policy/decision_package.go | 96 +++++++++++++ internal/cmd/policy/decision_test.go | 183 ++++++++++++++++++++---- internal/duration/window.go | 6 +- internal/duration/window_test.go | 5 +- 7 files changed, 385 insertions(+), 111 deletions(-) create mode 100644 internal/cmd/policy/decision_package.go diff --git a/docs/cmd/policy-decision-list.md b/docs/cmd/policy-decision-list.md index 8cc3025..2699184 100644 --- a/docs/cmd/policy-decision-list.md +++ b/docs/cmd/policy-decision-list.md @@ -27,9 +27,9 @@ safedep policy decision list [--since DURATION] [--until DURATION] | `--outcome ` | Only decisions that `blocked` an install, `failed` a check, or `allowed` a package because a trust entry waived a verdict. | | `--revision ` | Only decisions that applied this tenant policy revision. | | `--rule ` | Only decisions that this package rule made. | -| `--override ` | Only decisions that this policy override made: a field that the override set, or a package rule that attaches to it. The command finds the name in the policy at `--revision`, or at head. | +| `--override ` | Only decisions that this policy override made: a field that the override set, or a package rule that attaches to it. The command finds the name or the id in the policy at `--revision`, or at head. An override id that the policy does not have, such as the id of a deleted override, goes to the server as is. | | `--path ` | Only decisions that this field made, by its contract path, as `package_admission.cooldown.min_age`. | -| `--package ` | Only decisions about this package, in all its versions. Give a PURL with no version, as `pkg:npm/lodash`. | +| `--package ` | Only decisions about this package, in all its versions. Give a PURL with no version, no qualifiers and no subpath, as `pkg:npm/lodash`. The name must match the stored name exactly, with the same case. See [Package names](#package-names). | | `--project ` | Only decisions at a pull request or a scan of a repository that is bound to this project. | | `--endpoint ` | Only decisions that pmg made on this endpoint. | | `--limit ` | Page size. The server default applies when 0. | @@ -37,6 +37,23 @@ safedep policy decision list [--since DURATION] [--until DURATION] Inherits root flags `--output` and `--profile`. +The command refuses a value that the server refuses for its length: `--rule` +and `--override` take at most 64 characters, `--path` 256, `--project` 128, +`--endpoint` 100 and the package name 255. + +## Package names + +The command keeps the name of the PURL as you write it, and writes it in the +form that pmg and vet store: + +| Ecosystem | PURL | Name | +|-----------|------|------| +| npm | `pkg:npm/%40acme/ui` | `@acme/ui` | +| Maven | `pkg:maven/org.apache.commons/commons-lang3` | `org.apache.commons:commons-lang3` | +| Go | `pkg:golang/github.com/BurntSushi/toml` | `github.com/BurntSushi/toml` | +| Composer | `pkg:composer/laravel/framework` | `laravel/framework` | +| PyPI | `pkg:pypi/Requests` | `Requests` | + ## Columns | Column | Content | diff --git a/internal/cmd/audit/list.go b/internal/cmd/audit/list.go index 53855d7..69d9c17 100644 --- a/internal/cmd/audit/list.go +++ b/internal/cmd/audit/list.go @@ -94,7 +94,7 @@ func invalidFilter(cause error, help string) error { func newListRequest(in listInput, now time.Time) (*auditv1.ListAuditEventsRequest, error) { filters := &auditv1.ListAuditEventsRequest_Filters{} set := false - after, before, err := duration.Window(in.Since, in.Until, now) + after, before, err := duration.Window(in.Since, in.Until, now, "events") if err != nil { return nil, err } diff --git a/internal/cmd/policy/decision_list.go b/internal/cmd/policy/decision_list.go index f392725..e7d9206 100644 --- a/internal/cmd/policy/decision_list.go +++ b/internal/cmd/policy/decision_list.go @@ -5,13 +5,13 @@ import ( "fmt" "strings" "time" + "unicode/utf8" policyv1grpc "buf.build/gen/go/safedep/api/grpc/go/safedep/services/controltower/policy/v1/policyv1grpc" - packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1" policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" - drypb "github.com/safedep/dry/api/pb" "github.com/safedep/dry/log" "github.com/spf13/cobra" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/timestamppb" "github.com/safedep/cli/internal/app" @@ -24,6 +24,14 @@ import ( const ( decisionToolPrefix = "DECISION_TOOL_" decisionOutcomePrefix = "DECISION_OUTCOME_" + + // The longest values that the server accepts in the filters of + // ListPolicyDecisionsRequest, in characters. + maxRuleIDLen = 64 + maxOverrideIDLen = 64 + maxPathLen = 256 + maxProjectIDLen = 128 + maxEndpointIDLen = 100 ) type decisionListInput struct { @@ -42,8 +50,21 @@ type decisionListInput struct { PageToken string } +// decisionQuery is the checked input of one list. The filters hold every +// flag except --override, which needs the policy to resolve a name. +type decisionQuery struct { + filters *policyv1.ListPolicyDecisionsRequest_Filters + override string + revision uint64 + pageSize uint32 + pageToken string +} + func decisionListCmd(a *app.App) *cobra.Command { - var in decisionListInput + var ( + in decisionListInput + query *decisionQuery + ) cmd := &cobra.Command{ Use: "list", Short: "List the decisions that the policy made, newest first", @@ -57,7 +78,8 @@ func decisionListCmd(a *app.App) *cobra.Command { if err := cobra.NoArgs(cmd, args); err != nil { return err } - _, err := newDecisionFilters(in, "", time.Now()) + var err error + query, err = newDecisionQuery(in, time.Now()) return err }, RunE: func(cmd *cobra.Command, _ []string) error { @@ -65,7 +87,7 @@ func decisionListCmd(a *app.App) *cobra.Command { if err != nil { return err } - res, err := runDecisionList(cmd.Context(), cloudpolicy.NewPolicyClient(client.Connection()), in, time.Now()) + res, err := runDecisionList(cmd.Context(), cloudpolicy.NewPolicyClient(client.Connection()), query) if err != nil { return err } @@ -107,50 +129,28 @@ func parseDecisionEnum(flag, token string, names map[int32]string, prefix string return n, nil } -// parseDecisionPackage reads a PURL with no version. The filter matches every -// version of the package, so a version in the PURL would mislead. -func parseDecisionPackage(purl string) (*packagev1.Package, error) { - help := "Give a PURL with no version, as in --package pkg:npm/lodash" - if !strings.HasPrefix(purl, "pkg:") { - return nil, invalidInputError(fmt.Errorf("--package %q is not a PURL", purl), help) +// checkLength refuses locally a value that the server refuses for its length. +func checkLength(flag, value string, limit int) error { + if n := utf8.RuneCountInString(value); n > limit { + return invalidInputError(fmt.Errorf("--%s is %d characters long: the limit is %d", flag, n, limit), + fmt.Sprintf("Give a --%s value of at most %d characters", flag, limit)) } - parsed, err := drypb.NewPurlPackageVersion(purl) - if err != nil { - return nil, invalidInputError(fmt.Errorf("--package %q: %w", purl, err), help) - } - pv := parsed.PackageVersion() - switch { - case pv.GetPackage().GetEcosystem() == packagev1.Ecosystem_ECOSYSTEM_UNSPECIFIED: - return nil, invalidInputError(fmt.Errorf("--package %q: unknown ecosystem", purl), help) - case pv.GetVersion() != "": - return nil, invalidInputError( - fmt.Errorf("--package %q has a version: the filter matches every version of the package", purl), help) - case pv.GetPackage().GetName() == "": - return nil, invalidInputError(fmt.Errorf("--package %q has no name", purl), help) - } - return pv.GetPackage(), nil + return nil } -// newDecisionFilters builds the request filters from the flags. overrideID is -// the id that --override resolved to. It returns nil when no filter is set. -func newDecisionFilters( - in decisionListInput, - overrideID string, - now time.Time, -) (*policyv1.ListPolicyDecisionsRequest_Filters, error) { +// newDecisionQuery checks the flags and builds the filters. It makes no +// RPC, so the command refuses a bad flag before it reads credentials. +func newDecisionQuery(in decisionListInput, now time.Time) (*decisionQuery, error) { filters := &policyv1.ListPolicyDecisionsRequest_Filters{} - set := false - after, before, err := duration.Window(in.Since, in.Until, now) + after, before, err := duration.Window(in.Since, in.Until, now, "decisions") if err != nil { return nil, err } if after != nil { filters.SetDecidedAfter(timestamppb.New(*after)) - set = true } if before != nil { filters.SetDecidedBefore(timestamppb.New(*before)) - set = true } if in.Tool != "" { n, err := parseDecisionEnum("tool", in.Tool, policyv1.DecisionTool_name, decisionToolPrefix, decisionToolTokens()) @@ -158,7 +158,6 @@ func newDecisionFilters( return nil, err } filters.SetTool(policyv1.DecisionTool(n)) - set = true } if in.Outcome != "" { n, err := parseDecisionEnum("outcome", in.Outcome, policyv1.DecisionOutcome_name, decisionOutcomePrefix, @@ -167,11 +166,9 @@ func newDecisionFilters( return nil, err } filters.SetOutcome(policyv1.DecisionOutcome(n)) - set = true } if in.Revision > 0 { filters.SetRevision(in.Revision) - set = true } if in.Package != "" { pkg, err := parseDecisionPackage(in.Package) @@ -179,27 +176,38 @@ func newDecisionFilters( return nil, err } filters.SetPackage(pkg) - set = true + } + // An override name has at most 63 characters, so the limit of the id + // also holds for a name. + if err := checkLength("override", in.Override, maxOverrideIDLen); err != nil { + return nil, err } for _, text := range []struct { + flag string value string + limit int apply func(string) }{ - {in.Rule, filters.SetRuleId}, - {overrideID, filters.SetOverrideId}, - {in.Path, filters.SetPath}, - {in.Project, filters.SetProjectId}, - {in.Endpoint, filters.SetEndpointId}, + {"rule", in.Rule, maxRuleIDLen, filters.SetRuleId}, + {"path", in.Path, maxPathLen, filters.SetPath}, + {"project", in.Project, maxProjectIDLen, filters.SetProjectId}, + {"endpoint", in.Endpoint, maxEndpointIDLen, filters.SetEndpointId}, } { - if text.value != "" { - text.apply(text.value) - set = true + if text.value == "" { + continue } + if err := checkLength(text.flag, text.value, text.limit); err != nil { + return nil, err + } + text.apply(text.value) } - if !set { - return nil, nil - } - return filters, nil + return &decisionQuery{ + filters: filters, + override: in.Override, + revision: in.Revision, + pageSize: in.PageSize, + pageToken: in.PageToken, + }, nil } // runDecisionList reads one page of decisions. It reads the policy only to @@ -209,34 +217,26 @@ func newDecisionFilters( func runDecisionList( ctx context.Context, client policyv1grpc.PolicyServiceClient, - in decisionListInput, - now time.Time, + q *decisionQuery, ) (*decisionListResult, error) { - if _, err := newDecisionFilters(in, "", now); err != nil { - return nil, err - } var snapshot *policyv1.Policy - overrideID := "" - if in.Override != "" { - _, p, err := readPolicyAt(ctx, client, in.Revision) + if q.override != "" { + _, p, err := readPolicyAt(ctx, client, q.revision) if err != nil { return nil, err } snapshot = p - o, err := findOverrideByNameOrID(p, in.Override) + id, err := resolveOverrideID(p, q.override) if err != nil { return nil, err } - overrideID = o.GetOverrideId() - } - filters, err := newDecisionFilters(in, overrideID, now) - if err != nil { - return nil, err + q.filters.SetOverrideId(id) } req := &policyv1.ListPolicyDecisionsRequest{} - req.SetPagination(paging.NewPaginationRequest(in.PageSize, in.PageToken)) - if filters != nil { - req.SetFilters(filters) + req.SetPagination(paging.NewPaginationRequest(q.pageSize, q.pageToken)) + // Every filter is optional, so a set filter always adds bytes. + if proto.Size(q.filters) > 0 { + req.SetFilters(q.filters) } res, err := client.ListPolicyDecisions(ctx, req) if err != nil { @@ -246,7 +246,7 @@ func runDecisionList( names := map[string]string{} if citesOverride(res.GetDecisions()) { if snapshot == nil { - _, p, err := readPolicyAt(ctx, client, in.Revision) + _, p, err := readPolicyAt(ctx, client, q.revision) if err != nil { // The names only label the output. The ids stay, so the list is // still correct without them. @@ -268,17 +268,46 @@ func citesOverride(decisions []*policyv1.PolicyDecision) bool { return false } -// findOverrideByNameOrID accepts the id too, because the JSON output of a -// decision gives the id of its override. A name wins over an id. -func findOverrideByNameOrID(p *policyv1.Policy, ref string) (*policyv1.Override, error) { +// resolveOverrideID finds the override by name, then by id, in the policy +// that the command read. A stored decision keeps the id of an override that +// a later revision deleted, so a value with the shape of an override id goes +// to the server as is when the policy does not have it. +func resolveOverrideID(p *policyv1.Policy, ref string) (string, error) { o, err := findOverride(p, ref) if err == nil { - return o, nil + return o.GetOverrideId(), nil } for _, o := range p.GetOverrides() { if o.GetOverrideId() == ref { - return o, nil + return ref, nil + } + } + if isOverrideID(ref) { + return ref, nil + } + return "", err +} + +const ( + overrideIDPrefix = "ov_" + // overrideIDAlphabet is lowercase Crockford base32, the alphabet of the + // ids that the server makes. + overrideIDAlphabet = "0123456789abcdefghjkmnpqrstvwxyz" + minOverrideIDBody = 8 +) + +// isOverrideID reports the shape of an id that the server gives an override: +// "ov_" and 8 to 64 characters of overrideIDAlphabet. It repeats id.Valid of +// dry, which the dry version of this CLI does not have yet. +func isOverrideID(s string) bool { + body, ok := strings.CutPrefix(s, overrideIDPrefix) + if !ok || len(body) < minOverrideIDBody || len(s) > maxOverrideIDLen { + return false + } + for i := 0; i < len(body); i++ { + if strings.IndexByte(overrideIDAlphabet, body[i]) < 0 { + return false } } - return nil, err + return true } diff --git a/internal/cmd/policy/decision_package.go b/internal/cmd/policy/decision_package.go new file mode 100644 index 0000000..af1a7e1 --- /dev/null +++ b/internal/cmd/policy/decision_package.go @@ -0,0 +1,96 @@ +package policy + +import ( + "errors" + "fmt" + "net/url" + "strings" + + packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1" + drypb "github.com/safedep/dry/api/pb" +) + +// maxPackageNameLen is the longest package name that the server accepts. +const maxPackageNameLen = 255 + +const packageHelp = "Give a PURL with no version, as in --package pkg:npm/lodash or pkg:maven/org.example/lib" + +// parseDecisionPackage reads the --package PURL. The server compares the +// stored package name exactly, so the command keeps the name as the user +// wrote it. A PURL parser changes some names: it lowercases a PyPI name and +// drops the vendor of a Composer package. Only the ecosystem comes from the +// parser. The name has the form that pmg and vet store: +// +// npm @scope/name +// Maven group:artifact +// Go the module path +// Composer vendor/name +// others the namespace and the name, joined with "/" +// +// The filter matches every version, so the command refuses a version. It +// also refuses qualifiers and a subpath, because the filter has no field for +// them. +func parseDecisionPackage(purl string) (*packagev1.Package, error) { + fail := func(cause error) (*packagev1.Package, error) { + return nil, invalidInputError(fmt.Errorf("--package %q: %w", purl, cause), packageHelp) + } + if !strings.HasPrefix(strings.ToLower(purl), "pkg:") { + return fail(errors.New("not a PURL")) + } + rest := strings.TrimLeft(purl[len("pkg:"):], "/") + switch { + case strings.Contains(rest, "?"): + return fail(errors.New("has qualifiers: the filter does not use them")) + case strings.Contains(rest, "#"): + return fail(errors.New("has a subpath: the filter does not use it")) + } + purlType, path, ok := strings.Cut(rest, "/") + if !ok || path == "" { + return fail(errors.New("has no name")) + } + // An "@" at the start of a segment begins an npm scope. Any other "@" + // begins a version. + if at := strings.LastIndexByte(path, '@'); at > 0 && path[at-1] != '/' { + return fail(errors.New("has a version: the filter matches every version of the package")) + } + + parsed, err := drypb.NewPurlPackageVersion(purl) + if err != nil { + return fail(err) + } + ecosystem := parsed.Ecosystem() + if ecosystem == packagev1.Ecosystem_ECOSYSTEM_UNSPECIFIED { + return fail(fmt.Errorf("unknown ecosystem %q", purlType)) + } + + var segments []string + for _, raw := range strings.Split(path, "/") { + if raw == "" { + continue + } + segment, err := url.PathUnescape(raw) + if err != nil { + return fail(err) + } + segments = append(segments, segment) + } + if len(segments) == 0 { + return fail(errors.New("has no name")) + } + + name := strings.Join(segments, "/") + if ecosystem == packagev1.Ecosystem_ECOSYSTEM_MAVEN { + if len(segments) != 2 { + return fail(errors.New("a Maven PURL needs the group and the artifact, as in pkg:maven/org.example/lib")) + } + name = segments[0] + ":" + segments[1] + } + if err := checkLength("package", name, maxPackageNameLen); err != nil { + return nil, err + } + + pkg := &packagev1.Package{} + pkg.SetEcosystem(ecosystem) + pkg.SetName(name) + return pkg, nil +} diff --git a/internal/cmd/policy/decision_test.go b/internal/cmd/policy/decision_test.go index 9d2ff69..b0a67c4 100644 --- a/internal/cmd/policy/decision_test.go +++ b/internal/cmd/policy/decision_test.go @@ -134,19 +134,14 @@ func decisionsResponse(next string, decisions ...*policyv1.PolicyDecision) *poli return res } -func TestNewDecisionFilters(t *testing.T) { +func TestNewDecisionQuery(t *testing.T) { day := 24 * time.Hour tests := []struct { - name string - in decisionListInput - overrideID string - check func(*testing.T, *policyv1.ListPolicyDecisionsRequest_Filters) - wantErr string + name string + in decisionListInput + check func(*testing.T, *policyv1.ListPolicyDecisionsRequest_Filters) + wantErr string }{ - { - name: "no filters", - check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { assert.Nil(t, f) }, - }, { name: "time range", in: decisionListInput{Since: 7 * day, Until: day}, @@ -191,11 +186,10 @@ func TestNewDecisionFilters(t *testing.T) { }, }, { - name: "override id from the resolved name", - in: decisionListInput{Override: "payments"}, - overrideID: "ov_pay", + name: "override waits for the policy read", + in: decisionListInput{Override: "payments"}, check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { - assert.Equal(t, "ov_pay", f.GetOverrideId()) + assert.False(t, f.HasOverrideId()) }, }, { @@ -230,13 +224,95 @@ func TestNewDecisionFilters(t *testing.T) { {name: "unknown tool", in: decisionListInput{Tool: "npm"}, wantErr: "unknown --tool value"}, {name: "unknown outcome", in: decisionListInput{Outcome: "denied"}, wantErr: "unknown --outcome value"}, {name: "package with a version", in: decisionListInput{Package: "pkg:npm/lodash@4.17.21"}, wantErr: "has a version"}, - {name: "package not a PURL", in: decisionListInput{Package: "lodash"}, wantErr: "is not a PURL"}, + {name: "package not a PURL", in: decisionListInput{Package: "lodash"}, wantErr: "not a PURL"}, {name: "package of an unknown ecosystem", in: decisionListInput{Package: "pkg:nope/x"}, wantErr: "unknown ecosystem"}, + {name: "rule too long", in: decisionListInput{Rule: strings.Repeat("r", 65)}, wantErr: "--rule is 65 characters long: the limit is 64"}, + {name: "override too long", in: decisionListInput{Override: strings.Repeat("o", 65)}, wantErr: "--override is 65 characters long"}, + {name: "path too long", in: decisionListInput{Path: strings.Repeat("p", 257)}, wantErr: "--path is 257 characters long: the limit is 256"}, + {name: "project too long", in: decisionListInput{Project: strings.Repeat("j", 129)}, wantErr: "--project is 129 characters long: the limit is 128"}, + {name: "endpoint too long", in: decisionListInput{Endpoint: strings.Repeat("e", 101)}, wantErr: "--endpoint is 101 characters long: the limit is 100"}, + {name: "limits count characters, not bytes", in: decisionListInput{Rule: strings.Repeat("\u00e9", 64)}, + check: func(t *testing.T, f *policyv1.ListPolicyDecisionsRequest_Filters) { assert.True(t, f.HasRuleId()) }}, {name: "until after since", in: decisionListInput{Since: time.Hour, Until: 2 * time.Hour}, wantErr: "--until must be shorter"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - f, err := newDecisionFilters(tt.in, tt.overrideID, fixedNow) + q, err := newDecisionQuery(tt.in, fixedNow) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + ue, ok := usefulerror.AsUsefulError(err) + require.True(t, ok) + assert.Equal(t, usefulerror.ErrBadRequest, ue.Code()) + return + } + require.NoError(t, err) + tt.check(t, q.filters) + }) + } +} + +func TestNewDecisionQuery_UnsetFlagsSetNoFilter(t *testing.T) { + q, err := newDecisionQuery(decisionListInput{}, fixedNow) + require.NoError(t, err) + f := q.filters + for name, has := range map[string]bool{ + "decided_after": f.HasDecidedAfter(), + "decided_before": f.HasDecidedBefore(), + "tool": f.HasTool(), + "outcome": f.HasOutcome(), + "revision": f.HasRevision(), + "rule_id": f.HasRuleId(), + "override_id": f.HasOverrideId(), + "path": f.HasPath(), + "package": f.HasPackage(), + "project_id": f.HasProjectId(), + "endpoint_id": f.HasEndpointId(), + } { + assert.False(t, has, name) + } + + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.MatchedBy(func(r *policyv1.ListPolicyDecisionsRequest) bool { + return !r.HasFilters() + })).Return(decisionsResponse(""), nil) + _, err = runDecisionList(context.Background(), client, q) + require.NoError(t, err) +} + +func TestParseDecisionPackage(t *testing.T) { + tests := []struct { + purl string + ecosystem packagev1.Ecosystem + name string + wantErr string + }{ + {purl: "pkg:npm/lodash", ecosystem: packagev1.Ecosystem_ECOSYSTEM_NPM, name: "lodash"}, + {purl: "pkg:npm/%40acme/ui", ecosystem: packagev1.Ecosystem_ECOSYSTEM_NPM, name: "@acme/ui"}, + {purl: "pkg:npm/@acme/ui", ecosystem: packagev1.Ecosystem_ECOSYSTEM_NPM, name: "@acme/ui"}, + {purl: "pkg:pypi/Requests", ecosystem: packagev1.Ecosystem_ECOSYSTEM_PYPI, name: "Requests"}, + {purl: "pkg:pypi/typing_extensions", ecosystem: packagev1.Ecosystem_ECOSYSTEM_PYPI, name: "typing_extensions"}, + {purl: "pkg:maven/org.apache.commons/commons-lang3", ecosystem: packagev1.Ecosystem_ECOSYSTEM_MAVEN, name: "org.apache.commons:commons-lang3"}, + {purl: "pkg:golang/github.com/BurntSushi/toml", ecosystem: packagev1.Ecosystem_ECOSYSTEM_GO, name: "github.com/BurntSushi/toml"}, + {purl: "pkg:composer/laravel/framework", ecosystem: packagev1.Ecosystem_ECOSYSTEM_PACKAGIST, name: "laravel/framework"}, + {purl: "pkg:gem/Rails", ecosystem: packagev1.Ecosystem_ECOSYSTEM_RUBYGEMS, name: "Rails"}, + {purl: "pkg:cargo/serde", ecosystem: packagev1.Ecosystem_ECOSYSTEM_CARGO, name: "serde"}, + {purl: "pkg:nuget/Newtonsoft.Json", ecosystem: packagev1.Ecosystem_ECOSYSTEM_NUGET, name: "Newtonsoft.Json"}, + {purl: "pkg:github/actions/checkout", ecosystem: packagev1.Ecosystem_ECOSYSTEM_GITHUB_ACTIONS, name: "actions/checkout"}, + {purl: "pkg:npm/lodash@4.17.21", wantErr: "has a version"}, + {purl: "pkg:npm/%40acme/ui@1.0.0", wantErr: "has a version"}, + {purl: "pkg:npm/@acme/ui@1.0.0", wantErr: "has a version"}, + {purl: "pkg:npm/lodash?repository_url=https://r.example.com", wantErr: "has qualifiers"}, + {purl: "pkg:golang/github.com/acme/mod#sub/dir", wantErr: "has a subpath"}, + {purl: "pkg:maven/commons-lang3", wantErr: "needs the group and the artifact"}, + {purl: "pkg:npm/", wantErr: "has no name"}, + {purl: "lodash", wantErr: "not a PURL"}, + {purl: "pkg:nope/x", wantErr: "unknown ecosystem"}, + {purl: "pkg:npm/" + strings.Repeat("a", 256), wantErr: "--package is 256 characters long: the limit is 255"}, + } + for _, tt := range tests { + t.Run(tt.purl, func(t *testing.T) { + pkg, err := parseDecisionPackage(tt.purl) if tt.wantErr != "" { require.Error(t, err) assert.Contains(t, err.Error(), tt.wantErr) @@ -246,18 +322,27 @@ func TestNewDecisionFilters(t *testing.T) { return } require.NoError(t, err) - tt.check(t, f) + assert.Equal(t, tt.ecosystem, pkg.GetEcosystem()) + assert.Equal(t, tt.name, pkg.GetName()) }) } } +// listQuery is the checked query of valid flags. +func listQuery(t *testing.T, in decisionListInput) *decisionQuery { + t.Helper() + q, err := newDecisionQuery(in, fixedNow) + require.NoError(t, err) + return q +} + func TestRunDecisionList_RuleListsBothToolsWithThePlace(t *testing.T) { client := mocks.NewPolicyServiceClientMock(t) client.EXPECT().ListPolicyDecisions(mock.Anything, mock.MatchedBy(func(r *policyv1.ListPolicyDecisionsRequest) bool { return r.GetFilters().GetRuleId() == "r_9c1x4kq7dm" })).Return(decisionsResponse("", ruleDecisions()...), nil) - got, err := runDecisionList(context.Background(), client, decisionListInput{Rule: "r_9c1x4kq7dm"}, fixedNow) + got, err := runDecisionList(context.Background(), client, listQuery(t, decisionListInput{Rule: "r_9c1x4kq7dm"})) require.NoError(t, err) table := got.RenderTable() @@ -283,8 +368,7 @@ func TestRunDecisionList_CooldownShowsThePathAndTheDefaultRevision(t *testing.T) return r.GetFilters().GetPath() == "package_admission.cooldown.min_age" })).Return(decisionsResponse("", cooldown), nil) - got, err := runDecisionList(context.Background(), client, - decisionListInput{Path: "package_admission.cooldown.min_age"}, fixedNow) + got, err := runDecisionList(context.Background(), client, listQuery(t, decisionListInput{Path: "package_admission.cooldown.min_age"})) require.NoError(t, err) table := got.RenderTable() assert.Contains(t, table, "package_admission.cooldown.min_age · default policy") @@ -354,7 +438,7 @@ func TestRunDecisionList_Override(t *testing.T) { return r.GetFilters().GetOverrideId() == tt.wantID && r.GetFilters().GetRevision() == tt.in.Revision })).Return(decisionsResponse("", overrideDecision), nil) } - got, err := runDecisionList(context.Background(), client, tt.in, fixedNow) + got, err := runDecisionList(context.Background(), client, listQuery(t, tt.in)) if tt.wantErr != "" { require.Error(t, err) assert.Contains(t, err.Error(), tt.wantErr) @@ -367,6 +451,51 @@ func TestRunDecisionList_Override(t *testing.T) { } } +func TestRunDecisionList_TheIDOfADeletedOverrideIsSentAsIs(t *testing.T) { + const deleted = "ov_9c1x4kq7dm" + d := decision("d_old", policyv1.DecisionTool_DECISION_TOOL_PMG, policyv1.DecisionOutcome_DECISION_OUTCOME_BLOCKED, + "fresh", "0.0.1", citation(49, 12, "package_admission.cooldown.min_age", "", deleted, recordv1.Layer_LAYER_OVERRIDE), + atEndpoint(endpointPoint("dev-laptop", "alice"))) + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().GetPolicy(mock.Anything, mock.Anything).Return(headResponse(), nil).Once() + client.EXPECT().ListPolicyDecisions(mock.Anything, mock.MatchedBy(func(r *policyv1.ListPolicyDecisionsRequest) bool { + return r.GetFilters().GetOverrideId() == deleted + })).Return(decisionsResponse("", d), nil) + + got, err := runDecisionList(context.Background(), client, listQuery(t, decisionListInput{Override: deleted})) + require.NoError(t, err) + assert.Contains(t, got.RenderTable(), "override "+deleted, "the table names the deleted override by its id") +} + +func TestResolveOverrideID(t *testing.T) { + p := headPolicy() + tests := []struct { + ref string + want string + wantErr string + }{ + {ref: "payments", want: "ov_pay"}, + {ref: "ov_ci", want: "ov_ci"}, + {ref: "ov_9c1x4kq7dm", want: "ov_9c1x4kq7dm"}, + {ref: "ov_short", wantErr: `no policy override is named "ov_short"`}, + // "l" is not in the alphabet of the ids. + {ref: "ov_9c1x4kq7dl", wantErr: "no policy override is named"}, + {ref: "nope", wantErr: `no policy override is named "nope"`}, + } + for _, tt := range tests { + t.Run(tt.ref, func(t *testing.T) { + got, err := resolveOverrideID(p, tt.ref) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} + func TestRunDecisionList_OverrideNameReadFailureKeepsTheID(t *testing.T) { d := decision("d_ov", policyv1.DecisionTool_DECISION_TOOL_PMG, policyv1.DecisionOutcome_DECISION_OUTCOME_BLOCKED, "evil", "1.0.0", citation(47, 12, "", "r_1", "ov_gone", recordv1.Layer_LAYER_UNSPECIFIED), @@ -375,7 +504,7 @@ func TestRunDecisionList_OverrideNameReadFailureKeepsTheID(t *testing.T) { client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(decisionsResponse("", d), nil) client.EXPECT().GetPolicy(mock.Anything, mock.Anything).Return(nil, status.Error(codes.Unavailable, "down")) - got, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + got, err := runDecisionList(context.Background(), client, listQuery(t, decisionListInput{})) require.NoError(t, err) assert.Contains(t, got.RenderTable(), "rule r_1 · override ov_gone") } @@ -436,7 +565,7 @@ func TestRunDecisionList_EscapesServerText(t *testing.T) { client := mocks.NewPolicyServiceClientMock(t) client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(decisionsResponse("", d), nil) - got, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + got, err := runDecisionList(context.Background(), client, listQuery(t, decisionListInput{})) require.NoError(t, err) for _, out := range []string{got.RenderTable(), got.RenderPlain()} { assert.NotContains(t, out, "\x1b") @@ -452,7 +581,7 @@ func TestRunDecisionList_Empty(t *testing.T) { client := mocks.NewPolicyServiceClientMock(t) client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(decisionsResponse(""), nil) - got, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + got, err := runDecisionList(context.Background(), client, listQuery(t, decisionListInput{})) require.NoError(t, err) assert.Contains(t, got.RenderTable(), "No policy decisions match") assert.Len(t, strings.Split(got.RenderPlain(), "\n"), 1, "plain writes only the header") @@ -467,7 +596,7 @@ func TestRunDecisionList_Paging(t *testing.T) { return r.GetPagination().GetPageSize() == 2 && r.GetPagination().GetPageToken() == "tok_1" && !r.HasFilters() })).Return(decisionsResponse("tok_2", ruleDecisions()...), nil) - got, err := runDecisionList(context.Background(), client, decisionListInput{PageSize: 2, PageToken: "tok_1"}, fixedNow) + got, err := runDecisionList(context.Background(), client, listQuery(t, decisionListInput{PageSize: 2, PageToken: "tok_1"})) require.NoError(t, err) assert.Contains(t, got.RenderTable(), "2 decisions. More available: --page-token tok_2") b, err := got.RenderJSON() @@ -479,7 +608,7 @@ func TestRunDecisionList_JSONHasTheRawMessages(t *testing.T) { client := mocks.NewPolicyServiceClientMock(t) client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(decisionsResponse("", ruleDecisions()...), nil) - got, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + got, err := runDecisionList(context.Background(), client, listQuery(t, decisionListInput{})) require.NoError(t, err) b, err := got.RenderJSON() require.NoError(t, err) @@ -547,7 +676,7 @@ func TestMapDecisionListError(t *testing.T) { t.Run(tt.name, func(t *testing.T) { client := mocks.NewPolicyServiceClientMock(t) client.EXPECT().ListPolicyDecisions(mock.Anything, mock.Anything).Return(nil, tt.err) - _, err := runDecisionList(context.Background(), client, decisionListInput{}, fixedNow) + _, err := runDecisionList(context.Background(), client, listQuery(t, decisionListInput{})) ue, ok := usefulerror.AsUsefulError(err) require.True(t, ok) assert.Equal(t, tt.wantCode, ue.Code()) diff --git a/internal/duration/window.go b/internal/duration/window.go index 15d8579..76d9ddc 100644 --- a/internal/duration/window.go +++ b/internal/duration/window.go @@ -11,14 +11,14 @@ import ( // range. Both are durations before now, and zero means no bound. after is // nil when since is zero, and before is nil when until is zero. More than // one list command reads these flags, so they all refuse the same input with -// the same message. -func Window(since, until time.Duration, now time.Time) (after, before *time.Time, err error) { +// the same message. noun names the listed items in the help, as "events". +func Window(since, until time.Duration, now time.Time, noun string) (after, before *time.Time, err error) { if since < 0 || until < 0 { return nil, nil, badWindow(errors.New("--since and --until must not be negative"), "Use a duration such as 7d") } if since > 0 && until >= since { return nil, nil, badWindow(errors.New("--until must be shorter than --since"), - "As in --since 7d --until 1d for the time from 7 days ago to 1 day ago") + "As in --since 7d --until 1d for the "+noun+" from 7 days ago to 1 day ago") } if since > 0 { t := now.Add(-since) diff --git a/internal/duration/window_test.go b/internal/duration/window_test.go index 0b4ecec..1b1a015 100644 --- a/internal/duration/window_test.go +++ b/internal/duration/window_test.go @@ -29,13 +29,16 @@ func TestWindow(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - after, before, err := Window(tt.since, tt.until, now) + after, before, err := Window(tt.since, tt.until, now, "events") if tt.wantErr != "" { require.Error(t, err) assert.Contains(t, err.Error(), tt.wantErr) ue, ok := usefulerror.AsUsefulError(err) require.True(t, ok) assert.Equal(t, usefulerror.ErrBadRequest, ue.Code()) + if tt.since > 0 && tt.until >= tt.since { + assert.Equal(t, "As in --since 7d --until 1d for the events from 7 days ago to 1 day ago", ue.Help()) + } return } require.NoError(t, err)