-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpm: implement an RPM filescanner to discern RPM filepaths
Using the filepaths discovered by the RPM filescanner we can judge whether or not a language package has been installed via RPM or not. Signed-off-by: crozzy <[email protected]>
- Loading branch information
Showing
16 changed files
with
789 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package rpm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/fs" | ||
"runtime/trace" | ||
|
||
"github.com/quay/zlog" | ||
|
||
"github.com/quay/claircore" | ||
"github.com/quay/claircore/indexer" | ||
) | ||
|
||
const ( | ||
scannerName = "rpm" | ||
scannerVersion = "1" | ||
scannerKind = "file" | ||
) | ||
|
||
var ( | ||
_ indexer.FileScanner = (*FileScanner)(nil) | ||
_ indexer.VersionedScanner = (*FileScanner)(nil) | ||
) | ||
|
||
type FileScanner struct{} | ||
|
||
func (*FileScanner) Name() string { return scannerName } | ||
|
||
func (*FileScanner) Version() string { return scannerVersion } | ||
|
||
func (*FileScanner) Kind() string { return scannerKind } | ||
|
||
func (s *FileScanner) Scan(ctx context.Context, layer *claircore.Layer) ([]claircore.File, error) { | ||
if err := ctx.Err(); err != nil { | ||
return nil, err | ||
} | ||
defer trace.StartRegion(ctx, "FileScanner.Scan").End() | ||
trace.Log(ctx, "layer", layer.Hash.String()) | ||
ctx = zlog.ContextWithValues(ctx, | ||
"component", "rpm/FileScanner.Scan", | ||
"version", s.Version(), | ||
"layer", layer.Hash.String()) | ||
zlog.Debug(ctx).Msg("start") | ||
defer zlog.Debug(ctx).Msg("done") | ||
|
||
sys, err := layer.FS() | ||
if err != nil { | ||
return nil, fmt.Errorf("rpm: unable to open layer: %w", err) | ||
} | ||
|
||
found := make([]foundDB, 0) | ||
if err := fs.WalkDir(sys, ".", findDBs(ctx, &found, sys)); err != nil { | ||
return nil, fmt.Errorf("rpm: error walking fs: %w", err) | ||
} | ||
if len(found) == 0 { | ||
return nil, nil | ||
} | ||
|
||
done := map[string]struct{}{} | ||
files := []claircore.File{} | ||
|
||
for _, db := range found { | ||
ctx := zlog.ContextWithValues(ctx, "db", db.String()) | ||
zlog.Debug(ctx).Msg("examining database") | ||
if _, ok := done[db.Path]; ok { | ||
zlog.Debug(ctx).Msg("already seen, skipping") | ||
continue | ||
} | ||
done[db.Path] = struct{}{} | ||
nat, err := dbFileToNativeDB(ctx, sys, db) | ||
if err != nil { | ||
return nil, fmt.Errorf("rpm: error getting native DBs: %w", err) | ||
} | ||
fs, err := filesFromDB(ctx, nat) | ||
if err != nil { | ||
return nil, fmt.Errorf("rpm: error getting files from native DBs: %w", err) | ||
} | ||
files = append(files, fs...) | ||
} | ||
|
||
zlog.Debug(ctx).Int("count", len(found)).Msg("found possible databases") | ||
|
||
return files, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package rpm | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/quay/zlog" | ||
|
||
"github.com/quay/claircore" | ||
"github.com/quay/claircore/test" | ||
) | ||
|
||
var testcases = []struct { | ||
name string | ||
filename string | ||
expectedFiles int | ||
}{ | ||
{ | ||
name: "java layer", | ||
filename: "cdc13a947214994058941dee5dab876369896ec672defa07694cec6dd3fc7ca2", | ||
expectedFiles: 82, | ||
}, | ||
{ | ||
name: "open jdk layer", | ||
filename: "f68995d3d7382737a1ee41fb69ca9369693173dba4263233621f4defcb29c4bd", | ||
expectedFiles: 218, | ||
}, | ||
} | ||
|
||
func TestFileScannerLayer(t *testing.T) { | ||
ctx := context.Background() | ||
var s FileScanner | ||
desc := claircore.LayerDescription{ | ||
Digest: test.RandomSHA256Digest(t).String(), | ||
URI: "file:///dev/null", | ||
MediaType: test.MediaType, | ||
Headers: make(map[string][]string), | ||
} | ||
|
||
for _, tt := range testcases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := zlog.Test(ctx, t) | ||
f, err := os.Open(filepath.Join(`testdata/layers`, tt.filename)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(func() { | ||
if err := f.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
var l claircore.Layer | ||
if err := l.Init(ctx, &desc, f); err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(func() { | ||
if err := l.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
|
||
got, err := s.Scan(ctx, &l) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
t.Logf("found %d files", len(got)) | ||
if len(got) != tt.expectedFiles { | ||
t.Fatalf("expected %d files but got %d", tt.expectedFiles, len(got)) | ||
} | ||
t.Log(got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.