Skip to content

Commit

Permalink
refactor(pdk) vault pdk to be more like rest of the pdk modules
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
bungle committed Apr 19, 2022
1 parent 5156596 commit 3d583c8
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 310 deletions.
3 changes: 2 additions & 1 deletion kong/cmd/vault.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ end


local function get(args)
local vault = require "kong.pdk.vault".new()
if args.command == "get" then
local reference = args[1]
if not reference then
Expand All @@ -51,6 +50,8 @@ local function get(args)

init_db(args)

local vault = kong.vault

if not vault.is_reference(reference) then
-- assuming short form: <name>/<resource>[/<key>]
reference = fmt("{vault://%s}", reference)
Expand Down
8 changes: 3 additions & 5 deletions kong/db/schema/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ local tablex = require "pl.tablex"
local pretty = require "pl.pretty"
local utils = require "kong.tools.utils"
local cjson = require "cjson"
local vault = require "kong.pdk.vault".new()
local is_reference = require "kong.pdk.vault".new().is_reference


local is_reference = vault.is_reference
local dereference = vault.get
local setmetatable = setmetatable
local getmetatable = getmetatable
local re_match = ngx.re.match
Expand Down Expand Up @@ -1744,7 +1742,7 @@ function Schema:process_auto_fields(data, context, nulls, opts)
refs = { [key] = value }
end

local deref, err = dereference(value)
local deref, err = kong.vault.get(value)
if deref then
value = deref

Expand All @@ -1768,7 +1766,7 @@ function Schema:process_auto_fields(data, context, nulls, opts)
for i = 1, count do
if is_reference(value[i]) then
refs[key][i] = value[i]
local deref, err = dereference(value[i])
local deref, err = kong.vault.get(value[i])
if deref then
value[i] = deref
else
Expand Down
Loading

0 comments on commit 3d583c8

Please sign in to comment.