Skip to content

Commit

Permalink
respect log permissions if file already exists (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Jun 8, 2023
1 parent c3f8f70 commit 236a06b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
19 changes: 8 additions & 11 deletions pkg/cfg/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"fmt"
"io"
"os"
"path/filepath"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
"gopkg.in/natefinch/lumberjack.v2"

"github.com/crowdsecurity/go-cs-lib/pkg/logtools"
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
)

type LoggingConfig struct {
LogLevel *log.Level `yaml:"log_level"`
LogMode string `yaml:"log_mode"`
LogDir string `yaml:"log_dir"`
LogLevel *log.Level `yaml:"log_level"`
LogMaxSize int `yaml:"log_max_size,omitempty"`
LogMaxFiles int `yaml:"log_max_files,omitempty"`
LogMaxAge int `yaml:"log_max_age,omitempty"`
Expand All @@ -27,13 +28,11 @@ func (c *LoggingConfig) LoggerForFile(fileName string) (io.Writer, error) {
return os.Stderr, nil
}

logPath, err := logtools.SetLogFilePermissions(c.LogDir, fileName)
if err != nil {
return nil, err
}
// default permissions will be 0600 from lumberjack
// and are preserved if the file already exists

l := &lumberjack.Logger{
Filename: logPath,
Filename: filepath.Join(c.LogDir, fileName),
MaxSize: c.LogMaxSize,
MaxBackups: c.LogMaxFiles,
MaxAge: c.LogMaxAge,
Expand All @@ -53,8 +52,7 @@ func (c *LoggingConfig) setDefaults() {
}

if c.LogLevel == nil {
defaultLevel := log.InfoLevel
c.LogLevel = &defaultLevel
c.LogLevel = ptr.Of(log.InfoLevel)
}

if c.LogMaxSize == 0 {
Expand All @@ -70,8 +68,7 @@ func (c *LoggingConfig) setDefaults() {
}

if c.CompressLogs == nil {
defaultCompress := true
c.CompressLogs = &defaultCompress
c.CompressLogs = ptr.Of(true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/bouncer/test_custom_bouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def test_bin_args(bouncer_with_lapi, tmp_path_factory):
res = lapi.cont.exec_run(f'cscli decisions add -i 1.2.3.{i}')
assert res.exit_code == 0

time.sleep(1)
time.sleep(2)

with open(data) as f:
lines = f.readlines()
Expand Down

0 comments on commit 236a06b

Please sign in to comment.