From fb73474675cd0256cbfed764c03d4af9a34a480a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 18 Jan 2024 19:11:53 -0500 Subject: [PATCH] refactor: allow setting a custom mode when writing a slice to a file --- plugins/buildpacks/functions.go | 7 ++++- plugins/common/io.go | 46 ++++++++++++++++++----------- plugins/docker-options/functions.go | 6 +++- plugins/logs/functions.go | 7 ++++- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/plugins/buildpacks/functions.go b/plugins/buildpacks/functions.go index 8c4a40c3a..04f933b3d 100644 --- a/plugins/buildpacks/functions.go +++ b/plugins/buildpacks/functions.go @@ -3,6 +3,7 @@ package buildpacks import ( "errors" "fmt" + "os" "path/filepath" "regexp" "strings" @@ -34,7 +35,11 @@ func rewriteBuildpacksFile(sourceWorkDir string) error { buildpacks[i] = buildpack } - return common.WriteSliceToFile(buildpacksPath, buildpacks) + return common.WriteSliceToFile(common.WriteSliceToFileInput{ + Filename: buildpacksPath, + Lines: buildpacks, + Mode: os.FileMode(0600), + }) } func validBuildpackURL(buildpack string) (string, error) { diff --git a/plugins/common/io.go b/plugins/common/io.go index 1c5aa8a39..01afb2b41 100644 --- a/plugins/common/io.go +++ b/plugins/common/io.go @@ -217,33 +217,45 @@ func TouchFile(filename string) error { } defer file.Close() - file.Chmod(mode) - SetPermissions(filename, mode) - return nil -} - -// WriteSliceToFile writes a slice of strings to a file -func WriteSliceToFile(filename string, lines []string) error { - mode := os.FileMode(0600) - if strings.HasPrefix(filename, "/etc/sudoers.d/") { - mode = os.FileMode(0440) - } - - file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode) - if err != nil { + if err := file.Chmod(mode); err != nil { return err } + if err := SetPermissions(filename, mode); err != nil { + return err + } + return nil +} + +type WriteSliceToFileInput struct { + Filename string + Lines []string + Mode os.FileMode +} + +// WriteSliceToFile writes a slice of strings to a file +func WriteSliceToFile(input WriteSliceToFileInput) error { + file, err := os.OpenFile(input.Filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, input.Mode) + if err != nil { + return err + } + defer file.Close() + w := bufio.NewWriter(file) - for _, line := range lines { + for _, line := range input.Lines { fmt.Fprintln(w, line) } if err = w.Flush(); err != nil { return err } - file.Chmod(mode) - SetPermissions(filename, mode) + if err := file.Chmod(input.Mode); err != nil { + return err + } + + if err := SetPermissions(input.Filename, input.Mode); err != nil { + return err + } return nil } diff --git a/plugins/docker-options/functions.go b/plugins/docker-options/functions.go index d7f1919e6..f00fa5546 100644 --- a/plugins/docker-options/functions.go +++ b/plugins/docker-options/functions.go @@ -32,5 +32,9 @@ func touchPhaseFile(appName string, phase string) error { func writeDockerOptionsForPhase(appName string, phase string, options []string) error { phaseFilePath := getPhaseFilePath(appName, phase) - return common.WriteSliceToFile(phaseFilePath, options) + return common.WriteSliceToFile(common.WriteSliceToFileInput{ + Filename: phaseFilePath, + Lines: options, + Mode: os.FileMode(0600), + }) } diff --git a/plugins/logs/functions.go b/plugins/logs/functions.go index edc41faa5..785eb529d 100644 --- a/plugins/logs/functions.go +++ b/plugins/logs/functions.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net/url" + "os" "path/filepath" "strings" "time" @@ -224,7 +225,11 @@ func writeVectorConfig() error { b = bytes.Replace(b, []byte("\\u002B"), []byte("+"), -1) vectorConfig := filepath.Join(common.GetDataDirectory("logs"), "vector.json") - if err := common.WriteSliceToFile(vectorConfig, []string{string(b)}); err != nil { + if err := common.WriteSliceToFile(common.WriteSliceToFileInput{ + Filename: vectorConfig, + Lines: []string{string(b)}, + Mode: os.FileMode(0600), + }); err != nil { return err }