github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/cataloging/filecataloging/config.go (about)

     1  package filecataloging
     2  
     3  import (
     4  	"crypto"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	intFile "github.com/anchore/syft/internal/file"
    10  	"github.com/anchore/syft/internal/log"
    11  	"github.com/anchore/syft/syft/file"
    12  	"github.com/anchore/syft/syft/file/cataloger/executable"
    13  	"github.com/anchore/syft/syft/file/cataloger/filecontent"
    14  )
    15  
    16  type Config struct {
    17  	Selection  file.Selection     `yaml:"selection" json:"selection" mapstructure:"selection"`
    18  	Hashers    []crypto.Hash      `yaml:"hashers" json:"hashers" mapstructure:"hashers"`
    19  	Content    filecontent.Config `yaml:"content" json:"content" mapstructure:"content"`
    20  	Executable executable.Config  `yaml:"executable" json:"executable" mapstructure:"executable"`
    21  }
    22  
    23  type configMarshaledForm struct {
    24  	Selection file.Selection     `yaml:"selection" json:"selection" mapstructure:"selection"`
    25  	Hashers   []string           `yaml:"hashers" json:"hashers" mapstructure:"hashers"`
    26  	Content   filecontent.Config `yaml:"content" json:"content" mapstructure:"content"`
    27  }
    28  
    29  func DefaultConfig() Config {
    30  	hashers, err := intFile.Hashers("sha256")
    31  	if err != nil {
    32  		log.WithFields("error", err).Warn("unable to create file hashers")
    33  	}
    34  	return Config{
    35  		Selection:  file.FilesOwnedByPackageSelection,
    36  		Hashers:    hashers,
    37  		Content:    filecontent.DefaultConfig(),
    38  		Executable: executable.DefaultConfig(),
    39  	}
    40  }
    41  
    42  func (cfg Config) MarshalJSON() ([]byte, error) {
    43  	marshaled := configMarshaledForm{
    44  		Selection: cfg.Selection,
    45  		Hashers:   hashersToString(cfg.Hashers),
    46  	}
    47  	return json.Marshal(marshaled)
    48  }
    49  
    50  func hashersToString(hashers []crypto.Hash) []string {
    51  	var result []string
    52  	for _, h := range hashers {
    53  		result = append(result, strings.ToLower(h.String()))
    54  	}
    55  	return result
    56  }
    57  
    58  func (cfg *Config) UnmarshalJSON(data []byte) error {
    59  	var marshaled configMarshaledForm
    60  	if err := json.Unmarshal(data, &marshaled); err != nil {
    61  		return err
    62  	}
    63  
    64  	hashers, err := intFile.Hashers(marshaled.Hashers...)
    65  	if err != nil {
    66  		return fmt.Errorf("unable to parse configured hashers: %w", err)
    67  	}
    68  	cfg.Selection = marshaled.Selection
    69  	cfg.Hashers = hashers
    70  	return nil
    71  }
    72  
    73  func (cfg Config) WithSelection(selection file.Selection) Config {
    74  	cfg.Selection = selection
    75  	return cfg
    76  }
    77  
    78  func (cfg Config) WithHashers(hashers ...crypto.Hash) Config {
    79  	cfg.Hashers = intFile.NormalizeHashes(hashers)
    80  	return cfg
    81  }
    82  
    83  func (cfg Config) WithContentConfig(content filecontent.Config) Config {
    84  	cfg.Content = content
    85  	return cfg
    86  }