Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: preserve existing file mode when saving project and global env files (#3919)
Kyle Mendellc92f3932cca1
diff --git a/backend/internal/variable/service_test.go b/backend/internal/variable/service_test.goindex a679b0fbc..87ec675ec 100644--- a/backend/internal/variable/service_test.go+++ b/backend/internal/variable/service_test.go@@ -9,6 +9,7 @@ import ("net/http/httptest""os""path/filepath"+ "runtime""strings""sync""testing"@@ -22,6 +23,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/kv""github.com/getarcaneapp/arcane/backend/v2/internal/settings""github.com/getarcaneapp/arcane/backend/v2/pkg/remenv"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils"envtypes "github.com/getarcaneapp/arcane/types/v2/env""github.com/libtnb/sqlite""github.com/stretchr/testify/assert"@@ -220,6 +222,29 @@ func TestWriteLocalEnvFile_RejectsNewlineInjectionKey(t *testing.T) {require.True(t, os.IsNotExist(statErr), ".env.global must not be written on validation failure")}+func TestWriteLocalEnvFile_PreservesExistingFileMode(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("file mode bits are not represented on Windows FS")+ }+ service, _, projectsDir := setupVariableServiceTest(t)+ ctx := context.Background()+ envPath := filepath.Join(projectsDir, ".env.global")++ require.NoError(t, service.WriteLocalEnvFile(ctx, []envtypes.Variable{{Key: "A", Value: "1"}}))+ info, err := os.Stat(envPath)+ require.NoError(t, err)+ require.Equal(t, utils.FilePerm, info.Mode().Perm(), "new file must use the configured default mode")++ require.NoError(t, os.Chmod(envPath, 0o600))+ require.NoError(t, service.WriteLocalEnvFile(ctx, []envtypes.Variable{{Key: "A", Value: "2"}}))+ info, err = os.Stat(envPath)+ require.NoError(t, err)+ require.Equal(t, os.FileMode(0o600), info.Mode().Perm(), "rewrite must keep the existing mode")+ content, err := os.ReadFile(envPath)+ require.NoError(t, err)+ require.Contains(t, string(content), "A=2")+}+func TestSyncEnvironment_LocalWritesEnvGlobalFile(t *testing.T) {service, _, projectsDir := setupVariableServiceTest(t)ctx := context.Background()diff --git a/backend/internal/variable/variable.go b/backend/internal/variable/variable.goindex aab5be1d6..770697ca2 100644--- a/backend/internal/variable/variable.go+++ b/backend/internal/variable/variable.go@@ -559,7 +559,11 @@ func (s *VariableService) WriteLocalEnvFile(ctx context.Context, vars []env.Vari_, _ = fmt.Fprintf(&builder, "%s=%s\n", key, value)}- if err := acfs.Write(ctx, projectsDirectory, "/"+projects.GlobalEnvFileName, []byte(builder.String()), acfs.WriteOptions{Mode: utils.FilePerm, InPlace: true}); err != nil {+ mode := utils.FilePerm+ if entry, statErr := acfs.Stat(ctx, projectsDirectory, "/"+projects.GlobalEnvFileName, false); statErr == nil {+ mode = os.FileMode(entry.UnixMode).Perm()+ }+ if err := acfs.Write(ctx, projectsDirectory, "/"+projects.GlobalEnvFileName, []byte(builder.String()), acfs.WriteOptions{Mode: mode, InPlace: true}); err != nil {return errors.WrapIf(err, "failed to write global variables file")}diff --git a/backend/pkg/projects/fs_writer.go b/backend/pkg/projects/fs_writer.goindex a7f6c1071..cfc99392b 100644--- a/backend/pkg/projects/fs_writer.go+++ b/backend/pkg/projects/fs_writer.go@@ -68,11 +68,15 @@ func WriteComposeFile(ctx context.Context, projectsRoot, dirPath, content string}composeFileName := DefaultComposeFileName+ mode := utils.FilePermif existingFile := detectExistingComposeFileInternal(ctx, projectsRoot, dirPath); existingFile != "" {composeFileName = filepath.Base(existingFile)+ if entry, statErr := acfs.Stat(ctx, dirPath, "/"+composeFileName, false); statErr == nil {+ mode = os.FileMode(entry.UnixMode).Perm()+ }}- if err := acfs.Write(ctx, dirPath, "/"+composeFileName, []byte(content), acfs.WriteOptions{Mode: utils.FilePerm, InPlace: true}); err != nil {+ if err := acfs.Write(ctx, dirPath, "/"+composeFileName, []byte(content), acfs.WriteOptions{Mode: mode, InPlace: true}); err != nil {return errors.WrapIf(err, "failed to write compose file")}@@ -113,6 +117,7 @@ func WriteProjectFile(ctx context.Context, projectsRoot, dirPath, fileName, contreturn errors.WrapIff(statErr, "failed to inspect project file %s", fileName)}+ mode := utils.FilePermif statErr == nil {existingContent, readErr := acfs.ReadFile(ctx, dirPath, logicalPath)if readErr == nil && string(existingContent) == content {@@ -121,9 +126,10 @@ func WriteProjectFile(ctx context.Context, projectsRoot, dirPath, fileName, contif readErr != nil && !errors.Is(readErr, fs.ErrNotExist) {return errors.WrapIff(readErr, "failed to read project file %s", fileName)}+ mode = os.FileMode(entry.UnixMode).Perm()}- if err := acfs.Write(ctx, dirPath, logicalPath, []byte(content), acfs.WriteOptions{Mode: utils.FilePerm, InPlace: true}); err != nil {+ if err := acfs.Write(ctx, dirPath, logicalPath, []byte(content), acfs.WriteOptions{Mode: mode, InPlace: true}); err != nil {return errors.WrapIff(err, "failed to write project file %s", fileName)}diff --git a/backend/pkg/projects/fs_writer_test.go b/backend/pkg/projects/fs_writer_test.goindex 7c954c8e8..70bc636e4 100644--- a/backend/pkg/projects/fs_writer_test.go+++ b/backend/pkg/projects/fs_writer_test.go@@ -90,6 +90,9 @@ func TestWriteProjectFile_DoesNotReplaceIdenticalContent(t *testing.T) {updated, err := os.Stat(filePath)require.NoError(t, err)assert.True(t, os.SameFile(original, updated), "changed content should preserve the inode")+ if runtime.GOOS != "windows" {+ assert.Equal(t, os.FileMode(0o600), updated.Mode().Perm(), "changed content should preserve the file mode")+ }actual, err := os.ReadFile(filePath)require.NoError(t, err)assert.Equal(t, "VALUE=new\n", string(actual))@@ -297,6 +300,9 @@ func TestWriteComposeFile_PreservesExistingPodmanComposeNames(t *testing.T) {updated, err := os.Stat(existingComposePath)require.NoError(t, err)assert.True(t, os.SameFile(original, updated), "compose updates should preserve the inode")+ if runtime.GOOS != "windows" {+ assert.Equal(t, os.FileMode(0o600), updated.Mode().Perm(), "compose updates should preserve the file mode")+ }_, err = os.Stat(filepath.Join(projectDir, "compose.yaml"))assert.True(t, os.IsNotExist(err), "compose.yaml should not be created when existing podman-compose file is present")