2016-05-25 21:04:34 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-05-25 21:04:34 +00:00
|
|
|
|
|
|
|
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 rewrite
|
|
|
|
|
|
|
|
import (
|
2020-03-19 18:49:18 +00:00
|
|
|
"net/url"
|
|
|
|
|
2019-06-09 22:49:59 +00:00
|
|
|
networking "k8s.io/api/networking/v1beta1"
|
2020-08-08 23:31:02 +00:00
|
|
|
"k8s.io/klog/v2"
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
2020-03-19 18:49:18 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/errors"
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2016-05-25 21:04:34 +00:00
|
|
|
)
|
|
|
|
|
2017-11-07 16:36:51 +00:00
|
|
|
// Config describes the per location redirect config
|
|
|
|
type Config struct {
|
2016-05-27 14:58:13 +00:00
|
|
|
// Target URI where the traffic must be redirected
|
2016-11-16 18:24:26 +00:00
|
|
|
Target string `json:"target"`
|
2016-11-10 22:56:29 +00:00
|
|
|
// SSLRedirect indicates if the location section is accessible SSL only
|
2016-11-16 18:24:26 +00:00
|
|
|
SSLRedirect bool `json:"sslRedirect"`
|
2017-03-03 01:44:45 +00:00
|
|
|
// ForceSSLRedirect indicates if the location section is accessible SSL only
|
2017-03-13 15:03:47 +00:00
|
|
|
ForceSSLRedirect bool `json:"forceSSLRedirect"`
|
2017-12-13 17:29:41 +00:00
|
|
|
// AppRoot defines the Application Root that the Controller must redirect if it's in '/' context
|
2017-03-13 15:03:47 +00:00
|
|
|
AppRoot string `json:"appRoot"`
|
2018-10-01 17:54:11 +00:00
|
|
|
// UseRegex indicates whether or not the locations use regex paths
|
2018-10-30 16:31:07 +00:00
|
|
|
UseRegex bool `json:"useRegex"`
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 03:07:59 +00:00
|
|
|
// Equal tests for equality between two Redirect types
|
2017-11-07 16:36:51 +00:00
|
|
|
func (r1 *Config) Equal(r2 *Config) bool {
|
2017-06-14 21:33:12 +00:00
|
|
|
if r1 == r2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if r1 == nil || r2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r1.Target != r2.Target {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r1.SSLRedirect != r2.SSLRedirect {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r1.ForceSSLRedirect != r2.ForceSSLRedirect {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r1.AppRoot != r2.AppRoot {
|
|
|
|
return false
|
|
|
|
}
|
2018-10-01 17:54:11 +00:00
|
|
|
if r1.UseRegex != r2.UseRegex {
|
|
|
|
return false
|
|
|
|
}
|
2017-06-14 21:33:12 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-29 20:02:06 +00:00
|
|
|
type rewrite struct {
|
2017-11-08 20:58:57 +00:00
|
|
|
r resolver.Resolver
|
2016-12-29 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:54:51 +00:00
|
|
|
// NewParser creates a new rewrite annotation parser
|
2017-11-08 20:58:57 +00:00
|
|
|
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
|
|
|
|
return rewrite{r}
|
2016-12-29 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 21:04:34 +00:00
|
|
|
// ParseAnnotations parses the annotations contained in the ingress
|
|
|
|
// rule used to rewrite the defined paths
|
2019-06-09 22:49:59 +00:00
|
|
|
func (a rewrite) Parse(ing *networking.Ingress) (interface{}, error) {
|
2018-11-30 22:56:11 +00:00
|
|
|
var err error
|
|
|
|
config := &Config{}
|
|
|
|
|
|
|
|
config.Target, _ = parser.GetStringAnnotation("rewrite-target", ing)
|
|
|
|
config.SSLRedirect, err = parser.GetBoolAnnotation("ssl-redirect", ing)
|
2016-06-05 14:19:55 +00:00
|
|
|
if err != nil {
|
2018-11-30 22:56:11 +00:00
|
|
|
config.SSLRedirect = a.r.GetDefaultBackend().SSLRedirect
|
2016-06-05 14:19:55 +00:00
|
|
|
}
|
2018-11-30 22:56:11 +00:00
|
|
|
|
|
|
|
config.ForceSSLRedirect, err = parser.GetBoolAnnotation("force-ssl-redirect", ing)
|
2017-03-03 01:44:45 +00:00
|
|
|
if err != nil {
|
2018-11-30 22:56:11 +00:00
|
|
|
config.ForceSSLRedirect = a.r.GetDefaultBackend().ForceSSLRedirect
|
2017-03-03 01:44:45 +00:00
|
|
|
}
|
2018-11-30 22:56:11 +00:00
|
|
|
|
|
|
|
config.UseRegex, _ = parser.GetBoolAnnotation("use-regex", ing)
|
|
|
|
|
2020-03-19 18:49:18 +00:00
|
|
|
config.AppRoot, err = parser.GetStringAnnotation("app-root", ing)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.IsMissingAnnotations(err) && !errors.IsInvalidContent(err) {
|
|
|
|
klog.Warningf("Annotation app-root contains an invalid value: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.ParseRequestURI(config.AppRoot)
|
|
|
|
if err != nil {
|
|
|
|
klog.Warningf("Annotation app-root contains an invalid value: %v", err)
|
|
|
|
config.AppRoot = ""
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.IsAbs() {
|
|
|
|
klog.Warningf("Annotation app-root only allows absolute paths (%v)", config.AppRoot)
|
|
|
|
config.AppRoot = ""
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
return config, nil
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|