Skip to content

Commit

Permalink
add deprecation notices and split old and new cataloging config
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Dec 16, 2022
1 parent 77e1491 commit 57a9877
Show file tree
Hide file tree
Showing 25 changed files with 406 additions and 137 deletions.
217 changes: 217 additions & 0 deletions cmd/syft/cli/eventloop/tasks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package eventloop

import (
"crypto"
"fmt"

"github.com/anchore/syft/internal/config"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
)

// Deprecated: will be removed in syft v1.0.0
type Task func(*sbom.Artifacts, *source.Source) ([]artifact.Relationship, error)

// Deprecated: will be removed in syft v1.0.0
func Tasks(app *config.Application) ([]Task, error) {
var tasks []Task

generators := []func(app *config.Application) (Task, error){
generateCatalogPackagesTask,
generateCatalogFileMetadataTask,
generateCatalogFileDigestsTask,
generateCatalogSecretsTask,
generateCatalogContentsTask,
}

for _, generator := range generators {
task, err := generator(app)
if err != nil {
return nil, err
}

if task != nil {
tasks = append(tasks, task)
}
}

return tasks, nil
}

// Deprecated: will be removed in syft v1.0.0
func generateCatalogPackagesTask(app *config.Application) (Task, error) {
if !app.Package.Cataloger.Enabled {
return nil, nil
}

task := func(results *sbom.Artifacts, src *source.Source) ([]artifact.Relationship, error) {
cfg := cataloger.Config{
Search: cataloger.SearchConfig{
IncludeIndexedArchives: app.Package.SearchIndexedArchives,
IncludeUnindexedArchives: app.Package.SearchUnindexedArchives,
Scope: app.Package.Cataloger.ScopeOpt,
},
Catalogers: app.Catalogers,
}
packageCatalog, relationships, theDistro, err := syft.CatalogPackages(src, cfg)
if err != nil {
return nil, err
}

results.PackageCatalog = packageCatalog
results.LinuxDistribution = theDistro

return relationships, nil
}

return task, nil
}

// Deprecated: will be removed in syft v1.0.0
func generateCatalogFileMetadataTask(app *config.Application) (Task, error) {
if !app.FileMetadata.Cataloger.Enabled {
return nil, nil
}

metadataCataloger := file.NewMetadataCataloger()

task := func(results *sbom.Artifacts, src *source.Source) ([]artifact.Relationship, error) {
resolver, err := src.FileResolver(app.FileMetadata.Cataloger.ScopeOpt)
if err != nil {
return nil, err
}

result, err := metadataCataloger.Catalog(resolver)
if err != nil {
return nil, err
}
results.FileMetadata = result
return nil, nil
}

return task, nil
}

// Deprecated: will be removed in syft v1.0.0
func generateCatalogFileDigestsTask(app *config.Application) (Task, error) {
if !app.FileMetadata.Cataloger.Enabled {
return nil, nil
}

supportedHashAlgorithms := make(map[string]crypto.Hash)
for _, h := range []crypto.Hash{
crypto.MD5,
crypto.SHA1,
crypto.SHA256,
} {
supportedHashAlgorithms[file.DigestAlgorithmName(h)] = h
}

var hashes []crypto.Hash
for _, hashStr := range app.FileMetadata.Digests {
name := file.CleanDigestAlgorithmName(hashStr)
hashObj, ok := supportedHashAlgorithms[name]
if !ok {
return nil, fmt.Errorf("unsupported hash algorithm: %s", hashStr)
}
hashes = append(hashes, hashObj)
}

digestsCataloger := file.NewDigestsCataloger(hashes)

task := func(results *sbom.Artifacts, src *source.Source) ([]artifact.Relationship, error) {
resolver, err := src.FileResolver(app.FileMetadata.Cataloger.ScopeOpt)
if err != nil {
return nil, err
}

result, err := digestsCataloger.Catalog(resolver)
if err != nil {
return nil, err
}
results.FileDigests = result
return nil, nil
}

return task, nil
}

// Deprecated: will be removed in syft v1.0.0
func generateCatalogSecretsTask(app *config.Application) (Task, error) {
if !app.Secrets.Cataloger.Enabled {
return nil, nil
}

patterns, err := file.GenerateSearchPatterns(file.DefaultSecretsPatterns, app.Secrets.AdditionalPatterns, app.Secrets.ExcludePatternNames)
if err != nil {
return nil, err
}

secretsCataloger, err := file.NewSecretsCataloger(patterns, app.Secrets.RevealValues, app.Secrets.SkipFilesAboveSize)
if err != nil {
return nil, err
}

task := func(results *sbom.Artifacts, src *source.Source) ([]artifact.Relationship, error) {
resolver, err := src.FileResolver(app.Secrets.Cataloger.ScopeOpt)
if err != nil {
return nil, err
}

result, err := secretsCataloger.Catalog(resolver)
if err != nil {
return nil, err
}
results.Secrets = result
return nil, nil
}

return task, nil
}

// Deprecated: will be removed in syft v1.0.0
func generateCatalogContentsTask(app *config.Application) (Task, error) {
if !app.FileContents.Cataloger.Enabled {
return nil, nil
}

contentsCataloger, err := file.NewContentsCataloger(app.FileContents.Globs, app.FileContents.SkipFilesAboveSize)
if err != nil {
return nil, err
}

task := func(results *sbom.Artifacts, src *source.Source) ([]artifact.Relationship, error) {
resolver, err := src.FileResolver(app.FileContents.Cataloger.ScopeOpt)
if err != nil {
return nil, err
}

result, err := contentsCataloger.Catalog(resolver)
if err != nil {
return nil, err
}
results.FileContents = result
return nil, nil
}

return task, nil
}

// Deprecated: will be removed in syft v1.0.0
func RunTask(t Task, a *sbom.Artifacts, src *source.Source, c chan<- artifact.Relationship, errs chan<- error) {
defer close(c)

relationships, err := t(a, src)
if err != nil {
errs <- err
return
}

for _, relationship := range relationships {
c <- relationship
}
}
2 changes: 1 addition & 1 deletion cmd/syft/cli/options/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/spf13/viper"

"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/cataloger"
"github.com/anchore/syft/syft/formats/table"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/source"
)

Expand Down
21 changes: 17 additions & 4 deletions cmd/syft/cli/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/internal/ui"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/cataloger"
"github.com/anchore/syft/syft/event"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/formats/template"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
)
Expand Down Expand Up @@ -103,7 +104,7 @@ func GenerateSBOM(src *source.Source, errs chan error, app *config.Application)
}

cfg := syft.DefaultSBOMBuilderConfig().
WithCatalogers(src.Metadata,
WithDefaultCatalogers(src.Metadata,
cataloger.Config{
Search: cataloger.SearchConfig{
IncludeIndexedArchives: app.Package.SearchIndexedArchives,
Expand All @@ -118,13 +119,14 @@ func GenerateSBOM(src *source.Source, errs chan error, app *config.Application)
GenerateCPEs: true, // TODO: tie to app config
GuessLanguageFromPURL: true, // TODO: tie to app config
},
FileCatalogingSelection: cataloger.OwnedFilesSelection, // TODO: tie to app config
// TODO: make default the owned-files selection
FileCatalogingSelection: cataloger.NoFilesSelection, // TODO: tie to app config
FileHashers: hashers,
},
strings.Join(app.Catalogers, ","), // TODO: update app config to just be a string?
)

return syft.BuildSBOM(src, cfg)
return syft.CreateSBOM(src, cfg)
}

func validateOutputOptions(app *config.Application) error {
Expand All @@ -142,3 +144,14 @@ func validateOutputOptions(app *config.Application) error {

return nil
}

// Deprecated: will be removed in v1.0.0
func MergeRelationships(cs ...<-chan artifact.Relationship) (relationships []artifact.Relationship) {
for _, c := range cs {
for n := range c {
relationships = append(relationships, n)
}
}

return relationships
}
1 change: 1 addition & 0 deletions cmd/syft/cli/poweruser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const powerUserExample = ` {{.appName}} {{.command}} <image>
All behavior is controlled via application configuration and environment variables (see https://github.com/anchore/syft#configuration)
`

// Deprecated: will be removed in syft v1.0.0
func PowerUser(v *viper.Viper, app *config.Application, ro *options.RootOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "power-user [IMAGE]",
Expand Down
2 changes: 2 additions & 0 deletions cmd/syft/cli/poweruser/poweruser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/anchore/syft/syft/source"
)

// Deprecated: will be removed in syft v1.0.0
func Run(ctx context.Context, app *config.Application, args []string) error {
writer, err := sbom.NewWriter(sbom.WriterOption{
Format: syftjson.Format(),
Expand Down Expand Up @@ -65,6 +66,7 @@ func Run(ctx context.Context, app *config.Application, args []string) error {
)
}

// Deprecated: will be removed in syft v1.0.0
func execWorker(app *config.Application, si source.Input, writer sbom.Writer) <-chan error {
errs := make(chan error)
go func() {
Expand Down
4 changes: 1 addition & 3 deletions internal/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import (
"github.com/anchore/go-logger"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/cataloger"
)

var (
ErrApplicationConfigNotFound = fmt.Errorf("application config not found")
catalogerEnabledDefault = false
)

type defaultValueLoader interface {
Expand Down Expand Up @@ -65,7 +64,6 @@ func (cfg Application) ToCatalogerConfig() cataloger.Config {
IncludeUnindexedArchives: cfg.Package.SearchUnindexedArchives,
Scope: cfg.Package.Cataloger.ScopeOpt,
},
Catalogers: cfg.Catalogers,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/file_classification.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type fileClassification struct {
}

func (cfg fileClassification) loadDefaultValues(v *viper.Viper) {
v.SetDefault("file-classification.cataloger.enabled", catalogerEnabledDefault)
v.SetDefault("file-classification.cataloger.enabled", false)
v.SetDefault("file-classification.cataloger.scope", source.SquashedScope)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/file_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type fileContents struct {
}

func (cfg fileContents) loadDefaultValues(v *viper.Viper) {
v.SetDefault("file-contents.cataloger.enabled", catalogerEnabledDefault)
v.SetDefault("file-contents.cataloger.enabled", false)
v.SetDefault("file-contents.cataloger.scope", source.SquashedScope)
v.SetDefault("file-contents.skip-files-above-size", 1*file.MB)
v.SetDefault("file-contents.globs", []string{})
Expand Down
2 changes: 1 addition & 1 deletion internal/config/file_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type FileMetadata struct {
}

func (cfg FileMetadata) loadDefaultValues(v *viper.Viper) {
v.SetDefault("file-metadata.cataloger.enabled", catalogerEnabledDefault)
v.SetDefault("file-metadata.cataloger.enabled", false)
v.SetDefault("file-metadata.cataloger.scope", source.SquashedScope)
v.SetDefault("file-metadata.digests", []string{"sha256"})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"github.com/spf13/viper"

"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/cataloger"
)

type pkg struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type secrets struct {
}

func (cfg secrets) loadDefaultValues(v *viper.Viper) {
v.SetDefault("secrets.cataloger.enabled", catalogerEnabledDefault)
v.SetDefault("secrets.cataloger.enabled", false)
v.SetDefault("secrets.cataloger.scope", source.AllLayersScope)
v.SetDefault("secrets.reveal-values", false)
v.SetDefault("secrets.skip-files-above-size", 1*file.MB)
Expand Down
Loading

0 comments on commit 57a9877

Please sign in to comment.