Skip to content

Commit

Permalink
feat(templates): add LMDB validation tag directive (#12026)
Browse files Browse the repository at this point in the history
This PR adds validation of LMDB cache by Kong's version (major + minor),
wiping the content if tag mismatch to avoid compatibility issues
during minor version upgrade.

KAG-3093
  • Loading branch information
chronolaw authored Nov 29, 2023
1 parent 3b53039 commit 6d44e81
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .requirements
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PCRE=8.45
LIBEXPAT=2.5.0

LUA_KONG_NGINX_MODULE=4fbc3ddc7dcbc706ed286b95344f3cb6da17e637 # 0.8.0
LUA_RESTY_LMDB=d236fc5ba339897e8f2c6ada1c1b4ab9311feee8 # 1.4.0
LUA_RESTY_LMDB=19a6da0616db43baf8197dace59e64244430b3c4 # 1.4.1
LUA_RESTY_EVENTS=8448a92cec36ac04ea522e78f6496ba03c9b1fd8 # 0.2.0
LUA_RESTY_WEBSOCKET=60eafc3d7153bceb16e6327074e0afc3d94b1316 # 0.4.0
ATC_ROUTER=7a2ad42d4246598ba1f753b6ae79cb1456040afa # 1.3.1
Expand Down
3 changes: 0 additions & 3 deletions changelog/unreleased/kong/bump-lua-resty-lmdb-1.4.0.yml

This file was deleted.

3 changes: 3 additions & 0 deletions changelog/unreleased/kong/bump-lua-resty-lmdb-1.4.1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Bumped lua-resty-lmdb from 1.3.0 to 1.4.1
type: dependency
scope: Core
6 changes: 6 additions & 0 deletions changelog/unreleased/kong/introduce_lmdb_validation_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
message: |
Validate LMDB cache by Kong's version (major + minor),
wiping the content if tag mismatch to avoid compatibility issues
during minor version upgrade.
type: feature
scope: Configuration
10 changes: 10 additions & 0 deletions kong/conf_loader/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local require = require


local kong_meta = require "kong.meta"
local kong_default_conf = require "kong.templates.kong_defaults"
local process_secrets = require "kong.cmd.utils.process_secrets"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
Expand Down Expand Up @@ -683,6 +684,12 @@ local _nop_tostring_mt = {
}


-- using kong version, "major.minor"
local LMDB_VALIDATION_TAG = string.format("%d.%d",
kong_meta._VERSION_TABLE.major,
kong_meta._VERSION_TABLE.minor)


local function parse_value(value, typ)
if type(value) == "string" then
value = strip(value)
Expand Down Expand Up @@ -2008,6 +2015,9 @@ local function load(path, custom_conf, opts)
end
end

-- lmdb validation tag
conf.lmdb_validation_tag = LMDB_VALIDATION_TAG

-- Wasm module support
if conf.wasm then
local wasm_filters = get_wasm_filters(conf.wasm_filters_path)
Expand Down
5 changes: 5 additions & 0 deletions kong/templates/nginx_inject.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ return [[
> if database == "off" then
lmdb_environment_path ${{LMDB_ENVIRONMENT_PATH}};
lmdb_map_size ${{LMDB_MAP_SIZE}};
> if lmdb_validation_tag then
lmdb_validation_tag $(lmdb_validation_tag);
> end
> end
]]
8 changes: 8 additions & 0 deletions spec/01-unit/03-conf_loader_spec.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local kong_meta = require "kong.meta"
local conf_loader = require "kong.conf_loader"
local utils = require "kong.tools.utils"
local log = require "kong.cmd.utils.log"
Expand All @@ -16,6 +17,11 @@ ffi.cdef([[
]])


local KONG_VERSION = string.format("%d.%d",
kong_meta._VERSION_TABLE.major,
kong_meta._VERSION_TABLE.minor)


local function kong_user_group_exists()
if C.getpwnam("kong") == nil or C.getgrnam("kong") == nil then
return false
Expand Down Expand Up @@ -68,6 +74,7 @@ describe("Configuration loader", function()
assert.same(nil, conf.privileged_agent)
assert.same(true, conf.dedicated_config_processing)
assert.same(false, conf.allow_debug_header)
assert.same(KONG_VERSION, conf.lmdb_validation_tag)
assert.is_nil(getmetatable(conf))
end)
it("loads a given file, with higher precedence", function()
Expand All @@ -85,6 +92,7 @@ describe("Configuration loader", function()
assert.same({"127.0.0.1:9001"}, conf.admin_listen)
assert.same({"0.0.0.0:9000", "0.0.0.0:9443 http2 ssl",
"0.0.0.0:9002 http2"}, conf.proxy_listen)
assert.same(KONG_VERSION, conf.lmdb_validation_tag)
assert.is_nil(getmetatable(conf))
end)
it("preserves default properties if not in given file", function()
Expand Down
6 changes: 6 additions & 0 deletions spec/01-unit/04-prefix_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,7 @@ describe("NGINX conf compiler", function()
local main_inject_conf = prefix_handler.compile_nginx_main_inject_conf(helpers.test_conf)
assert.not_matches("lmdb_environment_path", main_inject_conf, nil, true)
assert.not_matches("lmdb_map_size", main_inject_conf, nil, true)
assert.not_matches("lmdb_validation_tag", main_inject_conf, nil, true)
end)

it("compiles a main NGINX inject conf #database=off", function()
Expand All @@ -1462,6 +1463,11 @@ describe("NGINX conf compiler", function()
local main_inject_conf = prefix_handler.compile_nginx_main_inject_conf(conf)
assert.matches("lmdb_environment_path%s+dbless.lmdb;", main_inject_conf)
assert.matches("lmdb_map_size%s+2048m;", main_inject_conf)

local kong_meta = require "kong.meta"
local major = kong_meta._VERSION_TABLE.major
local minor = kong_meta._VERSION_TABLE.minor
assert.matches("lmdb_validation_tag%s+" .. major .. "%." .. minor .. ";", main_inject_conf)
end)
end)

Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/custom_nginx.template
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ $(el.name) $(el.value);
> if database == "off" then
lmdb_environment_path ${{LMDB_ENVIRONMENT_PATH}};
lmdb_map_size ${{LMDB_MAP_SIZE}};

> if lmdb_validation_tag then
lmdb_validation_tag $(lmdb_validation_tag);
> end

> end

events {
Expand Down

0 comments on commit 6d44e81

Please sign in to comment.