Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(declarative): generate correct uuid for transient entities #14082

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/fix-declarative-config-load.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "Fixed an issue where a valid declarative config with certificate/sni entities cannot be loaded in dbless mode"
type: bugfix
scope: Core
4 changes: 2 additions & 2 deletions kong/db/schema/others/declarative_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ local function generate_ids(input, known_entities, parent_entity)
local child_key
if parent_entity then
local parent_schema = all_schemas[parent_entity]
if parent_schema.fields[entity] then
if parent_schema.fields[entity] and not parent_schema.fields[entity].transient then
goto continue
end
parent_fk = parent_schema:extract_pk_values(input)
Expand Down Expand Up @@ -663,7 +663,7 @@ local function populate_ids_for_validation(input, known_entities, parent_entity,
local child_key
if parent_entity then
local parent_schema = all_schemas[parent_entity]
if parent_schema.fields[entity] then
if parent_schema.fields[entity] and not parent_schema.fields[entity].transient then
goto continue
end
parent_fk = parent_schema:extract_pk_values(input)
Expand Down
54 changes: 54 additions & 0 deletions spec/01-unit/01-db/10-declarative_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require("spec.helpers") -- for kong.log
local declarative = require "kong.db.declarative"
local conf_loader = require "kong.conf_loader"
local uuid = require "kong.tools.uuid"

local pl_file = require "pl.file"

local null = ngx.null

Expand Down Expand Up @@ -64,4 +67,55 @@ keyauth_credentials:
end)
end)

it("parse nested entities correctly", function ()
-- This regression test case is to make sure that when a
-- "raw" input of declarative config is given, the dc parser
-- can generate correct UUIDs for those nested entites.
-- When the declarative config parser tries to flatten the
-- config input, the input will be running agains the
-- declarative_config schema validation. But some input
-- might lacks required fieds(such as UUIDS) and their
-- relationships are declared by nesting objects. In such
-- cases the declarative config parser must generate necessary
-- fields for all the objects(known entities) inside the config.
-- This test case is to make sure that the parser can generate
-- correct UUIDs for nested entities. What's more, it also makes
-- sure that UUID generation are not influenced by the `transient`
-- field(used as a backref to foreign objects) configured inside
-- the schema.
--
-- See https://github.com/Kong/kong/pull/14082 for more details.
local cluster_cert_content = assert(pl_file.read("spec/fixtures/kong_clustering.crt"))
local cluster_key_content = assert(pl_file.read("spec/fixtures/kong_clustering.key"))
local cert_id = uuid.uuid()
local sni_id = uuid.uuid()
local dc = declarative.new_config(conf_loader())
local entities, err = dc:parse_table(
{
_format_version = "3.0",
certificates = { {
cert = cluster_cert_content,
id = cert_id,
key = cluster_key_content,
snis = { {
id = sni_id,
name = "alpha.example"
} }
} },
consumers = { {
basicauth_credentials = { {
password = "qwerty",
username = "qwerty"
} },
username = "consumerA"
} }
}
)

assert.is_nil(err)
assert.is_table(entities)
assert.is_not_nil(entities.snis)
assert.same('alpha.example', entities.certificates[cert_id].snis[1].name)
end)

end)
Loading