Skip to content

Commit

Permalink
chore(deps) replace lua_uuid with lua-resty-jit-uuid
Browse files Browse the repository at this point in the history
jit-uuid is pure Lua and removes libuuid as a Kong dependency.
  • Loading branch information
thibaultcha committed Jul 30, 2016
1 parent 397550a commit c64ca30
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 48 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ addons:
apt:
packages:
- dnsmasq
- uuid-dev
- net-tools
- libpcre3-dev
- build-essential
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ The main focus of this release is Kong's new CLI. With a simpler configuration f
- correlation-id: new "tracker" generator, identifying requests per worker and connection. [#1288](https://github.com/Mashape/kong/pull/1288)
- request/response-transformer: ability to add strings including colon characters. [#1353](https://github.com/Mashape/kong/pull/1353)

### Removed

- We now use [lua-resty-jit-uuid](https://github.com/thibaultCha/lua-resty-jit-uuid) for UUID generation, which is a pure Lua implementation of [RFC 4122](https://www.ietf.org/rfc/rfc4122.txt). As a result, libuuid is not a dependency of Kong anymore.

### Fixed

- Sensitive configuration settings are not printed to stdout anymore. [#1256](https://github.com/Mashape/kong/issues/1256)
Expand Down
2 changes: 1 addition & 1 deletion kong-0.9.0rc1-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ dependencies = {
"penlight == 1.3.2",
"mediator_lua == 1.1.2",
"lua-resty-http == 0.08",
"lua-resty-jit-uuid == 0.0.4",
"multipart == 0.3",
"version == 0.2",
"lapis == 1.5.1",
"lua-cassandra == 0.5.2",
"pgmoon == 1.6.0",
"lua_uuid == 0.2.0",
"luatz == 0.3",
"lua_system_constants == 0.1.1",
"lua-resty-iputils == 0.2.1",
Expand Down
8 changes: 3 additions & 5 deletions kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
local utils = require "kong.tools.utils"
local responses = require "kong.tools.responses"
local validations = require "kong.dao.schemas_validation"
local app_helpers = require "lapis.application"
local utils = require "kong.tools.utils"
local is_uuid = validations.is_valid_uuid

local _M = {}

function _M.find_api_by_name_or_id(self, dao_factory, helpers)
local filter_keys = {
[is_uuid(self.params.name_or_id) and "id" or "name"] = self.params.name_or_id
[utils.is_valid_uuid(self.params.name_or_id) and "id" or "name"] = self.params.name_or_id
}
self.params.name_or_id = nil

Expand All @@ -26,7 +24,7 @@ end

function _M.find_consumer_by_username_or_id(self, dao_factory, helpers)
local filter_keys = {
[is_uuid(self.params.username_or_id) and "id" or "username"] = self.params.username_or_id
[utils.is_valid_uuid(self.params.username_or_id) and "id" or "username"] = self.params.username_or_id
}
self.params.username_or_id = nil

Expand Down
2 changes: 1 addition & 1 deletion kong/dao/cassandra_db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local timestamp = require "kong.tools.timestamp"
local Errors = require "kong.dao.errors"
local BaseDB = require "kong.dao.base_db"
local utils = require "kong.tools.utils"
local uuid = require "lua_uuid"
local uuid = require "resty.jit-uuid"

local ngx_stub = _G.ngx
_G.ngx = nil
Expand Down
4 changes: 2 additions & 2 deletions kong/dao/model_factory.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local utils = require "kong.tools.utils"
local Errors = require "kong.dao.errors"
local schemas_validation = require "kong.dao.schemas_validation"
local validate = schemas_validation.validate_entity
local valid_uuid = schemas_validation.is_valid_uuid

return setmetatable({}, {
__call = function(_, schema)
Expand Down Expand Up @@ -40,7 +40,7 @@ return setmetatable({}, {
local primary_keys, values, nils = {}, {}, {}
for _, key in pairs(schema.primary_key) do
-- check for uuid here. not all dbs might have ids of type uuid however
if schema.fields[key].type == "id" and not valid_uuid(self[key]) then
if schema.fields[key].type == "id" and not utils.is_valid_uuid(self[key]) then
return nil, nil, nil, self[key].." is not a valid uuid"
end
primary_keys[key] = self[key]
Expand Down
2 changes: 1 addition & 1 deletion kong/dao/postgres_db.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local BaseDB = require "kong.dao.base_db"
local Errors = require "kong.dao.errors"
local uuid = require "lua_uuid"
local uuid = require "resty.jit-uuid"
local utils = require "kong.tools.utils"

local TTL_CLEANUP_INTERVAL = 60 -- 1 minute
Expand Down
8 changes: 1 addition & 7 deletions kong/dao/schemas_validation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,14 @@ function _M.validate_entity(tbl, schema, options)
return errors == nil, errors
end

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) }, '%-').."$"
function _M.is_valid_uuid(uuid)
return uuid and uuid:match(uuid_pattern) ~= nil
end

function _M.is_schema_subset(tbl, schema)
local errors

for k, v in pairs(tbl) do
if schema.fields[k] == nil then
errors = utils.add_error(errors, k, "unknown field")
elseif schema.fields[k].type == "id" and v ~= nil then
if not _M.is_valid_uuid(v) then
if not utils.is_valid_uuid(v) then
errors = utils.add_error(errors, k, v.."is not a valid uuid")
end
end
Expand Down
8 changes: 8 additions & 0 deletions kong/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ _G._KONG = {

local core = require "kong.core.handler"
local Serf = require "kong.serf"
local uuid = require 'resty.jit-uuid'
local utils = require "kong.tools.utils"
local Events = require "kong.core.events"
local singletons = require "kong.singletons"
Expand Down Expand Up @@ -139,6 +140,13 @@ function Kong.init()
end

function Kong.init_worker()
-- it is very important to seed this module in the init_worker phase
-- to avoid duplicated UUID sequences accross workers since jit-uuid
-- uses LuaJIT's math.random().
-- jit-uuid handles unique seeds for multiple workers thanks to
-- ngx.worker.pid().
uuid.seed()

core.init_worker.before()

singletons.dao:init() -- Executes any initialization by the DB
Expand Down
7 changes: 4 additions & 3 deletions kong/plugins/correlation-id/handler.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
-- Copyright (C) Mashape, Inc.

local BasePlugin = require "kong.plugins.base_plugin"
local uuid = require "lua_uuid"
local uuid = require "resty.jit-uuid"
local req_set_header = ngx.req.set_header
local req_get_headers = ngx.req.get_headers
local uuid_v4 = uuid.generate_v4

local CorrelationIdHandler = BasePlugin:extend()

Expand All @@ -16,7 +17,7 @@ local worker_pid = ngx.worker.pid()

local generators = setmetatable({
["uuid"] = function()
return uuid()
return uuid_v4()
end,
["uuid#counter"] = function()
worker_counter = worker_counter + 1
Expand Down Expand Up @@ -44,7 +45,7 @@ end

function CorrelationIdHandler:init_worker()
CorrelationIdHandler.super.init_worker(self)
worker_uuid = uuid()
worker_uuid = uuid_v4()
worker_counter = 0
end

Expand Down
15 changes: 10 additions & 5 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
--

local url = require "socket.url"
local uuid = require "lua_uuid"
local uuid = require "resty.jit-uuid"
local pl_stringx = require "pl.stringx"
local ffi = require "ffi"

local fmt = string.format
local type = type
local pairs = pairs
local ipairs = ipairs
Expand All @@ -18,7 +19,6 @@ local table_sort = table.sort
local table_concat = table.concat
local table_insert = table.insert
local string_find = string.find
local string_format = string.format

ffi.cdef[[
int gethostname(char *name, size_t len);
Expand Down Expand Up @@ -49,11 +49,16 @@ function _M.get_hostname()
return result
end

local v4_uuid = uuid.generate_v4

--- Generates a random unique string
-- @return string The random string (a uuid without hyphens)
function _M.random_string()
return uuid():gsub("-", "")
return v4_uuid():gsub("-", "")
end

function _M.is_valid_uuid(str)
return str == "00000000-0000-0000-0000-000000000000" or uuid.is_valid(str)
end

_M.split = pl_stringx.split
Expand All @@ -72,7 +77,7 @@ local function encode_args_value(key, value, raw)
value = url.unescape(value)
value = url.escape(value)
end
return string_format("%s=%s", key, value)
return fmt("%s=%s", key, value)
else
return key
end
Expand Down Expand Up @@ -169,7 +174,7 @@ end
function _M.table_merge(t1, t2)
if not t1 then t1 = {} end
if not t2 then t2 = {} end

local res = {}
for k,v in pairs(t1) do res[k] = v end
for k,v in pairs(t2) do res[k] = v end
Expand Down
49 changes: 30 additions & 19 deletions spec/01-unit/04-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ describe("Utils", function()
assert.truthy(utils.get_hostname())
end)

describe("is_valid_uuid()", function()
it("validates UUIDs from jit-uuid", function()
assert.True(utils.is_valid_uuid("cbb297c0-a956-486d-ad1d-f9b42df9465a"))
assert.False(utils.is_valid_uuid("cbb297c0-a956-486d-dd1d-f9b42df9465a")) -- invalid variant
assert.False(utils.is_valid_uuid("cbb297c0-a956486d-ad1d-f9b42df9465a"))
end)
it("considers the null UUID a valid one", function()
-- we use the null UUID for plugins' consumer_id when none is set
assert.True(utils.is_valid_uuid("00000000-0000-0000-0000-000000000000"))
end)
end)

describe("https_check", function()

local old_ngx
local headers = {}

setup(function()
old_ngx = ngx
_G.ngx = {
Expand All @@ -22,68 +33,68 @@ describe("Utils", function()
}
}
end)

teardown(function()
_G.ngx = old_ngx
end)

describe("without X-Forwarded-Proto header", function()
setup(function()
headers["x-forwarded-proto"] = nil
end)

it("should validate an HTTPS scheme", function()
ngx.var.scheme = "hTTps" -- mixed casing to ensure case insensitiveness
assert.is.truthy(utils.check_https())
end)

it("should invalidate non-HTTPS schemes", function()
ngx.var.scheme = "hTTp"
ngx.var.scheme = "hTTp"
assert.is.falsy(utils.check_https())
ngx.var.scheme = "something completely different"
assert.is.falsy(utils.check_https())
end)

it("should invalidate non-HTTPS schemes with proto header allowed", function()
ngx.var.scheme = "hTTp"
ngx.var.scheme = "hTTp"
assert.is.falsy(utils.check_https(true))
end)
end)

describe("with X-Forwarded-Proto header", function()

teardown(function()
headers["x-forwarded-proto"] = nil
end)

it("should validate any scheme with X-Forwarded_Proto as HTTPS", function()
headers["x-forwarded-proto"] = "hTTPs" -- check mixed casing for case insensitiveness
ngx.var.scheme = "hTTps"
ngx.var.scheme = "hTTps"
assert.is.truthy(utils.check_https(true))
ngx.var.scheme = "hTTp"
ngx.var.scheme = "hTTp"
assert.is.truthy(utils.check_https(true))
ngx.var.scheme = "something completely different"
assert.is.truthy(utils.check_https(true))
end)

it("should validate only https scheme with X-Forwarded_Proto as non-HTTPS", function()
headers["x-forwarded-proto"] = "hTTP"
ngx.var.scheme = "hTTps"
ngx.var.scheme = "hTTps"
assert.is.truthy(utils.check_https(true))
ngx.var.scheme = "hTTp"
ngx.var.scheme = "hTTp"
assert.is.falsy(utils.check_https(true))
ngx.var.scheme = "something completely different"
assert.is.falsy(utils.check_https(true))
end)

it("should return an error with multiple X-Forwarded_Proto headers", function()
headers["x-forwarded-proto"] = { "hTTP", "https" }
ngx.var.scheme = "hTTps"
assert.is.truthy(utils.check_https(true))
ngx.var.scheme = "hTTp"
assert.are.same({ nil, "Only one X-Forwarded-Proto header allowed" }, { utils.check_https(true) })
end)
end)
end)
end)

describe("string", function()
Expand Down
2 changes: 1 addition & 1 deletion spec/02-integration/02-dao/04-constraints_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ helpers.for_each_dao(function(kong_config)
end)

describe("FOREIGN constraints", function()
local uuid = require "lua_uuid"
local uuid = require "resty.jit-uuid"

it("not insert plugin if invalid API foreign key", function()
plugin_fixture.api_id = uuid()
Expand Down
2 changes: 1 addition & 1 deletion spec/03-plugins/14-response-ratelimiting/02-daos_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local uuid = require "lua_uuid"
local uuid = require "resty.jit-uuid"
local helpers = require "spec.helpers"
local timestamp = require "kong.tools.timestamp"

Expand Down
2 changes: 1 addition & 1 deletion spec/03-plugins/98-rate-limiting/02-daos_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local uuid = require "lua_uuid"
local uuid = require "resty.jit-uuid"
local helpers = require "spec.helpers"
local timestamp = require "kong.tools.timestamp"

Expand Down

0 comments on commit c64ca30

Please sign in to comment.