/
registry.go
139 lines (119 loc) · 3.78 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
package registry
import (
"context"
"crypto/sha256"
"fmt"
"github.com/habakke/terraform-provider-docker/internal/util"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"io"
"net"
"net/http"
"time"
)
var GCRScopes = []string{"https://www.googleapis.com/auth/cloud-platform"}
type Registry interface {
ManifestDigest(name string, tag string) (string, error)
}
type registry struct {
dockerHost string
Client *http.Client
Logger util.Logger
username string
password string
}
func New(ctx context.Context, dockerHost, username, password string, logger util.Logger) (*registry, error) {
var httpClient *http.Client
googleCredentials, _ := google.FindDefaultCredentials(ctx, GCRScopes...)
httpClient, err := createHTTPClient(ctx, dockerHost, username, password, googleCredentials, logger)
if err != nil {
return nil, err
}
return ®istry{
dockerHost: dockerHost,
Client: httpClient,
Logger: logger,
username: username,
password: password,
}, nil
}
func (r registry) ManifestDigest(ctx context.Context, name string, tag string) (string, error) {
h, err := r.dockerRegistryHead(ctx, fmt.Sprintf("/%s/manifests/%s", name, tag))
if err != nil {
return "", err
}
return h.Get("Docker-Content-Digest"), nil
}
func createDefaultTransport() *http.Transport {
return &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableKeepAlives: true,
}
}
func createHTTPClient(ctx context.Context, dockerHost, username, password string, googleCreds *google.Credentials, logger util.Logger) (*http.Client, error) {
if googleCreds == nil {
return &http.Client{
Transport: util.NewLoggingRoundTripper(ctx,
NewBasicTransport(dockerHost, username, password, NewTokenTransport(username, password, createDefaultTransport())), logger),
}, nil
}
client := &http.Client{
Transport: util.NewLoggingRoundTripper(ctx, &oauth2.Transport{
Base: http.DefaultTransport,
Source: googleCreds.TokenSource,
}, logger),
}
return client, nil
}
func dockerManifestDigest(manifest []byte) string {
h := sha256.New()
h.Write(manifest)
bs := h.Sum(nil)
return fmt.Sprintf("%x", bs)
}
func (r registry) dockerRegistryHead(ctx context.Context, path string) (http.Header, error) {
req, err := http.NewRequestWithContext(ctx, "HEAD", "https://"+r.dockerHost+"/v2"+path, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.v2+json")
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.list.v2+json")
res, err := r.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("image not found")
}
return nil, fmt.Errorf("expected OK status, got %d from docker registry '%s'", res.StatusCode, path)
}
return res.Header.Clone(), nil
}
func (r registry) dockerRegistryGet(ctx context.Context, path string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "GET", "https://"+r.dockerHost+"/v2"+path, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.v2+json")
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.list.v2+json")
res, err := r.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("image not found")
}
return nil, fmt.Errorf("expected OK status, got %d from docker registry '%s'", res.StatusCode, path)
}
return io.ReadAll(res.Body)
}