Skip to content

Commit a5f6bc7

Browse files
authored
Alertmanager: Fix config validation gap around unreferenced templates (#9207)
* Create test covering an unreferenced invalid template * Validate all templates, not just referenced ones * changelog
1 parent f52911d commit a5f6bc7

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
* [BUGFIX] Querier: Fix invalid query results when multiple chunks are being merged. #8992
107107
* [BUGFIX] Query-frontend: return annotations generated during evaluation of sharded queries. #9138
108108
* [BUGFIX] Querier: Support optional start and end times on `/prometheus/api/v1/labels`, `/prometheus/api/v1/label/<label>/values`, and `/prometheus/api/v1/series` when `max_query_into_future: 0`. #9129
109+
* [BUGFIX] Alertmanager: Fix config validation gap around unreferenced templates. #9207
109110

110111
### Mixin
111112

pkg/alertmanager/api.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"io"
1212
"net/http"
1313
"os"
14-
"path/filepath"
1514
"reflect"
1615

1716
"github.com/go-kit/log"
@@ -246,6 +245,7 @@ func validateUserConfig(logger log.Logger, cfg alertspb.AlertConfigDesc, limits
246245
}
247246
defer os.RemoveAll(userTempDir)
248247

248+
templateFiles := make([]string, 0, len(cfg.Templates))
249249
for _, tmpl := range cfg.Templates {
250250
templateFilepath, err := safeTemplateFilepath(userTempDir, tmpl.Filename)
251251
if err != nil {
@@ -257,11 +257,8 @@ func validateUserConfig(logger log.Logger, cfg alertspb.AlertConfigDesc, limits
257257
level.Error(logger).Log("msg", "unable to store template file", "err", err, "user", cfg.User)
258258
return fmt.Errorf("unable to store template file '%s'", tmpl.Filename)
259259
}
260-
}
261260

262-
templateFiles := make([]string, len(amCfg.Templates))
263-
for i, t := range amCfg.Templates {
264-
templateFiles[i] = filepath.Join(userTempDir, t)
261+
templateFiles = append(templateFiles, templateFilepath)
265262
}
266263

267264
_, err = template.FromGlobs(templateFiles, WithCustomFunctions(user))

pkg/alertmanager/api_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,23 @@ alertmanager_config: |
746746
- "*.tmpl"
747747
template_files:
748748
"test.tmpl": "{{ invalid Go template }}"
749+
`,
750+
err: fmt.Errorf(`error validating Alertmanager config: template: test.tmpl:1: function "invalid" not defined`),
751+
},
752+
{
753+
name: "should return error if template is wrong even when not referenced by config",
754+
cfg: `
755+
alertmanager_config: |
756+
route:
757+
receiver: 'default-receiver'
758+
group_wait: 30s
759+
group_interval: 5m
760+
repeat_interval: 4h
761+
group_by: [cluster, alertname]
762+
receivers:
763+
- name: default-receiver
764+
template_files:
765+
"test.tmpl": "{{ invalid Go template }}"
749766
`,
750767
err: fmt.Errorf(`error validating Alertmanager config: template: test.tmpl:1: function "invalid" not defined`),
751768
},

0 commit comments

Comments
 (0)