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: "."