Skip to content
This repository was archived by the owner on Nov 9, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## UNRELEASED

### Added

- Ability to remove headers using a pattern


## 1.3.0

### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ You can combine `consumer_id` and `service_id` in the same request, to furthermo
| `consumer_id` | | The id of the Consumer which this plugin will target.
| `config.http_method` | | Changes the HTTP method for the upstream request
| `config.remove.headers` | | List of header names. Unset the headers with the given name.
| `config.remove.headers_pattern` | | List of header patterns. Unset the headers when the pattern matches the given name.
| `config.remove.querystring` | | List of querystring names. Remove the querystring if it is present.
| `config.remove.body` | | List of parameter names. Remove the parameter if and only if content-type is one the following [`application/json`,`multipart/form-data`, `application/x-www-form-urlencoded`] and parameter is present.
| `config.replace.headers` | | List of headername:value pairs. If and only if the header is already set, replace its old value with the new one. Ignored if the header is not already set.
Expand Down
13 changes: 12 additions & 1 deletion kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,25 @@ local function transform_headers(conf)
headers.host = nil

-- Remove header(s)
for _, name, value in iter(conf.remove.headers) do
for _, name, _ in iter(conf.remove.headers) do
name = name:lower()
if headers[name] then
headers[name] = nil
headers_to_remove[name] = true
end
end

for _, pattern, _ in iter(conf.remove.headers_pattern) do
pattern = pattern:lower()
for name, _ in pairs(headers) do
local match = name:find(pattern)
if match then
headers[name] = nil
headers_to_remove[name] = true
end
end
end

-- Rename headers(s)
for _, old_name, new_name in iter(conf.rename.headers) do
old_name = old_name:lower()
Expand Down
9 changes: 4 additions & 5 deletions kong/plugins/request-transformer/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@ local headers_array = {
elements = { type = "string", custom_validator = validate_headers },
}


local strings_array_record = {
local remove_array_record = {
type = "record",
fields = {
{ body = strings_array },
{ headers = headers_array },
{ headers_pattern = strings_array },
{ querystring = strings_array },
},
}
}


local colon_strings_array = {
type = "array",
default = {},
Expand Down Expand Up @@ -117,7 +116,7 @@ return {
type = "record",
fields = {
{ http_method = typedefs.http_method },
{ remove = strings_array_record },
{ remove = remove_array_record },
{ rename = colon_rename_strings_array_record },
{ replace = colon_strings_array_record_plus_uri },
{ add = colon_strings_array_record },
Expand Down
13 changes: 11 additions & 2 deletions spec/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function()
name = "request-transformer",
config = {
remove = {
headers = {"x-to-remove"},
headers = {"x-to-remove", "uber-trace-id"},
headers_pattern = {"^uberctx-"},
querystring = {"q1"},
body = {"toremoveform"}
}
Expand Down Expand Up @@ -483,13 +484,21 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function()
headers = {
host = "test4.com",
["x-to-remove"] = "true",
["x-another-header"] = "true"
["x-another-header"] = "true",
["uber-trace-id"] = "true",
["uberctx-foo"] = "true",
["uberctx-bar"] = "true",
["x-uberctx-spam"] = "true",
}
})
assert.response(r).has.status(200)
assert.response(r).has.jsonbody()
assert.request(r).has.no.header("x-to-remove")
assert.request(r).has.no.header("uber-trace-id")
assert.request(r).has.no.header("uberctx-foo")
assert.request(r).has.no.header("uberctx-bar")
assert.request(r).has.header("x-another-header")
assert.request(r).has.header("x-uberctx-spam")
end)
it("parameters on url encoded form POST", function()
local r = assert(client:send {
Expand Down
2 changes: 2 additions & 0 deletions spec/03-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe("Plugin: request-transformer (API) [#" .. strategy .. "]", function()
config = {
remove = {
headers = {"just_a_key"},
headers_pattern = {"^just_a_prefix_"},
body = {"just_a_key"},
querystring = {"just_a_key"},
},
Expand All @@ -49,6 +50,7 @@ describe("Plugin: request-transformer (API) [#" .. strategy .. "]", function()
assert.response(res).has.status(201)
local body = assert.response(res).has.jsonbody()
assert.equals("just_a_key", body.config.remove.headers[1])
assert.equals("^just_a_prefix_", body.config.remove.headers_pattern[1])
assert.equals("just_a_key", body.config.remove.body[1])
assert.equals("just_a_key", body.config.remove.querystring[1])
end)
Expand Down