-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to set the log file permissions
Adding a "mode" option to overwrite the default logfile permissions. Default remains "0600" which is the one currently used by lumberjack.
- Loading branch information
Showing
2 changed files
with
237 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
// Copyright 2015 Matthew Holt and The Caddy Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logging | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" | ||
) | ||
|
||
func TestFileCreationMode(t *testing.T) { | ||
on := true | ||
off := false | ||
|
||
tests := []struct { | ||
name string | ||
fw FileWriter | ||
wantMode os.FileMode | ||
}{ | ||
{ | ||
name: "default mode no roll", | ||
fw: FileWriter{ | ||
Roll: &off, | ||
}, | ||
wantMode: 0o600, | ||
}, | ||
{ | ||
name: "default mode roll", | ||
fw: FileWriter{ | ||
Roll: &on, | ||
}, | ||
wantMode: 0o600, | ||
}, | ||
{ | ||
name: "custom mode no roll", | ||
fw: FileWriter{ | ||
Roll: &off, | ||
Mode: 0o666, | ||
}, | ||
wantMode: 0o666, | ||
}, | ||
{ | ||
name: "custom mode roll", | ||
fw: FileWriter{ | ||
Roll: &on, | ||
Mode: 0o666, | ||
}, | ||
wantMode: 0o666, | ||
}, | ||
} | ||
|
||
m := syscall.Umask(0o000) | ||
Check failure on line 67 in modules/logging/filewriter_test.go GitHub Actions / test (windows, 1.21)
|
||
defer func() { | ||
syscall.Umask(m) | ||
Check failure on line 69 in modules/logging/filewriter_test.go GitHub Actions / test (windows, 1.21)
|
||
}() | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dir, err := os.MkdirTemp("", "caddytest") | ||
if err != nil { | ||
t.Fatalf("failed to create tempdir: %v", err) | ||
} | ||
defer os.RemoveAll(dir) | ||
fpath := path.Join(dir, "test.log") | ||
tt.fw.Filename = fpath | ||
|
||
logger, err := tt.fw.OpenWriter() | ||
if err != nil { | ||
t.Fatalf("failed to create file: %v", err) | ||
} | ||
defer logger.Close() | ||
|
||
st, err := os.Stat(fpath) | ||
if err != nil { | ||
t.Fatalf("failed to check file permissions: %v", err) | ||
} | ||
|
||
if st.Mode() != tt.wantMode { | ||
t.Errorf("file mode is %v, want %v", st.Mode(), tt.wantMode) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFileRotationPreserveMode(t *testing.T) { | ||
m := syscall.Umask(0o000) | ||
Check failure on line 101 in modules/logging/filewriter_test.go GitHub Actions / test (windows, 1.21)
|
||
defer func() { | ||
syscall.Umask(m) | ||
Check failure on line 103 in modules/logging/filewriter_test.go GitHub Actions / test (windows, 1.21)
|
||
}() | ||
|
||
dir, err := os.MkdirTemp("", "caddytest") | ||
if err != nil { | ||
t.Fatalf("failed to create tempdir: %v", err) | ||
} | ||
defer os.RemoveAll(dir) | ||
|
||
fpath := path.Join(dir, "test.log") | ||
|
||
roll := true | ||
mode := os.FileMode(0o640) | ||
fw := FileWriter{ | ||
Filename: fpath, | ||
Mode: mode, | ||
Roll: &roll, | ||
RollSizeMB: 1, | ||
} | ||
|
||
logger, err := fw.OpenWriter() | ||
if err != nil { | ||
t.Fatalf("failed to create file: %v", err) | ||
} | ||
defer logger.Close() | ||
|
||
b := make([]byte, 1024*1024-1000) | ||
logger.Write(b) | ||
logger.Write(b[0:2000]) | ||
|
||
files, err := os.ReadDir(dir) | ||
if err != nil { | ||
t.Fatalf("failed to read temporary log dir: %v", err) | ||
} | ||
|
||
if len(files) != 2 { | ||
t.Fatalf("got %v files want 2", len(files)) | ||
} | ||
|
||
wantPattern := "test-*-*-*-*-*.*.log" | ||
if m, _ := path.Match(wantPattern, files[0].Name()); m != true { | ||
t.Fatalf("got %v filename want %v", files[0].Name(), wantPattern) | ||
} | ||
|
||
st, err := os.Stat(path.Join(dir, files[0].Name())) | ||
if err != nil { | ||
t.Fatalf("failed to check file permissions: %v", err) | ||
} | ||
|
||
if st.Mode() != mode { | ||
t.Errorf("file mode is %v, want %v", st.Mode(), mode) | ||
} | ||
|
||
if files[1].Name() != "test.log" { | ||
t.Fatalf("got %v filename want test.log", files[1].Name()) | ||
} | ||
|
||
st, err = os.Stat(path.Join(dir, files[1].Name())) | ||
if err != nil { | ||
t.Fatalf("failed to check file permissions: %v", err) | ||
} | ||
|
||
if st.Mode() != mode { | ||
t.Errorf("file mode is %v, want %v", st.Mode(), mode) | ||
} | ||
} | ||
|
||
func TestFileModeConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
d *caddyfile.Dispenser | ||
fw FileWriter | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "set mode", | ||
d: caddyfile.NewTestDispenser(` | ||
file test.log { | ||
mode 0666 | ||
} | ||
`), | ||
fw: FileWriter{ | ||
Mode: 0o666, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid mode", | ||
d: caddyfile.NewTestDispenser(` | ||
file test.log { | ||
mode foobar | ||
} | ||
`), | ||
fw: FileWriter{}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
fw := &FileWriter{} | ||
if err := fw.UnmarshalCaddyfile(tt.d); (err != nil) != tt.wantErr { | ||
t.Fatalf("UnmarshalCaddyfile() error = %v, want %v", err, tt.wantErr) | ||
} | ||
if fw.Mode != tt.fw.Mode { | ||
t.Errorf("got mode %v, want %v", fw.Mode, tt.fw.Mode) | ||
} | ||
}) | ||
} | ||
} |