Skip to content

Commit

Permalink
refactor(plugins) integrates handler files
Browse files Browse the repository at this point in the history
Removes smaller code files by integrating them into the `handler` files.
Reducing overall file clutter

Signed-off-by: Thibault Charbonnier <[email protected]>
  • Loading branch information
Tieske authored and thibaultcha committed Jan 18, 2016
1 parent 78e56dd commit 6d5184a
Show file tree
Hide file tree
Showing 34 changed files with 871 additions and 1,042 deletions.
42 changes: 9 additions & 33 deletions kong-0.5.4-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ build = {
["kong.dao.cassandra.consumers"] = "kong/dao/cassandra/consumers.lua",
["kong.dao.cassandra.plugins"] = "kong/dao/cassandra/plugins.lua",

["kong.api.app"] = "kong/api/app.lua",
["kong.api.crud_helpers"] = "kong/api/crud_helpers.lua",
["kong.api.route_helpers"] = "kong/api/route_helpers.lua",
["kong.api.routes.kong"] = "kong/api/routes/kong.lua",
["kong.api.routes.apis"] = "kong/api/routes/apis.lua",
["kong.api.routes.consumers"] = "kong/api/routes/consumers.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",

["kong.plugins.base_plugin"] = "kong/plugins/base_plugin.lua",

["kong.plugins.basic-auth.schema.migrations"] = "kong/plugins/basic-auth/schema/migrations.lua",
Expand All @@ -107,7 +116,6 @@ build = {

["kong.plugins.key-auth.schema.migrations"] = "kong/plugins/key-auth/schema/migrations.lua",
["kong.plugins.key-auth.handler"] = "kong/plugins/key-auth/handler.lua",
["kong.plugins.key-auth.access"] = "kong/plugins/key-auth/access.lua",
["kong.plugins.key-auth.schema"] = "kong/plugins/key-auth/schema.lua",
["kong.plugins.key-auth.api"] = "kong/plugins/key-auth/api.lua",
["kong.plugins.key-auth.daos"] = "kong/plugins/key-auth/daos.lua",
Expand All @@ -123,29 +131,23 @@ build = {
["kong.plugins.log-serializers.alf"] = "kong/plugins/log-serializers/alf.lua",

["kong.plugins.tcp-log.handler"] = "kong/plugins/tcp-log/handler.lua",
["kong.plugins.tcp-log.log"] = "kong/plugins/tcp-log/log.lua",
["kong.plugins.tcp-log.schema"] = "kong/plugins/tcp-log/schema.lua",

["kong.plugins.udp-log.handler"] = "kong/plugins/udp-log/handler.lua",
["kong.plugins.udp-log.log"] = "kong/plugins/udp-log/log.lua",
["kong.plugins.udp-log.schema"] = "kong/plugins/udp-log/schema.lua",

["kong.plugins.http-log.handler"] = "kong/plugins/http-log/handler.lua",
["kong.plugins.http-log.log"] = "kong/plugins/http-log/log.lua",
["kong.plugins.http-log.schema"] = "kong/plugins/http-log/schema.lua",

["kong.plugins.file-log.handler"] = "kong/plugins/file-log/handler.lua",
["kong.plugins.file-log.schema"] = "kong/plugins/file-log/schema.lua",
["kong.plugins.file-log.log"] = "kong/plugins/file-log/log.lua",
["kong.plugins.file-log.fd_util"] = "kong/plugins/file-log/fd_util.lua",

["kong.plugins.mashape-analytics.handler"] = "kong/plugins/mashape-analytics/handler.lua",
["kong.plugins.mashape-analytics.schema"] = "kong/plugins/mashape-analytics/schema.lua",
["kong.plugins.mashape-analytics.buffer"] = "kong/plugins/mashape-analytics/buffer.lua",

["kong.plugins.rate-limiting.schema.migrations"] = "kong/plugins/rate-limiting/schema/migrations.lua",
["kong.plugins.rate-limiting.handler"] = "kong/plugins/rate-limiting/handler.lua",
["kong.plugins.rate-limiting.access"] = "kong/plugins/rate-limiting/access.lua",
["kong.plugins.rate-limiting.schema"] = "kong/plugins/rate-limiting/schema.lua",
["kong.plugins.rate-limiting.daos"] = "kong/plugins/rate-limiting/daos.lua",

Expand All @@ -158,7 +160,6 @@ build = {
["kong.plugins.response-ratelimiting.daos"] = "kong/plugins/response-ratelimiting/daos.lua",

["kong.plugins.request-size-limiting.handler"] = "kong/plugins/request-size-limiting/handler.lua",
["kong.plugins.request-size-limiting.access"] = "kong/plugins/request-size-limiting/access.lua",
["kong.plugins.request-size-limiting.schema"] = "kong/plugins/request-size-limiting/schema.lua",

["kong.plugins.request-transformer.handler"] = "kong/plugins/request-transformer/handler.lua",
Expand All @@ -171,46 +172,23 @@ build = {
["kong.plugins.response-transformer.schema"] = "kong/plugins/response-transformer/schema.lua",

["kong.plugins.cors.handler"] = "kong/plugins/cors/handler.lua",
["kong.plugins.cors.access"] = "kong/plugins/cors/access.lua",
["kong.plugins.cors.schema"] = "kong/plugins/cors/schema.lua",

["kong.plugins.ssl.handler"] = "kong/plugins/ssl/handler.lua",
["kong.plugins.ssl.certificate"] = "kong/plugins/ssl/certificate.lua",
["kong.plugins.ssl.access"] = "kong/plugins/ssl/access.lua",
["kong.plugins.ssl.ssl_util"] = "kong/plugins/ssl/ssl_util.lua",
["kong.plugins.ssl.schema"] = "kong/plugins/ssl/schema.lua",

["kong.plugins.ip-restriction.handler"] = "kong/plugins/ip-restriction/handler.lua",
["kong.plugins.ip-restriction.init_worker"] = "kong/plugins/ip-restriction/init_worker.lua",
["kong.plugins.ip-restriction.access"] = "kong/plugins/ip-restriction/access.lua",
["kong.plugins.ip-restriction.schema"] = "kong/plugins/ip-restriction/schema.lua",

["kong.plugins.acl.schema.migrations"] = "kong/plugins/acl/schema/migrations.lua",
["kong.plugins.acl.handler"] = "kong/plugins/acl/handler.lua",
["kong.plugins.acl.access"] = "kong/plugins/acl/access.lua",
["kong.plugins.acl.schema"] = "kong/plugins/acl/schema.lua",
["kong.plugins.acl.api"] = "kong/plugins/acl/api.lua",
["kong.plugins.acl.daos"] = "kong/plugins/acl/daos.lua",

["kong.plugins.acl.schema.migrations"] = "kong/plugins/acl/schema/migrations.lua",
["kong.plugins.acl.handler"] = "kong/plugins/acl/handler.lua",
["kong.plugins.acl.access"] = "kong/plugins/acl/access.lua",
["kong.plugins.acl.schema"] = "kong/plugins/acl/schema.lua",
["kong.plugins.acl.api"] = "kong/plugins/acl/api.lua",
["kong.plugins.acl.daos"] = "kong/plugins/acl/daos.lua",

["kong.api.app"] = "kong/api/app.lua",
["kong.api.crud_helpers"] = "kong/api/crud_helpers.lua",
["kong.api.route_helpers"] = "kong/api/route_helpers.lua",
["kong.api.routes.kong"] = "kong/api/routes/kong.lua",
["kong.api.routes.apis"] = "kong/api/routes/apis.lua",
["kong.api.routes.consumers"] = "kong/api/routes/consumers.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",

["kong.plugins.jwt.schema.migrations"] = "kong/plugins/jwt/schema/migrations.lua",
["kong.plugins.jwt.handler"] = "kong/plugins/jwt/handler.lua",
["kong.plugins.jwt.access"] = "kong/plugins/jwt/access.lua",
["kong.plugins.jwt.schema"] = "kong/plugins/jwt/schema.lua",
["kong.plugins.jwt.api"] = "kong/plugins/jwt/api.lua",
["kong.plugins.jwt.daos"] = "kong/plugins/jwt/daos.lua",
Expand All @@ -224,11 +202,9 @@ build = {
["kong.plugins.hmac-auth.daos"] = "kong/plugins/hmac-auth/daos.lua",

["kong.plugins.syslog.handler"] = "kong/plugins/syslog/handler.lua",
["kong.plugins.syslog.log"] = "kong/plugins/syslog/log.lua",
["kong.plugins.syslog.schema"] = "kong/plugins/syslog/schema.lua",

["kong.plugins.loggly.handler"] = "kong/plugins/loggly/handler.lua",
["kong.plugins.loggly.log"] = "kong/plugins/loggly/log.lua",
["kong.plugins.loggly.schema"] = "kong/plugins/loggly/schema.lua",

["kong.plugins.datadog.handler"] = "kong/plugins/datadog/handler.lua",
Expand Down
58 changes: 0 additions & 58 deletions kong/plugins/acl/access.lua

This file was deleted.

57 changes: 53 additions & 4 deletions kong/plugins/acl/handler.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
local BasePlugin = require "kong.plugins.base_plugin"
local access = require "kong.plugins.acl.access"
local cache = require "kong.tools.database_cache"
local responses = require "kong.tools.responses"
local utils = require "kong.tools.utils"

local ACLHandler = BasePlugin:extend()

ACLHandler.PRIORITY = 950

function ACLHandler:new()
ACLHandler.super.new(self, "acl")
end

function ACLHandler:access(conf)
ACLHandler.super.access(self)
access.execute(conf)
end

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")
end

ACLHandler.PRIORITY = 950
-- Retrieve ACL
local acls = cache.get_or_set(cache.acls_key(consumer_id), function()
local results, err = dao.acls:find_by_keys({consumer_id = consumer_id})
if err then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
end
return results
end)

if not acls then acls = {} end

local block

if utils.table_size(conf.blacklist) > 0 and utils.table_size(acls) > 0 then
for _, v in ipairs(acls) do
if utils.table_contains(conf.blacklist, v.group) then
block = true
break
end
end
end

if utils.table_size(conf.whitelist) > 0 then
if utils.table_size(acls) == 0 then
block = true
else
local contains
for _, v in ipairs(acls) do
if utils.table_contains(conf.whitelist, v.group) then
contains = true
break
end
end
if not contains then block = true end
end
end

if block then
return responses.send_HTTP_FORBIDDEN("You cannot consume this service")
end
end

return ACLHandler
66 changes: 0 additions & 66 deletions kong/plugins/cors/access.lua

This file was deleted.

66 changes: 62 additions & 4 deletions kong/plugins/cors/handler.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,75 @@
local BasePlugin = require "kong.plugins.base_plugin"
local access = require "kong.plugins.cors.access"
local responses = require "kong.tools.responses"

local CorsHandler = BasePlugin:extend()

CorsHandler.PRIORITY = 2000


local function configure_origin(ngx, conf)
if conf.origin == nil then
ngx.header["Access-Control-Allow-Origin"] = "*"
else
ngx.header["Access-Control-Allow-Origin"] = conf.origin
ngx.header["Vary"] = "Origin"
end
end

local function configure_credentials(ngx, conf)
if (conf.credentials) then
ngx.header["Access-Control-Allow-Credentials"] = "true"
end
end

local function configure_headers(ngx, conf, headers)
if conf.headers == nil then
ngx.header["Access-Control-Allow-Headers"] = headers["access-control-request-headers"] or ""
else
ngx.header["Access-Control-Allow-Headers"] = table.concat(conf.headers, ",")
end
end

local function configure_exposed_headers(ngx, conf)
if conf.exposed_headers ~= nil then
ngx.header["Access-Control-Expose-Headers"] = table.concat(conf.exposed_headers, ",")
end
end

local function configure_methods(ngx, conf)
if conf.methods == nil then
ngx.header["Access-Control-Allow-Methods"] = "GET,HEAD,PUT,PATCH,POST,DELETE"
else
ngx.header["Access-Control-Allow-Methods"] = table.concat(conf.methods, ",")
end
end

local function configure_max_age(ngx, conf)
if conf.max_age ~= nil then
ngx.header["Access-Control-Max-Age"] = tostring(conf.max_age)
end
end

function CorsHandler:new()
CorsHandler.super.new(self, "cors")
end

function CorsHandler:access(conf)
CorsHandler.super.access(self)
access.execute(conf)
end
configure_origin(ngx, conf)
configure_credentials(ngx, conf)

CorsHandler.PRIORITY = 2000
if ngx.req.get_method() == "OPTIONS" then -- Preflight request
configure_headers(ngx, conf, ngx.req.get_headers())
configure_methods(ngx, conf)
configure_max_age(ngx, conf)

if not conf.preflight_continue then -- Check if the preflight request should end here, or be proxied
return responses.send_HTTP_NO_CONTENT()
end

else
configure_exposed_headers(ngx, conf)
end
end

return CorsHandler
Loading

0 comments on commit 6d5184a

Please sign in to comment.