-
Notifications
You must be signed in to change notification settings - Fork 43
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
Add proto validation for RuleType #4907
Conversation
a0f1896
to
0de2694
Compare
0de2694
to
86c8194
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only got partway through, this is a big one.
proto/minder/v1/minder.proto
Outdated
string body = 2; | ||
string body = 2 [ | ||
(buf.validate.field).string = { | ||
pattern: "^\\s*\\{.*\\}\\s*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what this is trying to match. This may be a case where we can't qualify the contents or need to use CEL to do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's supposed to be JSON there. so I added the following ^\\s*\\{[\\s\\S]*\\}\\s*$
which should check for opening/closing bracket and match any character including new lines
proto/minder/v1/minder.proto
Outdated
max_len: 1000, | ||
}, | ||
(buf.validate.field).ignore = IGNORE_IF_DEFAULT_VALUE | ||
]; | ||
} | ||
|
||
// endpoint is the endpoint to fetch data from. | ||
// This can be a URL or the path on the API.bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what .bool means here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's perhaps pool or a typo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect it's a typo. Want to fix it?
proto/minder/v1/minder.proto
Outdated
string endpoint = 1; | ||
string endpoint = 1 [ | ||
(buf.validate.field).string = { | ||
pattern: "^(https:\\/\\/[a-zA-Z0-9.-]+)?(\\/[a-zA-Z0-9._-]+)*(\\/\\{\\{[^}]+\\}\\})*(\\/)?$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a "URI with Go template values embedded"? Again, CEL might be an option, or combining prefix with some other expressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I updated it to "^(https?:\\/\\/)?[a-zA-Z0-9._-]+(\\/[a-zA-Z0-9._{}\\/-]*)?$"
which should allow for:
- "https://api.something.com"
- "https://api.something.com/path/to/resource"
- "/repos/{{.Entity.Owner}}/{{.Entity.Name}}/branches/main"
- "{{ $branch_param := index .Params "branch" }}/repos/{{.Entity.Owner}}/{{.Entity.Name}}"
proto/minder/v1/minder.proto
Outdated
string method = 2; | ||
string method = 2 [ | ||
(buf.validate.field).string = { | ||
pattern: "^(GET|POST|PUT|PATCH|DELETE)$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer in
Is capitalization important?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, made it case insensitive 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in
doesn't work though as I couldn't find an option to ignore casing in parallel 👍 The other way was to expand the list with upper and lower case values which seems odd
proto/minder/v1/minder.proto
Outdated
(buf.validate.field).repeated = { | ||
items: { | ||
string: { | ||
pattern: "^[a-zA-Z0-9-]+: .+$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to limit the RHS to avoid \n
, among others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adressed 👍
proto/minder/v1/minder.proto
Outdated
string branch = 2; | ||
string branch = 2 [ | ||
(buf.validate.field).string = { | ||
pattern: "^[a-zA-Z0-9._/-]+$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^[[:word:]./-]+$
?
proto/minder/v1/minder.proto
Outdated
string version = 11; | ||
string version = 11 [ | ||
(buf.validate.field).string = { | ||
pattern: "^v\\d+(\\.\\d+)?(\\.\\d+)?(?:-[\\w\\.-]+)?$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I'd prefer just v1
than v1.1.7
here for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 👍
proto/minder/v1/minder.proto
Outdated
string short_failure_message = 10; | ||
string short_failure_message = 10 [ | ||
(buf.validate.field).string = { | ||
pattern: "[A-Za-z][-/.!?,:;[:word:] *#\\[\\]()`{}]*", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this markdown, or plain-text?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be markdown
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be a case where we want to simply say "this field is markdown", and then do validation in the code, rather than via annotation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the pattern check but left the length one 👍
proto/minder/v1/minder.proto
Outdated
string type = 1; | ||
string type = 1 [ | ||
(buf.validate.field).string = { | ||
pattern: "^[a-z]+(_[a-z]+)*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use in
, or are there more types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are, i.e. recently we added yq
I believe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is still a small enum, I'd use those values, as it also provides documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored it to using in
👍
proto/minder/v1/minder.proto
Outdated
string def = 1; | ||
string def = 1 [ | ||
(buf.validate.field).string = { | ||
pattern: "^\\.[a-z_]+(\\.[a-z_]+)*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, this would have fixed my bad ruletype debugging experience! Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to accommodate "
or array access ([]
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 👍
50b8d78
to
6892cd7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I got through the rest of this one.
At some point, we should look at creating predefined constraints for a lot of our use cases. For example:
- rule_type_name, markdown_with_go_templates, profile_name, file_name etc.
I wouldn't do that here at this point, because it looks like it might be a lot of toolchaining, but we should leave a note somewhere for when someone is looking for a mid-sized cleanup project.
proto/minder/v1/minder.proto
Outdated
string depfile = 2; | ||
string depfile = 2 [ | ||
(buf.validate.field).string = { | ||
pattern: "^(\\./)?([a-zA-Z0-9_]+\\/)*[a-zA-Z0-9_]+(\\.[a-zA-Z0-9]+)?$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pattern: "^(\\./)?([a-zA-Z0-9_]+\\/)*[a-zA-Z0-9_]+(\\.[a-zA-Z0-9]+)?$", | |
pattern: "^(\\./)?([[:word:]]+\\/)*[[:word:]]+(\\.[[:alnum:]]+)?$", |
Though, it seems like we should allow for .
to show up in other file parts, so maybe:
pattern: "^(\\./)?([a-zA-Z0-9_]+\\/)*[a-zA-Z0-9_]+(\\.[a-zA-Z0-9]+)?$", | |
pattern: "^([[:word:].]+\\/)*[[:word:].]+$", |
proto/minder/v1/minder.proto
Outdated
string short_failure_message = 10; | ||
string short_failure_message = 10 [ | ||
(buf.validate.field).string = { | ||
pattern: "[A-Za-z][-/.!?,:;[:word:] *#\\[\\]()`{}]*", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be a case where we want to simply say "this field is markdown", and then do validation in the code, rather than via annotation.
proto/minder/v1/minder.proto
Outdated
string type = 1; | ||
string type = 1 [ | ||
(buf.validate.field).string = { | ||
pattern: "^[a-z]+(_[a-z]+)*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is still a small enum, I'd use those values, as it also provides documentation.
proto/minder/v1/minder.proto
Outdated
string def = 1; | ||
string def = 1 [ | ||
(buf.validate.field).string = { | ||
pattern: "^\\.[a-z_]+(\\.[a-z_]+|\\[\\d+]|\\[\"[a-z_]+\"\\])*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we allow uppercase letters here? I know that GitHub prefers lower_with_underscores
, but I think these could also be camelCase
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated it to allow for camelCase 👍
proto/minder/v1/minder.proto
Outdated
string type = 1; | ||
string type = 1 [ | ||
(buf.validate.field).string = { | ||
pattern: "^[a-z]+(_[a-z]+)*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use:
pattern: "^[a-z]+(_[a-z]+)*$", | |
in: ["deny-by-default", "constraints"] |
pattern: "^minder(?:\\.[a-zA-Z0-9_]+)*$", | ||
max_len: 200, | ||
} | ||
]; | ||
// params are unstructured parameters passed to the method. These are optional | ||
// and evaluated by the method. | ||
google.protobuf.Struct params = 6; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is tricky, because it's arbitrary content, but in a structured format that may be interpreted by the rest of our rules.
I don't think we can do much here now, but we'll get a fun bug at some point later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes 👍 I wonder if we should cover this by having custom validation functions or by updating our API to expect a better defined field for this
proto/minder/v1/minder.proto
Outdated
string type = 1; | ||
string type = 1 [ | ||
(buf.validate.field).string = { | ||
pattern: "^[a-z]+(_[a-z]+)*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe:
pattern: "^[a-z]+(_[a-z]+)*$", | |
in: ["security_advisory"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied 👍
proto/minder/v1/minder.proto
Outdated
string severity = 1; | ||
string severity = 1 [ | ||
(buf.validate.field).string = { | ||
pattern: "^[a-z]+(_[a-z]+)*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a Severity
enum as a string:
pattern: "^[a-z]+(_[a-z]+)*$", | |
in: ["unknown", "info", "low", "medium", "high", "critical"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied 👍
proto/minder/v1/minder.proto
Outdated
string description = 5; | ||
string description = 5 [ | ||
(buf.validate.field).string = { | ||
pattern: "[A-Za-z][-/.!?,:;[:word:] *#\\[\\]()`{}]*", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, this is markdown, so we can do this for now, but we may need to parse in Go code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the pattern validation 👍
proto/minder/v1/minder.proto
Outdated
string guidance = 6; | ||
string guidance = 6 [ | ||
(buf.validate.field).string = { | ||
pattern: "[A-Za-z][-/.!?,:;[:word:] *#\\[\\]()`{}]*", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I think this is markdown. We should add to the comment in all the markdown cases, saying:
This value should be rendered as markdown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the pattern validation 👍
a25d1d5
to
ab5a7f1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few more comments, but I'm not sure they are blocking, you could probably revisit them in a future PR.
proto/minder/v1/minder.proto
Outdated
max_len: 1000, | ||
}, | ||
(buf.validate.field).ignore = IGNORE_IF_DEFAULT_VALUE | ||
]; | ||
} | ||
|
||
// endpoint is the endpoint to fetch data from. | ||
// This can be a URL or the path on the API.bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect it's a typo. Want to fix it?
proto/minder/v1/minder.proto
Outdated
string body = 2; | ||
string body = 2 [ | ||
(buf.validate.field).string = { | ||
pattern: "^\\s*\\{[\\s\\S]*\\}\\s*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this trying to say "valid JSON"? This pattern doesn't seem meaningful.
proto/minder/v1/minder.proto
Outdated
(buf.validate.field).repeated = { | ||
items: { | ||
string: { | ||
pattern: "^[a-zA-Z0-9-]+: [^\n\r]+$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to let this pass for now, but we should prefer a positive expression. From https://www.rfc-editor.org/rfc/rfc7230#section-3.2 :
pattern: "^[a-zA-Z0-9-]+: [^\n\r]+$", | |
pattern: "^[a-zA-Z0-9-]+:[[:graph:][:blank:]]+$", |
proto/minder/v1/minder.proto
Outdated
optional string body = 4; | ||
optional string body = 4 [ | ||
(buf.validate.field).string = { | ||
pattern: "^\\s*\\{[\\s\\S]*\\}\\s*$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I think this is "is JSON", which I'd just leave as a comment here.
string depfile = 2; | ||
string depfile = 2 [ | ||
(buf.validate.field).string = { | ||
pattern: "^(\\./)?([a-zA-Z0-9_\\-]+/)*[a-zA-Z0-9_\\-]+(\\.[a-zA-Z0-9]+)?$", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to allow .
in the filename for the depfile path or multiple dots in the requirements file? (The python "standard" for development dependencies is requirements-dep.txt
, but given how loose that standard is, I could see requirements.dep.txt
slipping in.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I wasn't sure about this too, but I'm okay to leave it for now because of the reasons you mentioned 👍
} | ||
|
||
// the title of the PR | ||
string title = 1; | ||
string title = 1 [ | ||
(buf.validate.field).string = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No pattern here? That could be okay, given that GitHub will validate this, but you should put a comment as to why no pattern.
string description = 5; | ||
string description = 5 [ | ||
(buf.validate.field).string = { | ||
min_len: 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, add a comment that this is markdown?
|
||
// guidance are instructions we give the user in case a rule fails. | ||
string guidance = 6; | ||
string guidance = 6 [ | ||
(buf.validate.field).string = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto on the markdown comment.
Signed-off-by: Radoslav Dimitrov <[email protected]>
Signed-off-by: Radoslav Dimitrov <[email protected]>
Signed-off-by: Radoslav Dimitrov <[email protected]>
Signed-off-by: Radoslav Dimitrov <[email protected]>
Signed-off-by: Radoslav Dimitrov <[email protected]>
Signed-off-by: Radoslav Dimitrov <[email protected]>
Signed-off-by: Radoslav Dimitrov <[email protected]>
Signed-off-by: Radoslav Dimitrov <[email protected]>
fc8e7b5
to
9d2b131
Compare
Summary
Ref https://github.com/stacklok/minder-stories/issues/94
Change Type
Mark the type of change your PR introduces:
Testing
Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.
Review Checklist: