Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: registry syncing to environments not running on initially pairing (#1822)
Kyle Mendellaaa11a2f5bac
diff --git a/backend/internal/huma/handlers/container_registries.go b/backend/internal/huma/handlers/container_registries.goindex a52eaeca9..f9cd56c6c 100644--- a/backend/internal/huma/handlers/container_registries.go+++ b/backend/internal/huma/handlers/container_registries.go@@ -3,6 +3,7 @@ package handlersimport ("context""fmt"+ "log/slog""github.com/danielgtaylor/huma/v2""github.com/getarcaneapp/arcane/backend/internal/common"@@ -17,7 +18,8 @@ import (// ContainerRegistryHandler handles container registry management endpoints.type ContainerRegistryHandler struct {- registryService *services.ContainerRegistryService+ registryService *services.ContainerRegistryService+ environmentService *services.EnvironmentService}// ============================================================================@@ -97,8 +99,11 @@ type SyncContainerRegistriesOutput struct {// ============================================================================// RegisterContainerRegistries registers all container registry endpoints.-func RegisterContainerRegistries(api huma.API, registryService *services.ContainerRegistryService) {- h := &ContainerRegistryHandler{registryService: registryService}+func RegisterContainerRegistries(api huma.API, registryService *services.ContainerRegistryService, environmentService *services.EnvironmentService) {+ h := &ContainerRegistryHandler{+ registryService: registryService,+ environmentService: environmentService,+ }huma.Register(api, huma.Operation{OperationID: "listContainerRegistries",@@ -240,6 +245,8 @@ func (h *ContainerRegistryHandler) CreateRegistry(ctx context.Context, input *Crreturn nil, huma.NewError(apiErr.HTTPStatus(), (&common.RegistryCreationError{Err: err}).Error())}+ h.triggerRemoteRegistrySync(ctx, "registry creation")+out, mapErr := mapper.MapOne[*models.ContainerRegistry, containerregistry.ContainerRegistry](reg)if mapErr != nil {return nil, huma.Error500InternalServerError((&common.RegistryMappingError{Err: mapErr}).Error())@@ -294,6 +301,8 @@ func (h *ContainerRegistryHandler) UpdateRegistry(ctx context.Context, input *Upreturn nil, huma.NewError(apiErr.HTTPStatus(), (&common.RegistryUpdateError{Err: err}).Error())}+ h.triggerRemoteRegistrySync(ctx, "registry update")+out, mapErr := mapper.MapOne[*models.ContainerRegistry, containerregistry.ContainerRegistry](reg)if mapErr != nil {return nil, huma.Error500InternalServerError((&common.RegistryMappingError{Err: mapErr}).Error())@@ -322,6 +331,8 @@ func (h *ContainerRegistryHandler) DeleteRegistry(ctx context.Context, input *Dereturn nil, huma.NewError(apiErr.HTTPStatus(), (&common.RegistryDeletionError{Err: err}).Error())}+ h.triggerRemoteRegistrySync(ctx, "registry deletion")+return &DeleteContainerRegistryOutput{Body: base.ApiResponse[base.MessageResponse]{Success: true,@@ -422,3 +433,17 @@ func (h *ContainerRegistryHandler) performRegistryTest(ctx context.Context, regi"message": "Authentication succeeded",}, nil}++func (h *ContainerRegistryHandler) triggerRemoteRegistrySync(ctx context.Context, reason string) { //nolint:contextcheck // intentionally spawns background sync+ if h.environmentService == nil {+ return+ }++ detachedCtx := context.WithoutCancel(ctx)++ go func(syncCtx context.Context, syncReason string) {+ if err := h.environmentService.SyncRegistriesToRemoteEnvironments(syncCtx); err != nil {+ slog.WarnContext(syncCtx, "Failed to fan out registry sync to remote environments", "reason", syncReason, "error", err.Error())+ }+ }(detachedCtx, reason)+}diff --git a/backend/internal/huma/handlers/environments.go b/backend/internal/huma/handlers/environments.goindex aa90d8e8d..a226295e0 100644--- a/backend/internal/huma/handlers/environments.go+++ b/backend/internal/huma/handlers/environments.go@@ -476,18 +476,7 @@ func (h *EnvironmentHandler) createEnvironmentLegacy(ctx context.Context, env *m// Sync registries and git repositories in background (intentionally detached from request context)if created.AccessToken != nil && *created.AccessToken != "" {- go func(envID string, envName string) { //nolint:contextcheck // intentional background context for async task- bgCtx := context.Background()- if err := h.environmentService.SyncRegistriesToEnvironment(bgCtx, envID); err != nil {- slog.WarnContext(bgCtx, "Failed to sync registries to new environment", "environmentID", envID, "environmentName", envName, "error", err.Error())- }- }(created.ID, created.Name)- go func(envID string, envName string) { //nolint:contextcheck // intentional background context for async task- bgCtx := context.Background()- if err := h.environmentService.SyncRepositoriesToEnvironment(bgCtx, envID); err != nil {- slog.WarnContext(bgCtx, "Failed to sync git repositories to new environment", "environmentID", envID, "environmentName", envName, "error", err.Error())- }- }(created.ID, created.Name)+ h.triggerEnvironmentResourceSync(ctx, created.ID, created.Name, "environment creation")}out, mapErr := mapper.MapOne[*models.Environment, environment.Environment](created)@@ -558,7 +547,7 @@ func (h *EnvironmentHandler) UpdateEnvironment(ctx context.Context, input *Updatreturn nil, huma.Error500InternalServerError((&common.EnvironmentUpdateError{Err: updateErr}).Error())}- h.triggerPostUpdateTasks(input.ID, updated, pairingSucceeded, &input.Body) //nolint:contextcheck // intentionally detached background tasks+ h.triggerPostUpdateTasks(ctx, input.ID, updated, pairingSucceeded, &input.Body) //nolint:contextcheck // intentionally detached background tasksout, mapErr := mapper.MapOne[*models.Environment, environment.Environment](updated)if mapErr != nil {@@ -825,36 +814,47 @@ func (h *EnvironmentHandler) handleEnvironmentPairing(ctx context.Context, envirreturn pairingSucceeded, nil}-func (h *EnvironmentHandler) triggerPostUpdateTasks(environmentID string, updated *models.Environment, pairingSucceeded bool, req *environment.Update) { //nolint:contextcheck // intentionally spawns background tasks+func (h *EnvironmentHandler) triggerPostUpdateTasks(ctx context.Context, environmentID string, updated *models.Environment, pairingSucceeded bool, req *environment.Update) { //nolint:contextcheck // intentionally spawns background tasksif updated.Enabled {- go func(envID string, envName string) {- ctx := context.Background()- status, err := h.environmentService.TestConnection(ctx, envID, nil)+ detachedCtx := context.WithoutCancel(ctx)+ go func(syncCtx context.Context, envID string, envName string) {+ status, err := h.environmentService.TestConnection(syncCtx, envID, nil)if err != nil {- slog.WarnContext(ctx, "Failed to test connection after environment update",+ slog.WarnContext(syncCtx, "Failed to test connection after environment update","environment_id", envID, "environment_name", envName, "status", status, "error", err)}- }(environmentID, updated.Name)+ }(detachedCtx, environmentID, updated.Name)}if pairingSucceeded || (req.AccessToken != nil && *req.AccessToken != "") {- go func(envID string, envName string) {- ctx := context.Background()- if err := h.environmentService.SyncRegistriesToEnvironment(ctx, envID); err != nil {- slog.WarnContext(ctx, "Failed to sync registries after environment update",- "environmentID", envID, "environmentName", envName, "error", err.Error())- }- }(environmentID, updated.Name)- go func(envID string, envName string) {- ctx := context.Background()- if err := h.environmentService.SyncRepositoriesToEnvironment(ctx, envID); err != nil {- slog.WarnContext(ctx, "Failed to sync git repositories after environment update",- "environmentID", envID, "environmentName", envName, "error", err.Error())- }- }(environmentID, updated.Name)+ h.triggerEnvironmentResourceSync(ctx, environmentID, updated.Name, "environment update")}}+func (h *EnvironmentHandler) triggerEnvironmentResourceSync(ctx context.Context, environmentID string, environmentName string, reason string) { //nolint:contextcheck // intentionally spawns background tasks+ detachedCtx := context.WithoutCancel(ctx)++ go func(syncCtx context.Context, envID string, envName string, syncReason string) {+ if err := h.environmentService.SyncRegistriesToEnvironment(syncCtx, envID); err != nil {+ slog.WarnContext(syncCtx, "Failed to sync registries to environment",+ "environmentID", envID,+ "environmentName", envName,+ "reason", syncReason,+ "error", err.Error())+ }+ }(detachedCtx, environmentID, environmentName, reason)++ go func(syncCtx context.Context, envID string, envName string, syncReason string) {+ if err := h.environmentService.SyncRepositoriesToEnvironment(syncCtx, envID); err != nil {+ slog.WarnContext(syncCtx, "Failed to sync git repositories to environment",+ "environmentID", envID,+ "environmentName", envName,+ "reason", syncReason,+ "error", err.Error())+ }+ }(detachedCtx, environmentID, environmentName, reason)+}+// PairEnvironment handles agent pairing callback with API key.func (h *EnvironmentHandler) PairEnvironment(ctx context.Context, input *PairEnvironmentInput) (*PairEnvironmentOutput, error) {if h.environmentService == nil || h.apiKeyService == nil {@@ -895,6 +895,7 @@ func (h *EnvironmentHandler) PairEnvironment(ctx context.Context, input *PairEnv}slog.InfoContext(ctx, "Environment pairing completed", "environmentID", *envID, "environmentName", env.Name)+ h.triggerEnvironmentResourceSync(ctx, *envID, env.Name, "environment pairing")return &PairEnvironmentOutput{Body: base.ApiResponse[base.MessageResponse]{diff --git a/backend/internal/huma/huma.go b/backend/internal/huma/huma.goindex c268555d3..f4d0f06c2 100644--- a/backend/internal/huma/huma.go+++ b/backend/internal/huma/huma.go@@ -349,7 +349,7 @@ func registerHandlers(api huma.API, svc *Services) {handlers.RegisterEvents(api, eventSvc)handlers.RegisterOidc(api, authSvc, oidcSvc, cfg)handlers.RegisterEnvironments(api, environmentSvc, settingsSvc, apiKeySvc, eventSvc, cfg)- handlers.RegisterContainerRegistries(api, containerRegistrySvc)+ handlers.RegisterContainerRegistries(api, containerRegistrySvc, environmentSvc)handlers.RegisterTemplates(api, templateSvc)handlers.RegisterImages(api, dockerSvc, imageSvc, imageUpdateSvc, settingsSvc)handlers.RegisterImageUpdates(api, imageUpdateSvc)diff --git a/backend/internal/services/environment_service.go b/backend/internal/services/environment_service.goindex 85a410546..82f370791 100644--- a/backend/internal/services/environment_service.go+++ b/backend/internal/services/environment_service.go@@ -164,6 +164,43 @@ func (s *EnvironmentService) ListRemoteEnvironments(ctx context.Context) ([]modereturn envs, nil}+// SyncRegistriesToRemoteEnvironments syncs container registries to all eligible remote environments.+// Eligibility requires a non-local, enabled environment with a configured access token.+func (s *EnvironmentService) SyncRegistriesToRemoteEnvironments(ctx context.Context) error {+ envs, err := s.ListRemoteEnvironments(ctx)+ if err != nil {+ return fmt.Errorf("failed to list remote environments for registry sync: %w", err)+ }++ if len(envs) == 0 {+ return nil+ }++ var failedCount int+ for _, env := range envs {+ if env.AccessToken == nil || *env.AccessToken == "" {+ slog.DebugContext(ctx, "Skipping registry sync for environment without access token",+ "environmentID", env.ID,+ "environmentName", env.Name)+ continue+ }++ if err := s.SyncRegistriesToEnvironment(ctx, env.ID); err != nil {+ failedCount+++ slog.WarnContext(ctx, "Failed to sync registries to remote environment",+ "environmentID", env.ID,+ "environmentName", env.Name,+ "error", err.Error())+ }+ }++ if failedCount > 0 {+ return fmt.Errorf("failed to sync registries to %d remote environment(s)", failedCount)+ }++ return nil+}+func (s *EnvironmentService) UpdateEnvironment(ctx context.Context, id string, updates map[string]any, userID, username *string) (*models.Environment, error) {now := time.Now()updates["updated_at"] = &nowdiff --git a/backend/internal/services/environment_service_test.go b/backend/internal/services/environment_service_test.gonew file mode 100644index 000000000..9ffe7c0c0--- /dev/null+++ b/backend/internal/services/environment_service_test.go@@ -0,0 +1,185 @@+package services++import (+ "context"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "sync/atomic"+ "testing"+ "time"++ glsqlite "github.com/glebarez/sqlite"+ "github.com/stretchr/testify/require"+ "gorm.io/gorm"++ "github.com/getarcaneapp/arcane/backend/internal/config"+ "github.com/getarcaneapp/arcane/backend/internal/database"+ "github.com/getarcaneapp/arcane/backend/internal/models"+ "github.com/getarcaneapp/arcane/backend/internal/utils/crypto"+ "github.com/getarcaneapp/arcane/types/containerregistry"+)++func setupEnvironmentServiceTestDB(t *testing.T) *database.DB {+ t.Helper()++ db, err := gorm.Open(glsqlite.Open(":memory:"), &gorm.Config{})+ require.NoError(t, err)+ require.NoError(t, db.AutoMigrate(&models.Environment{}, &models.ContainerRegistry{}))++ testCfg := &config.Config{+ EncryptionKey: "test-encryption-key-for-testing-32bytes-min",+ Environment: "test",+ }+ crypto.InitEncryption(testCfg)++ return &database.DB{DB: db}+}++func createTestEnvironment(t *testing.T, db *database.DB, id string, apiURL string, accessToken *string) {+ t.Helper()++ now := time.Now()+ env := &models.Environment{+ BaseModel: models.BaseModel{+ ID: id,+ CreatedAt: now,+ UpdatedAt: &now,+ },+ Name: "env-" + id,+ ApiUrl: apiURL,+ Status: string(models.EnvironmentStatusOnline),+ Enabled: true,+ AccessToken: accessToken,+ }++ require.NoError(t, db.WithContext(context.Background()).Create(env).Error)+}++func createTestRegistry(t *testing.T, db *database.DB, id string) {+ t.Helper()++ encryptedToken, err := crypto.Encrypt("registry-token")+ require.NoError(t, err)++ now := time.Now()+ registry := &models.ContainerRegistry{+ BaseModel: models.BaseModel{+ ID: id,+ CreatedAt: now,+ UpdatedAt: &now,+ },+ URL: "registry.example.com",+ Username: "registry-user",+ Token: encryptedToken,+ Enabled: true,+ Insecure: false,+ CreatedAt: now,+ UpdatedAt: now,+ }++ require.NoError(t, db.WithContext(context.Background()).Create(registry).Error)+}++func TestEnvironmentService_SyncRegistriesToRemoteEnvironments_SyncsEligibleRemotes(t *testing.T) {+ ctx := context.Background()+ db := setupEnvironmentServiceTestDB(t)+ svc := NewEnvironmentService(db, nil, nil, nil, nil)++ createTestRegistry(t, db, "reg-1")++ var env1Calls atomic.Int32+ env1Token := "token-1"+ env1Server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ require.Equal(t, http.MethodPost, r.Method)+ require.Equal(t, "/api/container-registries/sync", r.URL.Path)+ require.Equal(t, env1Token, r.Header.Get("X-API-Key"))+ require.Equal(t, env1Token, r.Header.Get("X-Arcane-Agent-Token"))++ var syncReq containerregistry.SyncRequest+ require.NoError(t, json.NewDecoder(r.Body).Decode(&syncReq))+ require.Len(t, syncReq.Registries, 1)+ env1Calls.Add(1)++ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"success":true,"data":{"message":"ok"}}`))+ }))+ defer env1Server.Close()++ var env2Calls atomic.Int32+ env2Token := "token-2"+ env2Server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ require.Equal(t, http.MethodPost, r.Method)+ require.Equal(t, "/api/container-registries/sync", r.URL.Path)+ require.Equal(t, env2Token, r.Header.Get("X-API-Key"))+ require.Equal(t, env2Token, r.Header.Get("X-Arcane-Agent-Token"))++ var syncReq containerregistry.SyncRequest+ require.NoError(t, json.NewDecoder(r.Body).Decode(&syncReq))+ require.Len(t, syncReq.Registries, 1)+ env2Calls.Add(1)++ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"success":true,"data":{"message":"ok"}}`))+ }))+ defer env2Server.Close()++ createTestEnvironment(t, db, "0", "http://localhost:3552", nil) // local env should be excluded+ createTestEnvironment(t, db, "env-1", env1Server.URL, &env1Token)+ createTestEnvironment(t, db, "env-2", env2Server.URL, &env2Token)++ err := svc.SyncRegistriesToRemoteEnvironments(ctx)+ require.NoError(t, err)+ require.EqualValues(t, 1, env1Calls.Load())+ require.EqualValues(t, 1, env2Calls.Load())+}++func TestEnvironmentService_SyncRegistriesToRemoteEnvironments_SkipsRemoteWithoutAccessToken(t *testing.T) {+ ctx := context.Background()+ db := setupEnvironmentServiceTestDB(t)+ svc := NewEnvironmentService(db, nil, nil, nil, nil)++ createTestRegistry(t, db, "reg-1")++ var syncCalls atomic.Int32+ token := "token-with-auth"+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ syncCalls.Add(1)+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"success":true,"data":{"message":"ok"}}`))+ }))+ defer server.Close()++ createTestEnvironment(t, db, "env-auth", server.URL, &token)+ createTestEnvironment(t, db, "env-no-token", "http://127.0.0.1:1", nil)++ err := svc.SyncRegistriesToRemoteEnvironments(ctx)+ require.NoError(t, err)+ require.EqualValues(t, 1, syncCalls.Load())+}++func TestEnvironmentService_SyncRegistriesToRemoteEnvironments_ReportsFailuresButContinues(t *testing.T) {+ ctx := context.Background()+ db := setupEnvironmentServiceTestDB(t)+ svc := NewEnvironmentService(db, nil, nil, nil, nil)++ createTestRegistry(t, db, "reg-1")++ var successCalls atomic.Int32+ successToken := "token-success"+ successServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ successCalls.Add(1)+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"success":true,"data":{"message":"ok"}}`))+ }))+ defer successServer.Close()++ failingToken := "token-fail"+ createTestEnvironment(t, db, "env-success", successServer.URL, &successToken)+ createTestEnvironment(t, db, "env-fail", "http://127.0.0.1:1", &failingToken)++ err := svc.SyncRegistriesToRemoteEnvironments(ctx)+ require.Error(t, err)+ require.Contains(t, err.Error(), "failed to sync registries to 1 remote environment")+ require.EqualValues(t, 1, successCalls.Load())+}