github.com/quay/claircore@v1.5.28/ubuntu/matcher.go (about)

     1  package ubuntu
     2  
     3  import (
     4  	"context"
     5  
     6  	version "github.com/knqyf263/go-deb-version"
     7  
     8  	"github.com/quay/claircore"
     9  	"github.com/quay/claircore/libvuln/driver"
    10  )
    11  
    12  var _ driver.Matcher = (*Matcher)(nil)
    13  
    14  // Matcher is a [driver.Matcher] for Ubuntu distributions.
    15  type Matcher struct{}
    16  
    17  // Name implements [driver.Matcher].
    18  func (*Matcher) Name() string {
    19  	return "ubuntu-matcher"
    20  }
    21  
    22  // Filter implements [driver.Matcher].
    23  func (*Matcher) Filter(record *claircore.IndexRecord) bool {
    24  	if record.Distribution == nil {
    25  		return false
    26  	}
    27  
    28  	switch {
    29  	case record.Distribution.DID == "ubuntu":
    30  		return true
    31  	case record.Distribution.Name == "Ubuntu":
    32  		return true
    33  	default:
    34  		return false
    35  	}
    36  }
    37  
    38  // Query implements [driver.Matcher].
    39  func (*Matcher) Query() []driver.MatchConstraint {
    40  	return []driver.MatchConstraint{
    41  		driver.DistributionDID,
    42  		driver.DistributionName,
    43  		driver.DistributionVersion,
    44  	}
    45  }
    46  
    47  // Vulnerable implements [driver.Matcher].
    48  func (*Matcher) Vulnerable(ctx context.Context, record *claircore.IndexRecord, vuln *claircore.Vulnerability) (bool, error) {
    49  	if vuln.FixedInVersion == "" {
    50  		return true, nil
    51  	}
    52  
    53  	v1, err := version.NewVersion(record.Package.Version)
    54  	if err != nil {
    55  		return false, err
    56  	}
    57  
    58  	v2, err := version.NewVersion(vuln.FixedInVersion)
    59  	if err != nil {
    60  		return false, err
    61  	}
    62  
    63  	if v2.String() == "0" {
    64  		return true, nil
    65  	}
    66  
    67  	if v1.LessThan(v2) {
    68  		return true, nil
    69  	}
    70  
    71  	return false, nil
    72  }