diff --git a/docs/cmd/endpoint-list.md b/docs/cmd/endpoint-list.md index b1b05ac..6800cf0 100644 --- a/docs/cmd/endpoint-list.md +++ b/docs/cmd/endpoint-list.md @@ -33,7 +33,10 @@ safedep endpoint list --applied-revision '<48' ``` The `Policy` column shows the applied policy revision that each tool reported: -`rev 48` when every tool applies the same revision, or `pmg 48, vet 47`. JSON +`rev 48` when every tool applies the same revision, or `pmg 48, vet 47`. A tool +that applies no cloud policy is not in the column: its state is `disabled`, as for +pmg with `cloud.policy: false`, or `none`, when the tool has no policy cache yet and applies its +baseline. The column shows `-` when no tool is left. JSON output carries `tool_policies` for each endpoint: the tool, its version, its applied policy revision and the state of the policy in the tool. diff --git a/internal/cloudpolicy/docpath.go b/internal/cloudpolicy/docpath.go new file mode 100644 index 0000000..3679040 --- /dev/null +++ b/internal/cloudpolicy/docpath.go @@ -0,0 +1,151 @@ +package cloudpolicy + +import ( + "regexp" + "strconv" + "strings" + "time" + + policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" + "google.golang.org/protobuf/reflect/protoreflect" +) + +// selectorKeys are the document keys of the label selector fields. The +// document writes a selector in the Kubernetes form. +var selectorKeys = map[protoreflect.Name]string{ + "match_labels": "matchLabels", + "match_expressions": "matchExpressions", +} + +// DocumentPath maps a dotted field path of a Policy, with proto field names +// as a server error gives it, to the keys of the edit document, as +// "overrides[0].selector.matchLabels". Indexes and map keys stay as they +// are. It also reports whether the field is a duration. A path that does +// not parse, or a part below a name that the Policy does not have, stays as +// it is, so a person still sees what the server sent. +func DocumentPath(path string) (string, bool) { + segments, ok := splitFieldPath(path) + if !ok { + return path, false + } + md := (&policyv1.Policy{}).ProtoReflect().Descriptor() + var last protoreflect.FieldDescriptor + for i, seg := range segments { + if md == nil { + last = nil + continue + } + fd := md.Fields().ByName(protoreflect.Name(seg.name)) + if fd == nil { + md, last = nil, nil + continue + } + key, isSelector := selectorKeys[fd.Name()] + if !isSelector || md.FullName() != labelSelectorFullName { + key = documentCodec.key(fd) + } + segments[i].name = key + last, md = fd, nextMessage(fd, seg.subscript != "") + } + return joinFieldPath(segments), last != nil && last.Message() != nil && + last.Message().FullName() == durationFullName +} + +// nextMessage is the message that the next part of a path names. +func nextMessage(fd protoreflect.FieldDescriptor, subscripted bool) protoreflect.MessageDescriptor { + switch { + case fd.IsMap(): + if subscripted { + return fd.MapValue().Message() + } + return nil + case fd.Message() != nil && fd.Message().FullName() != durationFullName && + fd.Message().FullName() != timestampFullName: + return fd.Message() + } + return nil +} + +type pathSegment struct { + name string + subscript string +} + +// splitFieldPath splits "a.b[0].c[\"k.x\"]" into its names and subscripts. +// A quoted map key can hold a dot or a bracket. +func splitFieldPath(path string) ([]pathSegment, bool) { + var segments []pathSegment + for i := 0; i < len(path); { + end := strings.IndexAny(path[i:], ".[") + if end < 0 { + end = len(path) - i + } + seg := pathSegment{name: path[i : i+end]} + if seg.name == "" { + return nil, false + } + i += end + for i < len(path) && path[i] == '[' { + n, ok := subscriptLen(path[i:]) + if !ok { + return nil, false + } + seg.subscript += path[i : i+n] + i += n + } + segments = append(segments, seg) + if i < len(path) { + if path[i] != '.' || i == len(path)-1 { + return nil, false + } + i++ + } + } + return segments, len(segments) > 0 +} + +// subscriptLen is the length of the subscript at the start of s, brackets +// included. +func subscriptLen(s string) (int, bool) { + if len(s) > 1 && s[1] == '"' { + for j := 2; j < len(s); j++ { + switch s[j] { + case '\\': + j++ + case '"': + if j+1 < len(s) && s[j+1] == ']' { + return j + 2, true + } + return 0, false + } + } + return 0, false + } + end := strings.IndexByte(s, ']') + return end + 1, end > 1 +} + +func joinFieldPath(segments []pathSegment) string { + parts := make([]string, len(segments)) + for i, seg := range segments { + parts[i] = seg.name + seg.subscript + } + return strings.Join(parts, ".") +} + +// wholeSeconds matches a protovalidate duration in seconds, as "31536000s". +var wholeSeconds = regexp.MustCompile(`\b([0-9]+)s\b`) + +// DocumentDurations writes each duration in text that is a whole number of +// days in the form of the document notes, as "31536000s" to "365d". +// protovalidate writes a duration limit in seconds, which a person cannot +// compare with the "from 0s to 365d" note of the file. +func DocumentDurations(text string) string { + return wholeSeconds.ReplaceAllStringFunc(text, func(m string) string { + n, err := strconv.ParseInt(strings.TrimSuffix(m, "s"), 10, 64) + if err != nil || n == 0 || n%(24*60*60) != 0 || n > int64(time.Duration(1<<62)/time.Second) { + return m + } + return humanDuration(time.Duration(n) * time.Second) + }) +} diff --git a/internal/cloudpolicy/docpath_test.go b/internal/cloudpolicy/docpath_test.go new file mode 100644 index 0000000..eccb628 --- /dev/null +++ b/internal/cloudpolicy/docpath_test.go @@ -0,0 +1,53 @@ +package cloudpolicy + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDocumentPath(t *testing.T) { + tests := []struct { + path string + want string + duration bool + }{ + {"base.package_admission.cooldown.min_age", "base.package_admission.cooldown.min_age", true}, + {`overrides[0].selector.match_labels["team/x"]`, `overrides[0].selector.matchLabels["team/x"]`, false}, + {`overrides[0].selector.match_labels["a.b[c]"]`, `overrides[0].selector.matchLabels["a.b[c]"]`, false}, + {"overrides[1].selector.match_expressions[0].key", "overrides[1].selector.matchExpressions[0].key", false}, + {"overrides[2].override_id", "overrides[2].id", false}, + {"overrides[2].expires_at", "overrides[2].expires_at", false}, + {"overrides[0].spec.scan_gate.fail_on", "overrides[0].spec.scan_gate.fail_on", false}, + {"base_description", "base_description", false}, + {"unknown.match_labels", "unknown.match_labels", false}, + {"overrides[0.name", "overrides[0.name", false}, + {"", "", false}, + } + for _, tt := range tests { + t.Run(tt.path, func(t *testing.T) { + got, duration := DocumentPath(tt.path) + assert.Equal(t, tt.want, got) + assert.Equal(t, tt.duration, duration) + }) + } +} + +func TestDocumentDurations(t *testing.T) { + tests := []struct { + in, want string + }{ + { + "must be greater than or equal to 0s and less than or equal to 31536000s", + "must be greater than or equal to 0s and less than or equal to 365d", + }, + {"must be at most 604800s", "must be at most 7d"}, + {"must be at most 3600s", "must be at most 3600s"}, + {"must be at most 1.5s", "must be at most 1.5s"}, + } + for _, tt := range tests { + t.Run(tt.in, func(t *testing.T) { + assert.Equal(t, tt.want, DocumentDurations(tt.in)) + }) + } +} diff --git a/internal/cmd/endpoint/list.go b/internal/cmd/endpoint/list.go index 3f5f471..d17efe2 100644 --- a/internal/cmd/endpoint/list.go +++ b/internal/cmd/endpoint/list.go @@ -421,8 +421,15 @@ func parsePolicyFilters(selector, applied string) (*messagescontroltowerv1.Label } // policyCell shows the applied policy revision of each tool, as "rev 47" when -// every tool applies the same revision, or "pmg 47, vet 46". -func policyCell(policies []ToolPolicy) string { +// every tool applies the same revision, or "pmg 47, vet 46". A tool that +// applies no cloud policy is not in the cell. +func policyCell(all []ToolPolicy) string { + policies := make([]ToolPolicy, 0, len(all)) + for _, p := range all { + if !p.noRevision { + policies = append(policies, p) + } + } if len(policies) == 0 { return "-" } diff --git a/internal/cmd/endpoint/policy_test.go b/internal/cmd/endpoint/policy_test.go index 763b3b6..524de8f 100644 --- a/internal/cmd/endpoint/policy_test.go +++ b/internal/cmd/endpoint/policy_test.go @@ -106,8 +106,47 @@ func TestParsePolicyFilters(t *testing.T) { } func TestPolicyCell(t *testing.T) { - assert.Equal(t, "-", policyCell(nil)) - assert.Equal(t, "rev 47", policyCell([]ToolPolicy{{Tool: "pmg", Revision: 47}, {Tool: "vet", Revision: 47}})) + tests := []struct { + name string + policies []ToolPolicy + want string + }{ + {"no tool", nil, "-"}, + {"one revision", []ToolPolicy{{Tool: "pmg", Revision: 47}, {Tool: "vet", Revision: 47}}, "rev 47"}, + {"two revisions", []ToolPolicy{{Tool: "pmg", Revision: 47}, {Tool: "vet", Revision: 46}}, "pmg 47, vet 46"}, + {"only a tool with no revision", []ToolPolicy{{Tool: "pmg", noRevision: true}}, "-"}, + { + "a tool with no revision and an applied tool", + []ToolPolicy{{Tool: "pmg", noRevision: true}, {Tool: "vet", Revision: 46, State: "applied"}}, + "rev 46", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, policyCell(tt.policies)) + }) + } +} + +func TestToolPolicies_StatesWithNoRevisionAreNotInThePolicyCell(t *testing.T) { + tests := []struct { + state recordv1.ApplyState + want string + }{ + {recordv1.ApplyState_APPLY_STATE_DISABLED, "-"}, + {recordv1.ApplyState_APPLY_STATE_NONE, "-"}, + {recordv1.ApplyState_APPLY_STATE_STALE, "rev 0"}, + } + for _, tt := range tests { + t.Run(tt.state.String(), func(t *testing.T) { + report := &recordv1.ApplyReport{} + report.SetState(tt.state) + p := &controltowerv1.EndpointToolPolicy{} + p.SetToolName("pmg") + p.SetPolicy(report) + assert.Equal(t, tt.want, policyCell(toolPolicies([]*controltowerv1.EndpointToolPolicy{p}))) + }) + } } func TestRunList_ShowsTheAppliedRevision(t *testing.T) { diff --git a/internal/cmd/endpoint/service.go b/internal/cmd/endpoint/service.go index 65110cc..ba9ee4f 100644 --- a/internal/cmd/endpoint/service.go +++ b/internal/cmd/endpoint/service.go @@ -9,6 +9,7 @@ import ( controltowerv1grpc "buf.build/gen/go/safedep/api/grpc/go/safedep/services/controltower/v1/controltowerv1grpc" messagescontroltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/controltower/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" controltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/v1" "google.golang.org/grpc" "google.golang.org/protobuf/types/known/timestamppb" @@ -120,6 +121,10 @@ type ToolPolicy struct { Revision uint64 State string ReportedAt time.Time + // noRevision marks a tool that applies no cloud policy: it is disabled + // in the tool, or the tool has no policy cache yet. Its revision is 0 + // and means nothing. + noRevision bool } type ListEndpoint struct { ID string @@ -338,6 +343,10 @@ func toolPolicies(in []*controltowerv1.EndpointToolPolicy) []ToolPolicy { Revision: p.GetPolicy().GetRevision(), State: tui.EnumToken(p.GetPolicy().GetState().String(), "APPLY_STATE_"), } + switch p.GetPolicy().GetState() { + case recordv1.ApplyState_APPLY_STATE_DISABLED, recordv1.ApplyState_APPLY_STATE_NONE: + tp.noRevision = true + } if ts := p.GetReportedAt(); ts != nil { tp.ReportedAt = ts.AsTime().UTC() } diff --git a/internal/cmd/policy/decision_test.go b/internal/cmd/policy/decision_test.go index f683c69..31dd0d0 100644 --- a/internal/cmd/policy/decision_test.go +++ b/internal/cmd/policy/decision_test.go @@ -678,6 +678,12 @@ func TestMapDecisionListError(t *testing.T) { wantCode: usefulerror.ErrBadRequest, wantHuman: "The page token is not valid", }, + { + name: "bad page token with an encoded field path", + err: badRequest(encodedFieldPath("pagination", "page_token"), "the page token is not valid"), + 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"), diff --git a/internal/cmd/policy/editor.go b/internal/cmd/policy/editor.go index 3b7d885..136985b 100644 --- a/internal/cmd/policy/editor.go +++ b/internal/cmd/policy/editor.go @@ -13,7 +13,6 @@ import ( "github.com/safedep/dry/usefulerror" "google.golang.org/genproto/googleapis/rpc/errdetails" "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" ) @@ -49,13 +48,13 @@ func isRetryable(err error) bool { if errors.As(err, &r) { return true } - st, ok := status.FromError(err) + st, ok := innerStatus(err) if !ok || st.Code() != codes.InvalidArgument { return false } if bad, ok := usefulerror.DetailAs[*errdetails.BadRequest](err); ok && len(bad.GetFieldViolations()) > 0 { for _, v := range bad.GetFieldViolations() { - if isDocumentField(v.GetField()) { + if isDocumentField(violationField(v)) { return true } } @@ -63,7 +62,7 @@ func isRetryable(err error) bool { } // With no field detail, the message starts with the field, as in // "title: must be at most 200 characters". - field, _, found := strings.Cut(st.Message(), ":") + field, _, found := strings.Cut(stripServicePrefix(st.Message()), ":") return !found || strings.ContainsAny(field, " ") || isDocumentField(field) } @@ -163,7 +162,7 @@ func editLoop( func errorLines(err error) []byte { var b strings.Builder b.WriteString(errorLinePrefix + "The last save failed:\n") - for _, line := range strings.Split(err.Error(), "\n") { + for _, line := range strings.Split(errorText(err), "\n") { b.WriteString(errorLinePrefix + " " + line + "\n") } b.WriteString(errorLinePrefix + "Fix the file and save it again. Close it with no change to stop.\n") @@ -181,6 +180,9 @@ func stripErrorLines(text []byte) []byte { return text } +// noHelpText is the help of a useful error that has no help. +const noHelpText = "No additional help is available for this error." + // keptEditError keeps the code, the message and the help of a useful // error, such as the one for a role that cannot change the policy, and adds // the path of the kept file. @@ -188,8 +190,8 @@ func keptEditError(path string, cause error) error { code, human, help := usefulerror.ErrBadRequest, "The command did not save the policy edit: "+cause.Error(), "" if ue, ok := usefulerror.AsUsefulError(cause); ok { code, human = ue.Code(), "The command did not save the policy edit: "+ue.HumanError() - if ue.Help() != "" { - help = ue.Help() + ". " + if h := strings.TrimRight(ue.Help(), ". "); h != "" && ue.Help() != noHelpText { + help = h + ". " } } return usefulerror.NewUsefulError(). diff --git a/internal/cmd/policy/errors.go b/internal/cmd/policy/errors.go index 909177d..ebf7ef9 100644 --- a/internal/cmd/policy/errors.go +++ b/internal/cmd/policy/errors.go @@ -1,14 +1,20 @@ package policy import ( + "errors" "fmt" + "strconv" + "strings" + validate "buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" 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" + "google.golang.org/protobuf/encoding/prototext" + "github.com/safedep/cli/internal/cloudpolicy" "github.com/safedep/cli/internal/tui" ) @@ -26,9 +32,198 @@ func invalidInputError(cause error, help string) error { // failedPreconditionError shows the detail of a FAILED_PRECONDITION. The // server sends no reason with it, so the hint covers the cases of the RPC. -func failedPreconditionError(st *status.Status, help string, cause error) error { +func failedPreconditionError(err error, help string, cause error) error { return newPolicyError(usefulerror.ErrConflict, - "The server refused the request: "+tui.EscapeLine(st.Message()), help, cause) + "The server refused the request: "+describeStatus(err), help, cause) +} + +// invalidArgumentError shows the message and each field violation of an +// INVALID_ARGUMENT. Without it, the generic conversion shows only "Invalid +// request" and the user cannot find the field to fix. +func invalidArgumentError(err error, help string, cause error) error { + return newPolicyError(usefulerror.ErrBadRequest, describeStatus(err), help, cause) +} + +// serviceFailedPrefix is the text that the server puts before the message of +// a service error. It names no field and no fix, so the CLI removes it. +const serviceFailedPrefix = "service execution failed: " + +// stripServicePrefix removes each leading serviceFailedPrefix of a status +// message. A server that wraps an error two times sends the prefix two +// times, and a field name after one prefix must still read as the field. +func stripServicePrefix(msg string) string { + msg = strings.TrimSpace(msg) + for strings.HasPrefix(msg, serviceFailedPrefix) { + msg = strings.TrimSpace(strings.TrimPrefix(msg, serviceFailedPrefix)) + } + return msg +} + +// noReasonText is the text of a status that has no message and no field +// violation. +const noReasonText = "The server gave no reason" + +// describeStatus is the text of a gRPC status error for a person, with each +// field path in the form of the edit document. See describeStatusAt. +func describeStatus(err error) string { + return describeStatusAt(err, documentPath) +} + +// describeStatusAt is the text of a gRPC status error for a person: the +// status message, then each field violation of a BadRequest detail as +// "field: description", with the field from path. The text of +// status.FromError on a wrapped error starts with "rpc error: code = ... +// desc =", so this reads the status of the innermost error. When the +// message already holds the description of each violation, the violations +// replace it, so no text shows two times. +func describeStatusAt(err error, path func(string) (string, bool)) string { + st, ok := innerStatus(err) + if !ok { + return tui.EscapeText(err.Error()) + } + msg := stripServicePrefix(st.Message()) + var lines []string + covered := true + if bad, ok := usefulerror.DetailAs[*errdetails.BadRequest](err); ok { + for _, v := range bad.GetFieldViolations() { + field, isDuration := path(violationField(v)) + description := v.GetDescription() + if isDuration { + description = cloudpolicy.DocumentDurations(description) + } + if line := violationLine(field, description); line != "" { + lines = append(lines, tui.EscapeLine(line)) + } + covered = covered && v.GetDescription() != "" && strings.Contains(msg, v.GetDescription()) + } + } + if msg != "" && (len(lines) == 0 || !covered) { + lines = append([]string{tui.EscapeText(msg)}, lines...) + } + if len(lines) == 0 { + return noReasonText + } + return strings.Join(lines, "\n") +} + +func violationLine(field, description string) string { + switch { + case field == "": + return description + case description == "": + return field + } + return field + ": " + description +} + +// innerStatus is the status of the innermost gRPC error in the chain of err, +// with its message as the server sent it. +func innerStatus(err error) (*status.Status, bool) { + var gs interface{ GRPCStatus() *status.Status } + if !errors.As(err, &gs) { + return nil, false + } + st := gs.GRPCStatus() + return st, st != nil +} + +// violationField is the field of a violation as a dotted path, as in +// "settings.overrides[0].name". The server sends the protovalidate field +// path of a request as the protobuf text of a buf.validate.FieldPath, which +// no person can read. A field that does not parse as that text stays as it +// is. +func violationField(v *errdetails.BadRequest_FieldViolation) string { + field := v.GetField() + path := &validate.FieldPath{} + if err := prototext.Unmarshal([]byte(field), path); err != nil || len(path.GetElements()) == 0 { + return field + } + return fieldPathString(path) +} + +// fieldPathString writes a field path as protovalidate.FieldPathString +// does. The CLI does not import protovalidate, because its CEL runtime makes +// the binary 18% larger. +func fieldPathString(path *validate.FieldPath) string { + var b strings.Builder + for i, el := range path.GetElements() { + if i > 0 { + b.WriteByte('.') + } + b.WriteString(el.GetFieldName()) + var key string + switch el.WhichSubscript() { + case validate.FieldPathElement_Index_case: + key = strconv.FormatUint(el.GetIndex(), 10) + case validate.FieldPathElement_BoolKey_case: + key = strconv.FormatBool(el.GetBoolKey()) + case validate.FieldPathElement_IntKey_case: + key = strconv.FormatInt(el.GetIntKey(), 10) + case validate.FieldPathElement_UintKey_case: + key = strconv.FormatUint(el.GetUintKey(), 10) + case validate.FieldPathElement_StringKey_case: + key = strconv.Quote(el.GetStringKey()) + default: + continue + } + b.WriteString("[" + key + "]") + } + return b.String() +} + +// documentFieldPrefix is the request field that holds the edit document. +// The CLI shows a document path without it, as the local checks do, so a +// path names a key of the file. +const documentFieldPrefix = "settings." + +// documentPath is a field path of a request as the path of the key in the +// edit document, and whether the field is a duration. Only a path below +// documentFieldPrefix is in the document. +func documentPath(field string) (string, bool) { + rest, ok := strings.CutPrefix(field, documentFieldPrefix) + if !ok { + return field, false + } + return cloudpolicy.DocumentPath(rest) +} + +// overrideFieldError marks a save error of an edit of one policy override. +// The file of that edit holds the override only, so a field path of it +// shows without "overrides[i].". +type overrideFieldError struct { + err error + index int +} + +func (e *overrideFieldError) Error() string { return e.err.Error() } +func (e *overrideFieldError) Unwrap() error { return e.err } + +func (e *overrideFieldError) path(field string) (string, bool) { + field, isDuration := documentPath(field) + prefix := fmt.Sprintf("overrides[%d]", e.index) + if field == prefix { + return "", isDuration + } + if rest, ok := strings.CutPrefix(field, prefix+"."); ok { + return rest, isDuration + } + return field, isDuration +} + +// errorText is the text of err for a person, for the error lines of the +// edit file. An INVALID_ARGUMENT of an edit of one policy override shows its +// field paths as paths of the file. Other errors show the human error of a +// useful error in the chain, else the text of describeStatus. +func errorText(err error) string { + var oe *overrideFieldError + if st, ok := innerStatus(err); ok && st.Code() == codes.InvalidArgument && errors.As(err, &oe) { + return describeStatusAt(err, oe.path) + } + var ue usefulerror.UsefulError + if errors.As(err, &ue) { + return ue.HumanError() + } + return describeStatus(err) } // violatesField reports whether a BadRequest detail of err names field. @@ -38,13 +233,28 @@ func violatesField(err error, field string) bool { return false } for _, v := range bad.GetFieldViolations() { - if v.GetField() == field { + if violationField(v) == field { return true } } return false } +// alreadyExistsError is the error for a package rule that selects the same +// package as a rule at head. The server names that rule in a ResourceInfo +// detail. Without the detail, the status message is the fallback. +func alreadyExistsError(err error, cause error) error { + if info, ok := usefulerror.DetailAs[*errdetails.ResourceInfo](err); ok && info.GetResourceName() != "" { + id := tui.EscapeLine(info.GetResourceName()) + return newPolicyError(usefulerror.ErrConflict, + fmt.Sprintf("Package rule %s already selects this package", id), + "See it with: safedep policy rule show "+id, + cause) + } + return newPolicyError(usefulerror.ErrConflict, describeStatus(err), + "List the rules: safedep policy rule list", cause) +} + // mapChangeError gives the recovery for the errors of a policy change RPC: // a stale base, an old etag, or a change that does not exist. func mapChangeError(op, changeID string, err error) error { @@ -77,7 +287,11 @@ func mapChangeError(op, changeID string, err error) error { "Ask an Admin of the tenant to do it", cause) case codes.FailedPrecondition: - return failedPreconditionError(st, changePreconditionHelp(changeID), cause) + return failedPreconditionError(err, changePreconditionHelp(changeID), cause) + case codes.InvalidArgument: + return invalidArgumentError(err, "Correct the values and run the command again", cause) + case codes.AlreadyExists: + return alreadyExistsError(err, cause) } } return cause @@ -127,7 +341,11 @@ func mapRuleError(op, ruleID string, err error) error { "Ask an Admin of the tenant to do it", cause) case codes.FailedPrecondition: - return failedPreconditionError(st, "List the rules: safedep policy rule list", cause) + return failedPreconditionError(err, "List the rules: safedep policy rule list", cause) + case codes.InvalidArgument: + return invalidArgumentError(err, fmt.Sprintf("See the flags: safedep policy rule %s --help", op), cause) + case codes.AlreadyExists: + return alreadyExistsError(err, cause) } } return cause @@ -156,18 +374,14 @@ func mapDecisionListError(err error) error { "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) - } - } + if violatesField(err, "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()), + "The server refused the filters: "+describeStatus(err), "See the filters: safedep policy decision list --help", cause) case codes.Unimplemented: diff --git a/internal/cmd/policy/errors_test.go b/internal/cmd/policy/errors_test.go new file mode 100644 index 0000000..d42deb4 --- /dev/null +++ b/internal/cmd/policy/errors_test.go @@ -0,0 +1,366 @@ +package policy + +import ( + "context" + "errors" + "fmt" + "strings" + "testing" + + validate "buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" + 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" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/encoding/prototext" + "google.golang.org/protobuf/types/descriptorpb" + + "github.com/safedep/cli/internal/cloudpolicy/mocks" +) + +// encodedFieldPath is a field path as the server sends it today: the +// protobuf text of a protovalidate FieldPath. +func encodedFieldPath(names ...string) string { + path := &validate.FieldPath{} + for _, name := range names { + el := &validate.FieldPathElement{} + n, index, indexed := strings.Cut(name, "[") + el.SetFieldName(n) + el.SetFieldType(descriptorpb.FieldDescriptorProto_TYPE_MESSAGE) + if indexed { + var i uint64 + _, err := fmt.Sscanf(strings.TrimSuffix(index, "]"), "%d", &i) + if err != nil { + panic(err) + } + el.SetIndex(i) + } + path.SetElements(append(path.GetElements(), el)) + } + return prototext.Format(path) +} + +// mapKeyPath is overrides[2].selector.match_labels["team/x"] as a field path. +func mapKeyPath() *validate.FieldPath { + el := func(name string) *validate.FieldPathElement { + e := &validate.FieldPathElement{} + e.SetFieldName(name) + return e + } + overrides, selector, labels := el("overrides"), el("selector"), el("match_labels") + overrides.SetIndex(2) + labels.SetStringKey("team/x") + path := &validate.FieldPath{} + path.SetElements([]*validate.FieldPathElement{el("settings"), overrides, selector, labels}) + return path +} + +func statusWithViolations(code codes.Code, message string, violations ...*errdetails.BadRequest_FieldViolation) error { + st, err := status.New(code, message).WithDetails(&errdetails.BadRequest{FieldViolations: violations}) + if err != nil { + panic(err) + } + return st.Err() +} + +func violation(field, description string) *errdetails.BadRequest_FieldViolation { + return &errdetails.BadRequest_FieldViolation{Field: field, Description: description} +} + +func TestDescribeStatus(t *testing.T) { + tests := []struct { + name string + err error + want string + }{ + { + name: "a status with no detail", + err: status.Error(codes.InvalidArgument, "override priorities must be unique"), + want: "override priorities must be unique", + }, + { + name: "the service prefix of the server", + err: status.Error(codes.InvalidArgument, "service execution failed: override priorities must be unique"), + want: "override priorities must be unique", + }, + { + name: "a wrapped status", + err: usefulerror.NewUsefulError().WithHumanError("x"). + Wrap(fmt.Errorf("policy change create: %w", status.Error(codes.InvalidArgument, "bad title"))), + want: "bad title", + }, + { + name: "a violation with the message as its description", + err: statusWithViolations(codes.InvalidArgument, + "service execution failed: unknown label prefix \"team/\"", + violation("settings.overrides[0].selector.match_labels", "unknown label prefix \"team/\"")), + want: "overrides[0].selector.matchLabels: unknown label prefix \"team/\"", + }, + { + name: "a protovalidate violation with an encoded field path", + err: statusWithViolations(codes.InvalidArgument, + "service execution failed: API validation error: validation error:\n - settings.overrides[0].name: "+ + "value does not match regex pattern `^[a-z0-9]$` [string.pattern]", + violation(encodedFieldPath("settings", "overrides[0]", "name"), + "value does not match regex pattern `^[a-z0-9]$`")), + want: "overrides[0].name: value does not match regex pattern `^[a-z0-9]$`", + }, + { + name: "a message that the violations do not hold", + err: statusWithViolations(codes.InvalidArgument, "the request is not valid", + violation("title", "must be at most 200 characters"), + violation("settings.base_description", "must be at most 1000 characters")), + want: "the request is not valid\ntitle: must be at most 200 characters\n" + + "base_description: must be at most 1000 characters", + }, + { + name: "a doubled service prefix", + err: status.Error(codes.InvalidArgument, "service execution failed: service execution failed: bad"), + want: "bad", + }, + { + name: "a status with no message", + err: status.Error(codes.InvalidArgument, ""), + want: "The server gave no reason", + }, + { + name: "a violation with no description", + err: statusWithViolations(codes.InvalidArgument, "", violation("title", "")), + want: "title", + }, + { + name: "map key subscripts", + err: statusWithViolations(codes.InvalidArgument, "", violation(prototext.Format(mapKeyPath()), + "unknown label prefix")), + want: `overrides[2].selector.matchLabels["team/x"]: unknown label prefix`, + }, + { + name: "control characters from the server", + err: status.Error(codes.InvalidArgument, "bad\x1b[2J"), + want: `bad\x1b[2J`, + }, + { + name: "an error that is not a status", + err: errors.New("line 1: bad\x1b[2J\nline 2"), + want: "line 1: bad\\x1b[2J\nline 2", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := describeStatus(tt.err) + assert.Equal(t, tt.want, got) + assert.NotContains(t, got, "rpc error") + }) + } +} + +func alreadyExists(ruleID string) error { + st := status.New(codes.AlreadyExists, "service execution failed: rules[0]: package rule r_7f2 selects the same "+ + "package with the same effect, and attaches to the same policy base or policy override") + if ruleID == "" { + return st.Err() + } + st, err := st.WithDetails(&errdetails.ResourceInfo{ResourceType: "package_rule", ResourceName: ruleID}) + if err != nil { + panic(err) + } + return st.Err() +} + +func TestServerErrorMapping_InvalidArgumentAndAlreadyExists(t *testing.T) { + tests := []struct { + name string + err error + wantCode string + wantHuman string + wantHelp string + }{ + { + name: "a change with a field out of range", + err: mapChangeError("create", "", statusWithViolations(codes.InvalidArgument, + "service execution failed: API validation error", + violation(encodedFieldPath("settings", "base", "package_admission", "cooldown", "min_age"), + "must be greater than or equal to 0s and less than or equal to 31536000s"))), + wantCode: usefulerror.ErrBadRequest, + wantHuman: "API validation error\nbase.package_admission.cooldown.min_age: " + + "must be greater than or equal to 0s and less than or equal to 365d", + wantHelp: "Correct the values and run the command again", + }, + { + name: "a rule with a bad flag", + err: mapRuleError("create", "", badRequest("rules[0].reason", "value is required")), + wantCode: usefulerror.ErrBadRequest, + wantHuman: "rules[0].reason: value is required", + wantHelp: "See the flags: safedep policy rule create --help", + }, + { + name: "a rule create that duplicates a rule", + err: mapRuleError("create", "", alreadyExists("r_7f2")), + wantCode: usefulerror.ErrConflict, + wantHuman: "Package rule r_7f2 already selects this package", + wantHelp: "See it with: safedep policy rule show r_7f2", + }, + { + name: "a change that duplicates a rule", + err: mapChangeError("publish", "3", alreadyExists("r_7f2")), + wantCode: usefulerror.ErrConflict, + wantHuman: "Package rule r_7f2 already selects this package", + wantHelp: "See it with: safedep policy rule show r_7f2", + }, + { + name: "a duplicate with no resource detail", + err: mapRuleError("create", "", alreadyExists("")), + wantCode: usefulerror.ErrConflict, + wantHuman: "rules[0]: package rule r_7f2 selects the same package with the same effect, " + + "and attaches to the same policy base or policy override", + wantHelp: "List the rules: safedep policy rule list", + }, + { + name: "a precondition with the service prefix", + err: mapRuleError("delete", "r_1", status.Error(codes.FailedPrecondition, "service execution failed: gone")), + wantCode: usefulerror.ErrConflict, + wantHuman: "The server refused the request: gone", + wantHelp: "List the rules: safedep policy rule list", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ue, ok := usefulerror.AsUsefulError(tt.err) + require.True(t, ok) + assert.Equal(t, tt.wantCode, ue.Code()) + assert.Equal(t, tt.wantHuman, ue.HumanError()) + assert.Equal(t, tt.wantHelp, ue.Help()) + }) + } +} + +func TestKeptEditError_Help(t *testing.T) { + tests := []struct { + name string + cause error + want string + }{ + { + name: "a converted gRPC error with a full stop", + cause: status.Error(codes.Unavailable, "down"), + want: "The service is temporarily unavailable. Retry after a short delay. Your edit is in /tmp/x.yaml", + }, + { + name: "a useful error with no help", + cause: usefulerror.NewUsefulError().WithCode(usefulerror.ErrBadRequest).WithHumanError("bad"), + want: "Your edit is in /tmp/x.yaml", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ue, ok := usefulerror.AsUsefulError(keptEditError("/tmp/x.yaml", tt.cause)) + require.True(t, ok) + assert.True(t, strings.HasPrefix(ue.Help(), tt.want), "help: %s", ue.Help()) + assert.NotContains(t, ue.Help(), "..") + }) + } +} + +func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { + minAge := func(s string) string { + s = uncomment(" # package_admission:", " # cooldown:", " # min_age: 7d")(s) + return strings.Replace(s, "min_age: 7d", "min_age: 400d", 1) + } + tests := []struct { + name string + override string + bad, fix func(string) string + violation *errdetails.BadRequest_FieldViolation + want string + }{ + { + name: "a min_age above the limit", + bad: minAge, + fix: replace("min_age: 400d", "min_age: 30d"), + violation: violation(encodedFieldPath("settings", "base", "package_admission", "cooldown", "min_age"), + "must be greater than or equal to 0s and less than or equal to 31536000s"), + want: "# ! base.package_admission.cooldown.min_age: must be greater than or equal to 0s and less than or equal to 365d\n", + }, + { + name: "an override name with an upper case letter", + bad: replace("name: payments", "name: Payments"), + fix: replace("name: Payments", "name: payments-team"), + violation: violation(encodedFieldPath("settings", "overrides[0]", "name"), "does not match regex pattern"), + want: "# ! overrides[0].name: does not match regex pattern\n", + }, + { + name: "an unknown selector label prefix", + bad: replace("custom/criticality", "team/criticality"), + fix: replace("{team/criticality:", "{custom/criticality:"), + violation: violation(`settings.overrides[0].selector.match_labels["team/criticality"]`, `unknown label prefix "team/"`), + want: "# ! overrides[0].selector.matchLabels[\"team/criticality\"]: unknown label prefix \"team/\"\n", + }, + { + name: "a field of the override in the file", + override: "ci", + bad: replace("name: ci", "name: CI"), + fix: replace("name: CI", "name: ci-runners"), + violation: violation(encodedFieldPath("settings", "overrides[1]", "name"), "does not match regex pattern"), + want: "# ! name: does not match regex pattern\n", + }, + { + name: "a spec field of the override in the file", + override: "ci", + bad: replace("fail_on: critical", "fail_on: high"), + fix: replace("fail_on: high", "fail_on: medium"), + violation: violation(encodedFieldPath("settings", "overrides[1]", "spec", "scan_gate", "fail_on"), + "value must be one of the defined enum values"), + want: "# ! spec.scan_gate.fail_on: value must be one of the defined enum values\n", + }, + { + name: "the override in the file as a whole", + override: "ci", + bad: replace("fail_on: critical", "fail_on: high"), + fix: replace("fail_on: high", "fail_on: medium"), + violation: violation(encodedFieldPath("settings", "overrides[1]"), "the override selects one endpoint"), + want: "# ! the override selects one endpoint\n", + }, + { + name: "another override in an edit of one override", + override: "ci", + bad: replace("fail_on: critical", "fail_on: high"), + fix: replace("fail_on: high", "fail_on: medium"), + violation: violation(encodedFieldPath("settings", "overrides[0]", "priority"), "must be unique"), + want: "# ! overrides[0].priority: must be unique\n", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().GetPolicy(mock.Anything, mock.Anything).Return(headResponse(), nil) + calls := 0 + client.EXPECT().CreatePolicyChange(mock.Anything, mock.Anything). + RunAndReturn(func(context.Context, *policyv1.CreatePolicyChangeRequest, ...grpc.CallOption) (*policyv1.CreatePolicyChangeResponse, error) { + calls++ + if calls == 1 { + return nil, statusWithViolations(codes.InvalidArgument, + "service execution failed: "+tt.violation.GetDescription(), tt.violation) + } + res := &policyv1.CreatePolicyChangeResponse{} + res.SetChange(openChange("18")) + return res, nil + }) + + ed := &scriptedEditor{edits: []func(string) string{tt.bad, tt.fix}} + change, err := runEdit(context.Background(), client, ed.open, "", editInput{Override: tt.override}) + require.NoError(t, err) + assert.Equal(t, "18", change.GetChangeId()) + assert.Equal(t, 2, calls) + require.Len(t, ed.seen, 2) + assert.Contains(t, ed.seen[1], tt.want) + assert.NotContains(t, ed.seen[1], "rpc error") + assert.NotContains(t, ed.seen[1], "service execution failed") + assert.NotContains(t, ed.seen[1], "settings.") + }) + } +} diff --git a/internal/cmd/policy/review_test.go b/internal/cmd/policy/review_test.go index a0fbec3..4bc92d1 100644 --- a/internal/cmd/policy/review_test.go +++ b/internal/cmd/policy/review_test.go @@ -85,6 +85,23 @@ func TestIsRetryable(t *testing.T) { {"the title with no detail", status.Error(codes.InvalidArgument, "title: must be at most 200 characters"), false}, {"a message with no field", status.Error(codes.InvalidArgument, "override priorities must be unique"), true}, {"another code", status.Error(codes.Unavailable, "down"), false}, + { + "an encoded field path of the document", + badRequest(encodedFieldPath("settings", "base", "package_admission", "cooldown", "min_age"), "must be at most 365d"), + true, + }, + {"the title after the service prefix", status.Error(codes.InvalidArgument, "service execution failed: title: too long"), false}, + { + "the title after a doubled service prefix", + status.Error(codes.InvalidArgument, "service execution failed: service execution failed: title: too long"), + false, + }, + { + "a mapped error of the document", + mapChangeError("create", "", badRequest(encodedFieldPath("settings", "overrides[0]", "name"), "does not match")), + true, + }, + {"a mapped error of the title", mapChangeError("create", "", status.Error(codes.InvalidArgument, "title: too long")), false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/cmd/policy/rule_create.go b/internal/cmd/policy/rule_create.go index 08a1561..54d2faa 100644 --- a/internal/cmd/policy/rule_create.go +++ b/internal/cmd/policy/rule_create.go @@ -2,14 +2,13 @@ package policy import ( "context" + "io" "strings" "time" policyv1grpc "buf.build/gen/go/safedep/api/grpc/go/safedep/services/controltower/policy/v1/policyv1grpc" - corev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/policy/core/v1" policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" "github.com/safedep/dry/tui" - "github.com/safedep/dry/tui/humanize" "github.com/spf13/cobra" "github.com/safedep/cli/internal/app" @@ -65,21 +64,14 @@ func ruleCreateCmd(a *app.App) *cobra.Command { return validateRuleCreate(in) }, RunE: func(cmd *cobra.Command, _ []string) error { - rules, err := buildRuleCreates(in, cmd.InOrStdin(), time.Now()) - if err != nil { - return err - } - client, err := a.ControlPlane() - if err != nil { - return err - } - announceRuleCreate(in, rules) - res, err := runRuleCreate(cmd.Context(), cloudpolicy.NewPolicyClient(client.Connection()), in, rules) - if err != nil { - return err + connect := func() (policyv1grpc.PolicyServiceClient, error) { + client, err := a.ControlPlane() + if err != nil { + return nil, err + } + return cloudpolicy.NewPolicyClient(client.Connection()), nil } - reportRuleCreate(in, res, time.Now()) - return nil + return createRules(cmd.Context(), connect, in, cmd.InOrStdin(), time.Now) }, } f := cmd.Flags() @@ -98,6 +90,35 @@ func ruleCreateCmd(a *app.App) *cobra.Command { return cmd } +// createRules reads the clock one time. The expiry of the rule and the +// report of it use the same instant, so --expires 7d reports "in 7d" and not +// "in 6d". The instant has no fraction of a second, because the server keeps +// a time to the microsecond and a rounded expiry would show one day less. +func createRules( + ctx context.Context, + connect func() (policyv1grpc.PolicyServiceClient, error), + in ruleCreateInput, + stdin io.Reader, + clock func() time.Time, +) error { + now := clock().Truncate(time.Second) + rules, err := buildRuleCreates(in, stdin, now) + if err != nil { + return err + } + client, err := connect() + if err != nil { + return err + } + announceRuleCreate(in, rules) + res, err := runRuleCreate(ctx, client, in, rules) + if err != nil { + return err + } + reportRuleCreate(in, res, now) + return nil +} + func runRuleCreate( ctx context.Context, client policyv1grpc.PolicyServiceClient, @@ -137,8 +158,8 @@ func announceRuleCreate(in ruleCreateInput, creates []*policyv1.PackageRuleCreat if in.Block { for _, c := range creates { if purl := c.GetRule().GetSelector().GetPurl(); isWideSelector(purl) { - tui.Warning("Selector %s blocks every package of its ecosystem. It stops every install of that ecosystem in the tenant", - clitui.EscapeLine(purl)) + tui.Warning("Selector %s blocks every package of its ecosystem. It stops every install of that ecosystem %s", + clitui.EscapeLine(purl), blockPlace(in.Override)) } } tui.Info("A block stops new installs. It does not remove installed copies.") @@ -151,19 +172,6 @@ func announceRuleCreate(in ruleCreateInput, creates []*policyv1.PackageRuleCreat tui.Warning("Verified malware stays blocked") } -func scopeEffect(scope corev1.TrustScope) string { - switch scope { - case corev1.TrustScope_TRUST_SCOPE_MALWARE: - return "a malware verdict that is not verified" - case corev1.TrustScope_TRUST_SCOPE_COOLDOWN: - return "the dependency cooldown" - case corev1.TrustScope_TRUST_SCOPE_VULNERABILITY: - return "the vulnerability findings of a scan gate" - default: - return "the verdicts of scope " + scope.String() - } -} - func reportRuleCreate(in ruleCreateInput, res *ruleCreateResult, now time.Time) { if len(res.Rules) == 1 { tui.Success("rule %s created%srevision %d published", clitui.EscapeLine(res.Rules[0].GetRule().GetRuleId()), sep, res.Revision) @@ -172,7 +180,7 @@ func reportRuleCreate(in ruleCreateInput, res *ruleCreateResult, now time.Time) } if in.Trust && len(res.Rules) == 1 { if expires := cloudpolicy.TimeOf(res.Rules[0].GetRule().GetExpiresAt()); expires != nil { - tui.Info(" The rule expires %s (%s)", humanize.Time(*expires, now), expires.Format(time.DateOnly)) + tui.Info(" The rule expires %s", expiryText(*expires, now)) } } tui.Info(" Each endpoint applies it after its next policy fetch. Pull request checks apply it at once.") diff --git a/internal/cmd/policy/rule_show.go b/internal/cmd/policy/rule_show.go index d1dc6b5..5792c19 100644 --- a/internal/cmd/policy/rule_show.go +++ b/internal/cmd/policy/rule_show.go @@ -65,15 +65,18 @@ func (r *ruleShowResult) RenderJSON() ([]byte, error) { } func (r *ruleShowResult) RenderTable() string { + return r.table(time.Now()) +} + +func (r *ruleShowResult) table(now time.Time) string { v := r.Rule - now := time.Now() removed := "active" if v.RemovedRevision > 0 { removed = fmt.Sprintf("removed in revision %d", v.RemovedRevision) } - expires := v.expiresCell(now) + expires := "never" if v.expires != nil { - expires += " (" + v.expires.Format(time.DateOnly) + ")" + expires = expiryText(*v.expires, now) } return cloudpolicy.KeyValues([][2]string{ {"Rule", v.ID}, diff --git a/internal/cmd/policy/rule_test.go b/internal/cmd/policy/rule_test.go index 2049db7..a15e88c 100644 --- a/internal/cmd/policy/rule_test.go +++ b/internal/cmd/policy/rule_test.go @@ -9,12 +9,14 @@ import ( "testing" "time" + policyv1grpc "buf.build/gen/go/safedep/api/grpc/go/safedep/services/controltower/policy/v1/policyv1grpc" corev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/policy/core/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/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/timestamppb" @@ -480,3 +482,82 @@ func TestRunRuleList_AtRevisionResolvesTheOverrideAtThatRevision(t *testing.T) { require.Len(t, got.Rules, 1) assert.Equal(t, `override "payments-old"`, got.Rules[0].AttachesTo) } + +func TestExpiryText(t *testing.T) { + tests := []struct { + name string + expires time.Time + want string + }{ + {"within 30 days", fixedNow.Add(3 * 24 * time.Hour), "in 3d (2026-11-03)"}, + {"beyond 30 days", time.Date(2027, 1, 4, 0, 0, 0, 0, time.UTC), "2027-01-04"}, + {"in the past", fixedNow.Add(-2 * time.Hour), "2h ago (2026-10-31)"}, + {"seconds less than whole days", fixedNow.Add(7*24*time.Hour - 5*time.Second), "in 7d (2026-11-07)"}, + {"more than half a day over whole days", fixedNow.Add(3*24*time.Hour + 13*time.Hour), "in 4d (2026-11-04)"}, + {"days in the past", fixedNow.Add(-(2*24*time.Hour - time.Minute)), "2d ago (2026-10-29)"}, + {"less than a day", fixedNow.Add(20 * time.Hour), "in 20h (2026-11-01)"}, + {"days that round to 30", fixedNow.Add(30*24*time.Hour - time.Hour), "in 29d (2026-11-30)"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, expiryText(tt.expires, fixedNow)) + }) + } +} + +func TestRuleShow_ShowsTheExpiryDateOneTime(t *testing.T) { + r := ruleWithAttributes("r_1", corev1.Effect_EFFECT_TRUST, "") + r.GetRule().SetExpiresAt(timestamppb.New(fixedNow.Add(65 * 24 * time.Hour))) + table := (&ruleShowResult{Rule: newRuleView(r, nil)}).table(fixedNow) + assert.Equal(t, 1, strings.Count(table, "2027-01-04"), table) +} + +func TestCreateRules_ReportsTheExpiryOfTheFlag(t *testing.T) { + stderr := captureStderr(t) + client := mocks.NewPolicyServiceClientMock(t) + client.EXPECT().CreatePackageRules(mock.Anything, mock.Anything).RunAndReturn( + func(_ context.Context, req *policyv1.CreatePackageRulesRequest, _ ...grpc.CallOption) (*policyv1.CreatePackageRulesResponse, error) { + r := ruleWithAttributes("r_1", corev1.Effect_EFFECT_TRUST, "") + // The server keeps a time to the microsecond. + r.GetRule().SetExpiresAt(timestamppb.New(req.GetRules()[0].GetRule().GetExpiresAt().AsTime().Truncate(time.Microsecond))) + res := &policyv1.CreatePackageRulesResponse{} + res.SetRules([]*policyv1.PackageRuleWithAttributes{r}) + res.SetRevision(revision(48, "trust")) + return res, nil + }) + // Each read of the clock is later than the one before it. + now := fixedNow.Add(123456789 * time.Nanosecond) + clock := func() time.Time { + now = now.Add(5 * time.Millisecond) + return now + } + in := ruleCreateInput{Trust: true, Selector: "pkg:npm/leftpad@1.0.0", Scopes: []string{"malware"}, Reason: "FP", Expires: "7d"} + connect := func() (policyv1grpc.PolicyServiceClient, error) { return client, nil } + + require.NoError(t, createRules(context.Background(), connect, in, nil, clock)) + assert.Contains(t, stderr.String(), "The rule expires in 7d (2026-11-07)") +} + +func TestAnnounceRuleCreate_WideBlock(t *testing.T) { + tests := []struct { + name string + override string + want string + }{ + {"the policy base", "", "It stops every install of that ecosystem in the tenant"}, + {"a policy override", "payments", `It stops every install of that ecosystem on the subjects that policy override "payments" selects`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stderr := captureStderr(t) + in := ruleCreateInput{Block: true, Selector: "pkg:npm/*", Reason: "x", Override: tt.override} + creates, err := buildRuleCreates(in, nil, fixedNow) + require.NoError(t, err) + announceRuleCreate(in, creates) + assert.Contains(t, stderr.String(), tt.want) + if tt.override != "" { + assert.NotContains(t, stderr.String(), "in the tenant") + } + }) + } +} diff --git a/internal/cmd/policy/rule_view.go b/internal/cmd/policy/rule_view.go index e6c341b..798fe30 100644 --- a/internal/cmd/policy/rule_view.go +++ b/internal/cmd/policy/rule_view.go @@ -4,6 +4,7 @@ import ( "fmt" "time" + corev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/policy/core/v1" policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" "github.com/safedep/dry/tui/humanize" @@ -78,5 +79,54 @@ func (v ruleView) expiresCell(now time.Time) string { if v.expires == nil { return "never" } - return humanize.Time(*v.expires, now) + return expiryRelative(*v.expires, now) +} + +// expiryText is a humanized expiry with its date, as "in 3d (2026-11-09)". +// Beyond 30 days humanize.Time gives the date itself, so the date shows one +// time only. +func expiryText(expires, now time.Time) string { + rel := expiryRelative(expires, now) + date := expires.UTC().Format(time.DateOnly) + if rel == date { + return rel + } + return rel + " (" + date + ")" +} + +// expiryRelative is humanize.Time with a time of a day or more rounded to +// the nearest day. humanize.Time drops the part of a day, so a rule that +// --expires 7d sets reads "in 6d" a few seconds after its create. +func expiryRelative(expires, now time.Time) string { + rel := humanize.Time(expires, now) + d := expires.Sub(now) + if d.Abs() < 24*time.Hour || rel == expires.UTC().Format(time.DateOnly) { + return rel + } + if rounded := d.Round(24 * time.Hour); rounded.Abs() < 30*24*time.Hour { + return humanize.Time(now.Add(rounded), now) + } + return rel +} + +// blockPlace names where a block applies: the tenant for the policy base, +// or the subjects of the policy override that the rule attaches to. +func blockPlace(override string) string { + if override == "" { + return "in the tenant" + } + return fmt.Sprintf("on the subjects that policy override %q selects", tui.EscapeLine(override)) +} + +func scopeEffect(scope corev1.TrustScope) string { + switch scope { + case corev1.TrustScope_TRUST_SCOPE_MALWARE: + return "a malware verdict that is not verified" + case corev1.TrustScope_TRUST_SCOPE_COOLDOWN: + return "the dependency cooldown" + case corev1.TrustScope_TRUST_SCOPE_VULNERABILITY: + return "the vulnerability findings of a scan gate" + default: + return "the verdicts of scope " + scope.String() + } } diff --git a/internal/cmd/policy/settings.go b/internal/cmd/policy/settings.go index 724b874..5cbec4a 100644 --- a/internal/cmd/policy/settings.go +++ b/internal/cmd/policy/settings.go @@ -134,10 +134,12 @@ func editOneOverride( if !ok { return errors.New("policy edit: clone the policy") } + index := -1 overrides := full.GetOverrides() for i, o := range overrides { if o.GetOverrideId() == current.GetOverrideId() { overrides[i] = edited + index = i } } full.SetOverrides(overrides) @@ -145,7 +147,10 @@ func editOneOverride( return retryable(err) } warnComments(saved, text) - return commit(full) + if err := commit(full); err != nil { + return &overrideFieldError{err: err, index: index} + } + return nil }) } diff --git a/internal/cmd/project/policy_test.go b/internal/cmd/project/policy_test.go index ca618bd..0081a67 100644 --- a/internal/cmd/project/policy_test.go +++ b/internal/cmd/project/policy_test.go @@ -10,6 +10,7 @@ import ( specv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/policy/spec/v1" policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" controltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/v1" + "github.com/safedep/dry/usefulerror" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -177,6 +178,26 @@ func TestRunPolicyShow(t *testing.T) { assert.Equal(t, "p_1", policies.req.GetSubject().GetId()) } +func TestRunPolicyShow_AmbiguousNameShowsTheCreationTime(t *testing.T) { + created := func(id string, at time.Time) *controltowerv1.ProjectWithAttributes { + p := labeledProject(id, "acme/shared", messagescontroltowerv1.Project_SOURCE_GITHUB, nil) + p.GetProject().SetOriginUrl("https://github.com/acme/shared") + p.GetProject().SetCreatedAt(timestamppb.New(at)) + return p + } + client := &fakeListProjectsClient{listFn: func(*controltowerv1.ListProjectsRequest) (*controltowerv1.ListProjectsResponse, error) { + return projectsResponse( + created("p_1", time.Date(2026, 9, 1, 10, 0, 0, 0, time.UTC)), + created("p_2", time.Date(2026, 10, 2, 11, 30, 0, 0, time.UTC)), + ), nil + }} + _, err := runPolicyShow(context.Background(), client, &fakeSubjectPolicyClient{}, policyShowInput{Name: "acme/shared"}) + ue, ok := usefulerror.AsUsefulError(err) + require.True(t, ok) + assert.Contains(t, ue.Help(), "p_1 (github, https://github.com/acme/shared, created 2026-09-01T10:00:00Z)") + assert.Contains(t, ue.Help(), "p_2 (github, https://github.com/acme/shared, created 2026-10-02T11:30:00Z)") +} + func TestRunLabelSet(t *testing.T) { projects := &fakeListProjectsClient{listFn: func(*controltowerv1.ListProjectsRequest) (*controltowerv1.ListProjectsResponse, error) { return projectsResponse( diff --git a/internal/cmd/project/resolver.go b/internal/cmd/project/resolver.go index c77cbaa..a59443d 100644 --- a/internal/cmd/project/resolver.go +++ b/internal/cmd/project/resolver.go @@ -5,8 +5,10 @@ import ( "fmt" "sort" "strings" + "time" controltowerv1grpc "buf.build/gen/go/safedep/api/grpc/go/safedep/services/controltower/v1/controltowerv1grpc" + messagescontroltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/controltower/v1" controltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/v1" "github.com/safedep/dry/usefulerror" "google.golang.org/grpc" @@ -32,6 +34,7 @@ type projectMatch struct { id string source string originURL string + createdAt time.Time } func newListProjectsClient(conn grpc.ClientConnInterface) listProjectsClient { @@ -176,15 +179,25 @@ func collectProjectMatches( if matches[name] == nil { matches[name] = make(map[string]projectMatch) } - matches[name][project.GetProjectId()] = projectMatch{ - id: project.GetProjectId(), - source: tui.EnumToken(project.GetSource().String(), "SOURCE_"), - originURL: project.GetOriginUrl(), - } + matches[name][project.GetProjectId()] = newProjectMatch(project) } return nil } +// newProjectMatch is one project of an ambiguous name. Each lookup by name +// builds its matches here, so every command shows the same details. +func newProjectMatch(project *messagescontroltowerv1.Project) projectMatch { + match := projectMatch{ + id: project.GetProjectId(), + source: tui.EnumToken(project.GetSource().String(), projectSourcePrefix), + originURL: project.GetOriginUrl(), + } + if ts := project.GetCreatedAt(); ts != nil { + match.createdAt = ts.AsTime().UTC() + } + return match +} + func ambiguousProjectNameError(op, name string, byID map[string]projectMatch) error { items := make([]projectMatch, 0, len(byID)) for _, match := range byID { @@ -200,6 +213,11 @@ func ambiguousProjectNameError(op, name string, byID map[string]projectMatch) er if item.originURL != "" { details += ", " + item.originURL } + // Two projects of one name can have the same source and URL. The + // creation time tells them apart. + if !item.createdAt.IsZero() { + details += ", created " + item.createdAt.Format(time.RFC3339) + } labels = append(labels, fmt.Sprintf("%s (%s)", item.id, details)) } cause := fmt.Errorf( diff --git a/internal/cmd/project/resolver_test.go b/internal/cmd/project/resolver_test.go index 8bb5593..72c4e2e 100644 --- a/internal/cmd/project/resolver_test.go +++ b/internal/cmd/project/resolver_test.go @@ -14,6 +14,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/timestamppb" ) type fakeListProjectsClient struct { @@ -272,6 +273,59 @@ func TestResolveProjectNames_RejectsAmbiguousName(t *testing.T) { ) } +func TestResolveProjectNames_AmbiguousNameShowsTheCreationTime(t *testing.T) { + t.Parallel() + + created := func(p *controltowerv1.ProjectWithAttributes, at time.Time) *controltowerv1.ProjectWithAttributes { + p.GetProject().SetCreatedAt(timestamppb.New(at)) + return p + } + tests := []struct { + name string + projects []*controltowerv1.ProjectWithAttributes + want []string + }{ + { + name: "two projects with the same URL", + projects: []*controltowerv1.ProjectWithAttributes{ + created(project("id-1", "shared", messagescontroltowerv1.Project_SOURCE_GITHUB, "https://github.com/acme/shared"), + time.Date(2026, 9, 1, 10, 0, 0, 0, time.UTC)), + created(project("id-2", "shared", messagescontroltowerv1.Project_SOURCE_GITHUB, "https://github.com/acme/shared"), + time.Date(2026, 10, 2, 11, 30, 0, 0, time.UTC)), + }, + want: []string{ + "id-1 (github, https://github.com/acme/shared, created 2026-09-01T10:00:00Z)", + "id-2 (github, https://github.com/acme/shared, created 2026-10-02T11:30:00Z)", + }, + }, + { + name: "a project with no creation time", + projects: []*controltowerv1.ProjectWithAttributes{ + project("id-1", "shared", messagescontroltowerv1.Project_SOURCE_GITHUB, ""), + created(project("id-2", "shared", messagescontroltowerv1.Project_SOURCE_GITHUB, ""), + time.Date(2026, 10, 2, 11, 30, 0, 0, time.UTC)), + }, + want: []string{"id-1 (github)", "id-2 (github, created 2026-10-02T11:30:00Z)"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + client := &fakeListProjectsClient{ + listFn: func(*controltowerv1.ListProjectsRequest) (*controltowerv1.ListProjectsResponse, error) { + return listProjectsResponse(tt.projects...), nil + }, + } + _, err := resolveProjectNames(context.Background(), client, []string{"shared"}) + ue, ok := usefulerror.AsUsefulError(err) + require.True(t, ok) + for _, want := range tt.want { + assert.Contains(t, ue.Help(), want) + } + }) + } +} + func TestResolveProjectNames_PreservesGRPCStatus(t *testing.T) { t.Parallel() diff --git a/internal/cmd/project/show.go b/internal/cmd/project/show.go index b69a295..5b2c9e8 100644 --- a/internal/cmd/project/show.go +++ b/internal/cmd/project/show.go @@ -63,11 +63,7 @@ func findProject(ctx context.Context, client listProjectsClient, op, name string continue } found = append(found, p) - byID[p.GetProject().GetProjectId()] = projectMatch{ - id: p.GetProject().GetProjectId(), - source: newProjectView(p).Source, - originURL: p.GetProject().GetOriginUrl(), - } + byID[p.GetProject().GetProjectId()] = newProjectMatch(p.GetProject()) } switch len(found) { case 0: diff --git a/internal/tui/escape.go b/internal/tui/escape.go index 562269e..819eddb 100644 --- a/internal/tui/escape.go +++ b/internal/tui/escape.go @@ -94,15 +94,19 @@ func isSafe(s string, keepNewlines bool) bool { return true } -// unsafeRune reports a C1 control character or a bidirectional format -// character. A bidirectional override can show text in another order than -// the bytes, as in the "Trojan Source" attack. +// unsafeRune reports a C1 control character, a bidirectional format +// character, or a Unicode line or paragraph separator. A bidirectional +// override can show text in another order than the bytes, as in the "Trojan +// Source" attack. Some terminals and editors break a line at U+2028 and +// U+2029, so a server value could add a line that looks like CLI output. func unsafeRune(r rune) bool { switch { case r >= 0x80 && r <= 0x9f: return true case r == 0x061c, r == 0x200e, r == 0x200f: return true + case r == 0x2028, r == 0x2029: + return true case r >= 0x202a && r <= 0x202e: return true case r >= 0x2066 && r <= 0x2069: diff --git a/internal/tui/escape_test.go b/internal/tui/escape_test.go index 6467fc6..2a61f5f 100644 --- a/internal/tui/escape_test.go +++ b/internal/tui/escape_test.go @@ -16,6 +16,7 @@ func TestEscape(t *testing.T) { {name: "ansi sequence", in: "evil\x1b[2J\x1b]0;title\x07", line: `evil\x1b[2J\x1b]0;title\x07`, text: `evil\x1b[2J\x1b]0;title\x07`}, {name: "c1 control", in: "a\u009bb", line: `a\u009bb`, text: `a\u009bb`}, {name: "bidi override", in: "admin\u202e\u2066txt", line: `admin\u202e\u2066txt`, text: `admin\u202e\u2066txt`}, + {name: "line and paragraph separators", in: "a\u2028b\u2029c", line: `a\u2028b\u2029c`, text: `a\u2028b\u2029c`}, {name: "delete", in: "a\x7fb", line: `a\x7fb`, text: `a\x7fb`}, {name: "invalid utf-8", in: "a\xffb", line: "a�b", text: "a�b"}, }