mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-22 23:26:39 -08:00
* wip: Add a failing test to demo severity override Signed-off-by: Simarpreet Singh <simar@linux.com> * scan.go: Return osFound for use in determining vendor. Signed-off-by: Simarpreet Singh <simar@linux.com> * pkg: Fix ScanImage return in case an OSFound Signed-off-by: Simarpreet Singh <simar@linux.com> * scan_test: Include a package-lock.json for happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * wip: Add a test to include various reportResult types Signed-off-by: Simarpreet Singh <simar@linux.com> * Makefile: Add a target to generate mocks. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Pass reportType as argument for FillInfo. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Add other types of vulnerabilities. Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Update golden files. Signed-off-by: Simarpreet Singh <simar@linux.com> * ospkg: Fix FillInfo for ospkg/server Signed-off-by: Simarpreet Singh <simar@linux.com> * rpc: Add os.Family type to Response. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability_test.go: Add case where no vendor severity exists. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Fallback to NVD if it exists. Also add tests for other cases. Signed-off-by: Simarpreet Singh <simar@linux.com> * rpc: Fix a few sites with reportType info and tests. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Remove VendorSeverity from displayed results Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Add vulnerability source information. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Add VendorSeverity logic for lightDB as well. This commit also makes FillInfo logic common to both light and full DBs. Signed-off-by: Simarpreet Singh <simar@linux.com> * remove some crufty TODOs Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability_test: Add a case for light db for documentation purposes Signed-off-by: Simarpreet Singh <simar@linux.com> * mod: update trivy-db to point to master Signed-off-by: Simarpreet Singh <simar@linux.com> * scan_test: Remove cruft and bring back test cases Signed-off-by: Simarpreet Singh <simar@linux.com> * scan_test: Add pkg Type to mock return Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: reorder err check after err Signed-off-by: Simarpreet Singh <simar@linux.com> * client_test: Fix import ordering Signed-off-by: Simarpreet Singh <simar@linux.com> * convert.go: Use result.Type Signed-off-by: Simarpreet Singh <simar@linux.com> * convert: Use result.Type and simplify ConvertFromRpcResults signature Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Refactor calls to getVendorSeverity Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Remove centos-7-critical.json.golden There's no critical vulnerability in CentOS 7 anymore. In addition this test was not adding any value that is already not covered by existing tests cases. Signed-off-by: Simarpreet Singh <simar@linux.com> * rpc: Include severity source in tests. Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Update test db to include VendorSeverity. Test DB is now a snapshot of full database from trivy-db. Also update golden files to include SeveritySource. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Make centos7 use RHEL vendor severities Signed-off-by: Simarpreet Singh <simar@linux.com>
652 lines
18 KiB
Go
652 lines
18 KiB
Go
package local
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
ftypes "github.com/aquasecurity/fanal/types"
|
|
dtypes "github.com/aquasecurity/go-dep-parser/pkg/types"
|
|
ospkgDetector "github.com/aquasecurity/trivy/pkg/detector/ospkg"
|
|
"github.com/aquasecurity/trivy/pkg/report"
|
|
"github.com/aquasecurity/trivy/pkg/types"
|
|
)
|
|
|
|
func TestScanner_Scan(t *testing.T) {
|
|
type args struct {
|
|
target string
|
|
layerIDs []string
|
|
options types.ScanOptions
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
applyLayersExpectation ApplierApplyLayersExpectation
|
|
ospkgDetectExpectations []OspkgDetectorDetectExpectation
|
|
libDetectExpectations []LibraryDetectorDetectExpectation
|
|
wantResults report.Results
|
|
wantOS *ftypes.OS
|
|
wantEosl bool
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "happy path",
|
|
args: args{
|
|
target: "alpine:latest",
|
|
layerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
options: types.ScanOptions{VulnType: []string{"os", "library"}},
|
|
},
|
|
applyLayersExpectation: ApplierApplyLayersExpectation{
|
|
Args: ApplierApplyLayersArgs{
|
|
LayerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
},
|
|
Returns: ApplierApplyLayersReturns{
|
|
Detail: ftypes.ImageDetail{
|
|
OS: &ftypes.OS{
|
|
Family: "alpine",
|
|
Name: "3.11",
|
|
},
|
|
Packages: []ftypes.Package{
|
|
{
|
|
Name: "musl",
|
|
Version: "1.2.3",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:ebf12965380b39889c99a9c02e82ba465f887b45975b6e389d42e9e6a3857888",
|
|
},
|
|
},
|
|
},
|
|
Applications: []ftypes.Application{
|
|
{
|
|
Type: "bundler",
|
|
FilePath: "/app/Gemfile.lock",
|
|
Libraries: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "6.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:0ea33a93585cf1917ba522b2304634c3073654062d5282c1346322967790ef33",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
ospkgDetectExpectations: []OspkgDetectorDetectExpectation{
|
|
{
|
|
Args: OspkgDetectorDetectArgs{
|
|
OsFamily: "alpine",
|
|
OsName: "3.11",
|
|
Pkgs: []ftypes.Package{
|
|
{
|
|
Name: "musl",
|
|
Version: "1.2.3",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:ebf12965380b39889c99a9c02e82ba465f887b45975b6e389d42e9e6a3857888",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Returns: OspkgDetectorDetectReturns{
|
|
DetectedVulns: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-9999",
|
|
PkgName: "musl",
|
|
InstalledVersion: "1.2.3",
|
|
FixedVersion: "1.2.4",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:ebf12965380b39889c99a9c02e82ba465f887b45975b6e389d42e9e6a3857888",
|
|
},
|
|
},
|
|
},
|
|
Eosl: false,
|
|
},
|
|
},
|
|
},
|
|
libDetectExpectations: []LibraryDetectorDetectExpectation{
|
|
{
|
|
Args: LibraryDetectorDetectArgs{
|
|
FilePath: "/app/Gemfile.lock",
|
|
Pkgs: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "6.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:0ea33a93585cf1917ba522b2304634c3073654062d5282c1346322967790ef33",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Returns: LibraryDetectorDetectReturns{
|
|
DetectedVulns: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-10000",
|
|
PkgName: "rails",
|
|
InstalledVersion: "6.0",
|
|
FixedVersion: "6.1",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:0ea33a93585cf1917ba522b2304634c3073654062d5282c1346322967790ef33",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantResults: report.Results{
|
|
{
|
|
Target: "alpine:latest (alpine 3.11)",
|
|
Vulnerabilities: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-9999",
|
|
PkgName: "musl",
|
|
InstalledVersion: "1.2.3",
|
|
FixedVersion: "1.2.4",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:ebf12965380b39889c99a9c02e82ba465f887b45975b6e389d42e9e6a3857888",
|
|
},
|
|
},
|
|
},
|
|
Type: vulnerability.Alpine,
|
|
},
|
|
{
|
|
Target: "/app/Gemfile.lock",
|
|
Vulnerabilities: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-10000",
|
|
PkgName: "rails",
|
|
InstalledVersion: "6.0",
|
|
FixedVersion: "6.1",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:0ea33a93585cf1917ba522b2304634c3073654062d5282c1346322967790ef33",
|
|
},
|
|
},
|
|
},
|
|
Type: "bundler",
|
|
},
|
|
},
|
|
wantOS: &ftypes.OS{
|
|
Family: "alpine",
|
|
Name: "3.11",
|
|
},
|
|
},
|
|
{
|
|
name: "happy path with empty os",
|
|
args: args{
|
|
target: "alpine:latest",
|
|
layerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
options: types.ScanOptions{VulnType: []string{"os", "library"}},
|
|
},
|
|
applyLayersExpectation: ApplierApplyLayersExpectation{
|
|
Args: ApplierApplyLayersArgs{
|
|
LayerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
},
|
|
Returns: ApplierApplyLayersReturns{
|
|
Detail: ftypes.ImageDetail{
|
|
OS: &ftypes.OS{},
|
|
Applications: []ftypes.Application{
|
|
{
|
|
Type: "bundler",
|
|
FilePath: "/app/Gemfile.lock",
|
|
Libraries: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "6.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
libDetectExpectations: []LibraryDetectorDetectExpectation{
|
|
{
|
|
Args: LibraryDetectorDetectArgs{
|
|
FilePath: "/app/Gemfile.lock",
|
|
Pkgs: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "6.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Returns: LibraryDetectorDetectReturns{
|
|
DetectedVulns: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-10000",
|
|
PkgName: "rails",
|
|
InstalledVersion: "6.0",
|
|
FixedVersion: "6.1",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantResults: report.Results{
|
|
{
|
|
Target: "/app/Gemfile.lock",
|
|
Vulnerabilities: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-10000",
|
|
PkgName: "rails",
|
|
InstalledVersion: "6.0",
|
|
FixedVersion: "6.1",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
Type: "bundler",
|
|
},
|
|
},
|
|
wantOS: &ftypes.OS{},
|
|
},
|
|
{
|
|
name: "happy path with unknown os",
|
|
args: args{
|
|
target: "alpine:latest",
|
|
layerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
options: types.ScanOptions{VulnType: []string{"os", "library"}},
|
|
},
|
|
applyLayersExpectation: ApplierApplyLayersExpectation{
|
|
Args: ApplierApplyLayersArgs{
|
|
LayerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
},
|
|
Returns: ApplierApplyLayersReturns{
|
|
Detail: ftypes.ImageDetail{
|
|
OS: &ftypes.OS{
|
|
Family: "fedora",
|
|
Name: "27",
|
|
},
|
|
Applications: []ftypes.Application{
|
|
{
|
|
Type: "bundler",
|
|
FilePath: "/app/Gemfile.lock",
|
|
Libraries: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "6.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
ospkgDetectExpectations: []OspkgDetectorDetectExpectation{
|
|
{
|
|
Args: OspkgDetectorDetectArgs{
|
|
OsFamily: "fedora",
|
|
OsName: "27",
|
|
},
|
|
Returns: OspkgDetectorDetectReturns{
|
|
Err: ospkgDetector.ErrUnsupportedOS,
|
|
},
|
|
},
|
|
},
|
|
libDetectExpectations: []LibraryDetectorDetectExpectation{
|
|
{
|
|
Args: LibraryDetectorDetectArgs{
|
|
FilePath: "/app/Gemfile.lock",
|
|
Pkgs: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "6.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Returns: LibraryDetectorDetectReturns{
|
|
DetectedVulns: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-10000",
|
|
PkgName: "rails",
|
|
InstalledVersion: "6.0",
|
|
FixedVersion: "6.1",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantResults: report.Results{
|
|
{
|
|
Target: "/app/Gemfile.lock",
|
|
Vulnerabilities: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-10000",
|
|
PkgName: "rails",
|
|
InstalledVersion: "6.0",
|
|
FixedVersion: "6.1",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
Type: "bundler",
|
|
},
|
|
},
|
|
wantOS: &ftypes.OS{
|
|
Family: "fedora",
|
|
Name: "27",
|
|
},
|
|
},
|
|
{
|
|
name: "happy path with only library detection",
|
|
args: args{
|
|
target: "alpine:latest",
|
|
layerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
options: types.ScanOptions{VulnType: []string{"library"}},
|
|
},
|
|
applyLayersExpectation: ApplierApplyLayersExpectation{
|
|
Args: ApplierApplyLayersArgs{
|
|
LayerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
},
|
|
Returns: ApplierApplyLayersReturns{
|
|
Detail: ftypes.ImageDetail{
|
|
OS: &ftypes.OS{
|
|
Family: "alpine",
|
|
Name: "3.11",
|
|
},
|
|
Packages: []ftypes.Package{
|
|
{Name: "musl", Version: "1.2.3"},
|
|
},
|
|
Applications: []ftypes.Application{
|
|
{
|
|
Type: "bundler",
|
|
FilePath: "/app/Gemfile.lock",
|
|
Libraries: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "5.1"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:5cb2a5009179b1e78ecfef81a19756328bb266456cf9a9dbbcf9af8b83b735f0",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Type: "composer",
|
|
FilePath: "/app/composer-lock.json",
|
|
Libraries: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "laravel", Version: "6.0.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
libDetectExpectations: []LibraryDetectorDetectExpectation{
|
|
{
|
|
Args: LibraryDetectorDetectArgs{
|
|
FilePath: "/app/Gemfile.lock",
|
|
Pkgs: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "5.1"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:5cb2a5009179b1e78ecfef81a19756328bb266456cf9a9dbbcf9af8b83b735f0",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Returns: LibraryDetectorDetectReturns{
|
|
DetectedVulns: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-11111",
|
|
PkgName: "rails",
|
|
InstalledVersion: "5.1",
|
|
FixedVersion: "5.2",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:5cb2a5009179b1e78ecfef81a19756328bb266456cf9a9dbbcf9af8b83b735f0",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Args: LibraryDetectorDetectArgs{
|
|
FilePath: "/app/composer-lock.json",
|
|
Pkgs: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "laravel", Version: "6.0.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Returns: LibraryDetectorDetectReturns{
|
|
DetectedVulns: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-22222",
|
|
PkgName: "laravel",
|
|
InstalledVersion: "6.0.0",
|
|
FixedVersion: "6.1.0",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantResults: report.Results{
|
|
{
|
|
Target: "/app/Gemfile.lock",
|
|
Vulnerabilities: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-11111",
|
|
PkgName: "rails",
|
|
InstalledVersion: "5.1",
|
|
FixedVersion: "5.2",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:5cb2a5009179b1e78ecfef81a19756328bb266456cf9a9dbbcf9af8b83b735f0",
|
|
},
|
|
},
|
|
},
|
|
Type: "bundler",
|
|
},
|
|
{
|
|
Target: "/app/composer-lock.json",
|
|
Vulnerabilities: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2020-22222",
|
|
PkgName: "laravel",
|
|
InstalledVersion: "6.0.0",
|
|
FixedVersion: "6.1.0",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9922bc15eeefe1637b803ef2106f178152ce19a391f24aec838cbe2e48e73303",
|
|
},
|
|
},
|
|
},
|
|
Type: "composer",
|
|
},
|
|
},
|
|
wantOS: &ftypes.OS{
|
|
Family: "alpine",
|
|
Name: "3.11",
|
|
},
|
|
},
|
|
{
|
|
name: "sad path: ApplyLayers returns an error",
|
|
args: args{
|
|
target: "alpine:latest",
|
|
layerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
options: types.ScanOptions{VulnType: []string{"os", "library"}},
|
|
},
|
|
applyLayersExpectation: ApplierApplyLayersExpectation{
|
|
Args: ApplierApplyLayersArgs{
|
|
LayerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
},
|
|
Returns: ApplierApplyLayersReturns{
|
|
Err: errors.New("error"),
|
|
},
|
|
},
|
|
wantErr: "failed to apply layers",
|
|
},
|
|
{
|
|
name: "sad path: ospkgDetector.Detect returns an error",
|
|
args: args{
|
|
target: "alpine:latest",
|
|
layerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
options: types.ScanOptions{VulnType: []string{"os", "library"}},
|
|
},
|
|
applyLayersExpectation: ApplierApplyLayersExpectation{
|
|
Args: ApplierApplyLayersArgs{
|
|
LayerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
},
|
|
Returns: ApplierApplyLayersReturns{
|
|
Detail: ftypes.ImageDetail{
|
|
OS: &ftypes.OS{
|
|
Family: "alpine",
|
|
Name: "3.11",
|
|
},
|
|
Packages: []ftypes.Package{
|
|
{
|
|
Name: "musl",
|
|
Version: "1.2.3",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:ebf12965380b39889c99a9c02e82ba465f887b45975b6e389d42e9e6a3857888",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
ospkgDetectExpectations: []OspkgDetectorDetectExpectation{
|
|
{
|
|
Args: OspkgDetectorDetectArgs{
|
|
OsFamily: "alpine",
|
|
OsName: "3.11",
|
|
Pkgs: []ftypes.Package{
|
|
{
|
|
Name: "musl",
|
|
Version: "1.2.3",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:ebf12965380b39889c99a9c02e82ba465f887b45975b6e389d42e9e6a3857888",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Returns: OspkgDetectorDetectReturns{
|
|
Err: errors.New("error"),
|
|
},
|
|
},
|
|
},
|
|
wantErr: "failed to scan OS packages",
|
|
},
|
|
{
|
|
name: "sad path: libDetector.Detect returns an error",
|
|
args: args{
|
|
target: "alpine:latest",
|
|
layerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
options: types.ScanOptions{VulnType: []string{"library"}},
|
|
},
|
|
applyLayersExpectation: ApplierApplyLayersExpectation{
|
|
Args: ApplierApplyLayersArgs{
|
|
LayerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
|
|
},
|
|
Returns: ApplierApplyLayersReturns{
|
|
Detail: ftypes.ImageDetail{
|
|
OS: &ftypes.OS{
|
|
Family: "alpine",
|
|
Name: "3.11",
|
|
},
|
|
Packages: []ftypes.Package{
|
|
{
|
|
Name: "musl",
|
|
Version: "1.2.3",
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:ebf12965380b39889c99a9c02e82ba465f887b45975b6e389d42e9e6a3857888",
|
|
},
|
|
},
|
|
},
|
|
Applications: []ftypes.Application{
|
|
{
|
|
Type: "bundler",
|
|
FilePath: "/app/Gemfile.lock",
|
|
Libraries: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "6.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9bdb2c849099a99c8ab35f6fd7469c623635e8f4479a0a5a3df61e22bae509f6",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
libDetectExpectations: []LibraryDetectorDetectExpectation{
|
|
{
|
|
Args: LibraryDetectorDetectArgs{
|
|
FilePath: "/app/Gemfile.lock",
|
|
Pkgs: []ftypes.LibraryInfo{
|
|
{
|
|
Library: dtypes.Library{Name: "rails", Version: "6.0"},
|
|
Layer: ftypes.Layer{
|
|
DiffID: "sha256:9bdb2c849099a99c8ab35f6fd7469c623635e8f4479a0a5a3df61e22bae509f6",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Returns: LibraryDetectorDetectReturns{
|
|
Err: errors.New("error"),
|
|
},
|
|
},
|
|
},
|
|
wantErr: "failed to scan application libraries",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
applier := new(MockApplier)
|
|
applier.ApplyApplyLayersExpectation(tt.applyLayersExpectation)
|
|
|
|
ospkgDetector := new(MockOspkgDetector)
|
|
ospkgDetector.ApplyDetectExpectations(tt.ospkgDetectExpectations)
|
|
|
|
libDetector := new(MockLibraryDetector)
|
|
libDetector.ApplyDetectExpectations(tt.libDetectExpectations)
|
|
|
|
s := NewScanner(applier, ospkgDetector, libDetector)
|
|
gotResults, gotOS, gotEosl, err := s.Scan(tt.args.target, "", tt.args.layerIDs, tt.args.options)
|
|
if tt.wantErr != "" {
|
|
require.NotNil(t, err, tt.name)
|
|
require.Contains(t, err.Error(), tt.wantErr, tt.name)
|
|
return
|
|
} else {
|
|
require.NoError(t, err, tt.name)
|
|
}
|
|
|
|
assert.Equal(t, tt.wantResults, gotResults)
|
|
assert.Equal(t, tt.wantOS, gotOS)
|
|
assert.Equal(t, tt.wantEosl, gotEosl)
|
|
|
|
applier.AssertExpectations(t)
|
|
ospkgDetector.AssertExpectations(t)
|
|
libDetector.AssertExpectations(t)
|
|
})
|
|
}
|
|
}
|