From 0ead811b49a0f3ecf9001157563893dfa6a405d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9na=C3=AFc=20Huard?= Date: Wed, 11 Oct 2023 17:54:29 +0200 Subject: [PATCH] Fix the setting of `host` field in contlcycle, contimage and sbom events --- .../corechecks/containerimage/processor.go | 8 ++++++++ .../corechecks/containerlifecycle/event.go | 13 ++++++++++++- pkg/collector/corechecks/sbom/processor.go | 12 ++++++++---- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/pkg/collector/corechecks/containerimage/processor.go b/pkg/collector/corechecks/containerimage/processor.go index e6acb18a612c0f..62c896e9147512 100644 --- a/pkg/collector/corechecks/containerimage/processor.go +++ b/pkg/collector/corechecks/containerimage/processor.go @@ -6,6 +6,7 @@ package containerimage import ( + "context" "strings" "time" @@ -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" @@ -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, }) diff --git a/pkg/collector/corechecks/containerlifecycle/event.go b/pkg/collector/corechecks/containerlifecycle/event.go index 2505fc270eb717..4d5665f99a82b4 100644 --- a/pkg/collector/corechecks/containerlifecycle/event.go +++ b/pkg/collector/corechecks/containerlifecycle/event.go @@ -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 { @@ -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 diff --git a/pkg/collector/corechecks/sbom/processor.go b/pkg/collector/corechecks/sbom/processor.go index 53b55aa24440f2..21338678ea1366 100644 --- a/pkg/collector/corechecks/sbom/processor.go +++ b/pkg/collector/corechecks/sbom/processor.go @@ -8,6 +8,7 @@ package sbom import ( + "context" "errors" "os" "strings" @@ -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" @@ -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) @@ -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 }