SMTP: Update SMTP TemplatesPatterns to do an 'or' filter instead of 'and' (#61421)

update template path parsing to use 'or' logic instead of 'and'
This commit is contained in:
Michael Mandrus
2023-01-13 10:24:22 -05:00
committed by GitHub
parent 2cadcd5aeb
commit 9dc081e629
2 changed files with 20 additions and 3 deletions
+10 -1
View File
@@ -57,13 +57,22 @@ func ProvideService(bus bus.Bus, cfg *setting.Cfg, mailer Mailer, store TempUser
})
mailTemplates.Funcs(sprig.FuncMap())
// Parse invalid templates using 'or' logic. Return an error only if no paths are valid.
invalidTemplates := make([]string, 0)
for _, pattern := range ns.Cfg.Smtp.TemplatesPatterns {
templatePattern := filepath.Join(ns.Cfg.StaticRootPath, pattern)
_, err := mailTemplates.ParseGlob(templatePattern)
if err != nil {
return nil, err
invalidTemplates = append(invalidTemplates, templatePattern)
}
}
if len(invalidTemplates) > 0 {
is := strings.Join(invalidTemplates, ", ")
if len(invalidTemplates) == len(ns.Cfg.Smtp.TemplatesPatterns) {
return nil, fmt.Errorf("provided html/template filepaths matched no files: %s", is)
}
ns.log.Warn("some provided html/template filepaths matched no files: %s", is)
}
if !util.IsEmail(ns.Cfg.Smtp.FromAddress) {
return nil, errors.New("invalid email address for SMTP from_address config")
@@ -31,12 +31,20 @@ func TestProvideService(t *testing.T) {
require.Error(t, err)
})
t.Run("When template_patterns fails to parse", func(t *testing.T) {
t.Run("When all template_patterns fail to parse", func(t *testing.T) {
cfg := createSmtpConfig()
cfg.Smtp.TemplatesPatterns = []string{"/usr/not-a-dir/**", "/usr/also-not-a-dir/**"}
_, _, err := createSutWithConfig(t, bus, cfg)
require.Error(t, err)
})
t.Run("When some template_patterns fail to parse", func(t *testing.T) {
cfg := createSmtpConfig()
cfg.Smtp.TemplatesPatterns = append(cfg.Smtp.TemplatesPatterns, "/usr/not-a-dir/**")
_, _, err := createSutWithConfig(t, bus, cfg)
require.Error(t, err)
require.NoError(t, err)
})
}