Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: pass registry credentials to environment (#917)
Chase Baucomd7a112a08ae1
diff --git a/backend/internal/api/container_handler.go b/backend/internal/api/container_handler.goindex af13bc0b1..884adf1a8 100644--- a/backend/internal/api/container_handler.go+++ b/backend/internal/api/container_handler.go@@ -535,6 +535,7 @@ func (h *ContainerHandler) Create(c *gin.Context) {networkingConfig,req.Name,*currentUser,+ req.Credentials,)if err != nil {c.JSON(http.StatusInternalServerError, gin.H{diff --git a/backend/internal/api/container_registry_handler.go b/backend/internal/api/container_registry_handler.goindex 19f04bf0a..2be3d0de8 100644--- a/backend/internal/api/container_registry_handler.go+++ b/backend/internal/api/container_registry_handler.go@@ -28,6 +28,7 @@ func NewContainerRegistryHandler(group *gin.RouterGroup, registryService *servic{apiGroup.GET("", handler.GetRegistries)apiGroup.POST("", handler.CreateRegistry)+ apiGroup.POST("/sync", handler.SyncRegistries)apiGroup.GET("/:id", handler.GetRegistry)apiGroup.PUT("/:id", handler.UpdateRegistry)apiGroup.DELETE("/:id", handler.DeleteRegistry)@@ -200,18 +201,44 @@ func (h *ContainerRegistryHandler) TestRegistry(c *gin.Context) {testResult, err := h.performRegistryTest(c.Request.Context(), registry, decryptedToken)if err != nil {c.JSON(http.StatusOK, gin.H{- "success": true,+ "success": false,"data": gin.H{"message": err.Error()},})return}c.JSON(http.StatusOK, gin.H{- "success": false,+ "success": true,"data": testResult,})}+func (h *ContainerRegistryHandler) SyncRegistries(c *gin.Context) {+ var req dto.SyncRegistriesRequest+ if err := c.ShouldBindJSON(&req); err != nil {+ apiErr := models.NewValidationError("Invalid request data", err)+ c.JSON(apiErr.HTTPStatus(), gin.H{+ "success": false,+ "data": gin.H{"error": apiErr.Message},+ })+ return+ }++ if err := h.registryService.SyncRegistries(c.Request.Context(), req.Registries); err != nil {+ apiErr := models.ToAPIError(err)+ c.JSON(apiErr.HTTPStatus(), gin.H{+ "success": false,+ "data": gin.H{"error": apiErr.Message},+ })+ return+ }++ c.JSON(http.StatusOK, gin.H{+ "success": true,+ "data": gin.H{"message": "Registries synced successfully"},+ })+}+func (h *ContainerRegistryHandler) performRegistryTest(ctx context.Context, registryModel *models.ContainerRegistry, decryptedToken string) (map[string]interface{}, error) {var creds *registry.Credentialsif registryModel.Username != "" && decryptedToken != "" {diff --git a/backend/internal/api/environment_handler.go b/backend/internal/api/environment_handler.goindex 875ea0890..ed6b728f7 100644--- a/backend/internal/api/environment_handler.go+++ b/backend/internal/api/environment_handler.go@@ -52,6 +52,7 @@ func NewEnvironmentHandler(apiGroup.POST("/:id/test", h.TestConnection)apiGroup.POST("/:id/heartbeat", h.UpdateHeartbeat)apiGroup.POST("/:id/agent/pair", h.PairAgent)+ apiGroup.POST("/:id/sync-registries", h.SyncRegistries)}}@@ -107,8 +108,8 @@ func (h *EnvironmentHandler) CreateEnvironment(c *gin.Context) {token, err := h.environmentService.PairAgentWithBootstrap(c.Request.Context(), req.ApiUrl, *req.BootstrapToken)if err != nil {slog.ErrorContext(c.Request.Context(), "Failed to pair with agent",- slog.String("apiUrl", req.ApiUrl),- slog.String("error", err.Error()))+ "apiUrl", req.ApiUrl,+ "error", err.Error())c.JSON(http.StatusBadGateway, gin.H{"success": false,@@ -130,6 +131,23 @@ func (h *EnvironmentHandler) CreateEnvironment(c *gin.Context) {return}+ // Sync registries to the new environment in the background+ if created.AccessToken != nil && *created.AccessToken != "" {+ go func() {+ ctx := context.Background()+ if err := h.environmentService.SyncRegistriesToEnvironment(ctx, created.ID); err != nil {+ slog.WarnContext(ctx, "Failed to sync registries to new environment",+ "environmentID", created.ID,+ "environmentName", created.Name,+ "error", err.Error())+ } else {+ slog.InfoContext(ctx, "Successfully synced registries to new environment",+ "environmentID", created.ID,+ "environmentName", created.Name)+ }+ }()+ }+out, mapErr := dto.MapOne[*models.Environment, dto.EnvironmentDto](created)if mapErr != nil {c.JSON(http.StatusInternalServerError, gin.H{"success": false, "data": gin.H{"error": "Failed to map environment"}})@@ -188,7 +206,33 @@ func (h *EnvironmentHandler) UpdateEnvironment(c *gin.Context) {return}- updates := map[string]interface{}{}+ updates := h.buildUpdateMapInternal(&req, isLocalEnv)++ pairingSucceeded, err := h.handleEnvironmentPairingInternal(c.Request.Context(), environmentID, &req, updates, isLocalEnv)+ if err != nil {+ c.JSON(err.statusCode, gin.H{"success": false, "data": gin.H{"error": err.message}})+ return+ }++ updated, updateErr := h.environmentService.UpdateEnvironment(c.Request.Context(), environmentID, updates)+ if updateErr != nil {+ c.JSON(http.StatusInternalServerError, gin.H{"success": false, "data": gin.H{"error": "Failed to update environment"}})+ return+ }++ h.triggerPostUpdateTasksInternal(environmentID, updated, pairingSucceeded, &req)++ out, mapErr := dto.MapOne[*models.Environment, dto.EnvironmentDto](updated)+ if mapErr != nil {+ c.JSON(http.StatusInternalServerError, gin.H{"success": false, "data": gin.H{"error": "Failed to map environment"}})+ return+ }++ c.JSON(http.StatusOK, gin.H{"success": true, "data": out})+}++func (h *EnvironmentHandler) buildUpdateMapInternal(req *dto.UpdateEnvironmentDto, isLocalEnv bool) map[string]any {+ updates := map[string]any{}// For local environment, only allow nameif !isLocalEnv {@@ -204,35 +248,53 @@ func (h *EnvironmentHandler) UpdateEnvironment(c *gin.Context) {updates["name"] = *req.Name}+ return updates+}++type updateError struct {+ statusCode int+ message string+}++func (h *EnvironmentHandler) handleEnvironmentPairingInternal(ctx context.Context, environmentID string, req *dto.UpdateEnvironmentDto, updates map[string]any, isLocalEnv bool) (bool, *updateError) {+ pairingSucceeded := false+// Local environment cannot be paired or have access token updated- if !isLocalEnv {- // If caller asked to pair (bootstrapToken present) and no accessToken provided in the request,- // resolve apiUrl (current or updated) and let the service pair and persist the token.- if (req.AccessToken == nil) && req.BootstrapToken != nil && *req.BootstrapToken != "" {- current, err := h.environmentService.GetEnvironmentByID(c.Request.Context(), environmentID)- if err != nil || current == nil {- c.JSON(http.StatusNotFound, gin.H{"success": false, "data": gin.H{"error": "Environment not found"}})- return- }- apiUrl := current.ApiUrl- if req.ApiUrl != nil && *req.ApiUrl != "" {- apiUrl = *req.ApiUrl+ if isLocalEnv {+ return pairingSucceeded, nil+ }++ // If caller asked to pair (bootstrapToken present) and no accessToken provided in the request,+ // resolve apiUrl (current or updated) and let the service pair and persist the token.+ if (req.AccessToken == nil) && req.BootstrapToken != nil && *req.BootstrapToken != "" {+ current, err := h.environmentService.GetEnvironmentByID(ctx, environmentID)+ if err != nil || current == nil {+ return false, &updateError{+ statusCode: http.StatusNotFound,+ message: "Environment not found",}- if _, err := h.environmentService.PairAndPersistAgentToken(c.Request.Context(), environmentID, apiUrl, *req.BootstrapToken); err != nil {- c.JSON(http.StatusBadGateway, gin.H{"success": false, "data": gin.H{"error": "Agent pairing failed: " + err.Error()}})- return+ }++ apiUrl := current.ApiUrl+ if req.ApiUrl != nil && *req.ApiUrl != "" {+ apiUrl = *req.ApiUrl+ }++ if _, err := h.environmentService.PairAndPersistAgentToken(ctx, environmentID, apiUrl, *req.BootstrapToken); err != nil {+ return false, &updateError{+ statusCode: http.StatusBadGateway,+ message: "Agent pairing failed: " + err.Error(),}- } else if req.AccessToken != nil {- updates["access_token"] = *req.AccessToken}+ pairingSucceeded = true+ } else if req.AccessToken != nil {+ updates["access_token"] = *req.AccessToken}- updated, err := h.environmentService.UpdateEnvironment(c.Request.Context(), environmentID, updates)- if err != nil {- c.JSON(http.StatusInternalServerError, gin.H{"success": false, "data": gin.H{"error": "Failed to update environment"}})- return- }+ return pairingSucceeded, nil+}+func (h *EnvironmentHandler) triggerPostUpdateTasksInternal(environmentID string, updated *models.Environment, pairingSucceeded bool, req *dto.UpdateEnvironmentDto) {// Trigger health check after update to verify new configuration// This runs in background and doesn't block the responseif updated.Enabled {@@ -247,13 +309,22 @@ func (h *EnvironmentHandler) UpdateEnvironment(c *gin.Context) {}()}- out, mapErr := dto.MapOne[*models.Environment, dto.EnvironmentDto](updated)- if mapErr != nil {- c.JSON(http.StatusInternalServerError, gin.H{"success": false, "data": gin.H{"error": "Failed to map environment"}})- return+ // Sync registries if pairing succeeded or if access token was updated+ if pairingSucceeded || (req.AccessToken != nil && *req.AccessToken != "") {+ go func() {+ ctx := context.Background()+ if err := h.environmentService.SyncRegistriesToEnvironment(ctx, environmentID); err != nil {+ slog.WarnContext(ctx, "Failed to sync registries after environment update",+ "environmentID", environmentID,+ "environmentName", updated.Name,+ "error", err.Error())+ } else {+ slog.InfoContext(ctx, "Successfully synced registries after environment update",+ "environmentID", environmentID,+ "environmentName", updated.Name)+ }+ }()}-- c.JSON(http.StatusOK, gin.H{"success": true, "data": out})}// Delete@@ -323,3 +394,21 @@ func (h *EnvironmentHandler) UpdateHeartbeat(c *gin.Context) {"message": "Heartbeat updated successfully",})}++func (h *EnvironmentHandler) SyncRegistries(c *gin.Context) {+ environmentID := c.Param("id")++ err := h.environmentService.SyncRegistriesToEnvironment(c.Request.Context(), environmentID)+ if err != nil {+ c.JSON(http.StatusInternalServerError, gin.H{+ "success": false,+ "data": gin.H{"error": "Failed to sync registries: " + err.Error()},+ })+ return+ }++ c.JSON(http.StatusOK, gin.H{+ "success": true,+ "data": gin.H{"message": "Registries synced successfully"},+ })+}diff --git a/backend/internal/api/project_handler.go b/backend/internal/api/project_handler.goindex 32884247c..fdb1e37b9 100644--- a/backend/internal/api/project_handler.go+++ b/backend/internal/api/project_handler.go@@ -249,6 +249,11 @@ func (h *ProjectHandler) PullProjectImages(c *gin.Context) {return}+ var req dto.ProjectImagePullDto+ if err := c.ShouldBindJSON(&req); err != nil {+ req = dto.ProjectImagePullDto{}+ }+c.Writer.Header().Set("Content-Type", "application/x-json-stream")c.Writer.Header().Set("Cache-Control", "no-cache")c.Writer.Header().Set("Connection", "keep-alive")@@ -256,7 +261,7 @@ func (h *ProjectHandler) PullProjectImages(c *gin.Context) {_, _ = fmt.Fprintln(c.Writer, `{"status":"starting project image pull"}`)- if err := h.projectService.PullProjectImages(c.Request.Context(), projectID, c.Writer); err != nil {+ if err := h.projectService.PullProjectImages(c.Request.Context(), projectID, c.Writer, req.Credentials); err != nil {_, _ = fmt.Fprintf(c.Writer, `{"error":%q}`+"\n", err.Error())return}diff --git a/backend/internal/bootstrap/services_bootstrap.go b/backend/internal/bootstrap/services_bootstrap.goindex 00e64a0ce..f907756fe 100644--- a/backend/internal/bootstrap/services_bootstrap.go+++ b/backend/internal/bootstrap/services_bootstrap.go@@ -59,7 +59,7 @@ func initializeServices(ctx context.Context, db *database.DB, cfg *config.Configsvcs.Image = services.NewImageService(db, svcs.Docker, svcs.ContainerRegistry, svcs.ImageUpdate, svcs.Event)svcs.Project = services.NewProjectService(db, svcs.Settings, svcs.Event, svcs.Image)svcs.Environment = services.NewEnvironmentService(db, httpClient, svcs.Docker)- svcs.Container = services.NewContainerService(db, svcs.Event, svcs.Docker)+ svcs.Container = services.NewContainerService(db, svcs.Event, svcs.Docker, svcs.Image)svcs.Volume = services.NewVolumeService(db, svcs.Docker, svcs.Event)svcs.Network = services.NewNetworkService(db, svcs.Docker, svcs.Event)svcs.Template = services.NewTemplateService(ctx, db, httpClient, svcs.Settings)diff --git a/backend/internal/dto/container_dto.go b/backend/internal/dto/container_dto.goindex 876d49601..0f1840620 100644--- a/backend/internal/dto/container_dto.go+++ b/backend/internal/dto/container_dto.go@@ -9,21 +9,22 @@ import ()type CreateContainerDto struct {- Name string `json:"name" binding:"required"`- Image string `json:"image" binding:"required"`- Command []string `json:"command,omitempty"`- Entrypoint []string `json:"entrypoint,omitempty"`- WorkingDir string `json:"workingDir,omitempty"`- User string `json:"user,omitempty"`- Environment []string `json:"environment,omitempty"`- Ports map[string]string `json:"ports,omitempty"`- Volumes []string `json:"volumes,omitempty"`- Networks []string `json:"networks,omitempty"`- RestartPolicy string `json:"restartPolicy,omitempty"`- Privileged bool `json:"privileged,omitempty"`- AutoRemove bool `json:"autoRemove,omitempty"`- Memory int64 `json:"memory,omitempty"`- CPUs float64 `json:"cpus,omitempty"`+ Name string `json:"name" binding:"required"`+ Image string `json:"image" binding:"required"`+ Command []string `json:"command,omitempty"`+ Entrypoint []string `json:"entrypoint,omitempty"`+ WorkingDir string `json:"workingDir,omitempty"`+ User string `json:"user,omitempty"`+ Environment []string `json:"environment,omitempty"`+ Ports map[string]string `json:"ports,omitempty"`+ Volumes []string `json:"volumes,omitempty"`+ Networks []string `json:"networks,omitempty"`+ RestartPolicy string `json:"restartPolicy,omitempty"`+ Privileged bool `json:"privileged,omitempty"`+ AutoRemove bool `json:"autoRemove,omitempty"`+ Memory int64 `json:"memory,omitempty"`+ CPUs float64 `json:"cpus,omitempty"`+ Credentials []ContainerRegistryCredential `json:"credentials,omitempty"`}type ContainerStatusLengthsDto struct {diff --git a/backend/internal/dto/container_registry_dto.go b/backend/internal/dto/container_registry_dto.goindex 5dd824273..17c3f0893 100644--- a/backend/internal/dto/container_registry_dto.go+++ b/backend/internal/dto/container_registry_dto.go@@ -13,9 +13,25 @@ type ContainerRegistryDto struct {UpdatedAt time.Time `json:"updatedAt"`}+type ContainerRegistrySyncDto struct {+ ID string `json:"id" binding:"required"`+ URL string `json:"url" binding:"required"`+ Username string `json:"username" binding:"required"`+ Token string `json:"token" binding:"required"`+ Description *string `json:"description,omitempty"`+ Insecure bool `json:"insecure"`+ Enabled bool `json:"enabled"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`+}+type ContainerRegistryCredential struct {URL string `json:"url" binding:"required"`Username string `json:"username" binding:"required"`Token string `json:"token" binding:"required"`Enabled bool `json:"enabled"`}++type SyncRegistriesRequest struct {+ Registries []ContainerRegistrySyncDto `json:"registries" binding:"required"`+}diff --git a/backend/internal/dto/project_dto.go b/backend/internal/dto/project_dto.goindex adfce2fa1..eebb91a5b 100644--- a/backend/internal/dto/project_dto.go+++ b/backend/internal/dto/project_dto.go@@ -63,3 +63,7 @@ type ProjectStatusCounts struct {StoppedProjects int `json:"stoppedProjects"`TotalProjects int `json:"totalProjects"`}++type ProjectImagePullDto struct {+ Credentials []ContainerRegistryCredential `json:"credentials,omitempty"`+}diff --git a/backend/internal/job/environment_health_job.go b/backend/internal/job/environment_health_job.goindex 026f10ecd..bdf2cecb5 100644--- a/backend/internal/job/environment_health_job.go+++ b/backend/internal/job/environment_health_job.go@@ -99,6 +99,22 @@ func (j *EnvironmentHealthJob) Execute(ctx context.Context) error {offlineCount++case status == "online":onlineCount+++ // Sync registries to online remote environments (skip local environment ID "0")+ if env.ID != "0" {+ go func(envID, envName string) {+ syncCtx := context.WithoutCancel(ctx)+ if err := j.environmentService.SyncRegistriesToEnvironment(syncCtx, envID); err != nil {+ slog.WarnContext(syncCtx, "failed to sync registries during health check",+ "environment_id", envID,+ "environment_name", envName,+ "error", err)+ } else {+ slog.DebugContext(syncCtx, "successfully synced registries during health check",+ "environment_id", envID,+ "environment_name", envName)+ }+ }(env.ID, env.Name)+ }default:offlineCount++}diff --git a/backend/internal/middleware/environment_middleware.go b/backend/internal/middleware/environment_middleware.goindex dd6380c6d..7e2f87fd6 100644--- a/backend/internal/middleware/environment_middleware.go+++ b/backend/internal/middleware/environment_middleware.go@@ -175,12 +175,7 @@ func (m *EnvironmentMiddleware) proxyHTTP(c *gin.Context, target string, accessT}func (m *EnvironmentMiddleware) createProxyRequest(c *gin.Context, target string, accessToken *string) (*http.Request, error) {- var bodyReader io.Reader- if c.Request.Body != nil {- bodyReader = c.Request.Body- }-- req, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, target, bodyReader)+ req, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, target, c.Request.Body)if err != nil {return nil, err}@@ -191,14 +186,6 @@ func (m *EnvironmentMiddleware) createProxyRequest(c *gin.Context, target stringremenv.SetAgentToken(req, accessToken)remenv.SetForwardedHeaders(req, c.ClientIP(), c.Request.Host)- if remenv.NeedsCredentialInjection(target) {- if err := remenv.InjectRegistryCredentials(c.Request.Context(), req, m.envService); err != nil {- slog.WarnContext(c.Request.Context(), "Failed to inject registry credentials",- slog.String("error", err.Error()),- slog.String("target", target))- }- }-return req, nil}diff --git a/backend/internal/services/container_registry_service.go b/backend/internal/services/container_registry_service.goindex f0c5893f5..1a93e3bd7 100644--- a/backend/internal/services/container_registry_service.go+++ b/backend/internal/services/container_registry_service.go@@ -112,30 +112,22 @@ func (s *ContainerRegistryService) UpdateRegistry(ctx context.Context, id string}// Update fields- if req.URL != nil {- registry.URL = *req.URL- }- if req.Username != nil {- registry.Username = *req.Username- }+ utils.UpdateIfChanged(®istry.URL, req.URL)+ utils.UpdateIfChanged(®istry.Username, req.Username)+if req.Token != nil && *req.Token != "" {// Encrypt the new tokenencryptedToken, err := utils.Encrypt(*req.Token)if err != nil {return nil, fmt.Errorf("failed to encrypt token: %w", err)}- registry.Token = encryptedToken- }- if req.Description != nil {- registry.Description = req.Description- }- if req.Insecure != nil {- registry.Insecure = *req.Insecure- }- if req.Enabled != nil {- registry.Enabled = *req.Enabled+ utils.UpdateIfChanged(®istry.Token, encryptedToken)}+ utils.UpdateIfChanged(®istry.Description, req.Description)+ utils.UpdateIfChanged(®istry.Insecure, req.Insecure)+ utils.UpdateIfChanged(®istry.Enabled, req.Enabled)+registry.UpdatedAt = time.Now()if err := s.db.WithContext(ctx).Save(registry).Error; err != nil {@@ -175,3 +167,116 @@ func (s *ContainerRegistryService) GetEnabledRegistries(ctx context.Context) ([]}return registries, nil}++// SyncRegistries syncs registries from a manager to this agent instance+// It creates, updates, or deletes registries to match the provided list+func (s *ContainerRegistryService) SyncRegistries(ctx context.Context, syncItems []dto.ContainerRegistrySyncDto) error {+ existingMap, err := s.getExistingRegistriesMapInternal(ctx)+ if err != nil {+ return err+ }++ syncedIDs := make(map[string]bool)++ // Process each sync item+ for _, item := range syncItems {+ syncedIDs[item.ID] = true++ if err := s.processSyncItemInternal(ctx, item, existingMap); err != nil {+ return err+ }+ }++ // Delete registries that are not in the sync list+ return s.deleteUnsyncedInternal(ctx, existingMap, syncedIDs)+}++func (s *ContainerRegistryService) getExistingRegistriesMapInternal(ctx context.Context) (map[string]*models.ContainerRegistry, error) {+ var existingRegistries []models.ContainerRegistry+ if err := s.db.WithContext(ctx).Find(&existingRegistries).Error; err != nil {+ return nil, fmt.Errorf("failed to get existing registries: %w", err)+ }++ existingMap := make(map[string]*models.ContainerRegistry)+ for i := range existingRegistries {+ existingMap[existingRegistries[i].ID] = &existingRegistries[i]+ }++ return existingMap, nil+}++func (s *ContainerRegistryService) processSyncItemInternal(ctx context.Context, item dto.ContainerRegistrySyncDto, existingMap map[string]*models.ContainerRegistry) error {+ existing, exists := existingMap[item.ID]+ if exists {+ return s.updateExistingRegistryInternal(ctx, item, existing)+ }+ return s.createNewRegistryInternal(ctx, item)+}++func (s *ContainerRegistryService) updateExistingRegistryInternal(ctx context.Context, item dto.ContainerRegistrySyncDto, existing *models.ContainerRegistry) error {+ needsUpdate := s.checkRegistryNeedsUpdateInternal(item, existing)++ if needsUpdate {+ existing.UpdatedAt = time.Now()+ if err := s.db.WithContext(ctx).Save(existing).Error; err != nil {+ return fmt.Errorf("failed to update registry %s: %w", item.ID, err)+ }+ }++ return nil+}++func (s *ContainerRegistryService) checkRegistryNeedsUpdateInternal(item dto.ContainerRegistrySyncDto, existing *models.ContainerRegistry) bool {+ needsUpdate := utils.UpdateIfChanged(&existing.URL, item.URL)+ needsUpdate = utils.UpdateIfChanged(&existing.Username, item.Username) || needsUpdate++ // Always update token as it comes decrypted from manager+ encryptedToken, err := utils.Encrypt(item.Token)+ if err == nil {+ needsUpdate = utils.UpdateIfChanged(&existing.Token, encryptedToken) || needsUpdate+ }++ needsUpdate = utils.UpdateIfChanged(&existing.Description, item.Description) || needsUpdate+ needsUpdate = utils.UpdateIfChanged(&existing.Insecure, item.Insecure) || needsUpdate+ needsUpdate = utils.UpdateIfChanged(&existing.Enabled, item.Enabled) || needsUpdate++ return needsUpdate+}++func (s *ContainerRegistryService) createNewRegistryInternal(ctx context.Context, item dto.ContainerRegistrySyncDto) error {+ encryptedToken, err := utils.Encrypt(item.Token)+ if err != nil {+ return fmt.Errorf("failed to encrypt token for new registry %s: %w", item.ID, err)+ }++ newRegistry := &models.ContainerRegistry{+ BaseModel: models.BaseModel{+ ID: item.ID,+ },+ URL: item.URL,+ Username: item.Username,+ Token: encryptedToken,+ Description: item.Description,+ Insecure: item.Insecure,+ Enabled: item.Enabled,+ CreatedAt: time.Now(),+ UpdatedAt: time.Now(),+ }++ if err := s.db.WithContext(ctx).Create(newRegistry).Error; err != nil {+ return fmt.Errorf("failed to create registry %s: %w", item.ID, err)+ }++ return nil+}++func (s *ContainerRegistryService) deleteUnsyncedInternal(ctx context.Context, existingMap map[string]*models.ContainerRegistry, syncedIDs map[string]bool) error {+ for id := range existingMap {+ if !syncedIDs[id] {+ if err := s.db.WithContext(ctx).Where("id = ?", id).Delete(&models.ContainerRegistry{}).Error; err != nil {+ return fmt.Errorf("failed to delete registry %s: %w", id, err)+ }+ }+ }+ return nil+}diff --git a/backend/internal/services/container_service.go b/backend/internal/services/container_service.goindex 48a03bd71..0d123c419 100644--- a/backend/internal/services/container_service.go+++ b/backend/internal/services/container_service.go@@ -7,6 +7,7 @@ import ("errors""fmt""io"+ "log/slog""strings""github.com/docker/docker/api/types/container"@@ -23,10 +24,11 @@ type ContainerService struct {db *database.DBdockerService *DockerClientServiceeventService *EventService+ imageService *ImageService}-func NewContainerService(db *database.DB, eventService *EventService, dockerService *DockerClientService) *ContainerService {- return &ContainerService{db: db, eventService: eventService, dockerService: dockerService}+func NewContainerService(db *database.DB, eventService *EventService, dockerService *DockerClientService, imageService *ImageService) *ContainerService {+ return &ContainerService{db: db, eventService: eventService, dockerService: dockerService, imageService: imageService}}func (s *ContainerService) StartContainer(ctx context.Context, containerID string, user models.User) error {@@ -176,7 +178,7 @@ func (s *ContainerService) DeleteContainer(ctx context.Context, containerID strireturn nil}-func (s *ContainerService) CreateContainer(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string, user models.User) (*container.InspectResponse, error) {+func (s *ContainerService) CreateContainer(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string, user models.User, credentials []dto.ContainerRegistryCredential) (*container.InspectResponse, error) {dockerClient, err := s.dockerService.CreateConnection(ctx)if err != nil {s.eventService.LogErrorEvent(ctx, models.EventTypeContainerError, "container", "", containerName, user.ID, user.Username, "0", err, models.JSON{"action": "create", "image": config.Image})@@ -186,7 +188,16 @@ func (s *ContainerService) CreateContainer(ctx context.Context, config *containe_, err = dockerClient.ImageInspect(ctx, config.Image)if err != nil {- reader, pullErr := dockerClient.ImagePull(ctx, config.Image, image.PullOptions{})+ // Image not found locally, need to pull it+ pullOptions, authErr := s.imageService.getPullOptionsWithAuth(ctx, config.Image, credentials)+ if authErr != nil {+ slog.WarnContext(ctx, "Failed to get registry authentication for container image; proceeding without auth",+ "image", config.Image,+ "error", authErr.Error())+ pullOptions = image.PullOptions{}+ }++ reader, pullErr := dockerClient.ImagePull(ctx, config.Image, pullOptions)if pullErr != nil {s.eventService.LogErrorEvent(ctx, models.EventTypeContainerError, "container", "", containerName, user.ID, user.Username, "0", pullErr, models.JSON{"action": "create", "image": config.Image, "step": "pull_image"})return nil, fmt.Errorf("failed to pull image %s: %w", config.Image, pullErr)diff --git a/backend/internal/services/environment_service.go b/backend/internal/services/environment_service.goindex 44dd6fd60..1faca77b2 100644--- a/backend/internal/services/environment_service.go+++ b/backend/internal/services/environment_service.go@@ -437,3 +437,124 @@ func (s *EnvironmentService) GetEnabledRegistryCredentials(ctx context.Context)return creds, nil}++// SyncRegistriesToEnvironment syncs all registries from this manager to a remote environment+func (s *EnvironmentService) SyncRegistriesToEnvironment(ctx context.Context, environmentID string) error {+ // Get the environment+ environment, err := s.GetEnvironmentByID(ctx, environmentID)+ if err != nil {+ return fmt.Errorf("failed to get environment: %w", err)+ }++ // Don't sync to local environment (ID "0")+ if environmentID == "0" {+ return fmt.Errorf("cannot sync registries to local environment")+ }++ slog.InfoContext(ctx, "Starting registry sync to environment",+ slog.String("environmentID", environmentID),+ slog.String("environmentName", environment.Name),+ slog.String("apiUrl", environment.ApiUrl))++ // Get all registries from this manager+ var registries []models.ContainerRegistry+ if err := s.db.WithContext(ctx).Find(®istries).Error; err != nil {+ return fmt.Errorf("failed to get registries: %w", err)+ }++ slog.InfoContext(ctx, "Found registries to sync",+ slog.Int("count", len(registries)))++ // Prepare sync items with decrypted tokens+ syncItems := make([]dto.ContainerRegistrySyncDto, 0, len(registries))+ for _, reg := range registries {+ decryptedToken, err := utils.Decrypt(reg.Token)+ if err != nil {+ slog.WarnContext(ctx, "Failed to decrypt registry token for sync",+ slog.String("registryID", reg.ID),+ slog.String("registryURL", reg.URL),+ slog.String("error", err.Error()))+ continue+ }++ syncItems = append(syncItems, dto.ContainerRegistrySyncDto{+ ID: reg.ID,+ URL: reg.URL,+ Username: reg.Username,+ Token: decryptedToken,+ Description: reg.Description,+ Insecure: reg.Insecure,+ Enabled: reg.Enabled,+ CreatedAt: reg.CreatedAt,+ UpdatedAt: reg.UpdatedAt,+ })+ }++ // Prepare the sync request+ syncReq := dto.SyncRegistriesRequest{+ Registries: syncItems,+ }++ // Marshal the request+ reqBody, err := json.Marshal(syncReq)+ if err != nil {+ return fmt.Errorf("failed to marshal sync request: %w", err)+ }++ // Send the sync request to the remote environment+ reqCtx, cancel := context.WithTimeout(ctx, 30*time.Second)+ defer cancel()++ targetURL := strings.TrimRight(environment.ApiUrl, "/") + "/api/container-registries/sync"+ req, err := http.NewRequestWithContext(reqCtx, http.MethodPost, targetURL, strings.NewReader(string(reqBody)))+ if err != nil {+ return fmt.Errorf("failed to create sync request: %w", err)+ }++ req.Header.Set("Content-Type", "application/json")+ if environment.AccessToken != nil && *environment.AccessToken != "" {+ req.Header.Set("X-Arcane-Agent-Token", *environment.AccessToken)+ slog.DebugContext(ctx, "Set agent token header for sync request")+ } else {+ slog.WarnContext(ctx, "No access token available for environment sync",+ slog.String("environmentID", environmentID))+ }++ slog.InfoContext(ctx, "Sending sync request to agent",+ slog.String("url", targetURL),+ slog.Int("registryCount", len(syncItems)))++ resp, err := s.httpClient.Do(req)+ if err != nil {+ return fmt.Errorf("failed to send sync request: %w", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode != http.StatusOK {+ body, _ := io.ReadAll(resp.Body)+ slog.ErrorContext(ctx, "Sync request failed",+ slog.Int("statusCode", resp.StatusCode),+ slog.String("response", string(body)))+ return fmt.Errorf("sync request failed with status %d: %s", resp.StatusCode, string(body))+ }++ var result struct {+ Success bool `json:"success"`+ Data struct {+ Message string `json:"message"`+ } `json:"data"`+ }+ if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {+ return fmt.Errorf("failed to decode sync response: %w", err)+ }++ if !result.Success {+ return fmt.Errorf("sync failed: %s", result.Data.Message)+ }++ slog.InfoContext(ctx, "Successfully synced registries to environment",+ slog.String("environmentID", environmentID),+ slog.String("environmentName", environment.Name))++ return nil+}diff --git a/backend/internal/services/project_service.go b/backend/internal/services/project_service.goindex 1efa4ef79..62b0a4022 100644--- a/backend/internal/services/project_service.go+++ b/backend/internal/services/project_service.go@@ -546,7 +546,7 @@ func (s *ProjectService) DeployProject(ctx context.Context, projectID string, usreturn fmt.Errorf("failed to update project status to deploying: %w", err)}- if perr := s.EnsureProjectImagesPresent(ctx, projectID, io.Discard); perr != nil {+ if perr := s.EnsureProjectImagesPresent(ctx, projectID, io.Discard, nil); perr != nil {slog.Warn("ensure images present failed (continuing to compose up)", "projectID", projectID, "error", perr)}@@ -704,7 +704,7 @@ func (s *ProjectService) RedeployProject(ctx context.Context, projectID string,return err}- if err := s.PullProjectImages(ctx, projectID, io.Discard); err != nil {+ if err := s.PullProjectImages(ctx, projectID, io.Discard, nil); err != nil {slog.WarnContext(ctx, "failed to pull project images", "error", err)}@@ -716,7 +716,7 @@ func (s *ProjectService) RedeployProject(ctx context.Context, projectID string,return s.DeployProject(ctx, projectID, systemUser)}-func (s *ProjectService) PullProjectImages(ctx context.Context, projectID string, progressWriter io.Writer) error {+func (s *ProjectService) PullProjectImages(ctx context.Context, projectID string, progressWriter io.Writer, credentials []dto.ContainerRegistryCredential) error {proj, err := s.GetProjectFromDatabaseByID(ctx, projectID)if err != nil {return err@@ -745,7 +745,7 @@ func (s *ProjectService) PullProjectImages(ctx context.Context, projectID string}for img := range images {- if err := s.imageService.PullImage(ctx, img, progressWriter, systemUser, nil); err != nil {+ if err := s.imageService.PullImage(ctx, img, progressWriter, systemUser, credentials); err != nil {return fmt.Errorf("failed to pull image %s: %w", img, err)}}@@ -754,7 +754,7 @@ func (s *ProjectService) PullProjectImages(ctx context.Context, projectID string// EnsureProjectImagesPresent checks all compose service images for the project and// only pulls images that are not already available locally.-func (s *ProjectService) EnsureProjectImagesPresent(ctx context.Context, projectID string, progressWriter io.Writer) error {+func (s *ProjectService) EnsureProjectImagesPresent(ctx context.Context, projectID string, progressWriter io.Writer, credentials []dto.ContainerRegistryCredential) error {proj, err := s.GetProjectFromDatabaseByID(ctx, projectID)if err != nil {return err@@ -792,7 +792,7 @@ func (s *ProjectService) EnsureProjectImagesPresent(ctx context.Context, projectslog.DebugContext(ctx, "image already present locally; skipping pull", "image", img)continue}- if err := s.imageService.PullImage(ctx, img, progressWriter, systemUser, nil); err != nil {+ if err := s.imageService.PullImage(ctx, img, progressWriter, systemUser, credentials); err != nil {return fmt.Errorf("failed to pull missing image %s: %w", img, err)}}diff --git a/backend/internal/utils/remenv/remenv.go b/backend/internal/utils/remenv/remenv.goindex 2307db121..4cc543471 100644--- a/backend/internal/utils/remenv/remenv.go+++ b/backend/internal/utils/remenv/remenv.go@@ -1,24 +1,12 @@package remenvimport (- "bytes"- "context"- "encoding/json"- "fmt"- "io"- "log/slog""net/http"- "strconv""strings""github.com/gin-gonic/gin"- "github.com/ofkm/arcane-backend/internal/dto")-type CredentialInjector interface {- GetEnabledRegistryCredentials(ctx context.Context) ([]dto.ContainerRegistryCredential, error)-}-func CopyRequestHeaders(from http.Header, to http.Header, skip map[string]struct{}) {for k, vs := range from {ck := http.CanonicalHeaderKey(k)@@ -90,110 +78,6 @@ func CopyResponseHeaders(from http.Header, to http.Header, hop map[string]struct}}-func NeedsCredentialInjection(target string) bool {- return strings.Contains(target, "/image-updates/check") ||- strings.Contains(target, "/images/pull")-}--func InjectRegistryCredentials(ctx context.Context, req *http.Request, injector CredentialInjector) error {- if injector == nil || req.Method != http.MethodPost {- return nil- }-- bodyBytes, err := io.ReadAll(req.Body)- if err != nil {- return fmt.Errorf("failed to read request body: %w", err)- }- req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))-- var pullReq dto.ImagePullDto- if err := json.Unmarshal(bodyBytes, &pullReq); err != nil {- return injectBatchUpdateCredentials(ctx, bodyBytes, req, injector)- }-- return injectImagePullCredentials(ctx, pullReq, req, injector)-}--func injectBatchUpdateCredentials(ctx context.Context, bodyBytes []byte, req *http.Request, injector CredentialInjector) error {- var batchReq dto.BatchImageUpdateRequest-- if err := json.Unmarshal(bodyBytes, &batchReq); err != nil {- return nil //nolint:nilerr- }-- slog.DebugContext(ctx, "Batch update credential injection check",- slog.Int("existingCredentials", len(batchReq.Credentials)),- slog.Any("imageRefs", batchReq.ImageRefs))-- if len(batchReq.Credentials) > 0 {- slog.DebugContext(ctx, "Credentials already present in request, skipping injection")- return nil- }-- creds, err := injector.GetEnabledRegistryCredentials(ctx)- if err != nil {- return fmt.Errorf("failed to load registry credentials: %w", err)- }-- if len(creds) == 0 {- slog.DebugContext(ctx, "No enabled registry credentials found to inject")- return nil- }-- batchReq.Credentials = creds- modifiedBody, err := json.Marshal(batchReq)- if err != nil {- return fmt.Errorf("failed to marshal modified request: %w", err)- }-- updateRequestBody(req, modifiedBody)- slog.InfoContext(ctx, "Injected registry credentials into batch update request",- slog.Int("credentialCount", len(creds)),- slog.Any("credentialURLs", func() []string {- urls := make([]string, len(creds))- for i, c := range creds {- urls[i] = c.URL- }- return urls- }()))-- return nil-}--func injectImagePullCredentials(ctx context.Context, pullReq dto.ImagePullDto, req *http.Request, injector CredentialInjector) error {- if len(pullReq.Credentials) > 0 {- return nil- }-- creds, err := injector.GetEnabledRegistryCredentials(ctx)- if err != nil {- return err- }-- if len(creds) == 0 {- return nil- }-- pullReq.Credentials = creds- modifiedBody, err := json.Marshal(pullReq)- if err != nil {- return fmt.Errorf("failed to marshal modified request: %w", err)- }-- updateRequestBody(req, modifiedBody)- slog.DebugContext(ctx, "Injected registry credentials into image pull request",- slog.Int("credentialCount", len(creds)),- slog.String("imageName", pullReq.ImageName))-- return nil-}--func updateRequestBody(req *http.Request, body []byte) {- req.Body = io.NopCloser(bytes.NewBuffer(body))- req.ContentLength = int64(len(body))- req.Header.Set("Content-Length", strconv.Itoa(len(body)))-}-func GetSkipHeaders() map[string]struct{} {return map[string]struct{}{"Host": {}, "Connection": {}, "Keep-Alive": {}, "Proxy-Authenticate": {},diff --git a/backend/internal/utils/update_value_util.go b/backend/internal/utils/update_value_util.gonew file mode 100644index 000000000..62612e7f1--- /dev/null+++ b/backend/internal/utils/update_value_util.go@@ -0,0 +1,43 @@+package utils++// UpdateIfChanged updates the target value if it differs from the new value.+// It returns true if an update occurred.+// Supported types: *string, *bool, **string.+// For *string and *bool targets, if the value is a pointer of the same type,+// the update only happens if the value pointer is not nil.+func UpdateIfChanged(target interface{}, value interface{}) bool {+ switch t := target.(type) {+ case *string:+ if v, ok := value.(string); ok {+ if *t != v {+ *t = v+ return true+ }+ } else if v, ok := value.(*string); ok && v != nil {+ if *t != *v {+ *t = *v+ return true+ }+ }+ case *bool:+ if v, ok := value.(bool); ok {+ if *t != v {+ *t = v+ return true+ }+ } else if v, ok := value.(*bool); ok && v != nil {+ if *t != *v {+ *t = *v+ return true+ }+ }+ case **string:+ if v, ok := value.(*string); ok {+ if (*t == nil && v != nil) || (*t != nil && v == nil) || (*t != nil && v != nil && **t != *v) {+ *t = v+ return true+ }+ }+ }+ return false+}diff --git a/backend/internal/utils/update_value_util_test.go b/backend/internal/utils/update_value_util_test.gonew file mode 100644index 000000000..264f9d991--- /dev/null+++ b/backend/internal/utils/update_value_util_test.go@@ -0,0 +1,99 @@+package utils++import (+ "testing"++ "github.com/stretchr/testify/assert"+)++func TestUpdateIfChanged(t *testing.T) {+ t.Run("string target with string value", func(t *testing.T) {+ target := "initial"+ updated := UpdateIfChanged(&target, "new")+ assert.True(t, updated)+ assert.Equal(t, "new", target)++ updated = UpdateIfChanged(&target, "new")+ assert.False(t, updated)+ assert.Equal(t, "new", target)+ })++ t.Run("string target with *string value", func(t *testing.T) {+ target := "initial"+ newValue := "new"+ updated := UpdateIfChanged(&target, &newValue)+ assert.True(t, updated)+ assert.Equal(t, "new", target)++ updated = UpdateIfChanged(&target, &newValue)+ assert.False(t, updated)+ assert.Equal(t, "new", target)++ var nilValue *string+ updated = UpdateIfChanged(&target, nilValue)+ assert.False(t, updated)+ assert.Equal(t, "new", target)+ })++ t.Run("bool target with bool value", func(t *testing.T) {+ target := false+ updated := UpdateIfChanged(&target, true)+ assert.True(t, updated)+ assert.True(t, target)++ updated = UpdateIfChanged(&target, true)+ assert.False(t, updated)+ assert.True(t, target)+ })++ t.Run("bool target with *bool value", func(t *testing.T) {+ target := false+ newValue := true+ updated := UpdateIfChanged(&target, &newValue)+ assert.True(t, updated)+ assert.True(t, target)++ updated = UpdateIfChanged(&target, &newValue)+ assert.False(t, updated)+ assert.True(t, target)++ var nilValue *bool+ updated = UpdateIfChanged(&target, nilValue)+ assert.False(t, updated)+ assert.True(t, target)+ })++ t.Run("*string target with *string value", func(t *testing.T) {+ initial := "initial"+ target := &initial+ newValue := "new"++ // Update from value to new value+ updated := UpdateIfChanged(&target, &newValue)+ assert.True(t, updated)+ assert.Equal(t, "new", *target)++ // Update with same value+ updated = UpdateIfChanged(&target, &newValue)+ assert.False(t, updated)+ assert.Equal(t, "new", *target)++ // Update to nil+ var nilValue *string+ updated = UpdateIfChanged(&target, nilValue)+ assert.True(t, updated)+ assert.Nil(t, target)++ // Update from nil to value+ updated = UpdateIfChanged(&target, &newValue)+ assert.True(t, updated)+ assert.Equal(t, "new", *target)+ })++ t.Run("unsupported types", func(t *testing.T) {+ target := 10+ updated := UpdateIfChanged(&target, 20)+ assert.False(t, updated)+ assert.Equal(t, 10, target)+ })+}diff --git a/frontend/messages/en.json b/frontend/messages/en.jsonindex b2b50ac76..7dca5393b 100644--- a/frontend/messages/en.json+++ b/frontend/messages/en.json@@ -48,6 +48,7 @@"common_save_changes": "Save Changes","common_remove_selected": "Remove Selected","common_registry_url_required": "Registry URL is required",+ "sync_registries": "Sync Registries","common_update_available": "Update available","common_restart_policy": "Restart Policy","common_gateway": "Gateway",diff --git a/frontend/src/lib/services/env-mgmt-service.ts b/frontend/src/lib/services/env-mgmt-service.tsindex 73def4448..86a046c81 100644--- a/frontend/src/lib/services/env-mgmt-service.ts+++ b/frontend/src/lib/services/env-mgmt-service.ts@@ -34,6 +34,10 @@ export default class EnvironmentManagementService extends BaseAPIService {const res = await this.api.post(`/environments/${environmentId}/test`, apiUrl ? { apiUrl } : undefined);return res.data.data as { status: 'online' | 'offline'; message?: string };}++ async syncRegistries(environmentId: string): Promise<void> {+ await this.api.post(`/environments/${environmentId}/sync-registries`);+ }}export const environmentManagementService = new EnvironmentManagementService();diff --git a/frontend/src/routes/environments/[id]/+page.svelte b/frontend/src/routes/environments/[id]/+page.svelteindex 7cfd34f54..99d536a2e 100644--- a/frontend/src/routes/environments/[id]/+page.svelte+++ b/frontend/src/routes/environments/[id]/+page.svelte@@ -10,6 +10,7 @@import SettingsIcon from '@lucide/svelte/icons/settings';import GlobeIcon from '@lucide/svelte/icons/globe';import SaveIcon from '@lucide/svelte/icons/save';+ import DatabaseIcon from '@lucide/svelte/icons/database';import RotateCcwIcon from '@lucide/svelte/icons/rotate-ccw';import AlertTriangleIcon from '@lucide/svelte/icons/alert-triangle';import { goto, invalidateAll } from '$app/navigation';@@ -22,6 +23,7 @@import { m } from '$lib/paraglide/messages';import { environmentManagementService } from '$lib/services/env-mgmt-service.js';import { environmentStore } from '$lib/stores/environment.store.svelte';+ import Spinner from '$lib/components/ui/spinner/spinner.svelte';let { data } = $props();let { environment, settings } = $derived(data);@@ -34,6 +36,7 @@let isTestingConnection = $state(false);let isPairing = $state(false);let isSaving = $state(false);+ let isSyncingRegistries = $state(false);let bootstrapToken = $state('');// Form state@@ -77,6 +80,20 @@}}+ async function syncRegistries() {+ if (isSyncingRegistries) return;+ try {+ isSyncingRegistries = true;+ await environmentManagementService.syncRegistries(environment.id);+ toast.success('Registries synced successfully');+ } catch (error) {+ console.error('Failed to sync registries:', error);+ toast.error('Failed to sync registries');+ } finally {+ isSyncingRegistries = false;+ }+ }+async function testConnection() {if (isTestingConnection) return;try {@@ -214,6 +231,17 @@{/if}</Button>+ {#if environment.id !== '0'}+ <Button variant="outline" onclick={syncRegistries} disabled={isSyncingRegistries}>+ {#if isSyncingRegistries}+ <Spinner />+ {:else}+ <DatabaseIcon class="mr-2 size-4" />+ {/if}+ {m.sync_registries()}+ </Button>+ {/if}+<Button variant="outline" onclick={refreshEnvironment} disabled={isRefreshing}>{#if isRefreshing}<RefreshCwIcon class="mr-2 size-4 animate-spin" />