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

Add JSON schemas for the policy configurations #522

Merged
merged 7 commits into from
Dec 5, 2017
Merged
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
4 changes: 3 additions & 1 deletion .busted
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ local default = {
verbose = true,
['shuffle-tests'] = true,
['shuffle-files'] = true,
lpath = path.join(root, 'spec/?.lua;') .. path.join(root, 'gateway/src/?.lua;'),

-- src/?/policy.lua allows us to require apicast.policy.apolicy
lpath = path.join(root, 'spec/?.lua;') .. path.join(root, 'gateway/src/?.lua;gateway/src/?/policy.lua'),
}

if ci then
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

## Added

- Definition of JSON schemas for policy configurations [PR #522](https://github.com/3scale/apicast/pull/522)

## Fixed

- Detecting local rover installation from the CLI [PR #519](https://github.com/3scale/apicast/pull/519)
Expand Down
12 changes: 6 additions & 6 deletions doc/policies.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ replaceable with custom ones.
## Write your own policy

To write your own policy you need to write a Lua module that instantiates a
[Policy](../gateway/src/apicast/policy.lua) and defines a method for each of
the phases where it needs to execute something.
[Policy](../gateway/src/apicast/policy/policy.lua) and defines a method for
each of the phases where it needs to execute something.

Suppose that we wanted to run a policy that logged a message in the `rewrite`
and the `header_filter` phases. This is how our module would look
Expand Down Expand Up @@ -125,10 +125,10 @@ end
In the [policy folder](../gateway/src/apicast/policy) you can find several
policies. These ones are quite simple and can be used as examples to write your
own:
- [Echo](../gateway/src/apicast/policy/echo.lua)
- [Phase logger](../gateway/src/apicast/policy/phase_logger.lua)
- [CORS](../gateway/src/apicast/policy/cors.lua)
- [Headers](../gateway/src/apicast/policy/headers.lua)
- [Echo](../gateway/src/apicast/policy/echo)
- [Phase logger](../gateway/src/apicast/policy/phase_logger)
- [CORS](../gateway/src/apicast/policy/cors)
- [Headers](../gateway/src/apicast/policy/headers)


## Integrate your policies
Expand Down
2 changes: 1 addition & 1 deletion examples/policy_chain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Environment configuration can define the global policy chain. You can provide cu

## Using Echo Policy

[Echo policy](../../gateway/src/apicast/policy/echo.lua) accepts configuration option to terminate the request phase. See the example in [`configuration.lua`](./configuration.lua).
[Echo policy](../../gateway/src/apicast/policy/echo) accepts configuration option to terminate the request phase. See the example in [`configuration.lua`](./configuration.lua).

You can start it as:

Expand Down
3 changes: 2 additions & 1 deletion gateway/bin/apicast
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ if ($rover && !$lua_path) {

chdir $apicast;

$ENV{LUA_PATH} = "$apicast/src/?.lua;${lua_path}";
# src/?/policy.lua allows us to require apicast.policy.apolicy
$ENV{LUA_PATH} = "$apicast/src/?.lua;$apicast/src/?/policy.lua;${lua_path}";
$ENV{PWD} = $cwd;

my @args = ('resty', "$bindir/cli", @ARGV);
Expand Down
3 changes: 2 additions & 1 deletion gateway/conf/nginx.conf.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ http {
log_format time '[$time_local] $host:$server_port $remote_addr:$remote_port "$request" $status $body_bytes_sent ($request_time) $post_action_impact';
access_log off;

lua_package_path ";;{{prefix}}/?.lua;{{prefix}}/src/?.lua";
# src/?/policy.lua allows us to require apicast.policy.apolicy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine now. 👍 Later we can solve it with custom loader.

lua_package_path ";;{{prefix}}/?.lua;{{prefix}}/src/?.lua;{{prefix}}/src/?/policy.lua";

{% if nameservers %}
resolver {{ nameservers | join: " " }};
Expand Down
4 changes: 3 additions & 1 deletion gateway/libexec/boot.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pcall(require, 'luarocks.loader')
package.path = package.path .. ";./src/?.lua"

-- src/?/policy.lua allows us to require apicast.policy.apolicy
package.path = package.path .. ";./src/?.lua;./src/?/policy.lua"

local configuration = require 'apicast.configuration_loader'

Expand Down
36 changes: 36 additions & 0 deletions gateway/src/apicast/policy/cors/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "CORS policy configuration",
"type": "object",
"properties": {
"allow_headers": {
"type": "array",
"items": {
"type": "string"
}
},
"allow_methods": {
"type": "array",
"items": {
"type": "string",
"enum": [
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"PATCH",
"OPTIONS",
"TRACE",
"CONNECT"
]
}
},
"allow_origin": {
"type": "string"
},
"allow_credentials": {
"type": "boolean"
}
}
}
14 changes: 14 additions & 0 deletions gateway/src/apicast/policy/echo/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Echo policy configuration",
"type": "object",
"properties": {
"status": {
"type": "integer"
},
"exit": {
"type": "string",
"enum": ["request", "phase"]
}
}
}
30 changes: 30 additions & 0 deletions gateway/src/apicast/policy/headers/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Headers policy configuration",
"type": "object",
"definitions": {
"commands": {
"type": "array",
"items": {
"type": "object",
"properties": {
"op": {
"type": "string",
"enum": ["add", "set", "push"]
},
"header": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": ["op", "header", "value"]
}
}
},
"properties": {
"request": { "$ref": "#/definitions/commands" },
"response": { "$ref": "#/definitions/commands" }
}
}
File renamed without changes.
3 changes: 2 additions & 1 deletion t/TestAPIcast.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ our $path = $ENV{TEST_NGINX_APICAST_PATH} ||= "$pwd/gateway";
our $spec = "$pwd/spec";
our $servroot = $Test::Nginx::Util::ServRoot;

$ENV{TEST_NGINX_LUA_PATH} = "$path/src/?.lua;;";
# src/?/policy.lua allows us to require apicast.policy.apolicy
$ENV{TEST_NGINX_LUA_PATH} = "$path/src/?.lua;$path/src/?/policy.lua;;";
$ENV{TEST_NGINX_MANAGEMENT_CONFIG} = "$path/conf.d/management.conf";
$ENV{TEST_NGINX_UPSTREAM_CONFIG} = "$path/http.d/upstream.conf";
$ENV{TEST_NGINX_BACKEND_CONFIG} = "$path/conf.d/backend.conf";
Expand Down