From 8a3399b0e27bf79d344845ff490057f9110fc932 Mon Sep 17 00:00:00 2001 From: Caio Ramos Casimiro Date: Wed, 9 Oct 2024 22:44:29 +0100 Subject: [PATCH] feat(proxy-wasm): add host properties 'kong.service_name' and 'kong.route_name' --- kong/runloop/wasm.lua | 14 ++++++ .../20-wasm/04-proxy-wasm_spec.lua | 43 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/kong/runloop/wasm.lua b/kong/runloop/wasm.lua index 351499ad4f7e..5a6a5538fadd 100644 --- a/kong/runloop/wasm.lua +++ b/kong/runloop/wasm.lua @@ -778,6 +778,13 @@ local function register_property_handlers() return ok, value, const end) + properties.add_getter("kong.route_name", function(_, _, ctx) + local value = ctx.route and ctx.route.name + local ok = value ~= nil + local const = ok + return ok, value, const + end) + properties.add_getter("kong.service.response.status", function(kong) return true, kong.service.response.get_status(), false end) @@ -789,6 +796,13 @@ local function register_property_handlers() return ok, value, const end) + properties.add_getter("kong.service_name", function(_, _, ctx) + local value = ctx.service and ctx.service.name + local ok = value ~= nil + local const = ok + return ok, value, const + end) + properties.add_getter("kong.version", function(kong) return true, kong.version, true end) diff --git a/spec/02-integration/20-wasm/04-proxy-wasm_spec.lua b/spec/02-integration/20-wasm/04-proxy-wasm_spec.lua index 29756c710525..bcda9bf1a4f3 100644 --- a/spec/02-integration/20-wasm/04-proxy-wasm_spec.lua +++ b/spec/02-integration/20-wasm/04-proxy-wasm_spec.lua @@ -38,6 +38,7 @@ describe("proxy-wasm filters (#wasm) (#" .. strategy .. ")", function() mock_service = assert(bp.services:insert { host = helpers.mock_upstream_host, port = helpers.mock_upstream_port, + name = "mock_service", }) local mock_upstream = assert(bp.upstreams:insert { @@ -50,12 +51,14 @@ describe("proxy-wasm filters (#wasm) (#" .. strategy .. ")", function() }) r_single = assert(bp.routes:insert { + name = "r_single", paths = { "/single" }, strip_path = true, service = mock_service, }) local r_double = assert(bp.routes:insert { + name = "r_double", paths = { "/double" }, strip_path = true, service = mock_service, @@ -687,6 +690,26 @@ describe("proxy-wasm filters (#wasm) (#" .. strategy .. ")", function() assert.logfile().has.no.line("[crit]", true, 0) end) + it("read kong.route_name", function() + local client = helpers.proxy_client() + finally(function() client:close() end) + + local res = assert(client:send { + method = "GET", + path = "/single/status/201", + headers = { + [HEADER_NAME_TEST] = "get_kong_property", + [HEADER_NAME_INPUT] = "route_name", + [HEADER_NAME_DISPATCH_ECHO] = "on", + } + }) + + local body = assert.res_status(200, res) + assert.equal(r_single.name, body) + assert.logfile().has.no.line("[error]", true, 0) + assert.logfile().has.no.line("[crit]", true, 0) + end) + it("read kong.service_id", function() local client = helpers.proxy_client() finally(function() client:close() end) @@ -707,6 +730,26 @@ describe("proxy-wasm filters (#wasm) (#" .. strategy .. ")", function() assert.logfile().has.no.line("[crit]", true, 0) end) + it("read kong.service_name", function() + local client = helpers.proxy_client() + finally(function() client:close() end) + + local res = assert(client:send { + method = "GET", + path = "/single/status/201", + headers = { + [HEADER_NAME_TEST] = "get_kong_property", + [HEADER_NAME_INPUT] = "service_name", + [HEADER_NAME_DISPATCH_ECHO] = "on", + } + }) + + local body = assert.res_status(200, res) + assert.equal(mock_service.name, body) + assert.logfile().has.no.line("[error]", true, 0) + assert.logfile().has.no.line("[crit]", true, 0) + end) + it("read kong.ctx.shared[]", function() local client = helpers.proxy_client() finally(function() client:close() end)