2016-05-25 21:04:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
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 (
|
2016-05-27 14:58:13 +00:00
|
|
|
"errors"
|
2016-05-25 21:04:34 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2016-06-05 14:19:55 +00:00
|
|
|
|
|
|
|
"k8s.io/contrib/ingress/controllers/nginx/nginx/config"
|
2016-05-25 21:04:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-06-05 14:19:55 +00:00
|
|
|
rewriteTo = "ingress.kubernetes.io/rewrite-target"
|
|
|
|
addBaseURL = "ingress.kubernetes.io/add-base-url"
|
|
|
|
sslRedirect = "ingress.kubernetes.io/ssl-redirect"
|
2016-05-25 21:04:34 +00:00
|
|
|
)
|
|
|
|
|
2016-06-05 14:19:55 +00:00
|
|
|
// Redirect describes the per location redirect config
|
2016-05-25 21:04:34 +00:00
|
|
|
type Redirect struct {
|
2016-05-27 14:58:13 +00:00
|
|
|
// Target URI where the traffic must be redirected
|
|
|
|
Target string
|
|
|
|
// AddBaseURL indicates if is required to add a base tag in the head
|
|
|
|
// of the responses from the upstream servers
|
|
|
|
AddBaseURL bool
|
2016-06-05 14:19:55 +00:00
|
|
|
// Should indicates if the location section should be accessible SSL only
|
|
|
|
SSLRedirect bool
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 14:19:55 +00:00
|
|
|
var (
|
|
|
|
// ErrMissingSSLRedirect returned error when the ingress does not contains the
|
|
|
|
// ssl-redirect annotation
|
|
|
|
ErrMissingSSLRedirect = errors.New("ssl-redirect annotations is missing")
|
|
|
|
|
|
|
|
// ErrInvalidBool gets returned when the str value is not convertible to a bool
|
|
|
|
ErrInvalidBool = errors.New("ssl-redirect annotations has invalid value")
|
|
|
|
)
|
|
|
|
|
2016-05-25 21:04:34 +00:00
|
|
|
type ingAnnotations map[string]string
|
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
func (a ingAnnotations) addBaseURL() bool {
|
|
|
|
val, ok := a[addBaseURL]
|
2016-05-25 21:04:34 +00:00
|
|
|
if ok {
|
|
|
|
if b, err := strconv.ParseBool(val); err == nil {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
func (a ingAnnotations) rewriteTo() string {
|
|
|
|
val, ok := a[rewriteTo]
|
|
|
|
if ok {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-06-05 14:19:55 +00:00
|
|
|
func (a ingAnnotations) sslRedirect() (bool, error) {
|
|
|
|
val, ok := a[sslRedirect]
|
|
|
|
if !ok {
|
|
|
|
return false, ErrMissingSSLRedirect
|
|
|
|
}
|
|
|
|
|
|
|
|
sr, err := strconv.ParseBool(val)
|
|
|
|
if err != nil {
|
|
|
|
return false, ErrInvalidBool
|
|
|
|
}
|
|
|
|
|
|
|
|
return sr, nil
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:04:34 +00:00
|
|
|
// ParseAnnotations parses the annotations contained in the ingress
|
|
|
|
// rule used to rewrite the defined paths
|
2016-06-05 14:19:55 +00:00
|
|
|
func ParseAnnotations(cfg config.Configuration, ing *extensions.Ingress) (*Redirect, error) {
|
2016-05-25 21:04:34 +00:00
|
|
|
if ing.GetAnnotations() == nil {
|
2016-05-27 14:58:13 +00:00
|
|
|
return &Redirect{}, errors.New("no annotations present")
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 14:19:55 +00:00
|
|
|
annotations := ingAnnotations(ing.GetAnnotations())
|
|
|
|
|
|
|
|
sslRe, err := annotations.sslRedirect()
|
|
|
|
if err != nil {
|
|
|
|
sslRe = cfg.SSLRedirect
|
|
|
|
}
|
|
|
|
|
|
|
|
rt := annotations.rewriteTo()
|
|
|
|
abu := annotations.addBaseURL()
|
2016-05-25 21:04:34 +00:00
|
|
|
return &Redirect{
|
2016-06-05 14:19:55 +00:00
|
|
|
Target: rt,
|
|
|
|
AddBaseURL: abu,
|
|
|
|
SSLRedirect: sslRe,
|
2016-05-25 21:04:34 +00:00
|
|
|
}, nil
|
|
|
|
}
|