/
registry.go
154 lines (136 loc) · 3.87 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 (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"strings"
log "github.com/sirupsen/logrus"
"github.com/neuvector/neuvector/share/httptrace"
)
const (
ErrorNone uint = iota
ErrorCertificate
ErrorAuthentication
ErrorUrl
ErrorUnknown
)
type Registry struct {
URL string
Client *httptrace.TraceClient
}
/*
* Create a new Registry with the given URL and credentials, then Ping()s it
* before returning it to verify that the registry is available.
*
* You can, alternately, construct a Registry manually by populating the fields.
* This passes http.DefaultTransport to WrapTransport when creating the
* http.Client.
*/
func NewSecure(registryUrl, username, password, proxy string, trace httptrace.HTTPTrace) (*Registry, uint, error) {
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
},
}
if proxy != "" {
pxyUrl, err := url.Parse(proxy)
if err != nil {
return nil, ErrorUrl, err
}
transport.Proxy = http.ProxyURL(pxyUrl)
}
r := newFromTransport(registryUrl, username, password, transport, trace)
return r, ErrorNone, nil
}
/*
* Create a new Registry, as with New, using an http.Transport that disables
* SSL certificate verification.
*/
func NewInsecure(registryUrl, username, password, proxy string, trace httptrace.HTTPTrace) (*Registry, uint, error) {
// same as http.DefaultTransport
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
if proxy != "" {
pxyUrl, err := url.Parse(proxy)
if err != nil {
return nil, ErrorUrl, err
}
transport.Proxy = http.ProxyURL(pxyUrl)
}
r := newFromTransport(registryUrl, username, password, transport, trace)
return r, ErrorNone, nil
}
/*
* Given an existing http.RoundTripper such as http.DefaultTransport, build the
* transport stack necessary to authenticate to the Docker registry API. This
* adds in support for OAuth bearer tokens and HTTP Basic auth, and sets up
* error handling this library relies on.
*/
func WrapTransport(transport http.RoundTripper, url, username, password string) http.RoundTripper {
tokenTransport := &TokenTransport{
Transport: transport,
Username: username,
Password: password,
}
basicAuthTransport := &BasicTransport{
Transport: tokenTransport,
URL: url,
Username: username,
Password: password,
}
errorTransport := &ErrorTransport{
Transport: basicAuthTransport,
}
return errorTransport
}
// Let client handle POST redirect
func redirectPolicyFunc(req *http.Request, via []*http.Request) error {
for _, v := range via {
if req.Method == http.MethodGet && v.Method == http.MethodPost {
return http.ErrUseLastResponse
}
}
return nil
}
func newFromTransport(registryUrl, username, password string, transport http.RoundTripper, trace httptrace.HTTPTrace) *Registry {
url := strings.TrimSuffix(registryUrl, "/")
transport = WrapTransport(transport, url, username, password)
return &Registry{
URL: url,
Client: &httptrace.TraceClient{
Trace: trace,
Client: &http.Client{
Timeout: nonDataTimeout,
Transport: transport,
CheckRedirect: redirectPolicyFunc,
},
},
}
}
func (r *Registry) url(pathTemplate string, args ...interface{}) string {
pathSuffix := fmt.Sprintf(pathTemplate, args...)
url := fmt.Sprintf("%s%s", r.URL, pathSuffix)
return url
}
func (r *Registry) Ping() (uint, error) {
url := r.url("/v2/")
resp, err := r.Client.Get(url)
if resp != nil {
resp.Body.Close()
}
if err == nil {
return ErrorNone, nil
} else if strings.Contains(err.Error(), "x509:") {
// if it's x509 certificate error, return failed. so we will start insecure connection
log.WithFields(log.Fields{"error": err}).Error()
return ErrorCertificate, err
} else if strings.Contains(err.Error(), "UNAUTHORIZED") {
log.WithFields(log.Fields{"error": err}).Error()
return ErrorAuthentication, err
}
return ErrorUnknown, err
}