Skip to content

Commit 4ca926d

Browse files
committed
filesystem: make WriteFile file permissions more restrictive
Currently, the only usage of 'WriteFile()' is in the daemon providers, used to write out their respective daemon configurations. The permissions '644' were selected somewhat arbitrarily (corresponding to Git's "non-executable" file mode). However, all other file writing in the repository uses the permissions '600' (removing read permissions for group & all). The daemon configuration files do not actually need anything more permissive than '600', so change 'WriteFile()'s permissions accordingly. Then, rather than simply modify the hardcoded value, create a const 'DefaultFilePermissions' to store the value, and do the same for 0o755 as 'DefaultDirPermissions'. In addition to internal consistency, this sets us up to more easily use 'common.FileSystem' for file writing across the repository. Signed-off-by: Victoria Dye <[email protected]>
1 parent c3beec2 commit 4ca926d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

internal/common/filesystem.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import (
44
"bufio"
55
"errors"
66
"fmt"
7+
"io/fs"
78
"os"
89
"path"
910
"syscall"
1011
)
1112

13+
const (
14+
DefaultFilePermissions fs.FileMode = 0o600
15+
DefaultDirPermissions fs.FileMode = 0o755
16+
)
17+
1218
type FileSystem interface {
1319
FileExists(filename string) (bool, error)
1420
WriteFile(filename string, content []byte) error
@@ -36,12 +42,12 @@ func (f *fileSystem) FileExists(filename string) (bool, error) {
3642
func (f *fileSystem) WriteFile(filename string, content []byte) error {
3743
// Get filename parent path
3844
parentDir := path.Dir(filename)
39-
err := os.MkdirAll(parentDir, 0o755)
45+
err := os.MkdirAll(parentDir, DefaultDirPermissions)
4046
if err != nil {
4147
return fmt.Errorf("error creating parent directories: %w", err)
4248
}
4349

44-
err = os.WriteFile(filename, content, 0o644)
50+
err = os.WriteFile(filename, content, DefaultFilePermissions)
4551
if err != nil {
4652
return fmt.Errorf("could not write file: %w", err)
4753
}

0 commit comments

Comments
 (0)