-
Notifications
You must be signed in to change notification settings - Fork 170
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
[THREESCALE-3189] Add maintenance policy #1105
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
879003a
Adding Maintenance Mode policy
6456b50
t: test maintenance mode policy
dd6ebcc
Move maintenance policy to builtin path
davidor a7256c3
policy/maintenance: minor fixes
davidor c0f69af
spec/policy: add specs for the maintenance policy
davidor f0cb126
CHANGELOG: add maintenance mode policy
davidor bcee0a9
policy/maintenance: make content-type header configurable
davidor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Maintenance Mode | ||
|
||
A policy which allows you reject incoming requests with a specified status code | ||
and message. It's useful for maintenance periods or to temporarily block an API. | ||
|
||
## Properties | ||
|
||
| Property | Default | Description | | ||
|------------------------------|-----------------------------------|------------------| | ||
| status (integer, _optional_) | 503 | Response code | | ||
| message (string, _optional_) | Service Unavailable - Maintenance | Response message | | ||
|
||
## Example Configuration | ||
```json | ||
{ | ||
"name": "maintenance-mode", | ||
"configuration": { | ||
"message": "Be back soon..", | ||
"status": 503 | ||
} | ||
} | ||
``` |
29 changes: 29 additions & 0 deletions
29
gateway/src/apicast/policy/maintenance_mode/apicast-policy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"$schema": "http://apicast.io/policy-v1.1/schema#manifest#", | ||
"name": "Maintenance Mode", | ||
"summary": "Rejects incoming requests. Useful for maintenance periods.", | ||
"description": ["A policy which allows you reject incoming requests with a specified status code and message.", | ||
"It's useful for maintenance periods or to temporarily block an API." | ||
], | ||
"version": "builtin", | ||
"configuration": { | ||
"type": "object", | ||
"properties": { | ||
"status": { | ||
"type": "integer", | ||
"description": "HTTP status code to return", | ||
"default": 503 | ||
}, | ||
"message": { | ||
"type": "string", | ||
"description": "HTTP response to return", | ||
"default": "Service Unavailable - Maintenance" | ||
}, | ||
"message_content_type": { | ||
"type": "string", | ||
"description": "Content-Type header for the response", | ||
"default": "text/plain; charset=utf-8" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
return require('maintenance_mode') |
38 changes: 38 additions & 0 deletions
38
gateway/src/apicast/policy/maintenance_mode/maintenance_mode.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
-- This is a simple policy. It allows you reject incoming requests with a | ||
-- specified status code and message. | ||
-- It's useful for maintenance periods or to temporarily block an API | ||
|
||
local _M = require('apicast.policy').new('Maintenance mode', 'builtin') | ||
|
||
local tonumber = tonumber | ||
local new = _M.new | ||
|
||
local default_status_code = 503 | ||
local default_message = "Service Unavailable - Maintenance" | ||
local default_message_content_type = "text/plain; charset=utf-8" | ||
|
||
function _M.new(configuration) | ||
local policy = new(configuration) | ||
|
||
policy.status_code = default_status_code | ||
policy.message = default_message | ||
policy.message_content_type = default_message_content_type | ||
|
||
if configuration then | ||
policy.status_code = tonumber(configuration.status) or policy.status_code | ||
policy.message = configuration.message or policy.message | ||
policy.message_content_type = configuration.message_content_type or policy.message_content_type | ||
end | ||
|
||
return policy | ||
end | ||
|
||
function _M:rewrite() | ||
ngx.header['Content-Type'] = self.message_content_type | ||
ngx.status = self.status_code | ||
ngx.say(self.message) | ||
|
||
return ngx.exit(ngx.status) | ||
eloycoto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
local MaintenancePolicy = require('apicast.policy.maintenance_mode') | ||
|
||
describe('Maintenance mode policy', function() | ||
describe('.rewrite', function() | ||
before_each(function() | ||
stub(ngx, 'say') | ||
stub(ngx, 'exit') | ||
ngx.header = {} | ||
end) | ||
|
||
context('when using the defaults', function() | ||
local maintenance_policy = MaintenancePolicy.new() | ||
|
||
it('returns 503', function() | ||
maintenance_policy:rewrite() | ||
|
||
assert.stub(ngx.exit).was_called_with(503) | ||
end) | ||
|
||
it('returns the default message', function() | ||
maintenance_policy:rewrite() | ||
|
||
assert.stub(ngx.say).was_called_with('Service Unavailable - Maintenance') | ||
end) | ||
|
||
it('returns the default Content-Type header', function() | ||
maintenance_policy:rewrite() | ||
|
||
assert.equals('text/plain; charset=utf-8', ngx.header['Content-Type']) | ||
end) | ||
end) | ||
|
||
context('when using a custom status code', function() | ||
it('returns that status code', function() | ||
local custom_code = 555 | ||
local maintenance_policy = MaintenancePolicy.new( | ||
{ status = custom_code } | ||
) | ||
|
||
maintenance_policy:rewrite() | ||
|
||
assert.stub(ngx.exit).was_called_with(custom_code) | ||
end) | ||
end) | ||
|
||
context('when using a custom message', function() | ||
it('returns that message', function() | ||
local custom_msg = 'Some custom message' | ||
local maintenance_policy = MaintenancePolicy.new( | ||
{ message = custom_msg } | ||
) | ||
|
||
maintenance_policy:rewrite() | ||
|
||
assert.stub(ngx.say).was_called_with(custom_msg) | ||
end) | ||
end) | ||
|
||
context('when using a custom content type', function() | ||
it('sets the Content-Type header accordingly', function() | ||
local custom_content_type = 'application/json' | ||
local maintenance_policy = MaintenancePolicy.new( | ||
{ | ||
message = '{ "msg": "some_msg" }', | ||
message_content_type = custom_content_type | ||
} | ||
) | ||
|
||
|
||
maintenance_policy:rewrite() | ||
|
||
assert.equals('application/json', ngx.header['Content-Type']) | ||
end) | ||
end) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
use lib 't'; | ||
use Test::APIcast::Blackbox 'no_plan'; | ||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: Use maintenance mode using default values | ||
Testing 3 things: | ||
1) Check default status code | ||
2) Check default response message | ||
3) Validates upstream doesn't get the request | ||
|
||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"id": 42, | ||
"backend_version": 1, | ||
"backend_authentication_type": "service_token", | ||
"backend_authentication_value": "token-value", | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "apicast.policy.maintenance_mode" }, | ||
{ "name": "apicast.policy.apicast" } | ||
], | ||
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/", | ||
"proxy_rules": [ | ||
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- upstream | ||
location / { | ||
content_by_lua_block { | ||
local assert = require('luassert') | ||
assert.is_true(false) | ||
} | ||
} | ||
--- request | ||
GET / | ||
--- response_body | ||
Service Unavailable - Maintenance | ||
--- error_code: 503 | ||
--- no_error_log | ||
[error] | ||
|
||
|
||
=== TEST 2: Use maintenance mode using custom values | ||
Testing 3 things: | ||
1) Check custom status code | ||
2) Check custom response message | ||
3) Validates upstream doesn't get request | ||
|
||
--- configuration | ||
{ | ||
"services": [{ | ||
"id": 42, | ||
"backend_version": 1, | ||
"backend_authentication_type": "service_token", | ||
"backend_authentication_value": "token-value", | ||
"proxy": { | ||
"policy_chain": [{ | ||
"name": "apicast.policy.maintenance_mode", | ||
"configuration": { | ||
"message": "Be back soon", | ||
"status": 501 | ||
} | ||
}, | ||
{ | ||
"name": "apicast.policy.apicast" | ||
} | ||
], | ||
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/", | ||
"proxy_rules": [{ | ||
"pattern": "/", | ||
"http_method": "GET", | ||
"metric_system_name": "hits", | ||
"delta": 2 | ||
}] | ||
} | ||
}] | ||
} | ||
--- upstream | ||
location / { | ||
content_by_lua_block { | ||
local assert = require('luassert') | ||
assert.is_true(false) | ||
} | ||
} | ||
--- request | ||
GET / | ||
--- response_body | ||
Be back soon | ||
--- error_code: 501 | ||
--- no_error_log | ||
[error] | ||
|
||
=== TEST 3: Maintenance policy works when placed after the APIcast policy | ||
In this test we need to send the app credentials, because APIcast will check | ||
that they are there before the maintenance policy runs. | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"id": 42, | ||
"backend_version": 1, | ||
"backend_authentication_type": "service_token", | ||
"backend_authentication_value": "token-value", | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "apicast.policy.apicast" }, | ||
{ "name": "apicast.policy.maintenance_mode" } | ||
], | ||
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/", | ||
"proxy_rules": [ | ||
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- upstream | ||
location / { | ||
content_by_lua_block { | ||
local assert = require('luassert') | ||
assert.is_true(false) | ||
} | ||
} | ||
--- request | ||
GET /?user_key=uk | ||
--- response_body | ||
Service Unavailable - Maintenance | ||
--- error_code: 503 | ||
--- no_error_log | ||
[error] | ||
|
||
=== TEST 4: custom content-type | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"id": 42, | ||
"backend_version": 1, | ||
"backend_authentication_type": "service_token", | ||
"backend_authentication_value": "token-value", | ||
"proxy": { | ||
"policy_chain": [ | ||
{ | ||
"name": "apicast.policy.maintenance_mode", | ||
"configuration": { | ||
"message": "{ \"msg\": \"Be back soon\" }", | ||
"message_content_type": "application/json" | ||
} | ||
}, | ||
{ "name": "apicast.policy.apicast" } | ||
], | ||
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/", | ||
"proxy_rules": [ | ||
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- upstream | ||
location / { | ||
content_by_lua_block { | ||
local assert = require('luassert') | ||
assert.is_true(false) | ||
} | ||
} | ||
--- request | ||
GET / | ||
--- response_body | ||
{ "msg": "Be back soon" } | ||
--- response_headers | ||
Content-Type: application/json | ||
--- error_code: 503 | ||
--- no_error_log | ||
[error] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to also set some header in case this is active. So you can have the payload as JSON for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean by setting a header here.
Do you mean returning JSON or plain text according to the value of the
Accept
header of the request?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean setting also Content-Type header when the policy is active. JSON payload is incorrect without proper Content-Type. And using two policies to activate mantenance mode would be weird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
bcee0a9