2023-12-15 16:35:41 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-12-14 12:09:01 +01:00
|
|
|
package logranger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-12-22 15:24:58 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-12-14 12:09:01 +01:00
|
|
|
|
|
|
|
"github.com/kkyr/fig"
|
2023-12-22 15:24:58 +01:00
|
|
|
"github.com/wneessen/go-parsesyslog"
|
|
|
|
"github.com/wneessen/go-parsesyslog/rfc3164"
|
|
|
|
"github.com/wneessen/go-parsesyslog/rfc5424"
|
2023-12-14 12:09:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config holds all the global configuration settings that are parsed by fig
|
|
|
|
type Config struct {
|
|
|
|
// Server holds server specific configuration values
|
|
|
|
Server struct {
|
2023-12-22 17:31:41 +01:00
|
|
|
PIDFile string `fig:"pid_file" default:"/var/run/logranger.pid"`
|
|
|
|
RuleFile string `fig:"rule_file" default:"etc/logranger.rules.toml"`
|
2023-12-14 12:09:01 +01:00
|
|
|
}
|
|
|
|
Listener struct {
|
|
|
|
ListenerUnix struct {
|
|
|
|
Path string `fig:"path" default:"/var/tmp/logranger.sock"`
|
|
|
|
} `fig:"unix"`
|
|
|
|
ListenerTCP struct {
|
|
|
|
Addr string `fig:"addr" default:"0.0.0.0"`
|
|
|
|
Port uint `fig:"port" default:"9099"`
|
|
|
|
} `fig:"tcp"`
|
|
|
|
ListenerTLS struct {
|
|
|
|
Addr string `fig:"addr" default:"0.0.0.0"`
|
|
|
|
Port uint `fig:"port" default:"9099"`
|
|
|
|
CertPath string `fig:"cert_path"`
|
|
|
|
KeyPath string `fig:"key_path"`
|
|
|
|
} `fig:"tls"`
|
|
|
|
Type ListenerType `fig:"type" default:"unix"`
|
|
|
|
} `fig:"listener"`
|
|
|
|
Log struct {
|
2023-12-22 15:24:58 +01:00
|
|
|
Level string `fig:"level" default:"info"`
|
|
|
|
Extended bool `fig:"extended"`
|
2023-12-14 12:09:01 +01:00
|
|
|
} `fig:"log"`
|
2023-12-22 15:24:58 +01:00
|
|
|
Parser struct {
|
|
|
|
Type string `fig:"type" validate:"required"`
|
|
|
|
Timeout time.Duration `fig:"timeout" default:"500ms"`
|
2023-12-22 16:08:54 +01:00
|
|
|
} `fig:"parser"`
|
2023-12-22 15:24:58 +01:00
|
|
|
internal struct {
|
|
|
|
ParserType parsesyslog.ParserType
|
|
|
|
}
|
2023-12-14 12:09:01 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 20:17:56 +01:00
|
|
|
// NewConfig creates a new instance of the Config object by reading and loading
|
|
|
|
// configuration values. It takes in the file path and file name of the configuration
|
|
|
|
// file as parameters. It returns a pointer to the Config object and an error if
|
|
|
|
// there was a problem reading or loading the configuration.
|
2024-03-21 20:22:33 +01:00
|
|
|
func NewConfig(path, file string) (*Config, error) {
|
|
|
|
config := Config{}
|
|
|
|
_, err := os.Stat(fmt.Sprintf("%s/%s", path, file))
|
2023-12-14 12:09:01 +01:00
|
|
|
if err != nil {
|
2024-03-21 20:22:33 +01:00
|
|
|
return &config, fmt.Errorf("failed to read config: %w", err)
|
2023-12-14 12:09:01 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 20:22:33 +01:00
|
|
|
if err := fig.Load(&config, fig.Dirs(path), fig.File(file), fig.UseEnv("logranger")); err != nil {
|
|
|
|
return &config, fmt.Errorf("failed to load config: %w", err)
|
2023-12-14 12:09:01 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 15:24:58 +01:00
|
|
|
switch {
|
2024-03-21 20:22:33 +01:00
|
|
|
case strings.EqualFold(config.Parser.Type, "rfc3164"):
|
|
|
|
config.internal.ParserType = rfc3164.Type
|
|
|
|
case strings.EqualFold(config.Parser.Type, "rfc5424"):
|
|
|
|
config.internal.ParserType = rfc5424.Type
|
2023-12-22 15:24:58 +01:00
|
|
|
default:
|
2024-03-21 20:22:33 +01:00
|
|
|
return nil, fmt.Errorf("unknown parser type: %s", config.Parser.Type)
|
2023-12-22 15:24:58 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 20:22:33 +01:00
|
|
|
return &config, nil
|
2023-12-14 12:09:01 +01:00
|
|
|
}
|