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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/cmd/endpoint-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ safedep endpoint list --applied-revision '<48'
```

The `Policy` column shows the applied policy revision that each tool reported:
`rev 48` when every tool applies the same revision, or `pmg 48, vet 47`. JSON
`rev 48` when every tool applies the same revision, or `pmg 48, vet 47`. A tool
that applies no cloud policy is not in the column: its state is `disabled`, as for
pmg with `cloud.policy: false`, or `none`, when the tool has no policy cache yet and applies its
baseline. The column shows `-` when no tool is left. JSON
output carries `tool_policies` for each endpoint: the tool, its version, its
applied policy revision and the state of the policy in the tool.

Expand Down
151 changes: 151 additions & 0 deletions internal/cloudpolicy/docpath.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
53 changes: 53 additions & 0 deletions internal/cloudpolicy/docpath_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}
11 changes: 9 additions & 2 deletions internal/cmd/endpoint/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,15 @@ func parsePolicyFilters(selector, applied string) (*messagescontroltowerv1.Label
}

// policyCell shows the applied policy revision of each tool, as "rev 47" when
// every tool applies the same revision, or "pmg 47, vet 46".
func policyCell(policies []ToolPolicy) string {
// every tool applies the same revision, or "pmg 47, vet 46". A tool that
// applies no cloud policy is not in the cell.
func policyCell(all []ToolPolicy) string {
policies := make([]ToolPolicy, 0, len(all))
for _, p := range all {
if !p.noRevision {
policies = append(policies, p)
}
}
if len(policies) == 0 {
return "-"
}
Expand Down
43 changes: 41 additions & 2 deletions internal/cmd/endpoint/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,47 @@ func TestParsePolicyFilters(t *testing.T) {
}

func TestPolicyCell(t *testing.T) {
assert.Equal(t, "-", policyCell(nil))
assert.Equal(t, "rev 47", policyCell([]ToolPolicy{{Tool: "pmg", Revision: 47}, {Tool: "vet", Revision: 47}}))
tests := []struct {
name string
policies []ToolPolicy
want string
}{
{"no tool", nil, "-"},
{"one revision", []ToolPolicy{{Tool: "pmg", Revision: 47}, {Tool: "vet", Revision: 47}}, "rev 47"},
{"two revisions", []ToolPolicy{{Tool: "pmg", Revision: 47}, {Tool: "vet", Revision: 46}}, "pmg 47, vet 46"},
{"only a tool with no revision", []ToolPolicy{{Tool: "pmg", noRevision: true}}, "-"},
{
"a tool with no revision and an applied tool",
[]ToolPolicy{{Tool: "pmg", noRevision: true}, {Tool: "vet", Revision: 46, State: "applied"}},
"rev 46",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, policyCell(tt.policies))
})
}
}

func TestToolPolicies_StatesWithNoRevisionAreNotInThePolicyCell(t *testing.T) {
tests := []struct {
state recordv1.ApplyState
want string
}{
{recordv1.ApplyState_APPLY_STATE_DISABLED, "-"},
{recordv1.ApplyState_APPLY_STATE_NONE, "-"},
{recordv1.ApplyState_APPLY_STATE_STALE, "rev 0"},
}
for _, tt := range tests {
t.Run(tt.state.String(), func(t *testing.T) {
report := &recordv1.ApplyReport{}
report.SetState(tt.state)
p := &controltowerv1.EndpointToolPolicy{}
p.SetToolName("pmg")
p.SetPolicy(report)
assert.Equal(t, tt.want, policyCell(toolPolicies([]*controltowerv1.EndpointToolPolicy{p})))
})
}
}

func TestRunList_ShowsTheAppliedRevision(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/endpoint/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -338,6 +343,10 @@ func toolPolicies(in []*controltowerv1.EndpointToolPolicy) []ToolPolicy {
Revision: p.GetPolicy().GetRevision(),
State: tui.EnumToken(p.GetPolicy().GetState().String(), "APPLY_STATE_"),
}
switch p.GetPolicy().GetState() {
case recordv1.ApplyState_APPLY_STATE_DISABLED, recordv1.ApplyState_APPLY_STATE_NONE:
tp.noRevision = true
Comment thread
abhisek marked this conversation as resolved.
Resolved
}
if ts := p.GetReportedAt(); ts != nil {
tp.ReportedAt = ts.AsTime().UTC()
}
Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/policy/decision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
16 changes: 9 additions & 7 deletions internal/cmd/policy/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -49,21 +48,21 @@ 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
}
}
return false
}
// With no field detail, the message starts with the field, as in
// "title: must be at most 200 characters".
field, _, found := strings.Cut(st.Message(), ":")
field, _, found := strings.Cut(stripServicePrefix(st.Message()), ":")
return !found || strings.ContainsAny(field, " ") || isDocumentField(field)
}

Expand Down Expand Up @@ -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")
Expand All @@ -181,15 +180,18 @@ 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.
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().
Expand Down
Loading
Loading