Skip to content

Commit

Permalink
fix(declarative-config): set hash when config is loaded
Browse files Browse the repository at this point in the history
Ensure the configuration hash field is correctly configured when the
declarative configuration is loaded on startup.
  • Loading branch information
samugi committed Dec 7, 2022
1 parent 2048aa7 commit cf0f9a5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
23 changes: 12 additions & 11 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ local CTX_NREC = 50 -- normally Kong has ~32 keys in ctx

local declarative_entities
local declarative_meta
local declarative_hash
local schema_state


Expand Down Expand Up @@ -429,15 +430,15 @@ local function parse_declarative_config(kong_config)
if not has_declarative_config(kong_config) then
-- return an empty configuration,
-- including only the default workspace
local entities, _, _, meta = dc:parse_table({ _format_version = "2.1" })
return entities, nil, meta
local entities, _, _, meta, hash = dc:parse_table({ _format_version = "2.1" })
return entities, nil, meta, hash
end

local entities, err, _, meta
local entities, err, _, meta, hash
if kong_config.declarative_config ~= nil then
entities, err, _, meta = dc:parse_file(kong_config.declarative_config)
entities, err, _, meta, hash = dc:parse_file(kong_config.declarative_config)
elseif kong_config.declarative_config_string ~= nil then
entities, err, _, meta = dc:parse_string(kong_config.declarative_config_string)
entities, err, _, meta, hash = dc:parse_string(kong_config.declarative_config_string)
end

if not entities then
Expand All @@ -450,7 +451,7 @@ local function parse_declarative_config(kong_config)
end
end

return entities, nil, meta
return entities, nil, meta, hash
end


Expand All @@ -472,7 +473,7 @@ local function declarative_init_build()
end


local function load_declarative_config(kong_config, entities, meta)
local function load_declarative_config(kong_config, entities, meta, hash)
local opts = {
name = "declarative_config",
}
Expand All @@ -483,8 +484,7 @@ local function load_declarative_config(kong_config, entities, meta)
if value then
return true
end

local ok, err = declarative.load_into_cache(entities, meta)
local ok, err = declarative.load_into_cache(entities, meta, hash)
if not ok then
return nil, err
end
Expand Down Expand Up @@ -603,7 +603,7 @@ function Kong.init()
#config.status_listeners == 0)
then
local err
declarative_entities, err, declarative_meta = parse_declarative_config(kong.configuration)
declarative_entities, err, declarative_meta, declarative_hash = parse_declarative_config(kong.configuration)
if not declarative_entities then
error(err)
end
Expand Down Expand Up @@ -733,7 +733,8 @@ function Kong.init_worker()
elseif declarative_entities then
ok, err = load_declarative_config(kong.configuration,
declarative_entities,
declarative_meta)
declarative_meta,
declarative_hash)
if not ok then
stash_init_worker_error("failed to load declarative config file: " .. err)
return
Expand Down
9 changes: 7 additions & 2 deletions spec/02-integration/04-admin_api/02-kong_routes_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"
local constants = require "kong.constants"

local UUID_PATTERN = "%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x"

Expand Down Expand Up @@ -188,6 +189,8 @@ describe("Admin API - Kong routes with strategy #" .. strategy, function()
end)

describe("/status", function()
local empty_config_hash = constants.DECLARATIVE_EMPTY_CONFIG_HASH

it("returns status info", function()
local res = assert(client:send {
method = "GET",
Expand All @@ -208,7 +211,9 @@ describe("Admin API - Kong routes with strategy #" .. strategy, function()
assert.is_number(json.server.connections_waiting)
assert.is_number(json.server.total_requests)
if strategy == "off" then
assert.is_equal(string.rep("0", 32), json.configuration_hash) -- all 0 in DBLESS mode until configuration is applied
assert.is_string(json.configuration_hash)
assert.equal(32, #json.configuration_hash)
assert.is_not_equal(empty_config_hash, json.configuration_hash)
else
assert.is_nil(json.configuration_hash) -- not present in DB mode
end
Expand Down Expand Up @@ -251,7 +256,7 @@ describe("Admin API - Kong routes with strategy #" .. strategy, function()
assert.is_number(json.server.total_requests)
assert.is_string(json.configuration_hash)
assert.equal(32, #json.configuration_hash)

assert.is_not_equal(empty_config_hash, json.configuration_hash)
end)

it("database.reachable is `true` when DB connection is healthy", function()
Expand Down
54 changes: 53 additions & 1 deletion spec/02-integration/11-dbless/03-config_persistence.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local helpers = require "spec.helpers"
local helpers = require "spec.helpers"
local constants = require "kong.constants"
local cjson = require "cjson"

local fmt = string.format

Expand Down Expand Up @@ -152,3 +154,53 @@ describe("dbless persistence with a declarative config #off", function()
assert.res_status(404, res) -- 404, only the declarative config is loaded
end)
end)

describe("dbless persistence configuration loaded on startup #off", function()
local admin_client, yaml_file

lazy_setup(function()
yaml_file = helpers.make_yaml_file([[
_format_version: "1.1"
services:
- name: my-service
url: https://example1.dev
plugins:
- name: key-auth
routes:
- name: my-route
paths:
- /test
]])
end)

before_each(function()
assert(helpers.start_kong({
database = "off",
declarative_config = yaml_file,
}))
admin_client = assert(helpers.admin_client())
end)

after_each(function()
if admin_client then
admin_client:close()
end
helpers.stop_kong(nil, true)
end)
lazy_teardown(function()
os.remove(yaml_file)
end)

it("hash is configured correctly", function()
local res = assert(admin_client:send {
method = "GET",
path = "/status"
})
local body = assert.res_status(200, res)
local json = cjson.decode(body)

assert.is_string(json.configuration_hash)
assert.equals(32, #json.configuration_hash)
assert.not_equal(constants.DECLARATIVE_EMPTY_CONFIG_HASH, json.configuration_hash)
end)
end)

0 comments on commit cf0f9a5

Please sign in to comment.