Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeToFile: allow empty permission to preserve the existing #1929

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1755,9 +1755,12 @@ func hmacSHA256Hex(message, key string) (string, error) {
// and optional flags to select appending mode or add a newline.
//
// The username and group name fields can be left blank to default to the current user and group.
// The permissions field can be left blank to default to 0644 if the file is created, or the existing
// permissions if the file is appended to or modified.
//
// For example:
//
// key "my/key/path" | writeToFile "/my/file/path.txt"
// key "my/key/path" | writeToFile "/my/file/path.txt" "" "" "0644"
// key "my/key/path" | writeToFile "/my/file/path.txt" "100" "1000" "0644"
// key "my/key/path" | writeToFile "/my/file/path.txt" "my-user" "my-group" "0644"
Expand All @@ -1771,14 +1774,20 @@ func writeToFile(path, username, groupName, permissions string, args ...string)
}
content := args[len(args)-1]

p_u, err := strconv.ParseUint(permissions, 8, 32)
if err != nil {
return "", err
perm := os.FileMode(0o664) // Default permissions for created files
if permissions != "" {
p_u, err := strconv.ParseUint(permissions, 8, 32)
if err != nil {
return "", err
}
perm = os.FileMode(p_u)
}
perm := os.FileMode(p_u)

// Write to file
var f *os.File
var (
f *os.File
err error
)
shouldAppend := strings.Contains(flags, "append")
if shouldAppend {
f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, perm)
Expand Down Expand Up @@ -1814,9 +1823,6 @@ func writeToFile(path, username, groupName, permissions string, args ...string)
// Change ownership and permissions
var uid int
var gid int
if err != nil {
return "", err
}

if username == "" {
uid = os.Getuid()
Expand Down Expand Up @@ -1857,9 +1863,11 @@ func writeToFile(path, username, groupName, permissions string, args ...string)
}
}

err = os.Chmod(path, perm)
if err != nil {
return "", err
if permissions != "" {
err = os.Chmod(path, perm)
if err != nil {
return "", err
}
}

return "", nil
Expand Down
39 changes: 34 additions & 5 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,28 @@ func Test_writeToFile(t *testing.T) {
"after",
false,
},
{
"writeToFile_without_permissions",
"",
"after",
"",
"",
"",
"",
"after",
false,
},
{
"writeToFile_without_permissions_create_directory",
"demo/testing.tmp",
"after",
currentUsername,
currentGroupName,
"",
"",
"after",
false,
},
}

for _, tc := range cases {
Expand All @@ -2570,6 +2592,7 @@ func Test_writeToFile(t *testing.T) {
defer os.RemoveAll(outDir)

var outputFilePath string
var perm os.FileMode
if tc.filePath == "" {
outputFile, err := os.CreateTemp(outDir, "")
if err != nil {
Expand All @@ -2580,8 +2603,12 @@ func Test_writeToFile(t *testing.T) {
t.Fatal(err)
}
outputFilePath = outputFile.Name()
// permission for the file created by CreateTemp is 0o600
perm = os.FileMode(0o600)
} else {
outputFilePath = outDir + "/" + tc.filePath
// default unmask permission
perm = os.FileMode(0o664)
}

templateContent := fmt.Sprintf(
Expand Down Expand Up @@ -2619,13 +2646,15 @@ func Test_writeToFile(t *testing.T) {
if err != nil {
t.Fatal(err)
}
p_u, err := strconv.ParseUint(tc.permissions, 8, 32)
if err != nil {
t.Fatal(err)
if tc.permissions != "" {
p_u, err := strconv.ParseUint(tc.permissions, 8, 32)
if err != nil {
t.Fatal(err)
}
perm = os.FileMode(p_u)
}
perm := os.FileMode(p_u)
if sts.Mode() != perm {
t.Errorf("writeToFile() wrong permissions got = %v, want %v", perm, tc.permissions)
t.Errorf("writeToFile() wrong permissions got = %v, want %v", sts.Mode(), perm)
}

stat := sts.Sys().(*syscall.Stat_t)
Expand Down