mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 08:23:15 +01:00
* add support for searching jars within archives Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add package cataloger config options Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * address review comments + factor out safeCopy helper Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * update config docs regarding package archive search options Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * show that unindexed archive cataloging defaults to false Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * remove lies about -s Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * address review comments Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * update search archive note about java Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package file
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
|
|
"github.com/bmatcuk/doublestar/v4"
|
|
"github.com/hashicorp/go-multierror"
|
|
)
|
|
|
|
// GenerateSearchPatterns takes a set of named base patterns, a set of additional named patterns and an name exclusion list and generates a final
|
|
// set of regular expressions (indexed by name). The sets are aggregated roughly as such: (base - excluded) + additional.
|
|
func GenerateSearchPatterns(basePatterns map[string]string, additionalPatterns map[string]string, excludePatternNames []string) (map[string]*regexp.Regexp, error) {
|
|
var regexObjs = make(map[string]*regexp.Regexp)
|
|
var errs error
|
|
|
|
addFn := func(name, pattern string) {
|
|
// always enable multiline search option for extracting secrets with multiline values
|
|
obj, err := regexp.Compile(`(?m)` + pattern)
|
|
if err != nil {
|
|
errs = multierror.Append(errs, fmt.Errorf("unable to parse %q regular expression: %w", name, err))
|
|
}
|
|
regexObjs[name] = obj
|
|
}
|
|
|
|
// add all base cases... unless that base case was asked to be excluded
|
|
for name, pattern := range basePatterns {
|
|
if !matchesExclusion(excludePatternNames, name) {
|
|
addFn(name, pattern)
|
|
}
|
|
}
|
|
|
|
// add all additional cases
|
|
for name, pattern := range additionalPatterns {
|
|
addFn(name, pattern)
|
|
}
|
|
|
|
if errs != nil {
|
|
return nil, errs
|
|
}
|
|
|
|
return regexObjs, nil
|
|
}
|
|
|
|
func matchesExclusion(excludePatternNames []string, name string) bool {
|
|
for _, exclude := range excludePatternNames {
|
|
matches, err := doublestar.Match(exclude, name)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if matches {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|