Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safer order of plugins execution #49

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ install:
- sudo make install
- sudo make dev

script: "make run-integration-tests COVERAGE_FLAG=--coverage FOLDER=spec"
script: "busted --coverage spec/"

after_success:
- luacov-coveralls -i kong
after_success: "luacov-coveralls -i kong"
37 changes: 13 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
KONG_HOME = `pwd`

export SILENT_FLAG ?=
export COVERAGE_FLAG ?=

TESTS_CONF ?= kong_TEST.yml
DEVELOPMENT_CONF ?= kong_DEVELOPMENT.yml

.PHONY: install dev seed drop test coverage run-integration-tests test-web test-proxy test-all
.PHONY: install dev seed drop test coverage test-api test-proxy test-server test-all

install:
@if [ `uname` == "Darwin" ]; then \
Expand All @@ -32,31 +27,25 @@ seed:
drop:
@scripts/db.lua -c $(DEVELOPMENT_CONF) drop

lint:
@luacheck kong*.rockspec

test:
@busted $(COVERAGE_FLAG) spec/unit
@busted spec/unit

coverage:
@rm -f luacov.*
@$(MAKE) test COVERAGE_FLAG=--coverage
@busted --coverage spec/unit
@luacov -c spec/.luacov

lint:
@luacheck kong*.rockspec

run-integration-tests:
@scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) migrate
@bin/kong -c $(TESTS_CONF) start
@while ! [ `ps aux | grep nginx | grep -c -v grep` -gt 0 ]; do sleep 1; done # Wait until nginx starts
@scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) seed
@busted $(COVERAGE_FLAG) $(FOLDER) || (bin/kong stop; scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) reset; exit 1)
@bin/kong stop
@scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) reset

test-web:
@$(MAKE) run-integration-tests FOLDER=spec/web SILENT_FLAG=-s
test-api:
@busted spec/api

test-proxy:
@$(MAKE) run-integration-tests FOLDER=spec/proxy SILENT_FLAG=-s
@busted spec/proxy

test-server:
@busted spec/server

test-all:
@$(MAKE) run-integration-tests FOLDER=spec SILENT_FLAG=-s
@busted spec/
7 changes: 5 additions & 2 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function start {
printf "$(tput setaf 2) [OK]\n$(tput sgr 0)"
else
printf "$(tput setaf 1) [ERROR]\n$(tput sgr 0)"
printf "\nYou can check what error has been returned by looking at the error log file"
exit 1
fi
}
Expand All @@ -86,15 +87,17 @@ function stop {

if [ ! -f $PID ]; then
printf "$(tput setaf 1) [NOT RUNNING]\n$(tput sgr 0)"
exit 1
if [ "$1" = false ] ; then # $1 is true when it's part of a restart
exit 1
fi
else
kill $(cat $PID)
printf "$(tput setaf 2) [OK]\n$(tput sgr 0)"
fi
}

function restart {
stop
stop true
start
}

Expand Down
6 changes: 5 additions & 1 deletion kong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ nginx: |
lua_max_running_timers 4096;
lua_max_pending_timers 16384;

# Cache
lua_shared_dict cache 512m;

# Generic Settings
resolver 8.8.8.8;
charset UTF-8;

Expand Down Expand Up @@ -111,7 +115,7 @@ nginx: |
internal;
content_by_lua '
local utils = require "kong.tools.utils"
utils.show_error(ngx.status, "Ops, an error occurred (╯°□°)╯")';
utils.show_error(ngx.status, "Ops, an unexpected error occurred!")';
}
}

Expand Down
4 changes: 3 additions & 1 deletion scripts/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ if args.COMMAND == "create" then
TEST = {
["keyspace: kong"] = "keyspace: kong_tests",
["lua_package_path \";;\""] = "lua_package_path \""..args.kong.."/src/?.lua;;\"",
["error_log logs/error.log info"] = "error_log logs/error.log debug"
["error_log logs/error.log info"] = "error_log logs/error.log debug",
["listen 8000"] = "listen 8100",
["listen 8001"] = "listen 8101"
},
DEVELOPMENT = {
["keyspace: kong"] = "keyspace: kong_development",
Expand Down
3 changes: 2 additions & 1 deletion site/app/_includes/pages/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ A typical `kong.yml` file looks like:
# Specify the DAO to use
database: cassandra

# Enabled plugins
plugins_enabled:
- authentication
- ratelimiting
Expand All @@ -111,7 +112,7 @@ databases_available:
keepalive: 60000
```

The `plugins_enabled` array describes the plugins that the server should support system-wide. Then you can configure which plugin to install or to configure using the `/plugins/` API endpoint. Plugins that haven't been added to `plugins_enabled` won't be executed.
The `plugins_enabled` array describes the plugins that the server should support system-wide (the order is irrelevant). Then you can configure which Plugin to install to which API by using the `/plugins/` API endpoint. Plugins that haven't been added to `plugins_enabled` won't be executed.

# Scalability

Expand Down
26 changes: 24 additions & 2 deletions spec/web/web_spec.lua → spec/api/api_spec.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
local spec_helper = require "spec.spec_helpers"
local utils = require "kong.tools.utils"
local cjson = require "cjson"
local kWebURL = "http://localhost:8001/"

local created_ids = {}

local kWebURL = spec_helper.API_URL
local ENDPOINTS = {
{
collection = "apis",
Expand Down Expand Up @@ -67,25 +68,39 @@ local ENDPOINTS = {

describe("Web API #web", function()

setup(function()
spec_helper.prepare_db()
spec_helper.start_kong()
end)

teardown(function()
spec_helper.stop_kong()
spec_helper.reset_db()
end)

describe("/", function()

it("should return Kong's version and a welcome message", function()
local response, status, headers = utils.get(kWebURL)
local body = cjson.decode(response)
assert.are.equal(200, status)
assert.truthy(body.version)
assert.truthy(body.tagline)
end)

end)

for i,v in ipairs(ENDPOINTS) do
for i, v in ipairs(ENDPOINTS) do
describe("#"..v.collection, function()

it("should not create on POST with invalid parameters", function()
if v.collection ~= "accounts" then
local response, status, headers = utils.post(kWebURL.."/"..v.collection.."/", {})
assert.are.equal(400, status)
assert.are.equal(v.error_message, response)
end
end)

it("should create an entity from valid paremeters", function()
-- Replace the IDs
for k,p in pairs(v.entity) do
Expand All @@ -102,6 +117,7 @@ describe("Web API #web", function()
-- Save the ID for later use
created_ids[v.collection] = body.id
end)

it("should GET all entities", function()
local response, status, headers = utils.get(kWebURL.."/"..v.collection.."/")
local body = cjson.decode(response)
Expand All @@ -111,20 +127,23 @@ describe("Web API #web", function()
--assert.are.equal(v.total, body.total)
assert.are.equal(v.total, table.getn(body.data))
end)

it("should GET one entity", function()
local response, status, headers = utils.get(kWebURL.."/"..v.collection.."/"..created_ids[v.collection])
local body = cjson.decode(response)
assert.are.equal(200, status)
assert.truthy(body)
assert.are.equal(created_ids[v.collection], body.id)
end)

it("should return not found on GET", function()
local response, status, headers = utils.get(kWebURL.."/"..v.collection.."/"..created_ids[v.collection].."blah")
local body = cjson.decode(response)
assert.are.equal(404, status)
assert.truthy(body)
assert.are.equal('{"id":"'..created_ids[v.collection]..'blah is an invalid uuid"}', response)
end)

it("should update a created entity on PUT", function()
local data = utils.get(kWebURL.."/"..v.collection.."/"..created_ids[v.collection])
local body = cjson.decode(data)
Expand All @@ -144,15 +163,18 @@ describe("Web API #web", function()
assert.are.equal(v, body[k])
end
end)

end)
end

for i,v in ipairs(ENDPOINTS) do
describe("#"..v.collection, function()

it("should delete an entity on DELETE", function()
local response, status, headers = utils.delete(kWebURL.."/"..v.collection.."/"..created_ids[v.collection])
assert.are.equal(204, status)
end)

end)
end

Expand Down
13 changes: 12 additions & 1 deletion spec/proxy/authentication_plugin_spec.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
local spec_helper = require "spec.spec_helpers"
local utils = require "kong.tools.utils"
local cjson = require "cjson"

local kProxyURL = "http://localhost:8000/"
local kProxyURL = spec_helper.PROXY_URL
local kPostURL = kProxyURL.."/post"
local kGetURL = kProxyURL.."/get"

describe("Authentication Plugin #proxy", function()

setup(function()
spec_helper.prepare_db()
spec_helper.start_kong()
end)

teardown(function()
spec_helper.stop_kong()
spec_helper.reset_db()
end)

describe("Query Authentication", function()

it("should return invalid credentials when the credential value is wrong", function()
Expand Down
13 changes: 12 additions & 1 deletion spec/proxy/core_access_spec.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
local spec_helper = require "spec.spec_helpers"
local utils = require "kong.tools.utils"
local cjson = require "cjson"

local kProxyURL = "http://localhost:8000/"
local kProxyURL = spec_helper.PROXY_URL
local kPostURL = kProxyURL.."/post"
local kGetURL = kProxyURL.."/get"

describe("Proxy API #proxy", function()

setup(function()
spec_helper.prepare_db()
spec_helper.start_kong()
end)

teardown(function()
spec_helper.stop_kong()
spec_helper.reset_db()
end)

describe("Invalid API", function()

it("should return API not found when the API is missing", function()
Expand Down
13 changes: 12 additions & 1 deletion spec/proxy/ratelimiting_plugin_spec.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
local spec_helper = require "spec.spec_helpers"
local utils = require "kong.tools.utils"
local cjson = require "cjson"

local kProxyURL = "http://localhost:8000/"
local kProxyURL = spec_helper.PROXY_URL
local kGetURL = kProxyURL.."/get"

describe("RateLimiting Plugin #proxy", function()

setup(function()
spec_helper.prepare_db()
spec_helper.start_kong()
end)

teardown(function()
spec_helper.stop_kong()
spec_helper.reset_db()
end)

describe("Without authentication (IP address)", function()

it("should get blocked if exceeding limit", function()
Expand Down
Loading