-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pdk) add consumer, service, route handling
Adds getters/setters to the PDK for the consumer (+credential), service, and route entities.
- Loading branch information
Showing
11 changed files
with
622 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- Router module | ||
-- A set of functions to access the routing properties of the request. | ||
-- | ||
-- @module kong.router | ||
|
||
|
||
local phase_checker = require "kong.pdk.private.phases" | ||
|
||
|
||
local ngx = ngx | ||
local check_phase = phase_checker.check | ||
|
||
|
||
local PHASES = phase_checker.phases | ||
local ROUTER_PHASES = phase_checker.new(PHASES.access, | ||
PHASES.header_filter, | ||
PHASES.body_filter, | ||
PHASES.log) | ||
|
||
local function new(self) | ||
local _ROUTER = {} | ||
|
||
|
||
--- | ||
-- Returns the current `route` entity. The request was matched against this | ||
-- route. | ||
-- | ||
-- @function kong.router.get_route | ||
-- @phases access, header_filter, body_filter, log | ||
-- @treturn table the `route` entity. | ||
-- @usage | ||
-- if kong.router.get_route() then | ||
-- -- routed by route & service entities | ||
-- else | ||
-- -- routed by a legacy API entity | ||
-- end | ||
function _ROUTER.get_route() | ||
check_phase(ROUTER_PHASES) | ||
|
||
return ngx.ctx.route | ||
end | ||
|
||
|
||
--- | ||
-- Returns the current `service` entity. The request will be targetted to this | ||
-- upstream service. | ||
-- | ||
-- @function kong.router.get_service | ||
-- @phases access, header_filter, body_filter, log | ||
-- @treturn table the `service` entity. | ||
-- @usage | ||
-- if kong.router.get_service() then | ||
-- -- routed by route & service entities | ||
-- else | ||
-- -- routed by a legacy API entity | ||
-- end | ||
function _ROUTER.get_service() | ||
check_phase(ROUTER_PHASES) | ||
|
||
return ngx.ctx.service | ||
end | ||
|
||
|
||
return _ROUTER | ||
end | ||
|
||
|
||
return { | ||
new = new, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use strict; | ||
use warnings FATAL => 'all'; | ||
use Test::Nginx::Socket::Lua; | ||
use t::Util; | ||
|
||
$ENV{TEST_NGINX_NXSOCK} ||= html_dir(); | ||
|
||
plan tests => repeat_each() * (blocks() * 3); | ||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
=== TEST 1: client.get_credential() returns selected credential | ||
--- http_config eval: $t::Util::HttpConfig | ||
--- config | ||
location = /t { | ||
content_by_lua_block { | ||
ngx.ctx.authenticated_credential = setmetatable({},{ | ||
__tostring = function() return "this credential" end, | ||
}) | ||
local PDK = require "kong.pdk" | ||
local pdk = PDK.new() | ||
ngx.say("credential: ", tostring(pdk.client.get_credential())) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
credential: this credential | ||
--- no_error_log | ||
[error] | ||
=== TEST 2: client.get_service() returns nil if not set | ||
--- http_config eval: $t::Util::HttpConfig | ||
--- config | ||
location = /t { | ||
content_by_lua_block { | ||
ngx.ctx.authenticated_credential = nil | ||
local PDK = require "kong.pdk" | ||
local pdk = PDK.new() | ||
ngx.say("credential: ", tostring(pdk.client.get_credential())) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
credential: nil | ||
--- no_error_log | ||
[error] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use strict; | ||
use warnings FATAL => 'all'; | ||
use Test::Nginx::Socket::Lua; | ||
use t::Util; | ||
|
||
$ENV{TEST_NGINX_NXSOCK} ||= html_dir(); | ||
|
||
plan tests => repeat_each() * (blocks() * 3); | ||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
=== TEST 1: client.get_consumer() returns selected consumer | ||
--- http_config eval: $t::Util::HttpConfig | ||
--- config | ||
location = /t { | ||
content_by_lua_block { | ||
ngx.ctx.authenticated_consumer = setmetatable({},{ | ||
__tostring = function() return "this consumer" end, | ||
}) | ||
local PDK = require "kong.pdk" | ||
local pdk = PDK.new() | ||
ngx.say("consumer: ", tostring(pdk.client.get_consumer())) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
consumer: this consumer | ||
--- no_error_log | ||
[error] | ||
=== TEST 2: client.get_service() returns nil if not set | ||
--- http_config eval: $t::Util::HttpConfig | ||
--- config | ||
location = /t { | ||
content_by_lua_block { | ||
ngx.ctx.authenticated_consumer = nil | ||
local PDK = require "kong.pdk" | ||
local pdk = PDK.new() | ||
ngx.say("consumer: ", tostring(pdk.client.get_consumer())) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
consumer: nil | ||
--- no_error_log | ||
[error] | ||
Oops, something went wrong.