Only set cookies on paths that enable session affinity
This commit is contained in:
parent
82721e575d
commit
2b109b360b
4 changed files with 92 additions and 5 deletions
|
|
@ -18,6 +18,7 @@ function _M.new(self, backend)
|
|||
cookie_expires = backend["sessionAffinityConfig"]["cookieSessionAffinity"]["expires"],
|
||||
cookie_max_age = backend["sessionAffinityConfig"]["cookieSessionAffinity"]["maxage"],
|
||||
cookie_path = backend["sessionAffinityConfig"]["cookieSessionAffinity"]["path"],
|
||||
cookie_locations = backend["sessionAffinityConfig"]["cookieSessionAffinity"]["locations"],
|
||||
digest_func = digest_func,
|
||||
traffic_shaping_policy = backend.trafficShapingPolicy,
|
||||
alternative_backends = backend.alternativeBackends,
|
||||
|
|
@ -81,7 +82,18 @@ function _M.balance(self)
|
|||
if not key then
|
||||
local random_str = string.format("%s.%s", ngx.now(), ngx.worker.pid())
|
||||
key = encrypted_endpoint_string(self, random_str)
|
||||
set_cookie(self, key)
|
||||
|
||||
if self.cookie_locations then
|
||||
local locs = self.cookie_locations[ngx.var.host]
|
||||
if locs ~= nil then
|
||||
for _, path in pairs(locs) do
|
||||
if ngx.var.location_path == path then
|
||||
set_cookie(self, key)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return self.instance:find(key)
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ end
|
|||
|
||||
describe("Sticky", function()
|
||||
before_each(function()
|
||||
mock_ngx({ var = { location_path = "/" } })
|
||||
mock_ngx({ var = { location_path = "/", host = "test.com" } })
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
|
|
@ -102,7 +102,8 @@ describe("Sticky", function()
|
|||
cookie.new = mocked_cookie_new
|
||||
end)
|
||||
|
||||
context("when client doesn't have a cookie set", function()
|
||||
|
||||
context("when client doesn't have a cookie set and location is in cookie_locations", function()
|
||||
it("picks an endpoint for the client", function()
|
||||
local sticky_balancer_instance = sticky:new(test_backend)
|
||||
local peer = sticky_balancer_instance:balance()
|
||||
|
|
@ -119,7 +120,7 @@ describe("Sticky", function()
|
|||
local expected_len = #util[test_backend_hash_fn .. "_digest"]("anything")
|
||||
assert.equal(#payload.value, expected_len)
|
||||
assert.equal(payload.path, ngx.var.location_path)
|
||||
assert.equal(payload.domain, nil)
|
||||
assert.equal(payload.domain, ngx.var.host)
|
||||
assert.equal(payload.httponly, true)
|
||||
return true, nil
|
||||
end,
|
||||
|
|
@ -128,12 +129,47 @@ describe("Sticky", function()
|
|||
s = spy.on(cookie_instance, "set")
|
||||
return cookie_instance, false
|
||||
end
|
||||
local sticky_balancer_instance = sticky:new(get_test_backend())
|
||||
local b = get_test_backend()
|
||||
b.sessionAffinityConfig.cookieSessionAffinity.locations = {}
|
||||
b.sessionAffinityConfig.cookieSessionAffinity.locations["test.com"] = {"/"}
|
||||
local sticky_balancer_instance = sticky:new(b)
|
||||
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
|
||||
assert.spy(s).was_called()
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when client doesn't have a cookie set and location not in cookie_locations", function()
|
||||
it("picks an endpoint for the client", function()
|
||||
local sticky_balancer_instance = sticky:new(test_backend)
|
||||
local peer = sticky_balancer_instance:balance()
|
||||
assert.equal(peer, test_backend_endpoint)
|
||||
end)
|
||||
|
||||
it("does not set a cookie on the client", function()
|
||||
local s = {}
|
||||
cookie.new = function(self)
|
||||
local test_backend_hash_fn = test_backend.sessionAffinityConfig.cookieSessionAffinity.hash
|
||||
local cookie_instance = {
|
||||
set = function(self, payload)
|
||||
assert.equal(payload.key, test_backend.sessionAffinityConfig.cookieSessionAffinity.name)
|
||||
local expected_len = #util[test_backend_hash_fn .. "_digest"]("anything")
|
||||
assert.equal(#payload.value, expected_len)
|
||||
assert.equal(payload.path, ngx.var.location_path)
|
||||
assert.equal(payload.domain, ngx.var.host)
|
||||
assert.equal(payload.httponly, true)
|
||||
return true, nil
|
||||
end,
|
||||
get = function(k) return false end,
|
||||
}
|
||||
s = spy.on(cookie_instance, "set")
|
||||
return cookie_instance, false
|
||||
end
|
||||
local sticky_balancer_instance = sticky:new(get_test_backend())
|
||||
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
|
||||
assert.spy(s).was_not_called()
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when client has a cookie set", function()
|
||||
it("does not set a cookie", function()
|
||||
local s = {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue