2021-02-06 15:17:35 +00:00
package cfg
import (
"fmt"
2023-06-21 10:31:33 +00:00
"log"
2021-02-06 15:17:35 +00:00
"os"
2023-06-20 20:16:23 +00:00
"github.com/alecthomas/kong"
2023-06-21 10:31:33 +00:00
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/auth"
2021-02-06 15:17:35 +00:00
)
2023-06-20 20:16:23 +00:00
var cliStruct struct {
VersionMode bool ` name:"version" short:"v" help:"Show version info and exit" `
2023-06-22 16:09:36 +00:00
DryRunMode bool ` name:"dry-run" help:"Attempt to connect to the fail2ban socket then exit before starting the server" `
2023-06-20 20:16:23 +00:00
ServerAddress string ` name:"web.listen-address" env:"F2B_WEB_LISTEN_ADDRESS" help:"Address to use for the metrics server" default:"$ { default_address}" `
F2bSocketPath string ` name:"collector.f2b.socket" env:"F2B_COLLECTOR_SOCKET" help:"Path to the fail2ban server socket" default:"$ { default_socket}" `
ExitOnSocketError bool ` name:"collector.f2b.exit-on-socket-connection-error" env:"F2B_EXIT_ON_SOCKET_CONN_ERROR" help:"When set to true the exporter will immediately exit on a fail2ban socket connection error" `
TextFileExporterPath string ` name:"collector.textfile.directory" env:"F2B_COLLECTOR_TEXT_PATH" help:"Directory to read text files with metrics from" `
BasicAuthUser string ` name:"web.basic-auth.username" env:"F2B_WEB_BASICAUTH_USER" help:"Username to use to protect endpoints with basic auth" `
BasicAuthPass string ` name:"web.basic-auth.password" env:"F2B_WEB_BASICAUTH_PASS" help:"Password to use to protect endpoints with basic auth" `
2022-01-30 21:32:48 +00:00
}
2021-02-06 15:17:35 +00:00
func Parse ( ) * AppSettings {
2023-06-20 20:16:23 +00:00
ctx := kong . Parse (
& cliStruct ,
kong . Vars {
"default_socket" : "/var/run/fail2ban/fail2ban.sock" ,
"default_address" : ":9191" ,
} ,
kong . Name ( "fail2ban_exporter" ) ,
kong . Description ( "🚀 Export prometheus metrics from a running Fail2Ban instance" ) ,
kong . UsageOnError ( ) ,
)
2022-01-30 21:32:48 +00:00
2023-06-20 20:16:23 +00:00
validateFlags ( ctx )
settings := & AppSettings {
VersionMode : cliStruct . VersionMode ,
2023-06-22 16:09:36 +00:00
DryRunMode : cliStruct . DryRunMode ,
2023-06-20 20:16:23 +00:00
MetricsAddress : cliStruct . ServerAddress ,
Fail2BanSocketPath : cliStruct . F2bSocketPath ,
FileCollectorPath : cliStruct . TextFileExporterPath ,
ExitOnSocketConnError : cliStruct . ExitOnSocketError ,
2023-06-21 10:31:33 +00:00
AuthProvider : createAuthProvider ( ) ,
2023-06-20 20:16:23 +00:00
}
return settings
2022-01-14 21:36:49 +00:00
}
2023-06-21 10:31:33 +00:00
func createAuthProvider ( ) auth . AuthProvider {
username := cliStruct . BasicAuthUser
password := cliStruct . BasicAuthPass
if len ( username ) == 0 && len ( password ) == 0 {
return auth . NewEmptyAuthProvider ( )
}
log . Print ( "basic auth enabled" )
return auth . NewBasicAuthProvider ( username , password )
}
2023-06-20 20:16:23 +00:00
func validateFlags ( cliCtx * kong . Context ) {
2021-02-06 15:17:35 +00:00
var flagsValid = true
2023-06-20 20:16:23 +00:00
var messages = [ ] string { }
if ! cliStruct . VersionMode {
if cliStruct . F2bSocketPath == "" {
messages = append ( messages , "error: fail2ban socket path must not be blank" )
2021-02-06 15:17:35 +00:00
flagsValid = false
}
2023-06-20 20:16:23 +00:00
if cliStruct . ServerAddress == "" {
messages = append ( messages , "error: invalid server address, must not be blank" )
2021-10-12 20:38:26 +00:00
flagsValid = false
}
2023-06-20 20:16:23 +00:00
if ( len ( cliStruct . BasicAuthUser ) > 0 ) != ( len ( cliStruct . BasicAuthPass ) > 0 ) {
messages = append ( messages , "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 {
2023-06-20 20:16:23 +00:00
cliCtx . PrintUsage ( false )
fmt . Println ( )
for i := 0 ; i < len ( messages ) ; i ++ {
fmt . Println ( messages [ i ] )
}
2021-02-06 15:17:35 +00:00
os . Exit ( 1 )
}
}