Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions apis/v1alpha2/validation/grpcroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ var (
repeatableGRPCRouteFilters = []gatewayv1a2.GRPCRouteFilterType{
gatewayv1a2.GRPCRouteFilterExtensionRef,
}
validServiceName = `^(?i)\.?[a-z_][a-z_0-9]*(\.[a-z_][a-z_0-9]*)*$`
validMethodName = `^[A-Za-z_][A-Za-z_0-9]*$`
validServiceName = `^(?i)\.?[a-z_][a-z_0-9]*(\.[a-z_][a-z_0-9]*)*$`
validServiceNameRegex = regexp.MustCompile(validServiceName)
validMethodName = `^[A-Za-z_][A-Za-z_0-9]*$`
validMethodNameRegex = regexp.MustCompile(validMethodName)
)

// ValidateGRPCRoute validates GRPCRoute according to the Gateway API specification.
Expand Down Expand Up @@ -76,27 +78,15 @@ func validateRuleMatches(matches []gatewayv1a2.GRPCRouteMatch, path *field.Path)
errs = append(errs, field.Required(path.Index(i).Child("method"), "one or both of `service` or `method` must be specified"))
}
// GRPCRoute method matcher admits two types: Exact and RegularExpression.
// If not specified, the match will be treated as type Exact
// If not specified, the match will be treated as type Exact (also the default value for this field).
if m.Method.Type == nil || *m.Method.Type == gatewayv1a2.GRPCMethodMatchExact {
if m.Method.Service != nil {
r, err := regexp.Compile(validServiceName)
if err != nil {
errs = append(errs, field.InternalError(path.Index(i).Child("method"),
fmt.Errorf("could not compile service name matching regex: %w", err)))
} else if !r.MatchString(*m.Method.Service) {
errs = append(errs, field.Invalid(path.Index(i).Child("method"), *m.Method.Service,
fmt.Sprintf("must only contain valid characters (matching %s)", validServiceName)))
}
if m.Method.Service != nil && !validServiceNameRegex.MatchString(*m.Method.Service) {
errs = append(errs, field.Invalid(path.Index(i).Child("method"), *m.Method.Service,
fmt.Sprintf("must only contain valid characters (matching %s)", validServiceName)))
}
if m.Method.Method != nil {
r, err := regexp.Compile(validMethodName)
if err != nil {
errs = append(errs, field.InternalError(path.Index(i).Child("method"),
fmt.Errorf("could not compile method name matching regex: %w", err)))
} else if !r.MatchString(*m.Method.Method) {
errs = append(errs, field.Invalid(path.Index(i).Child("method"), *m.Method.Method,
fmt.Sprintf("must only contain valid characters (matching %s)", validMethodName)))
}
if m.Method.Method != nil && !validMethodNameRegex.MatchString(*m.Method.Method) {
errs = append(errs, field.Invalid(path.Index(i).Child("method"), *m.Method.Method,
fmt.Sprintf("must only contain valid characters (matching %s)", validMethodName)))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion apis/v1beta1/validation/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func ValidateTLSCertificateRefs(listeners []gatewayv1b1.Listener, path *field.Pa
// must be unique within the Gateway
func ValidateListenerNames(listeners []gatewayv1b1.Listener, path *field.Path) field.ErrorList {
var errs field.ErrorList
nameMap := map[gatewayv1b1.SectionName]struct{}{}
nameMap := make(map[gatewayv1b1.SectionName]struct{}, len(listeners))
for i, c := range listeners {
if _, found := nameMap[c.Name]; found {
errs = append(errs, field.Duplicate(path.Index(i).Child("name"), fmt.Sprintln("must be unique within the Gateway")))
Expand Down