Skip to content

Modern Docker Management, Designed for Everyone

fix: forward registry auth when deploying a Swarm stack from Git Sync or a source edit (#3787)

Andrei Lavrenov1570a1da7a00
diff --git a/backend/internal/gitops/gitops_sync.go b/backend/internal/gitops/gitops_sync.go
index 901720089..4507bf62d 100644
--- a/backend/internal/gitops/gitops_sync.go
+++ b/backend/internal/gitops/gitops_sync.go
@@ -1139,6 +1139,24 @@ func entriesNeedDirectorySyncInternal(entries []string, allowed map[string]struc
return false
}
+// buildSwarmStackDeployRequestInternal assembles the deploy request for a Git Sync that targets
+// a Swarm stack. WithRegistryAuth is always set so the Git Sync path resolves stored registry
+// credentials the same way a direct stack deploy does. Resolution happens per image at deploy
+// time and yields nothing unless a configured container registry matches that image's host, so
+// stacks built only from public images are unaffected.
+func buildSwarmStackDeployRequestInternal(sync *projectpkg.GitOpsSync, source *preparedSyncSource, overrideContent string, envContent string, swarmFiles []swarmtypes.SyncFile) swarmtypes.StackDeployRequest {
+ return swarmtypes.StackDeployRequest{
+ Name: sync.ProjectName,
+ ComposeContent: source.composeContent,
+ OverrideContent: overrideContent,
+ EnvContent: envContent,
+ Files: swarmFiles,
+ Prune: true,
+ WithRegistryAuth: true,
+ WorkingDir: filepath.Dir(filepath.Join(source.repoPath, sync.ComposePath)),
+ }
+}
+
// performSwarmStackSyncInternal executes a single file sync targeted at a Swarm Stack
func (s *GitOpsSyncService) performSwarmStackSyncInternal(ctx context.Context, sync *projectpkg.GitOpsSync, id string, actor common.User, result *gitops.SyncResult, source *preparedSyncSource) (*gitops.SyncResult, error) {
slog.InfoContext(ctx, "Deploying Swarm Stack from GitOps sync", "syncId", id, "stackName", sync.ProjectName)
@@ -1176,15 +1194,7 @@ func (s *GitOpsSyncService) performSwarmStackSyncInternal(ctx context.Context, s
syncedFiles = append(syncedFiles, f.RelativePath)
}
- req := swarmtypes.StackDeployRequest{
- Name: sync.ProjectName,
- ComposeContent: source.composeContent,
- OverrideContent: overrideContent,
- EnvContent: envContent,
- Files: swarmFiles,
- Prune: true,
- WorkingDir: filepath.Dir(filepath.Join(source.repoPath, sync.ComposePath)),
- }
+ req := buildSwarmStackDeployRequestInternal(sync, source, overrideContent, envContent, swarmFiles)
if _, err := s.swarmService.DeployStack(ctx, sync.EnvironmentID, req); err != nil {
return result, s.failSync(ctx, id, result, sync, actor, "Failed to deploy swarm stack", err.Error())
diff --git a/backend/internal/gitops/service_test.go b/backend/internal/gitops/service_test.go
index 172a25b7f..ac64e4426 100644
--- a/backend/internal/gitops/service_test.go
+++ b/backend/internal/gitops/service_test.go
@@ -26,6 +26,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/entityjobs"
"github.com/getarcaneapp/arcane/types/v2/gitops"
schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler"
+ swarmtypes "github.com/getarcaneapp/arcane/types/v2/swarm"
"github.com/libtnb/sqlite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -1388,6 +1389,78 @@ func TestGitOpsSyncService_GetOrCreateProjectInternal_FailsWhenBoundProjectMissi
assert.Contains(t, testScheduler.removed, entityjobs.GitOpsSyncJobPrefix+sync.ID)
}
+// TestBuildSwarmStackDeployRequestInternal guards the Git Sync swarm deploy request.
+// WithRegistryAuth must always be set: swarm tasks pull with only the auth embedded in
+// the service spec, so a request without it makes every private image fail to pull.
+func TestBuildSwarmStackDeployRequestInternal(t *testing.T) {
+ t.Parallel()
+
+ files := []swarmtypes.SyncFile{{RelativePath: "configs/app.conf", Content: []byte("k=v")}}
+
+ cases := []struct {
+ name string
+ sync *projectpkg.GitOpsSync
+ source *preparedSyncSource
+ overrideContent string
+ envContent string
+ files []swarmtypes.SyncFile
+ want swarmtypes.StackDeployRequest
+ }{
+ {
+ name: "populates all fields and enables registry auth",
+ sync: &projectpkg.GitOpsSync{ProjectName: "descent", ComposePath: "deploy/swarm/compose.yaml"},
+ source: &preparedSyncSource{repoPath: "/tmp/repo", composeContent: "services: {}\n"},
+ overrideContent: "override",
+ envContent: "A=1",
+ files: files,
+ want: swarmtypes.StackDeployRequest{
+ Name: "descent",
+ ComposeContent: "services: {}\n",
+ OverrideContent: "override",
+ EnvContent: "A=1",
+ Files: files,
+ Prune: true,
+ WithRegistryAuth: true,
+ WorkingDir: filepath.Join("/tmp/repo", "deploy/swarm"),
+ },
+ },
+ {
+ name: "enables registry auth with empty optional content and a root-level compose path",
+ sync: &projectpkg.GitOpsSync{ProjectName: "ptest", ComposePath: "compose.yaml"},
+ source: &preparedSyncSource{repoPath: "/tmp/repo", composeContent: "services: {}\n"},
+ want: swarmtypes.StackDeployRequest{
+ Name: "ptest",
+ ComposeContent: "services: {}\n",
+ Prune: true,
+ WithRegistryAuth: true,
+ WorkingDir: "/tmp/repo",
+ },
+ },
+ {
+ name: "passes an empty file list through unchanged",
+ sync: &projectpkg.GitOpsSync{ProjectName: "ptest", ComposePath: "stacks/compose.yaml"},
+ source: &preparedSyncSource{repoPath: "/tmp/repo", composeContent: "services: {}\n"},
+ // performSwarmStackSyncInternal always passes a non-nil slice; the builder must not reshape it.
+ files: []swarmtypes.SyncFile{},
+ want: swarmtypes.StackDeployRequest{
+ Name: "ptest",
+ ComposeContent: "services: {}\n",
+ Files: []swarmtypes.SyncFile{},
+ Prune: true,
+ WithRegistryAuth: true,
+ WorkingDir: filepath.Join("/tmp/repo", "stacks"),
+ },
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got := buildSwarmStackDeployRequestInternal(tc.sync, tc.source, tc.overrideContent, tc.envContent, tc.files)
+ assert.Equal(t, tc.want, got)
+ })
+ }
+}
+
func TestEnvContentChangedInternal(t *testing.T) {
t.Run("ignores formatting-only changes", func(t *testing.T) {
oldEnv := "B=2\nA=1\n# comment\n"
diff --git a/backend/internal/swarm/service_test.go b/backend/internal/swarm/service_test.go
index 1c731bb6b..044b848a3 100644
--- a/backend/internal/swarm/service_test.go
+++ b/backend/internal/swarm/service_test.go
@@ -147,16 +147,22 @@ func TestSwarmService_FetchSwarmNodeIdentityViaEdgeInternal_UsesEnvironmentAcces
require.True(t, identity.SwarmActive)
}
-func stubStackSourceUpdateDeployInternal(t *testing.T) *int {
+type stackSourceDeployRecorder struct {
+ calls int
+ lastReq swarmtypes.StackDeployRequest
+}
+
+func stubStackSourceUpdateDeployInternal(t *testing.T) *stackSourceDeployRecorder {
t.Helper()
original := deployStackAfterSourceUpdateInternal
t.Cleanup(func() { deployStackAfterSourceUpdateInternal = original })
- calls := 0
+ rec := &stackSourceDeployRecorder{}
deployStackAfterSourceUpdateInternal = func(_ *SwarmService, _ context.Context, _ string, req swarmtypes.StackDeployRequest) (*swarmtypes.StackDeployResponse, error) {
- calls++
+ rec.calls++
+ rec.lastReq = req
return &swarmtypes.StackDeployResponse{Name: req.Name}, nil
}
- return &calls
+ return rec
}
func TestSwarmService_UpdateAndGetStackSource_UsesStoredFilesWithoutSwarmManager(t *testing.T) {
@@ -169,7 +175,7 @@ func TestSwarmService_UpdateAndGetStackSource_UsesStoredFilesWithoutSwarmManager
require.NoError(t, err)
svc := NewSwarmService(nil, settingsSvc, nil, nil, nil)
- deployCalls := stubStackSourceUpdateDeployInternal(t)
+ deployRec := stubStackSourceUpdateDeployInternal(t)
updated, err := svc.UpdateStackSource(ctx, "0", "demo-stack", swarmtypes.StackSourceUpdateRequest{
ComposeContent: "services:\n web:\n image: nginx:alpine\n",
@@ -178,7 +184,10 @@ func TestSwarmService_UpdateAndGetStackSource_UsesStoredFilesWithoutSwarmManager
require.NoError(t, err)
require.Equal(t, "demo-stack", updated.Name)
// Saving stack source must trigger an actual stack deploy (#3463).
- require.Equal(t, 1, *deployCalls)
+ require.Equal(t, 1, deployRec.calls)
+ // The edit-path redeploy must carry registry auth, the same as Git Sync (#3778):
+ // a service added in the edit has no previous spec to fall back on.
+ require.True(t, deployRec.lastReq.WithRegistryAuth)
composePath := filepath.Join(rootDir, "0", "demo-stack", "compose.yaml")
envPath := filepath.Join(rootDir, "0", "demo-stack", ".env")
diff --git a/backend/internal/swarm/swarm.go b/backend/internal/swarm/swarm.go
index 2f8ac0efb..9723db419 100644
--- a/backend/internal/swarm/swarm.go
+++ b/backend/internal/swarm/swarm.go
@@ -1846,13 +1846,18 @@ func (s *SwarmService) UpdateStackSource(ctx context.Context, environmentID, sta
// push the updated spec to the running services so the edit takes effect.
// The saved source is the full stack spec, so services removed from it
// must also be removed from the swarm.
+ // WithRegistryAuth is always set, the same as the Git Sync deploy path:
+ // resolution is per image and yields nothing unless a configured container
+ // registry matches the image host. Without it, a service added in the edit
+ // has no previous spec to fall back on and private images fail to pull.
if _, err := deployStackAfterSourceUpdateInternal(s, ctx, environmentID, swarmtypes.StackDeployRequest{
- Name: stackName,
- ComposeContent: req.ComposeContent,
- OverrideContent: req.OverrideContent,
- EnvContent: req.EnvContent,
- Files: req.Files,
- Prune: true,
+ Name: stackName,
+ ComposeContent: req.ComposeContent,
+ OverrideContent: req.OverrideContent,
+ EnvContent: req.EnvContent,
+ Files: req.Files,
+ Prune: true,
+ WithRegistryAuth: true,
}); err != nil {
// Roll back the persisted source so reads never return an edit that
// was never successfully deployed.