2019-07-30 19:43:13 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 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 mirror
|
|
|
|
|
|
|
|
import (
|
2020-02-05 02:06:07 +00:00
|
|
|
"fmt"
|
2023-07-22 03:32:07 +00:00
|
|
|
"regexp"
|
2022-05-08 00:39:17 +00:00
|
|
|
"strings"
|
2020-02-05 02:06:07 +00:00
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2019-07-30 19:43:13 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
2023-07-22 03:32:07 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/errors"
|
2019-07-30 19:43:13 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2023-07-22 03:32:07 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-07-30 19:43:13 +00:00
|
|
|
)
|
|
|
|
|
2023-07-22 03:32:07 +00:00
|
|
|
const (
|
|
|
|
mirrorRequestBodyAnnotation = "mirror-request-body"
|
|
|
|
mirrorTargetAnnotation = "mirror-target"
|
|
|
|
mirrorHostAnnotation = "mirror-host"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
OnOffRegex = regexp.MustCompile(`^(on|off)$`)
|
|
|
|
)
|
|
|
|
|
|
|
|
var mirrorAnnotation = parser.Annotation{
|
|
|
|
Group: "mirror",
|
|
|
|
Annotations: parser.AnnotationFields{
|
|
|
|
mirrorRequestBodyAnnotation: {
|
|
|
|
Validator: parser.ValidateRegex(*OnOffRegex, true),
|
|
|
|
Scope: parser.AnnotationScopeIngress,
|
|
|
|
Risk: parser.AnnotationRiskLow,
|
|
|
|
Documentation: `This annotation defines if the request-body should be sent to the mirror backend. Can be 'on' or 'off'`,
|
|
|
|
},
|
|
|
|
mirrorTargetAnnotation: {
|
|
|
|
Validator: parser.ValidateServerName,
|
|
|
|
Scope: parser.AnnotationScopeIngress,
|
|
|
|
Risk: parser.AnnotationRiskHigh,
|
|
|
|
Documentation: `This annotation enables a request to be mirrored to a mirror backend.`,
|
|
|
|
},
|
|
|
|
mirrorHostAnnotation: {
|
|
|
|
Validator: parser.ValidateServerName,
|
|
|
|
Scope: parser.AnnotationScopeIngress,
|
|
|
|
Risk: parser.AnnotationRiskHigh,
|
|
|
|
Documentation: `This annotation defines if a specific Host header should be set for mirrored request.`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-07-30 19:43:13 +00:00
|
|
|
// Config returns the mirror to use in a given location
|
|
|
|
type Config struct {
|
2020-02-05 02:06:07 +00:00
|
|
|
Source string `json:"source"`
|
2019-07-30 19:43:13 +00:00
|
|
|
RequestBody string `json:"requestBody"`
|
2020-02-05 02:06:07 +00:00
|
|
|
Target string `json:"target"`
|
2022-05-08 00:39:17 +00:00
|
|
|
Host string `json:"host"`
|
2020-02-05 02:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equal tests for equality between two Configuration types
|
|
|
|
func (m1 *Config) Equal(m2 *Config) bool {
|
|
|
|
if m1 == m2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if m1 == nil || m2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if m1.Source != m2.Source {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if m1.RequestBody != m2.RequestBody {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if m1.Target != m2.Target {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-08 00:39:17 +00:00
|
|
|
if m1.Host != m2.Host {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:06:07 +00:00
|
|
|
return true
|
2019-07-30 19:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type mirror struct {
|
2023-07-22 03:32:07 +00:00
|
|
|
r resolver.Resolver
|
|
|
|
annotationConfig parser.Annotation
|
2019-07-30 19:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser creates a new mirror configuration annotation parser
|
|
|
|
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
|
2023-07-22 03:32:07 +00:00
|
|
|
return mirror{
|
|
|
|
r: r,
|
|
|
|
annotationConfig: mirrorAnnotation,
|
|
|
|
}
|
2019-07-30 19:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAnnotations parses the annotations contained in the ingress
|
|
|
|
// rule used to configure mirror
|
|
|
|
func (a mirror) Parse(ing *networking.Ingress) (interface{}, error) {
|
2020-02-05 02:06:07 +00:00
|
|
|
config := &Config{
|
|
|
|
Source: fmt.Sprintf("/_mirror-%v", ing.UID),
|
2019-07-30 19:43:13 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 02:06:07 +00:00
|
|
|
var err error
|
2023-07-22 03:32:07 +00:00
|
|
|
config.RequestBody, err = parser.GetStringAnnotation(mirrorRequestBodyAnnotation, ing, a.annotationConfig.Annotations)
|
2019-07-30 19:43:13 +00:00
|
|
|
if err != nil || config.RequestBody != "off" {
|
2023-07-22 03:32:07 +00:00
|
|
|
if errors.IsValidationError(err) {
|
|
|
|
klog.Warningf("annotation %s contains invalid value", mirrorRequestBodyAnnotation)
|
|
|
|
}
|
2019-07-30 19:43:13 +00:00
|
|
|
config.RequestBody = "on"
|
|
|
|
}
|
|
|
|
|
2023-07-22 03:32:07 +00:00
|
|
|
config.Target, err = parser.GetStringAnnotation(mirrorTargetAnnotation, ing, a.annotationConfig.Annotations)
|
2020-02-05 02:06:07 +00:00
|
|
|
if err != nil {
|
2023-07-22 03:32:07 +00:00
|
|
|
if errors.IsValidationError(err) {
|
|
|
|
klog.Warningf("annotation %s contains invalid value, defaulting", mirrorTargetAnnotation)
|
|
|
|
} else {
|
|
|
|
config.Target = ""
|
|
|
|
config.Source = ""
|
|
|
|
}
|
2020-02-05 02:06:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 03:32:07 +00:00
|
|
|
config.Host, err = parser.GetStringAnnotation(mirrorHostAnnotation, ing, a.annotationConfig.Annotations)
|
2022-05-08 00:39:17 +00:00
|
|
|
if err != nil {
|
2023-07-22 03:32:07 +00:00
|
|
|
if errors.IsValidationError(err) {
|
|
|
|
klog.Warningf("annotation %s contains invalid value, defaulting", mirrorHostAnnotation)
|
|
|
|
}
|
2022-05-08 00:39:17 +00:00
|
|
|
if config.Target != "" {
|
2023-06-11 18:59:47 +00:00
|
|
|
target := strings.Split(config.Target, "$")
|
|
|
|
|
|
|
|
url, err := parser.StringToURL(target[0])
|
2022-05-08 00:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
config.Host = ""
|
|
|
|
} else {
|
2023-06-11 18:59:47 +00:00
|
|
|
config.Host = url.Hostname()
|
2022-05-08 00:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 19:43:13 +00:00
|
|
|
return config, nil
|
|
|
|
}
|
2023-07-22 03:32:07 +00:00
|
|
|
|
|
|
|
func (a mirror) GetDocumentation() parser.AnnotationFields {
|
|
|
|
return a.annotationConfig.Annotations
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a mirror) Validate(anns map[string]string) error {
|
|
|
|
maxrisk := parser.StringRiskToRisk(a.r.GetSecurityConfiguration().AnnotationsRiskLevel)
|
|
|
|
return parser.CheckAnnotationRisk(anns, maxrisk, mirrorAnnotation.Annotations)
|
|
|
|
}
|