github.com/anchore/syft@v1.38.2/cmd/syft/internal/options/source.go (about)

     1  package options
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/dustin/go-humanize"
     9  	"github.com/scylladb/go-set/strset"
    10  
    11  	"github.com/anchore/clio"
    12  	stereoscopeFile "github.com/anchore/stereoscope/pkg/file"
    13  	"github.com/anchore/syft/syft/source/sourceproviders"
    14  )
    15  
    16  type sourceConfig struct {
    17  	Name     string      `json:"name" yaml:"name" mapstructure:"name"`
    18  	Version  string      `json:"version" yaml:"version" mapstructure:"version"`
    19  	Supplier string      `json:"supplier" yaml:"supplier" mapstructure:"supplier"`
    20  	Source   string      `json:"source" yaml:"source" mapstructure:"source"`
    21  	BasePath string      `yaml:"base-path" json:"base-path" mapstructure:"base-path"` // specify base path for all file paths
    22  	File     fileSource  `json:"file" yaml:"file" mapstructure:"file"`
    23  	Image    imageSource `json:"image" yaml:"image" mapstructure:"image"`
    24  }
    25  
    26  type fileSource struct {
    27  	Digests []string `json:"digests" yaml:"digests" mapstructure:"digests"`
    28  }
    29  
    30  var _ interface {
    31  	clio.FieldDescriber
    32  } = (*sourceConfig)(nil)
    33  
    34  var _ clio.PostLoader = (*imageSource)(nil)
    35  
    36  func (o *sourceConfig) DescribeFields(descriptions clio.FieldDescriptionSet) {
    37  	descriptions.Add(&o.File.Digests, `the file digest algorithms to use on the scanned file (options: "md5", "sha1", "sha224", "sha256", "sha384", "sha512")`)
    38  	descriptions.Add(&o.Image.DefaultPullSource, `allows users to specify which image source should be used to generate the sbom
    39  valid values are: registry, docker, podman`)
    40  }
    41  
    42  type imageSource struct {
    43  	DefaultPullSource string `json:"default-pull-source" yaml:"default-pull-source" mapstructure:"default-pull-source"`
    44  	MaxLayerSize      string `json:"max-layer-size" yaml:"max-layer-size" mapstructure:"max-layer-size"`
    45  }
    46  
    47  func defaultSourceConfig() sourceConfig {
    48  	var digests []string
    49  	for _, alg := range sourceproviders.DefaultConfig().DigestAlgorithms {
    50  		digests = append(digests, alg.String())
    51  	}
    52  	return sourceConfig{
    53  		File: fileSource{
    54  			Digests: digests,
    55  		},
    56  	}
    57  }
    58  
    59  func (c *fileSource) PostLoad() error {
    60  	digests := strset.New(c.Digests...).List()
    61  	sort.Strings(digests)
    62  	c.Digests = digests
    63  	return nil
    64  }
    65  
    66  func (c *imageSource) PostLoad() error {
    67  	if c.MaxLayerSize != "" {
    68  		perFileReadLimit, err := humanize.ParseBytes(c.MaxLayerSize)
    69  		if err != nil {
    70  			return err
    71  		}
    72  		stereoscopeFile.SetPerFileReadLimit(int64(perFileReadLimit))
    73  	}
    74  	return checkDefaultSourceValues(c.DefaultPullSource)
    75  }
    76  
    77  var validDefaultSourceValues = []string{"registry", "docker", "podman", ""}
    78  
    79  func checkDefaultSourceValues(source string) error {
    80  	validValues := strset.New(validDefaultSourceValues...)
    81  	if !validValues.Has(source) {
    82  		validValuesString := strings.Join(validDefaultSourceValues, ", ")
    83  		return fmt.Errorf("%s is not a valid default source; please use one of the following: %s''", source, validValuesString)
    84  	}
    85  
    86  	return nil
    87  }