Skip to content

Commit

Permalink
config: root-level Cassandra configuration
Browse files Browse the repository at this point in the history
Remove `databases_available` to have a `cassandra` property at
root-level. root-level properties are simpler to grasp and customize for
users (especially when `databases_available` only have one key.
  • Loading branch information
thibaultcha committed Jan 8, 2016
1 parent e3341eb commit 457fb93
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 105 deletions.
7 changes: 3 additions & 4 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ becomes:
proxy_listen: ...
proxy_listen_ssl: ...
admin_api_listen: ...
databases_available:
cassandra:
contact_points:
- ...
cassandra:
contact_points:
- ...
```
Secondly, you will need to have [Serf](https://www.serfdom.io) installed on your system and available in your `$PATH`.
Expand Down
97 changes: 48 additions & 49 deletions kong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,58 +51,57 @@
# admin_api_port: 8001

######
## Specify which database to use from the databases_available property.
## Specify which database to use. Only "cassandra" is currently available.
# database: cassandra

######
## Databases configuration.
# databases_available:
# cassandra:
######
## Contact points to your Cassandra cluster.
# contact_points:
# - "127.0.0.1:9042"

######
## Name of the keyspace used by Kong. Will be created if it does not exist.
# keyspace: kong

######
## Keyspace options. Set those before running Kong or any migration.
## Those settings will be used to create a keyspace with the desired options
## when first running the migrations.
## See http://docs.datastax.com/en/cql/3.1/cql/cql_reference/create_keyspace_r.html
######
## The name of the replica placement strategy class for the keyspace.
## Can be "SimpleStrategy" or "NetworkTopologyStrategy".
# replication_strategy: SimpleStrategy
######
## For SimpleStrategy only.
## The number of replicas of data on multiple nodes.
# replication_factor: 1
######
## For NetworkTopologyStrategy only.
## The number of replicas of data on multiple nodes in each data center.
# data_centers:
# dc1: 2
# dc2: 3

#####
## Client-to-node TLS options.
## `enabled`: if true, will connect to your Cassandra instance using TLS.
## `verify`: if true, will verify the server certificate using the given CA file.
## `certificate_authority`: an absolute path to the trusted CA certificate in PEM format used to verify the server certificate.
## For additional SSL settings, see the ngx_lua `lua_ssl_*` directives.
# ssl:
# enabled: false
# verify: false
# certificate_authority: "/path/to/cluster-ca-certificate.pem"

######
## Cluster authentication options. Provide a user and a password here if your cluster uses the
## PasswordAuthenticator scheme.
# user: cassandra
# password: cassandra
## Cassandra configuration (keyspace, authentication, client-to-node encryption)
# cassandra:
######
## Contact points to your Cassandra cluster.
# contact_points:
# - "127.0.0.1:9042"

######
## Name of the keyspace used by Kong. Will be created if it does not exist.
# keyspace: kong

######
## Keyspace options. Set those before running Kong or any migration.
## Those settings will be used to create a keyspace with the desired options
## when first running the migrations.
## See http://docs.datastax.com/en/cql/3.1/cql/cql_reference/create_keyspace_r.html
######
## The name of the replica placement strategy class for the keyspace.
## Can be "SimpleStrategy" or "NetworkTopologyStrategy".
# replication_strategy: SimpleStrategy
######
## For SimpleStrategy only.
## The number of replicas of data on multiple nodes.
# replication_factor: 1
######
## For NetworkTopologyStrategy only.
## The number of replicas of data on multiple nodes in each data center.
# data_centers:
# dc1: 2
# dc2: 3

#####
## Client-to-node TLS options.
## `enabled`: if true, will connect to your Cassandra instance using TLS.
## `verify`: if true, will verify the server certificate using the given CA file.
## `certificate_authority`: an absolute path to the trusted CA certificate in PEM format used to verify the server certificate.
## For additional SSL settings, see the ngx_lua `lua_ssl_*` directives.
# ssl:
# enabled: false
# verify: false
# certificate_authority: "/path/to/cluster-ca-certificate.pem"

######
## Cluster authentication options. Provide a user and a password here if your cluster uses the
## PasswordAuthenticator scheme.
# user: cassandra
# password: cassandra

######
## Time (in seconds) for which entities from the database (APIs, plugins configurations...)
Expand Down
12 changes: 4 additions & 8 deletions kong/cli/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ local DEFAULT_ENV_VALUES = {
["proxy_ssl_port"] = 8543,
["admin_api_port"] = 8101,
["dnsmasq_port"] = 8153,
["databases_available"] = {
["cassandra"] = {
["keyspace"] = "kong_tests"
}
["cassandra"] = {
["keyspace"] = "kong_tests"
}
},
nginx = {
Expand All @@ -47,10 +45,8 @@ local DEFAULT_ENV_VALUES = {
},
DEVELOPMENT = {
yaml = {
["databases_available"] = {
["cassandra"] = {
["keyspace"] = "kong_development"
}
["cassandra"] = {
["keyspace"] = "kong_development"
}
},
nginx = {
Expand Down
31 changes: 13 additions & 18 deletions kong/tools/config_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,23 @@ return {
}
}
},
["database"] = {type = "string", default = "cassandra"},
["databases_available"] = {
["database"] = {type = "string", default = "cassandra", enum = {"cassandra"}},
["cassandra"] = {
type = "table",
content = {
["cassandra"] = {
["contact_points"] = {type = "array", default = {"127.0.0.1:9042"}},
["keyspace"] = {type = "string", default = "kong"},
["replication_strategy"] = {type = "string", default = "SimpleStrategy", enum = {"SimpleStrategy", "NetworkTopologyStrategy"}},
["replication_factor"] = {type = "number", default = 1},
["data_centers"] = {type = "table", default = {}},
["username"] = {type = "string", nullable = true},
["password"] = {type = "string", nullable = true},
["ssl"] = {
type = "table",
content = {
["contact_points"] = {type = "array", default = {"127.0.0.1:9042"}},
["keyspace"] = {type = "string", default = "kong"},
["replication_strategy"] = {type = "string", default = "SimpleStrategy", enum = {"SimpleStrategy", "NetworkTopologyStrategy"}},
["replication_factor"] = {type = "number", default = 1},
["data_centers"] = {type = "table", default = {}},
["username"] = {type = "string", nullable = true},
["password"] = {type = "string", nullable = true},
["ssl"] = {
type = "table",
content = {
["enabled"] = {type = "boolean", default = false},
["verify"] = {type = "boolean", default = false},
["certificate_authority"] = {type = "string", nullable = true}
}
}
["enabled"] = {type = "boolean", default = false},
["verify"] = {type = "boolean", default = false},
["certificate_authority"] = {type = "string", nullable = true}
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions kong/tools/config_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ function _M.validate(config)
return false, errors
end

-- Check selected database
if config.databases_available[config.database] == nil then
return false, {database = config.database.." is not listed in databases_available"}
end
-- Perform complex validations here if needed

return true
end
Expand All @@ -110,7 +107,7 @@ function _M.load(config_path)

-- Adding computed properties
config.pid_file = IO.path:join(config.nginx_working_dir, constants.CLI.NGINX_PID)
config.dao_config = config.databases_available[config.database]
config.dao_config = config[config.database]
if config.dns_resolver == "dnsmasq" then
config.dns_resolver = {
address = "127.0.0.1:"..config.dns_resolvers_available.dnsmasq.port,
Expand Down
2 changes: 0 additions & 2 deletions spec/plugins/oauth2/access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ local rex = require "rex_pcre"
-- Load everything we need from the spec_helper
local env = spec_helper.get_env() -- test environment
local dao_factory = env.dao_factory
local configuration = env.configuration
configuration.cassandra = configuration.databases_available[configuration.database].properties

local PROXY_SSL_URL = spec_helper.PROXY_SSL_URL
local PROXY_URL = spec_helper.PROXY_URL
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/dao/cassandra/factory_spec.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local Factory = require "kong.dao.cassandra.factory"
local spec_helpers = require "spec.spec_helpers"
local env = spec_helpers.get_env()
local default_dao_properties = env.configuration.databases_available.cassandra
local default_dao_properties = env.configuration.cassandra

describe("Cassadra factory", function()
describe("get_session_options()", function()
Expand Down
30 changes: 12 additions & 18 deletions spec/unit/tools/config_loader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ describe("Configuration validation", function()
assert.truthy(conf.admin_api_port)
assert.truthy(conf.proxy_port)
assert.truthy(conf.database)
assert.truthy(conf.databases_available)
assert.equal("table", type(conf.databases_available))
assert.truthy(conf.cassandra)

local function check_defaults(conf, conf_defaults)
for k, v in pairs(conf) do
Expand All @@ -47,22 +46,19 @@ describe("Configuration validation", function()
it("should validate various types", function()
local ok, errors = config.validate({
proxy_port = "string",
database = 666,
databases_available = {
cassandra = {
contact_points = "127.0.0.1",
ssl = {
enabled = "false"
}
database = "cassandra",
cassandra = {
contact_points = "127.0.0.1",
ssl = {
enabled = "false"
}
}
})
assert.False(ok)
assert.truthy(errors)
assert.equal("must be a number", errors.proxy_port)
assert.equal("must be a string", errors.database)
assert.equal("must be a array", errors["databases_available.cassandra.contact_points"])
assert.equal("must be a boolean", errors["databases_available.cassandra.ssl.enabled"])
assert.equal("must be a array", errors["cassandra.contact_points"])
assert.equal("must be a boolean", errors["cassandra.ssl.enabled"])
assert.falsy(errors.ssl_cert_path)
assert.falsy(errors.ssl_key_path)
end)
Expand All @@ -73,19 +69,17 @@ describe("Configuration validation", function()
end)
it("should check that the value is contained in `enum`", function()
local ok, errors = config.validate({
databases_available = {
cassandra = {
replication_strategy = "foo"
}
cassandra = {
replication_strategy = "foo"
}
})
assert.False(ok)
assert.equal("must be one of: 'SimpleStrategy, NetworkTopologyStrategy'", errors["databases_available.cassandra.replication_strategy"])
assert.equal("must be one of: 'SimpleStrategy, NetworkTopologyStrategy'", errors["cassandra.replication_strategy"])
end)
it("should validate the selected database property", function()
local ok, errors = config.validate({database = "foo"})
assert.False(ok)
assert.equal("foo is not listed in databases_available", errors.database)
assert.equal("must be one of: 'cassandra'", errors.database)
end)
end)

0 comments on commit 457fb93

Please sign in to comment.