2021-02-06 15:17:35 +00:00
|
|
|
package cfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-30 21:32:48 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2021-02-06 15:17:35 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-01-30 21:32:48 +00:00
|
|
|
minServerPort = 1000
|
|
|
|
maxServerPort = 65535
|
|
|
|
socketEnvName = "F2B_COLLECTOR_SOCKET"
|
|
|
|
fileCollectorEnabledEnvName = "F2B_COLLECTOR_TEXT"
|
|
|
|
fileCollectorPathEnvName = "F2B_COLLECTOR_TEXT_PATH"
|
|
|
|
portEnvName = "F2B_WEB_PORT"
|
|
|
|
addressEnvName = "F2B_WEB_LISTEN_ADDRESS"
|
|
|
|
basicAuthUserEnvName = "F2B_WEB_BASICAUTH_USER"
|
|
|
|
basicAuthPassEnvName = "F2B_WEB_BASICAUTH_PASS"
|
2021-02-06 15:17:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppSettings struct {
|
2021-10-12 20:38:26 +00:00
|
|
|
VersionMode bool
|
2021-12-18 06:45:37 +00:00
|
|
|
MetricsAddress string
|
2021-10-12 20:38:26 +00:00
|
|
|
MetricsPort int
|
|
|
|
Fail2BanSocketPath string
|
|
|
|
FileCollectorPath string
|
|
|
|
FileCollectorEnabled bool
|
2022-01-14 21:36:49 +00:00
|
|
|
BasicAuthProvider *hashedBasicAuth
|
2021-02-06 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 21:32:48 +00:00
|
|
|
func init() {
|
|
|
|
kingpin.HelpFlag.Short('h')
|
|
|
|
}
|
|
|
|
|
2021-02-06 15:17:35 +00:00
|
|
|
func Parse() *AppSettings {
|
2022-01-30 21:32:48 +00:00
|
|
|
settings := &AppSettings{}
|
|
|
|
readParamsFromCli(settings)
|
|
|
|
settings.validateFlags()
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
|
|
|
func readParamsFromCli(settings *AppSettings) {
|
|
|
|
versionMode := kingpin.
|
|
|
|
Flag("version", "show version info and exit").
|
|
|
|
Default("false").
|
|
|
|
Bool()
|
|
|
|
socketPath := kingpin.
|
|
|
|
Flag("socket", "path to the fail2ban server socket").
|
2022-02-18 22:15:32 +00:00
|
|
|
Default("/var/run/fail2ban/fail2ban.sock").
|
2022-01-30 21:32:48 +00:00
|
|
|
Envar(socketEnvName).
|
|
|
|
String()
|
|
|
|
fileCollectorEnabled := kingpin.
|
|
|
|
Flag("collector.textfile", "enable the textfile collector").
|
|
|
|
Default("false").
|
|
|
|
Envar(fileCollectorEnabledEnvName).
|
|
|
|
Bool()
|
|
|
|
fileCollectorPath := kingpin.
|
|
|
|
Flag("collector.textfile.directory", "directory to read text files with metrics from").
|
|
|
|
Default("").
|
|
|
|
Envar(fileCollectorPathEnvName).
|
|
|
|
String()
|
|
|
|
port := kingpin.
|
|
|
|
Flag("port", "port to use for the metrics server").
|
|
|
|
Default("9191").
|
|
|
|
Envar(portEnvName).
|
|
|
|
Int()
|
|
|
|
address := kingpin.
|
|
|
|
Flag("web.listen-address", "address to use for the metrics server").
|
|
|
|
Default("0.0.0.0").
|
|
|
|
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 22:51:13 +00:00
|
|
|
kingpin.Parse()
|
|
|
|
|
2022-01-30 21:32:48 +00:00
|
|
|
settings.VersionMode = *versionMode
|
|
|
|
settings.MetricsPort = *port
|
|
|
|
settings.MetricsAddress = *address
|
|
|
|
settings.Fail2BanSocketPath = *socketPath
|
|
|
|
settings.FileCollectorEnabled = *fileCollectorEnabled
|
|
|
|
settings.FileCollectorPath = *fileCollectorPath
|
|
|
|
settings.setBasicAuthValues(*rawBasicAuthUsername, *rawBasicAuthPassword)
|
2021-02-06 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 21:36:49 +00:00
|
|
|
func (settings *AppSettings) setBasicAuthValues(rawUsername, rawPassword string) {
|
|
|
|
settings.BasicAuthProvider = newHashedBasicAuth(rawUsername, rawPassword)
|
|
|
|
}
|
|
|
|
|
2021-02-06 15:17:35 +00:00
|
|
|
func (settings *AppSettings) validateFlags() {
|
|
|
|
var flagsValid = true
|
|
|
|
if !settings.VersionMode {
|
2021-10-15 18:02:26 +00:00
|
|
|
if settings.Fail2BanSocketPath == "" {
|
2022-02-19 11:21:58 +00:00
|
|
|
fmt.Println("error: fail2ban socket path must not be blank")
|
2021-02-06 15:17:35 +00:00
|
|
|
flagsValid = false
|
|
|
|
}
|
|
|
|
if settings.MetricsPort < minServerPort || settings.MetricsPort > maxServerPort {
|
2022-02-19 11:21:58 +00:00
|
|
|
fmt.Printf("error: invalid server port, must be within %d and %d (found %d)\n",
|
2021-02-06 15:17:35 +00:00
|
|
|
minServerPort, maxServerPort, settings.MetricsPort)
|
|
|
|
flagsValid = false
|
|
|
|
}
|
2021-10-12 20:38:26 +00:00
|
|
|
if settings.FileCollectorEnabled && settings.FileCollectorPath == "" {
|
2022-02-19 11:21:58 +00:00
|
|
|
fmt.Println("error: file collector directory path must not be empty if collector enabled")
|
2021-10-12 20:38:26 +00:00
|
|
|
flagsValid = false
|
|
|
|
}
|
2022-01-14 21:36:49 +00:00
|
|
|
if (len(settings.BasicAuthProvider.username) > 0) != (len(settings.BasicAuthProvider.password) > 0) {
|
2022-02-19 11:21:58 +00:00
|
|
|
fmt.Println("error: to enable basic auth both the username and the password must be provided")
|
2022-01-14 21:36:49 +00:00
|
|
|
flagsValid = false
|
|
|
|
}
|
2021-02-06 15:17:35 +00:00
|
|
|
}
|
|
|
|
if !flagsValid {
|
2022-02-19 11:21:58 +00:00
|
|
|
kingpin.Usage()
|
2021-02-06 15:17:35 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|