forked from crowdsecurity/cs-custom-bouncer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
80 lines (67 loc) · 2.43 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/crowdsecurity/crowdsec/pkg/types"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v2"
)
type bouncerConfig struct {
BinPath string `yaml:"bin_path"` // path to binary
PidDir string `yaml:"piddir"`
UpdateFrequency string `yaml:"update_frequency"`
Daemon bool `yaml:"daemonize"`
LogMode string `yaml:"log_mode"`
LogDir string `yaml:"log_dir"`
LogLevel log.Level `yaml:"log_level"`
APIUrl string `yaml:"api_url"`
APIKey string `yaml:"api_key"`
CacheRetentionDuration time.Duration `yaml:"cache_retention_duration"`
}
func NewConfig(configPath string) (*bouncerConfig, error) {
var LogOutput *lumberjack.Logger //io.Writer
config := &bouncerConfig{}
configBuff, err := ioutil.ReadFile(configPath)
if err != nil {
return &bouncerConfig{}, fmt.Errorf("failed to read %s : %v", configPath, err)
}
err = yaml.UnmarshalStrict(configBuff, &config)
if err != nil {
return &bouncerConfig{}, fmt.Errorf("failed to unmarshal %s : %v", configPath, err)
}
if config.BinPath == "" || config.LogMode == "" {
return &bouncerConfig{}, fmt.Errorf("invalid configuration in %s", configPath)
}
_, err = os.Stat(config.BinPath)
if os.IsNotExist(err) {
return config, fmt.Errorf("binary '%s' doesn't exist", config.BinPath)
}
/*Configure logging*/
if err = types.SetDefaultLoggerConfig(config.LogMode, config.LogDir, config.LogLevel); err != nil {
log.Fatal(err.Error())
}
if config.LogMode == "file" {
if config.LogDir == "" {
config.LogDir = "/var/log/"
}
LogOutput = &lumberjack.Logger{
Filename: config.LogDir + "/crowdsec-custom-bouncer.log",
MaxSize: 500, //megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: true, //disabled by default
}
log.SetOutput(LogOutput)
log.SetFormatter(&log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true})
} else if config.LogMode != "stdout" {
return &bouncerConfig{}, fmt.Errorf("log mode '%s' unknown, expecting 'file' or 'stdout'", config.LogMode)
}
if config.CacheRetentionDuration == 0 {
log.Infof("cache_retention_duration defaults to 10 seconds")
config.CacheRetentionDuration = time.Duration(10 * time.Second)
}
return config, nil
}