Skip to content

Commit

Permalink
policies are loaded and initialized with service config
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed Nov 7, 2017
1 parent 60ca803 commit 9f1e4fc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
14 changes: 14 additions & 0 deletions apicast/src/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local re = require 'ngx.re'
local env = require 'resty.env'
local resty_url = require 'resty.url'
local util = require 'util'
local policy_chain = require 'policy_chain'

local mt = { __index = _M, __tostring = function() return 'Configuration' end }

Expand Down Expand Up @@ -152,6 +153,18 @@ local function backend_endpoint(proxy)
end
end

local function build_policy_chain(policies)
if not policies then return nil, 'no policy chain' end

local chain = {}

for i=1, #policies do
chain[i] = policy_chain.load(policies[i].name, policies[i].configuration)
end

return policy_chain.new(chain)
end

function _M.parse_service(service)
local backend_version = tostring(service.backend_version)
local proxy = service.proxy or empty_t
Expand All @@ -164,6 +177,7 @@ function _M.parse_service(service)
authentication_method = proxy.authentication_method or backend_version,
hosts = proxy.hosts or { 'localhost' }, -- TODO: verify localhost is good default
api_backend = proxy.api_backend,
policy_chain = build_policy_chain(proxy.policy_chain),
error_auth_failed = proxy.error_auth_failed or 'Authentication failed',
error_limits_exceeded = proxy.error_limits_exceeded or 'Limits exceeded',
error_auth_missing = proxy.error_auth_missing or 'Authentication parameters missing',
Expand Down
9 changes: 9 additions & 0 deletions apicast/src/policy/echo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local policy = require('policy')
local _M = policy.new('Echo Policy')

function _M.access()
ngx.say(ngx.var.request)
ngx.exit(0)
end

return _M
20 changes: 10 additions & 10 deletions apicast/src/policy/local_chain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ local policy = require('policy')
local Proxy = require('proxy')
local _M = policy.new('Local Policy Chain')

local policy_chain = require('policy_chain')
local default_chain = require('policy_chain').build()

local new = _M.new

function _M.new(...)
local self = new(...)
self.policy_chain = policy_chain.build()
return self
local function find_policy_chain(context)
return context.policy_chain or (context.service and context.service.policy_chain) or default_chain
end

local function build_chain(context)
local proxy = Proxy.new(context.configuration)

context.proxy = context.proxy or proxy
context.policy_chain = policy_chain.build({})
context.policy_chain = find_policy_chain(context)
end

-- forward all policy methods to the policy chain
for _, phase in policy.phases() do
_M[phase] = function(self, ...)
return self.policy_chain[phase](self.policy_chain, ...)
_M[phase] = function(_, context, ...)
local policy_chain = find_policy_chain(context)

if policy_chain then
return policy_chain[phase](policy_chain, context, ...)
end
end
end

Expand Down
1 change: 1 addition & 0 deletions apicast/src/policy_chain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ end

function _M.load(module, ...)
if type(module) == 'string' then
ngx.log(ngx.DEBUG, 'loading policy module: ', module)
return require(module).new(...)
else
return module
Expand Down
3 changes: 3 additions & 0 deletions examples/configuration/echo.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"endpoint": "http://127.0.0.1:8081",
"host": "echo"
},
"policy_chain": [
{ "name": "policy.echo" }
],
"proxy_rules": [
{
"http_method": "GET",
Expand Down

0 comments on commit 9f1e4fc

Please sign in to comment.