Skip to content

Commit

Permalink
Merge pull request #3766 from Kong/feat/new-db-plugins-daos
Browse files Browse the repository at this point in the history
feat(*) port plugins entities and Admin endpoints to the new DAO
  • Loading branch information
thibaultcha authored Sep 14, 2018
2 parents 6e1d48c + 1a0924e commit 99c81b4
Show file tree
Hide file tree
Showing 85 changed files with 1,734 additions and 2,279 deletions.
2 changes: 2 additions & 0 deletions kong-0.14.1-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ build = {
["kong.plugins.basic-auth.schema"] = "kong/plugins/basic-auth/schema.lua",
["kong.plugins.basic-auth.api"] = "kong/plugins/basic-auth/api.lua",
["kong.plugins.basic-auth.daos"] = "kong/plugins/basic-auth/daos.lua",
["kong.plugins.basic-auth.basicauth_credentials"] = "kong/plugins/basic-auth/basicauth_credentials.lua",

["kong.plugins.key-auth.migrations.cassandra"] = "kong/plugins/key-auth/migrations/cassandra.lua",
["kong.plugins.key-auth.migrations.postgres"] = "kong/plugins/key-auth/migrations/postgres.lua",
Expand Down Expand Up @@ -281,6 +282,7 @@ build = {
["kong.plugins.acl.api"] = "kong/plugins/acl/api.lua",
["kong.plugins.acl.daos"] = "kong/plugins/acl/daos.lua",
["kong.plugins.acl.groups"] = "kong/plugins/acl/groups.lua",
["kong.plugins.acl.acls"] = "kong/plugins/acl/acls.lua",

["kong.plugins.correlation-id.handler"] = "kong/plugins/correlation-id/handler.lua",
["kong.plugins.correlation-id.schema"] = "kong/plugins/correlation-id/schema.lua",
Expand Down
15 changes: 14 additions & 1 deletion kong/api/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,27 @@ do
end


local function is_new_db_routes(mod)
for _, verbs in pairs(mod) do
if type(verbs) == "table" then -- ignore "before" functions
return verbs.schema
end
end
end


-- Loading plugins routes
if singletons.configuration and singletons.configuration.loaded_plugins then
for k in pairs(singletons.configuration.loaded_plugins) do
local loaded, mod = utils.load_module_if_exists("kong.plugins." .. k .. ".api")

if loaded then
ngx.log(ngx.DEBUG, "Loading API endpoints for plugin: ", k)
attach_routes(mod)
if is_new_db_routes(mod) then
attach_new_db_routes(mod)
else
attach_routes(mod)
end

else
ngx.log(ngx.DEBUG, "No API endpoints loaded for plugin: ", k)
Expand Down
8 changes: 6 additions & 2 deletions kong/dao/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ function _M.new(kong_config, new_db)
end
else
for k, v in pairs(plugin_schemas) do
schemas[k] = v
if not v.name then
schemas[k] = v
end
end
end
end
Expand Down Expand Up @@ -385,7 +387,9 @@ end

function _M:truncate_tables()
for _, dao in pairs(self.daos) do
self.db:truncate_table(dao.table)
if dao.table then
self.db:truncate_table(dao.table)
end
end

if self.db.additional_tables then
Expand Down
27 changes: 17 additions & 10 deletions kong/db/dao/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,19 @@ local function check_update(self, key, entity, options, name)
self.schema:process_auto_fields(entity, "update")
if not entity_to_update then
local err_t = self.errors:schema_violation(err)
return nil, tostring(err_t), err_t
return nil, nil, tostring(err_t), err_t
end

local rbw_entity
if read_before_write then
local rbw_entity, err, err_t
local err, err_t
if name then
rbw_entity, err, err_t = self.strategy:select_by_field(name, key, options)
else
rbw_entity, err, err_t = self.strategy:select(key, options)
end
if not rbw_entity then
return nil, err, err_t
return nil, nil, err, err_t
end

entity_to_update = self.schema:merge_values(entity_to_update, rbw_entity)
Expand All @@ -176,22 +177,22 @@ local function check_update(self, key, entity, options, name)
local ok, errors = self.schema:validate_update(entity_to_update)
if not ok then
local err_t = self.errors:schema_violation(errors)
return nil, tostring(err_t), err_t
return nil, nil, tostring(err_t), err_t
end

if options ~= nil then
ok, errors = validate_options_value(options, self.schema, "update")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
return nil, nil, tostring(err_t), err_t
end
end

if self.schema.cache_key and #self.schema.cache_key > 1 then
entity_to_update.cache_key = self:cache_key(entity_to_update)
end

return entity_to_update
return entity_to_update, rbw_entity
end


Expand Down Expand Up @@ -381,8 +382,8 @@ local function generate_foreign_key_methods(schema)
return nil, tostring(err_t), err_t
end

local entity_to_update, err, err_t = check_update(self, unique_value,
entity, options, name)
local entity_to_update, rbw_entity, err, err_t = check_update(self, unique_value,
entity, options, name)
if not entity_to_update then
return nil, err, err_t
end
Expand All @@ -398,6 +399,9 @@ local function generate_foreign_key_methods(schema)
return nil, err, err_t
end

if rbw_entity then
self:post_crud_event("update", rbw_entity)
end
self:post_crud_event("update", row)

return row
Expand Down Expand Up @@ -732,8 +736,8 @@ function DAO:update(primary_key, entity, options)
return nil, tostring(err_t), err_t
end

local entity_to_update, err, err_t = check_update(self, primary_key, entity,
options)
local entity_to_update, rbw_entity, err, err_t = check_update(self, primary_key, entity,
options)
if not entity_to_update then
return nil, err, err_t
end
Expand All @@ -748,6 +752,9 @@ function DAO:update(primary_key, entity, options)
return nil, err, err_t
end

if rbw_entity then
self:post_crud_event("update", rbw_entity)
end
self:post_crud_event("update", row)

return row
Expand Down
38 changes: 38 additions & 0 deletions kong/db/dao/plugins.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local constants = require "kong.constants"
local typedefs = require "kong.db.schema.typedefs"
local utils = require "kong.tools.utils"
local Entity = require "kong.db.schema.entity"
local DAO = require "kong.db.dao"
local MetaSchema = require "kong.db.schema.metaschema"


local Plugins = {}
Expand Down Expand Up @@ -201,6 +204,7 @@ end
-- @return an array of tables, or nil and an error message.
function Plugins:load_plugin_schemas(plugin_set)
local plugin_list = {}
local db = self.db

-- load installed plugins
for plugin in pairs(plugin_set) do
Expand Down Expand Up @@ -257,6 +261,40 @@ function Plugins:load_plugin_schemas(plugin_set)
name = plugin,
handler = handler(),
}

if db.strategy then -- skip during tests
local has_daos, daos_schemas = utils.load_module_if_exists("kong.plugins." .. plugin .. ".daos")
if has_daos then
local Strategy = require(string.format("kong.db.strategies.%s", db.strategy))
for name, schema_def in pairs(daos_schemas) do
if name ~= "tables" and schema_def.name then
ngx_log(ngx_DEBUG, string.format("Loading custom plugin entity: '%s.%s'", plugin, name))
local ok, err_t = MetaSchema:validate(schema_def)
if not ok then
return nil, string.format("schema of custom plugin entity '%s.%s' is invalid: %s", plugin, name,
-- tostring(db.errors:schema_violation(err_t)))
require("inspect")({ err_t = err_t, schema_def = schema_def }))
end
local schema, err = Entity.new(schema_def)
if not schema then
return nil, string.format("schema of custom plugin entity '%s.%s' is invalid: %s", plugin, name,
err)
end
local strategy, err = Strategy.new(db.connector, schema, db.errors)
if not strategy then
return nil, err
end
db.strategies[schema.name] = strategy

local dao, err = DAO.new(db, schema, strategy, db.errors)
if not dao then
return nil, err
end
db.daos[schema.name] = dao
end
end
end
end
end

return plugin_list
Expand Down
1 change: 1 addition & 0 deletions kong/db/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function DB.new(kong_config, strategy)
strategies = strategies,
connector = connector,
strategy = strategy,
errors = errors,
}

do
Expand Down
26 changes: 14 additions & 12 deletions kong/db/schema/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,7 @@ function Schema:process_auto_fields(input, context, nulls)
local now_s = ngx_time()
local now_ms = ngx_now()
local read_before_write = false
local setting_cache_key = 0
local cache_key_len = self.cache_key and #self.cache_key
local cache_key_modified = false

for key, field in self:each_field(input) do

Expand All @@ -1235,6 +1234,11 @@ function Schema:process_auto_fields(input, context, nulls)
elseif field.type == "integer" then
output[key] = now_s
end

elseif field.type == "string" and output[key] == nil
and (context == "insert" or
context == "upsert") then
output[key] = utils.random_string()
end
end

Expand All @@ -1245,8 +1249,8 @@ function Schema:process_auto_fields(input, context, nulls)
end

if output[key] ~= nil then
if cache_key_len and self.cache_key_set[key] then
setting_cache_key = setting_cache_key + 1
if self.cache_key_set and self.cache_key_set[key] then
cache_key_modified = true
end
end

Expand All @@ -1255,19 +1259,17 @@ function Schema:process_auto_fields(input, context, nulls)
end
end

if context == "update" then
if context == "update" and (
-- If a partial update does not provide the subschema key,
-- we need to do a read-before-write to get it and be
-- able to properly validate the entity.
if self.subschema_key and input[self.subschema_key] == nil then
read_before_write = true

-- If we're partially resetting the value of a composite cache key,
(self.subschema_key and input[self.subschema_key] == nil)
-- If we're resetting the value of a composite cache key,
-- we to do a read-before-write to get the rest of the cache key
-- and be able to properly update it.
elseif setting_cache_key > 0 and cache_key_len ~= setting_cache_key then
read_before_write = true
end
or cache_key_modified)
then
read_before_write = true
end

return output, nil, read_before_write
Expand Down
29 changes: 29 additions & 0 deletions kong/plugins/acl/acls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local singletons = require "kong.singletons"


local invalidate_cache = function(self, entity)
local consumer = entity.consumer
if type(consumer) ~= "table" then
return true
end
-- skip next lines in some tests where singletons is not available
if not singletons.cache then
return true
end

local cache_key = self:cache_key(consumer.id)
return singletons.cache:invalidate(cache_key)
end


local _ACLs = {}

function _ACLs:post_crud_event(operation, entity)
local _, err, err_t = invalidate_cache(self, entity)
if err then
return nil, err, err_t
end
return self.super.post_crud_event(self, entity)
end

return _ACLs
Loading

0 comments on commit 99c81b4

Please sign in to comment.