Skip to content

Modern Docker Management, Designed for Everyone

fix: use correct response messages for container registry test (#665)

Kyle Mendella3dfb309c39f
diff --git a/backend/internal/api/container_registry_handler.go b/backend/internal/api/container_registry_handler.go
index 7fb78150b..19f04bf0a 100644
--- a/backend/internal/api/container_registry_handler.go
+++ b/backend/internal/api/container_registry_handler.go
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
- "time"
"github.com/gin-gonic/gin"
"github.com/ofkm/arcane-backend/internal/dto"
@@ -200,16 +199,15 @@ func (h *ContainerRegistryHandler) TestRegistry(c *gin.Context) {
testResult, err := h.performRegistryTest(c.Request.Context(), registry, decryptedToken)
if err != nil {
- apiErr := models.NewInternalServerError(fmt.Sprintf("Registry test failed: %s", err.Error()))
- c.JSON(apiErr.HTTPStatus(), gin.H{
- "success": false,
- "data": gin.H{"error": apiErr.Message},
+ c.JSON(http.StatusOK, gin.H{
+ "success": true,
+ "data": gin.H{"message": err.Error()},
})
return
}
c.JSON(http.StatusOK, gin.H{
- "success": true,
+ "success": false,
"data": testResult,
})
}
@@ -225,51 +223,17 @@ func (h *ContainerRegistryHandler) performRegistryTest(ctx context.Context, regi
testResult, err := registry.TestRegistryConnection(ctx, registryModel.URL, creds)
if err != nil {
- return nil, fmt.Errorf("registry test failed: %w", err)
- }
-
- result := map[string]interface{}{
- "status": getStatusString(testResult.OverallSuccess),
- "url": registryModel.URL,
- "username": registryModel.Username,
- "timestamp": testResult.Timestamp.Format(time.RFC3339),
- "overall_success": testResult.OverallSuccess,
- "ping_success": testResult.PingSuccess,
- "auth_success": testResult.AuthSuccess,
- "catalog_success": testResult.CatalogSuccess,
- "registry_url": testResult.URL,
- "domain": testResult.Domain,
+ return nil, err
}
- if len(testResult.Errors) > 0 {
- result["errors"] = testResult.Errors
- result["message"] = fmt.Sprintf("Registry test completed with %d error(s)", len(testResult.Errors))
- } else {
- result["message"] = "All registry tests passed successfully"
- }
-
- result["tests"] = map[string]interface{}{
- "connectivity": map[string]interface{}{
- "success": testResult.PingSuccess,
- "description": "Tests if the registry endpoint is reachable",
- },
- "authentication": map[string]interface{}{
- "success": testResult.AuthSuccess,
- "description": "Tests if the provided credentials are valid",
- "skipped": creds == nil,
- },
- "catalog_access": map[string]interface{}{
- "success": testResult.CatalogSuccess,
- "description": "Tests if the registry catalog is accessible",
- },
+ if !testResult.AuthSuccess {
+ if len(testResult.Errors) > 0 {
+ return nil, fmt.Errorf("%s", testResult.Errors[0])
+ }
+ return nil, fmt.Errorf("invalid credentials")
}
- return result, nil
-}
-
-func getStatusString(success bool) string {
- if success {
- return "success"
- }
- return "failed"
+ return map[string]interface{}{
+ "message": "Authentication succeeded",
+ }, nil
}
diff --git a/backend/internal/utils/registry/test_connection.go b/backend/internal/utils/registry/test_connection.go
index bbbc9ebc6..5ebd49c9d 100644
--- a/backend/internal/utils/registry/test_connection.go
+++ b/backend/internal/utils/registry/test_connection.go
@@ -5,8 +5,11 @@ import (
"encoding/base64"
"fmt"
"net/http"
+ "net/url"
"strings"
"time"
+
+ dockerregistry "github.com/docker/docker/registry"
)
type TestResult struct {
@@ -34,12 +37,43 @@ func TestRegistryConnection(ctx context.Context, registryURL string, creds *Cred
reg = DefaultRegistry
}
- authURL, err := c.CheckAuth(ctx, reg)
+ // Use official docker/registry client to ping the registry
+ registryEndpoint := c.GetRegistryURL(reg)
+ endpointURL, err := url.Parse(registryEndpoint)
if err != nil {
- res.Errors = append(res.Errors, fmt.Sprintf("Ping failed: %v", err))
+ res.Errors = append(res.Errors, fmt.Sprintf("Invalid registry URL: %v", err))
res.PingSuccess = false
- } else {
- res.PingSuccess = true
+ res.OverallSuccess = false
+ return res, nil
+ }
+
+ challengeManager, err := dockerregistry.PingV2Registry(endpointURL, c.http.Transport)
+ if err != nil {
+ res.Errors = append(res.Errors, fmt.Sprintf("Connectivity test failed: %v", err))
+ res.PingSuccess = false
+ res.AuthSuccess = false
+ res.CatalogSuccess = false
+ res.OverallSuccess = false
+ return res, nil
+ }
+ res.PingSuccess = true
+
+ // 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)
+ }
+ }
+ }
+ }
+ }
}
var authHeader string