github.com/quay/claircore@v1.5.28/pkg/omnimatcher/omnimatcher.go (about)

     1  package omnimatcher
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/quay/claircore"
     7  	"github.com/quay/claircore/alpine"
     8  	"github.com/quay/claircore/aws"
     9  	"github.com/quay/claircore/debian"
    10  	"github.com/quay/claircore/gobin"
    11  	"github.com/quay/claircore/libvuln/driver"
    12  	"github.com/quay/claircore/python"
    13  	"github.com/quay/claircore/rhel"
    14  	"github.com/quay/claircore/ubuntu"
    15  )
    16  
    17  // defaultOmniMatcher is the default implementation
    18  // containing all in-tree matchers.
    19  var defaultOmniMatcher = []driver.Matcher{
    20  	&alpine.Matcher{},
    21  	&aws.Matcher{},
    22  	&debian.Matcher{},
    23  	&gobin.Matcher{},
    24  	&python.Matcher{},
    25  	&rhel.Matcher{},
    26  	&ubuntu.Matcher{},
    27  }
    28  
    29  // OmniMatcher is a aggregation of Matcher implementations.
    30  //
    31  // Its exported methods will call each implementation's method
    32  // of the same name and return the first true value.
    33  //
    34  // Currently Vulnerable is the only method implemented.
    35  type OmniMatcher []driver.Matcher
    36  
    37  // NewOmniMatcher is a constructor for an OmniMatcher.
    38  //
    39  // If a nil array of Matchers is provided the default
    40  // containing all in-tree matchers is used.
    41  func New(m []driver.Matcher) OmniMatcher {
    42  	if m == nil {
    43  		return defaultOmniMatcher
    44  	}
    45  	return m
    46  }
    47  
    48  // Vulnerable will call each Matcher's Vulnerable method until one returns true.
    49  func (om OmniMatcher) Vulnerable(ctx context.Context, record *claircore.IndexRecord, vuln *claircore.Vulnerability) (bool, error) {
    50  	for _, m := range om {
    51  		applicable := m.Filter(record)
    52  		if !applicable {
    53  			continue
    54  		}
    55  		match, err := m.Vulnerable(ctx, record, vuln)
    56  		if err != nil {
    57  			return false, err
    58  		}
    59  		if match {
    60  			return true, nil
    61  		}
    62  	}
    63  	return false, nil
    64  }