From ea147e247ceb5069b7501410f20daccf0610d61f Mon Sep 17 00:00:00 2001 From: Joshua Schmid Date: Tue, 10 Oct 2023 16:15:06 +0200 Subject: [PATCH 1/2] feat(core): add `/schemas/vaults/:name` endpoint Signed-off-by: Joshua Schmid --- changelog/unreleased/kong/11727.yml | 3 +++ kong/api/routes/kong.lua | 13 ++++++++++ kong/vaults/env/schema.lua | 2 +- .../04-admin_api/02-kong_routes_spec.lua | 24 +++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/kong/11727.yml diff --git a/changelog/unreleased/kong/11727.yml b/changelog/unreleased/kong/11727.yml new file mode 100644 index 000000000000..dfdf7090b22d --- /dev/null +++ b/changelog/unreleased/kong/11727.yml @@ -0,0 +1,3 @@ +message: "Add a new endpoint `/schemas/vaults/:name` to retrieve the schema of a vault." +type: feature +scope: Core diff --git a/kong/api/routes/kong.lua b/kong/api/routes/kong.lua index d7401c57f826..aa6df177c3f7 100644 --- a/kong/api/routes/kong.lua +++ b/kong/api/routes/kong.lua @@ -183,6 +183,19 @@ return { return validate_schema(db_entity_name, self.params) end }, + + ["/schemas/vaults/:name"] = { + GET = function(self, db, helpers) + local subschema = kong.db.vaults.schema.subschemas[self.params.name] + if not subschema then + return kong.response.exit(404, { message = "No vault named '" + .. self.params.name .. "'" }) + end + local copy = api_helpers.schema_to_jsonable(subschema) + strip_foreign_schemas(copy.fields) + return kong.response.exit(200, copy) + end + }, ["/schemas/plugins/:name"] = { GET = function(self, db, helpers) local subschema = kong.db.plugins.schema.subschemas[self.params.name] diff --git a/kong/vaults/env/schema.lua b/kong/vaults/env/schema.lua index 6e2750e276c2..1fa758089268 100644 --- a/kong/vaults/env/schema.lua +++ b/kong/vaults/env/schema.lua @@ -5,7 +5,7 @@ return { config = { type = "record", fields = { - { prefix = { type = "string", match = [[^[%a_-][%a%d_-]*$]] } }, + { prefix = { type = "string", match = [[^[%a_-][%a%d_-]*$]], description = "The prefix for the environment variable that the value will be stored in." } }, }, }, }, diff --git a/spec/02-integration/04-admin_api/02-kong_routes_spec.lua b/spec/02-integration/04-admin_api/02-kong_routes_spec.lua index 49d4e284f713..29ca1ffa1284 100644 --- a/spec/02-integration/04-admin_api/02-kong_routes_spec.lua +++ b/spec/02-integration/04-admin_api/02-kong_routes_spec.lua @@ -462,6 +462,30 @@ describe("Admin API - Kong routes with strategy #" .. strategy, function() end) end) + describe("/schemas/vaults/:name", function() + it("returns schema of all vaults", function() + for _, vault in ipairs({"env"}) do + local res = assert(client:send { + method = "GET", + path = "/schemas/vaults/" .. vault, + }) + local body = assert.res_status(200, res) + local json = cjson.decode(body) + assert.is_table(json.fields) + end + end) + + it("returns 404 on a non-existent plugin", function() + local res = assert(client:send { + method = "GET", + path = "/schemas/vaults/not-present", + }) + local body = assert.res_status(404, res) + local json = cjson.decode(body) + assert.same({ message = "No vault named 'not-present'" }, json) + end) + end) + describe("/schemas/:entity", function() it("returns schema of all plugins", function() for plugin, _ in pairs(helpers.test_conf.loaded_plugins) do From e873c8a1ec1308763e848131ff9cf997295fec1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20H=C3=BCbner?= Date: Wed, 11 Oct 2023 07:35:39 +0200 Subject: [PATCH 2/2] Update spec/02-integration/04-admin_api/02-kong_routes_spec.lua Co-authored-by: Joshua Schmid --- spec/02-integration/04-admin_api/02-kong_routes_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/02-integration/04-admin_api/02-kong_routes_spec.lua b/spec/02-integration/04-admin_api/02-kong_routes_spec.lua index 29ca1ffa1284..dce6ce2d7a50 100644 --- a/spec/02-integration/04-admin_api/02-kong_routes_spec.lua +++ b/spec/02-integration/04-admin_api/02-kong_routes_spec.lua @@ -475,7 +475,7 @@ describe("Admin API - Kong routes with strategy #" .. strategy, function() end end) - it("returns 404 on a non-existent plugin", function() + it("returns 404 on a non-existent vault", function() local res = assert(client:send { method = "GET", path = "/schemas/vaults/not-present",