From 187b946f95d3f682f877c007dd0188d81c56d6f8 Mon Sep 17 00:00:00 2001 From: Wangchong Zhou Date: Wed, 2 Jun 2021 23:18:10 +0800 Subject: [PATCH 1/3] fix(dc) exclude cases where "plugins" are used not as plugins entities Fix #7610 --- kong/db/schema/others/declarative_config.lua | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/kong/db/schema/others/declarative_config.lua b/kong/db/schema/others/declarative_config.lua index e04fe87144b..afbb0995721 100644 --- a/kong/db/schema/others/declarative_config.lua +++ b/kong/db/schema/others/declarative_config.lua @@ -243,14 +243,12 @@ local function load_plugin_subschemas(fields, plugin_set, indent) for _, f in ipairs(fields) do local fname, fdata = next(f) - if fname == "plugins" then + -- Exclude cases where `plugins` are used expect from plugins entities. + -- This assumes other entities doesn't have `name` as its subschema_key. + if fname == "plugins" and fdata.elements and fdata.elements.subschema_key == "name" then for plugin in pairs(plugin_set) do - local _, err, err_t = plugin_loader.load_subschema(fdata.elements, plugin, errors) + local _, err = plugin_loader.load_subschema(fdata.elements, plugin, errors) - if err_t then - return nil, "schema for plugin '" .. plugin .. "' is invalid: " .. - tostring(errors:schema_violation(err_t)) - end if err then return nil, err end @@ -735,10 +733,10 @@ function DeclarativeConfig.load(plugin_set, include_foreign) end for plugin in pairs(plugin_set) do - local entities, err, err_t = plugin_loader.load_entities(plugin, errors, + local entities, err = plugin_loader.load_entities(plugin, errors, plugin_loader.load_entity_schema) - if err or err_t then - return nil, err, err_t + if err then + return nil, err end for entity, schema in pairs(entities) do all_schemas[entity] = schema From 28004c7c2544034a64bb930631aea73c9f9534f4 Mon Sep 17 00:00:00 2001 From: Wangchong Zhou Date: Thu, 3 Jun 2021 00:37:36 +0800 Subject: [PATCH 2/3] add tests --- .../01-validate_spec.lua | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua b/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua index aab2e8467b3..76346163709 100644 --- a/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua +++ b/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua @@ -851,3 +851,63 @@ describe("declarative config: validate", function() end) end) end) + +describe("declarative config: validate", function() + + local daos = { + ["dao-keywords"] = { + name = "dao-keywords", + primary_key = {"field1"}, + admin_api_name = "dao-keywords", + admin_api_nested_name = "dao-keywords", + fields = { + {field1 = {type = "string", required = true}}, + {field2 = {type = "string", required = true}}, + } + } + } + + local plugins_set = helpers.test_conf.loaded_plugins + plugins_set["dao-keywords"] = true + + lazy_setup(function() + package.loaded["kong.plugins.dao-keywords.schema"] = { + name = "dao-keywords", + fields = { + { config = { + type = "record", + fields = {}, + }, }, + } + } + + package.loaded["kong.plugins.dao-keywords.daos"] = daos + end) + + lazy_teardown(function() + package.loaded["kong.plugins.dao-keywords.schema"] = nil + package.loaded["kong.plugins.dao-keywords.daos"] = nil + end) + + it("loads plugins with custom DAO that has keywords as string", function() + daos["dao-keywords"]["fields"][2] = {plugins = {type = "string", required = true}} + + local _ + DeclarativeConfig, _, DeclarativeConfig_def = assert(declarative_config.load(plugins_set)) + end) + + it("loads plugins with custom DAO that has keywords as array", function() + daos["dao-keywords"]["fields"][2] = { + plugins = { + type = "array", + required = false, + elements = { + type = "string", + }, + }, + } + + local _ + DeclarativeConfig, _, DeclarativeConfig_def = assert(declarative_config.load(plugins_set)) + end) +end) From 31d5c1c633afd7c51e93d742f0e1b6bd8f707840 Mon Sep 17 00:00:00 2001 From: Wangchong Zhou Date: Thu, 3 Jun 2021 00:42:45 +0800 Subject: [PATCH 3/3] lint --- .../01-schema/11-declarative_config/01-validate_spec.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua b/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua index 76346163709..b65e1139646 100644 --- a/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua +++ b/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua @@ -892,8 +892,7 @@ describe("declarative config: validate", function() it("loads plugins with custom DAO that has keywords as string", function() daos["dao-keywords"]["fields"][2] = {plugins = {type = "string", required = true}} - local _ - DeclarativeConfig, _, DeclarativeConfig_def = assert(declarative_config.load(plugins_set)) + assert(declarative_config.load(plugins_set)) end) it("loads plugins with custom DAO that has keywords as array", function() @@ -907,7 +906,6 @@ describe("declarative config: validate", function() }, } - local _ - DeclarativeConfig, _, DeclarativeConfig_def = assert(declarative_config.load(plugins_set)) + assert(declarative_config.load(plugins_set)) end) end)