Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: handle .env directories in project scan, env saves and workspace (#4213)
Mczubi95ff124a7da4
diff --git a/backend/internal/project/project_sync.go b/backend/internal/project/project_sync.goindex 0f913d334..97b364866 100644--- a/backend/internal/project/project_sync.go+++ b/backend/internal/project/project_sync.go@@ -554,6 +554,18 @@ func persistEffectiveEnvContentInternal(ctx context.Context, projectPath, projecreturn errors.WrapIf(err, "read project env state")}+ // WriteManagedEnvFile skips unreadable paths so git sync keeps working; an+ // explicit env save would then be dropped without any feedback.+ targets := []string{projects.EffectiveEnvFileName}+ if state.HasGitSource {+ targets = append(targets, projects.OverrideEnvFileName)+ }+ for _, name := range targets {+ if info, statErr := os.Stat(filepath.Join(projectPath, name)); statErr == nil && info.IsDir() {+ return errors.Errorf("cannot save environment: %s is a directory", name)+ }+ }+if state.HasGitSource && state.HasEffective && envContent == state.EffectiveContent {storedEffectiveContent, buildErr := projects.BuildEffectiveEnvContent(state.GitContent, state.OverrideContent)if buildErr == nil && envContent == storedEffectiveContent {diff --git a/backend/internal/project/service_unix_test.go b/backend/internal/project/service_unix_test.goindex fee955d68..6815d9a6b 100644--- a/backend/internal/project/service_unix_test.go+++ b/backend/internal/project/service_unix_test.go@@ -13,6 +13,7 @@ import (composetypes "github.com/compose-spec/compose-go/v2/types""github.com/getarcaneapp/arcane/backend/v2/internal/config""github.com/getarcaneapp/arcane/backend/v2/internal/event"+ projecttypes "github.com/getarcaneapp/arcane/types/v2/project""github.com/stretchr/testify/assert""github.com/stretchr/testify/require")@@ -97,3 +98,106 @@ func TestPrepareProjectBindDirectoriesInternal_PermissionFailure(t *testing.T) {assert.Contains(t, err.Error(), "service app")assert.NoDirExists(t, filepath.Join(locked, "conf"))}++func newEnvDirectoryProjectInternal(t *testing.T, id string) (*ProjectService, *Project, string, context.Context) {+ t.Helper()++ db := setupProjectTestDB(t)+ ctx := context.Background()++ projectsDir := t.TempDir()+ t.Setenv("PROJECTS_DIRECTORY", projectsDir)++ settingsService, err := newSettingsServiceForTestInternal(t, ctx, db)+ require.NoError(t, err)+ require.NoError(t, settingsService.SetStringSetting(ctx, "projectsDirectory", projectsDir))++ svc := NewProjectService(db, settingsService, event.NewEventService(db, nil, nil), nil, nil, nil, nil, nil, config.Load())++ projectPath := createComposeProjectDir(t, projectsDir, id)+ envDir := filepath.Join(projectPath, ".env")+ require.NoError(t, os.Mkdir(envDir, 0o755))+ require.NoError(t, os.WriteFile(filepath.Join(envDir, "keep"), []byte("x"), 0o644))++ project := &Project{+ ID: "proj-" + id,+ Name: id,+ DirName: new(id),+ Path: projectPath,+ Status: ProjectStatusStopped,+ }+ require.NoError(t, db.Create(project).Error)++ return svc, project, envDir, ctx+}++func assertEnvDirectoryIntactInternal(t *testing.T, envDir string) {+ t.Helper()+ info, err := os.Stat(envDir)+ require.NoError(t, err)+ assert.True(t, info.IsDir())+ assert.FileExists(t, filepath.Join(envDir, "keep"))+}++func TestProjectService_EnvDirectory_SyncAndDetails(t *testing.T) {+ svc, project, envDir, ctx := newEnvDirectoryProjectInternal(t, "env-dir-scan")++ require.NoError(t, svc.SyncProjectsFromFileSystem(ctx))+ all, err := svc.ListAllProjects(ctx)+ require.NoError(t, err)+ require.Len(t, all, 1)+ assert.Equal(t, project.Path, all[0].Path)++ details, err := svc.GetProjectDetails(ctx, project.ID, projecttypes.AllDetails())+ require.NoError(t, err)+ assert.Empty(t, details.EnvContent)+ assert.Contains(t, details.ComposeContent, "nginx:alpine")++ assertEnvDirectoryIntactInternal(t, envDir)+}++func TestProjectService_EnvDirectory_UpdateCompose(t *testing.T) {+ svc, project, envDir, ctx := newEnvDirectoryProjectInternal(t, "env-dir-update")++ _, err := svc.UpdateProject(ctx, project.ID, nil, new("services:\n app:\n image: nginx:1.27-alpine\n"), nil, nil, common.User{ID: "u1", Username: "tester"})+ require.NoError(t, err)++ composeBytes, err := os.ReadFile(filepath.Join(project.Path, "compose.yaml"))+ require.NoError(t, err)+ assert.Contains(t, string(composeBytes), "nginx:1.27-alpine")+ assertEnvDirectoryIntactInternal(t, envDir)+}++func TestProjectService_EnvDirectory_GitSync(t *testing.T) {+ svc, project, envDir, ctx := newEnvDirectoryProjectInternal(t, "env-dir-git")++ _, _, err := svc.ApplyGitSyncProjectFiles(ctx, project.ID, "services:\n app:\n image: nginx:1.27-alpine\n", new("FOO=fromgit\n"), nil, "", common.User{ID: "u1", Username: "tester"})+ require.NoError(t, err)++ composeBytes, err := os.ReadFile(filepath.Join(project.Path, "compose.yaml"))+ require.NoError(t, err)+ assert.Contains(t, string(composeBytes), "nginx:1.27-alpine")+ assertEnvDirectoryIntactInternal(t, envDir)+}++func TestProjectService_EnvDirectory_EnvSaveFails(t *testing.T) {+ svc, project, envDir, ctx := newEnvDirectoryProjectInternal(t, "env-dir-save")++ _, err := svc.UpdateProject(ctx, project.ID, nil, nil, new("FOO=bar\n"), nil, common.User{ID: "u1", Username: "tester"})+ require.ErrorContains(t, err, ".env is a directory")+ assertEnvDirectoryIntactInternal(t, envDir)+}++func TestProjectService_OverrideEnvDirectory_EnvSaveFails(t *testing.T) {+ svc, project, envDir, ctx := newEnvDirectoryProjectInternal(t, "override-dir-save")+ require.NoError(t, os.RemoveAll(envDir))+ require.NoError(t, os.WriteFile(filepath.Join(project.Path, ".env.git"), []byte("FOO=git\n"), 0o644))+ overrideDir := filepath.Join(project.Path, "project.env")+ require.NoError(t, os.Mkdir(overrideDir, 0o755))++ _, err := svc.UpdateProject(ctx, project.ID, nil, nil, new("FOO=local\n"), nil, common.User{ID: "u1", Username: "tester"})+ require.ErrorContains(t, err, "project.env is a directory")+ info, err := os.Stat(overrideDir)+ require.NoError(t, err)+ assert.True(t, info.IsDir())+}diff --git a/backend/internal/project/workspace.go b/backend/internal/project/workspace.goindex fefab2264..54d8f7837 100644--- a/backend/internal/project/workspace.go+++ b/backend/internal/project/workspace.go@@ -199,7 +199,7 @@ func (s *ProjectService) resolveProjectWorkspacePathInternal(ctx context.ContextcomposeFileName = filepath.Base(composeFile)}rootName, _, _ := strings.Cut(rel, "/")- protected := projects.ProtectedProjectFilePaths(composeFileName)+ protected := projects.ProtectedProjectPaths(proj.Path, composeFileName)if protected[rel] || protected[rootName] {return nil, "", "", acfstypes.Entry{}, common.Classify(common.ErrProjectWorkspaceForbidden, errors.New("project configuration is not part of the workspace"))}diff --git a/backend/pkg/projects/env.go b/backend/pkg/projects/env.goindex 5d8ebd2e2..7ac74b51a 100644--- a/backend/pkg/projects/env.go+++ b/backend/pkg/projects/env.go@@ -12,6 +12,7 @@ import ("sort""strconv""strings"+ "syscall""time""emperror.dev/errors"@@ -303,7 +304,8 @@ func parseProjectEnvFileExistingInternal(path string, contextEnv EnvMap) (EnvMap// Stays on os.*: env files may be symlinks resolving outside any confinement// root (a supported setup), which acfs cannot follow.func ParseProjectEnvFile(path string, contextEnv EnvMap) (EnvMap, error) {- if _, err := os.Stat(path); err != nil {+ info, err := os.Stat(path)+ if err != nil || info.IsDir() {return nil, nil //nolint:nilerr // missing .env is not an error}return parseProjectEnvFileExistingInternal(path, contextEnv)@@ -336,14 +338,14 @@ func WithTransientValidationEnvFile(ctx context.Context, projectPath string, efforiginalContent, readErr := os.ReadFile(filepath.Join(projectPath, ".env"))originalExists := readErr == nilif readErr != nil && !os.IsNotExist(readErr) {- if !errors.Is(readErr, os.ErrPermission) {+ if !errors.Is(readErr, os.ErrPermission) && !errors.Is(readErr, syscall.EISDIR) {return errors.WrapIf(readErr, "prepare env file for compose validation")}- // The file exists but is permission-locked (e.g. chmod 000, foreign-owned).+ // The path exists but is permission-locked (e.g. chmod 000, foreign-owned) or a directory.// Its contents can't be verified or safely overwritten, so leave it// untouched and validate against whatever's already on disk instead of// aborting the whole update.- slog.Warn("skipping permission-locked .env file during compose validation; leaving it untouched", "projectPath", projectPath)+ slog.Warn("skipping unreadable .env during compose validation; leaving it untouched", "projectPath", projectPath, "error", readErr)if run == nil {return nil}@@ -644,7 +646,7 @@ func ReadProjectEnvState(projectPath string) (ProjectEnvState, error) {}if effectiveUnreadable || gitSourceUnreadable || overrideUnreadable {- slog.Warn("skipping permission-locked project env file(s); leaving them untouched",+ slog.Warn("skipping unreadable project env file(s); leaving them untouched","projectPath", projectPath,"effectiveUnreadable", effectiveUnreadable,"gitSourceUnreadable", gitSourceUnreadable,@@ -690,13 +692,13 @@ func ReadProjectEnvState(projectPath string) (ProjectEnvState, error) {// WriteManagedEnvFile writes (or, for project.env, removes) one of the three// env-merge bookkeeping files — fileName must be EffectiveEnvFileName,-// GitSourceEnvFileName, or OverrideEnvFileName. If the existing file is-// permission-locked, the write is skipped and a warning logged instead: its-// contents can't be verified, and a locked file is typically unwritable too,-// so attempting the write would abort the whole caller.+// GitSourceEnvFileName, or OverrideEnvFileName. If the existing path is+// unreadable (permission-locked or a directory), the write is skipped and a+// warning logged instead: its contents can't be verified, and such a path is+// typically unwritable too, so attempting the write would abort the whole caller.func WriteManagedEnvFile(ctx context.Context, projectsDirectory, projectPath, fileName string, unreadable bool, content string) error {if unreadable {- slog.Warn("skipping permission-locked project env file; leaving it untouched", "projectPath", projectPath, "file", fileName)+ slog.Warn("skipping unreadable project env file; leaving it untouched", "projectPath", projectPath, "file", fileName)return nil}@@ -716,11 +718,11 @@ func WriteManagedEnvFile(ctx context.Context, projectsDirectory, projectPath, fi}// readOptionalProjectFileInternal reads fileName from projectPath. A missing-// file is reported via exists=false with no error. A permission error is-// reported via unreadable=true with no error: the file is present but its+// file is reported via exists=false with no error. A permission error or a+// directory at the path is reported via unreadable=true with no error: its// contents cannot be verified, so callers must treat it as absent for merge// purposes and must not attempt to overwrite or remove it. Any other I/O-// error (e.g. the path is a directory) is still returned as a hard failure.+// error is still returned as a hard failure.// A project env file may itself be a symlink whose target lives outside the// project directory, so the read goes through os rather than the root-confined// API — the same deliberate exception the .env write path makes (#3556).@@ -732,7 +734,7 @@ func readOptionalProjectFileInternal(projectPath, fileName string) (content striif errors.Is(readErr, os.ErrNotExist) {return "", false, false, nil}- if errors.Is(readErr, os.ErrPermission) {+ if errors.Is(readErr, os.ErrPermission) || errors.Is(readErr, syscall.EISDIR) {return "", false, true, nil}return "", false, false, errors.WrapIff(readErr, "read %s", fileName)diff --git a/backend/pkg/projects/env_unix_test.go b/backend/pkg/projects/env_unix_test.goindex ec028bb95..97ea08c97 100644--- a/backend/pkg/projects/env_unix_test.go+++ b/backend/pkg/projects/env_unix_test.go@@ -3,6 +3,7 @@package projectsimport (+ stderrors "errors""os""path/filepath""testing"@@ -39,3 +40,68 @@ func TestReadProjectEnvState_TreatsPermissionLockedEnvAsUnreadable(t *testing.T)require.NoError(t, err)assert.Equal(t, "FOO=locked\n", string(content))}++func TestReadProjectEnvState_TreatsDirectoryAsUnreadable(t *testing.T) {+ for _, fileName := range []string{EffectiveEnvFileName, GitSourceEnvFileName, OverrideEnvFileName} {+ t.Run(fileName, func(t *testing.T) {+ projectDir := t.TempDir()+ require.NoError(t, os.Mkdir(filepath.Join(projectDir, fileName), utils.DirPerm))++ state, err := ReadProjectEnvState(projectDir)+ require.NoError(t, err)+ assert.False(t, state.HasEffective)+ assert.False(t, state.HasGitSource)+ assert.False(t, state.HasOverride)+ assert.Equal(t, fileName == EffectiveEnvFileName, state.EffectiveUnreadable)+ assert.Equal(t, fileName == GitSourceEnvFileName, state.GitSourceUnreadable)+ assert.Equal(t, fileName == OverrideEnvFileName, state.OverrideUnreadable)+ })+ }+}++func TestParseEnvFile_IgnoresDirectory(t *testing.T) {+ envDir := filepath.Join(t.TempDir(), EffectiveEnvFileName)+ require.NoError(t, os.Mkdir(envDir, utils.DirPerm))++ projectEnv, err := ParseProjectEnvFile(envDir, EnvMap{})+ require.NoError(t, err)+ assert.Nil(t, projectEnv)++ validationEnv, err := ParseValidationEnvFile(envDir, EnvMap{})+ require.NoError(t, err)+ assert.Nil(t, validationEnv)+}++func TestWithTransientValidationEnvFile_LeavesEnvDirectoryUntouched(t *testing.T) {+ projectDir := t.TempDir()+ envDir := filepath.Join(projectDir, EffectiveEnvFileName)+ require.NoError(t, os.Mkdir(envDir, utils.DirPerm))+ nested := filepath.Join(envDir, "keep")+ require.NoError(t, os.WriteFile(nested, []byte("x"), utils.FilePerm))++ content := "FOO=bar\n"+ runErr := stderrors.New("validation failed")+ called := false+ err := WithTransientValidationEnvFile(t.Context(), projectDir, &content, func() error {+ called = true+ return runErr+ })+ require.ErrorIs(t, err, runErr)+ assert.True(t, called)++ info, err := os.Stat(envDir)+ require.NoError(t, err)+ assert.True(t, info.IsDir())+ _, err = os.Stat(nested)+ require.NoError(t, err)+}++func TestValidateComposeContentForUpdate_EnvDirectory(t *testing.T) {+ projectsDir := t.TempDir()+ projectDir := filepath.Join(projectsDir, "envdir")+ require.NoError(t, os.MkdirAll(filepath.Join(projectDir, EffectiveEnvFileName), utils.DirPerm))++ compose := "services:\n app:\n image: nginx:alpine\n"+ content := "FOO=bar\n"+ require.NoError(t, ValidateComposeContentForUpdate(t.Context(), projectsDir, projectDir, "envdir", compose, &content, nil, "", false))+}diff --git a/backend/pkg/projects/env_validation.go b/backend/pkg/projects/env_validation.goindex bfe73e877..cbeda00df 100644--- a/backend/pkg/projects/env_validation.go+++ b/backend/pkg/projects/env_validation.go@@ -61,12 +61,16 @@ func BuildValidationEnvironment(ctx context.Context, projectsDirectory, projectP// Stays on os.*: env files may be symlinks resolving outside any confinement// root (a supported setup), which acfs cannot follow.func ParseValidationEnvFile(path string, contextEnv EnvMap) (EnvMap, error) {- if _, err := os.Stat(path); err != nil {+ info, err := os.Stat(path)+ if err != nil {if os.IsNotExist(err) {return nil, nil}return nil, errors.WrapIf(err, "stat file")}+ if info.IsDir() {+ return nil, nil+ }content, err := os.ReadFile(path)if err != nil {diff --git a/backend/pkg/projects/workspace.go b/backend/pkg/projects/workspace.goindex 40a62d096..9c9792760 100644--- a/backend/pkg/projects/workspace.go+++ b/backend/pkg/projects/workspace.go@@ -73,7 +73,7 @@ func ReadProjectWorkspace(projectPath string, maxDepth int, skipDirectories, commaxDepth: maxDepth,maxEntries: maxEntries,maxFileSizeBytes: maxFileSizeBytes,- protected: ProtectedProjectFilePaths(composeFileName),+ protected: ProtectedProjectPaths(projectAbs, composeFileName),skipDirs: projectScanSkipDirectorySetInternal(skipDirectories),files: []workspacetypes.FileEntry{},revisionHash: sha256.New(),@@ -267,8 +267,10 @@ func ApplyProjectWorkspaceChanges(projectPath string, changes []project.Workspac}defer func() { _ = root.Close() }()- protected := ProtectedProjectFilePaths(opts.ComposeFileName)for _, change := range changes {+ // Recomputed per change: an earlier change may have removed a directory+ // that shadowed a protected name.+ protected := ProtectedProjectPaths(projectPath, opts.ComposeFileName)if err := applyWorkspaceFileChangeInternal(root, protected, change, uploads, opts.MaxFileSizeBytes); err != nil {return err}@@ -277,6 +279,19 @@ func ApplyProjectWorkspaceChanges(projectPath string, changes []project.Workspacreturn nil}+// ProtectedProjectPaths is ProtectedProjectFilePaths minus names that are+// directories in projectPath. Neither Arcane nor compose treats such a+// directory as project configuration, so it belongs to the workspace.+func ProtectedProjectPaths(projectPath, composeFileName string) map[string]bool {+ protected := ProtectedProjectFilePaths(composeFileName)+ for name := range protected {+ if info, err := os.Lstat(filepath.Join(projectPath, name)); err == nil && info.IsDir() {+ delete(protected, name)+ }+ }+ return protected+}+func ProtectedProjectFilePaths(composeFileName string) map[string]bool {protected := map[string]bool{EffectiveEnvFileName: true,diff --git a/backend/pkg/projects/workspace_test.go b/backend/pkg/projects/workspace_test.goindex a687fb724..66f76355a 100644--- a/backend/pkg/projects/workspace_test.go+++ b/backend/pkg/projects/workspace_test.go@@ -454,3 +454,47 @@ func TestApplyProjectWorkspaceChanges_BaselineGuardsConcurrentEdit(t *testing.T)require.NoError(t, err)require.Equal(t, "fresh draft\n", string(content))}++func TestReadProjectWorkspace_ListsEnvDirectoryContents(t *testing.T) {+ t.Parallel()++ projectDir := t.TempDir()+ require.NoError(t, os.WriteFile(filepath.Join(projectDir, "compose.yaml"), []byte("services: {}\n"), 0o644))+ require.NoError(t, os.WriteFile(filepath.Join(projectDir, "project.env"), []byte("APP_VALUE=local\n"), 0o644))+ require.NoError(t, os.Mkdir(filepath.Join(projectDir, ".env"), 0o755))+ require.NoError(t, os.WriteFile(filepath.Join(projectDir, ".env", "app.env"), []byte("APP_VALUE=sub\n"), 0o644))++ files, _, _, err := ReadProjectWorkspace(projectDir, 3, "", "compose.yaml", 0, 0)+ require.NoError(t, err)++ relativePaths := make([]string, 0, len(files))+ for _, file := range files {+ relativePaths = append(relativePaths, file.RelativePath)+ }+ assert.ElementsMatch(t, []string{".env", ".env/app.env"}, relativePaths)+}++func TestApplyWorkspaceFileChanges_EnvDirectory(t *testing.T) {+ t.Parallel()++ projectDir := t.TempDir()+ require.NoError(t, os.WriteFile(filepath.Join(projectDir, "compose.yaml"), []byte("services: {}\n"), 0o644))+ require.NoError(t, os.Mkdir(filepath.Join(projectDir, ".env"), 0o755))+ require.NoError(t, os.WriteFile(filepath.Join(projectDir, ".env", "app.env"), []byte("A=1\n"), 0o644))+ opts := ProjectWorkspaceApplyOptions{ComposeFileName: "compose.yaml"}++ require.NoError(t, ApplyProjectWorkspaceChanges(projectDir, []project.WorkspaceFileChange{+ {Operation: "update_file", RelativePath: ".env/app.env", UploadIndex: new(0)},+ }, map[int][]byte{0: []byte("A=2\n")}, opts))+ content, err := os.ReadFile(filepath.Join(projectDir, ".env", "app.env"))+ require.NoError(t, err)+ assert.Equal(t, "A=2\n", string(content))++ // Once the directory is gone, the name is protected configuration again,+ // even within the same batch.+ err = ApplyProjectWorkspaceChanges(projectDir, []project.WorkspaceFileChange{+ {Operation: "delete", RelativePath: ".env", Recursive: true},+ {Operation: "create_file", RelativePath: ".env", UploadIndex: new(0)},+ }, map[int][]byte{0: []byte("B=1\n")}, opts)+ require.ErrorIs(t, err, ErrProjectWorkspaceProtectedPath)+}