Skip to content

Commit

Permalink
[cli] use resty.sandbox to load environment configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed Feb 15, 2018
1 parent a708870 commit df7011f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- 3scale configuration (staging/production) can be passed as `-3` or `--channel` on the CLI [PR #590](https://github.com/3scale/apicast/pull/590)
- APIcast CLI loads environments defined by `APICAST_ENVIRONMENT` variable [PR #590](https://github.com/3scale/apicast/pull/590)
- Endpoint in management API to retrieve all the JSON manifests of the policies [PR #592](https://github.com/3scale/apicast/pull/592)
- More complete global environment when loading environment policies [PR #596](https://github.com/3scale/apicast/pull/596)

## Fixed

Expand Down
17 changes: 10 additions & 7 deletions gateway/src/apicast/cli/environment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
local pl_path = require('pl.path')
local resty_env = require('resty.env')
local linked_list = require('apicast.linked_list')
local sandbox = require('resty.sandbox')
local util = require('apicast.util')
local setmetatable = setmetatable
local loadfile = loadfile
local pcall = pcall
local require = require
local assert = assert
local error = error
local print = print
local pairs = pairs
local ipairs = ipairs
Expand Down Expand Up @@ -146,11 +145,15 @@ function _M:add(env)
return nil, 'no configuration found'
end

local config = loadfile(path, 't', {
print = print, inspect = require('inspect'), context = self._context, arg = arg, cli = arg,
tonumber = tonumber, tostring = tostring, os = { getenv = resty_env.value },
pcall = pcall, require = require, assert = assert, error = error,
})
-- using sandbox is not strictly needed,
-- but it is a nice way to add some extra env to the loaded code
-- and not using global variables
local box = sandbox.new()
local config = loadfile(path, 't', setmetatable({
inspect = require('inspect'), context = self._context,
arg = arg, cli = arg,
os = { getenv = resty_env.value },
}, { __index = box.env }))

if not config then
return nil, 'invalid config'
Expand Down
4 changes: 3 additions & 1 deletion gateway/src/resty/sandbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,16 @@ local mt = {
__call = function(loader, ...) return loader.env.require(...) end
}

local empty_t = {}

function _M.new(load_paths)
-- need to create global variable package that mimics the native one
local package = {
loaded = {},
preload = preload,
searchers = {}, -- http://www.lua.org/manual/5.2/manual.html#pdf-package.searchers
searchpath = searchpath,
path = concat(load_paths, ';'),
path = concat(load_paths or empty_t, ';'),
cpath = '', -- no C libraries allowed in sandbox
}

Expand Down

0 comments on commit df7011f

Please sign in to comment.