Skip to content

Commit 2055392

Browse files
committed
example policy
1 parent 23cda36 commit 2055392

13 files changed

+298
-0
lines changed

.circleci/config.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
version: 2
2+
jobs:
3+
build:
4+
docker:
5+
- image: quay.io/3scale/s2i-openresty-centos7:1.13.6.1-rover7
6+
working_directory: /opt/app-root/apicast
7+
steps:
8+
- checkout
9+
- restore_cache:
10+
keys:
11+
- rover-{{ arch }}-{{ checksum "Roverfile.lock" }}
12+
- global-{{ arch }}-{{ .Branch }}
13+
- global-{{ arch }}-master
14+
- run: rover install
15+
- save_cache:
16+
key: rover-{{ arch }}-{{ checksum "Roverfile.lock" }}
17+
paths:
18+
- lua_modules
19+
- restore_cache:
20+
keys:
21+
- cpanm-{{ arch }}-{{ checksum "cpanfile" }}
22+
- global-{{ arch }}-{{ .Branch }}
23+
- global-{{ arch }}-master
24+
- run:
25+
command: cpanm --notest --installdeps ./
26+
shell: /usr/libexec/s2i/entrypoint
27+
- save_cache:
28+
key: cpanm-{{ arch }}-{{ checksum "cpanfile" }}
29+
paths:
30+
- ~/perl5
31+
- run:
32+
shell: /usr/libexec/s2i/entrypoint
33+
name: rover exec prove
34+
command: |
35+
mkdir -p tmp/junit
36+
rover exec prove --harness=TAP::Harness::JUnit \
37+
$(circleci tests glob "t/**/*.t" | circleci tests split --split-by=timings --timings-type=filename)
38+
environment:
39+
JUNIT_OUTPUT_FILE: tmp/junit/prove.xml
40+
TEST_NGINX_ERROR_LOG: tmp/prove.log
41+
TEST_NGINX_BINARY: openresty
42+
- save_cache:
43+
key: global-{{ arch }}-{{ .Branch }}
44+
paths:
45+
- lua_modules
46+
- ~/perl5
47+
- store_artifacts:
48+
path: tmp
49+
destination: tmp
50+
- store_test_results:
51+
path: tmp/junit

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
t/servroot
2+
lua_modules

.s2i/bin/assemble

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
echo "---> Copying policy source..."
4+
cp -Rf /tmp/src/. ./

.s2i/bin/run

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
exec apicast

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[![CircleCI](https://circleci.com/gh/3scale/apicast-example-policy.svg?style=svg)](https://circleci.com/gh/3scale/apicast-example-policy)
2+
3+
# APIcast Example Policy
4+
5+
This policy is an example how to make custom policies for APIcast.
6+
7+
8+
## OpenShift
9+
10+
To install this on OpenShift you can use provided template:
11+
12+
```shell
13+
oc new-app -f openshift.yml --param AMP_RELEASE=2.2.0-GA
14+
```
15+
16+
The template creates new ImageStream for images containing this policy.
17+
Then it creates two BuildConfigs: one for building an image to apicast-policy ImageStream
18+
and second one for creating new APIcast image copying just necessary code from that previous image.
19+
20+
21+
# License
22+
23+
MIT

Roverfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
luarocks {
2+
group { 'development', 'test' } {
3+
module { 'apicast' },
4+
-- to be able to run repl as `require('resty.repl').start()`
5+
module { 'lua-resty-repl' },
6+
-- to automatically validate policy manifests
7+
module { 'ljsonschema' },
8+
}
9+
}

Roverfile.lock

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apicast scm-1|ef868e51efe32b68b9c4ba5eaa80d975e289e49e|development,test
2+
argparse 0.5.0-1||development,test
3+
inspect 3.1.1-0||development,test
4+
liquid 0.1.0-1||development,test
5+
ljsonschema 0.1.0-1||development,test
6+
lua-resty-env 0.4.0-1||development,test
7+
lua-resty-execvp 0.1.1-1||development,test
8+
lua-resty-http 0.12-0||development,test
9+
lua-resty-jwt 0.1.11-0||development,test
10+
lua-resty-repl 0.0.6-0|3878f41b7e8f97b1c96919db19dbee9496569dda|development,test
11+
lua-resty-url 0.2.0-1||development,test
12+
luafilesystem 1.7.0-2||development,test
13+
net-url 0.9-1||development,test
14+
nginx-lua-prometheus 0.20171117-4||development,test
15+
penlight 1.5.4-1||development,test
16+
router 2.1-0||development,test

cpanfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requires 'Test::APIcast', '0.11';

openshift.yml

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
apiVersion: v1
2+
kind: Template
3+
metadata:
4+
name: "apicast-example-policy"
5+
message: "APIcast Example Policy"
6+
objects:
7+
8+
- apiVersion: v1
9+
kind: ImageStream
10+
metadata:
11+
annotations:
12+
labels:
13+
app: apicast
14+
name: apicast-policy
15+
16+
- apiVersion: v1
17+
kind: BuildConfig
18+
metadata:
19+
annotations:
20+
labels:
21+
app: apicast
22+
name: apicast-example-policy
23+
spec:
24+
output:
25+
to:
26+
kind: ImageStreamTag
27+
name: apicast-policy:example
28+
source:
29+
git:
30+
uri: https://github.com/3scale/apicast-example-policy.git
31+
ref: 'master'
32+
type: Git
33+
strategy:
34+
type: Source
35+
sourceStrategy:
36+
from:
37+
kind: ImageStreamTag
38+
name: 'amp-apicast:${AMP_RELEASE}'
39+
40+
- apiVersion: v1
41+
kind: BuildConfig
42+
metadata:
43+
annotations:
44+
labels:
45+
app: apicast
46+
name: apicast-custom-policies
47+
spec:
48+
nodeSelector: null
49+
output:
50+
to:
51+
kind: ImageStreamTag
52+
name: 'amp-apicast:latest'
53+
postCommit:
54+
args:
55+
- '--test'
56+
- '--lazy'
57+
command:
58+
- bin/apicast
59+
resources: {}
60+
runPolicy: Serial
61+
source:
62+
images:
63+
- from:
64+
kind: ImageStreamTag
65+
name: 'apicast-policy:example'
66+
paths:
67+
# copy policy source code into the new image
68+
- destinationDir: policies
69+
sourcePath: /opt/app-root/policies/example
70+
# copy also installed dependencies to the policy folder, so they are vendored
71+
# - destinationDir: policies/example/0.1/resty/
72+
# sourcePath: /opt/app-root/src/lua_modules/share/lua/5.1/resty/iputils.lua
73+
type: Dockerfile
74+
dockerfile: |
75+
FROM scratch
76+
COPY . src
77+
78+
strategy:
79+
dockerStrategy:
80+
from:
81+
kind: ImageStreamTag
82+
name: 'amp-apicast:${AMP_RELEASE}'
83+
type: Docker
84+
triggers:
85+
- type: ConfigChange
86+
- type: ImageChange
87+
88+
parameters:
89+
- name: AMP_RELEASE
90+
required: true
91+
description: AMP Version (eg. 2.2.0-GA)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "http://apicast.io/policy-v1/schema#manifest#",
3+
"name": "APIcast Example Policy",
4+
"summary": "This is just an example.",
5+
"description": "This policy is just an example how to write your custom policy.",
6+
"version": "0.1",
7+
"configuration": {
8+
"type": "object",
9+
"properties": { }
10+
}
11+
}

policies/example/0.1/example.lua

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
local setmetatable = setmetatable
2+
3+
local _M = require('apicast.policy').new('Example', '0.1')
4+
local mt = { __index = _M }
5+
6+
function _M.new()
7+
return setmetatable({}, mt)
8+
end
9+
10+
function _M:init()
11+
-- do work when nginx master process starts
12+
end
13+
14+
function _M:init_worker()
15+
-- do work when nginx worker process is forked from master
16+
end
17+
18+
function _M:rewrite()
19+
-- change the request before it reaches upstream
20+
end
21+
22+
function _M:access()
23+
-- ability to deny the request before it is sent upstream
24+
end
25+
26+
function _M:content()
27+
-- can create content instead of connecting to upstream
28+
end
29+
30+
function _M:post_action()
31+
-- do something after the response was sent to the client
32+
end
33+
34+
function _M:header_filter()
35+
-- can change response headers
36+
end
37+
38+
function _M:body_filter()
39+
-- can read and change response body
40+
-- https://github.com/openresty/lua-nginx-module/blob/master/README.markdown#body_filter_by_lua
41+
end
42+
43+
function _M:log()
44+
-- can do extra logging
45+
end
46+
47+
function _M:balancer()
48+
-- use for example require('resty.balancer.round_robin').call to do load balancing
49+
end
50+
51+
return _M

policies/example/0.1/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return require('example')

t/example.t

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
BEGIN {
2+
$ENV{TEST_NGINX_APICAST_BINARY} ||= 'rover exec apicast';
3+
$ENV{APICAST_POLICY_LOAD_PATH} = './policies';
4+
}
5+
6+
use strict;
7+
use warnings FATAL => 'all';
8+
use Test::APIcast::Blackbox 'no_plan';
9+
10+
run_tests();
11+
12+
__DATA__
13+
14+
=== TEST 1: example
15+
The module does not crash without configuration.
16+
--- configuration
17+
{
18+
"services": [
19+
{
20+
"proxy": {
21+
"policy_chain": [
22+
{ "name": "example", "version": "0.1" },
23+
{ "name": "apicast.policy.echo", "configuration": { } }
24+
]
25+
}
26+
}
27+
]
28+
}
29+
--- request
30+
GET /t
31+
--- response_body
32+
GET /t HTTP/1.1
33+
--- error_code: 200
34+
--- no_error_log
35+
[error]

0 commit comments

Comments
 (0)