Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

refactor(scanner): propagate context to scanners/analyzers #1741

Merged
merged 10 commits into from
Jun 11, 2024
2 changes: 1 addition & 1 deletion plugins/runner/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
// Create plugin runner
fmt.Printf("Starting plugin runner\n")
config := LoadConfig()
runner, err := runner.New(config)
runner, err := runner.New(ctx, config)
if err != nil {
fmt.Println(err)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type containerManager struct {
runningErr atomic.Pointer[error]
}

func New(config types.PluginConfig) (containermanager.PluginContainerManager, error) {
func New(ctx context.Context, config types.PluginConfig) (containermanager.PluginContainerManager, error) {
// Load docker client
client, err := newDockerClient()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions plugins/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ type pluginRunner struct {
client runnerclient.ClientWithResponsesInterface
}

func New(config types.PluginConfig) (types.PluginRunner, error) {
func New(ctx context.Context, config types.PluginConfig) (types.PluginRunner, error) {
// Create docker container
// TODO: switch to factory once the support for more container engines is added
manager, err := docker.New(config)
manager, err := docker.New(ctx, config)
if err != nil {
return nil, fmt.Errorf("failed to create plugin manager: %w", err)
}
Expand Down
10 changes: 5 additions & 5 deletions scanner/analyzer/syft/syft.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ func New(_ string, c job_manager.IsConfig, logger *log.Entry, resultChan chan jo
}
}

func (a *Analyzer) Run(sourceType utils.SourceType, userInput string) error {
func (a *Analyzer) Run(ctx context.Context, sourceType utils.SourceType, userInput string) error {
src := utils.CreateSource(sourceType, a.localImage)

a.logger.Infof("Called %s analyzer on source %s", a.name, src)
// TODO platform can be defined
// https://github.com/anchore/syft/blob/b20310eaf847c259beb4fe5128c842bd8aa4d4fc/cmd/syft/cli/options/packages.go#L48
source, err := syft.GetSource(
context.Background(),
ctx,
userInput,
syft.DefaultGetSourceConfig().WithSources(src).WithRegistryOptions(a.config.RegistryOptions),
)
if err != nil {
return fmt.Errorf("failed to create source analyzer=%s: %w", a.name, err)
}

go func() {
go func(ctx context.Context) {
res := &analyzer.Results{}

sbomConfig := syft.DefaultCreateSBOMConfig().
WithSearchConfig(cataloging.DefaultSearchConfig().WithScope(a.config.Scope))

sbom, err := syft.CreateSBOM(context.TODO(), source, sbomConfig)
sbom, err := syft.CreateSBOM(ctx, source, sbomConfig)
if err != nil {
a.setError(res, fmt.Errorf("failed to write results: %w", err))
return
Expand Down Expand Up @@ -110,7 +110,7 @@ func (a *Analyzer) Run(sourceType utils.SourceType, userInput string) error {

a.logger.Infof("Sending successful results")
a.resultChan <- res
}()
}(ctx)

return nil
}
Expand Down
8 changes: 4 additions & 4 deletions scanner/analyzer/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func New(_ string, c job_manager.IsConfig, logger *log.Entry, resultChan chan jo
}

// nolint:cyclop
func (a *Analyzer) Run(sourceType utils.SourceType, userInput string) error {
func (a *Analyzer) Run(ctx context.Context, sourceType utils.SourceType, userInput string) error {
a.logger.Infof("Called %s analyzer on source %v %v", a.name, sourceType, userInput)

tempFile, err := os.CreateTemp(a.config.TempDir, "trivy.sbom.*.json")
Expand All @@ -75,7 +75,7 @@ func (a *Analyzer) Run(sourceType utils.SourceType, userInput string) error {
return fmt.Errorf("unable to get db options: %w", err)
}

go func() {
go func(ctx context.Context) {
defer os.Remove(tempFile.Name())

res := &analyzer.Results{}
Expand Down Expand Up @@ -139,7 +139,7 @@ func (a *Analyzer) Run(sourceType utils.SourceType, userInput string) error {
// Ensure we're configured for private registry if required
trivyOptions = trivy.SetTrivyRegistryConfigs(a.config.Registry, trivyOptions)

err = artifact.Run(context.TODO(), trivyOptions, trivySourceType)
err = artifact.Run(ctx, trivyOptions, trivySourceType)
if err != nil {
a.setError(res, fmt.Errorf("failed to generate SBOM: %w", err))
return
Expand Down Expand Up @@ -182,7 +182,7 @@ func (a *Analyzer) Run(sourceType utils.SourceType, userInput string) error {

a.logger.Infof("Sending successful results")
a.resultChan <- res
}()
}(ctx)

return nil
}
Expand Down
3 changes: 2 additions & 1 deletion scanner/analyzer/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package windows

import (
"context"
"fmt"

log "github.com/sirupsen/logrus"
Expand All @@ -42,7 +43,7 @@ func New(_ string, _ job_manager.IsConfig, logger *log.Entry, resultChan chan jo
}

// nolint:cyclop
func (a *Analyzer) Run(sourceType utils.SourceType, userInput string) error {
func (a *Analyzer) Run(ctx context.Context, sourceType utils.SourceType, userInput string) error {
a.logger.Infof("Called %s analyzer on source %v %v", a.name, sourceType, userInput)

go func() {
Expand Down
9 changes: 5 additions & 4 deletions scanner/families/exploits/exploitdb/exploitdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package exploitdb

import (
"context"
"fmt"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -45,8 +46,8 @@ func New(_ string, c job_manager.IsConfig, logger *log.Entry, resultChan chan jo
}
}

func (a *Scanner) Run(sourceType utils.SourceType, userInput string) error {
go func() {
func (a *Scanner) Run(ctx context.Context, sourceType utils.SourceType, userInput string) error {
go func(ctx context.Context) {
retResults := common.Results{
ScannedInput: userInput,
ScannerName: ScannerName,
Expand All @@ -63,15 +64,15 @@ func (a *Scanner) Run(sourceType utils.SourceType, userInput string) error {
}

// get exploits (get request to exploit db)
exploits, err := a.getExploitsFromCVEIDs(cveIDs)
exploits, err := a.getExploitsFromCVEIDs(ctx, cveIDs)
if err != nil {
a.sendResults(retResults, fmt.Errorf("failed to get exploits from cve ids: %w", err))
return
}
retResults.Exploits = exploits

a.sendResults(retResults, nil)
}()
}(ctx)

return nil
}
Expand Down
12 changes: 6 additions & 6 deletions scanner/families/exploits/exploitdb/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ func stringToArray(str string) []string {
return cveIDs
}

func (a *Scanner) getExploitsFromCVEIDs(cveIDs []string) ([]common.Exploit, error) {
func (a *Scanner) getExploitsFromCVEIDs(ctx context.Context, cveIDs []string) ([]common.Exploit, error) {
var ret []common.Exploit
prefix, err := url.JoinPath(a.config.BaseURL, "cves")
if err != nil {
return nil, fmt.Errorf("failed to join URLPath: %w", err)
}
responses, err := getExploitsViaHTTP(cveIDs, prefix)
responses, err := getExploitsViaHTTP(ctx, cveIDs, prefix)
if err != nil {
return nil, fmt.Errorf("failed to get exploits via HTTP: %w", err)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ const (
taskTimeoutSec = 30
)

func getExploitsViaHTTP(cveIDs []string, urlPrefix string) ([]exploitResponse, error) {
func getExploitsViaHTTP(ctx context.Context, cveIDs []string, urlPrefix string) ([]exploitResponse, error) {
var responses []exploitResponse

numCVEs := len(cveIDs)
Expand All @@ -127,7 +127,7 @@ func getExploitsViaHTTP(cveIDs []string, urlPrefix string) ([]exploitResponse, e
return
}
log.Debugf("HTTP Request to %s", URL)
httpGetExploit(URL, req, resChan, errChan)
httpGetExploit(ctx, URL, req, resChan, errChan)
}
}

Expand All @@ -150,13 +150,13 @@ func getExploitsViaHTTP(cveIDs []string, urlPrefix string) ([]exploitResponse, e
return responses, nil
}

func httpGetExploit(url string, req exploitRequest, resChan chan<- exploitResponse, errChan chan<- error) {
func httpGetExploit(ctx context.Context, url string, req exploitRequest, resChan chan<- exploitResponse, errChan chan<- error) {
var body string
var resp *http.Response
var maxRetries uint64 = 3

requestFn := func() error {
ctx, cancel := context.WithTimeout(context.Background(), taskTimeoutSec*time.Second)
ctx, cancel := context.WithTimeout(ctx, taskTimeoutSec*time.Second)
defer cancel()

r, err := http.NewRequest(http.MethodGet, url, nil)
Expand Down
2 changes: 1 addition & 1 deletion scanner/families/exploits/family.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (e Exploits) Run(ctx context.Context, res *results.Results) (interfaces.IsR
var exploitsResults Results
for _, input := range e.conf.Inputs {
startTime := time.Now()
managerResults, err := manager.Run(utils.SourceType(input.InputType), input.Input)
managerResults, err := manager.Run(ctx, utils.SourceType(input.InputType), input.Input)
if err != nil {
return nil, fmt.Errorf("failed to scan input %q for exploits: %w", input.Input, err)
}
Expand Down
2 changes: 1 addition & 1 deletion scanner/families/infofinder/family.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (i InfoFinder) Run(ctx context.Context, _ *results.Results) (interfaces.IsR
manager := job_manager.New(i.conf.ScannersList, i.conf.ScannersConfig, logger, job.Factory)
for _, input := range i.conf.Inputs {
startTime := time.Now()
managerResults, err := manager.Run(utils.SourceType(input.InputType), input.Input)
managerResults, err := manager.Run(ctx, utils.SourceType(input.InputType), input.Input)
if err != nil {
return nil, fmt.Errorf("failed to scan input %q for info: %w", input.Input, err)
}
Expand Down
8 changes: 4 additions & 4 deletions scanner/families/infofinder/sshtopology/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func New(_ string, c job_manager.IsConfig, logger *log.Entry, resultChan chan jo
}

// nolint:cyclop,gocognit
func (s *Scanner) Run(sourceType utils.SourceType, userInput string) error {
go func() {
func (s *Scanner) Run(ctx context.Context, sourceType utils.SourceType, userInput string) error {
go func(ctx context.Context) {
s.logger.Debugf("Running with input=%v and source type=%v", userInput, sourceType)
retResults := types.ScannerResult{
ScannerName: ScannerName,
Expand All @@ -69,7 +69,7 @@ func (s *Scanner) Run(sourceType utils.SourceType, userInput string) error {
return
}

fsPath, cleanup, err := familiesutils.ConvertInputToFilesystem(context.TODO(), sourceType, userInput)
fsPath, cleanup, err := familiesutils.ConvertInputToFilesystem(ctx, sourceType, userInput)
if err != nil {
s.sendResults(retResults, fmt.Errorf("failed to convert input to filesystem: %w", err))
return
Expand Down Expand Up @@ -163,7 +163,7 @@ func (s *Scanner) Run(sourceType utils.SourceType, userInput string) error {
}

s.sendResults(retResults, nil)
}()
}(ctx)

return nil
}
Expand Down
8 changes: 4 additions & 4 deletions scanner/families/malware/clam/clam.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ type Scanner struct {
}

// nolint: cyclop
func (s *Scanner) Run(sourceType utils.SourceType, userInput string) error {
go func() {
func (s *Scanner) Run(ctx context.Context, sourceType utils.SourceType, userInput string) error {
go func(ctx context.Context) {
retResults := common.Results{
Source: userInput,
ScannerName: ScannerName,
Expand Down Expand Up @@ -107,7 +107,7 @@ func (s *Scanner) Run(sourceType utils.SourceType, userInput string) error {
}
s.logger.Infof("freshclam has finished running: %s", string(freshclamOut))

fsPath, cleanup, err := familiesutils.ConvertInputToFilesystem(context.TODO(), sourceType, userInput)
fsPath, cleanup, err := familiesutils.ConvertInputToFilesystem(ctx, sourceType, userInput)
if err != nil {
s.sendResults(retResults, fmt.Errorf("failed to convert input to filesystem: %w", err))
return
Expand Down Expand Up @@ -140,7 +140,7 @@ func (s *Scanner) Run(sourceType utils.SourceType, userInput string) error {
retResults.Summary = summary

s.sendResults(retResults, nil)
}()
}(ctx)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion scanner/families/malware/family.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (m Malware) Run(ctx context.Context, _ *results.Results) (interfaces.IsResu

for _, input := range m.conf.Inputs {
startTime := time.Now()
resultArr, err := manager.Run(utils.SourceType(input.InputType), input.Input)
resultArr, err := manager.Run(ctx, utils.SourceType(input.InputType), input.Input)
if err != nil {
return nil, fmt.Errorf("failed to scan input %q for malware: %w", input.Input, err)
}
Expand Down
8 changes: 4 additions & 4 deletions scanner/families/malware/yara/yara.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type Scanner struct {
}

// nolint: gocognit,cyclop
func (s *Scanner) Run(sourceType utils.SourceType, userInput string) error {
go func() {
func (s *Scanner) Run(ctx context.Context, sourceType utils.SourceType, userInput string) error {
go func(ctx context.Context) {
retResults := common.Results{
Source: userInput,
ScannerName: ScannerName,
Expand All @@ -83,7 +83,7 @@ func (s *Scanner) Run(sourceType utils.SourceType, userInput string) error {
s.logger.Debugf("Yara rules URL: %s", s.config.CompiledRuleURL)
s.logger.Debugf("Yara rules file path: %s", s.compiledRuleFile)

fsPath, cleanup, err := familiesutils.ConvertInputToFilesystem(context.TODO(), sourceType, userInput)
fsPath, cleanup, err := familiesutils.ConvertInputToFilesystem(ctx, sourceType, userInput)
if err != nil {
s.sendResults(retResults, fmt.Errorf("failed to convert input to filesystem: %w", err))
return
Expand Down Expand Up @@ -164,7 +164,7 @@ func (s *Scanner) Run(sourceType utils.SourceType, userInput string) error {
retResults.Summary = &common.ScanSummary{}

s.sendResults(retResults, nil)
}()
}(ctx)

return nil
}
Expand Down
7 changes: 4 additions & 3 deletions scanner/families/misconfiguration/cisdocker/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cisdocker

import (
"context"
"fmt"

dockle_run "github.com/Portshift/dockle/pkg"
Expand Down Expand Up @@ -47,8 +48,8 @@ func New(_ string, c job_manager.IsConfig, logger *logrus.Entry, resultChan chan
}
}

func (a *Scanner) Run(sourceType utils.SourceType, userInput string) error {
go func() {
func (a *Scanner) Run(ctx context.Context, sourceType utils.SourceType, userInput string) error {
go func(ctx context.Context) {
retResults := types.ScannerResult{
ScannerName: ScannerName,
}
Expand All @@ -72,7 +73,7 @@ func (a *Scanner) Run(sourceType utils.SourceType, userInput string) error {
retResults.Misconfigurations = parseDockleReport(sourceType, userInput, assessmentMap)

a.sendResults(retResults, nil)
}()
}(ctx)

return nil
}
Expand Down
8 changes: 5 additions & 3 deletions scanner/families/misconfiguration/fake/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package fake

import (
"context"

log "github.com/sirupsen/logrus"

misconfigurationTypes "github.com/openclarity/vmclarity/scanner/families/misconfiguration/types"
Expand All @@ -39,15 +41,15 @@ func New(_ string, _ job_manager.IsConfig, logger *log.Entry, resultChan chan jo
}
}

func (a *Scanner) Run(sourceType utils.SourceType, userInput string) error {
go func() {
func (a *Scanner) Run(ctx context.Context, sourceType utils.SourceType, userInput string) error {
go func(ctx context.Context) {
retResults := misconfigurationTypes.ScannerResult{
ScannerName: ScannerName,
Misconfigurations: createFakeMisconfigurationReport(),
}

a.sendResults(retResults, nil)
}()
}(ctx)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion scanner/families/misconfiguration/family.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (m Misconfiguration) Run(ctx context.Context, _ *results.Results) (interfac
manager := job_manager.New(m.conf.ScannersList, m.conf.ScannersConfig, logger, job.Factory)
for _, input := range m.conf.Inputs {
startTime := time.Now()
managerResults, err := manager.Run(utils.SourceType(input.InputType), input.Input)
managerResults, err := manager.Run(ctx, utils.SourceType(input.InputType), input.Input)
if err != nil {
return nil, fmt.Errorf("failed to scan input %q for misconfigurations: %w", input.Input, err)
}
Expand Down
Loading
Loading