Skip to content

Commit

Permalink
Comments.
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
  • Loading branch information
pstibrany committed Mar 8, 2021
1 parent 31ae118 commit de67abb
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/alertmanager/multitenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -1144,17 +1144,21 @@ func (s StatusHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
}
}

func storeTemplateFile(tenantTemplateDir, templateFileName, content string) (changed bool, _ error) {
// storeTemplateFile stores template file with given content into specific directory.
// Since templateFileName is provided by end-user, it is verified that it doesn't do any path-traversal.
// Returns true, if file content has changed (new or updated file), false if file with the same name
// and content was already stored locally.
func storeTemplateFile(dir, templateFileName, content string) (bool, error) {
if templateFileName != filepath.Base(templateFileName) {
return false, fmt.Errorf("template file name '%s' is not not valid", templateFileName)
}

err := os.MkdirAll(tenantTemplateDir, 0755)
err := os.MkdirAll(dir, 0755)
if err != nil {
return false, fmt.Errorf("unable to create Alertmanager templates directory %q: %s", tenantTemplateDir, err)
return false, fmt.Errorf("unable to create Alertmanager templates directory %q: %s", dir, err)
}

file := filepath.Join(tenantTemplateDir, templateFileName)
file := filepath.Join(dir, templateFileName)
// Check if the template file already exists and if it has changed
if tmpl, err := ioutil.ReadFile(file); err == nil && string(tmpl) == content {
return false, nil
Expand Down

0 comments on commit de67abb

Please sign in to comment.