Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leveraging LRU cache for ip-restriction plugin #1245

Merged
merged 1 commit into from
May 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions kong/plugins/ip-restriction/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ function IpRestrictionHandler:access(conf)
return responses.send_HTTP_FORBIDDEN("Cannot identify the client IP address, unix domain sockets are not supported.")
end

if conf._blacklist_cache and #conf._blacklist_cache > 0 then
block = iputils.ip_in_cidrs(remote_addr, conf._blacklist_cache)
if conf.blacklist and #conf.blacklist > 0 then
block = iputils.ip_in_cidrs(remote_addr, iputils.parse_cidrs(conf.blacklist))
end

if conf._whitelist_cache and #conf._whitelist_cache > 0 then
block = not iputils.ip_in_cidrs(remote_addr, conf._whitelist_cache)
if conf.whitelist and #conf.whitelist > 0 then
block = not iputils.ip_in_cidrs(remote_addr, iputils.parse_cidrs(conf.whitelist))
end

if block then
Expand Down
23 changes: 23 additions & 0 deletions kong/plugins/ip-restriction/migrations/cassandra.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
return {
{
name = "2016-05-24-remove-cache",
up = function(_, _, factory)
local plugins, err = factory.plugins:find_all {name = "ip-restriction"}
if err then
return err
end

for _, plugin in ipairs(plugins) do
plugin.config._whitelist_cache = nil
plugin.config._blacklist_cache = nil
local _, err = factory.plugins:update(plugin, plugin, {full = true})
if err then
return err
end
end
end,
down = function(_, _, factory)
-- Do nothing
end
}
}
23 changes: 23 additions & 0 deletions kong/plugins/ip-restriction/migrations/postgres.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
return {
{
name = "2016-05-24-remove-cache",
up = function(_, _, factory)
local plugins, err = factory.plugins:find_all {name = "ip-restriction"}
if err then
return err
end

for _, plugin in ipairs(plugins) do
plugin.config._whitelist_cache = nil
plugin.config._blacklist_cache = nil
local _, err = factory.plugins:update(plugin, plugin, {full = true})
if err then
return err
end
end
end,
down = function()
-- Do nothing
end
}
}
10 changes: 2 additions & 8 deletions kong/plugins/ip-restriction/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@ local iputils = require "resty.iputils"
local Errors = require "kong.dao.errors"

local function validate_ips(v, t, column)
local new_fields
if v and type(v) == "table" then
for _, ip in ipairs(v) do
local _, err = iputils.parse_cidr(ip)
if type(err) == "string" then -- It's an error only if the second variable is a string
return false, "cannot parse '"..ip.."': "..err
end
end
new_fields = {["_"..column.."_cache"] = iputils.parse_cidrs(v)}
end
return true, nil, new_fields
return true
end

return {
fields = {
whitelist = {type = "array", func = validate_ips},
blacklist = {type = "array", func = validate_ips},

-- Internal use
_whitelist_cache = {type = "array"},
_blacklist_cache = {type = "array"}
blacklist = {type = "array", func = validate_ips}
},
self_check = function(schema, plugin_t, dao, is_update)
local wl = type(plugin_t.whitelist) == "table" and plugin_t.whitelist or {}
Expand Down
14 changes: 0 additions & 14 deletions spec/plugins/ip-restriction/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,5 @@ describe("ip-restriction schema", function()
assert.True(ok)
assert.falsy(err)
end)
it("should build _whitelist_cache", function()
local t = {whitelist = {"127.0.0.1", "127.0.0.2"}}
local ok, err = v(t, schema)
assert.True(ok)
assert.falsy(err)
assert.is_table(t._whitelist_cache)
end)
it("should build _blacklist_cache", function()
local t = {blacklist = {"127.0.0.1", "127.0.0.2"}}
local ok, err = v(t, schema)
assert.True(ok)
assert.falsy(err)
assert.is_table(t._blacklist_cache)
end)
end)
end)