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 (
|
2017-07-16 19:19:59 +00:00
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2016-05-25 21:04:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-03-03 01:44:45 +00:00
|
|
|
rewriteTo = "ingress.kubernetes.io/rewrite-target"
|
|
|
|
addBaseURL = "ingress.kubernetes.io/add-base-url"
|
2017-08-21 06:10:35 +00:00
|
|
|
baseURLScheme = "ingress.kubernetes.io/base-url-scheme"
|
2017-03-03 01:44:45 +00:00
|
|
|
sslRedirect = "ingress.kubernetes.io/ssl-redirect"
|
|
|
|
forceSSLRedirect = "ingress.kubernetes.io/force-ssl-redirect"
|
2017-03-12 22:06:10 +00:00
|
|
|
appRoot = "ingress.kubernetes.io/app-root"
|
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-05-27 14:58:13 +00:00
|
|
|
// AddBaseURL indicates if is required to add a base tag in the head
|
|
|
|
// of the responses from the upstream servers
|
2016-11-16 18:24:26 +00:00
|
|
|
AddBaseURL bool `json:"addBaseUrl"`
|
2017-08-21 06:10:35 +00:00
|
|
|
// BaseURLScheme override for the scheme passed to the base tag
|
|
|
|
BaseURLScheme string `json:"baseUrlScheme"`
|
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"`
|
|
|
|
// AppRoot defines the Application Root that the Controller must redirect if it's not in '/' context
|
|
|
|
AppRoot string `json:"appRoot"`
|
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.AddBaseURL != r2.AddBaseURL {
|
|
|
|
return false
|
|
|
|
}
|
2017-08-21 06:10:35 +00:00
|
|
|
if r1.BaseURLScheme != r2.BaseURLScheme {
|
|
|
|
return false
|
|
|
|
}
|
2017-06-14 21:33:12 +00:00
|
|
|
if r1.SSLRedirect != r2.SSLRedirect {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r1.ForceSSLRedirect != r2.ForceSSLRedirect {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r1.AppRoot != r2.AppRoot {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-29 20:02:06 +00:00
|
|
|
type rewrite struct {
|
|
|
|
backendResolver resolver.DefaultBackend
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser creates a new reqrite annotation parser
|
|
|
|
func NewParser(br resolver.DefaultBackend) parser.IngressAnnotation {
|
|
|
|
return rewrite{br}
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:04:34 +00:00
|
|
|
// ParseAnnotations parses the annotations contained in the ingress
|
|
|
|
// rule used to rewrite the defined paths
|
2016-12-29 20:02:06 +00:00
|
|
|
func (a rewrite) Parse(ing *extensions.Ingress) (interface{}, error) {
|
2017-02-11 21:34:50 +00:00
|
|
|
rt, _ := parser.GetStringAnnotation(rewriteTo, ing)
|
2016-11-10 22:56:29 +00:00
|
|
|
sslRe, err := parser.GetBoolAnnotation(sslRedirect, ing)
|
2016-06-05 14:19:55 +00:00
|
|
|
if err != nil {
|
2016-12-29 20:02:06 +00:00
|
|
|
sslRe = a.backendResolver.GetDefaultBackend().SSLRedirect
|
2016-06-05 14:19:55 +00:00
|
|
|
}
|
2017-03-03 01:44:45 +00:00
|
|
|
fSslRe, err := parser.GetBoolAnnotation(forceSSLRedirect, ing)
|
|
|
|
if err != nil {
|
|
|
|
fSslRe = a.backendResolver.GetDefaultBackend().ForceSSLRedirect
|
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
abu, _ := parser.GetBoolAnnotation(addBaseURL, ing)
|
2017-08-21 06:10:35 +00:00
|
|
|
bus, _ := parser.GetStringAnnotation(baseURLScheme, ing)
|
2017-03-12 22:06:10 +00:00
|
|
|
ar, _ := parser.GetStringAnnotation(appRoot, ing)
|
2017-11-07 16:36:51 +00:00
|
|
|
return &Config{
|
2017-03-03 01:44:45 +00:00
|
|
|
Target: rt,
|
|
|
|
AddBaseURL: abu,
|
2017-08-21 06:10:35 +00:00
|
|
|
BaseURLScheme: bus,
|
2017-03-03 01:44:45 +00:00
|
|
|
SSLRedirect: sslRe,
|
|
|
|
ForceSSLRedirect: fSslRe,
|
2017-03-12 22:06:10 +00:00
|
|
|
AppRoot: ar,
|
2016-05-25 21:04:34 +00:00
|
|
|
}, nil
|
|
|
|
}
|