fix default perms for log file (#1177)

* fix default perms
This commit is contained in:
Thibault "bui" Koechlin 2022-01-18 16:54:02 +01:00 committed by GitHub
parent a17f150e5d
commit a88848009a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 8 deletions

View file

@ -136,12 +136,37 @@ func NewServer(config *csconfig.LocalApiServerCfg) (*APIServer, error) {
/*Configure logs*/
if logFile != "" {
_maxsize := 500
if config.LogMaxSize != 0 {
_maxsize = config.LogMaxSize
}
_maxfiles := 3
if config.LogMaxFiles != 0 {
_maxfiles = config.LogMaxFiles
}
_maxage := 28
if config.LogMaxAge != 0 {
_maxage = config.LogMaxAge
}
_compress := true
if config.CompressLogs != nil {
_compress = *config.CompressLogs
}
/*cf. https://github.com/natefinch/lumberjack/issues/82
let's create the file beforehand w/ the right perms */
// check if file exists
_, err := os.Stat(logFile)
// create file if not exists, purposefully ignore errors
if os.IsNotExist(err) {
file, _ := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE, 0600)
file.Close()
}
LogOutput := &lumberjack.Logger{
Filename: logFile,
MaxSize: 500, //megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: true, //disabled by default
MaxSize: _maxsize, //megabytes
MaxBackups: _maxfiles,
MaxAge: _maxage, //days
Compress: _compress, //disabled by default
}
clog.SetOutput(LogOutput)
}

View file

@ -368,9 +368,9 @@ func TestLoggingErrorToFileConfig(t *testing.T) {
time.Sleep(500 * time.Millisecond)
//check file content
_, err = ioutil.ReadFile(expectedFile)
if err == nil {
t.Fatalf("file should be empty")
x, err := ioutil.ReadFile(expectedFile)
if err == nil && len(x) > 0 {
t.Fatalf("file should be empty, got '%s'", x)
}
os.Remove("./crowdsec.log")

View file

@ -42,9 +42,19 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level
if compress != nil {
_compress = *compress
}
/*cf. https://github.com/natefinch/lumberjack/issues/82
let's create the file beforehand w/ the right perms */
fname := cfgFolder + "/crowdsec.log"
// check if file exists
_, err := os.Stat(fname)
// create file if not exists, purposefully ignore errors
if os.IsNotExist(err) {
file, _ := os.OpenFile(fname, os.O_RDWR|os.O_CREATE, 0600)
file.Close()
}
LogOutput = &lumberjack.Logger{
Filename: cfgFolder + "/crowdsec.log",
Filename: fname,
MaxSize: _maxsize,
MaxBackups: _maxfiles,
MaxAge: _maxage,