Skip to content

Commit 8039543

Browse files
committed
fix: Fix registry authentication when URL has slash suffix (#331)
* fix: Fix registry authentication when URL has slash suffix Signed-off-by: jannfis <[email protected]> * Fix format string Signed-off-by: jannfis <[email protected]>
1 parent 1ca634e commit 8039543

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

pkg/registry/client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,16 @@ func (client *registryClient) TagMetadata(manifest distribution.Manifest) (*tag.
261261
// Without this, tokenHandler and AuthorizationHandler won't work
262262
func ping(manager challenge.Manager, endpoint *RegistryEndpoint, versionHeader string) ([]auth.APIVersion, error) {
263263
httpc := &http.Client{Transport: endpoint.GetTransport()}
264-
resp, err := httpc.Get(endpoint.RegistryAPI + "/v2/")
264+
url := endpoint.RegistryAPI + "/v2/"
265+
resp, err := httpc.Get(url)
265266
if err != nil {
266267
return nil, err
267268
}
268269
defer resp.Body.Close()
270+
// Let's consider only HTTP 200 and 401 valid responses for the inital request
271+
if resp.StatusCode != 200 && resp.StatusCode != 401 {
272+
return nil, fmt.Errorf("endpoint %s does not seem to be a valid v2 Docker Registry API (received HTTP code %d for GET %s)", endpoint.RegistryAPI, resp.StatusCode, url)
273+
}
269274

270275
if err := manager.AddResponse(resp); err != nil {
271276
return nil, err

pkg/registry/endpoints.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,10 @@ func AddRegistryEndpoint(prefix, name, apiUrl, credentials, defaultNS string, in
135135
if limit <= 0 {
136136
limit = RateLimitNone
137137
}
138-
log.Debugf("rate limit for %s is %d", apiUrl, limit)
139138
registries[prefix] = &RegistryEndpoint{
140139
RegistryName: name,
141140
RegistryPrefix: prefix,
142-
RegistryAPI: apiUrl,
141+
RegistryAPI: strings.TrimSuffix(apiUrl, "/"),
143142
Credentials: credentials,
144143
CredsExpire: credsExpire,
145144
Cache: cache.NewMemCache(),
@@ -148,6 +147,15 @@ func AddRegistryEndpoint(prefix, name, apiUrl, credentials, defaultNS string, in
148147
TagListSort: tagListSort,
149148
Limiter: ratelimit.New(limit),
150149
}
150+
151+
logCtx := log.WithContext()
152+
logCtx.AddField("registry", registries[prefix].RegistryAPI)
153+
logCtx.AddField("prefix", registries[prefix].RegistryPrefix)
154+
if limit != RateLimitNone {
155+
logCtx.Debugf("setting rate limit to %d requests per second", limit)
156+
} else {
157+
logCtx.Debugf("rate limiting is disabled")
158+
}
151159
return nil
152160
}
153161

0 commit comments

Comments
 (0)