|
| 1 | +package packaging |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "github.com/samber/lo" |
| 12 | + "golang.org/x/xerrors" |
| 13 | + |
| 14 | + "github.com/aquasecurity/trivy/pkg/dependency/parser/python/packaging" |
| 15 | + "github.com/aquasecurity/trivy/pkg/fanal/analyzer" |
| 16 | + "github.com/aquasecurity/trivy/pkg/fanal/analyzer/language" |
| 17 | + "github.com/aquasecurity/trivy/pkg/fanal/types" |
| 18 | + "github.com/aquasecurity/trivy/pkg/log" |
| 19 | + xio "github.com/aquasecurity/trivy/pkg/x/io" |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + analyzer.RegisterAnalyzer(&eggAnalyzer{}) |
| 24 | +} |
| 25 | + |
| 26 | +const ( |
| 27 | + eggAnalyzerVersion = 1 |
| 28 | + eggExt = ".egg" |
| 29 | +) |
| 30 | + |
| 31 | +type eggAnalyzer struct { |
| 32 | + logger *log.Logger |
| 33 | + licenseClassifierConfidenceLevel float64 |
| 34 | +} |
| 35 | + |
| 36 | +func (a *eggAnalyzer) Init(opt analyzer.AnalyzerOptions) error { |
| 37 | + a.logger = log.WithPrefix("python") |
| 38 | + a.licenseClassifierConfidenceLevel = opt.LicenseScannerOption.ClassifierConfidenceLevel |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +// Analyze analyzes egg archive files |
| 43 | +func (a *eggAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) { |
| 44 | + // .egg file is zip format and PKG-INFO needs to be extracted from the zip file. |
| 45 | + pkginfoInZip, err := findFileInZip(input.Content, input.Info.Size(), isEggFile) |
| 46 | + if err != nil { |
| 47 | + return nil, xerrors.Errorf("unable to open `.egg` archive: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + // Egg archive may not contain required files, then we will get nil. Skip this archives |
| 51 | + if pkginfoInZip == nil { |
| 52 | + return nil, nil |
| 53 | + } |
| 54 | + |
| 55 | + rsa, err := xio.NewReadSeekerAt(pkginfoInZip) |
| 56 | + if err != nil { |
| 57 | + return nil, xerrors.Errorf("unable to convert PKG-INFO reader: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + app, err := language.ParsePackage(types.PythonPkg, input.FilePath, rsa, packaging.NewParser(), input.Options.FileChecksum) |
| 61 | + if err != nil { |
| 62 | + return nil, xerrors.Errorf("parse error: %w", err) |
| 63 | + } else if app == nil { |
| 64 | + return nil, nil |
| 65 | + } |
| 66 | + |
| 67 | + opener := func(licPath string) (io.ReadCloser, error) { |
| 68 | + required := func(filePath string) bool { |
| 69 | + return path.Base(filePath) == licPath |
| 70 | + } |
| 71 | + |
| 72 | + f, err := findFileInZip(input.Content, input.Info.Size(), required) |
| 73 | + if err != nil { |
| 74 | + return nil, xerrors.Errorf("unable to find license file in `*.egg` file: %w", err) |
| 75 | + } else if f == nil { // zip doesn't contain license file |
| 76 | + return nil, nil |
| 77 | + } |
| 78 | + |
| 79 | + return f, nil |
| 80 | + } |
| 81 | + |
| 82 | + if err = fillAdditionalData(opener, app, a.licenseClassifierConfidenceLevel); err != nil { |
| 83 | + a.logger.Warn("Unable to collect additional info", log.Err(err)) |
| 84 | + } |
| 85 | + |
| 86 | + return &analyzer.AnalysisResult{ |
| 87 | + Applications: []types.Application{*app}, |
| 88 | + }, nil |
| 89 | +} |
| 90 | + |
| 91 | +func findFileInZip(r xio.ReadSeekerAt, zipSize int64, required func(filePath string) bool) (io.ReadCloser, error) { |
| 92 | + if _, err := r.Seek(0, io.SeekStart); err != nil { |
| 93 | + return nil, xerrors.Errorf("file seek error: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + zr, err := zip.NewReader(r, zipSize) |
| 97 | + if err != nil { |
| 98 | + return nil, xerrors.Errorf("zip reader error: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + found, ok := lo.Find(zr.File, func(f *zip.File) bool { |
| 102 | + return required(f.Name) |
| 103 | + }) |
| 104 | + if !ok { |
| 105 | + return nil, nil |
| 106 | + } |
| 107 | + |
| 108 | + f, err := found.Open() |
| 109 | + if err != nil { |
| 110 | + return nil, xerrors.Errorf("unable to open file in zip: %w", err) |
| 111 | + } |
| 112 | + |
| 113 | + return f, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (a *eggAnalyzer) Required(filePath string, _ os.FileInfo) bool { |
| 117 | + return filepath.Ext(filePath) == eggExt |
| 118 | +} |
| 119 | + |
| 120 | +func (a *eggAnalyzer) Type() analyzer.Type { |
| 121 | + return analyzer.TypePythonPkgEgg |
| 122 | +} |
| 123 | + |
| 124 | +func (a *eggAnalyzer) Version() int { |
| 125 | + return eggAnalyzerVersion |
| 126 | +} |
0 commit comments