Remove session-cookie-hash annotation
This commit is contained in:
parent
79c52cf094
commit
d3ac73be79
18 changed files with 22 additions and 189 deletions
|
|
@ -2,29 +2,20 @@ local balancer_resty = require("balancer.resty")
|
|||
local resty_chash = require("resty.chash")
|
||||
local util = require("util")
|
||||
local ck = require("resty.cookie")
|
||||
local math = require("math")
|
||||
|
||||
local _M = balancer_resty:new({ factory = resty_chash, name = "sticky" })
|
||||
local DEFAULT_COOKIE_NAME = "route"
|
||||
|
||||
local function get_digest_func(hash)
|
||||
local digest_func = util.md5_digest
|
||||
if hash == "sha1" then
|
||||
digest_func = util.sha1_digest
|
||||
end
|
||||
return digest_func
|
||||
end
|
||||
|
||||
function _M.cookie_name(self)
|
||||
return self.cookie_session_affinity.name or DEFAULT_COOKIE_NAME
|
||||
end
|
||||
|
||||
function _M.new(self, backend)
|
||||
local nodes = util.get_nodes(backend.endpoints)
|
||||
local digest_func = get_digest_func(backend["sessionAffinityConfig"]["cookieSessionAffinity"]["hash"])
|
||||
|
||||
local o = {
|
||||
instance = self.factory:new(nodes),
|
||||
digest_func = digest_func,
|
||||
traffic_shaping_policy = backend.trafficShapingPolicy,
|
||||
alternative_backends = backend.alternativeBackends,
|
||||
cookie_session_affinity = backend["sessionAffinityConfig"]["cookieSessionAffinity"]
|
||||
|
|
@ -34,15 +25,6 @@ function _M.new(self, backend)
|
|||
return o
|
||||
end
|
||||
|
||||
local function encrypted_endpoint_string(self, endpoint_string)
|
||||
local encrypted, err = self.digest_func(endpoint_string)
|
||||
if err ~= nil then
|
||||
ngx.log(ngx.ERR, err)
|
||||
end
|
||||
|
||||
return encrypted
|
||||
end
|
||||
|
||||
local function set_cookie(self, value)
|
||||
local cookie, err = ck:new()
|
||||
if not cookie then
|
||||
|
|
@ -86,8 +68,7 @@ function _M.balance(self)
|
|||
|
||||
local key = cookie:get(self:cookie_name())
|
||||
if not key then
|
||||
local random_str = string.format("%s.%s", ngx.now(), ngx.worker.pid())
|
||||
key = encrypted_endpoint_string(self, random_str)
|
||||
key = string.format("%s.%s.%s", ngx.now(), ngx.worker.pid(), math.random(999999))
|
||||
|
||||
if self.cookie_session_affinity.locations then
|
||||
local locs = self.cookie_session_affinity.locations[ngx.var.host]
|
||||
|
|
@ -118,7 +99,6 @@ function _M.sync(self, backend)
|
|||
end
|
||||
|
||||
self.cookie_session_affinity = backend.sessionAffinityConfig.cookieSessionAffinity
|
||||
self.digest_func = get_digest_func(backend.sessionAffinityConfig.cookieSessionAffinity.hash)
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
|
|
@ -68,26 +68,6 @@ describe("Sticky", function()
|
|||
assert.equal(sticky_balancer_instance:cookie_name(), default_cookie_name)
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when backend specifies hash function", function()
|
||||
it("returns an instance with the corresponding hash implementation", function()
|
||||
local sticky_balancer_instance = sticky:new(test_backend)
|
||||
local test_backend_hash_fn = test_backend.sessionAffinityConfig.cookieSessionAffinity.hash
|
||||
local test_backend_hash_implementation = util[test_backend_hash_fn .. "_digest"]
|
||||
assert.equal(sticky_balancer_instance.digest_func, test_backend_hash_implementation)
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when backend does not specify hash function", function()
|
||||
it("returns an instance with the default implementation (md5)", function()
|
||||
local temp_backend = util.deepcopy(test_backend)
|
||||
temp_backend.sessionAffinityConfig.cookieSessionAffinity.hash = nil
|
||||
local sticky_balancer_instance = sticky:new(temp_backend)
|
||||
local default_hash_fn = "md5"
|
||||
local default_hash_implementation = util[default_hash_fn .. "_digest"]
|
||||
assert.equal(sticky_balancer_instance.digest_func, default_hash_implementation)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("balance()", function()
|
||||
|
|
@ -112,12 +92,9 @@ describe("Sticky", function()
|
|||
it("sets 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, nil)
|
||||
assert.equal(payload.httponly, true)
|
||||
|
|
@ -141,12 +118,9 @@ describe("Sticky", function()
|
|||
ngx.var.https = "on"
|
||||
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, nil)
|
||||
assert.equal(payload.httponly, true)
|
||||
|
|
@ -177,12 +151,9 @@ describe("Sticky", function()
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ local function reset_backends()
|
|||
{ address = "10.184.97.100", port = "8080", maxFails = 0, failTimeout = 0 },
|
||||
{ address = "10.184.98.239", port = "8080", maxFails = 0, failTimeout = 0 },
|
||||
},
|
||||
sessionAffinityConfig = { name = "", cookieSessionAffinity = { name = "", hash = "" } },
|
||||
sessionAffinityConfig = { name = "", cookieSessionAffinity = { name = "" } },
|
||||
},
|
||||
{ name = "my-dummy-app-1", ["load-balance"] = "round_robin", },
|
||||
{
|
||||
|
|
@ -38,12 +38,12 @@ local function reset_backends()
|
|||
},
|
||||
{
|
||||
name = "my-dummy-app-3", ["load-balance"] = "ewma",
|
||||
sessionAffinityConfig = { name = "cookie", cookieSessionAffinity = { name = "route", hash = "sha1" } }
|
||||
sessionAffinityConfig = { name = "cookie", cookieSessionAffinity = { name = "route" } }
|
||||
},
|
||||
{ name = "my-dummy-app-4", ["load-balance"] = "ewma", },
|
||||
{
|
||||
name = "my-dummy-app-5", ["load-balance"] = "ewma", ["upstream-hash-by"] = "$request_uri",
|
||||
sessionAffinityConfig = { name = "cookie", cookieSessionAffinity = { name = "route", hash = "sha1" } }
|
||||
sessionAffinityConfig = { name = "cookie", cookieSessionAffinity = { name = "route" } }
|
||||
},
|
||||
}
|
||||
end
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ function get_backends()
|
|||
{
|
||||
name = "my-dummy-backend-1", ["load-balance"] = "sticky",
|
||||
endpoints = { { address = "10.183.7.40", port = "8080", maxFails = 0, failTimeout = 0 } },
|
||||
sessionAffinityConfig = { name = "cookie", cookieSessionAffinity = { name = "route", hash = "sha1" } },
|
||||
sessionAffinityConfig = { name = "cookie", cookieSessionAffinity = { name = "route" } },
|
||||
},
|
||||
{
|
||||
name = "my-dummy-backend-2", ["load-balance"] = "ewma",
|
||||
|
|
|
|||
|
|
@ -1,8 +1,5 @@
|
|||
local string_len = string.len
|
||||
local string_sub = string.sub
|
||||
local resty_str = require("resty.string")
|
||||
local resty_sha1 = require("resty.sha1")
|
||||
local resty_md5 = require("resty.md5")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -18,30 +15,6 @@ function _M.get_nodes(endpoints)
|
|||
return nodes
|
||||
end
|
||||
|
||||
local function hash_digest(hash_factory, message)
|
||||
local hash = hash_factory:new()
|
||||
if not hash then
|
||||
return nil, "failed to create object"
|
||||
end
|
||||
local ok = hash:update(message)
|
||||
if not ok then
|
||||
return nil, "failed to add data"
|
||||
end
|
||||
local binary_digest = hash:final()
|
||||
if binary_digest == nil then
|
||||
return nil, "failed to create digest"
|
||||
end
|
||||
return resty_str.to_hex(binary_digest), nil
|
||||
end
|
||||
|
||||
function _M.sha1_digest(message)
|
||||
return hash_digest(resty_sha1, message)
|
||||
end
|
||||
|
||||
function _M.md5_digest(message)
|
||||
return hash_digest(resty_md5, message)
|
||||
end
|
||||
|
||||
-- given an Nginx variable i.e $request_uri
|
||||
-- it returns value of ngx.var[request_uri]
|
||||
function _M.lua_ngx_var(ngx_var)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue