2018-10-25 16:35:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package customhttperrors
|
|
|
|
|
|
|
|
import (
|
2023-07-22 03:32:07 +00:00
|
|
|
"regexp"
|
2018-10-25 16:35:48 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2018-10-25 16:35:48 +00:00
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
|
|
|
)
|
|
|
|
|
2023-07-22 03:32:07 +00:00
|
|
|
const (
|
|
|
|
customHTTPErrorsAnnotation = "custom-http-errors"
|
|
|
|
)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
// We accept anything between 400 and 599, on a comma separated.
|
|
|
|
var arrayOfHTTPErrors = regexp.MustCompile(`^(?:[4,5]\d{2},?)*$`)
|
2023-07-22 03:32:07 +00:00
|
|
|
|
|
|
|
var customHTTPErrorsAnnotations = parser.Annotation{
|
|
|
|
Group: "backend",
|
|
|
|
Annotations: parser.AnnotationFields{
|
|
|
|
customHTTPErrorsAnnotation: {
|
2023-08-31 07:36:48 +00:00
|
|
|
Validator: parser.ValidateRegex(arrayOfHTTPErrors, true),
|
2023-07-22 03:32:07 +00:00
|
|
|
Scope: parser.AnnotationScopeLocation,
|
|
|
|
Risk: parser.AnnotationRiskLow,
|
|
|
|
Documentation: `If a default backend annotation is specified on the ingress, the errors code specified on this annotation
|
|
|
|
will be routed to that annotation's default backend service. Otherwise they will be routed to the global default backend.
|
|
|
|
A comma-separated list of error codes is accepted (anything between 400 and 599, like 403, 503)`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:35:48 +00:00
|
|
|
type customhttperrors struct {
|
2023-07-22 03:32:07 +00:00
|
|
|
r resolver.Resolver
|
|
|
|
annotationConfig parser.Annotation
|
2018-10-25 16:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser creates a new custom http errors annotation parser
|
|
|
|
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
|
2023-07-22 03:32:07 +00:00
|
|
|
return customhttperrors{
|
|
|
|
r: r,
|
|
|
|
annotationConfig: customHTTPErrorsAnnotations,
|
|
|
|
}
|
2018-10-25 16:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses the annotations contained in the ingress to use
|
|
|
|
// custom http errors
|
2019-06-09 22:49:59 +00:00
|
|
|
func (e customhttperrors) Parse(ing *networking.Ingress) (interface{}, error) {
|
2023-07-22 03:32:07 +00:00
|
|
|
c, err := parser.GetStringAnnotation(customHTTPErrorsAnnotation, ing, e.annotationConfig.Annotations)
|
2018-10-25 16:35:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cSplit := strings.Split(c, ",")
|
2023-08-31 07:36:48 +00:00
|
|
|
codes := make([]int, 0, len(cSplit))
|
2018-10-25 16:35:48 +00:00
|
|
|
for _, i := range cSplit {
|
|
|
|
num, err := strconv.Atoi(i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
codes = append(codes, num)
|
|
|
|
}
|
|
|
|
|
|
|
|
return codes, nil
|
|
|
|
}
|
2023-07-22 03:32:07 +00:00
|
|
|
|
|
|
|
func (e customhttperrors) GetDocumentation() parser.AnnotationFields {
|
|
|
|
return e.annotationConfig.Annotations
|
|
|
|
}
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
func (e customhttperrors) Validate(anns map[string]string) error {
|
|
|
|
maxrisk := parser.StringRiskToRisk(e.r.GetSecurityConfiguration().AnnotationsRiskLevel)
|
2023-07-22 03:32:07 +00:00
|
|
|
return parser.CheckAnnotationRisk(anns, maxrisk, customHTTPErrorsAnnotations.Annotations)
|
|
|
|
}
|