forked from aquasecurity/trivy-plugin-referrer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
167 lines (139 loc) · 4.56 KB
/
get.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/samber/lo"
)
var errNoReferrerFound = fmt.Errorf("no referrer found")
func getReferrer(writer io.Writer, opts getOptions) error {
var err error
var artifactDigest name.Digest
ref, err := name.ParseReference(opts.Reference, opts.NameOptions()...)
if err != nil {
return fmt.Errorf("error parsing reference: %w", err)
}
remoteOpts := append(opts.RemoteOptions(), remote.WithAuthFromKeychain(authn.DefaultKeychain))
desc, err := remote.Head(ref, remoteOpts...)
if err != nil {
return fmt.Errorf("error getting descriptor: %w", err)
}
digest, err := name.NewDigest(
fmt.Sprintf("%s/%s@%s", ref.Context().RegistryStr(), ref.Context().RepositoryStr(), desc.Digest.String()),
opts.NameOptions()...,
)
if err != nil {
return fmt.Errorf("error creating digest: %w", err)
}
artifactDigest, err = findArtifactDigest(digest, opts)
if errors.Is(err, errNoReferrerFound) {
log.Logger.Infof("no referrer found(%s)", opts.Type)
return nil
}
if err != nil {
return fmt.Errorf("error getting artifact digest: %w", err)
}
image, err := remote.Image(artifactDigest, remoteOpts...)
if err != nil {
return fmt.Errorf("error fetching image: %w", err)
}
layers, err := image.Layers()
if err != nil {
return fmt.Errorf("error getting layers: %w", err)
}
artifact, err := layers[0].Compressed()
if err != nil {
return fmt.Errorf("error getting artifact: %w", err)
}
if _, err = io.Copy(writer, artifact); err != nil {
return fmt.Errorf("error copying artifact: %w", err)
}
return nil
}
func findArtifactDigest(digest name.Digest, opts getOptions) (name.Digest, error) {
index, err := remote.Referrers(digest, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return name.Digest{}, fmt.Errorf("error fetching referrers: %w", err)
}
var manifest v1.Descriptor
if opts.Digest != "" {
manifest, err = findReferrerByDigest(index, opts)
if err != nil {
return name.Digest{}, fmt.Errorf("error finding referrers: %w", err)
}
} else if opts.Type != "" {
manifest, err = findLatestReferrerByType(index, opts)
if err != nil {
return name.Digest{}, fmt.Errorf("error fetching referrers: %w", err)
}
} else {
return name.Digest{}, fmt.Errorf("either digest or type must be specified")
}
artifactDigest, err := name.NewDigest(
fmt.Sprintf("%s/%s@%s", digest.Context().RegistryStr(), digest.Context().RepositoryStr(), manifest.Digest.String()),
opts.NameOptions()...,
)
if err != nil {
return name.Digest{}, fmt.Errorf("error parsing artifact tag: %w", err)
}
return artifactDigest, nil
}
func findReferrerByDigest(index v1.ImageIndex, opts getOptions) (v1.Descriptor, error) {
digest := opts.Digest
if !strings.HasPrefix(digest, "sha256:") {
digest = "sha256:" + digest
}
indexManifest, err := index.IndexManifest()
if err != nil {
return v1.Descriptor{}, fmt.Errorf("error getting index manifest: %w", err)
}
m, found := lo.Find(indexManifest.Manifests, func(item v1.Descriptor) bool {
return strings.HasPrefix(item.Digest.String(), digest)
})
if !found {
return v1.Descriptor{}, errNoReferrerFound
}
return m, nil
}
func findLatestReferrerByType(imageIndex v1.ImageIndex, opts getOptions) (v1.Descriptor, error) {
artifactType, err := artifactTypeFromName(opts.Type)
if err != nil {
return v1.Descriptor{}, fmt.Errorf("error getting artifact type: %w", err)
}
indexManifest, err := imageIndex.IndexManifest()
if err != nil {
return v1.Descriptor{}, fmt.Errorf("error getting index manifest: %w", err)
}
filtered := lo.Filter(indexManifest.Manifests, func(item v1.Descriptor, index int) bool {
return item.ArtifactType == artifactType.MediaType()
})
if len(filtered) == 0 {
return v1.Descriptor{}, errNoReferrerFound
}
if len(filtered) > 1 {
log.Logger.Infof("%d referrers found(%s)", len(filtered), opts.Type)
}
latest := lo.MaxBy(filtered, func(item v1.Descriptor, max v1.Descriptor) bool {
var t1, t2 time.Time
if v, ok := item.Annotations[annotationKeyCreated]; ok {
t1, _ = time.Parse(time.RFC3339, v)
} else {
t1 = time.Time{}
}
if v, ok := max.Annotations[annotationKeyCreated]; ok {
t2, _ = time.Parse(time.RFC3339, v)
} else {
t2 = time.Time{}
}
// In case both values are not set, take the latter one.
return t1.Compare(t2) >= 0
})
return latest, nil
}