-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathremote.go
90 lines (73 loc) · 1.93 KB
/
remote.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
package image
import (
"context"
"fmt"
"strings"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/remote"
)
func tryRemote(ctx context.Context, imageName string, ref name.Reference, option types.ImageOptions) (types.Image, func(), error) {
// This function doesn't need cleanup
cleanup := func() {}
desc, err := remote.Get(ctx, ref, option.RegistryOptions)
if err != nil {
return nil, cleanup, err
}
img, err := desc.Image()
if err != nil {
return nil, cleanup, err
}
// Return v1.Image if the image is found in Docker Registry
return remoteImage{
name: imageName,
Image: img,
ref: implicitReference{ref: ref},
descriptor: desc,
}, cleanup, nil
}
type remoteImage struct {
name string
ref implicitReference
descriptor *remote.Descriptor
v1.Image
}
func (img remoteImage) Name() string {
return img.name
}
func (img remoteImage) ID() (string, error) {
return ID(img)
}
func (img remoteImage) RepoTags() []string {
tag := img.ref.TagName()
if tag == "" {
return []string{}
}
return []string{fmt.Sprintf("%s:%s", img.ref.RepositoryName(), tag)}
}
func (img remoteImage) RepoDigests() []string {
repoDigest := fmt.Sprintf("%s@%s", img.ref.RepositoryName(), img.descriptor.Digest.String())
return []string{repoDigest}
}
type implicitReference struct {
ref name.Reference
}
func (r implicitReference) TagName() string {
if t, ok := r.ref.(name.Tag); ok {
return t.TagStr()
}
return ""
}
func (r implicitReference) RepositoryName() string {
ctx := r.ref.Context()
reg := ctx.RegistryStr()
repo := ctx.RepositoryStr()
// Default registry
if reg != name.DefaultRegistry {
return fmt.Sprintf("%s/%s", reg, repo)
}
// Trim default namespace
// See https://docs.docker.com/docker-hub/official_repos
return strings.TrimPrefix(repo, "library/")
}