From 003d6451fc95925446a08987e79b71512e33b6d8 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Sat, 26 Dec 2020 01:52:44 +0100 Subject: [PATCH] Promtool: add web config validation Signed-off-by: Julien Pivotto --- cmd/promtool/main.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 988af14e1..1a13c537e 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -37,6 +37,7 @@ import ( config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/common/version" + "github.com/prometheus/exporter-toolkit/https" "gopkg.in/alecthomas/kingpin.v2" "github.com/prometheus/prometheus/config" @@ -59,6 +60,12 @@ func main() { "The config files to check.", ).Required().ExistingFiles() + checkWebConfigCmd := checkCmd.Command("web-config", "Check if the web config files are valid or not.") + webConfigFiles := checkWebConfigCmd.Arg( + "web-config-files", + "The config files to check.", + ).Required().ExistingFiles() + checkRulesCmd := checkCmd.Command("rules", "Check if the rule files are valid or not.") ruleFiles := checkRulesCmd.Arg( "rule-files", @@ -154,6 +161,9 @@ func main() { case checkConfigCmd.FullCommand(): os.Exit(CheckConfig(*configFiles...)) + case checkWebConfigCmd.FullCommand(): + os.Exit(CheckWebConfig(*webConfigFiles...)) + case checkRulesCmd.FullCommand(): os.Exit(CheckRules(*ruleFiles...)) @@ -234,6 +244,24 @@ func CheckConfig(files ...string) int { return 0 } +// CheckWebConfig validates web configuration files. +func CheckWebConfig(files ...string) int { + failed := false + + for _, f := range files { + if err := https.Validate(f); err != nil { + fmt.Fprintln(os.Stderr, f, "FAILED:", err) + failed = true + continue + } + fmt.Fprintln(os.Stderr, f, "SUCCESS") + } + if failed { + return 1 + } + return 0 +} + func checkFileExists(fn string) error { // Nothing set, nothing to error on. if fn == "" {