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

feat(sdk): add json lib to lua script #5774

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ require (
k8s.io/apimachinery v0.0.0-20190223094358-dcb391cde5ca
k8s.io/client-go v10.0.0+incompatible
k8s.io/klog v0.2.0 // indirect
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
sigs.k8s.io/yaml v1.1.0 // indirect
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,8 @@ k8s.io/client-go v10.0.0+incompatible h1:F1IqCqw7oMBzDkqlcBymRq1450wD0eNqLE9jzUr
k8s.io/client-go v10.0.0+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
k8s.io/klog v0.2.0 h1:0ElL0OHzF3N+OhoJTL0uca20SxtYt4X4+bzHeqrB83c=
k8s.io/klog v0.2.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf h1:rRz0YsF7VXj9fXRF6yQgFI7DzST+hsI3TeFSGupntu0=
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf/go.mod h1:ivKkcY8Zxw5ba0jldhZCYYQfGdb2K6u9tbYK1AwMIBc=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
2 changes: 2 additions & 0 deletions sdk/luascript/luascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/yuin/gluare"
lua "github.com/yuin/gopher-lua"
luajson "layeh.com/gopher-json"
)

// Check is a type which helps to call a lua script with variables to check something.
Expand Down Expand Up @@ -52,6 +53,7 @@ func NewCheck() (*Check, error) {

//Open gluare module
state.PreloadModule("re", gluare.Loader)
state.PreloadModule("json", luajson.Loader)

// Sandboxing lua engine
if err := state.DoString("coroutine=nil;debug=nil;io=nil;open=nil;os.rename=nil;os.remove=nil;os.exit=nil;os.clock=nil;os.execute=nil;os.getenv=nil;os.setlocale=nil;os.tmpname=nil"); err != nil {
Expand Down
39 changes: 39 additions & 0 deletions sdk/luascript/luascript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,45 @@ func TestLuaCheckRegularExpression(t *testing.T) {
assert.False(t, l.Result)
}

func TestLuaCheckJSON(t *testing.T) {
l, err := NewCheck()
require.NoError(t, err)
l.SetVariables(map[string]string{
"cds.payload": "{\"key1\":\"value1\",\"key2\":2,\"key3\":true}",
})
require.NoError(t, l.Perform(`
local json = require("json")
local jsonObj = json.decode(cds_payload)
return jsonObj.key1 == "value1"
`))
assert.False(t, l.IsError)
assert.True(t, l.Result)

require.NoError(t, l.Perform(`
local json = require("json")
local jsonObj = json.decode(cds_payload)
return jsonObj.key1 ~= "value1"
`))
assert.False(t, l.IsError)
assert.False(t, l.Result)

require.NoError(t, l.Perform(`
local json = require("json")
local jsonObj = json.decode(cds_payload)
return jsonObj.key2 <= 2
`))
assert.False(t, l.IsError)
assert.True(t, l.Result)

require.NoError(t, l.Perform(`
local json = require("json")
local jsonObj = json.decode(cds_payload)
return jsonObj.key3
`))
assert.False(t, l.IsError)
assert.True(t, l.Result)
}

func Test_luaPerformStrictCheckOnVariable(t *testing.T) {
luaCheck, err := NewCheck()
require.NoError(t, err)
Expand Down