From 1ab5d1cf0a3aab864f89bdaff9380e104ad8c0d0 Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Thu, 17 Dec 2015 17:01:28 -0800 Subject: [PATCH] fix(dao) fixes and ensuring DAO supports TLS and auth - The factory now retrieves TLS and authentication options for session and cluster spawning. - The ngx stub is not require anymore inside of the CLI. - Migrations don't use the `all` consistency anymore to allow Kong to start even if all C* nodes are not up. --- bin/kong | 2 -- kong/dao/cassandra/factory.lua | 16 +++++++--------- kong/dao/cassandra/migrations.lua | 16 ++++------------ kong/plugins/rate-limiting/daos.lua | 4 ++-- kong/plugins/response-ratelimiting/daos.lua | 4 ++-- kong/tools/config_defaults.lua | 2 +- kong/tools/ngx_stub.lua | 2 +- kong/tools/printable.lua | 10 ++++++++-- 8 files changed, 25 insertions(+), 31 deletions(-) diff --git a/bin/kong b/bin/kong index 9da5c18eb553..589b197fbc8a 100755 --- a/bin/kong +++ b/bin/kong @@ -49,7 +49,5 @@ elseif not commands[cmd] then os.exit(1) end -require "kong.tools.ngx_stub" - -- Load and execute desired command require(commands[cmd]) diff --git a/kong/dao/cassandra/factory.lua b/kong/dao/cassandra/factory.lua index 020781eb9f6a..66cc83b7a90d 100644 --- a/kong/dao/cassandra/factory.lua +++ b/kong/dao/cassandra/factory.lua @@ -31,19 +31,15 @@ end -- Instantiate a Cassandra Factory and all its DAOs for various entities -- @param `properties` Cassandra properties function CassandraFactory:new(properties, plugins) - local ok, err = cassandra.spawn_cluster { - shm = "cassandra", - prepared_shm = "cassandra_prepared", - contact_points = properties.contact_points - } - if not ok then - error(err) - end - self.properties = properties self.type = "cassandra" self.daos = {} + local ok, err = cassandra.spawn_cluster(self:get_session_options()) + if not ok then + error(err) + end + -- Load core entities DAOs for _, entity in ipairs({"apis", "consumers", "plugins"}) do self:load_daos(require("kong.dao.cassandra."..entity)) @@ -120,6 +116,8 @@ function CassandraFactory:get_session_options() query_options = { prepare = true }, + username = self.properties.username, + password = self.properties.password, ssl_options = { enabled = self.properties.ssl.enabled, verify = self.properties.ssl.verify, diff --git a/kong/dao/cassandra/migrations.lua b/kong/dao/cassandra/migrations.lua index 9fa999ac4181..d11467802b46 100644 --- a/kong/dao/cassandra/migrations.lua +++ b/kong/dao/cassandra/migrations.lua @@ -41,9 +41,7 @@ end -- @return query result -- @return error if any function Migrations:add_migration(migration_name, identifier) - return Migrations.super.execute(self, self.queries.add_migration, {cassandra.list({migration_name}), identifier}, { - consistency = cassandra.consistencies.all - }) + return Migrations.super.execute(self, self.queries.add_migration, {cassandra.list({migration_name}), identifier}) end -- Return all logged migrations with a filter by identifier optionally. Check if keyspace exists before to avoid error during the first migration. @@ -61,13 +59,9 @@ function Migrations:get_migrations(identifier) local rows, err if identifier ~= nil then - rows, err = Migrations.super.execute(self, self.queries.get_migrations, {identifier}, { - consistency = cassandra.consistencies.all - }) + rows, err = Migrations.super.execute(self, self.queries.get_migrations, {identifier}) else - rows, err = Migrations.super.execute(self, self.queries.get_all_migrations, nil, { - consistency = cassandra.consistencies.all - }) + rows, err = Migrations.super.execute(self, self.queries.get_all_migrations) end if err and stringy.find(err.message, "unconfigured columnfamily schema_migrations") ~= nil then @@ -83,9 +77,7 @@ end -- @return query result -- @return error if any function Migrations:delete_migration(migration_name, identifier) - return Migrations.super.execute(self, self.queries.delete_migration, {cassandra.list({migration_name}), identifier}, { - consistency = cassandra.consistencies.all - }) + return Migrations.super.execute(self, self.queries.delete_migration, {cassandra.list({migration_name}), identifier}) end -- Drop the entire keyspace diff --git a/kong/plugins/rate-limiting/daos.lua b/kong/plugins/rate-limiting/daos.lua index 7b5ed06feee9..37ac937851d9 100644 --- a/kong/plugins/rate-limiting/daos.lua +++ b/kong/plugins/rate-limiting/daos.lua @@ -2,8 +2,8 @@ local BaseDao = require "kong.dao.cassandra.base_dao" local cassandra = require "cassandra" local timestamp = require "kong.tools.timestamp" -local ngx_log = ngx.log -local ngx_err = ngx.ERR +local ngx_log = ngx and ngx.log or print +local ngx_err = ngx and ngx.ERR local tostring = tostring local RateLimitingMetrics = BaseDao:extend() diff --git a/kong/plugins/response-ratelimiting/daos.lua b/kong/plugins/response-ratelimiting/daos.lua index c4c8ab19749d..e8cdeb03f507 100644 --- a/kong/plugins/response-ratelimiting/daos.lua +++ b/kong/plugins/response-ratelimiting/daos.lua @@ -2,8 +2,8 @@ local BaseDao = require "kong.dao.cassandra.base_dao" local cassandra = require "cassandra" local timestamp = require "kong.tools.timestamp" -local ngx_log = ngx.log -local ngx_err = ngx.ERR +local ngx_log = ngx and ngx.log or print +local ngx_err = ngx and ngx.ERR local tostring = tostring local ResponseRateLimitingMetrics = BaseDao:extend() diff --git a/kong/tools/config_defaults.lua b/kong/tools/config_defaults.lua index ae6fbb44dd82..7c53249b3e50 100644 --- a/kong/tools/config_defaults.lua +++ b/kong/tools/config_defaults.lua @@ -39,7 +39,7 @@ return { ["replication_strategy"] = {type = "string", default = "SimpleStrategy", enum = {"SimpleStrategy", "NetworkTopologyStrategy"}}, ["replication_factor"] = {type = "number", default = 1}, ["data_centers"] = {type = "table", default = {}}, - ["user"] = {type = "string", nullable = true}, + ["username"] = {type = "string", nullable = true}, ["password"] = {type = "string", nullable = true}, ["ssl"] = { type = "table", diff --git a/kong/tools/ngx_stub.lua b/kong/tools/ngx_stub.lua index b5cabbc798da..ba67851af997 100644 --- a/kong/tools/ngx_stub.lua +++ b/kong/tools/ngx_stub.lua @@ -106,7 +106,7 @@ _G.ngx = { req = {}, ctx = {}, header = {}, - get_phase = function() return "init" end, + get_phase = function() return "not_ngx_lua" end, exit = function() end, say = function() end, log = function() end, diff --git a/kong/tools/printable.lua b/kong/tools/printable.lua index 6228870f2cba..59809fd1b514 100644 --- a/kong/tools/printable.lua +++ b/kong/tools/printable.lua @@ -1,17 +1,23 @@ -- A metatable for pretty printing a table with key=value properties -- -- Example: --- { hello = "world", foo = "bar", baz = {"hello", "world"} } +-- {hello = "world", foo = "bar", baz = {"hello", "world"}} -- Output: -- "hello=world foo=bar, baz=hello,world" +local utils = require "kong.tools.utils" + local printable_mt = {} function printable_mt:__tostring() local t = {} for k, v in pairs(self) do if type(v) == "table" then - v = table.concat(v, ",") + if utils.is_array(v) then + v = table.concat(v, ",") + else + setmetatable(v, printable_mt) + end end table.insert(t, k.."="..tostring(v))