Skip to content

Commit

Permalink
Use promconfig.NewClientFromConfig
Browse files Browse the repository at this point in the history
Signed-off-by: clyang82 <[email protected]>
  • Loading branch information
clyang82 committed Dec 23, 2021
1 parent 7cac620 commit 17498b9
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 92 deletions.
38 changes: 0 additions & 38 deletions cli/config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,3 @@ func LoadHTTPConfig(httpConfigFile string) (*promconfig.HTTPClientConfig, error)
}
return httpConfig, nil
}

// FetchBearerToken returns bearerToken for the given authorization which type is bearer
func FetchBearerToken(auth *promconfig.Authorization) (string, error) {

if auth.Credentials != "" {
return string(auth.Credentials), nil
}

if auth.CredentialsFile != "" {
if _, err := os.Stat(auth.CredentialsFile); err != nil {
return "", err
}
b, err := ioutil.ReadFile(auth.CredentialsFile)
if err != nil {
return "", err
}
return string(b), nil
}
return "", nil
}

// FetchBasicAuthPassword returns password for the basic auth
func FetchBasicAuthPassword(auth *promconfig.BasicAuth) (string, error) {

if auth.PasswordFile != "" {
if _, err := os.Stat(auth.PasswordFile); err != nil {
return "", err
}
b, err := ioutil.ReadFile(auth.PasswordFile)
if err != nil {
return "", err
}
return string(b), nil
} else if auth.Password != "" {
return string(auth.Password), nil
}
return "", nil
}
30 changes: 2 additions & 28 deletions cli/config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,9 @@ func TestValidHTTPConfig(t *testing.T) {
if cfg.ProxyURL.String() != proxyURL {
t.Fatalf("Expected proxy_url is %q but got: %s", proxyURL, cfg.ProxyURL.String())
}

bearerToken, err := FetchBearerToken(cfg.Authorization)
if err != nil {
t.Fatalf("failed to fetch bearer token: %v", err)
}

if bearerToken != AuthorizationCredentials {
t.Fatalf("Expected bearer token is %q but got: %s", AuthorizationCredentials, bearerToken)
}
}

func TestValidBearerTokenFileHTTPConfig(t *testing.T) {

cfg, err := LoadHTTPConfig("testdata/http_config.bearer-token-file.good.yml")
if err != nil {
t.Fatalf("Error loading HTTP client config: %v", err)
}

bearerToken, err := FetchBearerToken(cfg.Authorization)
if err != nil {
t.Fatalf("failed to fetch bearer token: %v", err)
}

if bearerToken != AuthorizationCredentials {
t.Fatalf("Expected bearer token is %q but got: %s", AuthorizationCredentials, bearerToken)
}
}

func TestFetchBasicAuthPassword(t *testing.T) {
func TestValidBasicAuthHTTPConfig(t *testing.T) {

cfg, err := LoadHTTPConfig("testdata/http_config.basic_auth.good.yml")
if err != nil {
Expand All @@ -79,7 +53,7 @@ func TestFetchBasicAuthPassword(t *testing.T) {
t.Fatalf("Expected username is %q but got: %s", "user", cfg.BasicAuth.Username)
}

password, _ := FetchBasicAuthPassword(cfg.BasicAuth)
password := string(cfg.BasicAuth.Password)
if password != "password" {
t.Fatalf("Expected password is %q but got: %s", "password", password)
}
Expand Down
1 change: 0 additions & 1 deletion cli/config/testdata/bearer.token

This file was deleted.

2 changes: 1 addition & 1 deletion cli/config/testdata/http_config.basic_auth.good.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
basic_auth:
username: user
password: password
password: password
4 changes: 0 additions & 4 deletions cli/config/testdata/http_config.bearer-token-file.good.yml

This file was deleted.

23 changes: 3 additions & 20 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package cli

import (
"fmt"
"net/http"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -101,27 +100,11 @@ func NewAlertmanagerClient(amURL *url.URL) *client.Alertmanager {
kingpin.Fatalf("could not load HTTP config file: %v\n", err)
}

tlsConfig, err := promconfig.NewTLSConfig(&httpConfig.TLSConfig)
httpclient, err := promconfig.NewClientFromConfig(*httpConfig, "amtool")
if err != nil {
kingpin.Fatalf("failed to create TLS config: %v\n", err)
}

cr.Transport = &http.Transport{
TLSClientConfig: tlsConfig,
}

if httpConfig.BasicAuth != nil {
password, _ := config.FetchBasicAuthPassword(httpConfig.BasicAuth)
cr.DefaultAuthentication = clientruntime.BasicAuth(httpConfig.BasicAuth.Username, password)
}

if httpConfig.Authorization.Type == "Bearer" {
bearerToken, err := config.FetchBearerToken(httpConfig.Authorization)
if err != nil {
kingpin.Fatalf("failed to fetch bearer token: %v\n", err)
}
cr.DefaultAuthentication = clientruntime.BearerToken(bearerToken)
kingpin.Fatalf("could not create a new HTTP client: %v\n", err)
}
cr = clientruntime.NewWithClient(address, path.Join(amURL.Path, defaultAmApiv2path), schemes, httpclient)
}

c := client.New(cr, strfmt.Default)
Expand Down

0 comments on commit 17498b9

Please sign in to comment.