2016-02-22 00:13:08 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2016-02-22 00:13:08 +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.
|
|
|
|
*/
|
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
package template
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-15 15:31:39 +00:00
|
|
|
"bytes"
|
2016-08-19 14:51:40 +00:00
|
|
|
"encoding/base64"
|
2016-02-22 00:13:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-12-22 03:00:27 +00:00
|
|
|
"net"
|
2017-05-07 04:28:21 +00:00
|
|
|
"os"
|
2016-11-10 22:56:29 +00:00
|
|
|
"os/exec"
|
2017-08-23 04:57:35 +00:00
|
|
|
"strconv"
|
2016-05-25 21:04:34 +00:00
|
|
|
"strings"
|
2016-08-07 22:53:08 +00:00
|
|
|
text_template "text/template"
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2017-09-17 18:42:31 +00:00
|
|
|
|
2017-06-02 03:30:22 +00:00
|
|
|
"github.com/pborman/uuid"
|
2017-08-19 21:13:02 +00:00
|
|
|
|
2017-08-25 23:49:44 +00:00
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2017-08-19 21:13:02 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2016-11-16 18:24:26 +00:00
|
|
|
"k8s.io/ingress/controllers/nginx/pkg/config"
|
2016-11-10 22:56:29 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress"
|
2017-08-23 00:47:29 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress/annotations/ratelimit"
|
2016-12-22 03:00:27 +00:00
|
|
|
ing_net "k8s.io/ingress/core/pkg/net"
|
2016-11-10 22:56:29 +00:00
|
|
|
"k8s.io/ingress/core/pkg/watch"
|
2016-02-22 00:13:08 +00:00
|
|
|
)
|
|
|
|
|
2016-05-25 21:04:34 +00:00
|
|
|
const (
|
2016-11-10 22:56:29 +00:00
|
|
|
slash = "/"
|
|
|
|
defBufferSize = 65535
|
2016-03-22 18:01:04 +00:00
|
|
|
)
|
2016-02-22 00:13:08 +00:00
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
// Template ...
|
|
|
|
type Template struct {
|
2017-09-18 23:53:26 +00:00
|
|
|
tmpl *text_template.Template
|
|
|
|
fw watch.FileWatcher
|
|
|
|
s int
|
2016-08-07 22:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//NewTemplate returns a new Template instance or an
|
|
|
|
//error if the specified template file contains errors
|
|
|
|
func NewTemplate(file string, onChange func()) (*Template, error) {
|
|
|
|
tmpl, err := text_template.New("nginx.tmpl").Funcs(funcMap).ParseFiles(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
fw, err := watch.NewFileWatcher(file, onChange)
|
2016-07-28 21:35:36 +00:00
|
|
|
if err != nil {
|
2016-08-07 22:53:08 +00:00
|
|
|
return nil, err
|
2016-07-28 21:35:36 +00:00
|
|
|
}
|
2016-08-07 22:53:08 +00:00
|
|
|
|
|
|
|
return &Template{
|
2017-09-18 23:53:26 +00:00
|
|
|
tmpl: tmpl,
|
|
|
|
fw: fw,
|
|
|
|
s: defBufferSize,
|
2016-08-07 22:53:08 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close removes the file watcher
|
|
|
|
func (t *Template) Close() {
|
2016-11-10 22:56:29 +00:00
|
|
|
t.fw.Close()
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
// Write populates a buffer using a template with NGINX configuration
|
|
|
|
// and the servers and upstreams created by Ingress rules
|
2017-02-20 02:34:05 +00:00
|
|
|
func (t *Template) Write(conf config.TemplateConfig) ([]byte, error) {
|
2017-09-18 23:53:26 +00:00
|
|
|
tmplBuf := bytes.NewBuffer(make([]byte, 0, t.s))
|
|
|
|
outCmdBuf := bytes.NewBuffer(make([]byte, 0, t.s))
|
2016-02-22 00:13:08 +00:00
|
|
|
|
2016-11-16 18:24:26 +00:00
|
|
|
defer func() {
|
2017-09-18 23:53:26 +00:00
|
|
|
if t.s < tmplBuf.Cap() {
|
|
|
|
glog.V(2).Infof("adjusting template buffer size from %v to %v", t.s, tmplBuf.Cap())
|
|
|
|
t.s = tmplBuf.Cap()
|
2016-11-16 18:24:26 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-03-15 15:31:39 +00:00
|
|
|
if glog.V(3) {
|
2016-02-22 00:13:08 +00:00
|
|
|
b, err := json.Marshal(conf)
|
|
|
|
if err != nil {
|
2016-08-07 22:53:08 +00:00
|
|
|
glog.Errorf("unexpected error: %v", err)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
2016-03-19 20:17:58 +00:00
|
|
|
glog.Infof("NGINX configuration: %v", string(b))
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 23:53:26 +00:00
|
|
|
err := t.tmpl.Execute(tmplBuf, conf)
|
2017-05-16 20:06:33 +00:00
|
|
|
if err != nil {
|
2016-11-16 18:24:26 +00:00
|
|
|
return nil, err
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// squeezes multiple adjacent empty lines to be single
|
|
|
|
// spaced this is to avoid the use of regular expressions
|
|
|
|
cmd := exec.Command("/ingress-controller/clean-nginx-conf.sh")
|
2017-09-18 23:53:26 +00:00
|
|
|
cmd.Stdin = tmplBuf
|
|
|
|
cmd.Stdout = outCmdBuf
|
2016-11-10 22:56:29 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2017-05-16 20:06:33 +00:00
|
|
|
glog.Warningf("unexpected error cleaning template: %v", err)
|
2017-09-18 23:53:26 +00:00
|
|
|
return tmplBuf.Bytes(), nil
|
2016-09-22 17:08:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 23:53:26 +00:00
|
|
|
return outCmdBuf.Bytes(), nil
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
2016-03-22 18:01:04 +00:00
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
var (
|
|
|
|
funcMap = text_template.FuncMap{
|
|
|
|
"empty": func(input interface{}) bool {
|
|
|
|
check, ok := input.(string)
|
|
|
|
if ok {
|
|
|
|
return len(check) == 0
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
2017-04-09 23:51:38 +00:00
|
|
|
"buildLocation": buildLocation,
|
|
|
|
"buildAuthLocation": buildAuthLocation,
|
|
|
|
"buildAuthResponseHeaders": buildAuthResponseHeaders,
|
|
|
|
"buildProxyPass": buildProxyPass,
|
2017-08-23 00:47:29 +00:00
|
|
|
"filterRateLimits": filterRateLimits,
|
2017-04-09 23:51:38 +00:00
|
|
|
"buildRateLimitZones": buildRateLimitZones,
|
|
|
|
"buildRateLimit": buildRateLimit,
|
|
|
|
"buildResolvers": buildResolvers,
|
2017-06-16 00:43:17 +00:00
|
|
|
"buildUpstreamName": buildUpstreamName,
|
2017-04-09 23:51:38 +00:00
|
|
|
"isLocationAllowed": isLocationAllowed,
|
|
|
|
"buildLogFormatUpstream": buildLogFormatUpstream,
|
2017-06-02 03:30:22 +00:00
|
|
|
"buildDenyVariable": buildDenyVariable,
|
2017-05-07 04:28:21 +00:00
|
|
|
"getenv": os.Getenv,
|
2017-04-09 23:51:38 +00:00
|
|
|
"contains": strings.Contains,
|
|
|
|
"hasPrefix": strings.HasPrefix,
|
|
|
|
"hasSuffix": strings.HasSuffix,
|
|
|
|
"toUpper": strings.ToUpper,
|
|
|
|
"toLower": strings.ToLower,
|
2017-06-09 03:11:00 +00:00
|
|
|
"formatIP": formatIP,
|
2017-06-26 19:39:24 +00:00
|
|
|
"buildNextUpstream": buildNextUpstream,
|
2017-08-25 23:49:44 +00:00
|
|
|
"getIngressInformation": getIngressInformation,
|
2017-08-15 06:23:19 +00:00
|
|
|
"serverConfig": func(all config.TemplateConfig, server *ingress.Server) interface{} {
|
2017-08-19 21:13:02 +00:00
|
|
|
return struct{ First, Second interface{} }{all, server}
|
2017-08-15 06:23:19 +00:00
|
|
|
},
|
2017-08-23 04:57:35 +00:00
|
|
|
"isValidClientBodyBufferSize": isValidClientBodyBufferSize,
|
2017-09-07 21:11:23 +00:00
|
|
|
"buildForwardedFor": buildForwardedFor,
|
2017-09-17 18:03:05 +00:00
|
|
|
"trustHTTPHeaders": trustHTTPHeaders,
|
|
|
|
"trustProxyProtocol": trustProxyProtocol,
|
2016-03-22 18:01:04 +00:00
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
)
|
2016-03-22 18:01:04 +00:00
|
|
|
|
2017-09-09 05:10:38 +00:00
|
|
|
// formatIP will wrap IPv6 addresses in [] and return IPv4 addresses
|
2017-06-09 03:11:00 +00:00
|
|
|
// without modification. If the input cannot be parsed as an IP address
|
|
|
|
// it is returned without modification.
|
|
|
|
func formatIP(input string) string {
|
|
|
|
ip := net.ParseIP(input)
|
|
|
|
if ip == nil {
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
if v4 := ip.To4(); v4 != nil {
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("[%s]", input)
|
|
|
|
}
|
|
|
|
|
2016-12-22 03:00:27 +00:00
|
|
|
// buildResolvers returns the resolvers reading the /etc/resolv.conf file
|
2017-09-22 22:21:32 +00:00
|
|
|
func buildResolvers(input interface{}) string {
|
2017-09-09 05:10:38 +00:00
|
|
|
// NGINX need IPV6 addresses to be surrounded by brackets
|
2017-09-22 22:21:32 +00:00
|
|
|
nss, ok := input.([]net.IP)
|
|
|
|
if !ok {
|
|
|
|
glog.Errorf("expected a '[]net.IP' type but %T was returned", input)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-12-22 03:00:27 +00:00
|
|
|
if len(nss) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
r := []string{"resolver"}
|
|
|
|
for _, ns := range nss {
|
|
|
|
if ing_net.IsIPV6(ns) {
|
|
|
|
r = append(r, fmt.Sprintf("[%v]", ns))
|
|
|
|
} else {
|
|
|
|
r = append(r, fmt.Sprintf("%v", ns))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r = append(r, "valid=30s;")
|
|
|
|
|
|
|
|
return strings.Join(r, " ")
|
|
|
|
}
|
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
// buildLocation produces the location string, if the ingress has redirects
|
|
|
|
// (specified through the ingress.kubernetes.io/rewrite-to annotation)
|
2016-05-25 21:04:34 +00:00
|
|
|
func buildLocation(input interface{}) string {
|
2016-08-07 22:53:08 +00:00
|
|
|
location, ok := input.(*ingress.Location)
|
2016-05-25 21:04:34 +00:00
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
|
2016-05-25 21:04:34 +00:00
|
|
|
return slash
|
|
|
|
}
|
|
|
|
|
|
|
|
path := location.Path
|
2017-08-19 21:13:02 +00:00
|
|
|
if len(location.Rewrite.Target) > 0 && location.Rewrite.Target != path {
|
2017-03-14 11:19:21 +00:00
|
|
|
if path == slash {
|
2017-02-17 13:38:52 +00:00
|
|
|
return fmt.Sprintf("~* %s", path)
|
|
|
|
}
|
2017-03-13 11:15:09 +00:00
|
|
|
// baseuri regex will parse basename from the given location
|
|
|
|
baseuri := `(?<baseuri>.*)`
|
|
|
|
if !strings.HasSuffix(path, slash) {
|
|
|
|
// Not treat the slash after "location path" as a part of baseuri
|
|
|
|
baseuri = fmt.Sprintf(`\/?%s`, baseuri)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(`~* ^%s%s`, path, baseuri)
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2017-09-09 05:10:38 +00:00
|
|
|
// TODO: Needs Unit Tests
|
2016-08-19 14:51:40 +00:00
|
|
|
func buildAuthLocation(input interface{}) string {
|
|
|
|
location, ok := input.(*ingress.Location)
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
|
2016-08-19 14:51:40 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
if location.ExternalAuth.URL == "" {
|
2016-08-19 14:51:40 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
str := base64.URLEncoding.EncodeToString([]byte(location.Path))
|
|
|
|
// avoid locations containing the = char
|
|
|
|
str = strings.Replace(str, "=", "", -1)
|
|
|
|
return fmt.Sprintf("/_external-auth-%v", str)
|
|
|
|
}
|
|
|
|
|
2017-02-04 00:43:15 +00:00
|
|
|
func buildAuthResponseHeaders(input interface{}) []string {
|
|
|
|
location, ok := input.(*ingress.Location)
|
|
|
|
res := []string{}
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
|
2017-02-04 00:43:15 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(location.ExternalAuth.ResponseHeaders) == 0 {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, h := range location.ExternalAuth.ResponseHeaders {
|
|
|
|
hvar := strings.ToLower(h)
|
|
|
|
hvar = strings.NewReplacer("-", "_").Replace(hvar)
|
|
|
|
res = append(res, fmt.Sprintf("auth_request_set $authHeader%v $upstream_http_%v;", i, hvar))
|
|
|
|
res = append(res, fmt.Sprintf("proxy_set_header '%v' $authHeader%v;", h, i))
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2017-02-27 10:00:31 +00:00
|
|
|
func buildLogFormatUpstream(input interface{}) string {
|
2017-03-04 19:46:45 +00:00
|
|
|
cfg, ok := input.(config.Configuration)
|
2017-02-27 10:00:31 +00:00
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected a 'config.Configuration' type but %T was returned", input)
|
|
|
|
return ""
|
2017-02-27 10:00:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:46:45 +00:00
|
|
|
return cfg.BuildLogFormatUpstream()
|
2017-02-27 10:00:31 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
// buildProxyPass produces the proxy pass string, if the ingress has redirects
|
|
|
|
// (specified through the ingress.kubernetes.io/rewrite-to annotation)
|
|
|
|
// If the annotation ingress.kubernetes.io/add-base-url:"true" is specified it will
|
|
|
|
// add a base tag in the head of the response from the service
|
2017-06-16 00:43:17 +00:00
|
|
|
func buildProxyPass(host string, b interface{}, loc interface{}) string {
|
2017-09-22 22:21:32 +00:00
|
|
|
backends, ok := b.([]*ingress.Backend)
|
|
|
|
if !ok {
|
|
|
|
glog.Errorf("expected an '[]*ingress.Backend' type but %T was returned", b)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-11-16 18:24:26 +00:00
|
|
|
location, ok := loc.(*ingress.Location)
|
2016-05-25 21:04:34 +00:00
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected a '*ingress.Location' type but %T was returned", loc)
|
2016-05-25 21:04:34 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
path := location.Path
|
2016-06-01 18:47:37 +00:00
|
|
|
proto := "http"
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2017-06-16 00:43:17 +00:00
|
|
|
upstreamName := location.Backend
|
2016-11-16 18:24:26 +00:00
|
|
|
for _, backend := range backends {
|
|
|
|
if backend.Name == location.Backend {
|
2017-04-09 23:51:38 +00:00
|
|
|
if backend.Secure || backend.SSLPassthrough {
|
2016-11-16 18:24:26 +00:00
|
|
|
proto = "https"
|
|
|
|
}
|
2017-06-16 00:43:17 +00:00
|
|
|
|
|
|
|
if isSticky(host, location, backend.SessionAffinity.CookieSessionAffinity.Locations) {
|
|
|
|
upstreamName = fmt.Sprintf("sticky-%v", upstreamName)
|
|
|
|
}
|
|
|
|
|
2016-11-16 18:24:26 +00:00
|
|
|
break
|
|
|
|
}
|
2016-06-01 18:47:37 +00:00
|
|
|
}
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
// defProxyPass returns the default proxy_pass, just the name of the upstream
|
2017-06-16 00:43:17 +00:00
|
|
|
defProxyPass := fmt.Sprintf("proxy_pass %s://%s;", proto, upstreamName)
|
2016-05-27 14:58:13 +00:00
|
|
|
// if the path in the ingress rule is equals to the target: no special rewrite
|
2017-08-19 21:13:02 +00:00
|
|
|
if path == location.Rewrite.Target {
|
2016-05-27 14:58:13 +00:00
|
|
|
return defProxyPass
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 09:03:50 +00:00
|
|
|
if !strings.HasSuffix(path, slash) {
|
2016-05-25 21:04:34 +00:00
|
|
|
path = fmt.Sprintf("%s/", path)
|
|
|
|
}
|
|
|
|
|
2017-08-19 21:13:02 +00:00
|
|
|
if len(location.Rewrite.Target) > 0 {
|
2016-05-27 14:58:13 +00:00
|
|
|
abu := ""
|
2017-08-19 21:13:02 +00:00
|
|
|
if location.Rewrite.AddBaseURL {
|
2017-03-13 11:15:09 +00:00
|
|
|
// path has a slash suffix, so that it can be connected with baseuri directly
|
|
|
|
bPath := fmt.Sprintf("%s%s", path, "$baseuri")
|
2017-08-21 06:10:35 +00:00
|
|
|
if len(location.Rewrite.BaseURLScheme) > 0 {
|
|
|
|
abu = fmt.Sprintf(`subs_filter '<head(.*)>' '<head$1><base href="%v://$http_host%v">' r;
|
2017-08-31 06:58:01 +00:00
|
|
|
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="%v://$http_host%v">' r;
|
|
|
|
`, location.Rewrite.BaseURLScheme, bPath, location.Rewrite.BaseURLScheme, bPath)
|
2017-08-21 06:10:35 +00:00
|
|
|
} else {
|
|
|
|
abu = fmt.Sprintf(`subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host%v">' r;
|
2017-08-31 06:58:01 +00:00
|
|
|
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host%v">' r;
|
|
|
|
`, bPath, bPath)
|
2017-08-21 06:10:35 +00:00
|
|
|
}
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 21:13:02 +00:00
|
|
|
if location.Rewrite.Target == slash {
|
2016-05-25 21:04:34 +00:00
|
|
|
// special case redirect to /
|
|
|
|
// ie /something to /
|
2016-05-27 14:58:13 +00:00
|
|
|
return fmt.Sprintf(`
|
2017-08-31 06:58:01 +00:00
|
|
|
rewrite %s(.*) /$1 break;
|
|
|
|
rewrite %s / break;
|
|
|
|
proxy_pass %s://%s;
|
|
|
|
%v`, path, location.Path, proto, upstreamName, abu)
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
return fmt.Sprintf(`
|
2017-08-31 06:58:01 +00:00
|
|
|
rewrite %s(.*) %s/$1 break;
|
|
|
|
proxy_pass %s://%s;
|
|
|
|
%v`, path, location.Rewrite.Target, proto, upstreamName, abu)
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// default proxy_pass
|
2016-05-27 14:58:13 +00:00
|
|
|
return defProxyPass
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
2016-05-30 17:39:10 +00:00
|
|
|
|
2017-09-09 05:10:38 +00:00
|
|
|
// TODO: Needs Unit Tests
|
2017-08-23 00:47:29 +00:00
|
|
|
func filterRateLimits(input interface{}) []ratelimit.RateLimit {
|
|
|
|
ratelimits := []ratelimit.RateLimit{}
|
2017-08-22 20:16:59 +00:00
|
|
|
found := sets.String{}
|
2017-08-22 18:53:53 +00:00
|
|
|
|
2017-08-23 00:47:29 +00:00
|
|
|
servers, ok := input.([]*ingress.Server)
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected a '[]ratelimit.RateLimit' type but %T was returned", input)
|
2017-08-23 00:47:29 +00:00
|
|
|
return ratelimits
|
2017-08-21 19:36:31 +00:00
|
|
|
}
|
2017-08-23 00:47:29 +00:00
|
|
|
for _, server := range servers {
|
|
|
|
for _, loc := range server.Locations {
|
2017-08-22 20:16:59 +00:00
|
|
|
if loc.RateLimit.ID != "" && !found.Has(loc.RateLimit.ID) {
|
|
|
|
found.Insert(loc.RateLimit.ID)
|
2017-08-23 00:47:29 +00:00
|
|
|
ratelimits = append(ratelimits, loc.RateLimit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ratelimits
|
2017-08-21 19:36:31 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 05:10:38 +00:00
|
|
|
// TODO: Needs Unit Tests
|
2016-05-30 17:39:10 +00:00
|
|
|
// buildRateLimitZones produces an array of limit_conn_zone in order to allow
|
2017-08-23 00:47:29 +00:00
|
|
|
// rate limiting of request. Each Ingress rule could have up to three zones, one
|
|
|
|
// for connection limit by IP address, one for limiting requests per minute, and
|
|
|
|
// one for limiting requests per second.
|
2017-08-21 19:36:31 +00:00
|
|
|
func buildRateLimitZones(input interface{}) []string {
|
2017-01-24 08:19:28 +00:00
|
|
|
zones := sets.String{}
|
2016-05-30 17:39:10 +00:00
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
servers, ok := input.([]*ingress.Server)
|
2016-05-30 17:39:10 +00:00
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected a '[]*ingress.Server' type but %T was returned", input)
|
2017-01-24 08:19:28 +00:00
|
|
|
return zones.List()
|
2016-05-30 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, server := range servers {
|
|
|
|
for _, loc := range server.Locations {
|
2016-06-01 14:39:12 +00:00
|
|
|
if loc.RateLimit.Connections.Limit > 0 {
|
2017-08-22 12:33:56 +00:00
|
|
|
zone := fmt.Sprintf("limit_conn_zone $limit_%s zone=%v:%vm;",
|
2017-08-23 00:47:29 +00:00
|
|
|
loc.RateLimit.ID,
|
2016-09-22 17:08:35 +00:00
|
|
|
loc.RateLimit.Connections.Name,
|
|
|
|
loc.RateLimit.Connections.SharedSize)
|
2017-01-24 08:19:28 +00:00
|
|
|
if !zones.Has(zone) {
|
|
|
|
zones.Insert(zone)
|
|
|
|
}
|
2016-05-30 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 03:24:48 +00:00
|
|
|
if loc.RateLimit.RPM.Limit > 0 {
|
2017-08-22 12:33:56 +00:00
|
|
|
zone := fmt.Sprintf("limit_req_zone $limit_%s zone=%v:%vm rate=%vr/m;",
|
2017-08-23 00:47:29 +00:00
|
|
|
loc.RateLimit.ID,
|
2017-08-02 03:24:48 +00:00
|
|
|
loc.RateLimit.RPM.Name,
|
|
|
|
loc.RateLimit.RPM.SharedSize,
|
|
|
|
loc.RateLimit.RPM.Limit)
|
|
|
|
if !zones.Has(zone) {
|
|
|
|
zones.Insert(zone)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:39:12 +00:00
|
|
|
if loc.RateLimit.RPS.Limit > 0 {
|
2017-08-22 12:33:56 +00:00
|
|
|
zone := fmt.Sprintf("limit_req_zone $limit_%s zone=%v:%vm rate=%vr/s;",
|
2017-08-23 00:47:29 +00:00
|
|
|
loc.RateLimit.ID,
|
2017-01-23 02:01:51 +00:00
|
|
|
loc.RateLimit.RPS.Name,
|
|
|
|
loc.RateLimit.RPS.SharedSize,
|
|
|
|
loc.RateLimit.RPS.Limit)
|
2017-01-24 08:19:28 +00:00
|
|
|
if !zones.Has(zone) {
|
|
|
|
zones.Insert(zone)
|
|
|
|
}
|
2016-05-30 17:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 08:19:28 +00:00
|
|
|
return zones.List()
|
2016-05-30 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// buildRateLimit produces an array of limit_req to be used inside the Path of
|
2017-08-02 03:24:48 +00:00
|
|
|
// Ingress rules. The order: connections by IP first, then RPS, and RPM last.
|
2016-05-30 17:39:10 +00:00
|
|
|
func buildRateLimit(input interface{}) []string {
|
|
|
|
limits := []string{}
|
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
loc, ok := input.(*ingress.Location)
|
2016-05-30 17:39:10 +00:00
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
|
2016-05-30 17:39:10 +00:00
|
|
|
return limits
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:39:12 +00:00
|
|
|
if loc.RateLimit.Connections.Limit > 0 {
|
2016-05-30 17:39:10 +00:00
|
|
|
limit := fmt.Sprintf("limit_conn %v %v;",
|
|
|
|
loc.RateLimit.Connections.Name, loc.RateLimit.Connections.Limit)
|
|
|
|
limits = append(limits, limit)
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:39:12 +00:00
|
|
|
if loc.RateLimit.RPS.Limit > 0 {
|
2016-05-30 17:39:10 +00:00
|
|
|
limit := fmt.Sprintf("limit_req zone=%v burst=%v nodelay;",
|
2017-01-23 02:01:51 +00:00
|
|
|
loc.RateLimit.RPS.Name, loc.RateLimit.RPS.Burst)
|
2016-05-30 17:39:10 +00:00
|
|
|
limits = append(limits, limit)
|
2017-08-02 03:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if loc.RateLimit.RPM.Limit > 0 {
|
|
|
|
limit := fmt.Sprintf("limit_req zone=%v burst=%v nodelay;",
|
|
|
|
loc.RateLimit.RPM.Name, loc.RateLimit.RPM.Burst)
|
|
|
|
limits = append(limits, limit)
|
2017-08-13 06:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if loc.RateLimit.LimitRateAfter > 0 {
|
|
|
|
limit := fmt.Sprintf("limit_rate_after %vk;",
|
|
|
|
loc.RateLimit.LimitRateAfter)
|
|
|
|
limits = append(limits, limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if loc.RateLimit.LimitRate > 0 {
|
|
|
|
limit := fmt.Sprintf("limit_rate %vk;",
|
|
|
|
loc.RateLimit.LimitRate)
|
|
|
|
limits = append(limits, limit)
|
2016-05-30 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return limits
|
|
|
|
}
|
2016-12-29 20:02:06 +00:00
|
|
|
|
|
|
|
func isLocationAllowed(input interface{}) bool {
|
|
|
|
loc, ok := input.(*ingress.Location)
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
|
2016-12-29 20:02:06 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return loc.Denied == nil
|
|
|
|
}
|
2017-06-02 03:30:22 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
denyPathSlugMap = map[string]string{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// buildDenyVariable returns a nginx variable for a location in a
|
|
|
|
// server to be used in the whitelist check
|
|
|
|
// This method uses a unique id generator library to reduce the
|
|
|
|
// size of the string to be used as a variable in nginx to avoid
|
|
|
|
// issue with the size of the variable bucket size directive
|
|
|
|
func buildDenyVariable(a interface{}) string {
|
2017-09-22 22:21:32 +00:00
|
|
|
l, ok := a.(string)
|
|
|
|
if !ok {
|
|
|
|
glog.Errorf("expected a 'string' type but %T was returned", a)
|
|
|
|
return ""
|
|
|
|
}
|
2017-06-02 03:30:22 +00:00
|
|
|
|
|
|
|
if _, ok := denyPathSlugMap[l]; !ok {
|
2017-08-22 12:33:56 +00:00
|
|
|
denyPathSlugMap[l] = buildRandomUUID()
|
2017-06-02 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("$deny_%v", denyPathSlugMap[l])
|
|
|
|
}
|
2017-06-16 00:43:17 +00:00
|
|
|
|
2017-09-09 05:10:38 +00:00
|
|
|
// TODO: Needs Unit Tests
|
2017-06-16 00:43:17 +00:00
|
|
|
func buildUpstreamName(host string, b interface{}, loc interface{}) string {
|
2017-09-22 22:21:32 +00:00
|
|
|
|
|
|
|
backends, ok := b.([]*ingress.Backend)
|
|
|
|
if !ok {
|
|
|
|
glog.Errorf("expected an '[]*ingress.Backend' type but %T was returned", b)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-06-16 00:43:17 +00:00
|
|
|
location, ok := loc.(*ingress.Location)
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected a '*ingress.Location' type but %T was returned", loc)
|
2017-06-16 00:43:17 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
upstreamName := location.Backend
|
|
|
|
|
|
|
|
for _, backend := range backends {
|
|
|
|
if backend.Name == location.Backend {
|
|
|
|
if backend.SessionAffinity.AffinityType == "cookie" &&
|
|
|
|
isSticky(host, location, backend.SessionAffinity.CookieSessionAffinity.Locations) {
|
|
|
|
upstreamName = fmt.Sprintf("sticky-%v", upstreamName)
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return upstreamName
|
|
|
|
}
|
|
|
|
|
2017-09-09 05:10:38 +00:00
|
|
|
// TODO: Needs Unit Tests
|
2017-06-16 00:43:17 +00:00
|
|
|
func isSticky(host string, loc *ingress.Location, stickyLocations map[string][]string) bool {
|
|
|
|
if _, ok := stickyLocations[host]; ok {
|
|
|
|
for _, sl := range stickyLocations[host] {
|
|
|
|
if sl == loc.Path {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2017-06-26 19:39:24 +00:00
|
|
|
|
|
|
|
func buildNextUpstream(input interface{}) string {
|
|
|
|
nextUpstream, ok := input.(string)
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected a 'string' type but %T was returned", input)
|
|
|
|
return ""
|
2017-06-26 19:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(nextUpstream, " ")
|
|
|
|
|
|
|
|
nextUpstreamCodes := make([]string, 0, len(parts))
|
|
|
|
for _, v := range parts {
|
|
|
|
if v != "" && v != "non_idempotent" {
|
|
|
|
nextUpstreamCodes = append(nextUpstreamCodes, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(nextUpstreamCodes, " ")
|
2017-08-19 21:13:02 +00:00
|
|
|
}
|
2017-08-20 23:39:58 +00:00
|
|
|
|
2017-08-22 12:33:56 +00:00
|
|
|
// buildRandomUUID return a random string to be used in the template
|
|
|
|
func buildRandomUUID() string {
|
|
|
|
s := uuid.New()
|
|
|
|
return strings.Replace(s, "-", "", -1)
|
|
|
|
}
|
2017-08-23 04:57:35 +00:00
|
|
|
|
|
|
|
func isValidClientBodyBufferSize(input interface{}) bool {
|
|
|
|
s, ok := input.(string)
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected an 'string' type but %T was returned", input)
|
2017-08-23 04:57:35 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
sLowercase := strings.ToLower(s)
|
|
|
|
|
|
|
|
kCheck := strings.TrimSuffix(sLowercase, "k")
|
|
|
|
_, err := strconv.Atoi(kCheck)
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
mCheck := strings.TrimSuffix(sLowercase, "m")
|
|
|
|
_, err = strconv.Atoi(mCheck)
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Errorf("client-body-buffer-size '%v' was provided in an incorrect format, hence it will not be set.", s)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2017-08-25 23:49:44 +00:00
|
|
|
|
|
|
|
type ingressInformation struct {
|
2017-08-26 01:39:30 +00:00
|
|
|
Namespace string
|
|
|
|
Rule string
|
|
|
|
Service string
|
|
|
|
Annotations map[string]string
|
2017-08-25 23:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getIngressInformation(i, p interface{}) *ingressInformation {
|
|
|
|
ing, ok := i.(*extensions.Ingress)
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected an '*extensions.Ingress' type but %T was returned", i)
|
2017-08-25 23:49:44 +00:00
|
|
|
return &ingressInformation{}
|
|
|
|
}
|
|
|
|
|
|
|
|
path, ok := p.(string)
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected a 'string' type but %T was returned", p)
|
2017-08-25 23:49:44 +00:00
|
|
|
return &ingressInformation{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ing == nil {
|
|
|
|
return &ingressInformation{}
|
|
|
|
}
|
|
|
|
|
|
|
|
info := &ingressInformation{
|
2017-08-26 01:39:30 +00:00
|
|
|
Namespace: ing.GetNamespace(),
|
|
|
|
Rule: ing.GetName(),
|
|
|
|
Annotations: ing.Annotations,
|
2017-08-25 23:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ing.Spec.Backend != nil {
|
|
|
|
info.Service = ing.Spec.Backend.ServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range ing.Spec.Rules {
|
|
|
|
if rule.HTTP == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rPath := range rule.HTTP.Paths {
|
|
|
|
if path == rPath.Path {
|
|
|
|
info.Service = rPath.Backend.ServiceName
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
|
|
|
}
|
2017-09-07 21:11:23 +00:00
|
|
|
|
|
|
|
func buildForwardedFor(input interface{}) string {
|
|
|
|
s, ok := input.(string)
|
|
|
|
if !ok {
|
2017-09-22 22:21:32 +00:00
|
|
|
glog.Errorf("expected a 'string' type but %T was returned", input)
|
|
|
|
return ""
|
2017-09-07 21:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ffh := strings.Replace(s, "-", "_", -1)
|
|
|
|
ffh = strings.ToLower(ffh)
|
|
|
|
return fmt.Sprintf("$http_%v", ffh)
|
|
|
|
}
|
2017-09-17 18:03:05 +00:00
|
|
|
|
|
|
|
func trustHTTPHeaders(input interface{}) bool {
|
|
|
|
conf, ok := input.(config.TemplateConfig)
|
|
|
|
if !ok {
|
2017-09-29 21:03:27 +00:00
|
|
|
glog.Errorf("%v", input)
|
2017-09-17 18:03:05 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf.Cfg.RealClientFrom == "http-proxy" ||
|
2017-10-04 17:11:51 +00:00
|
|
|
(conf.Cfg.RealClientFrom == "auto" && !conf.Cfg.UseProxyProtocol)
|
2017-09-17 18:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func trustProxyProtocol(input interface{}) bool {
|
|
|
|
conf, ok := input.(config.TemplateConfig)
|
|
|
|
if !ok {
|
2017-09-29 21:03:27 +00:00
|
|
|
glog.Errorf("%v", input)
|
2017-09-17 18:03:05 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf.Cfg.RealClientFrom == "tcp-proxy" ||
|
2017-09-26 17:20:36 +00:00
|
|
|
(conf.Cfg.RealClientFrom == "auto" && conf.Cfg.UseProxyProtocol)
|
2017-09-17 18:03:05 +00:00
|
|
|
}
|