Skip to content

Commit

Permalink
feat(acl) allow acl plugin to use a consumer without credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske authored and thibaultcha committed Jul 26, 2017
1 parent cc4ffc3 commit 8fe391a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
22 changes: 18 additions & 4 deletions kong/plugins/acl/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,24 @@ function ACLHandler:access(conf)
ACLHandler.super.access(self)

local consumer_id
if ngx.ctx.authenticated_credential then
consumer_id = ngx.ctx.authenticated_credential.consumer_id
else
return responses.send_HTTP_FORBIDDEN("Cannot identify the consumer, add an authentication plugin to use the ACL plugin")
local ctx = ngx.ctx

local authenticated_consumer = ctx.authenticated_consumer
if authenticated_consumer then
consumer_id = authenticated_consumer.id
end

if not consumer_id then
local authenticated_credential = ctx.authenticated_credential
if authenticated_credential then
consumer_id = authenticated_credential.consumer_id
end
end

if not consumer_id then
return responses.send_HTTP_FORBIDDEN(
"Cannot identify the consumer, add an authentication plugin to use the ACL plugin"
)
end

-- Retrieve ACL
Expand Down
61 changes: 61 additions & 0 deletions spec/03-plugins/19-acl/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ describe("Plugin: ACL (access)", function()
consumer_id = consumer4.id
})

local anonymous = assert(helpers.dao.consumers:insert {
username = "anonymous"
})

assert(helpers.dao.acls:insert {
group = "anonymous",
consumer_id = anonymous.id
})

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "acl1.com" },
Expand Down Expand Up @@ -179,6 +188,28 @@ describe("Plugin: ACL (access)", function()
config = {}
})

local api8 = assert(helpers.dao.apis:insert {
name = "api-8",
hosts = { "acl8.com" },
upstream_url = "http://mockbin.com"
})

assert(helpers.dao.plugins:insert {
name = "acl",
api_id = api8.id,
config = {
whitelist = {"anonymous"}
}
})

assert(helpers.dao.plugins:insert {
name = "key-auth",
api_id = api8.id,
config = {
anonymous = anonymous.id,
}
})

assert(helpers.start_kong())
end)

Expand All @@ -196,6 +227,36 @@ describe("Plugin: ACL (access)", function()
helpers.stop_kong()
end)


describe("Mapping to Consumer", function()
it("should work with consumer with credentials", function()
local res = assert(client:send {
method = "GET",
path = "/request?apikey=apikey124",
headers = {
["Host"] = "acl2.com"
}
})

local body = cjson.decode(assert.res_status(200, res))
assert.equal("admin", body.headers["x-consumer-groups"])
end)

it("should work with consumer without credentials", function()
local res = assert(client:send {
method = "GET",
path = "/request",
headers = {
["Host"] = "acl8.com"
}
})

local body = cjson.decode(assert.res_status(200, res))
assert.equal("anonymous", body.headers["x-consumer-groups"])
end)
end)


describe("Simple lists", function()
it("should fail when an authentication plugin is missing", function()
local res = assert(client:send {
Expand Down

0 comments on commit 8fe391a

Please sign in to comment.