2021-02-07 15:05:35 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-11 23:37:13 +01:00
|
|
|
"io/ioutil"
|
2021-02-11 23:30:36 +01:00
|
|
|
"os"
|
|
|
|
|
2021-02-13 23:42:32 +01:00
|
|
|
lrh_gl "github.com/gemnasium/logrus-graylog-hook/v3"
|
2021-02-14 10:38:30 +01:00
|
|
|
"github.com/karrick/golf"
|
2021-02-10 23:58:14 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-02-11 23:30:36 +01:00
|
|
|
lrh_wr "github.com/sirupsen/logrus/hooks/writer"
|
2021-02-07 15:05:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-02-10 23:58:14 +01:00
|
|
|
// This structure contains all values that may be set from the command line.
|
|
|
|
cliFlags struct {
|
|
|
|
// The path to the configuration file.
|
|
|
|
cfgFile string
|
|
|
|
// The name of the instance, to be used in logs.
|
|
|
|
instance string
|
2021-02-11 23:37:13 +01:00
|
|
|
// Quiet mode. Will disable logging to stderr.
|
|
|
|
quiet bool
|
2021-02-10 23:58:14 +01:00
|
|
|
// The log level.
|
|
|
|
logLevel string
|
2021-02-11 23:30:36 +01:00
|
|
|
// A file to write logs into.
|
|
|
|
logFile string
|
2021-02-13 23:42:32 +01:00
|
|
|
// Graylog server to send logs to (using GELF/UDP). Format is <hostname>:<port>.
|
|
|
|
logGraylog string
|
2021-02-10 23:58:14 +01:00
|
|
|
}
|
2021-02-07 15:05:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-10 23:58:14 +01:00
|
|
|
// The logging context.
|
|
|
|
log *logrus.Entry
|
2021-02-07 15:05:35 +01:00
|
|
|
)
|
|
|
|
|
2021-02-10 23:58:14 +01:00
|
|
|
// Parse command line options.
|
|
|
|
func parseCommandLine() cliFlags {
|
2021-02-14 10:38:30 +01:00
|
|
|
var help bool
|
2021-02-10 23:58:14 +01:00
|
|
|
flags := cliFlags{}
|
2021-02-14 10:38:30 +01:00
|
|
|
|
|
|
|
golf.StringVarP(&flags.cfgFile, 'c', "config", "./graylog-groups.yml", "Set the configuration file.")
|
|
|
|
golf.StringVarP(&flags.logFile, 'f', "log-file", "", "Log file.")
|
|
|
|
golf.StringVarP(&flags.logGraylog, 'g', "log-graylog", "", "Log to Graylog server (format: <host>:<port>).")
|
|
|
|
golf.BoolVarP(&help, 'h', "help", false, "Display command line help and exit.")
|
|
|
|
golf.StringVarP(&flags.instance, 'i', "instance", "", "Specify an instance identifier.")
|
2021-02-14 10:44:28 +01:00
|
|
|
golf.StringVarP(&flags.logLevel, 'L', "log-level", "info", "Log level to use.")
|
2021-02-14 10:38:30 +01:00
|
|
|
golf.BoolVarP(&flags.quiet, 'q', "quiet", false, "Quiet mode; prevents logging to stderr.")
|
|
|
|
|
|
|
|
golf.Parse()
|
|
|
|
if help {
|
|
|
|
golf.Usage()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2021-02-10 23:58:14 +01:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the logging context.
|
|
|
|
func getLoggingContext(instance string) *logrus.Entry {
|
|
|
|
logFields := logrus.Fields{
|
|
|
|
"application": "graylog",
|
|
|
|
"component": "graylog-groups",
|
|
|
|
}
|
|
|
|
if instance != "" {
|
|
|
|
logFields["instance"] = instance
|
|
|
|
}
|
|
|
|
return logrus.WithFields(logFields)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the log level
|
2021-02-11 22:31:00 +01:00
|
|
|
func toLogLevel(cliLevel string) logrus.Level {
|
2021-02-10 23:58:14 +01:00
|
|
|
if cliLevel == "" {
|
2021-02-11 22:31:00 +01:00
|
|
|
return logrus.InfoLevel
|
2021-02-10 23:58:14 +01:00
|
|
|
}
|
2021-02-11 22:31:00 +01:00
|
|
|
lvl, err := logrus.ParseLevel(cliLevel)
|
|
|
|
if err == nil {
|
|
|
|
return lvl
|
|
|
|
}
|
|
|
|
log.WithField("level", cliLevel).Warning("Invalid log level on command line")
|
|
|
|
return logrus.InfoLevel
|
2021-02-10 23:58:14 +01:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:30:36 +01:00
|
|
|
// Add a file writer hook to the logging library.
|
|
|
|
func configureLogFile(path string) {
|
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
|
|
|
if err == nil {
|
2021-02-11 23:34:00 +01:00
|
|
|
log.Logger.AddHook(&lrh_wr.Hook{
|
2021-02-11 23:30:36 +01:00
|
|
|
Writer: file,
|
|
|
|
LogLevels: logrus.AllLevels,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"error": err,
|
|
|
|
"file": path,
|
|
|
|
}).Error("Could not open log file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 23:58:14 +01:00
|
|
|
// Configure the logging library based on the various command line flags.
|
|
|
|
func configureLogging(flags cliFlags) {
|
|
|
|
log = getLoggingContext(flags.instance)
|
2021-02-13 23:09:26 +01:00
|
|
|
log.Logger.SetFormatter(&logrus.TextFormatter{
|
|
|
|
DisableColors: true,
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
2021-02-11 22:31:00 +01:00
|
|
|
log.Logger.SetLevel(toLogLevel(flags.logLevel))
|
2021-02-11 23:30:36 +01:00
|
|
|
if flags.logFile != "" {
|
|
|
|
configureLogFile(flags.logFile)
|
|
|
|
}
|
2021-02-13 23:42:32 +01:00
|
|
|
if flags.logGraylog != "" {
|
|
|
|
log.Logger.AddHook(lrh_gl.NewGraylogHook(flags.logGraylog, nil))
|
|
|
|
}
|
2021-02-11 23:37:13 +01:00
|
|
|
if flags.quiet {
|
|
|
|
log.Logger.SetOutput(ioutil.Discard)
|
|
|
|
}
|
2021-02-10 23:58:14 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 15:05:35 +01:00
|
|
|
func main() {
|
2021-02-10 23:58:14 +01:00
|
|
|
flags := parseCommandLine()
|
|
|
|
configureLogging(flags)
|
|
|
|
configuration := loadConfiguration(flags)
|
2021-02-07 15:05:35 +01:00
|
|
|
glUsers := getGraylogUsers(configuration.Graylog)
|
|
|
|
groups := readLdapGroups(configuration)
|
|
|
|
applyMapping(configuration, glUsers, groups)
|
|
|
|
}
|