Files
aquasecurity-trivy/pkg/fanal/analyzer/language/analyze.go

53 lines
1.5 KiB
Go
Raw Normal View History

package language
feat: support local filesystem and remote git repository (fanal#107) * test(integration): move to the test directory * chore: update fixtures path * test: put common test images under the test directory * chore(Makefile): rename * feat: support local filesystem and remote git repository [PART 1] (fanal#109) * feat(walker): add tar/fs walker * fs_test: Add test names Signed-off-by: Simarpreet Singh <simar@linux.com> * walk_test: Add Test_isIgnored Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 2] (fanal#110) * refactor(analyzer): merge OSAnalyzer, PkgAnalyze, LibAnalyzer into Analyzer * test: comment out temporarily * fix(amazon): check the length * fix(analyzer): make AnalysisResult a reference * library/analyzer: Refactor library analyzer code. Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 3] (fanal#111) * refactor(image): move directory * feat(applier): add applier * fix(apk): replace extractor with applier * test: comment out temporarily * feat: support local filesystem and remote git repository [PART 4] (fanal#112) * feat(artifact): add image, local and remote artifact * image_test: Rename test field to use new convention Signed-off-by: Simarpreet Singh <simar@linux.com> * image_test: Add a test for put artifact failure Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(remote): remove unnecessary files for unit test * feat: support local filesystem and remote git repository [PART 5] (fanal#113) * test(integration): fix tests * feat: support local filesystem and remote git repository [PART 6] (fanal#114) * feat(main): add sub commands * refactor(types): remove unused type * chore(mod): update * test(artifact): add mock * fix(analyzer): redhat must be replaced with oracle * fix(analyzer): debian must be replaced with ubuntu * fix(fs): display dir when hostname is empty Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> * fix: make AnalysisResult a reference Co-authored-by: Simarpreet Singh <simar@linux.com> * refactor(walker): fix comment Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-05-28 23:29:07 +03:00
import (
"golang.org/x/xerrors"
dio "github.com/aquasecurity/go-dep-parser/pkg/io"
feat: support local filesystem and remote git repository (fanal#107) * test(integration): move to the test directory * chore: update fixtures path * test: put common test images under the test directory * chore(Makefile): rename * feat: support local filesystem and remote git repository [PART 1] (fanal#109) * feat(walker): add tar/fs walker * fs_test: Add test names Signed-off-by: Simarpreet Singh <simar@linux.com> * walk_test: Add Test_isIgnored Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 2] (fanal#110) * refactor(analyzer): merge OSAnalyzer, PkgAnalyze, LibAnalyzer into Analyzer * test: comment out temporarily * fix(amazon): check the length * fix(analyzer): make AnalysisResult a reference * library/analyzer: Refactor library analyzer code. Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 3] (fanal#111) * refactor(image): move directory * feat(applier): add applier * fix(apk): replace extractor with applier * test: comment out temporarily * feat: support local filesystem and remote git repository [PART 4] (fanal#112) * feat(artifact): add image, local and remote artifact * image_test: Rename test field to use new convention Signed-off-by: Simarpreet Singh <simar@linux.com> * image_test: Add a test for put artifact failure Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(remote): remove unnecessary files for unit test * feat: support local filesystem and remote git repository [PART 5] (fanal#113) * test(integration): fix tests * feat: support local filesystem and remote git repository [PART 6] (fanal#114) * feat(main): add sub commands * refactor(types): remove unused type * chore(mod): update * test(artifact): add mock * fix(analyzer): redhat must be replaced with oracle * fix(analyzer): debian must be replaced with ubuntu * fix(fs): display dir when hostname is empty Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> * fix: make AnalysisResult a reference Co-authored-by: Simarpreet Singh <simar@linux.com> * refactor(walker): fix comment Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-05-28 23:29:07 +03:00
godeptypes "github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/types"
feat: support local filesystem and remote git repository (fanal#107) * test(integration): move to the test directory * chore: update fixtures path * test: put common test images under the test directory * chore(Makefile): rename * feat: support local filesystem and remote git repository [PART 1] (fanal#109) * feat(walker): add tar/fs walker * fs_test: Add test names Signed-off-by: Simarpreet Singh <simar@linux.com> * walk_test: Add Test_isIgnored Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 2] (fanal#110) * refactor(analyzer): merge OSAnalyzer, PkgAnalyze, LibAnalyzer into Analyzer * test: comment out temporarily * fix(amazon): check the length * fix(analyzer): make AnalysisResult a reference * library/analyzer: Refactor library analyzer code. Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 3] (fanal#111) * refactor(image): move directory * feat(applier): add applier * fix(apk): replace extractor with applier * test: comment out temporarily * feat: support local filesystem and remote git repository [PART 4] (fanal#112) * feat(artifact): add image, local and remote artifact * image_test: Rename test field to use new convention Signed-off-by: Simarpreet Singh <simar@linux.com> * image_test: Add a test for put artifact failure Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(remote): remove unnecessary files for unit test * feat: support local filesystem and remote git repository [PART 5] (fanal#113) * test(integration): fix tests * feat: support local filesystem and remote git repository [PART 6] (fanal#114) * feat(main): add sub commands * refactor(types): remove unused type * chore(mod): update * test(artifact): add mock * fix(analyzer): redhat must be replaced with oracle * fix(analyzer): debian must be replaced with ubuntu * fix(fs): display dir when hostname is empty Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> * fix: make AnalysisResult a reference Co-authored-by: Simarpreet Singh <simar@linux.com> * refactor(walker): fix comment Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-05-28 23:29:07 +03:00
)
func Analyze(fileType, filePath string, r dio.ReadSeekerAt, parser godeptypes.Parser) (*analyzer.AnalysisResult, error) {
parsedLibs, parsedDependencies, err := parser.Parse(r)
feat: support local filesystem and remote git repository (fanal#107) * test(integration): move to the test directory * chore: update fixtures path * test: put common test images under the test directory * chore(Makefile): rename * feat: support local filesystem and remote git repository [PART 1] (fanal#109) * feat(walker): add tar/fs walker * fs_test: Add test names Signed-off-by: Simarpreet Singh <simar@linux.com> * walk_test: Add Test_isIgnored Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 2] (fanal#110) * refactor(analyzer): merge OSAnalyzer, PkgAnalyze, LibAnalyzer into Analyzer * test: comment out temporarily * fix(amazon): check the length * fix(analyzer): make AnalysisResult a reference * library/analyzer: Refactor library analyzer code. Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 3] (fanal#111) * refactor(image): move directory * feat(applier): add applier * fix(apk): replace extractor with applier * test: comment out temporarily * feat: support local filesystem and remote git repository [PART 4] (fanal#112) * feat(artifact): add image, local and remote artifact * image_test: Rename test field to use new convention Signed-off-by: Simarpreet Singh <simar@linux.com> * image_test: Add a test for put artifact failure Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(remote): remove unnecessary files for unit test * feat: support local filesystem and remote git repository [PART 5] (fanal#113) * test(integration): fix tests * feat: support local filesystem and remote git repository [PART 6] (fanal#114) * feat(main): add sub commands * refactor(types): remove unused type * chore(mod): update * test(artifact): add mock * fix(analyzer): redhat must be replaced with oracle * fix(analyzer): debian must be replaced with ubuntu * fix(fs): display dir when hostname is empty Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> * fix: make AnalysisResult a reference Co-authored-by: Simarpreet Singh <simar@linux.com> * refactor(walker): fix comment Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-05-28 23:29:07 +03:00
if err != nil {
return nil, xerrors.Errorf("failed to parse %s: %w", filePath, err)
feat: support local filesystem and remote git repository (fanal#107) * test(integration): move to the test directory * chore: update fixtures path * test: put common test images under the test directory * chore(Makefile): rename * feat: support local filesystem and remote git repository [PART 1] (fanal#109) * feat(walker): add tar/fs walker * fs_test: Add test names Signed-off-by: Simarpreet Singh <simar@linux.com> * walk_test: Add Test_isIgnored Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 2] (fanal#110) * refactor(analyzer): merge OSAnalyzer, PkgAnalyze, LibAnalyzer into Analyzer * test: comment out temporarily * fix(amazon): check the length * fix(analyzer): make AnalysisResult a reference * library/analyzer: Refactor library analyzer code. Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 3] (fanal#111) * refactor(image): move directory * feat(applier): add applier * fix(apk): replace extractor with applier * test: comment out temporarily * feat: support local filesystem and remote git repository [PART 4] (fanal#112) * feat(artifact): add image, local and remote artifact * image_test: Rename test field to use new convention Signed-off-by: Simarpreet Singh <simar@linux.com> * image_test: Add a test for put artifact failure Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(remote): remove unnecessary files for unit test * feat: support local filesystem and remote git repository [PART 5] (fanal#113) * test(integration): fix tests * feat: support local filesystem and remote git repository [PART 6] (fanal#114) * feat(main): add sub commands * refactor(types): remove unused type * chore(mod): update * test(artifact): add mock * fix(analyzer): redhat must be replaced with oracle * fix(analyzer): debian must be replaced with ubuntu * fix(fs): display dir when hostname is empty Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> * fix: make AnalysisResult a reference Co-authored-by: Simarpreet Singh <simar@linux.com> * refactor(walker): fix comment Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-05-28 23:29:07 +03:00
}
// The file path of each library should be empty in case of dependency list such as lock file
// since they all will be the same path.
return ToAnalysisResult(fileType, filePath, "", parsedLibs, parsedDependencies), nil
}
func ToAnalysisResult(fileType, filePath, libFilePath string, libs []godeptypes.Library, depGraph []godeptypes.Dependency) *analyzer.AnalysisResult {
if len(libs) == 0 {
return nil
}
deps := make(map[string][]string)
for _, dep := range depGraph {
deps[dep.ID] = dep.DependsOn
}
var pkgs []types.Package
for _, lib := range libs {
pkgs = append(pkgs, types.Package{
ID: lib.ID,
Name: lib.Name,
Version: lib.Version,
FilePath: libFilePath,
Indirect: lib.Indirect,
License: lib.License,
DependsOn: deps[lib.ID],
})
}
apps := []types.Application{{
Type: fileType,
FilePath: filePath,
Libraries: pkgs,
}}
return &analyzer.AnalysisResult{Applications: apps}
feat: support local filesystem and remote git repository (fanal#107) * test(integration): move to the test directory * chore: update fixtures path * test: put common test images under the test directory * chore(Makefile): rename * feat: support local filesystem and remote git repository [PART 1] (fanal#109) * feat(walker): add tar/fs walker * fs_test: Add test names Signed-off-by: Simarpreet Singh <simar@linux.com> * walk_test: Add Test_isIgnored Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 2] (fanal#110) * refactor(analyzer): merge OSAnalyzer, PkgAnalyze, LibAnalyzer into Analyzer * test: comment out temporarily * fix(amazon): check the length * fix(analyzer): make AnalysisResult a reference * library/analyzer: Refactor library analyzer code. Signed-off-by: Simarpreet Singh <simar@linux.com> * feat: support local filesystem and remote git repository [PART 3] (fanal#111) * refactor(image): move directory * feat(applier): add applier * fix(apk): replace extractor with applier * test: comment out temporarily * feat: support local filesystem and remote git repository [PART 4] (fanal#112) * feat(artifact): add image, local and remote artifact * image_test: Rename test field to use new convention Signed-off-by: Simarpreet Singh <simar@linux.com> * image_test: Add a test for put artifact failure Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(remote): remove unnecessary files for unit test * feat: support local filesystem and remote git repository [PART 5] (fanal#113) * test(integration): fix tests * feat: support local filesystem and remote git repository [PART 6] (fanal#114) * feat(main): add sub commands * refactor(types): remove unused type * chore(mod): update * test(artifact): add mock * fix(analyzer): redhat must be replaced with oracle * fix(analyzer): debian must be replaced with ubuntu * fix(fs): display dir when hostname is empty Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> * fix: make AnalysisResult a reference Co-authored-by: Simarpreet Singh <simar@linux.com> * refactor(walker): fix comment Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com> Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-05-28 23:29:07 +03:00
}