Skip to content

Commit

Permalink
feat(clustering/sync): add strict validation for delta workspace id (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chobits authored Feb 19, 2025
1 parent f4835f2 commit 296d682
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions kong/clustering/services/sync/validate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ local function validate_deltas(deltas, is_full_sync)
local delta_entity = delta.entity

if delta_entity ~= nil and delta_entity ~= null then

-- validate workspace id
local ws_id = delta_entity.ws_id or delta.ws_id
if not ws_id or ws_id == null then
if not errs[delta_type] then
errs[delta_type] = {}
end
insert(errs[delta_type], "workspace id not found")
end

-- table: primary key string -> entity
local schema = db[delta_type].schema
local pk = schema:extract_pk_values(delta_entity)
Expand Down
29 changes: 29 additions & 0 deletions spec/01-unit/19-hybrid/04-validate_deltas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,36 @@ describe("[delta validations]",function()
for _, delta in ipairs(deltas) do
local ws_id = delta.ws_id
assert(ws_id and ws_id ~= ngx.null)

-- XXX EE: kong-ce entities exported from export_config_sync() do not
-- contain ws_id field, while kong-ee enntities does.
-- assert(delta.entity.ws_id)

-- mannually remove routes ws_id, and then validation will report error
if delta.type == "routes" then
delta.entity.ws_id = nil
delta.ws_id = nil
end

if delta.type == "services" then
delta.entity.ws_id = ngx.null
delta.ws_id = ngx.null
end
end

local _, _, err_t = validate_deltas(deltas)

assert.same(err_t, {
code = 21,
fields = {
routes = { "workspace id not found" },
services = { "workspace id not found" },
},
flattened_errors = { },
message = 'sync deltas is invalid: {routes={"workspace id not found"},services={"workspace id not found"}}',
name = "sync deltas parse failure",
source = "kong.clustering.services.sync.validate.validate_deltas",
})
end)

it("route has no required field, but uses default value", function()
Expand Down

1 comment on commit 296d682

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong-dev:296d682f51fd6c136d71a11cc9ab017ab153f62b
Artifacts available https://github.com/Kong/kong/actions/runs/13407347086

Please sign in to comment.