From e6d13ac48ac754f37c53cf88bbeec9c6d299370d Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 06:14:21 +0000 Subject: [PATCH 1/4] fix(policy): show the field and the message of a refused request The dogfood run in safedep/control-tower#1663 found these errors: - safedep policy edit printed only "Invalid request" and "Verify the request parameters and types and try again.." for a min_age out of range and for an override name such as Payments. The message did not name the field, and the editor did not open again. - An unknown selector label prefix showed the raw "rpc error: code = InvalidArgument desc = service execution failed: ..." text in the editor, without the key. - A package rule that selects the same package as a rule at head showed the raw ALREADY_EXISTS message with no next step. The cause: mapChangeError and mapRuleError did not map INVALID_ARGUMENT or ALREADY_EXISTS, so the generic conversion of dry/usefulerror gave the text. The editor wrote err.Error() into the file, and status.FromError on a wrapped error puts the wrapper text into the message. The server sends a protovalidate field path as protobuf text, so isRetryable did not find a document field and the editor did not open again. The change: - describeStatus in errors.go gives the status message and each BadRequest field violation as "field: description". It reads the innermost status, so the "rpc error: code = ... desc =" text never shows. It decodes a field path in protobuf text with protovalidate.FieldPathString. It removes a leading "service execution failed: ", which names no field and no fix. When the message already holds each description, only the violations show. - The editor error lines, failedPreconditionError, and new INVALID_ARGUMENT cases in mapChangeError and mapRuleError use it. - ALREADY_EXISTS on a rule create or a change gives "Package rule r_x already selects this package." with "See it with: safedep policy rule show r_x". The rule id comes from the ResourceInfo detail. Without the detail, the status message shows. - validateSettings runs protovalidate on the edited document, so a value out of range opens the editor again with its field path before the RPC. - keptEditError removes the full stop at the end of the help before it adds ". ", and it drops the default "No additional help" text. Closes safedep/cli#71 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- go.mod | 5 + go.sum | 14 ++ internal/cmd/policy/editor.go | 16 +- internal/cmd/policy/errors.go | 120 +++++++++++- internal/cmd/policy/errors_test.go | 300 +++++++++++++++++++++++++++++ internal/cmd/policy/review_test.go | 12 ++ internal/cmd/policy/settings.go | 35 +++- 7 files changed, 488 insertions(+), 14 deletions(-) create mode 100644 internal/cmd/policy/errors_test.go diff --git a/go.mod b/go.mod index 08c5e11..2512e60 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.12-20260709200747-435963d16310.1 buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261104174030-9113dd6a8ea6.1 buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261104174030-9113dd6a8ea6.2 + buf.build/go/protovalidate v1.2.0 github.com/charmbracelet/huh v1.0.0 github.com/charmbracelet/lipgloss v1.1.0 github.com/cli/browser v1.3.0 @@ -29,6 +30,8 @@ require ( ) require ( + cel.dev/expr v0.25.1 // indirect + github.com/antlr4-go/antlr/v4 v4.13.1 // indirect github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/brunoga/deep v1.3.1 // indirect @@ -51,6 +54,7 @@ require ( github.com/fsnotify/fsnotify v1.10.1 // indirect github.com/go-viper/mapstructure/v2 v2.5.0 // indirect github.com/godbus/dbus/v5 v5.2.2 // indirect + github.com/google/cel-go v0.28.0 // indirect github.com/google/go-github/v74 v74.0.0 // indirect github.com/google/go-querystring v1.2.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect @@ -103,6 +107,7 @@ require ( golang.org/x/net v0.57.0 // indirect golang.org/x/sys v0.47.0 // indirect golang.org/x/tools v0.47.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20260226221140-a57be14db171 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/libc v1.74.4 // indirect diff --git a/go.sum b/go.sum index 65baad8..7b02f43 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,16 @@ buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261104174030-9113dd6a8ea6.1 h1:oKI buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261104174030-9113dd6a8ea6.1/go.mod h1:hpgi3VWa8hReWhVtNpPAjWsU4TPf+lYozGgjrHggWhw= buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261104174030-9113dd6a8ea6.2 h1:zpGu3bDWbHdBkUgeSt45/O/38JbZMEvn55nEf30u5qg= buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261104174030-9113dd6a8ea6.2/go.mod h1:LfbopTmazyAppwhYT2vdRDLDH8ZTm4QNbp9yFjGRbHs= +buf.build/go/protovalidate v1.2.0 h1:DQVrUWkmGTBij+kOYv/x2LLxwcLaGKMdzShj1/6/3H0= +buf.build/go/protovalidate v1.2.0/go.mod h1:7rYiQEhqvAipoazpVNBBH2S2f8bjG4huMVy1V2Yofn4= +cel.dev/expr v0.25.1 h1:1KrZg61W6TWSxuNZ37Xy49ps13NUovb66QLprthtwi4= +cel.dev/expr v0.25.1/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4= 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= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= +github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ= +github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw= github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= @@ -15,6 +21,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY= github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4= +github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs= github.com/brunoga/deep v1.3.1 h1:bSrL6FhAZa6JlVv4vsi7Hg8SLwroDb1kgDERRVipBCo= github.com/brunoga/deep v1.3.1/go.mod h1:GDV6dnXqn80ezsLSZ5Wlv1PdKAWAO4L5PnKYtv2dgaI= github.com/catppuccin/go v0.3.0 h1:d+0/YicIq+hSTo5oPuRi5kOpqkVA5tAsU6dNhvRu+aY= @@ -117,6 +125,8 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/cel-go v0.28.0 h1:KjSWstCpz/MN5t4a8gnGJNIYUsJRpdi/r97xWDphIQc= +github.com/google/cel-go v0.28.0/go.mod h1:X0bD6iVNR8pkROSOoHVdgTkzmRcosof7WQqCD6wcMc8= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= @@ -210,6 +220,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rodaine/protogofakeit v0.1.1 h1:ZKouljuRM3A+TArppfBqnH8tGZHOwM/pjvtXe9DaXH8= +github.com/rodaine/protogofakeit v0.1.1/go.mod h1:pXn/AstBYMaSfc1/RqH3N82pBuxtWgejz1AlYpY1mI0= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= @@ -362,6 +374,8 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto/googleapis/api v0.0.0-20260226221140-a57be14db171 h1:tu/dtnW1o3wfaxCOjSLn5IRX4YDcJrtlpzYkhHhGaC4= +google.golang.org/genproto/googleapis/api v0.0.0-20260226221140-a57be14db171/go.mod h1:M5krXqk4GhBKvB596udGL3UyjL4I1+cTbK0orROM9ng= google.golang.org/genproto/googleapis/rpc v0.0.0-20260729162451-8efbd57d26e0 h1:mJiOtnGp0k/BcSgdu03G2NwnscCfCH+h2QKUBZr18KI= google.golang.org/genproto/googleapis/rpc v0.0.0-20260729162451-8efbd57d26e0/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= diff --git a/internal/cmd/policy/editor.go b/internal/cmd/policy/editor.go index 3b7d885..985054d 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(strings.TrimPrefix(st.Message(), serviceFailedPrefix), ":") 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..f379c49 100644 --- a/internal/cmd/policy/errors.go +++ b/internal/cmd/policy/errors.go @@ -1,13 +1,18 @@ package policy import ( + "errors" "fmt" + "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" + "buf.build/go/protovalidate" "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/tui" ) @@ -26,9 +31,89 @@ 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: " + +// describeStatus 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". 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 describeStatus(err error) string { + st, ok := innerStatus(err) + if !ok { + return err.Error() + } + msg := strings.TrimSpace(st.Message()) + for strings.HasPrefix(msg, serviceFailedPrefix) { + msg = strings.TrimSpace(strings.TrimPrefix(msg, serviceFailedPrefix)) + } + var lines []string + covered := true + if bad, ok := usefulerror.DetailAs[*errdetails.BadRequest](err); ok { + for _, v := range bad.GetFieldViolations() { + line := v.GetDescription() + if field := violationField(v); field != "" { + line = field + ": " + 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 st.Code().String() + } + return strings.Join(lines, "\n") +} + +// 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 path, as in +// "settings.overrides[0].name". The server sends the protovalidate field +// path of a request as protobuf text, which no person can read. +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 protovalidate.FieldPathString(path) +} + +// errorText is the text of err for a person: the human error of a useful +// error in the chain, else the text of describeStatus. +func errorText(err error) string { + 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 +123,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 +177,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 +231,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 @@ -167,7 +275,7 @@ func mapDecisionListError(err error) error { } } 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..d94af96 --- /dev/null +++ b/internal/cmd/policy/errors_test.go @@ -0,0 +1,300 @@ +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) +} + +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: "settings.overrides[0].selector.match_labels: 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: "settings.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" + + "settings.base_description: must be at most 1000 characters", + }, + { + 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"), + want: "line 1: bad", + }, + } + 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: a package rule already selects pkg:npm/leftpad") + 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"), + "value must be greater than or equal to 0s and less than or equal to 8760h0m0s"))), + wantCode: usefulerror.ErrBadRequest, + wantHuman: "API validation error\nsettings.base.package_admission.cooldown.min_age: " + + "value must be greater than or equal to 0s and less than or equal to 8760h0m0s", + 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: "a package rule already selects pkg:npm/leftpad", + 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) { + 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: unknown label prefix "team/"`, + violation(encodedFieldPath("settings", "overrides[0]", "selector"), `unknown label prefix "team/"`)) + } + res := &policyv1.CreatePolicyChangeResponse{} + res.SetChange(openChange("18")) + return res, nil + }) + + ed := &scriptedEditor{edits: []func(string) string{ + replace("custom/criticality", "team/criticality"), + replace("team/criticality", "custom/criticality"), + }} + change, err := runEdit(context.Background(), client, ed.open, "", editInput{}) + require.NoError(t, err) + assert.Equal(t, "18", change.GetChangeId()) + require.Len(t, ed.seen, 2) + assert.Contains(t, ed.seen[1], "# ! settings.overrides[0].selector: unknown label prefix \"team/\"\n") + assert.NotContains(t, ed.seen[1], "rpc error") + assert.NotContains(t, ed.seen[1], "service execution failed") +} + +func TestRunEdit_ValueOutOfRangeReopensBeforeTheServer(t *testing.T) { + tests := []struct { + name string + bad func(string) string + fix func(string) string + want string + }{ + { + name: "a min_age above the limit", + bad: func(s string) string { + s = uncomment(" # package_admission:", " # cooldown:", " # min_age: 7d")(s) + return strings.Replace(s, "min_age: 7d", "min_age: 400d", 1) + }, + fix: replace("min_age: 400d", "min_age: 30d"), + want: "# ! base.package_admission.cooldown.min_age: must be greater than or equal to 0s", + }, + { + name: "an override name with an upper case letter", + bad: replace("name: payments", "name: Payments"), + fix: replace("name: Payments", "name: payments-team"), + want: "# ! overrides[0].name: does not match regex pattern", + }, + } + 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) + var got *policyv1.Policy + createdSettings(client, &got) + + ed := &scriptedEditor{edits: []func(string) string{tt.bad, tt.fix}} + _, err := runEdit(context.Background(), client, ed.open, "", editInput{}) + require.NoError(t, err) + require.Len(t, ed.seen, 2) + assert.Contains(t, ed.seen[1], tt.want) + require.NotNil(t, got) + client.AssertNumberOfCalls(t, "CreatePolicyChange", 1) + }) + } +} diff --git a/internal/cmd/policy/review_test.go b/internal/cmd/policy/review_test.go index a0fbec3..7171598 100644 --- a/internal/cmd/policy/review_test.go +++ b/internal/cmd/policy/review_test.go @@ -85,6 +85,18 @@ 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}, + { + "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/settings.go b/internal/cmd/policy/settings.go index 724b874..a7ad65d 100644 --- a/internal/cmd/policy/settings.go +++ b/internal/cmd/policy/settings.go @@ -9,6 +9,8 @@ import ( messagescontroltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/controltower/v1" policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" + "buf.build/go/protovalidate" + "github.com/safedep/dry/log" "github.com/safedep/dry/tui" "github.com/safedep/dry/usefulerror" "google.golang.org/protobuf/proto" @@ -264,7 +266,38 @@ func validateSettings(p *policyv1.Policy) error { return fmt.Errorf("override %q: give it a selector. Write selector: {} to select every subject", o.GetName()) } } - return nil + return checkProtoRules(p) +} + +// checkProtoRules checks the protovalidate rules of the policy, as the server +// does, and gives each violation with its field path. The server sends its +// field paths as protobuf text, so without this check an edit with a value +// out of range fails with no field name and the editor does not open again. +func checkProtoRules(p *policyv1.Policy) error { + err := protovalidate.Validate(p) + if err == nil { + return nil + } + var verr *protovalidate.ValidationError + if !errors.As(err, &verr) { + log.Warnf("policy edit: cannot check the policy rules before the save: %v", err) + return nil + } + errs := make([]error, 0, len(verr.Violations)) + for _, v := range verr.Violations { + if v == nil || v.Proto == nil { + continue + } + msg := v.Proto.GetMessage() + if path := protovalidate.FieldPathString(v.Proto.GetField()); path != "" { + msg = path + ": " + msg + } + errs = append(errs, errors.New(msg)) + } + if len(errs) == 0 { + return err + } + return errors.Join(errs...) } func warnComments(saved, written []byte) { From 0d8a790a942217b52cc301937e1b58e765153061 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 06:14:21 +0000 Subject: [PATCH 2/4] fix: correct the expiry, the block warning, the policy column and the project list The dogfood run in safedep/control-tower#1663 found these errors: - safedep policy rule show printed the expiry date two times, as "2027-01-04 (2027-01-04)". humanize.Time already gives the date beyond 30 days. expiryText in rule_view.go adds the date only when it is not the humanize text. rule show and rule create use it. - safedep policy rule create --expires 7d printed "expires in 6d". The command read the clock two times, and the server keeps a time to the microsecond. createRules reads the clock one time, with no fraction of a second, and uses that instant for the expiry and for the report. - The warning for a block of every package of an ecosystem said that the block stops every install "in the tenant", also with --override. With --override, it now names the policy override and its subjects. - safedep endpoint list showed "rev 0" in the POLICY column for a pmg with cloud.policy: false. policyCell now skips each tool whose state is disabled, and shows "-" when no tool is left. - The "Project name is ambiguous" error showed the same text for two projects with the same source and URL. It now shows the creation time of each project, after its id. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- docs/cmd/endpoint-list.md | 4 +- internal/cmd/endpoint/list.go | 19 ++++++- internal/cmd/endpoint/policy_test.go | 31 ++++++++++- internal/cmd/endpoint/service.go | 2 +- internal/cmd/policy/rule_create.go | 72 ++++++++++++++----------- internal/cmd/policy/rule_show.go | 2 +- internal/cmd/policy/rule_test.go | 77 +++++++++++++++++++++++++++ internal/cmd/policy/rule_view.go | 35 ++++++++++++ internal/cmd/project/resolver.go | 13 ++++- internal/cmd/project/resolver_test.go | 54 +++++++++++++++++++ 10 files changed, 269 insertions(+), 40 deletions(-) diff --git a/docs/cmd/endpoint-list.md b/docs/cmd/endpoint-list.md index b1b05ac..b257c7a 100644 --- a/docs/cmd/endpoint-list.md +++ b/docs/cmd/endpoint-list.md @@ -33,7 +33,9 @@ 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 does not apply the cloud policy, as pmg with `cloud.policy: false`, is not in +the column. The column shows `-` when no tool applies the cloud policy. JSON output carries `tool_policies` for each endpoint: the tool, its version, its applied policy revision and the state of the policy in the tool. diff --git a/internal/cmd/endpoint/list.go b/internal/cmd/endpoint/list.go index 3f5f471..be72a1e 100644 --- a/internal/cmd/endpoint/list.go +++ b/internal/cmd/endpoint/list.go @@ -9,6 +9,7 @@ import ( "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" controltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/v1" "github.com/safedep/cli/internal/app" "github.com/safedep/cli/internal/cloudpolicy" @@ -420,9 +421,23 @@ func parsePolicyFilters(selector, applied string) (*messagescontroltowerv1.Label return sel, n, nil } +const applyStatePrefix = "APPLY_STATE_" + +// disabledState is the state of a tool that does not apply the cloud +// policy, as a pmg with cloud.policy: false. Its revision is 0 and means +// nothing. +var disabledState = tui.EnumToken(recordv1.ApplyState_APPLY_STATE_DISABLED.String(), applyStatePrefix) + // 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 +// does not apply the cloud policy is not in the cell. +func policyCell(all []ToolPolicy) string { + policies := make([]ToolPolicy, 0, len(all)) + for _, p := range all { + if p.State != disabledState { + 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..1870b67 100644 --- a/internal/cmd/endpoint/policy_test.go +++ b/internal/cmd/endpoint/policy_test.go @@ -106,8 +106,35 @@ 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 disabled tool", []ToolPolicy{{Tool: "pmg", Revision: 0, State: "disabled"}}, "-"}, + { + "a disabled tool and an applied tool", + []ToolPolicy{{Tool: "pmg", Revision: 0, State: "disabled"}, {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 TestList_DisabledToolIsNotInThePolicyCell(t *testing.T) { + report := &recordv1.ApplyReport{} + report.SetState(recordv1.ApplyState_APPLY_STATE_DISABLED) + p := &controltowerv1.EndpointToolPolicy{} + p.SetToolName("pmg") + p.SetPolicy(report) + assert.Equal(t, "-", 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..06956ed 100644 --- a/internal/cmd/endpoint/service.go +++ b/internal/cmd/endpoint/service.go @@ -336,7 +336,7 @@ func toolPolicies(in []*controltowerv1.EndpointToolPolicy) []ToolPolicy { Tool: p.GetToolName(), Version: p.GetToolVersion(), Revision: p.GetPolicy().GetRevision(), - State: tui.EnumToken(p.GetPolicy().GetState().String(), "APPLY_STATE_"), + State: tui.EnumToken(p.GetPolicy().GetState().String(), applyStatePrefix), } if ts := p.GetReportedAt(); ts != nil { tp.ReportedAt = ts.AsTime().UTC() 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..b7c5667 100644 --- a/internal/cmd/policy/rule_show.go +++ b/internal/cmd/policy/rule_show.go @@ -73,7 +73,7 @@ func (r *ruleShowResult) RenderTable() string { } expires := v.expiresCell(now) 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..a1c01d0 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,78 @@ 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)"}, + } + 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, "") + expires := time.Now().Add(65 * 24 * time.Hour) + r.GetRule().SetExpiresAt(timestamppb.New(expires)) + table := (&ruleShowResult{Rule: newRuleView(r, nil)}).RenderTable() + assert.Equal(t, 1, strings.Count(table, expires.UTC().Format(time.DateOnly)), 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..b93c8ed 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" @@ -80,3 +81,37 @@ func (v ruleView) expiresCell(now time.Time) string { } return humanize.Time(*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 := humanize.Time(expires, now) + date := expires.UTC().Format(time.DateOnly) + if rel == date { + return rel + } + return rel + " (" + date + ")" +} + +// 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/project/resolver.go b/internal/cmd/project/resolver.go index c77cbaa..eb22229 100644 --- a/internal/cmd/project/resolver.go +++ b/internal/cmd/project/resolver.go @@ -5,6 +5,7 @@ import ( "fmt" "sort" "strings" + "time" controltowerv1grpc "buf.build/gen/go/safedep/api/grpc/go/safedep/services/controltower/v1/controltowerv1grpc" controltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/v1" @@ -32,6 +33,7 @@ type projectMatch struct { id string source string originURL string + createdAt time.Time } func newListProjectsClient(conn grpc.ClientConnInterface) listProjectsClient { @@ -176,11 +178,15 @@ func collectProjectMatches( if matches[name] == nil { matches[name] = make(map[string]projectMatch) } - matches[name][project.GetProjectId()] = projectMatch{ + match := projectMatch{ id: project.GetProjectId(), source: tui.EnumToken(project.GetSource().String(), "SOURCE_"), originURL: project.GetOriginUrl(), } + if ts := project.GetCreatedAt(); ts != nil { + match.createdAt = ts.AsTime().UTC() + } + matches[name][project.GetProjectId()] = match } return nil } @@ -200,6 +206,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() From 935fb6988d6911f65823d4b05ad9215f45e132c3 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 06:29:00 +0000 Subject: [PATCH 3/4] fix: apply the review of the error and the dogfood fixes The review of e6d13ac and 0d8a790 found these problems: - The local protovalidate check added cel-go and antlr. The binary grew from 45.9 MB to 54.3 MB. The CLI no longer runs protovalidate. The server error opens the editor again with the field, because violationField decodes the field path. violationField now reads the protobuf text with the generated buf.validate.FieldPath type and writes the dotted path itself, with [i] and [key] subscripts. A field that does not parse stays as it is. - The error lines showed a path with the "settings." prefix, but the local checks and the file have no such key. The CLI now shows each document path without "settings.". In an edit of one policy override, a path of that override shows as a path of the file, as in "name" or "spec.scan_gate.fail_on". Paths of other parts stay as they are. - mapDecisionListError compared the raw field with "pagination.page_token", so an encoded path did not match. It now uses violatesField. - isRetryable removed the service prefix one time only, so a doubled prefix made a title error look like an error of the document. stripServicePrefix removes each prefix, and describeStatus and isRetryable use it. - describeStatus did not escape the text of an error that is not a status, showed the code name for an empty message, and wrote "field: " for an empty description. It now escapes the text, shows "The server gave no reason", and shows the field alone. - endpoint list compared the rendered state token. toolPolicies now reads the ApplyState enum. A tool in state DISABLED or NONE has no applied revision, so it is not in the POLICY column. The doc says so. - rule show computed an expiry cell that it did not use. The test now uses fixedNow. - The human text of a duplicate rule ended with a full stop, unlike the other errors of the package. - EscapeLine and EscapeText did not escape U+2028 and U+2029. Some terminals and editors break a line at them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- docs/cmd/endpoint-list.md | 5 +- go.mod | 5 - go.sum | 14 -- internal/cmd/endpoint/list.go | 12 +- internal/cmd/endpoint/policy_test.go | 32 +++-- internal/cmd/endpoint/service.go | 11 +- internal/cmd/policy/decision_test.go | 6 + internal/cmd/policy/editor.go | 2 +- internal/cmd/policy/errors.go | 157 +++++++++++++++++----- internal/cmd/policy/errors_test.go | 188 ++++++++++++++++++--------- internal/cmd/policy/review_test.go | 5 + internal/cmd/policy/rule_show.go | 7 +- internal/cmd/policy/rule_test.go | 7 +- internal/cmd/policy/settings.go | 42 +----- internal/tui/escape.go | 10 +- internal/tui/escape_test.go | 1 + 16 files changed, 324 insertions(+), 180 deletions(-) diff --git a/docs/cmd/endpoint-list.md b/docs/cmd/endpoint-list.md index b257c7a..6800cf0 100644 --- a/docs/cmd/endpoint-list.md +++ b/docs/cmd/endpoint-list.md @@ -34,8 +34,9 @@ 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`. A tool -that does not apply the cloud policy, as pmg with `cloud.policy: false`, is not in -the column. The column shows `-` when no tool applies the cloud policy. JSON +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/go.mod b/go.mod index 2512e60..08c5e11 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.12-20260709200747-435963d16310.1 buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261104174030-9113dd6a8ea6.1 buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261104174030-9113dd6a8ea6.2 - buf.build/go/protovalidate v1.2.0 github.com/charmbracelet/huh v1.0.0 github.com/charmbracelet/lipgloss v1.1.0 github.com/cli/browser v1.3.0 @@ -30,8 +29,6 @@ require ( ) require ( - cel.dev/expr v0.25.1 // indirect - github.com/antlr4-go/antlr/v4 v4.13.1 // indirect github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/brunoga/deep v1.3.1 // indirect @@ -54,7 +51,6 @@ require ( github.com/fsnotify/fsnotify v1.10.1 // indirect github.com/go-viper/mapstructure/v2 v2.5.0 // indirect github.com/godbus/dbus/v5 v5.2.2 // indirect - github.com/google/cel-go v0.28.0 // indirect github.com/google/go-github/v74 v74.0.0 // indirect github.com/google/go-querystring v1.2.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect @@ -107,7 +103,6 @@ require ( golang.org/x/net v0.57.0 // indirect golang.org/x/sys v0.47.0 // indirect golang.org/x/tools v0.47.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20260226221140-a57be14db171 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/libc v1.74.4 // indirect diff --git a/go.sum b/go.sum index 7b02f43..65baad8 100644 --- a/go.sum +++ b/go.sum @@ -4,16 +4,10 @@ buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261104174030-9113dd6a8ea6.1 h1:oKI buf.build/gen/go/safedep/api/grpc/go v1.6.2-20261104174030-9113dd6a8ea6.1/go.mod h1:hpgi3VWa8hReWhVtNpPAjWsU4TPf+lYozGgjrHggWhw= buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261104174030-9113dd6a8ea6.2 h1:zpGu3bDWbHdBkUgeSt45/O/38JbZMEvn55nEf30u5qg= buf.build/gen/go/safedep/api/protocolbuffers/go v1.36.12-20261104174030-9113dd6a8ea6.2/go.mod h1:LfbopTmazyAppwhYT2vdRDLDH8ZTm4QNbp9yFjGRbHs= -buf.build/go/protovalidate v1.2.0 h1:DQVrUWkmGTBij+kOYv/x2LLxwcLaGKMdzShj1/6/3H0= -buf.build/go/protovalidate v1.2.0/go.mod h1:7rYiQEhqvAipoazpVNBBH2S2f8bjG4huMVy1V2Yofn4= -cel.dev/expr v0.25.1 h1:1KrZg61W6TWSxuNZ37Xy49ps13NUovb66QLprthtwi4= -cel.dev/expr v0.25.1/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4= 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= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= -github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ= -github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw= github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= @@ -21,8 +15,6 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY= github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= -github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4= -github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs= github.com/brunoga/deep v1.3.1 h1:bSrL6FhAZa6JlVv4vsi7Hg8SLwroDb1kgDERRVipBCo= github.com/brunoga/deep v1.3.1/go.mod h1:GDV6dnXqn80ezsLSZ5Wlv1PdKAWAO4L5PnKYtv2dgaI= github.com/catppuccin/go v0.3.0 h1:d+0/YicIq+hSTo5oPuRi5kOpqkVA5tAsU6dNhvRu+aY= @@ -125,8 +117,6 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= -github.com/google/cel-go v0.28.0 h1:KjSWstCpz/MN5t4a8gnGJNIYUsJRpdi/r97xWDphIQc= -github.com/google/cel-go v0.28.0/go.mod h1:X0bD6iVNR8pkROSOoHVdgTkzmRcosof7WQqCD6wcMc8= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= @@ -220,8 +210,6 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/rodaine/protogofakeit v0.1.1 h1:ZKouljuRM3A+TArppfBqnH8tGZHOwM/pjvtXe9DaXH8= -github.com/rodaine/protogofakeit v0.1.1/go.mod h1:pXn/AstBYMaSfc1/RqH3N82pBuxtWgejz1AlYpY1mI0= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= @@ -374,8 +362,6 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto/googleapis/api v0.0.0-20260226221140-a57be14db171 h1:tu/dtnW1o3wfaxCOjSLn5IRX4YDcJrtlpzYkhHhGaC4= -google.golang.org/genproto/googleapis/api v0.0.0-20260226221140-a57be14db171/go.mod h1:M5krXqk4GhBKvB596udGL3UyjL4I1+cTbK0orROM9ng= google.golang.org/genproto/googleapis/rpc v0.0.0-20260729162451-8efbd57d26e0 h1:mJiOtnGp0k/BcSgdu03G2NwnscCfCH+h2QKUBZr18KI= google.golang.org/genproto/googleapis/rpc v0.0.0-20260729162451-8efbd57d26e0/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= diff --git a/internal/cmd/endpoint/list.go b/internal/cmd/endpoint/list.go index be72a1e..d17efe2 100644 --- a/internal/cmd/endpoint/list.go +++ b/internal/cmd/endpoint/list.go @@ -9,7 +9,6 @@ import ( "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" controltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/v1" "github.com/safedep/cli/internal/app" "github.com/safedep/cli/internal/cloudpolicy" @@ -421,20 +420,13 @@ func parsePolicyFilters(selector, applied string) (*messagescontroltowerv1.Label return sel, n, nil } -const applyStatePrefix = "APPLY_STATE_" - -// disabledState is the state of a tool that does not apply the cloud -// policy, as a pmg with cloud.policy: false. Its revision is 0 and means -// nothing. -var disabledState = tui.EnumToken(recordv1.ApplyState_APPLY_STATE_DISABLED.String(), applyStatePrefix) - // policyCell shows the applied policy revision of each tool, as "rev 47" when // every tool applies the same revision, or "pmg 47, vet 46". A tool that -// does not apply the cloud policy is not in the cell. +// 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.State != disabledState { + if !p.noRevision { policies = append(policies, p) } } diff --git a/internal/cmd/endpoint/policy_test.go b/internal/cmd/endpoint/policy_test.go index 1870b67..524de8f 100644 --- a/internal/cmd/endpoint/policy_test.go +++ b/internal/cmd/endpoint/policy_test.go @@ -114,10 +114,10 @@ func TestPolicyCell(t *testing.T) { {"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 disabled tool", []ToolPolicy{{Tool: "pmg", Revision: 0, State: "disabled"}}, "-"}, + {"only a tool with no revision", []ToolPolicy{{Tool: "pmg", noRevision: true}}, "-"}, { - "a disabled tool and an applied tool", - []ToolPolicy{{Tool: "pmg", Revision: 0, State: "disabled"}, {Tool: "vet", Revision: 46, State: "applied"}}, + "a tool with no revision and an applied tool", + []ToolPolicy{{Tool: "pmg", noRevision: true}, {Tool: "vet", Revision: 46, State: "applied"}}, "rev 46", }, } @@ -128,13 +128,25 @@ func TestPolicyCell(t *testing.T) { } } -func TestList_DisabledToolIsNotInThePolicyCell(t *testing.T) { - report := &recordv1.ApplyReport{} - report.SetState(recordv1.ApplyState_APPLY_STATE_DISABLED) - p := &controltowerv1.EndpointToolPolicy{} - p.SetToolName("pmg") - p.SetPolicy(report) - assert.Equal(t, "-", policyCell(toolPolicies([]*controltowerv1.EndpointToolPolicy{p}))) +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 06956ed..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 @@ -336,7 +341,11 @@ func toolPolicies(in []*controltowerv1.EndpointToolPolicy) []ToolPolicy { Tool: p.GetToolName(), Version: p.GetToolVersion(), Revision: p.GetPolicy().GetRevision(), - State: tui.EnumToken(p.GetPolicy().GetState().String(), applyStatePrefix), + 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 985054d..136985b 100644 --- a/internal/cmd/policy/editor.go +++ b/internal/cmd/policy/editor.go @@ -62,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(strings.TrimPrefix(st.Message(), serviceFailedPrefix), ":") + field, _, found := strings.Cut(stripServicePrefix(st.Message()), ":") return !found || strings.ContainsAny(field, " ") || isDocumentField(field) } diff --git a/internal/cmd/policy/errors.go b/internal/cmd/policy/errors.go index f379c49..07ac062 100644 --- a/internal/cmd/policy/errors.go +++ b/internal/cmd/policy/errors.go @@ -3,11 +3,11 @@ 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" - "buf.build/go/protovalidate" "github.com/safedep/dry/usefulerror" "google.golang.org/genproto/googleapis/rpc/errdetails" "google.golang.org/grpc/codes" @@ -47,30 +47,47 @@ func invalidArgumentError(err error, help string, cause error) error { // a service error. It names no field and no fix, so the CLI removes it. const serviceFailedPrefix = "service execution failed: " -// describeStatus 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". 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. +// 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) string { st, ok := innerStatus(err) if !ok { - return err.Error() - } - msg := strings.TrimSpace(st.Message()) - for strings.HasPrefix(msg, serviceFailedPrefix) { - msg = strings.TrimSpace(strings.TrimPrefix(msg, serviceFailedPrefix)) + 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() { - line := v.GetDescription() - if field := violationField(v); field != "" { - line = field + ": " + line + if line := violationLine(path(violationField(v)), v.GetDescription()); line != "" { + lines = append(lines, tui.EscapeLine(line)) } - lines = append(lines, tui.EscapeLine(line)) covered = covered && v.GetDescription() != "" && strings.Contains(msg, v.GetDescription()) } } @@ -78,11 +95,21 @@ func describeStatus(err error) string { lines = append([]string{tui.EscapeText(msg)}, lines...) } if len(lines) == 0 { - return st.Code().String() + 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) { @@ -94,21 +121,91 @@ func innerStatus(err error) (*status.Status, bool) { return st, st != nil } -// violationField is the field of a violation as a path, as in +// 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 protobuf text, which no person can read. +// 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 protovalidate.FieldPathString(path) + return fieldPathString(path) } -// errorText is the text of err for a person: the human error of a useful -// error in the chain, else the text of describeStatus. +// 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." + +func documentPath(field string) string { + return strings.TrimPrefix(field, documentFieldPrefix) +} + +// 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 { + field = documentPath(field) + prefix := fmt.Sprintf("overrides[%d]", e.index) + if field == prefix { + return "" + } + if rest, ok := strings.CutPrefix(field, prefix+"."); ok { + return rest + } + return field +} + +// 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() @@ -137,7 +234,7 @@ 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), + fmt.Sprintf("Package rule %s already selects this package", id), "See it with: safedep policy rule show "+id, cause) } @@ -264,15 +361,11 @@ 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: "+describeStatus(err), diff --git a/internal/cmd/policy/errors_test.go b/internal/cmd/policy/errors_test.go index d94af96..e2390ec 100644 --- a/internal/cmd/policy/errors_test.go +++ b/internal/cmd/policy/errors_test.go @@ -45,6 +45,21 @@ func encodedFieldPath(names ...string) string { 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 { @@ -84,7 +99,7 @@ func TestDescribeStatus(t *testing.T) { err: statusWithViolations(codes.InvalidArgument, "service execution failed: unknown label prefix \"team/\"", violation("settings.overrides[0].selector.match_labels", "unknown label prefix \"team/\"")), - want: "settings.overrides[0].selector.match_labels: unknown label prefix \"team/\"", + want: "overrides[0].selector.match_labels: unknown label prefix \"team/\"", }, { name: "a protovalidate violation with an encoded field path", @@ -93,7 +108,7 @@ func TestDescribeStatus(t *testing.T) { "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: "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", @@ -101,7 +116,28 @@ func TestDescribeStatus(t *testing.T) { 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" + - "settings.base_description: must be at most 1000 characters", + "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.match_labels["team/x"]: unknown label prefix`, }, { name: "control characters from the server", @@ -110,8 +146,8 @@ func TestDescribeStatus(t *testing.T) { }, { name: "an error that is not a status", - err: errors.New("line 1: bad"), - want: "line 1: bad", + err: errors.New("line 1: bad\x1b[2J\nline 2"), + want: "line 1: bad\\x1b[2J\nline 2", }, } for _, tt := range tests { @@ -124,7 +160,8 @@ func TestDescribeStatus(t *testing.T) { } func alreadyExists(ruleID string) error { - st := status.New(codes.AlreadyExists, "service execution failed: a package rule already selects pkg:npm/leftpad") + 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() } @@ -150,7 +187,7 @@ func TestServerErrorMapping_InvalidArgumentAndAlreadyExists(t *testing.T) { violation(encodedFieldPath("settings", "base", "package_admission", "cooldown", "min_age"), "value must be greater than or equal to 0s and less than or equal to 8760h0m0s"))), wantCode: usefulerror.ErrBadRequest, - wantHuman: "API validation error\nsettings.base.package_admission.cooldown.min_age: " + + wantHuman: "API validation error\nbase.package_admission.cooldown.min_age: " + "value must be greater than or equal to 0s and less than or equal to 8760h0m0s", wantHelp: "Correct the values and run the command again", }, @@ -165,22 +202,23 @@ func TestServerErrorMapping_InvalidArgumentAndAlreadyExists(t *testing.T) { 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.", + 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.", + 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: "a package rule already selects pkg:npm/leftpad", - wantHelp: "List the rules: safedep policy rule list", + 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", @@ -229,72 +267,100 @@ func TestKeptEditError_Help(t *testing.T) { } func TestRunEdit_ServerFieldErrorReopensWithTheField(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: unknown label prefix "team/"`, - violation(encodedFieldPath("settings", "overrides[0]", "selector"), `unknown label prefix "team/"`)) - } - res := &policyv1.CreatePolicyChangeResponse{} - res.SetChange(openChange("18")) - return res, nil - }) - - ed := &scriptedEditor{edits: []func(string) string{ - replace("custom/criticality", "team/criticality"), - replace("team/criticality", "custom/criticality"), - }} - change, err := runEdit(context.Background(), client, ed.open, "", editInput{}) - require.NoError(t, err) - assert.Equal(t, "18", change.GetChangeId()) - require.Len(t, ed.seen, 2) - assert.Contains(t, ed.seen[1], "# ! settings.overrides[0].selector: unknown label prefix \"team/\"\n") - assert.NotContains(t, ed.seen[1], "rpc error") - assert.NotContains(t, ed.seen[1], "service execution failed") -} - -func TestRunEdit_ValueOutOfRangeReopensBeforeTheServer(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 - bad func(string) string - fix func(string) string - want string + name string + override string + bad, fix func(string) string + violation *errdetails.BadRequest_FieldViolation + want string }{ { name: "a min_age above the limit", - bad: func(s string) string { - s = uncomment(" # package_admission:", " # cooldown:", " # min_age: 7d")(s) - return strings.Replace(s, "min_age: 7d", "min_age: 400d", 1) - }, + 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", }, { - name: "an override name with an upper case letter", - bad: replace("name: payments", "name: Payments"), - fix: replace("name: Payments", "name: payments-team"), - want: "# ! overrides[0].name: does not match regex pattern", + 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", `unknown label prefix "team/"`), + want: "# ! overrides[0].selector: 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) - var got *policyv1.Policy - createdSettings(client, &got) + 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}} - _, err := runEdit(context.Background(), client, ed.open, "", editInput{}) + 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) - require.NotNil(t, got) - client.AssertNumberOfCalls(t, "CreatePolicyChange", 1) + 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 7171598..4bc92d1 100644 --- a/internal/cmd/policy/review_test.go +++ b/internal/cmd/policy/review_test.go @@ -91,6 +91,11 @@ func TestIsRetryable(t *testing.T) { 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")), diff --git a/internal/cmd/policy/rule_show.go b/internal/cmd/policy/rule_show.go index b7c5667..5792c19 100644 --- a/internal/cmd/policy/rule_show.go +++ b/internal/cmd/policy/rule_show.go @@ -65,13 +65,16 @@ 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 = expiryText(*v.expires, now) } diff --git a/internal/cmd/policy/rule_test.go b/internal/cmd/policy/rule_test.go index a1c01d0..ddda901 100644 --- a/internal/cmd/policy/rule_test.go +++ b/internal/cmd/policy/rule_test.go @@ -502,10 +502,9 @@ func TestExpiryText(t *testing.T) { func TestRuleShow_ShowsTheExpiryDateOneTime(t *testing.T) { r := ruleWithAttributes("r_1", corev1.Effect_EFFECT_TRUST, "") - expires := time.Now().Add(65 * 24 * time.Hour) - r.GetRule().SetExpiresAt(timestamppb.New(expires)) - table := (&ruleShowResult{Rule: newRuleView(r, nil)}).RenderTable() - assert.Equal(t, 1, strings.Count(table, expires.UTC().Format(time.DateOnly)), table) + 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) { diff --git a/internal/cmd/policy/settings.go b/internal/cmd/policy/settings.go index a7ad65d..5cbec4a 100644 --- a/internal/cmd/policy/settings.go +++ b/internal/cmd/policy/settings.go @@ -9,8 +9,6 @@ import ( messagescontroltowerv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/controltower/v1" policyv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/controltower/policy/v1" - "buf.build/go/protovalidate" - "github.com/safedep/dry/log" "github.com/safedep/dry/tui" "github.com/safedep/dry/usefulerror" "google.golang.org/protobuf/proto" @@ -136,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) @@ -147,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 }) } @@ -266,38 +269,7 @@ func validateSettings(p *policyv1.Policy) error { return fmt.Errorf("override %q: give it a selector. Write selector: {} to select every subject", o.GetName()) } } - return checkProtoRules(p) -} - -// checkProtoRules checks the protovalidate rules of the policy, as the server -// does, and gives each violation with its field path. The server sends its -// field paths as protobuf text, so without this check an edit with a value -// out of range fails with no field name and the editor does not open again. -func checkProtoRules(p *policyv1.Policy) error { - err := protovalidate.Validate(p) - if err == nil { - return nil - } - var verr *protovalidate.ValidationError - if !errors.As(err, &verr) { - log.Warnf("policy edit: cannot check the policy rules before the save: %v", err) - return nil - } - errs := make([]error, 0, len(verr.Violations)) - for _, v := range verr.Violations { - if v == nil || v.Proto == nil { - continue - } - msg := v.Proto.GetMessage() - if path := protovalidate.FieldPathString(v.Proto.GetField()); path != "" { - msg = path + ": " + msg - } - errs = append(errs, errors.New(msg)) - } - if len(errs) == 0 { - return err - } - return errors.Join(errs...) + return nil } func warnComments(saved, written []byte) { 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"}, } From b34454e9a1159e361c8ab92e0f7d9dfaca1ea59c Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 11:34:40 +0000 Subject: [PATCH 4/4] fix: show project times, round expiry days, and use document names in errors A test on a live tenant found these problems: - project show and project policy show built their own project match with no creation time. Two projects with one name still showed the same text. newProjectMatch now builds each match, so every lookup by name shows the creation time. - expiryText dropped the part of a day, so rule create --expires 7d showed "in 7d" and rule show a few seconds later showed "in 6d". The expiry text now rounds a time of a day or more to the nearest day. The date rule stays. rule list uses the same text. - An error path kept the protobuf field names, as "selector.match_labels", but the edit document writes "matchLabels" and "matchExpressions". cloudpolicy.DocumentPath now maps each part of a path to the key that the document writes, by the Policy descriptor and the rules of the document codec. Indexes and map keys stay as they are. - A duration limit from protovalidate showed in seconds, as "31536000s", while the file notes say "365d". For a duration field, the CLI now writes each whole number of days in the form of the notes. "0s" stays "0s". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MhRi597EX9odoSDXbVBsxz --- internal/cloudpolicy/docpath.go | 151 +++++++++++++++++++++++++++ internal/cloudpolicy/docpath_test.go | 53 ++++++++++ internal/cmd/policy/errors.go | 31 ++++-- internal/cmd/policy/errors_test.go | 16 +-- internal/cmd/policy/rule_test.go | 5 + internal/cmd/policy/rule_view.go | 19 +++- internal/cmd/project/policy_test.go | 21 ++++ internal/cmd/project/resolver.go | 25 +++-- internal/cmd/project/show.go | 6 +- 9 files changed, 294 insertions(+), 33 deletions(-) create mode 100644 internal/cloudpolicy/docpath.go create mode 100644 internal/cloudpolicy/docpath_test.go 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/policy/errors.go b/internal/cmd/policy/errors.go index 07ac062..ebf7ef9 100644 --- a/internal/cmd/policy/errors.go +++ b/internal/cmd/policy/errors.go @@ -14,6 +14,7 @@ import ( "google.golang.org/grpc/status" "google.golang.org/protobuf/encoding/prototext" + "github.com/safedep/cli/internal/cloudpolicy" "github.com/safedep/cli/internal/tui" ) @@ -75,7 +76,7 @@ func describeStatus(err error) string { // 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) string { +func describeStatusAt(err error, path func(string) (string, bool)) string { st, ok := innerStatus(err) if !ok { return tui.EscapeText(err.Error()) @@ -85,7 +86,12 @@ func describeStatusAt(err error, path func(string) string) string { covered := true if bad, ok := usefulerror.DetailAs[*errdetails.BadRequest](err); ok { for _, v := range bad.GetFieldViolations() { - if line := violationLine(path(violationField(v)), v.GetDescription()); line != "" { + 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()) @@ -170,8 +176,15 @@ func fieldPathString(path *validate.FieldPath) string { // path names a key of the file. const documentFieldPrefix = "settings." -func documentPath(field string) string { - return strings.TrimPrefix(field, documentFieldPrefix) +// 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. @@ -185,16 +198,16 @@ type overrideFieldError struct { func (e *overrideFieldError) Error() string { return e.err.Error() } func (e *overrideFieldError) Unwrap() error { return e.err } -func (e *overrideFieldError) path(field string) string { - field = documentPath(field) +func (e *overrideFieldError) path(field string) (string, bool) { + field, isDuration := documentPath(field) prefix := fmt.Sprintf("overrides[%d]", e.index) if field == prefix { - return "" + return "", isDuration } if rest, ok := strings.CutPrefix(field, prefix+"."); ok { - return rest + return rest, isDuration } - return field + return field, isDuration } // errorText is the text of err for a person, for the error lines of the diff --git a/internal/cmd/policy/errors_test.go b/internal/cmd/policy/errors_test.go index e2390ec..d42deb4 100644 --- a/internal/cmd/policy/errors_test.go +++ b/internal/cmd/policy/errors_test.go @@ -99,7 +99,7 @@ func TestDescribeStatus(t *testing.T) { 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.match_labels: unknown label prefix \"team/\"", + want: "overrides[0].selector.matchLabels: unknown label prefix \"team/\"", }, { name: "a protovalidate violation with an encoded field path", @@ -137,7 +137,7 @@ func TestDescribeStatus(t *testing.T) { name: "map key subscripts", err: statusWithViolations(codes.InvalidArgument, "", violation(prototext.Format(mapKeyPath()), "unknown label prefix")), - want: `overrides[2].selector.match_labels["team/x"]: unknown label prefix`, + want: `overrides[2].selector.matchLabels["team/x"]: unknown label prefix`, }, { name: "control characters from the server", @@ -185,10 +185,10 @@ func TestServerErrorMapping_InvalidArgumentAndAlreadyExists(t *testing.T) { err: mapChangeError("create", "", statusWithViolations(codes.InvalidArgument, "service execution failed: API validation error", violation(encodedFieldPath("settings", "base", "package_admission", "cooldown", "min_age"), - "value must be greater than or equal to 0s and less than or equal to 8760h0m0s"))), + "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: " + - "value must be greater than or equal to 0s and less than or equal to 8760h0m0s", + "must be greater than or equal to 0s and less than or equal to 365d", wantHelp: "Correct the values and run the command again", }, { @@ -284,7 +284,7 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { fix: replace("min_age: 400d", "min_age: 30d"), violation: violation(encodedFieldPath("settings", "base", "package_admission", "cooldown", "min_age"), "must be greater than or equal to 0s and less than or equal to 31536000s"), - want: "# ! base.package_admission.cooldown.min_age: must be greater than or equal to 0s", + 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", @@ -296,9 +296,9 @@ func TestRunEdit_ServerFieldErrorReopensWithTheField(t *testing.T) { { name: "an unknown selector label prefix", bad: replace("custom/criticality", "team/criticality"), - fix: replace("team/criticality", "custom/criticality"), - violation: violation("settings.overrides[0].selector", `unknown label prefix "team/"`), - want: "# ! overrides[0].selector: unknown label prefix \"team/\"\n", + 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", diff --git a/internal/cmd/policy/rule_test.go b/internal/cmd/policy/rule_test.go index ddda901..a15e88c 100644 --- a/internal/cmd/policy/rule_test.go +++ b/internal/cmd/policy/rule_test.go @@ -492,6 +492,11 @@ func TestExpiryText(t *testing.T) { {"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) { diff --git a/internal/cmd/policy/rule_view.go b/internal/cmd/policy/rule_view.go index b93c8ed..798fe30 100644 --- a/internal/cmd/policy/rule_view.go +++ b/internal/cmd/policy/rule_view.go @@ -79,14 +79,14 @@ 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 := humanize.Time(expires, now) + rel := expiryRelative(expires, now) date := expires.UTC().Format(time.DateOnly) if rel == date { return rel @@ -94,6 +94,21 @@ func expiryText(expires, now time.Time) string { 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 { 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 eb22229..a59443d 100644 --- a/internal/cmd/project/resolver.go +++ b/internal/cmd/project/resolver.go @@ -8,6 +8,7 @@ import ( "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" @@ -178,19 +179,25 @@ func collectProjectMatches( if matches[name] == nil { matches[name] = make(map[string]projectMatch) } - match := projectMatch{ - id: project.GetProjectId(), - source: tui.EnumToken(project.GetSource().String(), "SOURCE_"), - originURL: project.GetOriginUrl(), - } - if ts := project.GetCreatedAt(); ts != nil { - match.createdAt = ts.AsTime().UTC() - } - matches[name][project.GetProjectId()] = match + 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 { 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: