Skip to content

Commit c33859a

Browse files
StarlightIbukichronolawbunglekikito
authored andcommitted
fix(db/migration): do migration before validation (#10348)
Co-authored-by: Chrono <[email protected]> Co-authored-by: Aapo Talvensaari <[email protected]> Co-authored-by: Enrique García Cota <[email protected]> (cherry picked from commit 2f82ced)
1 parent 0f77a1d commit c33859a

File tree

5 files changed

+68
-20
lines changed

5 files changed

+68
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
[#10346](https://github.com/Kong/kong/pull/10346)
8383
- Fix an issue where control plane does not rename fields correctly for `session` for older version of data planes.
8484
[#10352](https://github.com/Kong/kong/pull/10352)
85+
- Fix an issue where validation to regex routes may be skipped when the old-fashioned config is used for DB-less Kong.
86+
[#10348](https://github.com/Kong/kong/pull/10348)
8587

8688
## 3.2.0
8789

kong/db/declarative/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ function _M:parse_table(dc_table, hash)
219219
error("expected a table as input", 2)
220220
end
221221

222+
on_the_fly_migration(dc_table)
223+
222224
local entities, err_t, meta = self.schema:flatten(dc_table)
223225
if err_t then
224226
return nil, pretty_print_error(err_t), err_t
225227
end
226228

227-
on_the_fly_migration(entities, dc_table._format_version)
228-
229229
yield()
230230

231231
if not self.partial then
Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
11
local migrate_path = require "kong.db.migrations.migrate_path_280_300"
2+
local lyaml_null = require("lyaml").null
3+
local cjson_null = require("cjson").null
24

5+
local ngx_null = ngx.null
36
local pairs = pairs
47
local ipairs = ipairs
5-
local null = ngx.null
68

7-
return function(tbl, version)
8-
if not tbl or not (version == "1.1" or version == "2.1") then
9-
return
10-
end
11-
12-
local routes = tbl.routes
9+
local EMPTY = {}
1310

14-
if not routes then
15-
-- no need to migrate
16-
return
11+
local function ensure_table(val)
12+
if val == nil or val == ngx_null or val == lyaml_null or val == cjson_null or type(val) ~= "table" then
13+
return EMPTY
1714
end
15+
return val
16+
end
1817

18+
local function migrate_routes(routes)
1919
for _, route in pairs(routes) do
20-
local paths = route.paths
21-
if not paths or paths == null then
22-
-- no need to migrate
23-
goto continue
24-
end
20+
local paths = ensure_table(route.paths)
2521

2622
for idx, path in ipairs(paths) do
2723
paths[idx] = migrate_path(path)
2824
end
25+
end
26+
end
27+
28+
return function(tbl)
29+
local version = tbl._format_version
30+
if not tbl or not (version == "1.1" or version == "2.1") then
31+
return
32+
end
33+
34+
-- migrate top-level routes
35+
local routes = ensure_table(tbl.routes)
36+
migrate_routes(routes)
2937

30-
::continue::
38+
-- migrate routes nested in top-level services
39+
local services = ensure_table(tbl.services)
40+
for _, service in ipairs(services) do
41+
local nested_routes = ensure_table(service.routes)
42+
43+
migrate_routes(nested_routes)
3144
end
45+
46+
tbl._format_version = "3.0"
3247
end

spec/01-unit/01-db/01-schema/11-declarative_config/04-on-the-fly-migration_spec.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,35 @@ describe("declarative config: on the fly migration", function()
154154
end)
155155
end
156156
end)
157+
158+
it("validation should happens after migration", function ()
159+
local dc = assert(declarative.new_config(conf_loader()))
160+
local config =
161+
[[
162+
_format_version: "2.1"
163+
services:
164+
- name: foo
165+
host: example.com
166+
protocol: https
167+
enabled: false
168+
_comment: my comment
169+
- name: bar
170+
host: example.test
171+
port: 3000
172+
_comment: my comment
173+
routes:
174+
- name: foo
175+
path_handling: v0
176+
protocols: ["https"]
177+
paths: ["/regex.+(", "/prefix" ]
178+
snis:
179+
- "example.com"
180+
]]
181+
182+
local config_tbl, err = dc:parse_string(config)
183+
184+
assert.falsy(config_tbl)
185+
assert.matches("invalid regex:", err, nil, true)
186+
assert.matches("/regex.+(", err, nil, true)
187+
assert.matches("missing )", err, nil, true)
188+
end)

spec/02-integration/02-cmd/11-config_spec.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ describe("kong config", function()
109109
local _, res = assert(thread:join())
110110
assert.matches("signal=config-db-import", res, nil, true)
111111
-- it will be updated on-the-fly
112-
-- but the version should still be 1.1
113-
assert.matches("decl_fmt_version=1.1", res, nil, true)
112+
assert.matches("decl_fmt_version=3.0", res, nil, true)
114113
assert.matches("file_ext=.yml", res, nil, true)
115114

116115
local client = helpers.admin_client()

0 commit comments

Comments
 (0)