diff --git a/pkg/client.go b/pkg/client.go index 1f956d4..46da891 100644 --- a/pkg/client.go +++ b/pkg/client.go @@ -1,6 +1,9 @@ package pkg -import am "github.com/prometheus/alertmanager/api/v2/client" +import ( + clientruntime "github.com/go-openapi/runtime/client" + am "github.com/prometheus/alertmanager/api/v2/client" +) const ( BasePath = "/api/v2" @@ -8,6 +11,15 @@ const ( // create alertmanager client func (tui *TUI) amClient() *am.AlertmanagerAPI { - cfg := am.DefaultTransportConfig().WithHost(tui.Config.Host + ":" + tui.Config.Port).WithBasePath(BasePath).WithSchemes([]string{tui.Config.Scheme}) - return am.NewHTTPClientWithConfig(nil, cfg) + address := tui.Config.Host + ":" + tui.Config.Port + scheme := []string{tui.Config.Scheme} + if tui.Config.Auth.Username != "" { + cr := clientruntime.New(address, BasePath, scheme) + cr.DefaultAuthentication = clientruntime.BasicAuth(tui.Config.Auth.Username, tui.Config.Auth.Password) + c := am.New(cr, nil) + return c + } else { + cfg := am.DefaultTransportConfig().WithHost(address).WithBasePath(BasePath).WithSchemes(scheme) + return am.NewHTTPClientWithConfig(nil, cfg) + } } diff --git a/pkg/config.go b/pkg/config.go index 53f31d1..7ade759 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -29,6 +29,8 @@ var ( host = fl.String("host", "", "Alertmanager host") port = fl.StringP("port", "p", "", "Alertmanager port") scheme = fl.StringP("scheme", "s", "", "Alertmanager scheme (http or https)") + username = fl.StringP("username", "", "", "Alertmanager username for basic auth") + password = fl.StringP("password", "", "", "Alertmanager password for basic auth") help = fl.BoolP("help", "h", false, "Show help") version = fl.BoolP("version", "v", false, "Show version") ) @@ -46,6 +48,12 @@ type Config struct { Host string `yaml:"host"` Port string `yaml:"port"` Scheme string `yaml:"scheme"` + Auth Auth `yaml:"auth"` +} + +type Auth struct { + Username string `yaml:"username"` + Password string `yaml:"password"` } func initConfig() Config { @@ -74,10 +82,23 @@ func initConfig() Config { log.Fatalf("Error: scheme must be http or https. Got: %s\n", *scheme) } - config := Config{ - Host: *host, - Port: *port, - Scheme: *scheme, + var config Config + if *username == "" && *password == "" { + config = Config{ + Host: *host, + Port: *port, + Scheme: *scheme, + } + } else { + config = Config{ + Host: *host, + Port: *port, + Scheme: *scheme, + Auth: Auth{ + Username: *username, + Password: *password, + }, + } } // if flags are set, overwrite config file @@ -85,6 +106,10 @@ func initConfig() Config { viper.Set("host", config.Host) viper.Set("port", config.Port) viper.Set("scheme", config.Scheme) + if config.Auth.Username != "" { + viper.Set("auth.username", config.Auth.Username) + viper.Set("auth.password", config.Auth.Password) + } if err := viper.WriteConfig(); err != nil { log.Fatalf("Error writing config file: %v", err) } diff --git a/pkg/status.go b/pkg/status.go index ce1dfeb..66d8118 100644 --- a/pkg/status.go +++ b/pkg/status.go @@ -22,6 +22,7 @@ func (tui *TUI) getStatus() { status, err := tui.amClient().General.GetStatus(params) if err != nil { tui.Errorf("Error fetching status data: %s", err) + return } tui.ClearPreviews()