2021-02-06 16:17:35 +01:00
|
|
|
package cfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
minServerPort = 1000
|
|
|
|
maxServerPort = 65535
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppSettings struct {
|
2021-10-12 22:38:26 +02:00
|
|
|
VersionMode bool
|
2021-12-18 07:45:37 +01:00
|
|
|
MetricsAddress string
|
2021-10-12 22:38:26 +02:00
|
|
|
MetricsPort int
|
|
|
|
Fail2BanSocketPath string
|
|
|
|
FileCollectorPath string
|
|
|
|
FileCollectorEnabled bool
|
2022-01-14 22:36:49 +01:00
|
|
|
BasicAuthProvider *hashedBasicAuth
|
2021-02-06 16:17:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Parse() *AppSettings {
|
2022-01-14 22:36:49 +01:00
|
|
|
var rawBasicAuthUsername string
|
|
|
|
var rawBasicAuthPassword string
|
|
|
|
|
2021-02-06 16:17:35 +01:00
|
|
|
appSettings := &AppSettings{}
|
|
|
|
flag.BoolVar(&appSettings.VersionMode, "version", false, "show version info and exit")
|
2021-12-18 07:45:37 +01:00
|
|
|
flag.StringVar(&appSettings.MetricsAddress, "web.listen-address", "0.0.0.0", "address to use for the metrics server")
|
2021-02-06 16:17:35 +01:00
|
|
|
flag.IntVar(&appSettings.MetricsPort, "port", 9191, "port to use for the metrics server")
|
2021-08-29 13:50:53 +02:00
|
|
|
flag.StringVar(&appSettings.Fail2BanSocketPath, "socket", "", "path to the fail2ban server socket")
|
2021-10-12 22:38:26 +02:00
|
|
|
flag.BoolVar(&appSettings.FileCollectorEnabled, "collector.textfile", false, "enable the textfile collector")
|
|
|
|
flag.StringVar(&appSettings.FileCollectorPath, "collector.textfile.directory", "", "directory to read text files with metrics from")
|
2022-01-14 22:36:49 +01:00
|
|
|
flag.StringVar(&rawBasicAuthUsername, "web.basic-auth.username", "", "username to use to protect endpoints with basic auth")
|
|
|
|
flag.StringVar(&rawBasicAuthPassword, "web.basic-auth.password", "", "password to use to protect endpoints with basic auth")
|
2021-02-06 16:17:35 +01:00
|
|
|
|
|
|
|
flag.Parse()
|
2022-01-14 22:36:49 +01:00
|
|
|
appSettings.setBasicAuthValues(rawBasicAuthUsername, rawBasicAuthPassword)
|
2021-02-06 16:17:35 +01:00
|
|
|
appSettings.validateFlags()
|
|
|
|
return appSettings
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:36:49 +01:00
|
|
|
func (settings *AppSettings) setBasicAuthValues(rawUsername, rawPassword string) {
|
|
|
|
settings.BasicAuthProvider = newHashedBasicAuth(rawUsername, rawPassword)
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:17:35 +01:00
|
|
|
func (settings *AppSettings) validateFlags() {
|
|
|
|
var flagsValid = true
|
|
|
|
if !settings.VersionMode {
|
2021-10-15 20:02:26 +02:00
|
|
|
if settings.Fail2BanSocketPath == "" {
|
|
|
|
fmt.Println("fail2ban socket path must not be blank")
|
2021-02-06 16:17:35 +01:00
|
|
|
flagsValid = false
|
|
|
|
}
|
|
|
|
if settings.MetricsPort < minServerPort || settings.MetricsPort > maxServerPort {
|
|
|
|
fmt.Printf("invalid server port, must be within %d and %d (found %d)\n",
|
|
|
|
minServerPort, maxServerPort, settings.MetricsPort)
|
|
|
|
flagsValid = false
|
|
|
|
}
|
2021-10-12 22:38:26 +02:00
|
|
|
if settings.FileCollectorEnabled && settings.FileCollectorPath == "" {
|
|
|
|
fmt.Printf("file collector directory path must not be empty if collector enabled\n")
|
|
|
|
flagsValid = false
|
|
|
|
}
|
2022-01-14 22:36:49 +01:00
|
|
|
if (len(settings.BasicAuthProvider.username) > 0) != (len(settings.BasicAuthProvider.password) > 0) {
|
|
|
|
fmt.Printf("to enable basic auth both the username and the password must be provided")
|
|
|
|
flagsValid = false
|
|
|
|
}
|
2021-02-06 16:17:35 +01:00
|
|
|
}
|
|
|
|
if !flagsValid {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|