From 5849521e905f7d27cb2600977ca40598b657e8e2 Mon Sep 17 00:00:00 2001 From: Filip Petkovski Date: Tue, 30 Nov 2021 05:02:07 +0100 Subject: [PATCH] promtool: Fix credentials file check (#9883) The promtool check config command still uses the bearer_token_file field which is deprecated in favour of authorization.credentials_file. This commit modifies the command to use the new field insted. Fixes #9874 Signed-off-by: fpetkovski --- cmd/promtool/main.go | 6 ++-- cmd/promtool/main_test.go | 30 +++++++++++++++++++ .../authorization_credentials_file.bad.yml | 4 +++ .../authorization_credentials_file.good.yml | 4 +++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 cmd/promtool/testdata/authorization_credentials_file.bad.yml create mode 100644 cmd/promtool/testdata/authorization_credentials_file.good.yml diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 7ae656c68..e01b95eeb 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -353,8 +353,10 @@ func checkConfig(agentMode bool, filename string) ([]string, error) { } for _, scfg := range cfg.ScrapeConfigs { - if err := checkFileExists(scfg.HTTPClientConfig.BearerTokenFile); err != nil { - return nil, errors.Wrapf(err, "error checking bearer token file %q", scfg.HTTPClientConfig.BearerTokenFile) + if scfg.HTTPClientConfig.Authorization != nil { + if err := checkFileExists(scfg.HTTPClientConfig.Authorization.CredentialsFile); err != nil { + return nil, errors.Wrapf(err, "error checking authorization credentials or bearer token file %q", scfg.HTTPClientConfig.Authorization.CredentialsFile) + } } if err := checkTLSConfig(scfg.HTTPClientConfig.TLSConfig); err != nil { diff --git a/cmd/promtool/main_test.go b/cmd/promtool/main_test.go index 1a8a47060..59a15b7a7 100644 --- a/cmd/promtool/main_test.go +++ b/cmd/promtool/main_test.go @@ -203,3 +203,33 @@ func TestCheckTargetConfig(t *testing.T) { }) } } + +func TestAuthorizationConfig(t *testing.T) { + cases := []struct { + name string + file string + err string + }{ + { + name: "authorization_credentials_file.bad", + file: "authorization_credentials_file.bad.yml", + err: "error checking authorization credentials or bearer token file", + }, + { + name: "authorization_credentials_file.good", + file: "authorization_credentials_file.good.yml", + err: "", + }, + } + + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + _, err := checkConfig(false, "testdata/"+test.file) + if test.err != "" { + require.Contains(t, err.Error(), test.err, "Expected error to contain %q, got %q", test.err, err.Error()) + return + } + require.NoError(t, err) + }) + } +} diff --git a/cmd/promtool/testdata/authorization_credentials_file.bad.yml b/cmd/promtool/testdata/authorization_credentials_file.bad.yml new file mode 100644 index 000000000..854b8c229 --- /dev/null +++ b/cmd/promtool/testdata/authorization_credentials_file.bad.yml @@ -0,0 +1,4 @@ +scrape_configs: + - job_name: test + authorization: + credentials_file: "/random/file/which/does/not/exist.yml" diff --git a/cmd/promtool/testdata/authorization_credentials_file.good.yml b/cmd/promtool/testdata/authorization_credentials_file.good.yml new file mode 100644 index 000000000..f6a5a7650 --- /dev/null +++ b/cmd/promtool/testdata/authorization_credentials_file.good.yml @@ -0,0 +1,4 @@ +scrape_configs: + - job_name: test + authorization: + credentials_file: "."