Skip to content

Modern Docker Management, Designed for Everyone

chore(linter): fix internal reponse of challenge manager

Kyle Mendell3d7187dda8b5
diff --git a/backend/go.mod b/backend/go.mod
index dbab581f8..2bb39dda5 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -9,6 +9,7 @@ require (
github.com/distribution/reference v0.6.0
github.com/docker/cli v28.5.0+incompatible
github.com/docker/compose/v2 v2.40.0
+ github.com/docker/distribution v2.8.3+incompatible
github.com/docker/docker v28.5.0+incompatible
github.com/docker/go-connections v0.6.0
github.com/fsnotify/fsnotify v1.9.0
@@ -77,7 +78,6 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/buildx v0.28.0 // indirect
github.com/docker/cli-docs-tool v0.10.0 // indirect
- github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker-credential-helpers v0.9.3 // indirect
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c // indirect
github.com/docker/go-metrics v0.0.1 // indirect
diff --git a/backend/internal/utils/registry/test_connection.go b/backend/internal/utils/registry/test_connection.go
index 5ebd49c9d..5764798db 100644
--- a/backend/internal/utils/registry/test_connection.go
+++ b/backend/internal/utils/registry/test_connection.go
@@ -9,6 +9,7 @@ import (
"strings"
"time"
+ "github.com/docker/distribution/registry/client/auth/challenge"
dockerregistry "github.com/docker/docker/registry"
)
@@ -32,21 +33,46 @@ func TestRegistryConnection(ctx context.Context, registryURL string, creds *Cred
Errors: []string{},
}
+ reg := normalizeRegistry(registryURL)
+ endpointURL, err := parseRegistryEndpoint(c, reg, res)
+ if err != nil {
+ return res, err
+ }
+
+ challengeManager, err := pingRegistry(endpointURL, c, res)
+ if err != nil {
+ return res, err
+ }
+
+ authURL := extractAuthURL(challengeManager, endpointURL)
+ authHeader := performAuth(ctx, c, reg, authURL, creds, res)
+ testCatalog(ctx, c, reg, authHeader, res)
+
+ res.OverallSuccess = res.PingSuccess && res.AuthSuccess && res.CatalogSuccess
+ return res, nil
+}
+
+func normalizeRegistry(registryURL string) string {
reg := trimScheme(registryURL)
if reg == "docker.io" {
- reg = DefaultRegistry
+ return DefaultRegistry
}
+ return reg
+}
- // Use official docker/registry client to ping the registry
+func parseRegistryEndpoint(c *Client, reg string, res *TestResult) (*url.URL, error) {
registryEndpoint := c.GetRegistryURL(reg)
endpointURL, err := url.Parse(registryEndpoint)
if err != nil {
res.Errors = append(res.Errors, fmt.Sprintf("Invalid registry URL: %v", err))
res.PingSuccess = false
res.OverallSuccess = false
- return res, nil
+ return nil, err
}
+ return endpointURL, nil
+}
+func pingRegistry(endpointURL *url.URL, c *Client, res *TestResult) (challenge.Manager, error) {
challengeManager, err := dockerregistry.PingV2Registry(endpointURL, c.http.Transport)
if err != nil {
res.Errors = append(res.Errors, fmt.Sprintf("Connectivity test failed: %v", err))
@@ -54,93 +80,120 @@ func TestRegistryConnection(ctx context.Context, registryURL string, creds *Cred
res.AuthSuccess = false
res.CatalogSuccess = false
res.OverallSuccess = false
- return res, nil
+ return nil, err
}
res.PingSuccess = true
+ return challengeManager, nil
+}
- // Extract auth URL from challenge manager if available
- var authURL string
- if challengeManager != nil {
- challenges, err := challengeManager.GetChallenges(*endpointURL)
- if err == nil {
- for _, challenge := range challenges {
- if challenge.Scheme == "bearer" {
- if realm, ok := challenge.Parameters["realm"]; ok {
- authURL = realm
- if service, ok := challenge.Parameters["service"]; ok {
- authURL = fmt.Sprintf("%s?service=%s", realm, service)
- }
- }
- }
- }
- }
+func extractAuthURL(challengeManager challenge.Manager, endpointURL *url.URL) string {
+ if challengeManager == nil {
+ return ""
+ }
+
+ challenges, err := challengeManager.GetChallenges(*endpointURL)
+ if err != nil {
+ return ""
}
- var authHeader string
-
- if authURL != "" && creds != nil {
- if tok, err := c.GetTokenMulti(ctx, authURL, []string{}, creds); err == nil && tok != "" {
- res.AuthSuccess = true
- authHeader = "Bearer " + tok
- } else {
- pingURL := c.GetRegistryURL(reg) + "/v2/"
- ctx2, cancel := context.WithTimeout(ctx, 10*time.Second)
- defer cancel()
- req, rerr := http.NewRequestWithContext(ctx2, http.MethodGet, pingURL, nil)
- if rerr == nil {
- req.SetBasicAuth(creds.Username, creds.Token)
- resp, derr := c.http.Do(req)
- if derr == nil {
- defer resp.Body.Close()
- if resp.StatusCode == http.StatusOK {
- res.AuthSuccess = true
- ba := []byte(creds.Username + ":" + creds.Token)
- authHeader = "Basic " + base64.StdEncoding.EncodeToString(ba)
- } else {
- res.Errors = append(res.Errors, fmt.Sprintf("Auth failed: status %d", resp.StatusCode))
- res.AuthSuccess = false
- }
- } else {
- res.Errors = append(res.Errors, fmt.Sprintf("Auth request failed: %v", derr))
- res.AuthSuccess = false
+ for _, challenge := range challenges {
+ if challenge.Scheme == "bearer" {
+ if realm, ok := challenge.Parameters["realm"]; ok {
+ authURL := realm
+ if service, ok := challenge.Parameters["service"]; ok {
+ authURL = fmt.Sprintf("%s?service=%s", realm, service)
}
- } else {
- res.Errors = append(res.Errors, fmt.Sprintf("Auth request creation failed: %v", rerr))
- res.AuthSuccess = false
+ return authURL
}
}
- } else {
+ }
+ return ""
+}
+
+func performAuth(ctx context.Context, c *Client, reg, authURL string, creds *Credentials, res *TestResult) string {
+ if authURL == "" || creds == nil {
res.AuthSuccess = (authURL == "")
+ return ""
+ }
+
+ if authHeader := tryBearerAuth(ctx, c, authURL, creds, res); authHeader != "" {
+ return authHeader
+ }
+
+ return tryBasicAuth(ctx, c, reg, creds, res)
+}
+
+func tryBearerAuth(ctx context.Context, c *Client, authURL string, creds *Credentials, res *TestResult) string {
+ tok, err := c.GetTokenMulti(ctx, authURL, []string{}, creds)
+ if err == nil && tok != "" {
+ res.AuthSuccess = true
+ return "Bearer " + tok
+ }
+ return ""
+}
+
+func tryBasicAuth(ctx context.Context, c *Client, reg string, creds *Credentials, res *TestResult) string {
+ pingURL := c.GetRegistryURL(reg) + "/v2/"
+ ctx2, cancel := context.WithTimeout(ctx, 10*time.Second)
+ defer cancel()
+
+ req, err := http.NewRequestWithContext(ctx2, http.MethodGet, pingURL, nil)
+ if err != nil {
+ res.Errors = append(res.Errors, fmt.Sprintf("Auth request creation failed: %v", err))
+ res.AuthSuccess = false
+ return ""
}
+ req.SetBasicAuth(creds.Username, creds.Token)
+ resp, err := c.http.Do(req)
+ if err != nil {
+ res.Errors = append(res.Errors, fmt.Sprintf("Auth request failed: %v", err))
+ res.AuthSuccess = false
+ return ""
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode == http.StatusOK {
+ res.AuthSuccess = true
+ ba := []byte(creds.Username + ":" + creds.Token)
+ return "Basic " + base64.StdEncoding.EncodeToString(ba)
+ }
+
+ res.Errors = append(res.Errors, fmt.Sprintf("Auth failed: status %d", resp.StatusCode))
+ res.AuthSuccess = false
+ return ""
+}
+
+func testCatalog(ctx context.Context, c *Client, reg, authHeader string, res *TestResult) {
catalogURL := c.GetRegistryURL(reg) + "/v2/_catalog"
- ctx3, cancel3 := context.WithTimeout(ctx, 15*time.Second)
- defer cancel3()
+ ctx3, cancel := context.WithTimeout(ctx, 15*time.Second)
+ defer cancel()
+
req, err := http.NewRequestWithContext(ctx3, http.MethodGet, catalogURL, nil)
if err != nil {
res.Errors = append(res.Errors, fmt.Sprintf("Catalog request creation failed: %v", err))
res.CatalogSuccess = false
- } else {
- if authHeader != "" {
- req.Header.Set("Authorization", authHeader)
- }
- resp, derr := c.http.Do(req)
- if derr != nil {
- res.Errors = append(res.Errors, fmt.Sprintf("Catalog request failed: %v", derr))
- res.CatalogSuccess = false
- } else {
- defer resp.Body.Close()
- if resp.StatusCode == http.StatusOK {
- res.CatalogSuccess = true
- } else {
- res.CatalogSuccess = false
- res.Errors = append(res.Errors, fmt.Sprintf("Catalog returned status: %d", resp.StatusCode))
- }
- }
+ return
}
- res.OverallSuccess = res.PingSuccess && res.AuthSuccess && res.CatalogSuccess
- return res, nil
+ if authHeader != "" {
+ req.Header.Set("Authorization", authHeader)
+ }
+
+ resp, err := c.http.Do(req)
+ if err != nil {
+ res.Errors = append(res.Errors, fmt.Sprintf("Catalog request failed: %v", err))
+ res.CatalogSuccess = false
+ return
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode == http.StatusOK {
+ res.CatalogSuccess = true
+ } else {
+ res.CatalogSuccess = false
+ res.Errors = append(res.Errors, fmt.Sprintf("Catalog returned status: %d", resp.StatusCode))
+ }
}
func trimScheme(u string) string {