2021-02-06 16:17:35 +01:00
|
|
|
package cfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-30 22:32:48 +01:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2021-02-06 16:17:35 +01:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-02-19 18:01:49 +01:00
|
|
|
socketEnvName = "F2B_COLLECTOR_SOCKET"
|
|
|
|
fileCollectorPathEnvName = "F2B_COLLECTOR_TEXT_PATH"
|
|
|
|
addressEnvName = "F2B_WEB_LISTEN_ADDRESS"
|
|
|
|
basicAuthUserEnvName = "F2B_WEB_BASICAUTH_USER"
|
|
|
|
basicAuthPassEnvName = "F2B_WEB_BASICAUTH_PASS"
|
2021-02-06 16:17:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppSettings struct {
|
2022-02-19 18:01:49 +01:00
|
|
|
VersionMode bool
|
|
|
|
MetricsAddress string
|
|
|
|
Fail2BanSocketPath string
|
|
|
|
FileCollectorPath string
|
|
|
|
BasicAuthProvider *hashedBasicAuth
|
2021-02-06 16:17:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-30 22:32:48 +01:00
|
|
|
func init() {
|
|
|
|
kingpin.HelpFlag.Short('h')
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:17:35 +01:00
|
|
|
func Parse() *AppSettings {
|
2022-01-30 22:32:48 +01:00
|
|
|
settings := &AppSettings{}
|
|
|
|
readParamsFromCli(settings)
|
|
|
|
settings.validateFlags()
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
|
|
|
func readParamsFromCli(settings *AppSettings) {
|
|
|
|
versionMode := kingpin.
|
|
|
|
Flag("version", "show version info and exit").
|
2022-02-19 18:01:49 +01:00
|
|
|
Short('v').
|
2022-01-30 22:32:48 +01:00
|
|
|
Default("false").
|
|
|
|
Bool()
|
|
|
|
socketPath := kingpin.
|
2022-02-19 18:01:49 +01:00
|
|
|
Flag("collector.f2b.socket", "path to the fail2ban server socket").
|
2022-02-18 23:15:32 +01:00
|
|
|
Default("/var/run/fail2ban/fail2ban.sock").
|
2022-01-30 22:32:48 +01:00
|
|
|
Envar(socketEnvName).
|
|
|
|
String()
|
|
|
|
fileCollectorPath := kingpin.
|
|
|
|
Flag("collector.textfile.directory", "directory to read text files with metrics from").
|
|
|
|
Default("").
|
|
|
|
Envar(fileCollectorPathEnvName).
|
|
|
|
String()
|
|
|
|
address := kingpin.
|
|
|
|
Flag("web.listen-address", "address to use for the metrics server").
|
2022-02-19 18:01:49 +01:00
|
|
|
Default(":9191").
|
2022-01-30 22:32:48 +01:00
|
|
|
Envar(addressEnvName).
|
|
|
|
String()
|
|
|
|
rawBasicAuthUsername := kingpin.
|
|
|
|
Flag("web.basic-auth.username", "username to use to protect endpoints with basic auth").
|
|
|
|
Default("").
|
|
|
|
Envar(basicAuthUserEnvName).
|
|
|
|
String()
|
|
|
|
rawBasicAuthPassword := kingpin.
|
|
|
|
Flag("web.basic-auth.password", "password to use to protect endpoints with basic auth").
|
|
|
|
Default("").
|
|
|
|
Envar(basicAuthPassEnvName).
|
|
|
|
String()
|
|
|
|
|
2022-01-31 23:51:13 +01:00
|
|
|
kingpin.Parse()
|
|
|
|
|
2022-01-30 22:32:48 +01:00
|
|
|
settings.VersionMode = *versionMode
|
|
|
|
settings.MetricsAddress = *address
|
|
|
|
settings.Fail2BanSocketPath = *socketPath
|
|
|
|
settings.FileCollectorPath = *fileCollectorPath
|
|
|
|
settings.setBasicAuthValues(*rawBasicAuthUsername, *rawBasicAuthPassword)
|
2021-02-06 16:17:35 +01:00
|
|
|
}
|
|
|
|
|
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 == "" {
|
2022-02-19 12:21:58 +01:00
|
|
|
fmt.Println("error: fail2ban socket path must not be blank")
|
2021-02-06 16:17:35 +01:00
|
|
|
flagsValid = false
|
|
|
|
}
|
2022-02-19 18:01:49 +01:00
|
|
|
if settings.MetricsAddress == "" {
|
|
|
|
fmt.Println("error: invalid server address, must not be blank")
|
2021-10-12 22:38:26 +02:00
|
|
|
flagsValid = false
|
|
|
|
}
|
2022-01-14 22:36:49 +01:00
|
|
|
if (len(settings.BasicAuthProvider.username) > 0) != (len(settings.BasicAuthProvider.password) > 0) {
|
2022-02-19 12:21:58 +01:00
|
|
|
fmt.Println("error: to enable basic auth both the username and the password must be provided")
|
2022-01-14 22:36:49 +01:00
|
|
|
flagsValid = false
|
|
|
|
}
|
2021-02-06 16:17:35 +01:00
|
|
|
}
|
|
|
|
if !flagsValid {
|
2022-02-19 12:21:58 +01:00
|
|
|
kingpin.Usage()
|
2021-02-06 16:17:35 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|