mirror of
https://github.com/anchore/syft.git
synced 2026-10-31 06:26:30 +01:00
[wip] api refactor
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
@@ -3,6 +3,9 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/anchore/syft/internal/version"
|
||||
"github.com/anchore/syft/syft"
|
||||
"github.com/anchore/syft/syft/file"
|
||||
"path"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -49,6 +52,35 @@ type Application struct {
|
||||
Platform string `yaml:"platform" json:"platform" mapstructure:"platform"`
|
||||
}
|
||||
|
||||
func (cfg Application) ToCatalogingConfig() (*syft.CatalogingConfig, error) {
|
||||
digests, err := file.DigestHashesByName(cfg.FileMetadata.Digests...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse config item 'file-metadata.digests': %w", err)
|
||||
}
|
||||
|
||||
secretsConfig, err := cfg.Secrets.ToConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &syft.CatalogingConfig{
|
||||
// note: package catalogers cannot be determined until runtime
|
||||
ToolName: internal.ApplicationName,
|
||||
ToolVersion: version.FromBuild().Version,
|
||||
ToolConfiguration: cfg,
|
||||
Scope: cfg.Package.Cataloger.ScopeOpt,
|
||||
ProcessTasksInSerial: false,
|
||||
CaptureFileMetadata: cfg.FileMetadata.Cataloger.Enabled,
|
||||
DigestHashes: digests,
|
||||
CaptureSecrets: cfg.Secrets.Cataloger.Enabled,
|
||||
SecretsConfig: *secretsConfig,
|
||||
SecretsScope: cfg.Secrets.Cataloger.ScopeOpt,
|
||||
ClassifyFiles: cfg.FileClassification.Cataloger.Enabled,
|
||||
FileClassifiers: file.DefaultClassifiers(),
|
||||
ContentsConfig: cfg.FileContents.ToConfig(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// PowerUserCatalogerEnabledDefault switches all catalogers to be enabled when running power-user command
|
||||
func PowerUserCatalogerEnabledDefault() {
|
||||
catalogerEnabledDefault = true
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/anchore/syft/internal/file"
|
||||
internalFile "github.com/anchore/syft/internal/file"
|
||||
"github.com/anchore/syft/syft/file"
|
||||
"github.com/anchore/syft/syft/source"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -15,10 +16,17 @@ type fileContents struct {
|
||||
func (cfg fileContents) loadDefaultValues(v *viper.Viper) {
|
||||
v.SetDefault("file-contents.cataloger.enabled", catalogerEnabledDefault)
|
||||
v.SetDefault("file-contents.cataloger.scope", source.SquashedScope)
|
||||
v.SetDefault("file-contents.skip-files-above-size", 1*file.MB)
|
||||
v.SetDefault("file-contents.skip-files-above-size", 1*internalFile.MB)
|
||||
v.SetDefault("file-contents.globs", []string{})
|
||||
}
|
||||
|
||||
func (cfg *fileContents) parseConfigValues() error {
|
||||
return cfg.Cataloger.parseConfigValues()
|
||||
}
|
||||
|
||||
func (cfg fileContents) ToConfig() file.ContentsCatalogerConfig {
|
||||
return file.ContentsCatalogerConfig{
|
||||
Globs: cfg.Globs,
|
||||
SkipFilesAboveSizeInBytes: cfg.SkipFilesAboveSize,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/anchore/syft/syft/pkg/cataloger"
|
||||
"github.com/anchore/syft/syft/pkg/cataloger/packages"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -13,7 +13,7 @@ type pkg struct {
|
||||
|
||||
func (cfg pkg) loadDefaultValues(v *viper.Viper) {
|
||||
cfg.Cataloger.loadDefaultValues(v)
|
||||
c := cataloger.DefaultSearchConfig()
|
||||
c := packages.DefaultSearchConfig()
|
||||
v.SetDefault("package.search-unindexed-archives", c.IncludeUnindexedArchives)
|
||||
v.SetDefault("package.search-indexed-archives", c.IncludeIndexedArchives)
|
||||
}
|
||||
@@ -22,12 +22,9 @@ func (cfg *pkg) parseConfigValues() error {
|
||||
return cfg.Cataloger.parseConfigValues()
|
||||
}
|
||||
|
||||
func (cfg pkg) ToConfig() cataloger.Config {
|
||||
return cataloger.Config{
|
||||
Search: cataloger.SearchConfig{
|
||||
IncludeIndexedArchives: cfg.SearchIndexedArchives,
|
||||
IncludeUnindexedArchives: cfg.SearchUnindexedArchives,
|
||||
Scope: cfg.Cataloger.ScopeOpt,
|
||||
},
|
||||
func (cfg pkg) ToConfig() packages.SearchConfig {
|
||||
return packages.SearchConfig{
|
||||
IncludeIndexedArchives: cfg.SearchIndexedArchives,
|
||||
IncludeUnindexedArchives: cfg.SearchUnindexedArchives,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/anchore/syft/internal/file"
|
||||
"fmt"
|
||||
internalFile "github.com/anchore/syft/internal/file"
|
||||
"github.com/anchore/syft/syft/file"
|
||||
"github.com/anchore/syft/syft/source"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -18,7 +20,7 @@ func (cfg secrets) loadDefaultValues(v *viper.Viper) {
|
||||
v.SetDefault("secrets.cataloger.enabled", catalogerEnabledDefault)
|
||||
v.SetDefault("secrets.cataloger.scope", source.AllLayersScope)
|
||||
v.SetDefault("secrets.reveal-values", false)
|
||||
v.SetDefault("secrets.skip-files-above-size", 1*file.MB)
|
||||
v.SetDefault("secrets.skip-files-above-size", 1*internalFile.MB)
|
||||
v.SetDefault("secrets.additional-patterns", map[string]string{})
|
||||
v.SetDefault("secrets.exclude-pattern-names", []string{})
|
||||
}
|
||||
@@ -26,3 +28,15 @@ func (cfg secrets) loadDefaultValues(v *viper.Viper) {
|
||||
func (cfg *secrets) parseConfigValues() error {
|
||||
return cfg.Cataloger.parseConfigValues()
|
||||
}
|
||||
|
||||
func (cfg secrets) ToConfig() (*file.SecretsCatalogerConfig, error) {
|
||||
patterns, err := file.GenerateSearchPatterns(file.DefaultSecretsPatterns, cfg.AdditionalPatterns, cfg.ExcludePatternNames)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to process secrets config patterns: %w", err)
|
||||
}
|
||||
return &file.SecretsCatalogerConfig{
|
||||
Patterns: patterns,
|
||||
RevealValues: cfg.RevealValues,
|
||||
MaxFileSize: cfg.SkipFilesAboveSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"github.com/anchore/syft/internal/log"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Guess() string {
|
||||
v := FromBuild().Version
|
||||
if strings.HasPrefix(v, "v") {
|
||||
return v
|
||||
}
|
||||
|
||||
buildInfo, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
log.Warn("syft version could not be determined: unable to find the buildinfo section of the binary")
|
||||
return v
|
||||
}
|
||||
|
||||
var found bool
|
||||
for _, d := range buildInfo.Deps {
|
||||
if d.Path == "github.com/anchore/syft" {
|
||||
v = d.Version
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
// look for probable forks
|
||||
for _, d := range buildInfo.Deps {
|
||||
if strings.HasSuffix(d.Path, "/syft") {
|
||||
v = d.Version
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
log.Warn("syft version could not be determined: unable to find syft within the buildinfo section of the binary")
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
Reference in New Issue
Block a user