-
Notifications
You must be signed in to change notification settings - Fork 118
Implement NKG-specific field validation for HTTPRoutes #455
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
30 commits
Select commit
Hold shift + click to select a range
2f01d7a
Add NKG-specific field validation for HTTPRoutes
pleshakov cbd0e2a
Apply suggestions on GitHub
pleshakov b64c795
fmt.Errorf(string) -> errors.New(string)
pleshakov 3e8004b
add a comment about the purpose of examples
pleshakov 8c5283a
add type constraint
pleshakov 0ba4d14
Place examples into variables
pleshakov 63d5dfc
Make NJS stand out more
pleshakov 8b2dace
test all valid http methods
pleshakov 79c2c81
Implement TODO condition for partial validity
pleshakov d5f3e72
Make createBackend() return pointer instead of a slice
pleshakov 9ef4bf4
Add comment for BackendGroup field of Rule
pleshakov c5e3460
Make Route invalid if all Rules are invalid
pleshakov 1695f43
Improve name of buildRoutes
pleshakov 5ef2ce2
keep the happy path aligned left in bindRouteToListeners
pleshakov 2968f74
handle case 2 first in bindRouteToListeners
pleshakov f0ee31e
No need for multilines params in bindRoute(s)ToListener
pleshakov 636c0e5
improve validateMethodMatch
pleshakov cacdeb2
Remove unnecessary route in tests
pleshakov cf2e566
unbound -> unattached
pleshakov 976ab42
Add unattached section case to TestGetAllConditionsForSectionName
pleshakov 57bd5a6
Add a case for explicit ground and kind in TestFindGatewayForParentRef
pleshakov 0fb3f9d
Move helper test functions to the top
pleshakov bdbff1d
Nil -> Missing
pleshakov 547c482
Test multiple valid hostnames
pleshakov 12f1e40
change the weight of the normal ref to something besides the default …
pleshakov c9f5834
Add a link to a fixme about removing warnings
pleshakov 3b5a532
Add a FIXME about refactoring BackendGroups
pleshakov 2f6f83d
Fix linting
pleshakov c59d568
Add not found case and remove no-longer needed section name
pleshakov e08c130
Merge branch 'main' into feature/httproute-validation
pleshakov 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
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
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,48 @@ | ||
package validation | ||
kate-osborn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
|
||
k8svalidation "k8s.io/apimachinery/pkg/util/validation" | ||
) | ||
|
||
const ( | ||
escapedStringsFmt = `([^"\\]|\\.)*` | ||
escapedStringsErrMsg = `must have all '"' (double quotes) escaped and must not end with an unescaped '\' ` + | ||
`(backslash)` | ||
) | ||
|
||
var escapedStringsFmtRegexp = regexp.MustCompile("^" + escapedStringsFmt + "$") | ||
|
||
// validateEscapedString is used to validate a string that is surrounded by " in the NGINX config for a directive | ||
// that doesn't support any regex rules or variables (it doesn't try to expand the variable name behind $). | ||
// For example, server_name "hello $not_a_var world" | ||
// If the value is invalid, the function returns an error that includes the specified examples of valid values. | ||
func validateEscapedString(value string, examples []string) error { | ||
if !escapedStringsFmtRegexp.MatchString(value) { | ||
msg := k8svalidation.RegexError(escapedStringsErrMsg, escapedStringsFmt, examples...) | ||
return errors.New(msg) | ||
} | ||
return nil | ||
} | ||
|
||
const ( | ||
escapedStringsNoVarExpansionFmt = `([^"$\\]|\\[^$])*` | ||
escapedStringsNoVarExpansionErrMsg string = `a valid header must have all '"' escaped and must not contain any ` + | ||
`'$' or end with an unescaped '\'` | ||
) | ||
|
||
var escapedStringsNoVarExpansionFmtRegexp = regexp.MustCompile("^" + escapedStringsNoVarExpansionFmt + "$") | ||
|
||
// validateEscapedStringNoVarExpansion is the same as validateEscapedString except it doesn't allow $ to | ||
// prevent variable expansion. | ||
// If the value is invalid, the function returns an error that includes the specified examples of valid values. | ||
func validateEscapedStringNoVarExpansion(value string, examples []string) error { | ||
if !escapedStringsNoVarExpansionFmtRegexp.MatchString(value) { | ||
msg := k8svalidation.RegexError(escapedStringsNoVarExpansionErrMsg, escapedStringsNoVarExpansionFmt, | ||
examples...) | ||
return errors.New(msg) | ||
} | ||
return nil | ||
} |
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,32 @@ | ||
package validation | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestValidateEscapedString(t *testing.T) { | ||
validator := func(value string) error { return validateEscapedString(value, []string{"example"}) } | ||
|
||
testValidValuesForSimpleValidator(t, validator, | ||
`test`, | ||
`test test`, | ||
`\"`, | ||
`\\`) | ||
testInvalidValuesForSimpleValidator(t, validator, | ||
`\`, | ||
`test"test`) | ||
} | ||
|
||
func TestValidateEscapedStringNoVarExpansion(t *testing.T) { | ||
validator := func(value string) error { return validateEscapedStringNoVarExpansion(value, []string{"example"}) } | ||
|
||
testValidValuesForSimpleValidator(t, validator, | ||
`test`, | ||
`test test`, | ||
`\"`, | ||
`\\`) | ||
testInvalidValuesForSimpleValidator(t, validator, | ||
`\`, | ||
`test"test`, | ||
`$test`) | ||
} |
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,15 @@ | ||
/* | ||
Package validation includes validators to validate values that will propagate to the NGINX configuration. | ||
|
||
The validation rules prevent two cases: | ||
(1) Invalid values. Such values will cause NGINX to fail to reload the configuration. | ||
(2) Malicious values. Such values will cause NGINX to succeed to reload, but will configure NGINX maliciously, outside | ||
of the NKG capabilities. For example, configuring NGINX to serve the contents of the file system of its container. | ||
|
||
The validation rules are based on the types in the parent config package and how they are used in the NGINX | ||
configuration templates. Changes to those might require changing the validation rules. | ||
|
||
The rules are much looser for NGINX than for the Gateway API. However, some valid Gateway API values are not valid for | ||
NGINX. | ||
*/ | ||
package validation |
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.