Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 53 additions & 5 deletions controllers/mgmtcluster_classifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ import (
)

const (
testLabelEnv = "env"
testLabelApp = "app"
testValueProd = "prod"
testLabelEnv = "env"
testLabelApp = "app"
testValueProd = "prod"
testKindConfigMap = "ConfigMap"
)

var _ = Describe("ManagementClusterClassifier utils", func() {
Expand Down Expand Up @@ -115,7 +116,7 @@ var _ = Describe("ManagementClusterClassifier utils", func() {
rs := &libsveltosv1beta1.ResourceSelector{
Group: "",
Version: corev1.SchemeGroupVersion.Version,
Kind: "ConfigMap",
Kind: testKindConfigMap,
}

resources, err := controllers.FetchResourcesForSelector(context.TODO(), fakeClient, rs, logger)
Expand All @@ -130,6 +131,53 @@ var _ = Describe("ManagementClusterClassifier utils", func() {
})
})

Context("Lua sandbox", func() {
const readHostFileScript = `
function evaluate(resources)
local f = io.open("/etc/hosts", "r")
return {matching=(f ~= nil), message=""}
end
`

It("fetchResourcesForSelector does not let a per-resource script read host files", func() {
u := buildUnstructuredConfigMap("cm", map[string]string{testLabelEnv: testValueProd})
fakeClient := fake.NewClientBuilder().WithScheme(mgmtTestScheme()).WithObjects(u).Build()

rs := &libsveltosv1beta1.ResourceSelector{
Version: corev1.SchemeGroupVersion.Version,
Kind: testKindConfigMap,
Evaluate: readHostFileScript,
}

// The script fails for every resource, so none is a match.
resources, err := controllers.FetchResourcesForSelector(context.TODO(), fakeClient, rs, testEnv.GetLogger())
Expect(err).ToNot(HaveOccurred())
Expect(resources).To(BeEmpty())
})

It("runClassificationLua does not let classificationLua read host files", func() {
script := `
function evaluate(resources)
local f = io.open("/etc/hosts", "r")
return {}
end
`
_, err := controllers.RunClassificationLua(script, nil, testEnv.GetLogger())
Expect(err).To(HaveOccurred())
})

It("runClassificationLua does not let classificationLua read the environment", func() {
script := `
function evaluate(resources)
local home = os.getenv("HOME")
return {}
end
`
_, err := controllers.RunClassificationLua(script, nil, testEnv.GetLogger())
Expect(err).To(HaveOccurred())
})
})

Context("runClassificationLua", func() {
It("returns no clusters for empty resource list", func() {
script := `
Expand Down Expand Up @@ -357,7 +405,7 @@ func buildUnstructuredConfigMap(name string, lbls map[string]string) *unstructur
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cm)
Expect(err).ToNot(HaveOccurred())
u := &unstructured.Unstructured{Object: obj}
u.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap"))
u.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind(testKindConfigMap))
return u
}

Expand Down
4 changes: 2 additions & 2 deletions controllers/mgmtcluster_classifier_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func fetchResourcesForSelector(ctx context.Context, c client.Client,
// Compile per-resource Lua once so we don't re-parse it for every resource.
var luaState *lua.LState
if rs.Evaluate != "" {
luaState = lua.NewState()
luaState = sveltoslua.NewSandboxedState()
sveltoslua.LoadModulesAndRegisterMethods(luaState)
if err := luaState.DoString(rs.Evaluate); err != nil {
luaState.Close()
Expand Down Expand Up @@ -217,7 +217,7 @@ func isMatchForEvaluateScript(u *unstructured.Unstructured, l *lua.LState, logge
func runClassificationLua(luaScript string, resources []*unstructured.Unstructured,
logger logr.Logger) ([]clusterRef, error) {

l := lua.NewState()
l := sveltoslua.NewSandboxedState()
defer l.Close()
sveltoslua.LoadModulesAndRegisterMethods(l)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/onsi/ginkgo/v2 v2.34.0
github.com/onsi/gomega v1.44.0
github.com/pkg/errors v0.9.1
github.com/projectsveltos/libsveltos v1.17.1-0.20261117192944-bd11a04a1e5f
github.com/projectsveltos/libsveltos v1.17.1-0.20261121162648-4c6efa5c8263
github.com/prometheus/client_golang v1.25.0
github.com/spf13/pflag v1.1.0
github.com/yuin/gopher-lua v1.1.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/projectsveltos/libsveltos v1.17.1-0.20261117192944-bd11a04a1e5f h1:1Sly6XRWugk2dOwkQHgut9Aoahr97AoM3VnL3xlHW2o=
github.com/projectsveltos/libsveltos v1.17.1-0.20261117192944-bd11a04a1e5f/go.mod h1:dHJrl1V5/WrNda5wzxbHs2dlsvoV/FUlwK1TXy2X3pA=
github.com/projectsveltos/libsveltos v1.17.1-0.20261121162648-4c6efa5c8263 h1:E2/jlu5M6aQIjsimBdajj/+qD6mlZw1N7dCR9Iuj++s=
github.com/projectsveltos/libsveltos v1.17.1-0.20261121162648-4c6efa5c8263/go.mod h1:dHJrl1V5/WrNda5wzxbHs2dlsvoV/FUlwK1TXy2X3pA=
github.com/projectsveltos/lua-utils/glua-json v0.0.0-20251212200258-2b3cdcb7c0f5 h1:khnc+994UszxZYu69J+R5FKiLA/Nk1JQj0EYAkwTWz0=
github.com/projectsveltos/lua-utils/glua-json v0.0.0-20251212200258-2b3cdcb7c0f5/go.mod h1:yVL8KQFa9tmcxgwl9nwIMtKgtmIVC1zaFRSCfOwYvPY=
github.com/projectsveltos/lua-utils/glua-runes v0.0.0-20251212200258-2b3cdcb7c0f5 h1:YbsebwRwTRhV8QacvEAdFqxcxHdeu7JTVtsBovbkgos=
Expand Down