From 17498b9a6a3b699b06151ce5170ea7dacdf38926 Mon Sep 17 00:00:00 2001 From: clyang82 Date: Thu, 23 Dec 2021 22:27:55 +0800 Subject: [PATCH] Use promconfig.NewClientFromConfig Signed-off-by: clyang82 --- cli/config/http_config.go | 38 ------------------- cli/config/http_config_test.go | 30 +-------------- cli/config/testdata/bearer.token | 1 - .../testdata/http_config.basic_auth.good.yml | 2 +- .../http_config.bearer-token-file.good.yml | 4 -- cli/root.go | 23 ++--------- 6 files changed, 6 insertions(+), 92 deletions(-) delete mode 100644 cli/config/testdata/bearer.token delete mode 100644 cli/config/testdata/http_config.bearer-token-file.good.yml diff --git a/cli/config/http_config.go b/cli/config/http_config.go index 81557898ba..4ac7eb81ee 100644 --- a/cli/config/http_config.go +++ b/cli/config/http_config.go @@ -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 -} diff --git a/cli/config/http_config_test.go b/cli/config/http_config_test.go index 2957a4736e..b3c792fd15 100644 --- a/cli/config/http_config_test.go +++ b/cli/config/http_config_test.go @@ -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 { @@ -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) } diff --git a/cli/config/testdata/bearer.token b/cli/config/testdata/bearer.token deleted file mode 100644 index 24051f03db..0000000000 --- a/cli/config/testdata/bearer.token +++ /dev/null @@ -1 +0,0 @@ -theanswertothegreatquestionoflifetheuniverseandeverythingisfortytwo \ No newline at end of file diff --git a/cli/config/testdata/http_config.basic_auth.good.yml b/cli/config/testdata/http_config.basic_auth.good.yml index ee0215a9be..cb3ba6a9e5 100644 --- a/cli/config/testdata/http_config.basic_auth.good.yml +++ b/cli/config/testdata/http_config.basic_auth.good.yml @@ -1,3 +1,3 @@ basic_auth: username: user - password: password \ No newline at end of file + password: password diff --git a/cli/config/testdata/http_config.bearer-token-file.good.yml b/cli/config/testdata/http_config.bearer-token-file.good.yml deleted file mode 100644 index 863505e36f..0000000000 --- a/cli/config/testdata/http_config.bearer-token-file.good.yml +++ /dev/null @@ -1,4 +0,0 @@ -authorization: - type: Bearer - credentials_file: testdata/bearer.token -proxy_url: "http://remote.host" diff --git a/cli/root.go b/cli/root.go index 62e4dea01d..0cd9f45e6c 100644 --- a/cli/root.go +++ b/cli/root.go @@ -15,7 +15,6 @@ package cli import ( "fmt" - "net/http" "net/url" "os" "path" @@ -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)