Repository navigation
Show the field of a refused request, and fix the dogfood findings #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e6d13ac
fix(policy): show the field and the message of a refused request
claude 0d8a790
fix: correct the expiry, the block warning, the policy column and the…
claude 935fb69
fix: apply the review of the error and the dogfood fixes
claude b34454e
fix: show project times, round expiry days, and use document names in…
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.