Skip to content

Commit 02341b8

Browse files
committed
fix(vault): make it possible to use vault references in declarative config (#11843)
### Summary Warmup cache on `init` where we have Lua `coroutines` available so that it won't happen on `init_worker` where we don't have them (and cannot use e.g. lua-resty-http). See KAG-2620 and FTI-5080. Signed-off-by: Aapo Talvensaari <[email protected]> * Update spec/02-integration/02-cmd/02-start_stop_spec.lua --------- Signed-off-by: Aapo Talvensaari <[email protected]> Co-authored-by: Samuele <[email protected]> (cherry picked from commit 12324a1)
1 parent a65923a commit 02341b8

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
message: Vault references can be used in Dbless mode in declarative config
2+
type: bugfix
3+
scope: Core

kong/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,8 @@ function Kong.init()
717717
if not declarative_entities then
718718
error(err)
719719
end
720+
721+
kong.vault.warmup(declarative_entities)
720722
end
721723

722724
else

kong/pdk/vault.lua

+22
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,28 @@ local function new(self)
15631563
init_worker()
15641564
end
15651565

1566+
---
1567+
-- Warmups vault caches from config.
1568+
--
1569+
-- @local
1570+
-- @function kong.vault.warmup
1571+
function _VAULT.warmup(input)
1572+
for k, v in pairs(input) do
1573+
local kt = type(k)
1574+
if kt == "table" then
1575+
_VAULT.warmup(k)
1576+
elseif kt == "string" and is_reference(k) then
1577+
get(k)
1578+
end
1579+
local vt = type(v)
1580+
if vt == "table" then
1581+
_VAULT.warmup(v)
1582+
elseif vt == "string" and is_reference(v) then
1583+
get(v)
1584+
end
1585+
end
1586+
end
1587+
15661588
if get_phase() == "init" then
15671589
init()
15681590
end

spec/02-integration/02-cmd/02-start_stop_spec.lua

+36-1
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,43 @@ describe("kong start/stop #" .. strategy, function()
663663
assert.matches("in 'name': invalid value '@gobo': the only accepted ascii characters are alphanumerics or ., -, _, and ~", err, nil, true)
664664
assert.matches("in entry 2 of 'hosts': invalid hostname: \\\\99", err, nil, true)
665665
end)
666-
end
667666

667+
it("dbless can reference secrets in declarative configuration", function()
668+
local yaml_file = helpers.make_yaml_file [[
669+
_format_version: "3.0"
670+
_transform: true
671+
plugins:
672+
- name: session
673+
instance_name: session
674+
config:
675+
secret: "{vault://mocksocket/test}"
676+
]]
677+
678+
finally(function()
679+
os.remove(yaml_file)
680+
end)
681+
682+
helpers.setenv("KONG_LUA_PATH_OVERRIDE", "./spec/fixtures/custom_vaults/?.lua;./spec/fixtures/custom_vaults/?/init.lua;;")
683+
helpers.get_db_utils(strategy, {
684+
"vaults",
685+
}, {
686+
"session"
687+
}, {
688+
"mocksocket"
689+
})
690+
691+
local ok, err = helpers.start_kong({
692+
database = "off",
693+
declarative_config = yaml_file,
694+
vaults = "mocksocket",
695+
plugins = "session"
696+
})
697+
698+
assert.truthy(ok)
699+
assert.not_matches("error", err)
700+
assert.logfile().has.no.line("[error]", true, 0)
701+
end)
702+
end
668703
end)
669704

670705
describe("deprecated properties", function()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
local env = require "kong.vaults.env"
2+
local http = require "resty.luasocket.http"
3+
4+
5+
local assert = assert
6+
local getenv = os.getenv
7+
8+
9+
local function init()
10+
env.init()
11+
assert(getenv("KONG_PROCESS_SECRETS") == nil, "KONG_PROCESS_SECRETS environment variable found")
12+
assert(env.get({}, "KONG_PROCESS_SECRETS") == nil, "KONG_PROCESS_SECRETS environment variable found")
13+
end
14+
15+
16+
local function get(conf, resource, version)
17+
local client, err = http.new()
18+
if not client then
19+
return nil, err
20+
end
21+
22+
client:set_timeouts(20000, 20000, 20000)
23+
assert(client:request_uri("http://mockbin.org/headers", {
24+
headers = {
25+
Accept = "application/json",
26+
},
27+
}))
28+
29+
return env.get(conf, resource, version)
30+
end
31+
32+
33+
return {
34+
VERSION = "1.0.0",
35+
init = init,
36+
get = get,
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
return {
2+
name = "mocksocket",
3+
fields = {
4+
{
5+
config = {
6+
type = "record",
7+
fields = {
8+
{ prefix = { type = "string", match = [[^[%a_][%a%d_]*$]] } },
9+
},
10+
},
11+
},
12+
},
13+
}

0 commit comments

Comments
 (0)