-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NKG-specific field validation for HTTPRoutes (#455)
* Add NKG-specific field validation for HTTPRoutes - Introduce HTTPFieldsValidator interface for validating fields of HTTP-related Gateway API resources according to the data-plane specific rules. - Validate HTTPRoute resources when building the graph using data-plane agnostic rules. - Validate HTTPRoute resources when building the graph using HTTPFieldsValidator according to the data-plane rules. - Implement an HTTPFieldsValidator for NGINX-specific validation rules. Fixes #412 * Apply suggestions on GitHub Co-authored-by: Kate Osborn <[email protected]>
- Loading branch information
Showing
40 changed files
with
4,760 additions
and
1,214 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 | ||
|
||
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 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 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.