Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
feat: show pull usage and limits (if applicable) (#2458)
Kyle Mendell07683530fb2d
diff --git a/backend/internal/bootstrap/services_bootstrap.go b/backend/internal/bootstrap/services_bootstrap.goindex ff5ec808a..64c3fc581 100644--- a/backend/internal/bootstrap/services_bootstrap.go+++ b/backend/internal/bootstrap/services_bootstrap.go@@ -71,7 +71,7 @@ func initializeServices(ctx context.Context, db *database.DB, cfg *config.Configsvcs.ApiKey = services.NewApiKeyService(db, svcs.User)svcs.ContainerRegistry = services.NewContainerRegistryService(db, func(ctx context.Context) (services.RegistryDaemonClient, error) {return dockerClient.GetClient(ctx)- })+ }, svcs.KV)svcs.Environment = services.NewEnvironmentService(db, httpClient, svcs.Docker, svcs.Event, svcs.Settings, svcs.ApiKey)svcs.Version = services.NewVersionService(httpClient, cfg.UpdateCheckDisabled, config.Version, config.Revision, svcs.ContainerRegistry, svcs.Docker)svcs.Notification = services.NewNotificationService(db, cfg, svcs.Environment)diff --git a/backend/internal/huma/handlers/container_registries.go b/backend/internal/huma/handlers/container_registries.goindex d48ea2cce..66d0a4b32 100644--- a/backend/internal/huma/handlers/container_registries.go+++ b/backend/internal/huma/handlers/container_registries.go@@ -85,6 +85,10 @@ type TestContainerRegistryOutput struct {Body base.ApiResponse[base.MessageResponse]}+type GetContainerRegistryPullUsageOutput struct {+ Body base.ApiResponse[containerregistry.PullUsageResponse]+}+type SyncContainerRegistriesInput struct {Body containerregistry.SyncRequest}@@ -143,6 +147,19 @@ func RegisterContainerRegistries(api huma.API, registryService *services.Contain},}, h.SyncRegistries)+ huma.Register(api, huma.Operation{+ OperationID: "getContainerRegistryPullUsage",+ Method: "GET",+ Path: "/container-registries/pull-usage",+ Summary: "Get container registry pull usage",+ Description: "Get configured registry pull usage and rate limit visibility",+ Tags: []string{"Container Registries"},+ Security: []map[string][]string{+ {"BearerAuth": {}},+ {"ApiKeyAuth": {}},+ },+ }, h.GetPullUsage)+huma.Register(api, huma.Operation{OperationID: "getContainerRegistry",Method: "GET",@@ -228,6 +245,25 @@ func (h *ContainerRegistryHandler) ListRegistries(ctx context.Context, input *Li}, nil}+// GetPullUsage returns pull usage visibility for configured registries.+func (h *ContainerRegistryHandler) GetPullUsage(ctx context.Context, input *struct{}) (*GetContainerRegistryPullUsageOutput, error) {+ if h.registryService == nil {+ return nil, huma.Error500InternalServerError("service not available")+ }++ usage, err := h.registryService.GetRegistryPullUsage(ctx)+ if err != nil {+ return nil, huma.Error500InternalServerError((&common.RegistryRetrievalError{Err: err}).Error())+ }++ return &GetContainerRegistryPullUsageOutput{+ Body: base.ApiResponse[containerregistry.PullUsageResponse]{+ Success: true,+ Data: usage,+ },+ }, nil+}+// CreateRegistry creates a new container registry.func (h *ContainerRegistryHandler) CreateRegistry(ctx context.Context, input *CreateContainerRegistryInput) (*CreateContainerRegistryOutput, error) {if h.registryService == nil {diff --git a/backend/internal/services/build_service_test.go b/backend/internal/services/build_service_test.goindex 9e54a7411..4201ecc53 100644--- a/backend/internal/services/build_service_test.go+++ b/backend/internal/services/build_service_test.go@@ -22,7 +22,7 @@ func TestBuildService_GetRegistryAuthForHost_UsesDatabaseCredentials(t *testing._, db := setupImageServiceAuthTest(t)createTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")- svc := &BuildService{registryService: NewContainerRegistryService(db, nil)}+ svc := &BuildService{registryService: NewContainerRegistryService(db, nil, nil)}auth, err := svc.GetRegistryAuthForHost(context.Background(), "registry-1.docker.io")require.NoError(t, err)@@ -38,7 +38,7 @@ func TestBuildService_GetRegistryAuthForImage_UsesHostLookup(t *testing.T) {_, db := setupImageServiceAuthTest(t)createTestPullRegistry(t, db, "https://ghcr.io", "gh-user", "gh-token")- svc := &BuildService{registryService: NewContainerRegistryService(db, nil)}+ svc := &BuildService{registryService: NewContainerRegistryService(db, nil, nil)}auth, err := svc.GetRegistryAuthForImage(context.Background(), "ghcr.io/getarcaneapp/arcane:latest")require.NoError(t, err)@@ -54,7 +54,7 @@ func TestBuildService_GetAllRegistryAuthConfigs_UsesDatabaseCredentials(t *testi_, db := setupImageServiceAuthTest(t)createTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")- svc := &BuildService{registryService: NewContainerRegistryService(db, nil)}+ svc := &BuildService{registryService: NewContainerRegistryService(db, nil, nil)}authConfigs, err := svc.GetAllRegistryAuthConfigs(context.Background())require.NoError(t, err)diff --git a/backend/internal/services/container_registry_service.go b/backend/internal/services/container_registry_service.goindex 26f0bcb56..b5d691ed4 100644--- a/backend/internal/services/container_registry_service.go+++ b/backend/internal/services/container_registry_service.go@@ -2,6 +2,7 @@ package servicesimport ("context"+ "encoding/json""errors""fmt""log/slog"@@ -29,9 +30,11 @@ import ()const (- registryCacheTTL = 30 * time.Minute- registryTypeGeneric string = "generic"- registryTypeECR string = "ecr"+ registryCacheTTL = 30 * time.Minute+ registryTypeGeneric string = "generic"+ registryTypeECR string = "ecr"+ registryPullCountKeyPrefix = "container_registry:pulls:"+ registryRateLimitKeyPrefix = "container_registry:rate_limits:")type RegistryDaemonClient interface {@@ -55,6 +58,11 @@ type resolvedRegistryCredential struct {ServerAddress string}+type registryRateLimitCacheEntryInternal struct {+ RateLimit utilsdistribution.RateLimitInfo `json:"rateLimit"`+ CheckedAt time.Time `json:"checkedAt"`+}+type ContainerRegistryService struct {db *database.DBdockerClient registryDaemonGetter@@ -62,14 +70,18 @@ type ContainerRegistryService struct {cacheMu sync.RWMutexecrRefreshGroup singleflight.GroupdistributionHTTPClient *http.Client+ kvService *KVService}-func NewContainerRegistryService(db *database.DB, dockerClient registryDaemonGetter) *ContainerRegistryService {+// NewContainerRegistryService creates a registry service. kvService may be nil+// in tests that do not need pull tracking or rate-limit caching.+func NewContainerRegistryService(db *database.DB, dockerClient registryDaemonGetter, kvService *KVService) *ContainerRegistryService {return &ContainerRegistryService{db: db,dockerClient: dockerClient,distributionHTTPClient: utilsdistribution.NewRegistryHTTPClient(),cache: make(map[string]*cache.Cache[string]),+ kvService: kvService,}}@@ -402,6 +414,232 @@ func (s *ContainerRegistryService) GetAllRegistryAuthConfigs(ctx context.Contextreturn authConfigs, nil}+// RecordImagePull increments Arcane's observed successful pull counter for an image registry.+func (s *ContainerRegistryService) RecordImagePull(ctx context.Context, imageRef string) error {+ if s.kvService == nil {+ return nil+ }++ registryHost, err := normalizePullRegistryHostInternal(imageRef)+ if err != nil {+ return err+ }+ if registryHost == "" {+ return nil+ }++ if _, err := s.kvService.IncrementInt64(ctx, registryPullCountKeyInternal(registryHost), 1); err != nil {+ return err+ }++ return nil+}++// GetRegistryPullUsage returns pull usage visibility for configured registries.+func (s *ContainerRegistryService) GetRegistryPullUsage(ctx context.Context) (containerregistry.PullUsageResponse, error) {+ registries, err := s.GetAllRegistries(ctx)+ if err != nil {+ return containerregistry.PullUsageResponse{}, err+ }++ results := make([]containerregistry.PullUsage, 0, len(registries))+ for i := range registries {+ results = append(results, s.buildRegistryPullUsageInternal(ctx, registries[i]))+ }++ return containerregistry.PullUsageResponse{Registries: results}, nil+}++func (s *ContainerRegistryService) buildRegistryPullUsageInternal(ctx context.Context, reg models.ContainerRegistry) containerregistry.PullUsage {+ registryHost := utilsregistry.NormalizeRegistryForComparison(reg.URL)+ usage := containerregistry.PullUsage{+ RegistryID: reg.ID,+ Provider: registryProviderInternal(registryHost, reg.RegistryType),+ Registry: registryHost,+ DisplayName: registryDisplayNameInternal(registryHost, reg.RegistryType),+ ObservedPulls: s.getObservedPullsInternal(ctx, registryHost),+ AuthMethod: "unknown",+ CheckedAt: time.Now().UTC(),+ }++ if registryHost != "docker.io" || !reg.Enabled {+ return usage+ }++ credential, authMethod, authUsername, err := s.dockerHubCredentialForRegistryInternal(reg)+ usage.AuthMethod = authMethod+ usage.AuthUsername = authUsername+ usage.Repository = "ratelimitpreview/test"+ if err != nil {+ usage.Error = err.Error()+ return usage+ }++ if cachedRateLimit, checkedAt, ok := s.getCachedRateLimitInternal(ctx, reg.ID); ok {+ usage.Limit = cachedRateLimit.Limit+ usage.Remaining = cachedRateLimit.Remaining+ usage.Used = cachedRateLimit.Used+ usage.WindowSeconds = cachedRateLimit.WindowSeconds+ usage.Source = cachedRateLimit.Source+ usage.CheckedAt = checkedAt+ return usage+ }++ rateLimit, err := s.fetchDockerHubRateLimitInternal(ctx, credential)+ usage.CheckedAt = time.Now().UTC()+ if err != nil {+ usage.Error = err.Error()+ return usage+ }++ usage.Limit = rateLimit.Limit+ usage.Remaining = rateLimit.Remaining+ usage.Used = rateLimit.Used+ usage.WindowSeconds = rateLimit.WindowSeconds+ usage.Source = rateLimit.Source+ s.setCachedRateLimitInternal(ctx, reg.ID, rateLimit, usage.CheckedAt)++ return usage+}++func (s *ContainerRegistryService) getObservedPullsInternal(ctx context.Context, registryHost string) int64 {+ if s.kvService == nil || registryHost == "" {+ return 0+ }++ value, err := s.kvService.GetInt64(ctx, registryPullCountKeyInternal(registryHost), 0)+ if err != nil {+ slog.WarnContext(ctx, "failed to read registry pull count", "registry", registryHost, "error", err)+ return 0+ }++ return value+}++func (s *ContainerRegistryService) dockerHubCredentialForRegistryInternal(reg models.ContainerRegistry) (*utilsdistribution.Credentials, string, string, error) {+ if reg.RegistryType != registryTypeGeneric {+ return nil, "anonymous", "", nil+ }++ username := strings.TrimSpace(reg.Username)+ if username == "" || strings.TrimSpace(reg.Token) == "" {+ return nil, "anonymous", "", nil+ }++ token, err := crypto.Decrypt(reg.Token)+ if err != nil {+ return nil, "credential", username, fmt.Errorf("failed to decrypt Docker Hub credential: %w", err)+ }++ token = strings.TrimSpace(token)+ if token == "" {+ return nil, "anonymous", "", nil+ }++ return &utilsdistribution.Credentials{+ Username: username,+ Token: token,+ }, "credential", username, nil+}++func (s *ContainerRegistryService) fetchDockerHubRateLimitInternal(ctx context.Context, credential *utilsdistribution.Credentials) (*utilsdistribution.RateLimitInfo, error) {+ if s.distributionHTTPClient == nil {+ return utilsdistribution.FetchDockerHubRateLimit(ctx, credential)+ }++ return utilsdistribution.FetchDockerHubRateLimitWithHTTPClient(ctx, credential, s.distributionHTTPClient)+}++func (s *ContainerRegistryService) getCachedRateLimitInternal(ctx context.Context, registryID string) (*utilsdistribution.RateLimitInfo, time.Time, bool) {+ if s.kvService == nil || registryID == "" {+ return nil, time.Time{}, false+ }++ raw, ok, err := s.kvService.Get(ctx, registryRateLimitKeyInternal(registryID))+ if err != nil {+ slog.WarnContext(ctx, "failed to read registry rate limit cache", "registryID", registryID, "error", err)+ return nil, time.Time{}, false+ }+ if !ok {+ return nil, time.Time{}, false+ }++ var entry registryRateLimitCacheEntryInternal+ if err := json.Unmarshal([]byte(raw), &entry); err != nil {+ slog.WarnContext(ctx, "failed to parse registry rate limit cache", "registryID", registryID, "error", err)+ return nil, time.Time{}, false+ }+ if time.Since(entry.CheckedAt) > registryCacheTTL {+ return nil, time.Time{}, false+ }++ return &entry.RateLimit, entry.CheckedAt, true+}++func (s *ContainerRegistryService) setCachedRateLimitInternal(ctx context.Context, registryID string, rateLimit *utilsdistribution.RateLimitInfo, checkedAt time.Time) {+ if s.kvService == nil || registryID == "" || rateLimit == nil {+ return+ }++ payload, err := json.Marshal(registryRateLimitCacheEntryInternal{+ RateLimit: *rateLimit,+ CheckedAt: checkedAt,+ })+ if err != nil {+ slog.WarnContext(ctx, "failed to encode registry rate limit cache", "registryID", registryID, "error", err)+ return+ }++ if err := s.kvService.Set(ctx, registryRateLimitKeyInternal(registryID), string(payload)); err != nil {+ slog.WarnContext(ctx, "failed to save registry rate limit cache", "registryID", registryID, "error", err)+ }+}++func normalizePullRegistryHostInternal(imageRef string) (string, error) {+ registryHost, err := utilsregistry.GetRegistryAddress(imageRef)+ if err != nil {+ return "", fmt.Errorf("parse image registry for %q: %w", imageRef, err)+ }++ return utilsregistry.NormalizeRegistryForComparison(registryHost), nil+}++func registryPullCountKeyInternal(registryHost string) string {+ return registryPullCountKeyPrefix + utilsregistry.NormalizeRegistryForComparison(registryHost)+}++func registryRateLimitKeyInternal(registryID string) string {+ return registryRateLimitKeyPrefix + strings.TrimSpace(registryID)+}++func registryProviderInternal(registryHost, registryType string) string {+ if registryType == registryTypeECR {+ return "ecr"+ }+ if registryHost == "docker.io" {+ return "dockerhub"+ }+ return "generic"+}++func registryDisplayNameInternal(registryHost, registryType string) string {+ if registryType == registryTypeECR {+ return "Amazon ECR"+ }+ switch {+ case registryHost == "" || registryHost == "docker.io":+ return "Docker Hub"+ case strings.Contains(registryHost, "ghcr.io"):+ return "GitHub Container Registry"+ case strings.Contains(registryHost, "gcr.io"):+ return "Google Container Registry"+ case strings.Contains(registryHost, "quay.io"):+ return "Quay.io Registry"+ default:+ return registryHost+ }+}+func (s *ContainerRegistryService) TestRegistry(ctx context.Context, registryURL, username, token string) error {if strings.TrimSpace(username) == "" && strings.TrimSpace(token) == "" {// No credentials configured — skip the credential test.diff --git a/backend/internal/services/container_registry_service_test.go b/backend/internal/services/container_registry_service_test.goindex a3339736f..d901dfb43 100644--- a/backend/internal/services/container_registry_service_test.go+++ b/backend/internal/services/container_registry_service_test.go@@ -64,8 +64,33 @@ func newTestDockerClient(t *testing.T, server *httptest.Server) *client.Client {return cli}+func newDockerHubRateLimitTestClient(t *testing.T, handler http.HandlerFunc) *http.Client {+ t.Helper()++ server := httptest.NewTLSServer(handler)+ t.Cleanup(server.Close)++ targetURL, err := url.Parse(server.URL)+ require.NoError(t, err)++ client := server.Client()+ baseTransport := client.Transport+ client.Transport = roundTripFunc(func(req *http.Request) (*http.Response, error) {+ if req.URL.Host == "registry-1.docker.io" || req.URL.Host == "auth.docker.io" {+ rewritten := req.Clone(req.Context())+ rewritten.URL.Scheme = targetURL.Scheme+ rewritten.URL.Host = targetURL.Host+ return baseTransport.RoundTrip(rewritten)+ }++ return baseTransport.RoundTrip(req)+ })++ return client+}+func TestNewContainerRegistryService_InitializesDistributionHTTPClient(t *testing.T) {- svc := NewContainerRegistryService(nil, nil)+ svc := NewContainerRegistryService(nil, nil, nil)require.NotNil(t, svc.distributionHTTPClient)}@@ -74,7 +99,7 @@ func TestContainerRegistryService_GetAllRegistryAuthConfigs_NormalizesHosts(t *tcreateTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")createTestPullRegistry(t, db, "https://GHCR.IO/", "gh-user", "gh-token")- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)authConfigs, err := svc.GetAllRegistryAuthConfigs(context.Background())require.NoError(t, err)require.NotNil(t, authConfigs)@@ -101,7 +126,7 @@ func TestContainerRegistryService_GetAllRegistryAuthConfigs_SkipsInvalidEntries(createTestPullRegistry(t, db, "https://ghcr.io", "gh-user", " ")createTestPullRegistry(t, db, "https://registry.example.com", "example-user", "example-token")- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)authConfigs, err := svc.GetAllRegistryAuthConfigs(context.Background())require.NoError(t, err)require.NotNil(t, authConfigs)@@ -116,9 +141,152 @@ func TestContainerRegistryService_GetAllRegistryAuthConfigs_SkipsInvalidEntries(assert.Equal(t, "registry.example.com", exampleCfg.ServerAddress)}+func TestContainerRegistryService_GetRegistryPullUsage_AnonymousDockerHubLimit(t *testing.T) {+ _, db := setupImageServiceAuthTest(t)+ require.NoError(t, db.WithContext(context.Background()).Create(&models.ContainerRegistry{+ URL: "docker.io",+ Enabled: true,+ RegistryType: registryTypeGeneric,+ }).Error)++ var tokenCalls int++ svc := NewContainerRegistryService(db, nil, NewKVService(db))+ svc.distributionHTTPClient = newDockerHubRateLimitTestClient(t, func(w http.ResponseWriter, r *http.Request) {+ switch {+ case r.URL.Path == "/token":+ tokenCalls+++ assert.Empty(t, r.Header.Get("Authorization"))+ _, _ = w.Write([]byte(`{"token":"anonymous-token"}`))+ case r.Method == http.MethodHead && r.URL.Path == "/v2/ratelimitpreview/test/manifests/latest":+ if r.Header.Get("Authorization") != "Bearer anonymous-token" {+ w.Header().Set("WWW-Authenticate", `Bearer realm="https://auth.docker.io/token",service="registry.docker.io"`)+ w.WriteHeader(http.StatusUnauthorized)+ return+ }+ w.Header().Set("RateLimit-Limit", "100;w=21600")+ w.Header().Set("RateLimit-Remaining", "90;w=21600")+ w.WriteHeader(http.StatusOK)+ default:+ t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)+ }+ })++ result, err := svc.GetRegistryPullUsage(context.Background())+ require.NoError(t, err)++ require.Len(t, result.Registries, 1)+ registry := result.Registries[0]+ assert.Equal(t, "dockerhub", registry.Provider)+ assert.Equal(t, "anonymous", registry.AuthMethod)+ assert.Empty(t, registry.Error)+ require.NotNil(t, registry.Limit)+ require.NotNil(t, registry.Remaining)+ require.NotNil(t, registry.Used)+ assert.Equal(t, 100, *registry.Limit)+ assert.Equal(t, 90, *registry.Remaining)+ assert.Equal(t, 10, *registry.Used)+ assert.Equal(t, 1, tokenCalls)++ cachedResult, err := svc.GetRegistryPullUsage(context.Background())+ require.NoError(t, err)+ require.Len(t, cachedResult.Registries, 1)+ require.NotNil(t, cachedResult.Registries[0].Remaining)+ assert.Equal(t, 90, *cachedResult.Registries[0].Remaining)+ assert.Equal(t, 1, tokenCalls)+}++func TestContainerRegistryService_GetRegistryPullUsage_UsesDockerHubCredential(t *testing.T) {+ _, db := setupImageServiceAuthTest(t)+ createTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")+ createTestPullRegistry(t, db, "https://ghcr.io", "gh-user", "gh-token")++ var tokenAuth string+ svc := NewContainerRegistryService(db, nil, NewKVService(db))+ svc.distributionHTTPClient = newDockerHubRateLimitTestClient(t, func(w http.ResponseWriter, r *http.Request) {+ switch {+ case r.URL.Path == "/token":+ tokenAuth = r.Header.Get("Authorization")+ _, _ = w.Write([]byte(`{"token":"credential-token"}`))+ case r.Method == http.MethodHead && r.URL.Path == "/v2/ratelimitpreview/test/manifests/latest":+ if r.Header.Get("Authorization") != "Bearer credential-token" {+ w.Header().Set("WWW-Authenticate", `Bearer realm="https://auth.docker.io/token",service="registry.docker.io"`)+ w.WriteHeader(http.StatusUnauthorized)+ return+ }+ w.Header().Set("RateLimit-Limit", "200;w=21600")+ w.Header().Set("RateLimit-Remaining", "199;w=21600")+ w.WriteHeader(http.StatusOK)+ default:+ t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)+ }+ })++ result, err := svc.GetRegistryPullUsage(context.Background())+ require.NoError(t, err)++ require.Len(t, result.Registries, 2)+ registry := result.Registries[0]+ assert.Equal(t, "credential", registry.AuthMethod)+ assert.Equal(t, "docker-user", registry.AuthUsername)+ assert.Empty(t, registry.Error)+ require.NotNil(t, registry.Limit)+ require.NotNil(t, registry.Remaining)+ assert.Equal(t, 200, *registry.Limit)+ assert.Equal(t, 199, *registry.Remaining)+ assert.Contains(t, tokenAuth, "Basic ")+}++func TestContainerRegistryService_GetRegistryPullUsage_CredentialErrorIsNonFatal(t *testing.T) {+ _, db := setupImageServiceAuthTest(t)+ createTestPullRegistry(t, db, "https://docker.io", "docker-user", "docker-token")++ svc := NewContainerRegistryService(db, nil, NewKVService(db))+ svc.distributionHTTPClient = newDockerHubRateLimitTestClient(t, func(w http.ResponseWriter, r *http.Request) {+ switch {+ case r.URL.Path == "/token":+ w.WriteHeader(http.StatusUnauthorized)+ case r.Method == http.MethodHead && r.URL.Path == "/v2/ratelimitpreview/test/manifests/latest":+ w.Header().Set("WWW-Authenticate", `Bearer realm="https://auth.docker.io/token",service="registry.docker.io"`)+ w.WriteHeader(http.StatusUnauthorized)+ default:+ t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)+ }+ })++ result, err := svc.GetRegistryPullUsage(context.Background())+ require.NoError(t, err)++ require.Len(t, result.Registries, 1)+ registry := result.Registries[0]+ assert.Equal(t, "credential", registry.AuthMethod)+ assert.Equal(t, "docker-user", registry.AuthUsername)+ assert.Contains(t, registry.Error, "token request failed with status: 401")+}++func TestContainerRegistryService_RecordImagePull_IncrementsObservedRegistryCount(t *testing.T) {+ _, db := setupImageServiceAuthTest(t)+ require.NoError(t, db.WithContext(context.Background()).Create(&models.ContainerRegistry{+ URL: "ghcr.io",+ Enabled: true,+ RegistryType: registryTypeGeneric,+ }).Error)++ svc := NewContainerRegistryService(db, nil, NewKVService(db))+ require.NoError(t, svc.RecordImagePull(context.Background(), "ghcr.io/example/app:latest"))+ require.NoError(t, svc.RecordImagePull(context.Background(), "ghcr.io/example/worker:latest"))++ result, err := svc.GetRegistryPullUsage(context.Background())+ require.NoError(t, err)+ require.Len(t, result.Registries, 1)+ assert.Equal(t, "ghcr.io", result.Registries[0].Registry)+ assert.Equal(t, int64(2), result.Registries[0].ObservedPulls)+ assert.Nil(t, result.Registries[0].Limit)+}+func TestContainerRegistryService_CreateRegistry_RejectsUnsupportedRegistryType(t *testing.T) {_, db := setupImageServiceAuthTest(t)- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)_, err := svc.CreateRegistry(context.Background(), models.CreateContainerRegistryRequest{URL: "registry.example.com",@@ -133,7 +301,7 @@ func TestContainerRegistryService_CreateRegistry_RejectsUnsupportedRegistryType(func TestContainerRegistryService_CreateRegistry_RejectsEmptyUsernameForGeneric(t *testing.T) {_, db := setupImageServiceAuthTest(t)- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)_, err := svc.CreateRegistry(context.Background(), models.CreateContainerRegistryRequest{URL: "https://registry.example.com",@@ -149,7 +317,7 @@ func TestContainerRegistryService_CreateRegistry_RejectsEmptyUsernameForGeneric(func TestContainerRegistryService_CreateRegistry_RejectsEmptyTokenForGeneric(t *testing.T) {_, db := setupImageServiceAuthTest(t)- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)_, err := svc.CreateRegistry(context.Background(), models.CreateContainerRegistryRequest{URL: "https://registry.example.com",@@ -165,7 +333,7 @@ func TestContainerRegistryService_CreateRegistry_RejectsEmptyTokenForGeneric(t *func TestContainerRegistryService_CreateRegistry_AcceptsValidGenericCredentials(t *testing.T) {_, db := setupImageServiceAuthTest(t)- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)reg, err := svc.CreateRegistry(context.Background(), models.CreateContainerRegistryRequest{URL: "https://registry.example.com",@@ -179,7 +347,7 @@ func TestContainerRegistryService_CreateRegistry_AcceptsValidGenericCredentials(func TestContainerRegistryService_UpdateRegistry_RejectsBlankingUsername(t *testing.T) {_, db := setupImageServiceAuthTest(t)- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)reg, err := svc.CreateRegistry(context.Background(), models.CreateContainerRegistryRequest{URL: "https://registry.example.com",@@ -200,7 +368,7 @@ func TestContainerRegistryService_UpdateRegistry_RejectsBlankingUsername(t *testfunc TestContainerRegistryService_UpdateRegistry_KeepsExistingTokenWhenNotProvided(t *testing.T) {_, db := setupImageServiceAuthTest(t)- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)reg, err := svc.CreateRegistry(context.Background(), models.CreateContainerRegistryRequest{URL: "https://registry.example.com",@@ -220,7 +388,7 @@ func TestContainerRegistryService_UpdateRegistry_KeepsExistingTokenWhenNotProvidfunc TestContainerRegistryService_UpdateRegistry_RejectsChangingRegistryType(t *testing.T) {_, db := setupImageServiceAuthTest(t)- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)reg, err := svc.CreateRegistry(context.Background(), models.CreateContainerRegistryRequest{URL: "https://registry.example.com",@@ -241,7 +409,7 @@ func TestContainerRegistryService_UpdateRegistry_RejectsChangingRegistryType(t *func TestContainerRegistryService_UpdateRegistry_AllowsSameRegistryType(t *testing.T) {_, db := setupImageServiceAuthTest(t)- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)reg, err := svc.CreateRegistry(context.Background(), models.CreateContainerRegistryRequest{URL: "https://registry.example.com",@@ -266,7 +434,7 @@ func TestContainerRegistryService_SyncRegistries_ClearsGenericTokenWhenManagerSevar existing models.ContainerRegistryrequire.NoError(t, db.WithContext(context.Background()).First(&existing).Error)- svc := NewContainerRegistryService(db, nil)+ svc := NewContainerRegistryService(db, nil, nil)err := svc.SyncRegistries(context.Background(), []containerregistry.Sync{{ID: existing.ID,@@ -299,7 +467,7 @@ func TestContainerRegistryService_TestRegistry_UsesDockerDaemon(t *testing.T) {return client.RegistryLoginResult{}, nil},}, nil- })+ }, nil)err := svc.TestRegistry(context.Background(), "https://registry.example.com:5443", "user", "token")require.NoError(t, err)@@ -313,7 +481,7 @@ func TestContainerRegistryService_TestRegistry_PropagatesDaemonError(t *testing.return client.RegistryLoginResult{}, expectedErr},}, nil- })+ }, nil)err := svc.TestRegistry(context.Background(), "registry.example.com", "user", "token")require.Error(t, err)@@ -328,7 +496,7 @@ func TestContainerRegistryService_TestRegistry_SkipsLoginForEmptyCredentials(t *return client.RegistryLoginResult{}, nil},}, nil- })+ }, nil)err := svc.TestRegistry(context.Background(), "registry.example.com", "", "")require.NoError(t, err)@@ -354,7 +522,7 @@ func TestContainerRegistryService_InspectImageDigest_AnonymousSuccess(t *testing}, nil},}, nil- })+ }, nil)result, err := svc.inspectImageDigestInternal(context.Background(), "registry.example.com:5443/team/app:1.2.3", nil)require.NoError(t, err)@@ -393,7 +561,7 @@ func TestContainerRegistryService_InspectImageDigest_RetriesWithStoredCredential}, nil},}, nil- })+ }, nil)result, err := svc.inspectImageDigestInternal(context.Background(), "registry-1.docker.io/library/nginx:latest", nil)require.NoError(t, err)@@ -430,7 +598,7 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionNoreturn client.DistributionInspectResult{}, errors.New("Error response from daemon: Not Found")},}, nil- })+ }, nil)svc.distributionHTTPClient = server.Client()result, err := svc.inspectImageDigestInternal(context.Background(), serverURL.Host+"/team/app:1.2.3", nil)@@ -467,7 +635,7 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionForeturn client.DistributionInspectResult{}, errors.New("Error response from daemon: <html><body><h1>403 Forbidden</h1> Request forbidden by administrative rules. </body></html>")},}, nil- })+ }, nil)svc.distributionHTTPClient = server.Client()result, err := svc.inspectImageDigestInternal(context.Background(), serverURL.Host+"/team/app:1.2.3", nil)@@ -533,7 +701,7 @@ func TestContainerRegistryService_InspectImageDigest_RetriesStoredCredentialsAftreturn client.DistributionInspectResult{}, errors.New("Error response from daemon: Not Found")},}, nil- })+ }, nil)svc.distributionHTTPClient = server.Client()result, err := svc.inspectImageDigestInternal(context.Background(), serverURL.Host+"/team/app:1.2.3", nil)@@ -557,7 +725,7 @@ func TestContainerRegistryService_InspectImageDigest_DoesNotFallbackOnTLSFailurereturn client.DistributionInspectResult{}, errors.New("tls: failed to verify certificate: x509: certificate signed by unknown authority")},}, nil- })+ }, nil)result, err := svc.inspectImageDigestInternal(context.Background(), "registry.example.com/team/app:1.2.3", nil)require.Error(t, err)@@ -578,7 +746,7 @@ func TestContainerRegistryService_InspectImageDigest_PreservesDaemonAndFallbackEreturn client.DistributionInspectResult{}, daemonErr},}, nil- })+ }, nil)svc.distributionHTTPClient = &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {return nil, fallbackErr@@ -624,7 +792,7 @@ func TestContainerRegistryService_InspectImageDigest_PreservesAnonymousUnauthorireturn client.DistributionInspectResult{}, errors.New("Error response from daemon: Not Found")},}, nil- })+ }, nil)svc.distributionHTTPClient = server.Client()result, err := svc.inspectImageDigestInternal(context.Background(), serverURL.Host+"/team/app:1.2.3", nil)diff --git a/backend/internal/services/image_service.go b/backend/internal/services/image_service.goindex 69c5d44e7..42f984e97 100644--- a/backend/internal/services/image_service.go+++ b/backend/internal/services/image_service.go@@ -238,6 +238,11 @@ func (s *ImageService) PullImage(ctx context.Context, imageName string, progressif logErr := s.eventService.LogImageEvent(ctx, models.EventTypeImagePull, "", imageName, user.ID, user.Username, "0", metadata); logErr != nil {slog.Warn("could not log image pull action", "err", logErr, "image", imageName)}+ if s.registryService != nil {+ if err := s.registryService.RecordImagePull(ctx, imageName); err != nil {+ slog.WarnContext(ctx, "failed to record registry pull count", "image", imageName, "error", err)+ }+ }return nil}diff --git a/backend/internal/services/image_service_test.go b/backend/internal/services/image_service_test.goindex 5a09e1d0b..ef5ca0c44 100644--- a/backend/internal/services/image_service_test.go+++ b/backend/internal/services/image_service_test.go@@ -107,7 +107,7 @@ func setupImageServiceAuthTest(t *testing.T) (*ImageService, *database.DB) {db, err := gorm.Open(glsqlite.Open(":memory:"), &gorm.Config{})require.NoError(t, err)- require.NoError(t, db.AutoMigrate(&models.ContainerRegistry{}))+ require.NoError(t, db.AutoMigrate(&models.ContainerRegistry{}, &models.KVEntry{}))crypto.InitEncryption(&crypto.Config{Environment: string(config.AppEnvironmentTest),@@ -116,7 +116,7 @@ func setupImageServiceAuthTest(t *testing.T) (*ImageService, *database.DB) {dbWrap := &database.DB{DB: db}svc := &ImageService{- registryService: NewContainerRegistryService(dbWrap, nil),+ registryService: NewContainerRegistryService(dbWrap, nil, NewKVService(dbWrap)),}return svc, dbWrapdiff --git a/backend/internal/services/image_update_service_test.go b/backend/internal/services/image_update_service_test.goindex ec8eaedf5..3727431c3 100644--- a/backend/internal/services/image_update_service_test.go+++ b/backend/internal/services/image_update_service_test.go@@ -516,7 +516,7 @@ func TestImageUpdateService_CheckImageUpdate_UsesRegistryFallback(t *testing.T)return client.DistributionInspectResult{}, errors.New("Error response from daemon: Not Found")},}, nil- })+ }, nil)registryService.distributionHTTPClient = server.Client()dockerService := &DockerClientService{client: newTestDockerClient(t, server)}@@ -556,7 +556,7 @@ func TestImageUpdateService_CheckMultipleImages_UsesRegistryFallback(t *testing.return client.DistributionInspectResult{}, errors.New("Error response from daemon: <html><body><h1>403 Forbidden</h1> Request forbidden by administrative rules. </body></html>")},}, nil- })+ }, nil)registryService.distributionHTTPClient = server.Client()dockerService := &DockerClientService{client: newTestDockerClient(t, server)}@@ -597,7 +597,7 @@ func TestImageUpdateService_CheckMultipleImages_PersistsRefScopedErrorsWhenLocalreturn client.DistributionInspectResult{}, errors.New("Error response from daemon: Not Found")},}, nil- })+ }, nil)registryService.distributionHTTPClient = server.Client()dockerService := &DockerClientService{client: newTestDockerClient(t, server)}diff --git a/backend/internal/services/kv_service.go b/backend/internal/services/kv_service.goindex ffac00227..2dae9b5df 100644--- a/backend/internal/services/kv_service.go+++ b/backend/internal/services/kv_service.go@@ -69,3 +69,52 @@ func (s *KVService) GetBool(ctx context.Context, key string, defaultValue bool)func (s *KVService) SetBool(ctx context.Context, key string, value bool) error {return s.Set(ctx, key, strconv.FormatBool(value))}++func (s *KVService) GetInt64(ctx context.Context, key string, defaultValue int64) (int64, error) {+ rawValue, ok, err := s.Get(ctx, key)+ if err != nil {+ return defaultValue, err+ }+ if !ok {+ return defaultValue, nil+ }++ parsedValue, err := strconv.ParseInt(rawValue, 10, 64)+ if err != nil {+ return defaultValue, fmt.Errorf("failed to parse kv entry %q as int64: %w", key, err)+ }++ return parsedValue, nil+}++func (s *KVService) IncrementInt64(ctx context.Context, key string, delta int64) (int64, error) {+ var nextValue int64+ err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {+ var entry models.KVEntry+ err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("key = ?", key).First(&entry).Error+ if errors.Is(err, gorm.ErrRecordNotFound) {+ nextValue = delta+ return tx.Create(&models.KVEntry{+ Key: key,+ Value: strconv.FormatInt(nextValue, 10),+ }).Error+ }+ if err != nil {+ return err+ }++ currentValue, parseErr := strconv.ParseInt(entry.Value, 10, 64)+ if parseErr != nil {+ return fmt.Errorf("failed to parse kv entry %q as int64: %w", key, parseErr)+ }++ nextValue = currentValue + delta+ entry.Value = strconv.FormatInt(nextValue, 10)+ return tx.Save(&entry).Error+ })+ if err != nil {+ return 0, fmt.Errorf("failed to increment kv entry %q: %w", key, err)+ }++ return nextValue, nil+}diff --git a/backend/pkg/utils/distribution/distribution.go b/backend/pkg/utils/distribution/distribution.goindex 34b49ef80..f732e46eb 100644--- a/backend/pkg/utils/distribution/distribution.go+++ b/backend/pkg/utils/distribution/distribution.go@@ -8,6 +8,7 @@ import ("net""net/http""net/url"+ "strconv""strings""time"@@ -15,7 +16,8 @@ import (ref "go.podman.io/image/v5/docker/reference")-const defaultRegistryHost = "index.docker.io"+const defaultRegistryHost = "registry-1.docker.io"+const dockerHubRateLimitRepository = "ratelimitpreview/test"// trustedAuthDelegations maps registry hosts to their trusted external auth realm hosts.// Some registries serve images under their own domain but delegate token auth to a@@ -30,6 +32,15 @@ type Credentials struct {Token string}+// RateLimitInfo contains pull quota information returned by registry headers.+type RateLimitInfo struct {+ Limit *int `json:"limit,omitempty"`+ Remaining *int `json:"remaining,omitempty"`+ Used *int `json:"used,omitempty"`+ WindowSeconds *int `json:"windowSeconds,omitempty"`+ Source string `json:"source,omitempty"`+}+type Reference struct {NormalizedRef stringRegistryHost string@@ -120,6 +131,53 @@ func FetchDigest(ctx context.Context, registryHost, repository, tag string, credreturn FetchDigestWithHTTPClient(ctx, registryHost, repository, tag, credential, nil)}+// FetchDockerHubRateLimit fetches Docker Hub pull rate limit information.+func FetchDockerHubRateLimit(ctx context.Context, credential *Credentials) (*RateLimitInfo, error) {+ return FetchDockerHubRateLimitWithHTTPClient(ctx, credential, nil)+}++// FetchDockerHubRateLimitWithHTTPClient fetches Docker Hub pull rate limit+// information using the provided HTTP client.+func FetchDockerHubRateLimitWithHTTPClient(ctx context.Context, credential *Credentials, httpClient *http.Client) (*RateLimitInfo, error) {+ return FetchRegistryRateLimitWithHTTPClient(ctx, "docker.io", dockerHubRateLimitRepository, "latest", credential, httpClient)+}++// FetchRegistryRateLimitWithHTTPClient fetches pull rate limit information from+// an OCI registry manifest response.+func FetchRegistryRateLimitWithHTTPClient(ctx context.Context, registryHost, repository, tag string, credential *Credentials, httpClient *http.Client) (*RateLimitInfo, error) {+ if httpClient == nil {+ httpClient = NewRegistryHTTPClient()+ }++ requestCtx, cancel := context.WithTimeout(ctx, 30*time.Second)+ defer cancel()++ authHeader := ""+ if credential != nil && strings.TrimSpace(credential.Username) != "" && strings.TrimSpace(credential.Token) != "" {+ authHeader = basicAuthHeaderInternal(credential.Username, credential.Token)+ }++ resp, err := manifestRequestInternal(requestCtx, httpClient, registryHost, repository, tag, authHeader)+ if err != nil {+ return nil, err+ }+ defer func() { _ = resp.Body.Close() }()++ if resp.StatusCode == http.StatusUnauthorized {+ challenge := resp.Header.Get("WWW-Authenticate")+ if challenge == "" {+ return nil, fmt.Errorf("manifest request failed with status: %d", resp.StatusCode)+ }+ return fetchRateLimitWithTokenAuthInternal(requestCtx, httpClient, registryHost, repository, tag, challenge, credential)+ }++ if resp.StatusCode != http.StatusOK {+ return nil, fmt.Errorf("manifest request failed with status: %d", resp.StatusCode)+ }++ return extractRateLimitFromHeadersInternal(resp.Header)+}+func FetchDigestWithHTTPClient(ctx context.Context, registryHost, repository, tag string, credential *Credentials, httpClient *http.Client) (string, error) {if httpClient == nil {httpClient = NewRegistryHTTPClient()@@ -159,6 +217,33 @@ func FetchDigestWithHTTPClient(ctx context.Context, registryHost, repository, tareturn digest, nil}+func fetchRateLimitWithTokenAuthInternal(ctx context.Context, httpClient *http.Client, registryHost, repository, tag, challenge string, credential *Credentials) (*RateLimitInfo, error) {+ realm, service := parseWWWAuthInternal(challenge)+ if realm == "" {+ return nil, fmt.Errorf("no auth realm found")+ }+ if err := validateAuthRealmInternal(registryHost, realm); err != nil {+ return nil, err+ }++ token, err := fetchRegistryTokenInternal(ctx, httpClient, realm, service, repository, credential)+ if err != nil {+ return nil, err+ }++ resp, err := manifestRequestInternal(ctx, httpClient, registryHost, repository, tag, token)+ if err != nil {+ return nil, err+ }+ defer func() { _ = resp.Body.Close() }()++ if resp.StatusCode != http.StatusOK {+ return nil, fmt.Errorf("authenticated manifest request failed with status: %d", resp.StatusCode)+ }++ return extractRateLimitFromHeadersInternal(resp.Header)+}+func fetchWithTokenAuthInternal(ctx context.Context, httpClient *http.Client, registryHost, repository, tag, challenge string, credential *Credentials) (string, error) {realm, service := parseWWWAuthInternal(challenge)if realm == "" {@@ -352,6 +437,88 @@ func extractDigestFromHeadersInternal(headers http.Header) string {return ""}+func extractRateLimitFromHeadersInternal(headers http.Header) (*RateLimitInfo, error) {+ limit, limitWindow, limitErr := parseRateLimitHeaderInternal(headers.Get("RateLimit-Limit"))+ if limitErr != nil {+ return nil, fmt.Errorf("parse RateLimit-Limit: %w", limitErr)+ }++ remaining, remainingWindow, remainingErr := parseRateLimitHeaderInternal(headers.Get("RateLimit-Remaining"))+ if remainingErr != nil {+ return nil, fmt.Errorf("parse RateLimit-Remaining: %w", remainingErr)+ }++ if limit == nil && remaining == nil {+ return nil, fmt.Errorf("rate limit headers not returned")+ }++ windowSeconds := limitWindow+ if windowSeconds == nil {+ windowSeconds = remainingWindow+ }++ var used *int+ if limit != nil && remaining != nil {+ value := *limit - *remaining+ used = &value+ }++ return &RateLimitInfo{+ Limit: limit,+ Remaining: remaining,+ Used: used,+ WindowSeconds: windowSeconds,+ Source: strings.TrimSpace(headers.Get("Docker-RateLimit-Source")),+ }, nil+}++func parseRateLimitHeaderInternal(value string) (*int, *int, error) {+ value = strings.TrimSpace(value)+ if value == "" {+ return nil, nil, nil+ }++ if beforeComma, _, ok := strings.Cut(value, ","); ok {+ value = strings.TrimSpace(beforeComma)+ }++ parts := strings.Split(value, ";")+ quota, err := parsePositiveIntInternal(strings.TrimSpace(parts[0]))+ if err != nil {+ return nil, nil, err+ }++ var window *int+ for _, part := range parts[1:] {+ key, raw, ok := strings.Cut(strings.TrimSpace(part), "=")+ if !ok || strings.TrimSpace(key) != "w" {+ continue+ }++ parsedWindow, parseErr := parsePositiveIntInternal(strings.TrimSpace(raw))+ if parseErr != nil {+ return nil, nil, fmt.Errorf("invalid window: %w", parseErr)+ }+ window = parsedWindow+ break+ }++ return quota, window, nil+}++func parsePositiveIntInternal(value string) (*int, error) {+ if value == "" {+ return nil, fmt.Errorf("empty integer value")+ }++ parsed, err := strconv.Atoi(value)+ if err != nil || parsed < 0 {+ return nil, fmt.Errorf("invalid integer value %q", value)+ }++ return &parsed, nil+}+func parseWWWAuthInternal(header string) (string, string) {lower := strings.ToLower(header)if !strings.HasPrefix(lower, "bearer ") {diff --git a/backend/pkg/utils/distribution/distribution_test.go b/backend/pkg/utils/distribution/distribution_test.goindex f9923bd0c..939acb250 100644--- a/backend/pkg/utils/distribution/distribution_test.go+++ b/backend/pkg/utils/distribution/distribution_test.go@@ -12,6 +12,10 @@ import ("github.com/stretchr/testify/require")+func serverURLFromRequestInternal(r *http.Request) string {+ return "https://" + r.Host+}+func TestIsFallbackEligibleDaemonError(t *testing.T) {eligible := []string{"not found",@@ -59,7 +63,7 @@ func TestFetchDigestWithHTTPClient_FallsBackToGetOnMethodNotAllowed(t *testing.Tvar getCalls intwantDigest := digest.FromString("method-not-allowed").String()- server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {switch r.Method {case http.MethodHead:headCalls++@@ -134,6 +138,141 @@ func TestFetchDigestWithHTTPClient_DoesNotFallbackToGetOnResourceErrors(t *testi}}+func TestFetchRegistryRateLimitWithHTTPClient_AnonymousTokenFlow(t *testing.T) {+ var tokenCalls int+ var manifestCalls int++ server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ switch {+ case r.URL.Path == "/token":+ tokenCalls+++ assert.Empty(t, r.Header.Get("Authorization"))+ assert.Equal(t, "registry.example.com", r.URL.Query().Get("service"))+ assert.Equal(t, "repository:team/app:pull", r.URL.Query().Get("scope"))+ _, _ = w.Write([]byte(`{"token":"test-token"}`))+ case r.Method == http.MethodHead && r.URL.Path == "/v2/team/app/manifests/latest":+ manifestCalls+++ if r.Header.Get("Authorization") != "Bearer test-token" {+ w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Bearer realm="%s/token",service="registry.example.com"`, serverURLFromRequestInternal(r)))+ w.WriteHeader(http.StatusUnauthorized)+ return+ }+ w.Header().Set("RateLimit-Limit", "100;w=21600")+ w.Header().Set("RateLimit-Remaining", "76;w=21600")+ w.Header().Set("Docker-RateLimit-Source", "203.0.113.10")+ w.WriteHeader(http.StatusOK)+ default:+ t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)+ }+ }))+ defer server.Close()++ info, err := FetchRegistryRateLimitWithHTTPClient(+ context.Background(),+ server.URL,+ "team/app",+ "latest",+ nil,+ server.Client(),+ )++ require.NoError(t, err)+ require.NotNil(t, info.Limit)+ require.NotNil(t, info.Remaining)+ require.NotNil(t, info.Used)+ require.NotNil(t, info.WindowSeconds)+ assert.Equal(t, 100, *info.Limit)+ assert.Equal(t, 76, *info.Remaining)+ assert.Equal(t, 24, *info.Used)+ assert.Equal(t, 21600, *info.WindowSeconds)+ assert.Equal(t, "203.0.113.10", info.Source)+ assert.Equal(t, 1, tokenCalls)+ assert.Equal(t, 2, manifestCalls)+}++func TestFetchRegistryRateLimitWithHTTPClient_CredentialBackedTokenFlow(t *testing.T) {+ var tokenAuth string+ var initialManifestAuth string++ server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ switch {+ case r.URL.Path == "/token":+ tokenAuth = r.Header.Get("Authorization")+ _, _ = w.Write([]byte(`{"access_token":"credential-token"}`))+ case r.Method == http.MethodHead && r.URL.Path == "/v2/team/app/manifests/latest":+ if r.Header.Get("Authorization") != "Bearer credential-token" {+ initialManifestAuth = r.Header.Get("Authorization")+ w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Bearer realm="%s/token",service="registry.example.com"`, serverURLFromRequestInternal(r)))+ w.WriteHeader(http.StatusUnauthorized)+ return+ }+ w.Header().Set("RateLimit-Limit", "200;w=21600")+ w.Header().Set("RateLimit-Remaining", "199;w=21600")+ w.WriteHeader(http.StatusOK)+ default:+ t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)+ }+ }))+ defer server.Close()++ info, err := FetchRegistryRateLimitWithHTTPClient(+ context.Background(),+ server.URL,+ "team/app",+ "latest",+ &Credentials{Username: "docker-user", Token: "docker-token"},+ server.Client(),+ )++ require.NoError(t, err)+ require.NotNil(t, info.Limit)+ require.NotNil(t, info.Remaining)+ assert.Equal(t, 200, *info.Limit)+ assert.Equal(t, 199, *info.Remaining)+ assert.Contains(t, initialManifestAuth, "Basic ")+ assert.Contains(t, tokenAuth, "Basic ")+}++func TestFetchRegistryRateLimitWithHTTPClient_MissingHeaders(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusOK)+ }))+ defer server.Close()++ info, err := FetchRegistryRateLimitWithHTTPClient(context.Background(), server.URL, "team/app", "latest", nil, server.Client())++ require.Error(t, err)+ assert.Nil(t, info)+ assert.Contains(t, err.Error(), "rate limit headers not returned")+}++func TestFetchRegistryRateLimitWithHTTPClient_MalformedHeader(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("RateLimit-Limit", "bad;w=21600")+ w.WriteHeader(http.StatusOK)+ }))+ defer server.Close()++ info, err := FetchRegistryRateLimitWithHTTPClient(context.Background(), server.URL, "team/app", "latest", nil, server.Client())++ require.Error(t, err)+ assert.Nil(t, info)+ assert.Contains(t, err.Error(), "parse RateLimit-Limit")+}++func TestFetchRegistryRateLimitWithHTTPClient_NonOKResponse(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusTooManyRequests)+ }))+ defer server.Close()++ info, err := FetchRegistryRateLimitWithHTTPClient(context.Background(), server.URL, "team/app", "latest", nil, server.Client())++ require.Error(t, err)+ assert.Nil(t, info)+ assert.Contains(t, err.Error(), "manifest request failed with status: 429")+}+func TestParseWWWAuthInternal_AllowsCommasInsideQuotedRealm(t *testing.T) {realm, service := parseWWWAuthInternal(`Bearer realm="https://auth.example.com/token?a=1,b=2",service="registry.example.com"`)assert.Equal(t, "https://auth.example.com/token?a=1,b=2", realm)diff --git a/frontend/messages/en.json b/frontend/messages/en.jsonindex 4e809212c..4dd249000 100644--- a/frontend/messages/en.json+++ b/frontend/messages/en.json@@ -1681,6 +1681,9 @@"registries_test_connection": "Test Connection","registries_test_failed": "Failed to test registry \"{url}\"","registries_test_success": "Registry test passed: {url} - {message}",+ "registries_pull_usage": "Pull Usage",+ "registries_pull_limit_value": "{remaining}/{limit}",+ "registries_observed_pulls_value": "{count} pulls","registries_url": "Registry URL","registries_url_required": "Registry URL is required","registries_info_title": "Registry Information",diff --git a/frontend/src/lib/query/query-keys.ts b/frontend/src/lib/query/query-keys.tsindex 38025a2b3..788ae3fac 100644--- a/frontend/src/lib/query/query-keys.ts+++ b/frontend/src/lib/query/query-keys.ts@@ -49,7 +49,8 @@ export const queryKeys = {},containerRegistries: {all: ['container-registries'] as const,- list: (options: SearchPaginationSortRequest) => ['container-registries', stableSerialize(options)] as const+ list: (options: SearchPaginationSortRequest) => ['container-registries', stableSerialize(options)] as const,+ pullUsage: () => ['container-registries', 'pull-usage'] as const},templates: {all: ['templates'] as const,diff --git a/frontend/src/lib/services/container-registry-service.ts b/frontend/src/lib/services/container-registry-service.tsindex b72ea8ce3..df348f8e9 100644--- a/frontend/src/lib/services/container-registry-service.ts+++ b/frontend/src/lib/services/container-registry-service.ts@@ -1,5 +1,9 @@import BaseAPIService from './api-service';-import type { ContainerRegistryCreateDto, ContainerRegistryUpdateDto } from '$lib/types/container-registry.type';+import type {+ ContainerRegistryCreateDto,+ ContainerRegistryPullUsageResponse,+ ContainerRegistryUpdateDto+} from '$lib/types/container-registry.type';import type { ContainerRegistry } from '$lib/types/container-registry.type';import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';import { transformPaginationParams } from '$lib/utils/params.util';@@ -15,6 +19,10 @@ export default class ContainerRegistryService extends BaseAPIService {return this.handleResponse(this.api.get(`/container-registries/${id}`));}+ async getPullUsage(): Promise<ContainerRegistryPullUsageResponse> {+ return this.handleResponse(this.api.get('/container-registries/pull-usage'));+ }+async createRegistry(registry: ContainerRegistryCreateDto): Promise<ContainerRegistry> {return this.handleResponse(this.api.post(`/container-registries`, registry));}diff --git a/frontend/src/lib/types/container-registry.type.ts b/frontend/src/lib/types/container-registry.type.tsindex 7c09d15db..90f5d6024 100644--- a/frontend/src/lib/types/container-registry.type.ts+++ b/frontend/src/lib/types/container-registry.type.ts@@ -40,3 +40,25 @@ export interface ContainerRegistry {createdAt?: string;updatedAt?: string;}++export interface ContainerRegistryPullUsageResponse {+ registries: ContainerRegistryPullUsage[];+}++export interface ContainerRegistryPullUsage {+ registryId: string;+ provider: string;+ registry: string;+ displayName: string;+ repository?: string;+ limit?: number;+ remaining?: number;+ used?: number;+ windowSeconds?: number;+ observedPulls: number;+ authMethod: 'anonymous' | 'credential' | 'unknown';+ authUsername?: string;+ source?: string;+ checkedAt: string;+ error?: string;+}diff --git a/frontend/src/routes/(app)/customize/registries/+page.svelte b/frontend/src/routes/(app)/customize/registries/+page.svelteindex f835617c4..7c492f63a 100644--- a/frontend/src/routes/(app)/customize/registries/+page.svelte+++ b/frontend/src/routes/(app)/customize/registries/+page.svelte@@ -3,7 +3,7 @@import * as Dialog from '$lib/components/ui/dialog/index.js';import { InfoIcon } from '$lib/icons';import { toast } from 'svelte-sonner';- import type { ContainerRegistry } from '$lib/types/container-registry.type';+ import type { ContainerRegistry, ContainerRegistryPullUsage } from '$lib/types/container-registry.type';import type { ContainerRegistryCreateDto, ContainerRegistryUpdateDto } from '$lib/types/container-registry.type';import ContainerRegistryFormSheet from '$lib/components/sheets/container-registry-sheet.svelte';import RegistryTable from './registry-table.svelte';@@ -11,8 +11,10 @@import { tryCatch } from '$lib/utils/try-catch';import { m } from '$lib/paraglide/messages';import { containerRegistryService } from '$lib/services/container-registry-service';+ import { queryKeys } from '$lib/query/query-keys';import { untrack } from 'svelte';- import { ResourcePageLayout, type ActionButton, type StatCardConfig } from '$lib/layouts/index.js';+ import { ResourcePageLayout, type ActionButton } from '$lib/layouts/index.js';+ import { createQuery } from '@tanstack/svelte-query';let { data } = $props();@@ -22,6 +24,15 @@let isInfoDialogOpen = $state(false);let registryToEdit = $state<ContainerRegistry | null>(null);let requestOptions = $state(untrack(() => data.registryRequestOptions));+ const pullUsageQuery = createQuery(() => ({+ queryKey: queryKeys.containerRegistries.pullUsage(),+ queryFn: () => containerRegistryService.getPullUsage(),+ initialData: data.pullUsage ?? undefined+ }));+ const pullUsageByRegistry = $derived.by<Record<string, ContainerRegistryPullUsage>>(() => {+ const entries = pullUsageQuery.data?.registries ?? [];+ return Object.fromEntries(entries.map((usage) => [usage.registryId, usage]));+ });let isLoading = $state({create: false,@@ -37,6 +48,7 @@setLoadingState: (value) => (isLoading.refresh = value),onSuccess: async (newRegistries) => {registries = newRegistries;+ await pullUsageQuery.refetch();toast.success(m.registries_refreshed());}});@@ -69,7 +81,7 @@toast.success(m.common_create_success({ resource: m.resource_registry() }));}- registries = await containerRegistryService.getRegistries(requestOptions);+ [registries] = await Promise.all([containerRegistryService.getRegistries(requestOptions), pullUsageQuery.refetch()]);isRegistryDialogOpen = false;} catch (error) {console.error('Error saving registry:', error);@@ -105,7 +117,13 @@<ResourcePageLayout title={m.registries_title()} subtitle={m.registries_subtitle()} {actionButtons}>{#snippet mainContent()}- <RegistryTable bind:registries bind:selectedIds bind:requestOptions onEditRegistry={openEditRegistryDialog} />+ <RegistryTable+ bind:registries+ bind:selectedIds+ bind:requestOptions+ {pullUsageByRegistry}+ onEditRegistry={openEditRegistryDialog}+ />{/snippet}{#snippet additionalContent()}diff --git a/frontend/src/routes/(app)/customize/registries/+page.ts b/frontend/src/routes/(app)/customize/registries/+page.tsindex 841992c58..93ca7fad3 100644--- a/frontend/src/routes/(app)/customize/registries/+page.ts+++ b/frontend/src/routes/(app)/customize/registries/+page.ts@@ -22,6 +22,12 @@ export const load: PageLoad = async ({ parent }) => {queryKey: queryKeys.containerRegistries.list(registryRequestOptions),queryFn: () => containerRegistryService.getRegistries(registryRequestOptions)});+ const pullUsage = await queryClient+ .fetchQuery({+ queryKey: queryKeys.containerRegistries.pullUsage(),+ queryFn: () => containerRegistryService.getPullUsage()+ })+ .catch(() => null);- return { registries, registryRequestOptions };+ return { registries, registryRequestOptions, pullUsage };};diff --git a/frontend/src/routes/(app)/customize/registries/registry-table.svelte b/frontend/src/routes/(app)/customize/registries/registry-table.svelteindex ab1bf319f..542422a58 100644--- a/frontend/src/routes/(app)/customize/registries/registry-table.svelte+++ b/frontend/src/routes/(app)/customize/registries/registry-table.svelte@@ -9,7 +9,7 @@import { handleApiResultWithCallbacks } from '$lib/utils/api.util';import { tryCatch } from '$lib/utils/try-catch';import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';- import type { ContainerRegistry } from '$lib/types/container-registry.type';+ import type { ContainerRegistry, ContainerRegistryPullUsage } from '$lib/types/container-registry.type';import type { ColumnSpec, MobileFieldVisibility, BulkAction } from '$lib/components/arcane-table';import { UniversalMobileCard } from '$lib/components/arcane-table/index.js';import { format } from 'date-fns';@@ -21,11 +21,13 @@registries = $bindable(),selectedIds = $bindable(),requestOptions = $bindable(),+ pullUsageByRegistry = {},onEditRegistry}: {registries: Paginated<ContainerRegistry>;selectedIds: string[];requestOptions: SearchPaginationSortRequest;+ pullUsageByRegistry?: Record<string, ContainerRegistryPullUsage>;onEditRegistry: (registry: ContainerRegistry) => void;} = $props();@@ -48,6 +50,15 @@return url;}+ function formatPullUsage(item: ContainerRegistry) {+ const usage = pullUsageByRegistry[item.id];+ if (!usage) return m.common_unavailable();+ if (usage.remaining !== undefined && usage.limit !== undefined) {+ return m.registries_pull_limit_value({ remaining: usage.remaining, limit: usage.limit });+ }+ return m.registries_observed_pulls_value({ count: usage.observedPulls });+ }+async function handleDeleteSelected(ids: string[]) {if (!ids?.length) return;@@ -154,6 +165,12 @@sortable: true,cell: StatusCell},+ {+ id: 'pullUsage',+ accessorFn: (row) => row.id,+ title: m.registries_pull_usage(),+ cell: PullUsageCell+ },{accessorKey: 'createdAt',title: m.common_created(),@@ -167,6 +184,7 @@{ id: 'username', label: m.registries_username_key_label(), defaultVisible: true },{ id: 'description', label: m.common_description(), defaultVisible: true },{ id: 'enabled', label: m.common_status(), defaultVisible: true },+ { id: 'pullUsage', label: m.registries_pull_usage(), defaultVisible: true },{ id: 'createdAt', label: m.common_created(), defaultVisible: true }];@@ -213,6 +231,10 @@<StatusBadge variant={enabled ? 'green' : 'red'} text={enabled ? m.common_enabled() : m.common_disabled()} />{/snippet}+{#snippet PullUsageCell({ item }: { item: ContainerRegistry })}+ <span class="text-sm">{formatPullUsage(item)}</span>+{/snippet}+{#snippet CreatedCell({ value }: { value: unknown })}<span class="text-sm">{value ? format(new Date(String(value)), 'PP p') : m.common_na()}</span>{/snippet}@@ -247,6 +269,13 @@icon: ExternalLinkIcon,iconVariant: 'gray' as const,show: (mobileFieldVisibility.description ?? true) && item.description !== undefined+ },+ {+ label: m.registries_pull_usage(),+ getValue: (item: ContainerRegistry) => formatPullUsage(item),+ icon: RegistryIcon,+ iconVariant: 'gray' as const,+ show: mobileFieldVisibility.pullUsage ?? true}]}rowActions={RowActions}diff --git a/frontend/src/routes/(app)/images/+page.svelte b/frontend/src/routes/(app)/images/+page.svelteindex c8b310e3e..6ad9651a7 100644--- a/frontend/src/routes/(app)/images/+page.svelte+++ b/frontend/src/routes/(app)/images/+page.svelte@@ -14,7 +14,7 @@import type { ImageUsageCounts } from '$lib/types/image.type';import { untrack } from 'svelte';import { ResourcePageLayout, type ActionButton, type StatCardConfig } from '$lib/layouts/index.js';- import { CloseIcon, VolumesIcon, LocalFolderComputerIcon, CodeIcon } from '$lib/icons';+ import { CloseIcon, VolumesIcon, LocalFolderComputerIcon } from '$lib/icons';import { createMutation, createQuery } from '@tanstack/svelte-query';import PruneModeCard from '$lib/components/prune/prune-mode-card.svelte';diff --git a/tests/spec/registries.spec.ts b/tests/spec/registries.spec.tsindex aef071cee..57c0efa94 100644--- a/tests/spec/registries.spec.ts+++ b/tests/spec/registries.spec.ts@@ -1,7 +1,79 @@-import { test, expect } from '@playwright/test';+import { test, expect, type Page } from '@playwright/test';const route = '/customize/registries';const TOKEN = process.env.REGISTRY_TEST_TOKEN ?? 'e2e-token';+const pullUsageRoute = /\/api\/container-registries\/pull-usage(?:\?|$)/;+const registryListRoute = /\/api\/container-registries(?:\?|$)/;++async function mockRegistryList(page: Page, registryId = 'dockerhub-registry') {+ await page.context().route(registryListRoute, async (route) => {+ if (route.request().method() !== 'GET') {+ await route.fallback();+ return;+ }++ await route.fulfill({+ status: 200,+ contentType: 'application/json',+ body: JSON.stringify({+ success: true,+ data: [+ {+ id: registryId,+ url: 'docker.io',+ username: 'docker-user',+ description: 'Docker Hub test registry',+ insecure: false,+ enabled: true,+ registryType: 'generic',+ createdAt: new Date().toISOString(),+ updatedAt: new Date().toISOString()+ }+ ],+ pagination: {+ totalPages: 1,+ totalItems: 1,+ currentPage: 1,+ itemsPerPage: 20,+ grandTotalItems: 1+ }+ })+ });+ });+}++async function mockRegistryPullUsage(+ page: Page,+ options: { registryId?: string; remaining?: number; limit?: number; observedPulls?: number }+) {+ const registryId = options.registryId ?? 'dockerhub-registry';+ await page.context().route(pullUsageRoute, async (route) => {+ await route.fulfill({+ status: 200,+ contentType: 'application/json',+ body: JSON.stringify({+ success: true,+ data: {+ registries: [+ {+ registryId,+ provider: 'dockerhub',+ registry: 'docker.io',+ displayName: 'Docker Hub',+ repository: 'ratelimitpreview/test',+ ...(options.limit === undefined ? {} : { limit: options.limit }),+ ...(options.remaining === undefined ? {} : { remaining: options.remaining }),+ observedPulls: options.observedPulls ?? 0,+ authMethod: 'credential',+ authUsername: 'docker-user',+ checkedAt: new Date().toISOString()+ }+ ]+ }+ })+ });+ });+}test.describe('Container Registries', () => {test.beforeEach(async ({ page }) => {@@ -42,6 +114,30 @@ test.describe('Container Registries', () => {await expect(dialog).toBeHidden();});+ test('should display registry pull limit usage when headers are available', async ({ page }) => {+ await mockRegistryList(page);+ await mockRegistryPullUsage(page, { remaining: 76, limit: 100, observedPulls: 4 });+ await page.goto(route);+ await page.waitForLoadState('networkidle');++ const table = page.getByRole('table');+ await expect(table.getByText('Pull Usage')).toBeVisible();+ await expect(table.getByText('76/100')).toBeVisible();+ });++ test('should display observed pull count when registry limit headers are unavailable', async ({+ page+ }) => {+ await mockRegistryList(page);+ await mockRegistryPullUsage(page, { observedPulls: 4 });+ await page.goto(route);+ await page.waitForLoadState('networkidle');++ const table = page.getByRole('table');+ await expect(table.getByText('Pull Usage')).toBeVisible();+ await expect(table.getByText('4 pulls')).toBeVisible();+ });+test('should create, test, edit, and delete a registry', async ({ page }) => {// Createawait page.getByRole('button', { name: 'Add Registry' }).click();diff --git a/types/containerregistry/container_registry.go b/types/containerregistry/container_registry.goindex 473ddc19d..748b9a1cd 100644--- a/types/containerregistry/container_registry.go+++ b/types/containerregistry/container_registry.go@@ -60,6 +60,90 @@ type ContainerRegistry struct {UpdatedAt time.Time `json:"updatedAt"`}+type PullUsageResponse struct {+ // Registries contains pull usage visibility by configured registry.+ //+ // Required: true+ Registries []PullUsage `json:"registries"`+}++type PullUsage struct {+ // RegistryID is the configured registry row ID.+ //+ // Required: true+ RegistryID string `json:"registryId"`++ // Provider is the registry provider identifier when known.+ //+ // Required: true+ Provider string `json:"provider"`++ // Registry is the normalized registry host.+ //+ // Required: true+ Registry string `json:"registry"`++ // DisplayName is the human-readable registry name.+ //+ // Required: true+ DisplayName string `json:"displayName"`++ // Repository is the repository used for an optional rate limit probe.+ //+ // Required: false+ Repository string `json:"repository,omitempty"`++ // Limit is the pull limit for the current window.+ //+ // Required: false+ Limit *int `json:"limit,omitempty"`++ // Remaining is the remaining pulls for the current window.+ //+ // Required: false+ Remaining *int `json:"remaining,omitempty"`++ // Used is the number of pulls used in the current window.+ //+ // Required: false+ Used *int `json:"used,omitempty"`++ // WindowSeconds is the current rate limit window duration in seconds.+ //+ // Required: false+ WindowSeconds *int `json:"windowSeconds,omitempty"`++ // ObservedPulls is the number of successful pulls Arcane has initiated for this registry.+ //+ // Required: true+ ObservedPulls int64 `json:"observedPulls"`++ // AuthMethod is the authentication method used for probing.+ //+ // Required: true+ AuthMethod string `json:"authMethod"`++ // AuthUsername is the username used when probing with credentials.+ //+ // Required: false+ AuthUsername string `json:"authUsername,omitempty"`++ // Source is the registry-reported source for the rate limit bucket.+ //+ // Required: false+ Source string `json:"source,omitempty"`++ // CheckedAt is the time when usage was computed.+ //+ // Required: true+ CheckedAt time.Time `json:"checkedAt"`++ // Error contains a recoverable probe or counter error.+ //+ // Required: false+ Error string `json:"error,omitempty"`+}+type Sync struct {// ID of the container registry.//