Partially copied from: https://github.com/prometheus/alertmanager/blob/8e861c646bf67599a1704fc843c6a94d519ce312/cli/check_config.go#L65-L96
(logger log.Logger, cfg alertspb.AlertConfigDesc, limits Limits, user string)
| 202 | |
| 203 | // Partially copied from: https://github.com/prometheus/alertmanager/blob/8e861c646bf67599a1704fc843c6a94d519ce312/cli/check_config.go#L65-L96 |
| 204 | func validateUserConfig(logger log.Logger, cfg alertspb.AlertConfigDesc, limits Limits, user string) error { |
| 205 | // We don't have a valid use case for empty configurations. If a tenant does not have a |
| 206 | // configuration set and issue a request to the Alertmanager, we'll a) upload an empty |
| 207 | // config and b) immediately start an Alertmanager instance for them if a fallback |
| 208 | // configuration is provisioned. |
| 209 | if cfg.RawConfig == "" { |
| 210 | return fmt.Errorf("configuration provided is empty, if you'd like to remove your configuration please use the delete configuration endpoint") |
| 211 | } |
| 212 | |
| 213 | amCfg, err := config.Load(cfg.RawConfig) |
| 214 | if err != nil { |
| 215 | return err |
| 216 | } |
| 217 | |
| 218 | // Validate the config recursively scanning it. |
| 219 | if err := validateAlertmanagerConfig(amCfg); err != nil { |
| 220 | return err |
| 221 | } |
| 222 | |
| 223 | // Validate templates referenced in the alertmanager config. |
| 224 | for _, name := range amCfg.Templates { |
| 225 | if err := validateTemplateFilename(name); err != nil { |
| 226 | return err |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Check template limits. |
| 231 | if l := limits.AlertmanagerMaxTemplatesCount(user); l > 0 && len(cfg.Templates) > l { |
| 232 | return fmt.Errorf(errTooManyTemplates, len(cfg.Templates), l) |
| 233 | } |
| 234 | |
| 235 | if maxSize := limits.AlertmanagerMaxTemplateSize(user); maxSize > 0 { |
| 236 | for _, tmpl := range cfg.Templates { |
| 237 | if size := len(tmpl.GetBody()); size > maxSize { |
| 238 | return fmt.Errorf(errTemplateTooBig, tmpl.GetFilename(), size, maxSize) |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // Validate template files. |
| 244 | for _, tmpl := range cfg.Templates { |
| 245 | if err := validateTemplateFilename(tmpl.Filename); err != nil { |
| 246 | return err |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // Create templates on disk in a temporary directory. |
| 251 | // Note: This means the validation will succeed if we can write to tmp but |
| 252 | // not to configured data dir, and on the flipside, it'll fail if we can't write |
| 253 | // to tmpDir. Ignoring both cases for now as they're ultra rare but will revisit if |
| 254 | // we see this in the wild. |
| 255 | userTempDir, err := os.MkdirTemp("", "validate-config-"+cfg.User) |
| 256 | if err != nil { |
| 257 | return err |
| 258 | } |
| 259 | defer os.RemoveAll(userTempDir) |
| 260 | |
| 261 | for _, tmpl := range cfg.Templates { |
no test coverage detected