Skip to content

Commit

Permalink
[CWS] fix container_id during snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
safchain committed Nov 7, 2024
1 parent de82921 commit f2169f4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 22 deletions.
2 changes: 2 additions & 0 deletions pkg/security/resolvers/process/resolver_ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ func (p *EBPFResolver) enrichEventFromProc(entry *model.ProcessCacheEntry, proc
return fmt.Errorf("snapshot failed for %d: couldn't parse container ID: %w", proc.Pid, err)
}

entry.ContainerID = containerID

entry.FileEvent.FileFields = *info
setPathname(&entry.FileEvent, pathnameStr)

Expand Down
88 changes: 66 additions & 22 deletions pkg/security/tests/snapshot_replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package tests

import (
"os/exec"
"testing"
"time"

Expand All @@ -19,32 +18,77 @@ import (
"github.com/DataDog/datadog-agent/pkg/security/secl/rules"
)

func TestSnapshotReplay(t *testing.T) {
func TestSnapshot(t *testing.T) {
SkipIfNotAvailable(t)

ruleDef := &rules.RuleDefinition{
ID: "test_rule_snapshot_replay",
Expression: "exec.comm in [\"testsuite\"]",
}
t.Run("host-event", func(t *testing.T) {
ruleDefs := []*rules.RuleDefinition{
{
ID: "test_rule_snapshot_host",
Expression: `exec.comm in ["testsuite"]`,
},
}

gotEvent := atomic.NewBool(false)
gotEvent := atomic.NewBool(false)

test, err := newTestModule(t, nil, []*rules.RuleDefinition{ruleDef}, withStaticOpts(testOpts{
snapshotRuleMatchHandler: func(testMod *testModule, e *model.Event, r *rules.Rule) {
assertTriggeredRule(t, r, "test_rule_snapshot_replay")
testMod.validateExecSchema(t, e)
gotEvent.Store(true)
},
}))
test, err := newTestModule(t, nil, ruleDefs, withStaticOpts(testOpts{
snapshotRuleMatchHandler: func(testMod *testModule, e *model.Event, r *rules.Rule) {
assertTriggeredRule(t, r, "test_rule_snapshot_host")
testMod.validateExecSchema(t, e)
gotEvent.Store(true)
},
}))

if err != nil {
t.Fatal(err)
}
defer test.Close()
if err != nil {
t.Fatal(err)
}
defer test.Close()

if _, err := exec.Command("echo", "hello", "world").CombinedOutput(); err != nil {
t.Fatal(err)
}
assert.Eventually(t, func() bool { return gotEvent.Load() }, 10*time.Second, 100*time.Millisecond, "didn't get the event from snapshot")
})

assert.Eventually(t, func() bool { return gotEvent.Load() }, 10*time.Second, 100*time.Millisecond, "didn't get the event from snapshot")
t.Run("container-event", func(t *testing.T) {
ruleDefs := []*rules.RuleDefinition{
{
ID: "test_rule_snapshot_container",
Expression: `exec.comm in ["sleep"] && process.argv in ["123"] && container.id != ""`,
},
}

dockerWrapper, err := newDockerCmdWrapper("/tmp", "/tmp", "ubuntu", "")
if err != nil {
t.Skip("Skipping created time in containers tests: Docker not available")
return
}

if _, err := dockerWrapper.start(); err != nil {
t.Fatal(err)
}
defer dockerWrapper.stop()

go func() {
cmd := dockerWrapper.Command("sh", []string{"-c", "sleep 123"}, nil)
_ = cmd.Run()
}()

// wait a bit so that the command is running and captured by the snapshot
time.Sleep(2 * time.Second)

gotEvent := atomic.NewBool(false)

test, err := newTestModule(t, nil, ruleDefs, withStaticOpts(testOpts{
snapshotRuleMatchHandler: func(testMod *testModule, e *model.Event, r *rules.Rule) {
assertTriggeredRule(t, r, "test_rule_snapshot_container")
testMod.validateExecSchema(t, e)
gotEvent.Store(true)
},
}))

if err != nil {
t.Fatal(err)
}
defer test.Close()

assert.Eventually(t, func() bool { return gotEvent.Load() }, 10*time.Second, 100*time.Millisecond, "didn't get the event from snapshot")
})
}

0 comments on commit f2169f4

Please sign in to comment.