Skip to content

Commit

Permalink
Fix functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lebauce committed Nov 14, 2024
1 parent a3247b6 commit acabcbb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
15 changes: 13 additions & 2 deletions pkg/security/resolvers/cgroup/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ const (
CGroupMaxEvent
)

// ResolverInterface defines the interface implemented by a cgroup resolver
type ResolverInterface interface {
Start(context.Context)
AddPID(*model.ProcessCacheEntry)
GetWorkload(string) (*cgroupModel.CacheEntry, bool)
DelPID(uint32)
DelPIDWithID(string, uint32)
Len() int
RegisterListener(Event, utils.Listener[*cgroupModel.CacheEntry]) error
}

// Resolver defines a cgroup monitor
type Resolver struct {
*utils.Notifier[Event, *cgroupModel.CacheEntry]
Expand All @@ -48,7 +59,7 @@ func NewResolver() (*Resolver, error) {
value.CallReleaseCallback()
value.Deleted.Store(true)

cr.NotifyListener(CGroupDeleted, value)
cr.NotifyListeners(CGroupDeleted, value)
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -84,7 +95,7 @@ func (cr *Resolver) AddPID(process *model.ProcessCacheEntry) {
// add the new CGroup to the cache
cr.workloads.Add(string(process.ContainerID), newCGroup)

cr.NotifyListener(CGroupCreated, newCGroup)
cr.NotifyListeners(CGroupCreated, newCGroup)
}

// GetWorkload returns the workload referenced by the provided ID
Expand Down
2 changes: 1 addition & 1 deletion pkg/security/resolvers/resolvers_ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (r *EBPFResolvers) Start(ctx context.Context) error {
return err
}

if err := r.TagsResolver.Start(ctx); err != nil {
if err := r.TagsResolver.Start(ctx, r.CGroupResolver); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/security/resolvers/resolvers_ebpfless.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r *EBPFLessResolvers) Start(ctx context.Context) error {
return err
}

if err := r.TagsResolver.Start(ctx); err != nil {
if err := r.TagsResolver.Start(ctx, nil); err != nil {
return err
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/security/resolvers/tags/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/api/security"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/security/probe/config"
"github.com/DataDog/datadog-agent/pkg/security/resolvers/cgroup"
cgroupModel "github.com/DataDog/datadog-agent/pkg/security/resolvers/cgroup/model"
"github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand Down Expand Up @@ -55,10 +56,10 @@ func (n *nullTagger) Tag(_ types.EntityID, _ types.TagCardinality) ([]string, er

// Resolver represents a cache resolver
type Resolver interface {
Start(ctx context.Context) error
Start(ctx context.Context, cgroupManager cgroup.ResolverInterface) error
Stop() error
Resolve(id string) []string
ResolveWithErr(id string) ([]string, error)
ResolveWithErr(fid string) ([]string, error)
GetValue(id string, tag string) string
RegisterListener(event Event, listener utils.Listener[*cgroupModel.CacheEntry]) error
}
Expand Down Expand Up @@ -94,7 +95,7 @@ func (t *DefaultResolver) GetValue(id string, tag string) string {
}

// Start the resolver
func (t *DefaultResolver) Start(ctx context.Context) error {
func (t *DefaultResolver) Start(ctx context.Context, _ cgroup.ResolverInterface) error {
go func() {
if err := t.tagger.Start(ctx); err != nil {
log.Errorf("failed to init tagger: %s", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/security/resolvers/tags/resolver_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type LinuxResolver struct {
}

// Start the resolver
func (t *LinuxResolver) Start(ctx context.Context) error {
if err := t.DefaultResolver.Start(ctx); err != nil {
func (t *LinuxResolver) Start(ctx context.Context, cgroupResolver cgroup.ResolverInterface) error {
if err := t.DefaultResolver.Start(ctx, cgroupResolver); err != nil {
return err
}

if err := t.cgroupResolver.RegisterListener(cgroup.CGroupCreated, t.checkTags); err != nil {
if err := cgroupResolver.RegisterListener(cgroup.CGroupCreated, t.checkTags); err != nil {
return err
}

Expand Down Expand Up @@ -74,7 +74,7 @@ func (t *LinuxResolver) checkTags(workload *cgroupModel.CacheEntry) {
}
}

t.NotifyListener(WorkloadSelectorResolved, workload)
t.NotifyListeners(WorkloadSelectorResolved, workload)
}

// fetchTags fetches tags for the provided workload
Expand Down
28 changes: 22 additions & 6 deletions pkg/security/tests/fake_tags_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"sync"

"github.com/DataDog/datadog-agent/pkg/security/resolvers/cgroup"
cgroupModel "github.com/DataDog/datadog-agent/pkg/security/resolvers/cgroup/model"
"github.com/DataDog/datadog-agent/pkg/security/resolvers/tags"
"github.com/DataDog/datadog-agent/pkg/security/utils"
Expand All @@ -27,9 +28,14 @@ type FakeResolver struct {
containerIDs []string
}

// checkTags checks if the tags of a workload were properly set
func (fr *FakeResolver) checkTags(workload *cgroupModel.CacheEntry) {
fr.NotifyListeners(tags.WorkloadSelectorResolved, workload)
}

// Start the resolver
func (fr *FakeResolver) Start(_ context.Context) error {
return nil
func (fr *FakeResolver) Start(_ context.Context, cgroupResolver cgroup.ResolverInterface) error {
return cgroupResolver.RegisterListener(cgroup.CGroupCreated, fr.checkTags)
}

// Stop the resolver
Expand Down Expand Up @@ -77,9 +83,14 @@ type FakeMonoResolver struct {
*utils.Notifier[tags.Event, *cgroupModel.CacheEntry]
}

// checkTags checks if the tags of a workload were properly set
func (fmr *FakeMonoResolver) checkTags(workload *cgroupModel.CacheEntry) {
fmr.NotifyListeners(tags.WorkloadSelectorResolved, workload)
}

// Start the resolver
func (fmr *FakeMonoResolver) Start(_ context.Context) error {
return nil
func (fmr *FakeMonoResolver) Start(_ context.Context, cgroupResolver cgroup.ResolverInterface) error {
return cgroupResolver.RegisterListener(cgroup.CGroupCreated, fmr.checkTags)
}

// Stop the resolver
Expand Down Expand Up @@ -120,9 +131,14 @@ type FakeManualResolver struct {
nextSelectors []*cgroupModel.WorkloadSelector
}

// checkTags checks if the tags of a workload were properly set
func (fmr *FakeManualResolver) checkTags(workload *cgroupModel.CacheEntry) {
fmr.NotifyListeners(tags.WorkloadSelectorResolved, workload)
}

// Start the resolver
func (fmr *FakeManualResolver) Start(_ context.Context) error {
return nil
func (fmr *FakeManualResolver) Start(_ context.Context, cgroupResolver cgroup.ResolverInterface) error {
return cgroupResolver.RegisterListener(cgroup.CGroupCreated, fmr.checkTags)
}

// Stop the resolver
Expand Down
3 changes: 2 additions & 1 deletion pkg/security/utils/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (n *Notifier[E, O]) RegisterListener(event E, listener Listener[O]) error {
return nil
}

func (n *Notifier[E, O]) NotifyListener(event E, obj O) {
// NotifyListeners notifies all listeners of an event type
func (n *Notifier[E, O]) NotifyListeners(event E, obj O) {
// notify listeners
n.listenersLock.RLock()
for _, l := range n.listeners[event] {
Expand Down

0 comments on commit acabcbb

Please sign in to comment.