-
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.
- Loading branch information
Showing
8 changed files
with
417 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package validation | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
|
||
k8svalidation "k8s.io/apimachinery/pkg/util/validation" | ||
) | ||
|
||
// GenericValidator validates values for generic cases in the nginx conf. | ||
type GenericValidator struct{} | ||
|
||
// ValidateEscapedStringNoVarExpansion ensures that no invalid characters are included in the string value that | ||
// could lead to unwanted nginx behavior. | ||
func (GenericValidator) ValidateEscapedStringNoVarExpansion(value string) error { | ||
return validateEscapedStringNoVarExpansion(value, nil) | ||
} | ||
|
||
const ( | ||
alphaNumericStringFmt = `[a-zA-Z0-9_-]+` | ||
alphaNumericStringErrMsg = "must contain only alphanumeric characters or '-' or '_'" | ||
) | ||
|
||
var alphaNumericStringFmtRegexp = regexp.MustCompile("^" + alphaNumericStringFmt + "$") | ||
|
||
// ValidateServiceName validates a k8s service name that can only use alphanumeric characters. | ||
func (GenericValidator) ValidateServiceName(name string) error { | ||
if !alphaNumericStringFmtRegexp.MatchString(name) { | ||
examples := []string{ | ||
"svc1", | ||
"svc-1", | ||
"svc_1", | ||
} | ||
|
||
return errors.New(k8svalidation.RegexError(alphaNumericStringErrMsg, alphaNumericStringFmt, examples...)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
const ( | ||
durationStringFmt = `\d{1,4}(ms|s)?` | ||
durationStringErrMsg = "must contain a number followed by 'ms' or 's'" | ||
) | ||
|
||
var durationStringFmtRegexp = regexp.MustCompile("^" + durationStringFmt + "$") | ||
|
||
// ValidateNginxDuration validates a duration string that nginx can understand. | ||
func (GenericValidator) ValidateNginxDuration(duration string) error { | ||
if !durationStringFmtRegexp.MatchString(duration) { | ||
examples := []string{ | ||
"5ms", | ||
"10s", | ||
} | ||
|
||
return errors.New(k8svalidation.RegexError(durationStringFmt, durationStringErrMsg, examples...)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
const ( | ||
//nolint:lll | ||
endpointStringFmt = `(?:http?:\/\/)?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*(?::\d{1,5})?` | ||
endpointStringErrMsg = "must be an alphanumeric hostname with optional http scheme and optional port" | ||
) | ||
|
||
var endpointStringFmtRegexp = regexp.MustCompile("^" + endpointStringFmt + "$") | ||
|
||
// ValidateEndpoint validates an alphanumeric endpoint, with optional http scheme and port. | ||
func (GenericValidator) ValidateEndpoint(endpoint string) error { | ||
if !endpointStringFmtRegexp.MatchString(endpoint) { | ||
examples := []string{ | ||
"my-endpoint", | ||
"my.endpoint:5678", | ||
"http://my-endpoint", | ||
} | ||
|
||
return errors.New(k8svalidation.RegexError(endpointStringFmt, endpointStringErrMsg, examples...)) | ||
} | ||
|
||
return nil | ||
} |
86 changes: 86 additions & 0 deletions
86
internal/mode/static/nginx/config/validation/generic_test.go
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,86 @@ | ||
package validation | ||
|
||
import "testing" | ||
|
||
func TestGenericValidator_ValidateEscapedStringNoVarExpansion(t *testing.T) { | ||
validator := GenericValidator{} | ||
|
||
testValidValuesForSimpleValidator( | ||
t, | ||
validator.ValidateEscapedStringNoVarExpansion, | ||
`test`, | ||
`test test`, | ||
`\"`, | ||
`\\`, | ||
) | ||
|
||
testInvalidValuesForSimpleValidator( | ||
t, | ||
validator.ValidateEscapedStringNoVarExpansion, | ||
`\`, | ||
`test"test`, | ||
`$test`, | ||
) | ||
} | ||
|
||
func TestValidateServiceName(t *testing.T) { | ||
validator := GenericValidator{} | ||
|
||
testValidValuesForSimpleValidator( | ||
t, | ||
validator.ValidateServiceName, | ||
`test`, | ||
`Test-test`, | ||
`test_Test`, | ||
`test123`, | ||
) | ||
|
||
testInvalidValuesForSimpleValidator( | ||
t, | ||
validator.ValidateServiceName, | ||
`test#$%`, | ||
`test test`, | ||
`test.test`, | ||
) | ||
} | ||
|
||
func TestValidateNginxDuration(t *testing.T) { | ||
validator := GenericValidator{} | ||
|
||
testValidValuesForSimpleValidator( | ||
t, | ||
validator.ValidateNginxDuration, | ||
`5ms`, | ||
`10s`, | ||
`123ms`, | ||
) | ||
|
||
testInvalidValuesForSimpleValidator( | ||
t, | ||
validator.ValidateNginxDuration, | ||
`test`, | ||
`12345`, | ||
`5m`, | ||
) | ||
} | ||
|
||
func TestValidateEndpoint(t *testing.T) { | ||
validator := GenericValidator{} | ||
|
||
testValidValuesForSimpleValidator( | ||
t, | ||
validator.ValidateEndpoint, | ||
`http://my-endpoint:5678`, | ||
`my.endpoint`, | ||
`myendpoint:123`, | ||
`my-endpoint123:456`, | ||
) | ||
|
||
testInvalidValuesForSimpleValidator( | ||
t, | ||
validator.ValidateEndpoint, | ||
`https://my-endpoint`, | ||
`my_endpoint`, | ||
`my$endpoint`, | ||
) | ||
} |
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
Oops, something went wrong.