Merge pull request #3174 from Shopify/rewrite-regex

Generalize Rewrite Block Creation and Deprecate AddBaseUrl (not backwards compatible)
This commit is contained in:
Kubernetes Prow Robot 2019-01-02 12:30:18 -08:00 committed by GitHub
commit 71cc6df74f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 71 additions and 396 deletions

View file

@ -27,11 +27,6 @@ import (
type Config struct {
// Target URI where the traffic must be redirected
Target string `json:"target"`
// AddBaseURL indicates if is required to add a base tag in the head
// of the responses from the upstream servers
AddBaseURL bool `json:"addBaseUrl"`
// BaseURLScheme override for the scheme passed to the base tag
BaseURLScheme string `json:"baseUrlScheme"`
// SSLRedirect indicates if the location section is accessible SSL only
SSLRedirect bool `json:"sslRedirect"`
// ForceSSLRedirect indicates if the location section is accessible SSL only
@ -53,12 +48,6 @@ func (r1 *Config) Equal(r2 *Config) bool {
if r1.Target != r2.Target {
return false
}
if r1.AddBaseURL != r2.AddBaseURL {
return false
}
if r1.BaseURLScheme != r2.BaseURLScheme {
return false
}
if r1.SSLRedirect != r2.SSLRedirect {
return false
}
@ -101,8 +90,6 @@ func (a rewrite) Parse(ing *extensions.Ingress) (interface{}, error) {
config.ForceSSLRedirect = a.r.GetDefaultBackend().ForceSSLRedirect
}
config.AddBaseURL, _ = parser.GetBoolAnnotation("add-base-url", ing)
config.BaseURLScheme, _ = parser.GetStringAnnotation("base-url-scheme", ing)
config.AppRoot, _ = parser.GetStringAnnotation("app-root", ing)
config.UseRegex, _ = parser.GetBoolAnnotation("use-regex", ing)

View file

@ -339,19 +339,6 @@ func buildLocation(input interface{}, enforceRegex bool) string {
}
path := location.Path
if needsRewrite(location) {
if path == slash {
return fmt.Sprintf("~* ^%s", path)
}
// 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)
}
if enforceRegex {
return fmt.Sprintf(`~* "^%s"`, path)
}
@ -466,48 +453,16 @@ func buildProxyPass(host string, b interface{}, loc interface{}) string {
return defProxyPass
}
if !strings.HasSuffix(path, slash) {
path = fmt.Sprintf("%s/", path)
}
if len(location.Rewrite.Target) > 0 {
var abu string
var xForwardedPrefix string
if location.Rewrite.AddBaseURL {
bPath := fmt.Sprintf("%s$escaped_base_uri", path)
regex := `(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)`
scheme := "$scheme"
if len(location.Rewrite.BaseURLScheme) > 0 {
scheme = location.Rewrite.BaseURLScheme
}
abu = fmt.Sprintf(`
set_escape_uri $escaped_base_uri $baseuri;
subs_filter '%v' '$1<base href="%v://$http_host%v">' ro;
`, regex, scheme, bPath)
}
if location.XForwardedPrefix {
xForwardedPrefix = fmt.Sprintf("proxy_set_header X-Forwarded-Prefix \"%s\";\n", path)
}
if location.Rewrite.Target == slash {
// special case redirect to /
// ie /something to /
return fmt.Sprintf(`
rewrite "(?i)%s(.*)" /$1 break;
rewrite "(?i)%s$" / break;
%v%v %s%s;
%v`, path, location.Path, xForwardedPrefix, proxyPass, proto, upstreamName, abu)
}
return fmt.Sprintf(`
rewrite "(?i)%s(.*)" %s/$1 break;
rewrite "(?i)%s$" %s/ break;
%v%v %s%s;
%v`, path, location.Rewrite.Target, location.Path, location.Rewrite.Target, xForwardedPrefix, proxyPass, proto, upstreamName, abu)
rewrite "(?i)%s" %s break;
%v%v %s%s;`, path, location.Rewrite.Target, xForwardedPrefix, proxyPass, proto, upstreamName)
}
// default proxy_pass

View file

@ -45,8 +45,6 @@ var (
Target string
Location string
ProxyPass string
AddBaseURL bool
BaseURLScheme string
Sticky bool
XForwardedPrefix bool
SecureBackend bool
@ -58,8 +56,6 @@ var (
"/",
"proxy_pass https://upstream_balancer;",
false,
"",
false,
false,
true,
false,
@ -70,8 +66,6 @@ var (
"/",
"proxy_pass https://upstream_balancer;",
false,
"",
false,
false,
true,
false,
@ -81,8 +75,6 @@ var (
"/",
"/",
"proxy_pass https://upstream_balancer;",
false,
"",
true,
false,
true,
@ -94,8 +86,6 @@ var (
"/",
"proxy_pass http://upstream_balancer;",
false,
"",
false,
false,
false,
false,
@ -106,8 +96,6 @@ var (
"/",
"proxy_pass http://upstream_balancer;",
false,
"",
false,
false,
false,
false,
@ -115,157 +103,10 @@ var (
"redirect / to /jenkins": {
"/",
"/jenkins",
"~* ^/",
`~* "^/"`,
`
rewrite "(?i)/(.*)" /jenkins/$1 break;
rewrite "(?i)/$" /jenkins/ break;
proxy_pass http://upstream_balancer;
`,
false,
"",
false,
false,
false,
true,
},
"redirect /something to /": {
"/something",
"/",
`~* "^/something\/?(?<baseuri>.*)"`,
`
rewrite "(?i)/something/(.*)" /$1 break;
rewrite "(?i)/something$" / break;
proxy_pass http://upstream_balancer;
`,
false,
"",
false,
false,
false,
true,
},
"redirect /end-with-slash/ to /not-root": {
"/end-with-slash/",
"/not-root",
`~* "^/end-with-slash/(?<baseuri>.*)"`,
`
rewrite "(?i)/end-with-slash/(.*)" /not-root/$1 break;
rewrite "(?i)/end-with-slash/$" /not-root/ break;
proxy_pass http://upstream_balancer;
`,
false,
"",
false,
false,
false,
true,
},
"redirect /something-complex to /not-root": {
"/something-complex",
"/not-root",
`~* "^/something-complex\/?(?<baseuri>.*)"`,
`
rewrite "(?i)/something-complex/(.*)" /not-root/$1 break;
rewrite "(?i)/something-complex$" /not-root/ break;
proxy_pass http://upstream_balancer;
`,
false,
"",
false,
false,
false,
true,
},
"redirect / to /jenkins and rewrite": {
"/",
"/jenkins",
"~* ^/",
`
rewrite "(?i)/(.*)" /jenkins/$1 break;
rewrite "(?i)/$" /jenkins/ break;
proxy_pass http://upstream_balancer;
set_escape_uri $escaped_base_uri $baseuri;
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="$scheme://$http_host/$escaped_base_uri">' ro;
`,
true,
"",
false,
false,
false,
true,
},
"redirect /something to / and rewrite": {
"/something",
"/",
`~* "^/something\/?(?<baseuri>.*)"`,
`
rewrite "(?i)/something/(.*)" /$1 break;
rewrite "(?i)/something$" / break;
proxy_pass http://upstream_balancer;
set_escape_uri $escaped_base_uri $baseuri;
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="$scheme://$http_host/something/$escaped_base_uri">' ro;
`,
true,
"",
false,
false,
false,
true,
},
"redirect /end-with-slash/ to /not-root and rewrite": {
"/end-with-slash/",
"/not-root",
`~* "^/end-with-slash/(?<baseuri>.*)"`,
`
rewrite "(?i)/end-with-slash/(.*)" /not-root/$1 break;
rewrite "(?i)/end-with-slash/$" /not-root/ break;
proxy_pass http://upstream_balancer;
set_escape_uri $escaped_base_uri $baseuri;
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="$scheme://$http_host/end-with-slash/$escaped_base_uri">' ro;
`,
true,
"",
false,
false,
false,
true,
},
"redirect /something-complex to /not-root and rewrite": {
"/something-complex",
"/not-root",
`~* "^/something-complex\/?(?<baseuri>.*)"`,
`
rewrite "(?i)/something-complex/(.*)" /not-root/$1 break;
rewrite "(?i)/something-complex$" /not-root/ break;
proxy_pass http://upstream_balancer;
set_escape_uri $escaped_base_uri $baseuri;
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="$scheme://$http_host/something-complex/$escaped_base_uri">' ro;
`,
true,
"",
false,
false,
false,
true,
},
"redirect /something to / and rewrite with specific scheme": {
"/something",
"/",
`~* "^/something\/?(?<baseuri>.*)"`,
`
rewrite "(?i)/something/(.*)" /$1 break;
rewrite "(?i)/something$" / break;
proxy_pass http://upstream_balancer;
set_escape_uri $escaped_base_uri $baseuri;
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="http://$http_host/something/$escaped_base_uri">' ro;
`,
true,
"http",
rewrite "(?i)/" /jenkins break;
proxy_pass http://upstream_balancer;`,
false,
false,
false,
@ -274,14 +115,10 @@ subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="
"redirect / to /something with sticky enabled": {
"/",
"/something",
`~* ^/`,
`~* "^/"`,
`
rewrite "(?i)/(.*)" /something/$1 break;
rewrite "(?i)/$" /something/ break;
proxy_pass http://upstream_balancer;
`,
false,
"http",
rewrite "(?i)/" /something break;
proxy_pass http://upstream_balancer;`,
true,
false,
false,
@ -290,14 +127,10 @@ proxy_pass http://upstream_balancer;
"redirect / to /something with sticky and dynamic config enabled": {
"/",
"/something",
`~* ^/`,
`~* "^/"`,
`
rewrite "(?i)/(.*)" /something/$1 break;
rewrite "(?i)/$" /something/ break;
proxy_pass http://upstream_balancer;
`,
false,
"http",
rewrite "(?i)/" /something break;
proxy_pass http://upstream_balancer;`,
true,
false,
false,
@ -306,15 +139,11 @@ proxy_pass http://upstream_balancer;
"add the X-Forwarded-Prefix header": {
"/there",
"/something",
`~* "^/there\/?(?<baseuri>.*)"`,
`~* "^/there"`,
`
rewrite "(?i)/there/(.*)" /something/$1 break;
rewrite "(?i)/there$" /something/ break;
proxy_set_header X-Forwarded-Prefix "/there/";
proxy_pass http://upstream_balancer;
`,
false,
"http",
rewrite "(?i)/there" /something break;
proxy_set_header X-Forwarded-Prefix "/there";
proxy_pass http://upstream_balancer;`,
true,
true,
false,
@ -326,8 +155,6 @@ proxy_pass http://upstream_balancer;
`~* "^/something"`,
"proxy_pass http://upstream_balancer;",
false,
"",
false,
false,
false,
true,
@ -385,7 +212,7 @@ func TestBuildLocation(t *testing.T) {
for k, tc := range tmplFuncTestcases {
loc := &ingress.Location{
Path: tc.Path,
Rewrite: rewrite.Config{Target: tc.Target, AddBaseURL: tc.AddBaseURL},
Rewrite: rewrite.Config{Target: tc.Target},
}
newLoc := buildLocation(loc, tc.enforceRegex)
@ -402,7 +229,7 @@ func TestBuildProxyPass(t *testing.T) {
for k, tc := range tmplFuncTestcases {
loc := &ingress.Location{
Path: tc.Path,
Rewrite: rewrite.Config{Target: tc.Target, AddBaseURL: tc.AddBaseURL, BaseURLScheme: tc.BaseURLScheme},
Rewrite: rewrite.Config{Target: tc.Target},
Backend: defaultBackend,
XForwardedPrefix: tc.XForwardedPrefix,
}
@ -768,7 +595,7 @@ func TestBuildUpstreamName(t *testing.T) {
for k, tc := range tmplFuncTestcases {
loc := &ingress.Location{
Path: tc.Path,
Rewrite: rewrite.Config{Target: tc.Target, AddBaseURL: tc.AddBaseURL, BaseURLScheme: tc.BaseURLScheme},
Rewrite: rewrite.Config{Target: tc.Target},
Backend: defaultBackend,
XForwardedPrefix: tc.XForwardedPrefix,
}