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

Fix reg cannot scan vulnerabilities on images with empty layers #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 4 additions & 12 deletions clair/layerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *Clair) NewClairV3Layer(ctx context.Context, r *registry.Registry, image
}, nil
}

func (c *Clair) getLayers(ctx context.Context, r *registry.Registry, repo, tag string, filterEmpty bool) (map[int]distribution.Descriptor, string, error) {
func (c *Clair) getLayers(ctx context.Context, r *registry.Registry, repo, tag string) (map[int]distribution.Descriptor, string, error) {
ok := true
// Get the manifest to pass to clair.
mf, err := r.ManifestV2(ctx, repo, tag)
Expand All @@ -67,11 +67,7 @@ func (c *Clair) getLayers(ctx context.Context, r *registry.Registry, repo, tag s
// Filter out the empty layers.
if ok {
for i := 0; i < len(mf.Layers); i++ {
if filterEmpty && IsEmptyLayer(mf.Layers[i].Digest) {
continue
} else {
filteredLayers[len(mf.Layers)-i-1] = mf.Layers[i]
}
filteredLayers[len(mf.Layers)-i-1] = mf.Layers[i]
}

return filteredLayers, mf.Config.Digest.String(), nil
Expand All @@ -83,12 +79,8 @@ func (c *Clair) getLayers(ctx context.Context, r *registry.Registry, repo, tag s
}

for i := 0; i < len(m.FSLayers); i++ {
if filterEmpty && IsEmptyLayer(m.FSLayers[i].BlobSum) {
continue
} else {
filteredLayers[i] = distribution.Descriptor{
Digest: m.FSLayers[i].BlobSum,
}
filteredLayers[i] = distribution.Descriptor{
Digest: m.FSLayers[i].BlobSum,
}
}

Expand Down
28 changes: 17 additions & 11 deletions clair/vulns.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ func (c *Clair) Vulnerabilities(ctx context.Context, r *registry.Registry, repo,
VulnsBySeverity: make(map[string][]Vulnerability),
}

filteredLayers, _, err := c.getLayers(ctx, r, repo, tag, true)
layers, _, err := c.getLayers(ctx, r, repo, tag)
if err != nil {
return report, fmt.Errorf("getting filtered layers failed: %v", err)
return report, fmt.Errorf("getting layers failed: %v", err)
}

if len(filteredLayers) == 0 {
fmt.Printf("No need to analyse image %s:%s as there is no non-emtpy layer", repo, tag)
return report, nil
}
lastPostedLayer := ""
for i := len(layers) - 1; i >= 0; i-- {
if IsEmptyLayer(layers[i].Digest) {
continue
}

for i := len(filteredLayers) - 1; i >= 0; i-- {
// Form the clair layer.
l, err := c.NewClairLayer(ctx, r, repo, filteredLayers, i)
l, err := c.NewClairLayer(ctx, r, repo, layers, i)
if err != nil {
return report, err
}
Expand All @@ -41,11 +41,17 @@ func (c *Clair) Vulnerabilities(ctx context.Context, r *registry.Registry, repo,
if _, err := c.PostLayer(ctx, l); err != nil {
return report, err
}

lastPostedLayer = l.Name
}

report.Name = filteredLayers[0].Digest.String()
if lastPostedLayer == "" {
fmt.Printf("No need to analyse image %s:%s as there is no non-emtpy layer", repo, tag)
return report, nil
}

vl, err := c.GetLayer(ctx, filteredLayers[0].Digest.String(), true, true)
report.Name = lastPostedLayer
vl, err := c.GetLayer(ctx, lastPostedLayer, true, true)
if err != nil {
return report, err
}
Expand Down Expand Up @@ -86,7 +92,7 @@ func (c *Clair) VulnerabilitiesV3(ctx context.Context, r *registry.Registry, rep
VulnsBySeverity: make(map[string][]Vulnerability),
}

layers, reportName, err := c.getLayers(ctx, r, repo, tag, false)
layers, reportName, err := c.getLayers(ctx, r, repo, tag)
if err != nil {
return report, fmt.Errorf("getting filtered layers failed: %v", err)
}
Expand Down