Skip to content

Commit

Permalink
Allow multiple paths in the module_directory configuration Issue mozi…
Browse files Browse the repository at this point in the history
…lla-services#1525

Conflicts:
	CHANGES.txt
  • Loading branch information
trink authored and rafrombrc committed Jul 15, 2015
1 parent 7193a9c commit 5b67959
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 20 deletions.
8 changes: 8 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
0.10.0 (2015-??-??)
===================

Features
--------

* Allow multiple sandbox module directories to be specified (#1525).

0.10.0b0 (2015-07-13)
=====================

Expand Down
54 changes: 37 additions & 17 deletions sandbox/lua/lua_sandbox.go.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ remove_entries = {
[''] = { 'dofile', 'load', 'loadfile','loadstring', 'print'},
os = {'exit', 'setlocale'}}}`

const SandboxTemplate = `{
memory_limit = %d,
instruction_limit = %d,
output_limit = %d,
path = [[%s]],
cpath = [[%s]],
remove_entries = {
[''] = {'collectgarbage','coroutine','dofile','load','loadfile','loadstring','newproxy','print'},
os = {'getenv','execute','exit','remove','rename','setlocale','tmpname'}
},
disable_modules = {io = 1}
}`

func extractLuaFieldName(wrapped string) (fn string, found bool) {
if l := len(wrapped); l > 0 && wrapped[l-1] == ']' {
if strings.HasPrefix(wrapped, "Fields[") {
Expand Down Expand Up @@ -647,31 +660,38 @@ type LuaSandbox struct {
}

func CreateLuaSandbox(conf *sandbox.SandboxConfig) (sandbox.Sandbox, error) {
var (
lua_path, lua_cpath []string
template string
)
lsb := new(LuaSandbox)
lsb.sbConfig = conf
cs := C.CString(conf.ScriptFilename)
defer C.free(unsafe.Pointer(cs))

paths := strings.Split(conf.ModuleDirectory, ";")
for _, p := range paths {
lua_path = append(lua_path, filepath.Join(p, "?.lua"))
lua_cpath = append(lua_cpath, filepath.Join(p, "?@LUA_SHARED_LIBRARY_SUFFIX@"))
}

if conf.PluginType == "output" || conf.PluginType == "input" {
cfg := fmt.Sprintf(SandboxIoTemplate,
conf.MemoryLimit,
conf.InstructionLimit,
conf.OutputLimit,
filepath.Join(conf.ModuleDirectory, "?.lua"),
filepath.Join(conf.ModuleDirectory, "?@LUA_SHARED_LIBRARY_SUFFIX@"))
ccfg := C.CString(cfg)
defer C.free(unsafe.Pointer(ccfg))
lsb.lsb = C.lsb_create_custom(unsafe.Pointer(lsb), cs, ccfg)
template = SandboxIoTemplate
} else {
md := C.CString(conf.ModuleDirectory)
defer C.free(unsafe.Pointer(md))
lsb.lsb = C.lsb_create(unsafe.Pointer(lsb),
cs,
md,
C.uint(conf.MemoryLimit),
C.uint(conf.InstructionLimit),
C.uint(conf.OutputLimit))
template = SandboxTemplate
}

cfg := fmt.Sprintf(template,
conf.MemoryLimit,
conf.InstructionLimit,
conf.OutputLimit,
strings.Join(lua_path, ";"),
strings.Join(lua_cpath, ";"))
ccfg := C.CString(cfg)
defer C.free(unsafe.Pointer(ccfg))

lsb.lsb = C.lsb_create_custom(unsafe.Pointer(lsb), cs, ccfg)

if lsb.lsb == nil {
return nil, fmt.Errorf("Sandbox creation failed")
}
Expand Down
2 changes: 1 addition & 1 deletion sandbox/lua/lua_sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func TestAPIErrors(t *testing.T) {
"invalid error message",
}
msgs := []string{
"process_message() ./testsupport/errors.lua:11: module 'unknown' not found:\n\tno file '/unknown.lua'\n\tno file '/unknown.so'",
"process_message() ./testsupport/errors.lua:11: module 'unknown' not found:\n\tno file 'unknown.lua'\n\tno file 'unknown.so'",
"process_message() ./testsupport/errors.lua:13: bad argument #0 to 'add_to_payload' (must have at least one argument)",
"process_message() not enough memory",
"process_message() instruction_limit exceeded",
Expand Down
6 changes: 6 additions & 0 deletions sandbox/lua/testsupport/modules/dummy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local M = {}
setfenv(1, M) -- Remove external access to contain everything in the module.

version = "1.0.0"

return M
1 change: 1 addition & 0 deletions sandbox/lua/testsupport/output.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "io"
require "string"
require "dummy"

function process_message()
local payload = read_message("Payload")
Expand Down
2 changes: 1 addition & 1 deletion sandbox/plugins/sandbox_manager_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (this *SandboxManagerFilter) Init(config interface{}) (err error) {
globals := this.pConfig.Globals
this.maxFilters = conf.MaxFilters
this.workingDirectory = globals.PrependBaseDir(conf.WorkingDirectory)
this.moduleDirectory = globals.PrependShareDir(conf.ModuleDirectory)
this.moduleDirectory = conf.ModuleDirectory
this.memoryLimit = conf.MemoryLimit
this.instructionLimit = conf.InstructionLimit
this.outputLimit = conf.OutputLimit
Expand Down
2 changes: 1 addition & 1 deletion sandbox/plugins/sandbox_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func OutputSpec(c gs.Context) {
output.SetPipelineConfig(pConfig)
conf := output.ConfigStruct().(*sandbox.SandboxConfig)
conf.ScriptFilename = "../lua/testsupport/output.lua"
conf.ModuleDirectory = "../lua/modules"
conf.ModuleDirectory = "../lua/modules;../lua/testsupport/modules"
supply := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(supply)
data := "1376389920 debug id=2321 url=example.com item=1"
Expand Down

0 comments on commit 5b67959

Please sign in to comment.