Skip to content

Commit 55afae5

Browse files
authored
fix(declarative): generate correct uuid for transient entities (#14082)
1 parent f68de81 commit 55afae5

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
message: "Fixed an issue where a valid declarative config with certificate/sni entities cannot be loaded in dbless mode"
2+
type: bugfix
3+
scope: Core

kong/db/schema/others/declarative_config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ local function generate_ids(input, known_entities, parent_entity)
626626
local child_key
627627
if parent_entity then
628628
local parent_schema = all_schemas[parent_entity]
629-
if parent_schema.fields[entity] then
629+
if parent_schema.fields[entity] and not parent_schema.fields[entity].transient then
630630
goto continue
631631
end
632632
parent_fk = parent_schema:extract_pk_values(input)
@@ -663,7 +663,7 @@ local function populate_ids_for_validation(input, known_entities, parent_entity,
663663
local child_key
664664
if parent_entity then
665665
local parent_schema = all_schemas[parent_entity]
666-
if parent_schema.fields[entity] then
666+
if parent_schema.fields[entity] and not parent_schema.fields[entity].transient then
667667
goto continue
668668
end
669669
parent_fk = parent_schema:extract_pk_values(input)

spec/01-unit/01-db/10-declarative_spec.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require("spec.helpers") -- for kong.log
22
local declarative = require "kong.db.declarative"
33
local conf_loader = require "kong.conf_loader"
4+
local uuid = require "kong.tools.uuid"
5+
6+
local pl_file = require "pl.file"
47

58
local null = ngx.null
69

@@ -64,4 +67,55 @@ keyauth_credentials:
6467
end)
6568
end)
6669

70+
it("parse nested entities correctly", function ()
71+
-- This regression test case is to make sure that when a
72+
-- "raw" input of declarative config is given, the dc parser
73+
-- can generate correct UUIDs for those nested entites.
74+
-- When the declarative config parser tries to flatten the
75+
-- config input, the input will be running agains the
76+
-- declarative_config schema validation. But some input
77+
-- might lacks required fieds(such as UUIDS) and their
78+
-- relationships are declared by nesting objects. In such
79+
-- cases the declarative config parser must generate necessary
80+
-- fields for all the objects(known entities) inside the config.
81+
-- This test case is to make sure that the parser can generate
82+
-- correct UUIDs for nested entities. What's more, it also makes
83+
-- sure that UUID generation are not influenced by the `transient`
84+
-- field(used as a backref to foreign objects) configured inside
85+
-- the schema.
86+
--
87+
-- See https://github.com/Kong/kong/pull/14082 for more details.
88+
local cluster_cert_content = assert(pl_file.read("spec/fixtures/kong_clustering.crt"))
89+
local cluster_key_content = assert(pl_file.read("spec/fixtures/kong_clustering.key"))
90+
local cert_id = uuid.uuid()
91+
local sni_id = uuid.uuid()
92+
local dc = declarative.new_config(conf_loader())
93+
local entities, err = dc:parse_table(
94+
{
95+
_format_version = "3.0",
96+
certificates = { {
97+
cert = cluster_cert_content,
98+
id = cert_id,
99+
key = cluster_key_content,
100+
snis = { {
101+
id = sni_id,
102+
name = "alpha.example"
103+
} }
104+
} },
105+
consumers = { {
106+
basicauth_credentials = { {
107+
password = "qwerty",
108+
username = "qwerty"
109+
} },
110+
username = "consumerA"
111+
} }
112+
}
113+
)
114+
115+
assert.is_nil(err)
116+
assert.is_table(entities)
117+
assert.is_not_nil(entities.snis)
118+
assert.same('alpha.example', entities.certificates[cert_id].snis[1].name)
119+
end)
120+
67121
end)

0 commit comments

Comments
 (0)