Fix sticky session for ingress without host
This commit is contained in:
parent
8db541e24b
commit
6c92c80073
4 changed files with 75 additions and 5 deletions
|
|
@ -82,11 +82,16 @@ local function get_failed_upstreams()
|
|||
end
|
||||
|
||||
local function should_set_cookie(self)
|
||||
if self.cookie_session_affinity.locations and ngx.var.host then
|
||||
local locs = self.cookie_session_affinity.locations[ngx.var.host]
|
||||
local host = ngx.var.host
|
||||
if ngx.var.server_name == '_' then
|
||||
host = ngx.var.server_name
|
||||
end
|
||||
|
||||
if self.cookie_session_affinity.locations then
|
||||
local locs = self.cookie_session_affinity.locations[host]
|
||||
if locs == nil then
|
||||
-- Based off of wildcard hostname in ../certificate.lua
|
||||
local wildcard_host, _, err = ngx.re.sub(ngx.var.host, "^[^\\.]+\\.", "*.", "jo")
|
||||
local wildcard_host, _, err = ngx.re.sub(host, "^[^\\.]+\\.", "*.", "jo")
|
||||
if err then
|
||||
ngx.log(ngx.ERR, "error: ", err);
|
||||
elseif wildcard_host then
|
||||
|
|
|
|||
|
|
@ -202,7 +202,7 @@ describe("Sticky", function()
|
|||
local sticky_balancer_instance = sticky:new(b)
|
||||
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
|
||||
assert.spy(s).was_called()
|
||||
end
|
||||
end
|
||||
|
||||
it("sets a cookie on the client", function() test(sticky_balanced) end)
|
||||
it("sets a cookie on the client", function() test(sticky_persistent) end)
|
||||
|
|
@ -353,4 +353,41 @@ describe("Sticky", function()
|
|||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when client doesn't have a cookie set and no host header, matching default server '_'",
|
||||
function()
|
||||
before_each(function ()
|
||||
ngx.var.host = "not-default-server"
|
||||
ngx.var.server_name = "_"
|
||||
end)
|
||||
|
||||
local function test(sticky)
|
||||
local s = {}
|
||||
cookie.new = function(self)
|
||||
local cookie_instance = {
|
||||
set = function(self, payload)
|
||||
assert.equal(payload.key, test_backend.sessionAffinityConfig.cookieSessionAffinity.name)
|
||||
assert.equal(payload.path, ngx.var.location_path)
|
||||
assert.equal(payload.domain, nil)
|
||||
assert.equal(payload.httponly, true)
|
||||
assert.equal(payload.secure, false)
|
||||
return true, nil
|
||||
end,
|
||||
get = function(k) return false end,
|
||||
}
|
||||
s = spy.on(cookie_instance, "set")
|
||||
return cookie_instance, false
|
||||
end
|
||||
|
||||
local b = get_test_backend()
|
||||
b.sessionAffinityConfig.cookieSessionAffinity.locations = {}
|
||||
b.sessionAffinityConfig.cookieSessionAffinity.locations["_"] = {"/"}
|
||||
local sticky_balancer_instance = sticky:new(b)
|
||||
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
|
||||
assert.spy(s).was_called()
|
||||
end
|
||||
|
||||
it("sets a cookie on the client", function() test(sticky_balanced) end)
|
||||
it("sets a cookie on the client", function() test(sticky_persistent) end)
|
||||
end)
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue