Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@ Environment variables can be used to set parameters without using command line f
| `F2BD_SOCKET` | `-s, --socket` | Fail2ban socket path | `/var/run/fail2ban/fail2ban.sock` |
| `F2BD_TRUST_PROXY_HEADERS` | `--trust-proxy-headers` | Trust proxy headers like X-Forwarded-For | `false` |

### Config file

It is also possible to configure the application using a config file.
Supported config file formats are the ones supported by the [viper](https://github.com/spf13/viper) library.

The config file can be located at `/etc/fail2ban-dashboard/`, the user home directory `~/.config/fail2ban-dashboard/` or the current working directory.

For example, for a TOML file located at `/etc/fail2ban-dashboard/config.toml` the chaning the address is like

```toml
address="127.0.0.1:4000"
```

Supported configurations are

| Configuration |
|-----------------|
| socket |
| address |
| auth-user |
| auth-password |
| cache-dir |
| log-level |
| base-path |
| metrics-address |

## Dashboard

### Web application
Expand Down
14 changes: 14 additions & 0 deletions cmd/fail2ban-dashboard/fail2ban-dashboard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -36,6 +37,19 @@ var versionCmd = &cobra.Command{
}

func setupRootCommand() {
// Add search paths to find the file
viper.SetConfigName("config")
viper.AddConfigPath("/etc/fail2ban-dashboard/")
viper.AddConfigPath("$HOME/.config/fail2ban-dashboard")
viper.AddConfigPath(".")

if err := viper.ReadInConfig(); err != nil {
if _, ok := errors.AsType[viper.ConfigFileNotFoundError](err); !ok {
fmt.Printf("Could not parse config file: %s\n", err)
os.Exit(1)
}
}

viper.AutomaticEnv()
viper.SetEnvPrefix("F2BD")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
Expand Down
15 changes: 12 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
_ "embed"
"encoding/binary"
"encoding/hex"
"fmt"
"html/template"
"net"
Expand Down Expand Up @@ -103,6 +104,14 @@ type detailData struct {
Jail store.Jail
}

func generateRandomPassword() string {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return "default-password-change-me"
}
return hex.EncodeToString(b)
}

func RegisterDashboardEndpoints(app *fiber.App, dataStore *store.DataStore, geoIP *geoip.GeoIP, configuration *Configuration) error {

templateFunctions := template.FuncMap{
Expand Down Expand Up @@ -197,13 +206,13 @@ func RegisterDashboardEndpoints(app *fiber.App, dataStore *store.DataStore, geoI
}
log.Infof("Basic authentication username set to %s", configuration.AuthUser)
if configuration.AuthPassword == "" {
configuration.AuthPassword = rand.Text()
configuration.AuthPassword = generateRandomPassword()
log.Infof("Basic authentication password set to %s", configuration.AuthPassword)
}

app.Use(basicauth.New(basicauth.Config{
Users: map[string]string{
configuration.AuthUser: configuration.AuthPassword,
Authorizer: func(user, password string, c fiber.Ctx) bool {
return user == configuration.AuthUser && password == configuration.AuthPassword
},
}))
}
Expand Down