Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] drop pcre regexes for lua patterns #209

Merged
merged 5 commits into from
May 9, 2015
Merged
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
8 changes: 5 additions & 3 deletions kong-0.2.1-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ description = {
}
dependencies = {
"lua ~> 5.1",
"luasec ~> 0.5-2",

"uuid ~> 0.2-1",
"luatz ~> 0.3-1",
"yaml ~> 1.1.1-1",
"luasec ~> 0.5-2",
"lapis ~> 1.1.0-1",
"stringy ~> 0.4-1",
"cassandra ~> 0.5-7",
"multipart ~> 0.1-2",
"lua-path ~> 0.2.3-1",
"lua-cjson ~> 2.1.0-1",
"luasocket ~> 2.0.2-5",
"ansicolors ~> 1.0.2-3",

"luasocket ~> 2.0.2-5",
"lrexlib-pcre ~> 2.7.2-1",
"multipart ~> 0.1-2"
"lua-llthreads2 ~> 0.1.3-1"
}
build = {
type = "builtin",
Expand Down
6 changes: 3 additions & 3 deletions kong/dao/cassandra/base_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ local stringy = require "stringy"
local Object = require "classic"
local utils = require "kong.tools.utils"
local uuid = require "uuid"
local rex = require "rex_pcre"

local cassandra_constants = require "cassandra.constants"
local error_types = constants.DATABASE_ERROR_TYPES
Expand Down Expand Up @@ -183,9 +182,10 @@ function BaseDao:_close_session(session)
end
end

local pattern = "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"
local digit = "[0-9a-f]"
local uuid_pattern = "^"..table.concat({ digit:rep(8), digit:rep(4), digit:rep(4), digit:rep(4), digit:rep(12) }, '%-').."$"
local function is_valid_uuid(uuid)
return rex.match(uuid, pattern) ~= nil
return uuid and uuid:match(uuid_pattern) ~= nil
end

-- Build the array of arguments to pass to lua-resty-cassandra :execute method.
Expand Down
5 changes: 2 additions & 3 deletions kong/dao/schemas.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local rex = require "rex_pcre"
local utils = require "kong.tools.utils"
local constants = require "kong.constants"

Expand Down Expand Up @@ -69,7 +68,7 @@ function _M.validate(t, schema, is_update)

-- Check field against a regex if specified
elseif t[column] ~= nil and v.regex then
if not rex.match(t[column], v.regex) then
if not ngx.re.match(t[column], v.regex) then
errors = utils.add_error(errors, column, column.." has an invalid value")
end

Expand Down Expand Up @@ -109,7 +108,7 @@ function _M.validate(t, schema, is_update)
end

-- Check for unexpected fields in the entity
for k,v in pairs(t) do
for k, v in pairs(t) do
if schema[k] == nil then
errors = utils.add_error(errors, k, k.." is an unknown field")
end
Expand Down
11 changes: 6 additions & 5 deletions kong/tools/faker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Faker.FIXTURES = {
{ name = "API TESTS 3", public_dns = "test3.com", target_url = "http://mockbin.com" },
{ name = "API TESTS 4", public_dns = "test4.com", target_url = "http://mockbin.com" },
{ name = "API TESTS 5", public_dns = "test5.com", target_url = "http://mockbin.com" },

{ name = "API TESTS 6", public_dns = "cors1.com", target_url = "http://mockbin.com" },
{ name = "API TESTS 7", public_dns = "cors2.com", target_url = "http://mockbin.com" },
{ name = "API TESTS 8 (logging)", public_dns = "logging.com", target_url = "http://mockbin.com" },

{ name = "API TESTS 8 (dns)", public_dns = "dns1.com", target_url = "http://127.0.0.1:7771" },
{ name = "API TESTS 9 (dns)", public_dns = "dns2.com", target_url = "http://localhost:7771" },
Expand All @@ -53,9 +53,6 @@ Faker.FIXTURES = {
plugin_configuration = {
-- API 1
{ name = "keyauth", value = { key_names = { "apikey" }}, __api = 1 },
{ name = "tcplog", value = { host = "127.0.0.1", port = 7777 }, __api = 1 },
{ name = "udplog", value = { host = "127.0.0.1", port = 8888 }, __api = 1 },
{ name = "filelog", value = {}, __api = 1 },
-- API 2
{ name = "basicauth", value = {}, __api = 2 },
-- API 3
Expand All @@ -80,7 +77,11 @@ Faker.FIXTURES = {
headers = "origin, type, accepts",
exposed_headers = "x-auth-token",
max_age = 23,
credentials = true }, __api = 7 }
credentials = true }, __api = 7 },
-- API 8
{ name = "tcplog", value = { host = "127.0.0.1", port = 7777 }, __api = 8 },
{ name = "udplog", value = { host = "127.0.0.1", port = 8888 }, __api = 8 },
{ name = "filelog", value = {}, __api = 8 },
}
}

Expand Down
48 changes: 5 additions & 43 deletions kong/tools/http_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,6 @@ local cjson = require "cjson"

local _M = {}

-- Builds a querystring from a table, separated by `&`
-- @param tab The key/value parameters
-- @param key The parent key if the value is multi-dimensional (optional)
-- @return a string representing the built querystring
local function build_query(tab, key)
if ngx then
return ngx.encode_args(tab)
else
local query = {}
local keys = {}

for k in pairs(tab) do
keys[#keys+1] = k
end

table.sort(keys)

for _,name in ipairs(keys) do
local value = tab[name]
if key then
name = string.format("%s[%s]", tostring(key), tostring(name))
end
if type(value) == "table" then
query[#query+1] = build_query(value, name)
else
value = tostring(value)
if value ~= "" then
query[#query+1] = string.format("%s=%s", name, value)
else
query[#query+1] = name
end
end
end

return table.concat(query, "&")
end
end

local function http_call(options)
-- Set Host header accordingly
if not options.headers["host"] then
Expand All @@ -67,7 +29,7 @@ function _M.get(url, querystring, headers)
if not headers then headers = {} end

if querystring then
url = string.format("%s?%s", url, build_query(querystring))
url = string.format("%s?%s", url, ngx.encode_args(querystring))
end

return http_call {
Expand All @@ -82,7 +44,7 @@ function _M.post(url, form, headers)
if not headers then headers = {} end
if not form then form = {} end

local body = build_query(form)
local body = ngx.encode_args(form)
headers["content-length"] = string.len(body)
if not headers["content-type"] then
headers["content-type"] = "application/x-www-form-urlencoded"
Expand Down Expand Up @@ -148,7 +110,7 @@ function _M.delete(url, querystring, headers)
if not headers then headers = {} end

if querystring then
url = string.format("%s?%s", url, build_query(querystring))
url = string.format("%s?%s", url, ngx.encode_args(querystring))
end

return http_call {
Expand All @@ -163,7 +125,7 @@ function _M.options(url, querystring, headers)
if not headers then headers = {} end

if querystring then
url = string.format("%s?%s", url, build_query(querystring))
url = string.format("%s?%s", url, ngx.encode_args(querystring))
end

return http_call {
Expand All @@ -173,4 +135,4 @@ function _M.options(url, querystring, headers)
}
end

return _M
return _M
4 changes: 2 additions & 2 deletions kong/tools/io.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local constants = require "kong.constants"
local path = require("path").new("/")
local yaml = require "yaml"
local path = require("path").new("/")
local stringy = require "stringy"
local constants = require "kong.constants"

local _M = {}

Expand Down
26 changes: 12 additions & 14 deletions spec/integration/proxy/realip_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ local yaml = require "yaml"
local IO = require "kong.tools.io"
local stringy = require "stringy"
local uuid = require "uuid"
local rex = require "rex_pcre"

-- This is important to seed the UUID generator
uuid.seed()
Expand All @@ -29,10 +28,16 @@ describe("Real IP", function()
local uuid,_ = string.gsub(uuid(), "-", "")

-- Making the request
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test1.com", ["X-Forwarded-For"] = "4.4.4.4, 1.1.1.1, 5.5.5.5", file_log_uuid = uuid})
local response, status = http_client.get(STUB_GET_URL, nil,
{
host = "logging.com",
["X-Forwarded-For"] = "4.4.4.4, 1.1.1.1, 5.5.5.5",
file_log_uuid = uuid
}
)
assert.are.equal(200, status)

-- Reading the log file and finding the entry
-- Reading the log file and finding the line with the entry
local configuration = yaml.load(IO.read_file(TEST_CONF))
assert.truthy(configuration)
local error_log = IO.read_file(configuration.nginx_working_dir.."/logs/error.log")
Expand All @@ -46,18 +51,11 @@ describe("Real IP", function()
end
assert.truthy(line)

-- Matching the Json
local iterator, iter_err = rex.gmatch(line, "\\s+({.+})\\s+")
if not iterator then
error(iter_err)
end
local m, err = iterator()
if err then
error(err)
end
assert.truthy(m)
-- Retrieve the JSON part of the line
local json_str = line:match("(%{.*%})")
assert.truthy(json_str)

local log_message = cjson.decode(m)
local log_message = cjson.decode(json_str)
assert.are.same("4.4.4.4", log_message.ip)
assert.are.same(uuid, log_message.request.headers.file_log_uuid)
end)
Expand Down
41 changes: 41 additions & 0 deletions spec/ngx_stub.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local reg = require "rex_pcre"

_G.ngx = {
time = function() return os.time() end, -- cassandra.lua
re = {
match = reg.match
},
-- Builds a querystring from a table, separated by `&`
-- @param tab The key/value parameters
-- @param key The parent key if the value is multi-dimensional (optional)
-- @return a string representing the built querystring
encode_args = function(tab, key)
local query = {}
local keys = {}

for k in pairs(tab) do
keys[#keys+1] = k
end

table.sort(keys)

for _, name in ipairs(keys) do
local value = tab[name]
if key then
name = string.format("%s[%s]", tostring(key), tostring(name))
end
if type(value) == "table" then
query[#query+1] = build_query(value, name)
else
value = tostring(value)
if value ~= "" then
query[#query+1] = string.format("%s=%s", name, value)
else
query[#query+1] = name
end
end
end

return table.concat(query, "&")
end
}
29 changes: 11 additions & 18 deletions spec/plugins/logging_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local yaml = require "yaml"
local IO = require "kong.tools.io"
local uuid = require "uuid"
local stringy = require "stringy"
local rex = require "rex_pcre"

-- This is important to seed the UUID generator
uuid.seed()
Expand Down Expand Up @@ -64,7 +63,7 @@ describe("Logging Plugins", function()
local thread = start_tcp_server() -- Starting the mock TCP server

-- Making the request
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test1.com"})
local response, status, headers = http_client.get(STUB_GET_URL, nil, { host = "logging.com" })
assert.are.equal(200, status)

-- Getting back the TCP server input
Expand All @@ -81,7 +80,7 @@ describe("Logging Plugins", function()
local thread = start_udp_server() -- Starting the mock TCP server

-- Making the request
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test1.com"})
local response, status = http_client.get(STUB_GET_URL, nil, { host = "logging.com" })
assert.are.equal(200, status)

-- Getting back the TCP server input
Expand All @@ -95,13 +94,15 @@ describe("Logging Plugins", function()
end)

it("should log to file", function()
local uuid,_ = string.gsub(uuid(), "-", "")
local uuid = string.gsub(uuid(), "-", "")

-- Making the request
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test1.com", file_log_uuid = uuid})
local response, status = http_client.get(STUB_GET_URL, nil,
{ host = "logging.com", file_log_uuid = uuid }
)
assert.are.equal(200, status)

-- Reading the log file and finding the entry
-- Reading the log file and finding the line with the entry
local configuration = yaml.load(IO.read_file(TEST_CONF))
assert.truthy(configuration)
local error_log = IO.read_file(configuration.nginx_working_dir.."/logs/error.log")
Expand All @@ -115,22 +116,14 @@ describe("Logging Plugins", function()
end
assert.truthy(line)

-- Matching the Json
local iterator, iter_err = rex.gmatch(line, "\\s+({.+})\\s+")
if not iterator then
error(iter_err)
end
local m, err = iterator()
if err then
error(err)
end
assert.truthy(m)
-- Retrieve the JSON part of the line
local json_str = line:match("(%{.*%})")
assert.truthy(json_str)

local log_message = cjson.decode(m)
local log_message = cjson.decode(json_str)
assert.are.same("127.0.0.1", log_message.ip)
assert.are.same(uuid, log_message.request.headers.file_log_uuid)
end)

end)

end)
2 changes: 2 additions & 0 deletions spec/spec_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ local TEST_CONF_FILE = "kong_TEST.yml"
local TEST_PROXY_URL = "http://localhost:8100"
local TEST_API_URL = "http://localhost:8101"

require "spec.ngx_stub"

local _M = {}

_M.API_URL = TEST_API_URL
Expand Down
2 changes: 2 additions & 0 deletions spec/unit/schemas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local schemas = require "kong.dao.schemas"
local constants = require "kong.constants"
local validate = schemas.validate

require "spec.ngx_stub"

describe("Schemas", function()

it("should alias lua types to database types", function()
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/statics_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("Static files", function()

describe("Configuration", function()

it("should parse a correct configuration", function()
it("should equal to this template to make sure no errors are pushed in the default config", function()
local configuration = IO.read_file(spec_helper.DEFAULT_CONF_FILE)

assert.are.same([[
Expand Down
Loading