syft/test/integration/files_test.go
Alex Goodman b0ab75fd89
Replace core SBOM-creation API with builder pattern (#1383)
* remove existing cataloging API

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add file cataloging config

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add package cataloging config

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add configs for cross-cutting concerns

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* rename CLI option configs to not require import aliases later

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* update all nested structs for the Catalog struct

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* update Catalog cli options

- add new cataloger selection options (selection and default)
- remove the excludeBinaryOverlapByOwnership
- deprecate "catalogers" flag
- add new javascript configuration

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* migrate relationship capabilities to separate internal package

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* refactor golang cataloger to use configuration options when creating packages

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* create internal object to facilitate reading from and writing to an SBOM

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* create a command-like object (task) to facilitate partial SBOM creation

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add cataloger selection capability

- be able to parse string expressions into a set of resolved actions against sets
- be able to use expressions to select/add/remove tasks to/from the final set of tasks to run

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add package, file, and environment related tasks

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* update existing file catalogers to use nested UI elements

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add CreateSBOMConfig that drives the SBOM creation process

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* capture SBOM creation info as a struct

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add CreateSBOM() function

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix tests

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* update docs with SBOM selection help + breaking changes

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix multiple override default inputs

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix deprecation flag printing to stdout

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* refactor cataloger selection description to separate object

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* address review comments

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* keep expression errors and show specific suggestions only

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* address additional review feedback

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* address more review comments

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* addressed additional PR review feedback

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix file selection references

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* remove guess language data generation option

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add tests for coordinatesForSelection

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* rename relationship attributes

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add descriptions to relationships config fields

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* improve documentation around configuration options

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add explicit errors around legacy config entries

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
2024-01-12 17:39:13 -05:00

155 lines
4.8 KiB
Go

package integration
import (
"crypto"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anchore/clio"
stereoscopeFile "github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/syft/cmd/syft/cli/options"
"github.com/anchore/syft/syft/cataloging/filecataloging"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/file/cataloger/filecontent"
"github.com/anchore/syft/syft/sbom"
)
func TestFileCataloging_Default(t *testing.T) {
cfg := options.DefaultCatalog().ToSBOMConfig(clio.Identification{})
cfg = cfg.WithFilesConfig(filecataloging.DefaultConfig())
sbom, _ := catalogDirectoryWithConfig(t, "test-fixtures/files", cfg)
var metadata map[file.Coordinates]file.Metadata
var digests map[file.Coordinates][]file.Digest
var contents map[file.Coordinates]string
assertFileData(t, metadata, digests, contents, sbom)
}
func TestFileCataloging_AllFiles(t *testing.T) {
cfg := options.DefaultCatalog().ToSBOMConfig(clio.Identification{})
cfg = cfg.WithFilesConfig(filecataloging.Config{
Selection: file.AllFilesSelection,
Hashers: []crypto.Hash{
crypto.SHA256,
},
Content: filecontent.Config{
// this is enough to potentially capture a/file, a-small-file, a-symlink-to-a-small-file, and a-symlink-to-file
// but the size of a/file will cause it to be filtered, and the symlinks will not be included since
// they are not regular files
Globs: []string{"**/*file"},
SkipFilesAboveSize: 30,
},
})
sbom, _ := catalogDirectoryWithConfig(t, "test-fixtures/files", cfg)
pwd, err := os.Getwd()
require.NoError(t, err)
testPath := func(path string) string {
return filepath.Join(pwd, "test-fixtures/files", path)
}
metadata := map[file.Coordinates]file.Metadata{
{RealPath: ""}: {
Path: testPath(""),
Type: stereoscopeFile.TypeDirectory,
},
{RealPath: "/somewhere"}: {
Path: testPath("/somewhere"),
Type: stereoscopeFile.TypeDirectory,
},
{RealPath: "/somewhere/there"}: {
Path: testPath("/somewhere/there"),
Type: stereoscopeFile.TypeDirectory,
},
{RealPath: "/somewhere/there/is"}: {
Path: testPath("/somewhere/there/is"),
Type: stereoscopeFile.TypeDirectory,
},
{RealPath: "/somewhere/there/is/a"}: {
Path: testPath("/somewhere/there/is/a"),
Type: stereoscopeFile.TypeDirectory,
},
{RealPath: "/somewhere/there/is/a-small-file"}: {
Path: testPath("/somewhere/there/is/a-small-file"),
Type: stereoscopeFile.TypeRegular,
MIMEType: "text/plain",
},
{RealPath: "/somewhere/there/is/a-symlink-to-a-small-file"}: {
Path: testPath("/somewhere/there/is/a-symlink-to-a-small-file"),
LinkDestination: testPath("/somewhere/there/is/a-small-file"),
Type: stereoscopeFile.TypeSymLink,
},
{RealPath: "/somewhere/there/is/a-symlink-to-file"}: {
Path: testPath("/somewhere/there/is/a-symlink-to-file"),
LinkDestination: testPath("/somewhere/there/is/a/file"),
Type: stereoscopeFile.TypeSymLink,
},
{RealPath: "/somewhere/there/is/a/file"}: {
Path: testPath("/somewhere/there/is/a/file"),
Type: stereoscopeFile.TypeRegular,
MIMEType: "text/plain",
},
}
digests := map[file.Coordinates][]file.Digest{
{RealPath: "/somewhere/there/is/a-small-file"}: {
file.Digest{Algorithm: "sha256", Value: "672c23470e4ce99cf270bb63ae66ad2b8a80aa19090c40e59fbb1229a4ab661a"},
},
{RealPath: "/somewhere/there/is/a/file"}: {
file.Digest{Algorithm: "sha256", Value: "00dac26d6d94353ac0d92bb9640cba76f82f5ca8707bb845ecdc574bd002348e"},
},
}
contents := map[file.Coordinates]string{
{RealPath: "/somewhere/there/is/a-small-file"}: "c29tZSBjb250ZW50cyE=",
}
assertFileData(t, metadata, digests, contents, sbom)
}
func assertFileData(t testing.TB, metadata map[file.Coordinates]file.Metadata, digests map[file.Coordinates][]file.Digest, contents map[file.Coordinates]string, sbom sbom.SBOM) {
metadataCompareOpts := cmp.Options{
cmp.Comparer(func(x, y file.Metadata) bool {
if x.Path != y.Path {
t.Logf("path mismatch: %s != %s", x.Path, y.Path)
return false
}
if x.Type != y.Type {
t.Logf("type mismatch: %s != %s", x.Type, y.Type)
return false
}
if x.LinkDestination != y.LinkDestination {
t.Logf("link destination mismatch: %s != %s", x.LinkDestination, y.LinkDestination)
return false
}
if x.MIMEType != y.MIMEType {
t.Logf("mime type mismatch: %s != %s", x.MIMEType, y.MIMEType)
return false
}
return true
}),
}
if d := cmp.Diff(metadata, sbom.Artifacts.FileMetadata, metadataCompareOpts...); d != "" {
t.Errorf("unexpected metadata (-want +got):\n%s", d)
}
assert.Equal(t, digests, sbom.Artifacts.FileDigests, "different digests detected")
assert.Equal(t, contents, sbom.Artifacts.FileContents, "different contents detected")
}