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

respect log permissions if file already exists #73

Merged
merged 4 commits into from
Jun 8, 2023
Merged
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
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
12 changes: 6 additions & 6 deletions test/bouncer/test_custom_bouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_no_custom_binary(crowdsec, bouncer, cb_cfg_factory):
cb.wait_for_lines_fnmatch([
"*unable to load configuration: binary '/does/not/exist' doesn't exist*",
])
cb.proc.wait(timeout=0.2)
cb.proc.wait()
assert not cb.proc.is_running()


Expand All @@ -19,7 +19,7 @@ def test_no_api_key(crowdsec, bouncer, cb_stream_cfg_factory):
cb.wait_for_lines_fnmatch([
"*unable to configure bouncer: config does not contain LAPI key or certificate*",
])
cb.proc.wait(timeout=0.2)
cb.proc.wait()
assert not cb.proc.is_running()

cfg['api_key'] = ''
Expand All @@ -28,7 +28,7 @@ def test_no_api_key(crowdsec, bouncer, cb_stream_cfg_factory):
cb.wait_for_lines_fnmatch([
"*unable to configure bouncer: config does not contain LAPI key or certificate*",
])
cb.proc.wait(timeout=0.2)
cb.proc.wait()
assert not cb.proc.is_running()


Expand Down Expand Up @@ -58,7 +58,7 @@ def test_bad_api_key(crowdsec, bouncer, cb_stream_cfg_factory):
"*auth-api: auth with api key failed return nil response, error*",
"*process terminated with error: bouncer stream halted*",
])
cb.proc.wait(timeout=1)
cb.proc.wait()
assert not cb.proc.is_running()


Expand Down Expand Up @@ -166,7 +166,7 @@ def test_binary_monitor(bouncer_with_lapi):
# This will exceed max_retry and the bouncer will stop
assert cb.proc.is_running()
cb.halt_children()
cb.proc.wait(timeout=0.5)
cb.proc.wait()
assert not cb.proc.is_running()
cb.wait_for_lines_fnmatch([
"*Binary exited (retry 3/3): signal: killed*",
Expand Down 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