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

[CWS] Add test on container scope variables #32319

Merged
merged 1 commit into from
Dec 18, 2024
Merged
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
70 changes: 70 additions & 0 deletions pkg/security/tests/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,73 @@ func TestContainerFlagsPodman(t *testing.T) {
})
})
}

func TestContainerVariables(t *testing.T) {
SkipIfNotAvailable(t)

ruleDefs := []*rules.RuleDefinition{
{
ID: "test_container_set_variable",
Expression: `container.id != "" && open.file.path == "{{.Root}}/test-open"`,
Actions: []*rules.ActionDefinition{
{
Set: &rules.SetDefinition{
Scope: "container",
Value: 1,
Name: "foo",
},
},
},
},
{
ID: "test_container_check_variable",
Expression: `container.id != "" && open.file.path == "{{.Root}}/test-open2" && ${container.foo} == 1`,
},
}
test, err := newTestModule(t, nil, ruleDefs)
if err != nil {
t.Fatal(err)
}
defer test.Close()

testFile, _, err := test.Path("test-open")
if err != nil {
t.Fatal(err)
}

testFile2, _, err := test.Path("test-open2")
if err != nil {
t.Fatal(err)
}

dockerWrapper, err := newDockerCmdWrapper(test.Root(), test.Root(), "ubuntu", "")
if err != nil {
t.Skip("Skipping created time in containers tests: Docker not available")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about this error message

return
}
defer dockerWrapper.stop()

dockerWrapper.Run(t, "container-variables", func(t *testing.T, _ wrapperType, cmdFunc func(cmd string, args []string, envs []string) *exec.Cmd) {
test.WaitSignal(t, func() error {
cmd := cmdFunc("touch", []string{testFile}, nil)
return cmd.Run()
}, func(event *model.Event, rule *rules.Rule) {
assertTriggeredRule(t, rule, "test_container_set_variable")
assertFieldEqual(t, event, "open.file.path", testFile)
assertFieldNotEmpty(t, event, "container.id", "container id shouldn't be empty")

test.validateOpenSchema(t, event)
})

test.WaitSignal(t, func() error {
cmd := cmdFunc("touch", []string{testFile2}, nil)
return cmd.Run()
}, func(event *model.Event, rule *rules.Rule) {
assertTriggeredRule(t, rule, "test_container_check_variable")
assertFieldEqual(t, event, "open.file.path", testFile2)
assertFieldNotEmpty(t, event, "container.id", "container id shouldn't be empty")

test.validateOpenSchema(t, event)
})
})
}
Loading