forked from genuinetools/reg
-
Notifications
You must be signed in to change notification settings - Fork 2
/
layerutil.go
96 lines (80 loc) · 2.49 KB
/
layerutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package clair
import (
"context"
"fmt"
"github.com/ttys3/reg/registry"
"strings"
"github.com/distribution/distribution/v3"
"github.com/quay/clair/v3/api/v3/clairpb"
)
// NewClairLayer will form a layer struct required for a clair scan.
func (c *Clair) NewClairLayer(ctx context.Context, r *registry.Registry, image string, fsLayers map[int]distribution.Descriptor, index int) (*Layer, error) {
var parentName string
if index < len(fsLayers)-1 {
parentName = fsLayers[index+1].Digest.String()
}
// Form the path.
p := strings.Join([]string{r.URL, "v2", image, "blobs", fsLayers[index].Digest.String()}, "/")
// Get the headers.
h, err := r.Headers(ctx, p)
if err != nil {
return nil, err
}
return &Layer{
Name: fsLayers[index].Digest.String(),
Path: p,
ParentName: parentName,
Format: "Docker",
Headers: h,
}, nil
}
// NewClairV3Layer will form a layer struct required for a clair scan.
func (c *Clair) NewClairV3Layer(ctx context.Context, r *registry.Registry, image string, fsLayer distribution.Descriptor) (*clairpb.PostAncestryRequest_PostLayer, error) {
// Form the path.
p := strings.Join([]string{r.URL, "v2", image, "blobs", fsLayer.Digest.String()}, "/")
// Get the headers.
h, err := r.Headers(ctx, p)
if err != nil {
return nil, err
}
return &clairpb.PostAncestryRequest_PostLayer{
Hash: fsLayer.Digest.String(),
Path: p,
Headers: h,
}, nil
}
func (c *Clair) getLayers(ctx context.Context, r *registry.Registry, repo, tag string, filterEmpty bool) (map[int]distribution.Descriptor, string, error) {
ok := true
// Get the manifest to pass to clair.
mf, err := r.ManifestV2(ctx, repo, tag)
if err != nil {
ok = false
c.Logf("couldn't retrieve manifest v2, falling back to v1")
}
filteredLayers := map[int]distribution.Descriptor{}
// 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]
}
}
return filteredLayers, mf.Config.Digest.String(), nil
}
m, err := r.ManifestOCI(ctx, repo, tag)
if err != nil {
return nil, "", fmt.Errorf("getting the v1 manifest for %s:%s failed: %v", repo, tag, err)
}
for i := 0; i < len(m.Layers); i++ {
if filterEmpty && IsEmptyLayer(m.Layers[i].Digest) {
continue
} else {
filteredLayers[i] = distribution.Descriptor{
Digest: m.Layers[i].Digest,
}
}
}
return filteredLayers, filteredLayers[0].Digest.String(), nil
}