Skip to content

Commit

Permalink
[CWS] Fix SBOM generation for flare in k8s environment (#29419)
Browse files Browse the repository at this point in the history
  • Loading branch information
lebauce authored Sep 19, 2024
1 parent 8f8f146 commit 04902d8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
3 changes: 1 addition & 2 deletions comp/collector/collector/collectorimpl/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"sync"
"time"

Expand Down Expand Up @@ -149,7 +148,7 @@ func (c *collectorImpl) fillFlare(fb flaretypes.FlareBuilder) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

scanRequest := host.NewScanRequest("/", os.DirFS("/"))
scanRequest := host.NewHostScanRequest()
scanResult := scanner.PerformScan(ctx, scanRequest, scanner.GetCollector(scanRequest.Collector()))
if scanResult.Error != nil {
return scanResult.Error
Expand Down
32 changes: 1 addition & 31 deletions pkg/collector/corechecks/sbom/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ package sbom
import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -21,7 +18,6 @@ import (
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/config/env"

pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/sbom"
Expand Down Expand Up @@ -222,39 +218,13 @@ func (p *processor) processHostScanResult(result sbom.ScanResult) {
p.queue <- sbom
}

type relFS struct {
root string
fs fs.FS
}

func newFS(root string) fs.FS {
fs := os.DirFS(root)
return &relFS{root: "/", fs: fs}
}

func (f *relFS) Open(name string) (fs.File, error) {
if filepath.IsAbs(name) {
var err error
name, err = filepath.Rel(f.root, name)
if err != nil {
return nil, err
}
}

return f.fs.Open(name)
}

func (p *processor) triggerHostScan() {
if !p.hostSBOM {
return
}
log.Debugf("Triggering host SBOM refresh")

scanPath := "/"
if hostRoot := os.Getenv("HOST_ROOT"); env.IsContainerized() && hostRoot != "" {
scanPath = hostRoot
}
scanRequest := host.NewScanRequest(scanPath, newFS("/"))
scanRequest := host.NewHostScanRequest()

if err := p.sbomScanner.Scan(scanRequest); err != nil {
log.Errorf("Failed to trigger SBOM generation for host: %s", err)
Expand Down
34 changes: 34 additions & 0 deletions pkg/sbom/collectors/host/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package host

import (
"io/fs"
"os"
"path/filepath"

"github.com/DataDog/datadog-agent/pkg/config/env"
"github.com/DataDog/datadog-agent/pkg/sbom/types"
)

Expand All @@ -18,11 +21,42 @@ type scanRequest struct {
FS fs.FS
}

type relFS struct {
root string
fs fs.FS
}

func newFS(root string) fs.FS {
fs := os.DirFS(root)
return &relFS{root: "/", fs: fs}
}

func (f *relFS) Open(name string) (fs.File, error) {
if filepath.IsAbs(name) {
var err error
name, err = filepath.Rel(f.root, name)
if err != nil {
return nil, err
}
}

return f.fs.Open(name)
}

// NewScanRequest creates a new scan request
func NewScanRequest(path string, fs fs.FS) types.ScanRequest {
return scanRequest{Path: path, FS: fs}
}

// NewHostScanRequest creates a new scan request for the root filesystem
func NewHostScanRequest() types.ScanRequest {
scanPath := "/"
if hostRoot := os.Getenv("HOST_ROOT"); env.IsContainerized() && hostRoot != "" {
scanPath = hostRoot
}
return NewScanRequest(scanPath, newFS("/"))
}

// Collector returns the collector name
func (r scanRequest) Collector() string {
return "host"
Expand Down

0 comments on commit 04902d8

Please sign in to comment.