Skip to content

Commit

Permalink
Appends X-Consumer-Groups header when ACL is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Apr 13, 2016
1 parent 8df8a5e commit 0e1bd58
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion kong/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ return {
CONSUMER_USERNAME = "X-Consumer-Username",
CREDENTIAL_USERNAME = "X-Credential-Username",
RATELIMIT_LIMIT = "X-RateLimit-Limit",
RATELIMIT_REMAINING = "X-RateLimit-Remaining"
RATELIMIT_REMAINING = "X-RateLimit-Remaining",
CONSUMER_GROUPS = "X-Consumer-Groups"
},
AUTHENTICATION = {
QUERY = "query",
Expand Down
12 changes: 12 additions & 0 deletions kong/plugins/acl/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ local BasePlugin = require "kong.plugins.base_plugin"
local cache = require "kong.tools.database_cache"
local responses = require "kong.tools.responses"
local utils = require "kong.tools.utils"
local constants = require "kong.constants"

local table_insert = table.insert
local table_concat = table.concat
local ipairs = ipairs

local ACLHandler = BasePlugin:extend()

Expand Down Expand Up @@ -62,6 +67,13 @@ function ACLHandler:access(conf)
if block then
return responses.send_HTTP_FORBIDDEN("You cannot consume this service")
end

-- Prepare header
local str_acls = {}
for _, v in ipairs(acls) do
table_insert(str_acls, v.group)
end
ngx.req.set_header(constants.HEADERS.CONSUMER_GROUPS, table_concat(str_acls, ", "))
end

return ACLHandler
8 changes: 6 additions & 2 deletions spec/plugins/acl/access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ describe("ACL Plugin", function()
end)

it("should work when in whitelist", function()
local _, status = http_client.get(STUB_GET_URL, {apikey = "apikey124"}, {host = "acl2.com"})
local response, status = http_client.get(STUB_GET_URL, {apikey = "apikey124"}, {host = "acl2.com"})
assert.equal(200, status)
local body = cjson.decode(response)
assert.equal("admin", body.headers["x-consumer-groups"])
end)

it("should work when not in blacklist", function()
Expand All @@ -98,8 +100,10 @@ describe("ACL Plugin", function()

describe("Multi lists", function()
it("should work when in whitelist", function()
local _, status = http_client.get(STUB_GET_URL, {apikey = "apikey125"}, {host = "acl4.com"})
local response, status = http_client.get(STUB_GET_URL, {apikey = "apikey125"}, {host = "acl4.com"})
assert.equal(200, status)
local body = cjson.decode(response)
assert.truthy(body.headers["x-consumer-groups"] == "pro, hello" or body.headers["x-consumer-groups"] == "hello, pro")
end)

it("should fail when not in whitelist", function()
Expand Down

0 comments on commit 0e1bd58

Please sign in to comment.