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

[CONTINT-3744] Fill the repo_digests field in the SBOM payloads #22161

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ require github.com/lorenzosaino/go-sysctl v0.3.1

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/DataDog/agent-payload/v5 v5.0.102
github.com/DataDog/agent-payload/v5 v5.0.103
github.com/DataDog/datadog-agent/cmd/agent/common/path v0.51.0-rc.2
github.com/DataDog/datadog-agent/comp/core/config v0.51.0-rc.2
github.com/DataDog/datadog-agent/comp/core/flare/types v0.51.0-rc.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions pkg/collector/corechecks/sbom/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/config"

//nolint:revive // TODO(CINT) Fix revive linter
ddConfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/epforwarder"
Expand Down Expand Up @@ -295,6 +296,18 @@ func (p *processor) processImageSBOM(img *workloadmeta.ContainerImageMetadata) {
}
}

repoDigests := make([]string, 0, len(img.RepoDigests))
for _, repoDigest := range img.RepoDigests {
if strings.HasPrefix(repoDigest, repo+"@sha256:") {
L3n41c marked this conversation as resolved.
Show resolved Hide resolved
repoDigests = append(repoDigests, repoDigest)
}
}

if len(repoDigests) == 0 {
log.Errorf("The image %s has no repo digest for repo %s", img.ID, repo)
continue
}

// Because we split a single image entity into different payloads if it has several repo digests,
// me must re-compute `image_id`, `image_name`, `short_image` and `image_tag` tags.
ddTags2 := make([]string, 0, len(ddTags))
Expand All @@ -316,11 +329,12 @@ func (p *processor) processImageSBOM(img *workloadmeta.ContainerImageMetadata) {
}

sbom := &model.SBOMEntity{
L3n41c marked this conversation as resolved.
Show resolved Hide resolved
Type: model.SBOMSourceType_CONTAINER_IMAGE_LAYERS,
Id: id,
DdTags: ddTags2,
RepoTags: repoTags,
InUse: inUse,
Type: model.SBOMSourceType_CONTAINER_IMAGE_LAYERS,
Id: id,
DdTags: ddTags2,
RepoTags: repoTags,
RepoDigests: repoDigests,
InUse: inUse,
}

switch img.SBOM.Status {
Expand Down
71 changes: 37 additions & 34 deletions pkg/collector/corechecks/sbom/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func TestProcessEvents(t *testing.T) {
"7-rc",
"7.41.1-rc.1",
},
RepoDigests: []string{
"datadog/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
GeneratedAt: timestamppb.New(sbomGenerationTime),
GenerationDuration: durationpb.New(10 * time.Second),
Expand Down Expand Up @@ -142,6 +145,9 @@ func TestProcessEvents(t *testing.T) {
"7-rc",
"7.41.1-rc.1",
},
RepoDigests: []string{
"gcr.io/datadoghq/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
GeneratedAt: timestamppb.New(sbomGenerationTime),
GenerationDuration: durationpb.New(10 * time.Second),
Expand Down Expand Up @@ -178,6 +184,9 @@ func TestProcessEvents(t *testing.T) {
"7-rc",
"7.41.1-rc.1",
},
RepoDigests: []string{
"public.ecr.aws/datadog/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
GeneratedAt: timestamppb.New(sbomGenerationTime),
GenerationDuration: durationpb.New(10 * time.Second),
Expand Down Expand Up @@ -208,7 +217,8 @@ func TestProcessEvents(t *testing.T) {
// this test, we define an image with 2 repo tags: one for the gcr.io
// registry and another for the public.ecr.aws registry, but there's only
// one repo digest.
// We expect to send 2 events, one for each registry.
// We expect to send only one event as the backend-end will drop
// sbom without any repo digest anyhow.
name: "repo tag with no matching repo digest",
inputEvents: []workloadmeta.Event{
{
Expand Down Expand Up @@ -262,39 +272,8 @@ func TestProcessEvents(t *testing.T) {
RepoTags: []string{
"7-rc",
},
InUse: false,
GeneratedAt: timestamppb.New(sbomGenerationTime),
GenerationDuration: durationpb.New(10 * time.Second),
Sbom: &model.SBOMEntity_Cyclonedx{
Cyclonedx: &cyclonedx_v1_4.Bom{
SpecVersion: "1.4",
Version: pointer.Ptr(int32(42)),
Components: []*cyclonedx_v1_4.Component{
{
Name: "Foo",
},
{
Name: "Bar",
},
{
Name: "Baz",
},
},
},
},
Status: model.SBOMStatus_SUCCESS,
},
{
Type: model.SBOMSourceType_CONTAINER_IMAGE_LAYERS,
Id: "gcr.io/datadoghq/agent@sha256:9634b84c45c6ad220c3d0d2305aaa5523e47d6d43649c9bbeda46ff010b4aacd",
DdTags: []string{
"image_id:gcr.io/datadoghq/agent@sha256:9634b84c45c6ad220c3d0d2305aaa5523e47d6d43649c9bbeda46ff010b4aacd",
"image_name:gcr.io/datadoghq/agent",
"short_image:agent",
"image_tag:7-rc",
},
RepoTags: []string{
"7-rc",
RepoDigests: []string{
"public.ecr.aws/datadog/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
GeneratedAt: timestamppb.New(sbomGenerationTime),
Expand Down Expand Up @@ -372,6 +351,9 @@ func TestProcessEvents(t *testing.T) {
RepoTags: []string{
"7-rc",
},
RepoDigests: []string{
"datadog/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
GeneratedAt: timestamppb.New(sbomGenerationTime),
GenerationDuration: durationpb.New(10 * time.Second),
Expand All @@ -395,6 +377,9 @@ func TestProcessEvents(t *testing.T) {
RepoTags: []string{
"7-rc",
},
RepoDigests: []string{
"datadog/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: true,
GeneratedAt: timestamppb.New(sbomGenerationTime),
GenerationDuration: durationpb.New(10 * time.Second),
Expand Down Expand Up @@ -452,6 +437,9 @@ func TestProcessEvents(t *testing.T) {
"7-rc",
"7.41.1-rc.1",
},
RepoDigests: []string{
"datadog/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
Status: model.SBOMStatus_PENDING,
},
Expand All @@ -469,6 +457,9 @@ func TestProcessEvents(t *testing.T) {
"7-rc",
"7.41.1-rc.1",
},
RepoDigests: []string{
"gcr.io/datadoghq/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
Status: model.SBOMStatus_PENDING,
},
Expand All @@ -486,6 +477,9 @@ func TestProcessEvents(t *testing.T) {
"7-rc",
"7.41.1-rc.1",
},
RepoDigests: []string{
"public.ecr.aws/datadog/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
Status: model.SBOMStatus_PENDING,
},
Expand Down Expand Up @@ -536,6 +530,9 @@ func TestProcessEvents(t *testing.T) {
"7-rc",
"7.41.1-rc.1",
},
RepoDigests: []string{
"datadog/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
Sbom: &model.SBOMEntity_Error{
Error: "error",
Expand All @@ -556,6 +553,9 @@ func TestProcessEvents(t *testing.T) {
"7-rc",
"7.41.1-rc.1",
},
RepoDigests: []string{
"gcr.io/datadoghq/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
Sbom: &model.SBOMEntity_Error{
Error: "error",
Expand All @@ -576,6 +576,9 @@ func TestProcessEvents(t *testing.T) {
"7-rc",
"7.41.1-rc.1",
},
RepoDigests: []string{
"public.ecr.aws/datadog/agent@sha256:052f1fdf4f9a7117d36a1838ab60782829947683007c34b69d4991576375c409",
},
InUse: false,
Sbom: &model.SBOMEntity_Error{
Error: "error",
Expand Down
2 changes: 1 addition & 1 deletion test/fakeintake/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go 1.21
replace github.com/DataDog/datadog-agent/pkg/proto => ../../pkg/proto

require (
github.com/DataDog/agent-payload/v5 v5.0.100
github.com/DataDog/agent-payload/v5 v5.0.103-0.20240118142331-3069f58aa284
github.com/DataDog/datadog-agent/pkg/proto v0.50.2
github.com/benbjohnson/clock v1.3.5
github.com/cenkalti/backoff v2.2.1+incompatible
Expand Down
4 changes: 2 additions & 2 deletions test/fakeintake/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/new-e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ replace (
)

require (
github.com/DataDog/agent-payload/v5 v5.0.100
github.com/DataDog/agent-payload/v5 v5.0.103-0.20240118142331-3069f58aa284
github.com/DataDog/datadog-agent/pkg/util/pointer v0.51.0-rc.2
github.com/DataDog/datadog-agent/pkg/util/testutil v0.51.0-rc.2
github.com/DataDog/datadog-agent/pkg/version v0.51.0-rc.2
Expand Down
4 changes: 2 additions & 2 deletions test/new-e2e/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.