forked from p0pr0ck5/lua-resty-waf
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for differing persistent storage engines
Refactor lib.storage to implement a polymorphic interface for collection initialization and persistence. Initial backends include ngx.shared and memcached, which both implement the same pattern with respect to collection serialization and expiry handling.
- Loading branch information
Showing
14 changed files
with
1,311 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
local _M = {} | ||
|
||
_M.version = "0.7.2" | ||
|
||
local cjson = require("cjson") | ||
local logger = require("lib.log") | ||
|
||
function _M.initialize(waf, storage, col) | ||
if (not waf._storage_zone) then | ||
return | ||
end | ||
|
||
local altered, serialized, shm | ||
shm = ngx.shared[waf._storage_zone] | ||
serialized = shm:get(col) | ||
altered = false | ||
|
||
if (not serialized) then | ||
logger.log(waf, "Initializing an empty collection for " .. col) | ||
storage[col] = {} | ||
else | ||
local data = cjson.decode(serialized) | ||
|
||
-- because we're serializing out the contents of the collection | ||
-- we need to roll our own expire handling. lua_shared_dict's | ||
-- internal expiry can't act on individual collection elements | ||
for key in pairs(data) do | ||
if (not key:find("__", 1, true) and data["__expire_" .. key]) then | ||
logger.log(waf, "checking " .. key) | ||
if (data["__expire_" .. key] < ngx.time()) then | ||
logger.log(waf, "Removing expired key: " .. key) | ||
data["__expire_" .. key] = nil | ||
data[key] = nil | ||
altered = true | ||
end | ||
end | ||
end | ||
|
||
storage[col] = data | ||
end | ||
|
||
storage[col]["__altered"] = altered | ||
end | ||
|
||
function _M.persist(waf, col, data) | ||
if (not waf._storage_zone) then | ||
return | ||
end | ||
|
||
local shm = ngx.shared[waf._storage_zone] | ||
local serialized = cjson.encode(data) | ||
logger.log(waf, 'Persisting value: ' .. tostring(serialized)) | ||
shm:set(col, serialized) | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
local _M = {} | ||
|
||
_M.version = "0.7.2" | ||
|
||
local cjson = require("cjson") | ||
local logger = require("lib.log") | ||
local memcached_m = require("resty.memcached") | ||
|
||
function _M.initialize(waf, storage, col) | ||
local memcached = memcached_m:new() | ||
local host = waf._storage_memcached_host | ||
local port = waf._storage_memcached_port | ||
|
||
local ok, err = memcached:connect(host, port) | ||
if (not ok) then | ||
logger.log(waf, "Error in connecting to memcached: " .. err) | ||
storage[col] = {} | ||
return | ||
end | ||
|
||
local serialized, flags, err = memcached:get(col) | ||
if (err) then | ||
logger.log(waf, "Error retrieving " .. col .. ": " .. err) | ||
storage[col] = {} | ||
return | ||
end | ||
|
||
local ok, err = memcached:set_keepalive(10000, 100) | ||
if (not ok) then | ||
logger.log(waf, "Error setting memcached keepalive: " .. err) | ||
end | ||
|
||
local altered = false | ||
|
||
if (not serialized) then | ||
logger.log(waf, "Initializing an empty collection for " .. col) | ||
storage[col] = {} | ||
else | ||
local data = cjson.decode(serialized) | ||
|
||
-- because we're serializing out the contents of the collection | ||
-- we need to roll our own expire handling | ||
for key in pairs(data) do | ||
if (not key:find("__", 1, true) and data["__expire_" .. key]) then | ||
logger.log(waf, "checking " .. key) | ||
if (data["__expire_" .. key] < ngx.time()) then | ||
logger.log(waf, "Removing expired key: " .. key) | ||
data["__expire_" .. key] = nil | ||
data[key] = nil | ||
altered = true | ||
end | ||
end | ||
end | ||
|
||
storage[col] = data | ||
end | ||
|
||
storage[col]["__altered"] = altered | ||
end | ||
|
||
function _M.persist(waf, col, data) | ||
local serialized = cjson.encode(data) | ||
logger.log(waf, 'Persisting value: ' .. tostring(serialized)) | ||
|
||
local memcached = memcached_m:new() | ||
local host = waf._storage_memcached_host | ||
local port = waf._storage_memcached_port | ||
|
||
local ok, err = memcached:connect(host, port) | ||
if (not ok) then | ||
logger.log(waf, "Error in connecting to memcached: " .. err) | ||
return | ||
end | ||
|
||
local ok, err = memcached:set(col, serialized) | ||
if (not ok) then | ||
logger.log(waf, "Error persisting storage data: " .. err) | ||
end | ||
|
||
local ok, err = memcached:set_keepalive(10000, 100) | ||
if (not ok) then | ||
logger.log(waf, "Error setting memcached keepalive: " .. err) | ||
end | ||
end | ||
|
||
|
||
return _M |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.