Skip to content

Commit

Permalink
Fix the setting of host field in contlcycle, contimage and sbom eve…
Browse files Browse the repository at this point in the history
…nts (#20074)
  • Loading branch information
L3n41c authored Oct 12, 2023
1 parent 0730fb4 commit ad11347
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 16 deletions.
8 changes: 8 additions & 0 deletions pkg/collector/corechecks/containerimage/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package containerimage

import (
"context"
"strings"
"time"

Expand All @@ -14,6 +15,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/tagger"
"github.com/DataDog/datadog-agent/pkg/tagger/collectors"
queue "github.com/DataDog/datadog-agent/pkg/util/aggregatingqueue"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/workloadmeta"

Expand All @@ -32,10 +34,16 @@ type processor struct {
}

func newProcessor(sender sender.Sender, maxNbItem int, maxRetentionTime time.Duration) *processor {
hname, err := hostname.Get(context.TODO())
if err != nil {
log.Warnf("Error getting hostname: %v", err)
}

return &processor{
queue: queue.NewQueue(maxNbItem, maxRetentionTime, func(images []*model.ContainerImage) {
encoded, err := proto.Marshal(&model.ContainerImagePayload{
Version: "v1",
Host: hname,
Source: &sourceAgent,
Images: images,
})
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/corechecks/containerimage/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package containerimage

import (
"context"
"testing"
"time"

Expand All @@ -19,6 +20,7 @@ import (

"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"github.com/DataDog/datadog-agent/pkg/epforwarder"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/pointer"
"github.com/DataDog/datadog-agent/pkg/workloadmeta"
)
Expand Down Expand Up @@ -435,9 +437,11 @@ func TestProcessEvents(t *testing.T) {
return imagesSent.Load() == int32(len(test.expectedImages))
}, 1*time.Second, 5*time.Millisecond)

hname, _ := hostname.Get(context.TODO())
for _, expectedImage := range test.expectedImages {
encoded, err := proto.Marshal(&model.ContainerImagePayload{
Version: "v1",
Host: hname,
Source: &sourceAgent,
Images: []*model.ContainerImage{expectedImage},
})
Expand Down
13 changes: 12 additions & 1 deletion pkg/collector/corechecks/containerlifecycle/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
package containerlifecycle

import (
"context"
"fmt"

model "github.com/DataDog/agent-payload/v5/contlcycle"

types "github.com/DataDog/datadog-agent/pkg/containerlifecycle"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

type event interface {
Expand Down Expand Up @@ -80,7 +83,15 @@ func (e *eventTransformer) withOwnerID(id string) {
}

func (e *eventTransformer) toPayloadModel() (*model.EventsPayload, error) {
payload := &model.EventsPayload{Version: types.PayloadV1}
hname, err := hostname.Get(context.TODO())
if err != nil {
log.Warnf("Error getting hostname: %v", err)
}

payload := &model.EventsPayload{
Version: types.PayloadV1,
Host: hname,
}
kind, err := e.kind(e.objectKind)
if err != nil {
return nil, err
Expand Down
13 changes: 8 additions & 5 deletions pkg/collector/corechecks/containerlifecycle/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import (

"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
checkid "github.com/DataDog/datadog-agent/pkg/collector/check/id"
"github.com/DataDog/datadog-agent/pkg/util/hostname"

"github.com/stretchr/testify/mock"
)

func TestProcessQueues(t *testing.T) {
hname, _ := hostname.Get(context.TODO())

tests := []struct {
name string
containersQueue *queue
Expand All @@ -34,7 +37,7 @@ func TestProcessQueues(t *testing.T) {
{
name: "one container",
containersQueue: &queue{data: []*model.EventsPayload{
{Version: "v1", Events: modelEvents("cont1")},
{Version: "v1", Host: hname, Events: modelEvents("cont1")},
}},
podsQueue: &queue{},
wantFunc: func(t *testing.T, s *mocksender.MockSender) {
Expand All @@ -44,12 +47,12 @@ func TestProcessQueues(t *testing.T) {
{
name: "multiple chunks per types",
containersQueue: &queue{data: []*model.EventsPayload{
{Version: "v1", Events: modelEvents("cont1", "cont2")},
{Version: "v1", Events: modelEvents("cont3")},
{Version: "v1", Host: hname, Events: modelEvents("cont1", "cont2")},
{Version: "v1", Host: hname, Events: modelEvents("cont3")},
}},
podsQueue: &queue{data: []*model.EventsPayload{
{Version: "v1", Events: modelEvents("pod1", "pod2")},
{Version: "v1", Events: modelEvents("pod3")},
{Version: "v1", Host: hname, Events: modelEvents("pod1", "pod2")},
{Version: "v1", Host: hname, Events: modelEvents("pod3")},
}},
wantFunc: func(t *testing.T, s *mocksender.MockSender) {
s.AssertNumberOfCalls(t, "EventPlatformEvent", 4)
Expand Down
15 changes: 9 additions & 6 deletions pkg/collector/corechecks/containerlifecycle/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package containerlifecycle

import (
"context"
"strconv"
"testing"

model "github.com/DataDog/agent-payload/v5/contlcycle"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/stretchr/testify/assert"
)

Expand All @@ -32,6 +34,7 @@ func modelEvents(objIDs ...string) []*model.Event {
}

func TestSingleQueueAdd(t *testing.T) {
hname, _ := hostname.Get(context.TODO())
commonChunkSize := 2

tests := []struct {
Expand All @@ -44,21 +47,21 @@ func TestSingleQueueAdd(t *testing.T) {
name: "empty queue",
data: []*model.EventsPayload{},
ev: fakeContainerEvent("obj1"),
want: []*model.EventsPayload{{Version: "v1", Events: modelEvents("obj1")}},
want: []*model.EventsPayload{{Version: "v1", Host: hname, Events: modelEvents("obj1")}},
},
{
name: "last payload not full",
data: []*model.EventsPayload{{Version: "v1", Events: modelEvents("obj1")}},
data: []*model.EventsPayload{{Version: "v1", Host: hname, Events: modelEvents("obj1")}},
ev: fakeContainerEvent("obj2"),
want: []*model.EventsPayload{{Version: "v1", Events: modelEvents("obj1", "obj2")}},
want: []*model.EventsPayload{{Version: "v1", Host: hname, Events: modelEvents("obj1", "obj2")}},
},
{
name: "last payload full",
data: []*model.EventsPayload{{Version: "v1", Events: modelEvents("obj1", "obj2")}},
data: []*model.EventsPayload{{Version: "v1", Host: hname, Events: modelEvents("obj1", "obj2")}},
ev: fakeContainerEvent("obj3"),
want: []*model.EventsPayload{
{Version: "v1", Events: modelEvents("obj1", "obj2")},
{Version: "v1", Events: modelEvents("obj3")},
{Version: "v1", Host: hname, Events: modelEvents("obj1", "obj2")},
{Version: "v1", Host: hname, Events: modelEvents("obj3")},
},
},
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/collector/corechecks/sbom/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package sbom

import (
"context"
"errors"
"os"
"strings"
Expand All @@ -20,10 +21,10 @@ import (
"github.com/DataDog/datadog-agent/pkg/sbom"
"github.com/DataDog/datadog-agent/pkg/sbom/collectors/host"
sbomscanner "github.com/DataDog/datadog-agent/pkg/sbom/scanner"
"github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/tagger"
"github.com/DataDog/datadog-agent/pkg/tagger/collectors"
queue "github.com/DataDog/datadog-agent/pkg/util/aggregatingqueue"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/workloadmeta"

Expand Down Expand Up @@ -59,16 +60,19 @@ func newProcessor(workloadmetaStore workloadmeta.Store, sender sender.Sender, ma
if sbomScanner == nil {
return nil, errors.New("failed to get global SBOM scanner")
}
hostname, _ := utils.GetHostname()
hname, err := hostname.Get(context.TODO())
if err != nil {
log.Warnf("Error getting hostname: %v", err)
}

return &processor{
queue: queue.NewQueue(maxNbItem, maxRetentionTime, func(entities []*model.SBOMEntity) {
encoded, err := proto.Marshal(&model.SBOMPayload{
Version: 1,
Host: hname,
Source: &sourceAgent,
Entities: entities,
DdEnv: &envVarEnv,
Host: hostname,
})
if err != nil {
log.Errorf("Unable to encode message: %+v", err)
Expand All @@ -83,7 +87,7 @@ func newProcessor(workloadmetaStore workloadmeta.Store, sender sender.Sender, ma
sbomScanner: sbomScanner,
hostSBOM: hostSBOM,
hostScanOpts: hostScanOpts,
hostname: hostname,
hostname: hname,
hostHeartbeatValidity: hostHeartbeatValidity,
}, nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/corechecks/sbom/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package sbom

import (
"context"
"os"
"testing"
"time"
Expand All @@ -26,12 +27,14 @@ import (
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/epforwarder"
sbomscanner "github.com/DataDog/datadog-agent/pkg/sbom/scanner"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/pointer"
"github.com/DataDog/datadog-agent/pkg/workloadmeta"
fakeworkloadmeta "github.com/DataDog/datadog-agent/pkg/workloadmeta/testing"
)

func TestProcessEvents(t *testing.T) {
hname, _ := hostname.Get(context.TODO())
sbomGenerationTime := time.Now()

tests := []struct {
Expand Down Expand Up @@ -632,6 +635,7 @@ func TestProcessEvents(t *testing.T) {
for _, expectedSBOM := range test.expectedSBOMs {
encoded, err := proto.Marshal(&model.SBOMPayload{
Version: 1,
Host: hname,
Source: &sourceAgent,
Entities: []*model.SBOMEntity{expectedSBOM},
DdEnv: &envVarEnv,
Expand Down

0 comments on commit ad11347

Please sign in to comment.