forked from Kong/kong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core) new rewrite_by_lua handler for plugins
Signed-off-by: Thibault Charbonnier <[email protected]> This handler exposes the Nginx rewrite phase. This handler is called for all loaded plugins (since the rewrite phase is executed prior to API matching). From Kong#2354
- Loading branch information
1 parent
72d0138
commit d89343e
Showing
10 changed files
with
231 additions
and
10 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
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 |
---|---|---|
@@ -1,32 +1,39 @@ | ||
local Object = require "kong.vendor.classic" | ||
local BasePlugin = Object:extend() | ||
|
||
local ngx_log = ngx.log | ||
local DEBUG = ngx.DEBUG | ||
|
||
function BasePlugin:new(name) | ||
self._name = name | ||
end | ||
|
||
function BasePlugin:init_worker() | ||
ngx.log(ngx.DEBUG, " executing plugin \""..self._name.."\": init_worker") | ||
ngx_log(DEBUG, "executing plugin \"", self._name, "\": init_worker") | ||
end | ||
|
||
function BasePlugin:certificate() | ||
ngx.log(ngx.DEBUG, " executing plugin \""..self._name.."\": certificate") | ||
ngx_log(DEBUG, "executing plugin \"", self._name, "\": certificate") | ||
end | ||
|
||
function BasePlugin:rewrite() | ||
ngx_log(DEBUG, "executing plugin \"", self._name, "\": rewrite") | ||
end | ||
|
||
function BasePlugin:access() | ||
ngx.log(ngx.DEBUG, " executing plugin \""..self._name.."\": access") | ||
ngx_log(DEBUG, "executing plugin \"", self._name, "\": access") | ||
end | ||
|
||
function BasePlugin:header_filter() | ||
ngx.log(ngx.DEBUG, " executing plugin \""..self._name.."\": header_filter") | ||
ngx_log(DEBUG, "executing plugin \"", self._name, "\": header_filter") | ||
end | ||
|
||
function BasePlugin:body_filter() | ||
ngx.log(ngx.DEBUG, " executing plugin \""..self._name.."\": body_filter") | ||
ngx_log(DEBUG, "executing plugin \"", self._name, "\": body_filter") | ||
end | ||
|
||
function BasePlugin:log() | ||
ngx.log(ngx.DEBUG, " executing plugin \""..self._name.."\": log") | ||
ngx_log(DEBUG, "executing plugin \"", self._name, "\": log") | ||
end | ||
|
||
return BasePlugin |
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,151 @@ | ||
local helpers = require "spec.helpers" | ||
|
||
describe("OpenResty phases", function() | ||
describe("rewrite_by_lua", function() | ||
describe("enabled on all APIs", function() | ||
local api_client, proxy_client | ||
|
||
setup(function() | ||
-- insert plugin-less api and a global plugin | ||
assert(helpers.dao.apis:insert { | ||
name = "rewrite1", | ||
hosts = { "rewriter1.com" }, | ||
upstream_url = "http://mockbin.org" | ||
}) | ||
assert(helpers.dao.plugins:insert { | ||
name = "rewriter", | ||
config = { | ||
value = "global plugin", | ||
}, | ||
}) | ||
|
||
assert(helpers.start_kong({ | ||
custom_plugins = "rewriter", | ||
})) | ||
|
||
api_client = helpers.admin_client() | ||
proxy_client = helpers.proxy_client() | ||
end) | ||
|
||
teardown(function() | ||
if api_client then api_client:close() end | ||
helpers.stop_kong() | ||
end) | ||
|
||
it("runs", function() | ||
local res = assert(proxy_client:send { | ||
method = "GET", | ||
path = "/request", | ||
headers = { | ||
host = "rewriter1.com", | ||
}, | ||
}) | ||
assert.response(res).has.status(200) | ||
local value = assert.request(res).has.header("rewriter") | ||
assert.equal("global plugin", value) | ||
end) | ||
end) | ||
|
||
describe("enabled on a specific APIs", function() | ||
local api_client, proxy_client | ||
|
||
setup(function() | ||
-- api specific plugin | ||
local api2 = assert(helpers.dao.apis:insert { | ||
name = "rewrite2", | ||
hosts = { "rewriter2.com" }, | ||
upstream_url = "http://mockbin.org" | ||
}) | ||
assert(helpers.dao.plugins:insert { | ||
api_id = api2.id, | ||
name = "rewriter", | ||
config = { | ||
value = "api-specific plugin", | ||
}, | ||
}) | ||
|
||
assert(helpers.start_kong({ | ||
custom_plugins = "rewriter", | ||
})) | ||
|
||
api_client = helpers.admin_client() | ||
proxy_client = helpers.proxy_client() | ||
end) | ||
|
||
teardown(function() | ||
if api_client then api_client:close() end | ||
helpers.stop_kong() | ||
end) | ||
|
||
it("doesn't run", function() | ||
local res = assert(proxy_client:send { | ||
method = "GET", | ||
path = "/request", | ||
headers = { | ||
host = "rewriter2.com", | ||
}, | ||
}) | ||
assert.response(res).has.status(200) | ||
assert.request(res).has.no.header("rewriter") | ||
end) | ||
end) | ||
|
||
describe("enabled on a specific Consumers", function() | ||
local api_client, proxy_client | ||
|
||
setup(function() | ||
-- consumer specific plugin | ||
local api3 = assert(helpers.dao.apis:insert { | ||
name = "rewrite3", | ||
hosts = { "rewriter3.com" }, | ||
upstream_url = "http://mockbin.org" | ||
}) | ||
assert(helpers.dao.plugins:insert { | ||
api_id = api3.id, | ||
name = "key-auth", | ||
}) | ||
local consumer3 = assert(helpers.dao.consumers:insert { | ||
username = "test-consumer", | ||
}) | ||
assert(helpers.dao.keyauth_credentials:insert { | ||
key = "kong", | ||
consumer_id = consumer3.id | ||
}) | ||
assert(helpers.dao.plugins:insert { | ||
consumer_id = consumer3.id, | ||
name = "rewriter", | ||
config = { | ||
value = "consumer-specific plugin", | ||
}, | ||
}) | ||
|
||
assert(helpers.start_kong({ | ||
custom_plugins = "rewriter", | ||
})) | ||
|
||
api_client = helpers.admin_client() | ||
proxy_client = helpers.proxy_client() | ||
end) | ||
|
||
teardown(function() | ||
if api_client then api_client:close() end | ||
helpers.stop_kong() | ||
end) | ||
|
||
it("doesn't run", function() | ||
local res = assert(proxy_client:send { | ||
method = "GET", | ||
path = "/request", | ||
headers = { | ||
host = "rewriter3.com", | ||
apikey = "kong" | ||
}, | ||
}) | ||
assert.response(res).has.status(200) | ||
local value = assert.request(res).has.header("x-consumer-username") | ||
assert.equal("test-consumer", value) | ||
assert.request(res).has.no.header("rewriter") | ||
end) | ||
end) | ||
end) | ||
end) |
19 changes: 19 additions & 0 deletions
19
spec/fixtures/custom_plugins/kong/plugins/rewriter/handler.lua
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,19 @@ | ||
-- a plugin fixture to test running of the rewrite phase handler. | ||
|
||
local BasePlugin = require "kong.plugins.base_plugin" | ||
|
||
local Rewriter = BasePlugin:extend() | ||
|
||
Rewriter.PRIORITY = 1000 | ||
|
||
function Rewriter:new() | ||
Rewriter.super.new(self, "rewriter") | ||
end | ||
|
||
function Rewriter:rewrite(conf) | ||
Rewriter.super.access(self) | ||
|
||
ngx.req.set_header("rewriter", conf.value) | ||
end | ||
|
||
return Rewriter |
5 changes: 5 additions & 0 deletions
5
spec/fixtures/custom_plugins/kong/plugins/rewriter/schema.lua
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,5 @@ | ||
return { | ||
fields = { | ||
value = { typ = "string" } | ||
} | ||
} |