Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set InstalledFiles for DEB and RPM packages #5488

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/fanal/analyzer/pkg/dpkg/dpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (a dpkgAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysis
return path != availableFile
}

packageFiles := make(map[string][]string)

// parse other files
err = fsutils.WalkDir(input.FS, ".", required, func(path string, d fs.DirEntry, r io.Reader) error {
// parse list files
Expand All @@ -74,6 +76,7 @@ func (a dpkgAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysis
if err != nil {
return err
}
packageFiles[strings.TrimSuffix(filepath.Base(path), ".list")] = systemFiles
systemInstalledFiles = append(systemInstalledFiles, systemFiles...)
return nil
}
Expand All @@ -89,6 +92,17 @@ func (a dpkgAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysis
return nil, xerrors.Errorf("dpkg walk error: %w", err)
}

// map the packages to their respective files
for i, pkgInfo := range packageInfos {
for j, pkg := range pkgInfo.Packages {
installedFiles, found := packageFiles[pkg.Name]
if !found {
installedFiles = packageFiles[pkg.Name+":"+pkg.Arch]
}
packageInfos[i].Packages[j].InstalledFiles = installedFiles
}
}

return &analyzer.AnalysisResult{
PackageInfos: packageInfos,
SystemInstalledFiles: systemInstalledFiles,
Expand Down
8 changes: 5 additions & 3 deletions pkg/fanal/analyzer/pkg/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ func (a rpmPkgAnalyzer) listPkgs(db RPMDB) (types.Packages, []string, error) {
if err != nil {
return nil, nil, xerrors.Errorf("unable to get installed files: %w", err)
}
files = lo.Map(files, func(file string, _ int) string {
return filepath.ToSlash(file)
})

for i, file := range files {
files[i] = filepath.ToSlash(file)
}
}

// RPM DB uses MD5 digest
Expand Down Expand Up @@ -171,6 +172,7 @@ func (a rpmPkgAnalyzer) listPkgs(db RPMDB) (types.Packages, []string, error) {
DependsOn: pkg.Requires, // Will be replaced with package IDs
Maintainer: pkg.Vendor,
Digest: d,
InstalledFiles: files,
}
pkgs = append(pkgs, p)
installedFiles = append(installedFiles, files...)
Expand Down
5 changes: 5 additions & 0 deletions pkg/fanal/analyzer/pkg/rpm/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ func Test_rpmPkgAnalyzer_listPkgs(t *testing.T) {
SrcVersion: "2.17",
SrcRelease: "317.el7",
Maintainer: "Red Hat",
InstalledFiles: []string{
"/etc/ld.so.conf",
"/etc/rpc",
"/lib64/libm-2.27.so",
},
},
},
wantFiles: []string{
Expand Down