Skip to content

Commit 3d583c8

Browse files
committed
refactor(pdk) vault pdk to be more like rest of the pdk modules
### Summary Refactor Vault PDK to follow other Kong PDK modules. This means that functions are created inside `.new` function. This has benefit of being able to access up-value `self`, which means that no direct references to global `kong` is needed. In general, it makes testing and mocking easier too. I need this so I can pass some initial configuration very early on when Kong does process secrets resolving of Kong configuration references.
1 parent 5156596 commit 3d583c8

File tree

3 files changed

+295
-310
lines changed

3 files changed

+295
-310
lines changed

kong/cmd/vault.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ end
4242

4343

4444
local function get(args)
45-
local vault = require "kong.pdk.vault".new()
4645
if args.command == "get" then
4746
local reference = args[1]
4847
if not reference then
@@ -51,6 +50,8 @@ local function get(args)
5150

5251
init_db(args)
5352

53+
local vault = kong.vault
54+
5455
if not vault.is_reference(reference) then
5556
-- assuming short form: <name>/<resource>[/<key>]
5657
reference = fmt("{vault://%s}", reference)

kong/db/schema/init.lua

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ local tablex = require "pl.tablex"
22
local pretty = require "pl.pretty"
33
local utils = require "kong.tools.utils"
44
local cjson = require "cjson"
5-
local vault = require "kong.pdk.vault".new()
5+
local is_reference = require "kong.pdk.vault".new().is_reference
66

77

8-
local is_reference = vault.is_reference
9-
local dereference = vault.get
108
local setmetatable = setmetatable
119
local getmetatable = getmetatable
1210
local re_match = ngx.re.match
@@ -1744,7 +1742,7 @@ function Schema:process_auto_fields(data, context, nulls, opts)
17441742
refs = { [key] = value }
17451743
end
17461744

1747-
local deref, err = dereference(value)
1745+
local deref, err = kong.vault.get(value)
17481746
if deref then
17491747
value = deref
17501748

@@ -1768,7 +1766,7 @@ function Schema:process_auto_fields(data, context, nulls, opts)
17681766
for i = 1, count do
17691767
if is_reference(value[i]) then
17701768
refs[key][i] = value[i]
1771-
local deref, err = dereference(value[i])
1769+
local deref, err = kong.vault.get(value[i])
17721770
if deref then
17731771
value[i] = deref
17741772
else

0 commit comments

Comments
 (0)