diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 214ba7d75..f8d8aa993 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -170,7 +170,7 @@ type Reloadable interface { func reloadConfig(filename string, rls ...Reloadable) bool { log.Infof("Loading configuration file %s", filename) - conf, err := config.LoadFromFile(filename) + conf, err := config.LoadFile(filename) if err != nil { log.Errorf("Couldn't load configuration (-config.file=%s): %v", filename, err) log.Errorf("Note: The configuration format has changed with version 0.14. Please see the documentation (http://prometheus.io/docs/operating/configuration/) and the provided configuration migration tool (https://github.com/prometheus/migrate).") diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 791fa069f..b630b6166 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -22,8 +22,6 @@ import ( "strings" "text/template" - "gopkg.in/yaml.v2" - "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/util/cli" @@ -73,32 +71,53 @@ func checkConfig(t cli.Term, filename string) ([]string, error) { return nil, fmt.Errorf("is a directory") } - content, err := ioutil.ReadFile(filename) + cfg, err := config.LoadFile(filename) if err != nil { return nil, err } - var cfg config.Config - if err := yaml.Unmarshal(content, &cfg); err != nil { - return nil, err + check := func(fn string) error { + // Nothing set, nothing to error on. + if fn == "" { + return nil + } + _, err := os.Stat(fn) + return err } + var ruleFiles []string for _, rf := range cfg.RuleFiles { - if !filepath.IsAbs(rf) { - rf = filepath.Join(filepath.Dir(filename), rf) - } - rfs, err := filepath.Glob(rf) if err != nil { return nil, err } - // If an explicit file was given, error if it doesn't exist. - if !strings.Contains(rf, "*") && len(rfs) == 0 { - return nil, fmt.Errorf("%q does not point to an existing file", rf) + // If an explicit file was given, error if it is not accessible. + if !strings.Contains(rf, "*") { + if len(rfs) == 0 { + return nil, fmt.Errorf("%q does not point to an existing file", rf) + } + if err := check(rfs[0]); err != nil { + return nil, fmt.Errorf("error checking rule file %q: %s", rfs[0], err) + } } ruleFiles = append(ruleFiles, rfs...) } + for _, scfg := range cfg.ScrapeConfigs { + if err := check(scfg.BearerTokenFile); err != nil { + return nil, fmt.Errorf("error checking bearer token file %q: %s", scfg.BearerTokenFile, err) + } + + if scfg.ClientCert != nil { + if err := check(scfg.ClientCert.Cert); err != nil { + return nil, fmt.Errorf("error checking client cert file %q: %s", scfg.ClientCert.Cert, err) + } + if err := check(scfg.ClientCert.Key); err != nil { + return nil, fmt.Errorf("error checking client key file %q: %s", scfg.ClientCert.Key, err) + } + } + } + return ruleFiles, nil } diff --git a/config/config.go b/config/config.go index dc0fdca7e..34b9362c8 100644 --- a/config/config.go +++ b/config/config.go @@ -40,8 +40,8 @@ func Load(s string) (*Config, error) { return cfg, nil } -// LoadFromFile parses the given YAML file into a Config. -func LoadFromFile(filename string) (*Config, error) { +// LoadFile parses the given YAML file into a Config. +func LoadFile(filename string) (*Config, error) { content, err := ioutil.ReadFile(filename) if err != nil { return nil, err diff --git a/config/config_test.go b/config/config_test.go index 5284699fa..16ab2c604 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -181,11 +181,11 @@ var expectedConf = &Config{ func TestLoadConfig(t *testing.T) { // Parse a valid file that sets a global scrape timeout. This tests whether parsing // an overwritten default field in the global config permanently changes the default. - if _, err := LoadFromFile("testdata/global_timeout.good.yml"); err != nil { + if _, err := LoadFile("testdata/global_timeout.good.yml"); err != nil { t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err) } - c, err := LoadFromFile("testdata/conf.good.yml") + c, err := LoadFile("testdata/conf.good.yml") if err != nil { t.Fatalf("Error parsing %s: %s", "testdata/conf.good.yml", err) } @@ -252,7 +252,7 @@ var expectedErrors = []struct { func TestBadConfigs(t *testing.T) { for _, ee := range expectedErrors { - _, err := LoadFromFile("testdata/" + ee.filename) + _, err := LoadFile("testdata/" + ee.filename) if err == nil { t.Errorf("Expected error parsing %s but got none", ee.filename) continue