Skip to content

Commit

Permalink
index-report: update Files field
Browse files Browse the repository at this point in the history
We should be accounting for more than one file per layer and therefore
this change (from map[layer_id]claircore.File to
map[layer_id][]claircore.File) is needed.

Signed-off-by: crozzy <[email protected]>
  • Loading branch information
crozzy committed May 22, 2024
1 parent 5b7c8e0 commit 942e2a0
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 49 deletions.
2 changes: 1 addition & 1 deletion indexer/controller/coalesce.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func MergeSR(source *claircore.IndexReport, merge []*claircore.IndexReport) *cla
}

for k, v := range ir.Files {
source.Files[k] = v
source.Files[k] = append(source.Files[k], v...)
}
}
return source
Expand Down
2 changes: 1 addition & 1 deletion indexer/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func New(options *indexer.Options) *Controller {
Environments: map[string][]*claircore.Environment{},
Distributions: map[string]*claircore.Distribution{},
Repositories: map[string]*claircore.Repository{},
Files: map[string]claircore.File{},
Files: map[string][]claircore.File{},
}

s := &Controller{
Expand Down
2 changes: 1 addition & 1 deletion indexreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type IndexReport struct {
// an error string in the case the index did not succeed
Err string `json:"err"`
// Files doesn't end up in the json report but needs to be available at post-coalesce
Files map[string]File `json:"-"`
Files map[string][]File `json:"-"`
}

// IndexRecords returns a list of IndexRecords derived from the IndexReport
Expand Down
4 changes: 2 additions & 2 deletions whiteout/coalescer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func (c *coalescer) Coalesce(ctx context.Context, layerArtifacts []*indexer.Laye
for _, l := range layerArtifacts {
for _, f := range l.Files {
if ir.Files == nil {
ir.Files = make(map[string]claircore.File)
ir.Files = make(map[string][]claircore.File)
}
ir.Files[l.Hash.String()] = f
ir.Files[l.Hash.String()] = append(ir.Files[l.Hash.String()], f)
}
}
return ir, nil
Expand Down
28 changes: 15 additions & 13 deletions whiteout/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ func (r *Resolver) Resolve(ctx context.Context, ir *claircore.IndexReport, layer
packageLayer = ir.Environments[pkgID][i].IntroducedIn.String()
}
}
for fileLayer, f := range ir.Files {
// Check if it's a whiteout file, if it applies to the package's
// filepath and if the layer the whiteout file came from came after.
// The spec states: "Whiteout files MUST only apply to resources in
// lower/parent layers" hence why we don't check if they're in the same
// layer.
if f.Kind == claircore.FileKindWhiteout && ls.isChildOf(fileLayer, packageLayer) && fileIsDeleted(pkg.Filepath, f.Path) {
packageDeleted = true
zlog.Debug(ctx).
Str("package name", pkg.Name).
Str("package file", pkg.Filepath).
Str("whiteout file", f.Path).
Msg("package determined to be deleted")
for fileLayer, fs := range ir.Files {
for _, f := range fs {
// Check if it's a whiteout file, if it applies to the package's
// filepath and if the layer the whiteout file came from came after.
// The spec states: "Whiteout files MUST only apply to resources in
// lower/parent layers" hence why we don't check if they're in the same
// layer.
if f.Kind == claircore.FileKindWhiteout && ls.isChildOf(fileLayer, packageLayer) && fileIsDeleted(pkg.Filepath, f.Path) {
packageDeleted = true
zlog.Debug(ctx).
Str("package name", pkg.Name).
Str("package file", pkg.Filepath).
Str("whiteout file", f.Path).
Msg("package determined to be deleted")
}
}
}
if !packageDeleted {
Expand Down
108 changes: 77 additions & 31 deletions whiteout/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ func TestResolver(t *testing.T) {
"1": {{IntroducedIn: firstLayerHash}},
"2": {{IntroducedIn: firstLayerHash}},
},
Files: map[string]claircore.File{
Files: map[string][]claircore.File{
secondLayerHash.String(): {
Path: "a/path/to/some/file/site-packages/.wh.a_package",
Kind: claircore.FileKindWhiteout,
{
Path: "a/path/to/some/file/site-packages/.wh.a_package",
Kind: claircore.FileKindWhiteout,
},
},
},
},
Expand Down Expand Up @@ -82,18 +84,20 @@ func TestResolver(t *testing.T) {
"1": {{IntroducedIn: firstLayerHash}},
"2": {{IntroducedIn: firstLayerHash}},
},
Files: map[string]claircore.File{
Files: map[string][]claircore.File{
secondLayerHash.String(): {
Path: "a/path/to/some/different_file/site-packages/.wh.a_package",
Kind: claircore.FileKindWhiteout,
},
secondLayerHash.String(): {
Path: "a/path/to/some/different_file/.wh.site-packages",
Kind: claircore.FileKindWhiteout,
},
secondLayerHash.String(): {
Path: "a/path/to/some/.wh.different_file",
Kind: claircore.FileKindWhiteout,
{
Path: "a/path/to/some/different_file/site-packages/.wh.a_package",
Kind: claircore.FileKindWhiteout,
},
{
Path: "a/path/to/some/different_file/.wh.site-packages",
Kind: claircore.FileKindWhiteout,
},
{
Path: "a/path/to/some/.wh.different_file",
Kind: claircore.FileKindWhiteout,
},
},
},
},
Expand Down Expand Up @@ -121,10 +125,12 @@ func TestResolver(t *testing.T) {
"1": {{IntroducedIn: firstLayerHash}},
"2": {{IntroducedIn: firstLayerHash}},
},
Files: map[string]claircore.File{
Files: map[string][]claircore.File{
secondLayerHash.String(): {
Path: "a/path/to/some/file/.wh.site-packages",
Kind: claircore.FileKindWhiteout,
{
Path: "a/path/to/some/file/.wh.site-packages",
Kind: claircore.FileKindWhiteout,
},
},
},
},
Expand Down Expand Up @@ -152,10 +158,12 @@ func TestResolver(t *testing.T) {
"1": {{IntroducedIn: firstLayerHash}},
"2": {{IntroducedIn: firstLayerHash}},
},
Files: map[string]claircore.File{
firstLayerHash.String(): { // whiteout is in the same layer as packages
Path: "a/path/to/some/file/site-packages/.wh.b_package",
Kind: claircore.FileKindWhiteout,
Files: map[string][]claircore.File{
firstLayerHash.String(): {
{ // whiteout is in the same layer as packages
Path: "a/path/to/some/file/site-packages/.wh.b_package",
Kind: claircore.FileKindWhiteout,
},
},
},
},
Expand Down Expand Up @@ -183,10 +191,12 @@ func TestResolver(t *testing.T) {
"1": {{IntroducedIn: firstLayerHash}},
"2": {{IntroducedIn: firstLayerHash}},
},
Files: map[string]claircore.File{
Files: map[string][]claircore.File{
secondLayerHash.String(): {
Path: "a/path/to/some/file/site/.wh..wh..opq",
Kind: claircore.FileKindWhiteout,
{
Path: "a/path/to/some/file/site/.wh..wh..opq",
Kind: claircore.FileKindWhiteout,
},
},
},
},
Expand Down Expand Up @@ -214,10 +224,12 @@ func TestResolver(t *testing.T) {
"1": {{IntroducedIn: firstLayerHash}},
"2": {{IntroducedIn: firstLayerHash}},
},
Files: map[string]claircore.File{
Files: map[string][]claircore.File{
secondLayerHash.String(): {
Path: "a/path/to/some/file/site-packages/.wh..wh..opq",
Kind: claircore.FileKindWhiteout,
{
Path: "a/path/to/some/file/site-packages/.wh..wh..opq",
Kind: claircore.FileKindWhiteout,
},
},
},
},
Expand All @@ -240,10 +252,12 @@ func TestResolver(t *testing.T) {
Environments: map[string][]*claircore.Environment{
"1": {{IntroducedIn: firstLayerHash}, {IntroducedIn: thirdLayerHash}},
},
Files: map[string]claircore.File{
secondLayerHash.String(): { // whiteout is sandwiched
Path: "a/path/to/some/file/site-packages/.wh.a_package",
Kind: claircore.FileKindWhiteout,
Files: map[string][]claircore.File{
secondLayerHash.String(): {
{ // whiteout is sandwiched
Path: "a/path/to/some/file/site-packages/.wh.a_package",
Kind: claircore.FileKindWhiteout,
},
},
},
},
Expand All @@ -255,6 +269,38 @@ func TestResolver(t *testing.T) {
LenPackage: 1,
LenEnvs: 1,
},
{
Name: "Multiple whiteout files",
Report: &claircore.IndexReport{
Packages: map[string]*claircore.Package{
"1": {
Name: "something interesting",
Filepath: "a/path/to/some/file/site-packages/a_package/METADATA",
},
},
Environments: map[string][]*claircore.Environment{
"1": {{IntroducedIn: firstLayerHash}},
},
Files: map[string][]claircore.File{
secondLayerHash.String(): {
{
Path: "a/path/to/some/file/site-packages/.wh.a_package",
Kind: claircore.FileKindWhiteout,
},
{ // non interesting whiteout
Path: "another/path/to/some/file/site-packages/.wh.a_package",
Kind: claircore.FileKindWhiteout,
},
},
},
},
Layers: []*claircore.Layer{
{Hash: firstLayerHash},
{Hash: secondLayerHash},
},
LenPackage: 0,
LenEnvs: 0,
},
}

r := &Resolver{}
Expand Down

0 comments on commit 942e2a0

Please sign in to comment.