aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/logging/filewriter.go
diff options
context:
space:
mode:
authorRirisoft <[email protected]>2024-06-06 16:33:34 +0200
committerGitHub <[email protected]>2024-06-06 08:33:34 -0600
commit101d3e740783581110340a68f0b0cbe5f1ab6dbb (patch)
treef5304fcae9361a0f81ce5eda830198b634f2ba9e /modules/logging/filewriter.go
parent3f1add6c9f5a41500cd6cfc96c5200d2c8291e14 (diff)
downloadcaddy-101d3e740783581110340a68f0b0cbe5f1ab6dbb.tar.gz
caddy-101d3e740783581110340a68f0b0cbe5f1ab6dbb.zip
logging: Customize log file permissions (#6314)
Adding a "mode" option to overwrite the default logfile permissions. Default remains "0600" which is the one currently used by lumberjack.
Diffstat (limited to 'modules/logging/filewriter.go')
-rw-r--r--modules/logging/filewriter.go65
1 files changed, 63 insertions, 2 deletions
diff --git a/modules/logging/filewriter.go b/modules/logging/filewriter.go
index 3b1001b7c..393228fda 100644
--- a/modules/logging/filewriter.go
+++ b/modules/logging/filewriter.go
@@ -15,6 +15,7 @@
package logging
import (
+ "encoding/json"
"fmt"
"io"
"math"
@@ -33,6 +34,43 @@ func init() {
caddy.RegisterModule(FileWriter{})
}
+// fileMode is a string made of 1 to 4 octal digits representing
+// a numeric mode as specified with the `chmod` unix command.
+// `"0777"` and `"777"` are thus equivalent values.
+type fileMode os.FileMode
+
+// UnmarshalJSON satisfies json.Unmarshaler.
+func (m *fileMode) UnmarshalJSON(b []byte) error {
+ if len(b) == 0 {
+ return io.EOF
+ }
+
+ var s string
+ if err := json.Unmarshal(b, &s); err != nil {
+ return err
+ }
+
+ mode, err := parseFileMode(s)
+ if err != nil {
+ return err
+ }
+
+ *m = fileMode(mode)
+ return err
+}
+
+// parseFileMode parses a file mode string,
+// adding support for `chmod` unix command like
+// 1 to 4 digital octal values.
+func parseFileMode(s string) (os.FileMode, error) {
+ modeStr := fmt.Sprintf("%04s", s)
+ mode, err := strconv.ParseUint(modeStr, 8, 32)
+ if err != nil {
+ return 0, err
+ }
+ return os.FileMode(mode), nil
+}
+
// FileWriter can write logs to files. By default, log files
// are rotated ("rolled") when they get large, and old log
// files get deleted, to ensure that the process does not
@@ -41,6 +79,10 @@ type FileWriter struct {
// Filename is the name of the file to write.
Filename string `json:"filename,omitempty"`
+ // The file permissions mode.
+ // 0600 by default.
+ Mode fileMode `json:"mode,omitempty"`
+
// Roll toggles log rolling or rotation, which is
// enabled by default.
Roll *bool `json:"roll,omitempty"`
@@ -100,6 +142,10 @@ func (fw FileWriter) WriterKey() string {
// OpenWriter opens a new file writer.
func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
+ if fw.Mode == 0 {
+ fw.Mode = 0o600
+ }
+
// roll log files by default
if fw.Roll == nil || *fw.Roll {
if fw.RollSizeMB == 0 {
@@ -116,6 +162,9 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
fw.RollKeepDays = 90
}
+ f_tmp, _ := os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(fw.Mode))
+ f_tmp.Close()
+
return &lumberjack.Logger{
Filename: fw.Filename,
MaxSize: fw.RollSizeMB,
@@ -127,12 +176,13 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
}
// otherwise just open a regular file
- return os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o666)
+ return os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(fw.Mode))
}
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
//
// file <filename> {
+// mode <mode>
// roll_disabled
// roll_size <size>
// roll_uncompressed
@@ -150,7 +200,7 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
// The roll_keep_for duration has day resolution.
// Fractional values are rounded up to the next whole number of days.
//
-// If any of the roll_size, roll_keep, or roll_keep_for subdirectives are
+// If any of the mode, roll_size, roll_keep, or roll_keep_for subdirectives are
// omitted or set to a zero value, then Caddy's default value for that
// subdirective is used.
func (fw *FileWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
@@ -165,6 +215,17 @@ func (fw *FileWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.NextBlock(0) {
switch d.Val() {
+ case "mode":
+ var modeStr string
+ if !d.AllArgs(&modeStr) {
+ return d.ArgErr()
+ }
+ mode, err := parseFileMode(modeStr)
+ if err != nil {
+ return d.Errf("parsing mode: %v", err)
+ }
+ fw.Mode = fileMode(mode)
+
case "roll_disabled":
var f bool
fw.Roll = &f