Skip to content

Commit

Permalink
fix(dao) fixes and ensuring DAO supports TLS and auth
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
thibaultcha committed Dec 18, 2015
1 parent 57e5d8a commit 1ab5d1c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 31 deletions.
2 changes: 0 additions & 2 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -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])
16 changes: 7 additions & 9 deletions kong/dao/cassandra/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 4 additions & 12 deletions kong/dao/cassandra/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions kong/plugins/rate-limiting/daos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions kong/plugins/response-ratelimiting/daos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion kong/tools/config_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion kong/tools/ngx_stub.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions kong/tools/printable.lua
Original file line number Diff line number Diff line change
@@ -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))
Expand Down

0 comments on commit 1ab5d1c

Please sign in to comment.