Skip to content

Commit

Permalink
Merge branch 'hotfix/cassandra-auth' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Feb 3, 2016
2 parents 2cfc642 + a5bd9a5 commit 75f85ad
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion kong-0.6.0-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies = {
"yaml ~> 1.1.2-1",
"lapis ~> 1.3.1-1",
"stringy ~> 0.4-1",
"lua-cassandra ~> 0.4.2-0",
"lua-cassandra ~> 0.5.0",
"multipart ~> 0.2-1",
"lua-path ~> 0.2.3-1",
"lua-cjson ~> 2.1.0-1",
Expand Down
4 changes: 4 additions & 0 deletions kong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
## Name of the keyspace used by Kong. Will be created if it does not exist.
# keyspace: kong

#####
## Connection and reading timeout (in ms).
# timeout: 5000

######
## Keyspace options. Set those before running Kong or any migration.
## Those settings will be used to create a keyspace with the desired options
Expand Down
19 changes: 10 additions & 9 deletions kong/dao/cassandra/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ local stringy = require "stringy"
local Object = require "classic"
local utils = require "kong.tools.utils"

if ngx ~= nil and type(ngx.get_phase) == "function" and ngx.get_phase() == "init" and not ngx.stub then
cassandra.set_log_level("INFO")
else
cassandra.set_log_level("QUIET")
end

local CassandraFactory = Object:extend()

-- Shorthand for accessing one of the underlying DAOs
Expand All @@ -36,6 +30,10 @@ function CassandraFactory:new(properties, plugins, spawn_cluster, events_handler
self.type = "cassandra"
self.daos = {}

if properties.username and properties.password then
self.properties.auth = cassandra.auth.PlainTextProvider(properties.username, properties.password)
end

if spawn_cluster then
local ok, err = cassandra.spawn_cluster(self:get_session_options())
if not ok then
Expand Down Expand Up @@ -119,13 +117,16 @@ function CassandraFactory:get_session_options()
query_options = {
prepare = true
},
username = self.properties.username,
password = self.properties.password,
socket_options = {
connect_timeout = self.properties.timeout,
read_timeout = self.properties.timeout
},
ssl_options = {
enabled = self.properties.ssl.enabled,
verify = self.properties.ssl.verify,
ca = self.properties.ssl.certificate_authority
}
},
auth = self.properties.auth
}
end

Expand Down
16 changes: 10 additions & 6 deletions kong/dao/cassandra/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ function Migrations:new(properties, events_handler)
Migrations.super.new(self, properties, events_handler)
end

function Migrations:execute(query, args, keyspace)
return Migrations.super.execute(self, query, args, {prepare = false}, keyspace)
end

function Migrations:keyspace_exists(keyspace)
local rows, err = Migrations.super.execute(self, self.queries.get_keyspace, {self.properties.keyspace}, nil, "system")
local rows, err = self:execute(self.queries.get_keyspace, {self.properties.keyspace}, "system")
if err then
return nil, err
else
Expand All @@ -41,7 +45,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})
return self:execute(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 @@ -59,9 +63,9 @@ function Migrations:get_migrations(identifier)

local rows, err
if identifier ~= nil then
rows, err = Migrations.super.execute(self, self.queries.get_migrations, {identifier})
rows, err = self:execute(self.queries.get_migrations, {identifier})
else
rows, err = Migrations.super.execute(self, self.queries.get_all_migrations)
rows, err = self:execute(self.queries.get_all_migrations)
end

if err and stringy.find(err.message, "unconfigured columnfamily schema_migrations") ~= nil then
Expand All @@ -77,14 +81,14 @@ 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})
return self:execute(self.queries.delete_migration, {cassandra.list({migration_name}), identifier})
end

-- Drop the entire keyspace
-- @param `keyspace` Name of the keyspace to drop
-- @return query result
function Migrations:drop_keyspace(keyspace)
return Migrations.super.execute(self, string.format("DROP keyspace \"%s\"", keyspace))
return self:execute(string.format("DROP keyspace \"%s\"", keyspace))
end

function Migrations:drop()
Expand Down
1 change: 1 addition & 0 deletions kong/tools/config_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ return {
content = {
["contact_points"] = {type = "array", default = {"127.0.0.1:9042"}},
["keyspace"] = {type = "string", default = "kong"},
["timeout"] = {type = "number", default = 5000},
["replication_strategy"] = {type = "string", default = "SimpleStrategy", enum = {"SimpleStrategy", "NetworkTopologyStrategy"}},
["replication_factor"] = {type = "number", default = 1},
["data_centers"] = {type = "table", default = {}},
Expand Down
24 changes: 22 additions & 2 deletions spec/unit/dao/cassandra/factory_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Factory = require "kong.dao.cassandra.factory"
local utils = require "kong.tools.utils"
local cassandra = require "cassandra"
local spec_helpers = require "spec.spec_helpers"
local env = spec_helpers.get_env()
local default_dao_properties = env.configuration.cassandra
Expand All @@ -24,6 +25,10 @@ describe("Cassadra factory", function()
query_options = {
prepare = true
},
socket_options = {
connect_timeout = 5000,
read_timeout = 5000
},
ssl_options = {
enabled = false,
verify = false
Expand All @@ -46,6 +51,10 @@ describe("Cassadra factory", function()
query_options = {
prepare = true
},
socket_options = {
connect_timeout = 5000,
read_timeout = 5000
},
ssl_options = {
enabled = false,
verify = false
Expand All @@ -68,12 +77,15 @@ describe("Cassadra factory", function()
query_options = {
prepare = true
},
socket_options = {
connect_timeout = 5000,
read_timeout = 5000
},
ssl_options = {
enabled = false,
verify = false
},
username = "cassie",
password = "cassiepwd"
auth = cassandra.auth.PlainTextProvider("cassie", "cassiepwd")
}, options)
end)
it("should accept SSL properties", function()
Expand All @@ -93,6 +105,10 @@ describe("Cassadra factory", function()
query_options = {
prepare = true
},
socket_options = {
connect_timeout = 5000,
read_timeout = 5000
},
ssl_options = {
enabled = false,
verify = true
Expand All @@ -113,6 +129,10 @@ describe("Cassadra factory", function()
query_options = {
prepare = true
},
socket_options = {
connect_timeout = 5000,
read_timeout = 5000
},
ssl_options = {
enabled = true,
verify = true
Expand Down

0 comments on commit 75f85ad

Please sign in to comment.