Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for alertmanagers with basic auth. #7

Merged
merged 1 commit into from
Sep 17, 2023
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
18 changes: 15 additions & 3 deletions pkg/client.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
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"
)

// 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)
}
}
33 changes: 29 additions & 4 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand All @@ -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 {
Expand Down Expand Up @@ -74,17 +82,34 @@ 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
if config.Host != "" && config.Port != "" && config.Scheme != "" {
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)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down