-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
154 lines (125 loc) · 4.62 KB
/
registry.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
package registry
import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
)
const EnvGcrJSONKeyPath = "GCR_JSON_KEY_PATH"
// Registry is a struct to work with authenticated container registries.
type Registry struct {
URL string
authenticator authn.Authenticator
}
// New creates a new Registry instance.
func New(url string) (*Registry, error) {
r := Registry{URL: url}
if err := r.initAuthenticator(); err != nil {
return nil, fmt.Errorf("failed to init authenticator: %w", err)
}
return &r, nil
}
// String is the Implementation of "github.com/google/go-containerregistry/pkg/authn/Resource".
func (r *Registry) String() string {
return r.URL
}
// RegistryStr is the Implementation of "github.com/google/go-containerregistry/pkg/authn/Resource".
func (r *Registry) RegistryStr() string {
return strings.Split(r.URL, "/")[0]
}
// initAuthenticator returns an authn.Authenticator used by the docker golang library
// to authenticate with a docker registry
//
// We generate the authn.Authenticator once, otherwise the resolver will try to resolve
// the gcloud credentials before each api call. We check for the presence of an environment
// variable GCR_JSON_KEY_PATH. If present, we use it, otherwise, we default to the default
// keychain mechanism.
func (r *Registry) initAuthenticator() error {
gcrJSONKeyPath := os.Getenv(EnvGcrJSONKeyPath)
if gcrJSONKeyPath != "" {
key, err := os.ReadFile(gcrJSONKeyPath)
if err != nil {
return fmt.Errorf("failed to resolve authenticator using gcr json key at %s: %w", gcrJSONKeyPath, err)
}
r.authenticator = &authn.Basic{
Username: "_json_key",
Password: string(key),
}
return nil
}
var err error
r.authenticator, err = authn.DefaultKeychain.Resolve(r)
if err != nil {
return fmt.Errorf("failed to resolve authenticator using default keychain: %w", err)
}
return nil
}
// Head is a wrapper to the remote.Head method.
func (r *Registry) Head(imageRef string) (*v1.Descriptor, error) {
ref, err := name.ParseReference(imageRef)
if err != nil {
return nil, fmt.Errorf("failed to parse image reference %s: %w", imageRef, err)
}
head, err := remote.Head(ref, remote.WithAuth(r.authenticator))
if err != nil {
return nil, fmt.Errorf("failed to get head from remote for image %s: %w", imageRef, err)
}
return head, nil
}
// RefExists checks for the presence of the given ref on the registry.
func (r *Registry) RefExists(imageRef string) (bool, error) {
ref, err := name.ParseReference(imageRef)
if err != nil {
return false, fmt.Errorf("failed to parse image reference %s: %w", imageRef, err)
}
if _, err = remote.Head(ref, remote.WithAuth(r.authenticator)); err != nil {
var tErr *transport.Error
if errors.As(err, &tErr) && tErr.StatusCode == http.StatusNotFound {
return false, nil
}
return false, fmt.Errorf("failed to get head from remote for image %s: %w", imageRef, err)
}
return true, nil
}
// Inspect fetches the remote to get image information and returns it.
// The information returned is similar to what is output by the `docker inspect` command.
func (r *Registry) Inspect(imageRef string) (*v1.ConfigFile, error) {
ref, err := name.ParseReference(imageRef)
if err != nil {
return nil, fmt.Errorf("failed to parse image reference %s: %w", imageRef, err)
}
img, err := remote.Image(ref, remote.WithAuth(r.authenticator))
if err != nil {
return nil, fmt.Errorf("failed to get image details from remote for image %s: %w", imageRef, err)
}
cfg, err := img.ConfigFile()
if err != nil {
return nil, fmt.Errorf("failed to get image details from remote for image %s: %w", imageRef, err)
}
return cfg, nil
}
// Retag creates a new tag for a given image ref.
func (r *Registry) Retag(existingRef, toCreateRef string) error {
ref, err := name.ParseReference(existingRef)
if err != nil {
return fmt.Errorf("failed to parse image reference %s: %w", existingRef, err)
}
image, err := remote.Image(ref, remote.WithAuth(r.authenticator))
if err != nil {
return fmt.Errorf("failed to get reference from remote for image %s: %w", existingRef, err)
}
newTag, err := name.NewTag(toCreateRef)
if err != nil {
return fmt.Errorf("failed to create tag reference %s: %w", toCreateRef, err)
}
if err := remote.Tag(newTag, image, remote.WithAuth(r.authenticator)); err != nil {
return fmt.Errorf("failed to create tag (from %s to %s): %w", existingRef, toCreateRef, err)
}
return nil
}