From 6945b6ea29d3b5aa0e4b2df996053d6575b9d874 Mon Sep 17 00:00:00 2001 From: Benji Visser Date: Thu, 23 Feb 2023 12:39:08 -0700 Subject: [PATCH] feat: haproxy binary matcher (#1591) Signed-off-by: Benji Visser --- syft/pkg/cataloger/binary/cataloger_test.go | 36 ++++++++++++++++++ syft/pkg/cataloger/binary/classifier.go | 15 ++++++++ .../cataloger/binary/default_classifiers.go | 11 ++++++ .../positive/haproxy-1.5.14/haproxy | 1 + .../positive/haproxy-1.8.22/haproxy | Bin 0 -> 109 bytes .../positive/haproxy-2.7.3/haproxy | Bin 0 -> 5474 bytes 6 files changed, 63 insertions(+) create mode 100644 syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-1.5.14/haproxy create mode 100644 syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-1.8.22/haproxy create mode 100644 syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-2.7.3/haproxy diff --git a/syft/pkg/cataloger/binary/cataloger_test.go b/syft/pkg/cataloger/binary/cataloger_test.go index 41f3348faf42..faa8b2dcd665 100644 --- a/syft/pkg/cataloger/binary/cataloger_test.go +++ b/syft/pkg/cataloger/binary/cataloger_test.go @@ -207,6 +207,42 @@ func Test_Cataloger_DefaultClassifiers_PositiveCases(t *testing.T) { Metadata: metadata("perl-binary"), }, }, + { + name: "positive-haproxy-1.5.14", + fixtureDir: "test-fixtures/classifiers/positive/haproxy-1.5.14", + expected: pkg.Package{ + Name: "haproxy", + Version: "1.5.14", + Type: "binary", + PURL: "pkg:generic/haproxy@1.5.14", + Locations: locations("haproxy"), + Metadata: metadata("haproxy-binary"), + }, + }, + { + name: "positive-haproxy-1.8.22", + fixtureDir: "test-fixtures/classifiers/positive/haproxy-1.8.22", + expected: pkg.Package{ + Name: "haproxy", + Version: "1.8.22", + Type: "binary", + PURL: "pkg:generic/haproxy@1.8.22", + Locations: locations("haproxy"), + Metadata: metadata("haproxy-binary"), + }, + }, + { + name: "positive-haproxy-2.7.3", + fixtureDir: "test-fixtures/classifiers/positive/haproxy-2.7.3", + expected: pkg.Package{ + Name: "haproxy", + Version: "2.7.3", + Type: "binary", + PURL: "pkg:generic/haproxy@2.7.3", + Locations: locations("haproxy"), + Metadata: metadata("haproxy-binary"), + }, + }, { name: "positive-redis-2.8.23", fixtureDir: "test-fixtures/classifiers/positive/redis-server-2.8.23", diff --git a/syft/pkg/cataloger/binary/classifier.go b/syft/pkg/cataloger/binary/classifier.go index d625166b03f3..15272e92dce1 100644 --- a/syft/pkg/cataloger/binary/classifier.go +++ b/syft/pkg/cataloger/binary/classifier.go @@ -51,6 +51,21 @@ type classifier struct { // evidenceMatcher is a function called to catalog Packages that match some sort of evidence type evidenceMatcher func(classifier classifier, reader source.LocationReadCloser) ([]pkg.Package, error) +func evidenceMatchers(matchers ...evidenceMatcher) evidenceMatcher { + return func(classifier classifier, reader source.LocationReadCloser) ([]pkg.Package, error) { + for _, matcher := range matchers { + match, err := matcher(classifier, reader) + if err != nil { + return nil, err + } + if match != nil { + return match, nil + } + } + return nil, nil + } +} + func fileNameTemplateVersionMatcher(fileNamePattern string, contentTemplate string) evidenceMatcher { pat := regexp.MustCompile(fileNamePattern) return func(classifier classifier, reader source.LocationReadCloser) ([]pkg.Package, error) { diff --git a/syft/pkg/cataloger/binary/default_classifiers.go b/syft/pkg/cataloger/binary/default_classifiers.go index 46bf31ca7803..75e7befc3d5a 100644 --- a/syft/pkg/cataloger/binary/default_classifiers.go +++ b/syft/pkg/cataloger/binary/default_classifiers.go @@ -120,6 +120,17 @@ var defaultClassifiers = []classifier{ Package: "busybox", CPEs: singleCPE("cpe:2.3:a:busybox:busybox:*:*:*:*:*:*:*:*"), }, + { + Class: "haproxy-binary", + FileGlob: "**/haproxy", + EvidenceMatcher: evidenceMatchers( + fileContentsVersionMatcher(`(?m)HA-Proxy version (?P[0-9]+\.[0-9]+\.[0-9]+)`), + fileContentsVersionMatcher(`(?m)(?P[0-9]+\.[0-9]+\.[0-9]+)-[0-9a-zA-Z]{7}.+HAProxy version`), + ), + Package: "haproxy", + PURL: mustPURL("pkg:generic/haproxy@version"), + CPEs: singleCPE("cpe:2.3:a:haproxy:haproxy:*:*:*:*:*:*:*:*"), + }, { Class: "perl-binary", FileGlob: "**/perl", diff --git a/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-1.5.14/haproxy b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-1.5.14/haproxy new file mode 100644 index 000000000000..7fc2c2a35b47 --- /dev/null +++ b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-1.5.14/haproxy @@ -0,0 +1 @@ +HA-Proxy version 1.5.14 2015/07/02Copyright 2000-2015 Willy Tarreau diff --git a/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-1.8.22/haproxy b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-1.8.22/haproxy new file mode 100644 index 0000000000000000000000000000000000000000..a424ab0ac667b0abc2815a3184749cfcfa20b00a GIT binary patch literal 109 zcmY$*$w|>x1rmA;AmHJs8&H&AQK?XtT2!2wpQm7`XQ5|gq+n!VXsK^#pl@UfR>t6* xUrS1xYJ}XXfNoDug5!6{RMYD%g~RxDFYK1t9bE@{7{#xBwY*9>f3u literal 0 HcmV?d00001 diff --git a/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-2.7.3/haproxy b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/haproxy-2.7.3/haproxy new file mode 100644 index 0000000000000000000000000000000000000000..da21a03ddc887b7b5cf87c55c037959fbaee7cea GIT binary patch literal 5474 zcmeH}*-{fx5QYcu^hPaxfTJjGkW3O5#ckNb9+m{r5(msg1CfzLv#?mjgIT_fx4eQs znNxixEtxUPNI~T*iqq5Q^xyyKNu_e~>*vX(`}=)-eEBXF^o_c@g6_~R{j0u08ga*m zyFNVh;gJuoeR#ll!Z>7fOI}G%FM1Cc;2n4met?aQqoWt*fu>6$lBi@;@=;<*vJzWT zkbIK}a3A!65D0@9=m!H}5DbB*;2C%cM!*NK1=1h`K7r352flz^@D=32cQ8CWIyyEs zK0YxqIXN{Ik55m}%*@Wt&CSm*EG#Z25=%?V%PT9ZtI1?4wYIjtzP-J(v$t0)mP+Mv zrLw=@^SJlb5;ELdp~)uXYn9t}tOtDhP6 zsMVC=M0_NXx62ih&Dgn|T`Fk!NL~+B6>1_pQ1%9oPK%tJpva)eqzJ*8ajW$Zy-A+j z9d7-dRlT&^&aS_|dcFursc>?BwB#5`iBNHsBW z*2#GkxzAWqA>*tb(%72!6tMe?GiT+LO}Y8m zG9mYCGYXpb}6KE2$^t&U0Bo zn+8RVHPEO|MS^k$MJ7cE>c!Gt^`~^_UuOcH33MiK{u79pJyjD|q!s52th@SzICl28 J7IxCt{s2GBxzGRr literal 0 HcmV?d00001