-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,245 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022-present Datadog, Inc. | ||
|
||
package sbom | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
yaml "gopkg.in/yaml.v2" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration" | ||
"github.com/DataDog/datadog-agent/pkg/collector/check" | ||
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks" | ||
ddConfig "github.com/DataDog/datadog-agent/pkg/config" | ||
"github.com/DataDog/datadog-agent/pkg/util/log" | ||
"github.com/DataDog/datadog-agent/pkg/workloadmeta" | ||
) | ||
|
||
const ( | ||
checkName = "sbom" | ||
) | ||
|
||
func init() { | ||
core.RegisterCheck(checkName, CheckFactory) | ||
} | ||
|
||
// Config holds the container_image check configuration | ||
type Config struct { | ||
chunkSize int `yaml:"chunk_size"` | ||
newSBOMMaxLatencySeconds int `yaml:"new_images_max_latency_seconds"` | ||
periodicRefreshSeconds int `yaml:"periodic_refresh_seconds"` | ||
} | ||
|
||
type configValueRange struct { | ||
min int | ||
max int | ||
default_ int | ||
} | ||
|
||
var /* const */ ( | ||
chunkSizeValueRange = &configValueRange{ | ||
min: 1, | ||
max: 100, | ||
default_: 1, | ||
} | ||
|
||
newSBOMMaxLatencySecondsValueRange = &configValueRange{ | ||
min: 1, // 1 s | ||
max: 300, // 5 min | ||
default_: 30, // 30 s | ||
} | ||
|
||
periodicRefreshSecondsValueRange = &configValueRange{ | ||
min: 60, // 1 min | ||
max: 604800, // 1 week | ||
default_: 3600, // 1h | ||
} | ||
) | ||
|
||
func validateValue(val *int, range_ *configValueRange) { | ||
if *val == 0 { | ||
*val = range_.default_ | ||
} else if *val < range_.min { | ||
*val = range_.min | ||
} else if *val > range_.max { | ||
*val = range_.max | ||
} | ||
} | ||
|
||
func (c *Config) Parse(data []byte) error { | ||
if err := yaml.Unmarshal(data, c); err != nil { | ||
return err | ||
} | ||
|
||
validateValue(&c.chunkSize, chunkSizeValueRange) | ||
validateValue(&c.newSBOMMaxLatencySeconds, newSBOMMaxLatencySecondsValueRange) | ||
validateValue(&c.periodicRefreshSeconds, periodicRefreshSecondsValueRange) | ||
|
||
return nil | ||
} | ||
|
||
// Check reports SBOM | ||
type Check struct { | ||
core.CheckBase | ||
workloadmetaStore workloadmeta.Store | ||
instance *Config | ||
processor *processor | ||
stopCh chan struct{} | ||
} | ||
|
||
// CheckFactory registers the sbom check | ||
func CheckFactory() check.Check { | ||
return &Check{ | ||
CheckBase: core.NewCheckBase(checkName), | ||
workloadmetaStore: workloadmeta.GetGlobalStore(), | ||
instance: &Config{}, | ||
stopCh: make(chan struct{}), | ||
} | ||
} | ||
|
||
// Configure parses the check configuration and initializes the sbom check | ||
func (c *Check) Configure(integrationConfigDigest uint64, config, initConfig integration.Data, source string) error { | ||
if !ddConfig.Datadog.GetBool("sbom.enabled") { | ||
return errors.New("collection of SBOM is disabled") | ||
} | ||
|
||
if err := c.CommonConfigure(integrationConfigDigest, initConfig, config, source); err != nil { | ||
return err | ||
} | ||
|
||
if err := c.instance.Parse(config); err != nil { | ||
return err | ||
} | ||
|
||
sender, err := c.GetSender() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.processor = newProcessor(sender, c.instance.chunkSize, time.Duration(c.instance.newSBOMMaxLatencySeconds)*time.Second) | ||
|
||
return nil | ||
} | ||
|
||
// Run starts the sbom check | ||
func (c *Check) Run() error { | ||
log.Infof("Starting long-running check %q", c.ID()) | ||
defer log.Infof("Shutting down long-running check %q", c.ID()) | ||
|
||
imgEventsCh := c.workloadmetaStore.Subscribe( | ||
checkName, | ||
workloadmeta.NormalPriority, | ||
workloadmeta.NewFilter( | ||
[]workloadmeta.Kind{workloadmeta.KindContainerImageMetadata}, | ||
workloadmeta.SourceAll, | ||
workloadmeta.EventTypeSet, // We don’t care about SBOM removal because we just have to wait for them to expire on BE side once we stopped refreshing them periodically. | ||
), | ||
) | ||
|
||
imgRefreshTicker := time.NewTicker(time.Duration(c.instance.periodicRefreshSeconds) * time.Second) | ||
|
||
for { | ||
select { | ||
case eventBundle := <-imgEventsCh: | ||
c.processor.processEvents(eventBundle) | ||
case <-imgRefreshTicker.C: | ||
c.processor.processRefresh(c.workloadmetaStore.ListImages()) | ||
case <-c.stopCh: | ||
c.processor.stop() | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// Stop stops the sbom check | ||
func (c *Check) Stop() { | ||
close(c.stopCh) | ||
} | ||
|
||
// Interval returns 0. It makes sbom a long-running check | ||
func (c *Check) Interval() time.Duration { | ||
return 0 | ||
} |
Oops, something went wrong.