2019-09-06 00:01:48 -04:00
package config
import (
2019-10-19 22:03:55 -04:00
"errors"
2022-09-01 20:51:37 -04:00
"fmt"
2023-01-08 17:53:37 -05:00
"io/fs"
2020-10-30 21:00:03 +05:30
"os"
2023-01-08 17:53:37 -05:00
"path/filepath"
2026-02-09 01:08:59 +01:00
"slices"
2025-08-14 09:15:34 -04:00
"sort"
2023-02-11 22:43:13 -05:00
"strings"
2021-05-18 22:29:15 -04:00
"time"
2020-10-30 21:00:03 +05:30
2023-01-08 17:53:37 -05:00
"github.com/TwiN/deepmerge"
2022-12-06 01:41:09 -05:00
"github.com/TwiN/gatus/v5/alerting"
"github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/alerting/provider"
2025-09-28 14:26:12 -04:00
"github.com/TwiN/gatus/v5/client"
2025-08-16 09:54:50 -04:00
"github.com/TwiN/gatus/v5/config/announcement"
2023-05-02 22:41:22 -04:00
"github.com/TwiN/gatus/v5/config/connectivity"
2024-05-09 22:56:16 -04:00
"github.com/TwiN/gatus/v5/config/endpoint"
2025-09-05 15:39:12 -04:00
"github.com/TwiN/gatus/v5/config/key"
2022-12-06 01:41:09 -05:00
"github.com/TwiN/gatus/v5/config/maintenance"
"github.com/TwiN/gatus/v5/config/remote"
2025-09-05 15:39:12 -04:00
"github.com/TwiN/gatus/v5/config/suite"
2025-09-28 14:26:12 -04:00
"github.com/TwiN/gatus/v5/config/tunneling"
2022-12-06 01:41:09 -05:00
"github.com/TwiN/gatus/v5/config/ui"
"github.com/TwiN/gatus/v5/config/web"
"github.com/TwiN/gatus/v5/security"
"github.com/TwiN/gatus/v5/storage"
2024-11-14 01:02:53 +01:00
"github.com/TwiN/logr"
2023-01-08 17:53:37 -05:00
"gopkg.in/yaml.v3"
2019-09-06 00:01:48 -04:00
)
2020-06-25 21:31:34 -04:00
const (
2020-09-26 15:15:50 -04:00
// DefaultConfigurationFilePath is the default path that will be used to search for the configuration file
2023-01-08 17:53:37 -05:00
// if a custom path isn't configured through the GATUS_CONFIG_PATH environment variable
2020-06-25 21:31:34 -04:00
DefaultConfigurationFilePath = "config/config.yaml"
2020-10-19 19:26:29 -04:00
// DefaultFallbackConfigurationFilePath is the default fallback path that will be used to search for the
// configuration file if DefaultConfigurationFilePath didn't work
DefaultFallbackConfigurationFilePath = "config/config.yml"
2025-09-05 15:39:12 -04:00
// DefaultConcurrency is the default number of endpoints/suites that can be monitored concurrently
DefaultConcurrency = 3
2020-06-25 21:31:34 -04:00
)
2019-09-06 00:01:48 -04:00
2019-10-19 22:03:55 -04:00
var (
2025-09-16 16:56:36 -04:00
// ErrNoEndpointOrSuiteInConfig is an error returned when a configuration file or directory has no endpoints configured
ErrNoEndpointOrSuiteInConfig = errors . New ( "configuration should contain at least one endpoint or suite" )
2020-10-23 16:07:51 -04:00
2023-01-08 17:53:37 -05:00
// ErrConfigFileNotFound is an error returned when a configuration file could not be found
2020-10-23 16:07:51 -04:00
ErrConfigFileNotFound = errors . New ( "configuration file not found" )
// ErrInvalidSecurityConfig is an error returned when the security configuration is invalid
2020-10-14 19:22:58 -04:00
ErrInvalidSecurityConfig = errors . New ( "invalid security configuration" )
2023-01-08 17:53:37 -05:00
// errEarlyReturn is returned to break out of a loop from a callback early
errEarlyReturn = errors . New ( "early escape" )
2019-10-19 22:03:55 -04:00
)
2019-09-06 00:01:48 -04:00
2020-09-26 15:15:50 -04:00
// Config is the main configuration structure
2020-06-25 21:31:34 -04:00
type Config struct {
2020-10-16 23:07:14 -04:00
// Debug Whether to enable debug logs
2024-11-13 23:54:00 -05:00
// Deprecated: Use the GATUS_LOG_LEVEL environment variable instead
2021-10-24 15:03:41 -04:00
Debug bool `yaml:"debug,omitempty"`
2020-10-16 23:07:14 -04:00
// Metrics Whether to expose metrics at /metrics
2021-10-24 15:03:41 -04:00
Metrics bool `yaml:"metrics,omitempty"`
2020-10-16 23:07:14 -04:00
2021-05-18 22:29:15 -04:00
// SkipInvalidConfigUpdate Whether to make the application ignore invalid configuration
// if the configuration file is updated while the application is running
2021-10-24 15:03:41 -04:00
SkipInvalidConfigUpdate bool `yaml:"skip-invalid-config-update,omitempty"`
2021-05-18 22:29:15 -04:00
2020-10-16 23:07:14 -04:00
// DisableMonitoringLock Whether to disable the monitoring lock
2021-10-23 16:47:12 -04:00
// The monitoring lock is what prevents multiple endpoints from being processed at the same time.
2020-10-16 23:07:14 -04:00
// Disabling this may lead to inaccurate response times
2025-09-05 15:39:12 -04:00
//
// Deprecated: Use Concurrency instead TODO: REMOVE THIS IN v6.0.0
2021-10-24 15:03:41 -04:00
DisableMonitoringLock bool `yaml:"disable-monitoring-lock,omitempty"`
2020-10-16 23:07:14 -04:00
2025-09-05 15:39:12 -04:00
// Concurrency is the maximum number of endpoints/suites that can be monitored concurrently
// Defaults to DefaultConcurrency. Set to 0 for unlimited concurrency.
Concurrency int `yaml:"concurrency,omitempty"`
2024-04-08 21:00:40 -04:00
// Security is the configuration for securing access to Gatus
2021-10-24 15:03:41 -04:00
Security * security . Config `yaml:"security,omitempty"`
2020-10-16 23:07:14 -04:00
2024-04-08 21:00:40 -04:00
// Alerting is the configuration for alerting providers
2021-10-24 15:03:41 -04:00
Alerting * alerting . Config `yaml:"alerting,omitempty"`
2020-10-16 23:07:14 -04:00
2024-04-08 21:00:40 -04:00
// Endpoints is the list of endpoints to monitor
2024-05-09 22:56:16 -04:00
Endpoints [] * endpoint . Endpoint `yaml:"endpoints,omitempty"`
2021-10-23 16:47:12 -04:00
2024-04-08 21:00:40 -04:00
// ExternalEndpoints is the list of all external endpoints
2024-05-09 22:56:16 -04:00
ExternalEndpoints [] * endpoint . ExternalEndpoint `yaml:"external-endpoints,omitempty"`
2024-04-08 21:00:40 -04:00
2025-09-05 15:39:12 -04:00
// Suites is the list of suites to monitor
Suites [] * suite . Suite `yaml:"suites,omitempty"`
2021-02-02 23:06:34 -05:00
// Storage is the configuration for how the data is stored
2021-10-24 15:03:41 -04:00
Storage * storage . Config `yaml:"storage,omitempty"`
2021-02-02 23:06:34 -05:00
2021-10-23 16:47:12 -04:00
// Web is the web configuration for the application
2021-10-24 15:03:41 -04:00
Web * web . Config `yaml:"web,omitempty"`
2021-05-18 22:29:15 -04:00
2021-09-11 01:51:14 -04:00
// UI is the configuration for the UI
2021-10-24 15:03:41 -04:00
UI * ui . Config `yaml:"ui,omitempty"`
2021-09-11 01:51:14 -04:00
2021-09-22 00:04:51 -04:00
// Maintenance is the configuration for creating a maintenance window in which no alerts are sent
2021-10-24 15:03:41 -04:00
Maintenance * maintenance . Config `yaml:"maintenance,omitempty"`
2021-09-22 00:04:51 -04:00
2022-07-28 20:07:53 -04:00
// Remote is the configuration for remote Gatus instances
// WARNING: This is in ALPHA and may change or be completely removed in the future
Remote * remote . Config `yaml:"remote,omitempty"`
2023-05-02 22:41:22 -04:00
// Connectivity is the configuration for connectivity
Connectivity * connectivity . Config `yaml:"connectivity,omitempty"`
2025-09-28 14:26:12 -04:00
// Tunneling is the configuration for SSH tunneling
Tunneling * tunneling . Config `yaml:"tunneling,omitempty"`
2025-08-16 09:54:50 -04:00
// Announcements is the list of system-wide announcements
Announcements [] * announcement . Announcement `yaml:"announcements,omitempty"`
2023-01-08 17:53:37 -05:00
configPath string // path to the file or directory from which config was loaded
2021-05-18 22:29:15 -04:00
lastFileModTime time . Time // last modification time
2020-06-25 21:31:34 -04:00
}
2025-08-06 00:26:50 +08:00
// GetUniqueExtraMetricLabels returns a slice of unique metric labels from all enabled endpoints
// in the configuration. It iterates through each endpoint, checks if it is enabled,
// and then collects unique labels from the endpoint's labels map.
func ( config * Config ) GetUniqueExtraMetricLabels () [] string {
labels := make ([] string , 0 )
for _ , ep := range config . Endpoints {
if ! ep . IsEnabled () {
continue
}
for label := range ep . ExtraLabels {
2026-02-09 01:08:59 +01:00
if slices . Contains ( labels , label ) {
2025-08-06 00:26:50 +08:00
continue
}
labels = append ( labels , label )
}
}
2025-08-14 09:15:34 -04:00
if len ( labels ) > 1 {
sort . Strings ( labels )
}
2025-08-06 00:26:50 +08:00
return labels
}
2024-05-09 22:56:16 -04:00
func ( config * Config ) GetEndpointByKey ( key string ) * endpoint . Endpoint {
2022-08-11 03:05:34 +02:00
for i := 0 ; i < len ( config . Endpoints ); i ++ {
ep := config . Endpoints [ i ]
2025-07-08 12:21:56 -04:00
if ep . Key () == strings . ToLower ( key ) {
2022-08-11 03:05:34 +02:00
return ep
}
}
return nil
}
2024-05-09 22:56:16 -04:00
func ( config * Config ) GetExternalEndpointByKey ( key string ) * endpoint . ExternalEndpoint {
2024-04-08 21:00:40 -04:00
for i := 0 ; i < len ( config . ExternalEndpoints ); i ++ {
ee := config . ExternalEndpoints [ i ]
2025-07-08 12:21:56 -04:00
if ee . Key () == strings . ToLower ( key ) {
2024-04-08 21:00:40 -04:00
return ee
}
}
return nil
}
2023-01-08 17:53:37 -05:00
// HasLoadedConfigurationBeenModified returns whether one of the file that the
2021-05-18 22:29:15 -04:00
// configuration has been loaded from has been modified since it was last read
2024-04-08 21:00:40 -04:00
func ( config * Config ) HasLoadedConfigurationBeenModified () bool {
2023-01-08 17:53:37 -05:00
lastMod := config . lastFileModTime . Unix ()
fileInfo , err := os . Stat ( config . configPath )
if err != nil {
return false
2023-01-07 05:46:19 +01:00
}
2023-01-08 17:53:37 -05:00
if fileInfo . IsDir () {
err = walkConfigDir ( config . configPath , func ( path string , d fs . DirEntry , err error ) error {
if info , err := d . Info (); err == nil && lastMod < info . ModTime (). Unix () {
return errEarlyReturn
}
return nil
})
2024-04-08 21:00:40 -04:00
return errors . Is ( err , errEarlyReturn )
2023-01-08 17:53:37 -05:00
}
return ! fileInfo . ModTime (). IsZero () && config . lastFileModTime . Unix () < fileInfo . ModTime (). Unix ()
2019-09-06 00:01:48 -04:00
}
2021-05-18 22:29:15 -04:00
// UpdateLastFileModTime refreshes Config.lastFileModTime
func ( config * Config ) UpdateLastFileModTime () {
2023-01-08 17:53:37 -05:00
config . lastFileModTime = time . Now ()
2023-01-07 03:45:43 -05:00
}
2023-01-07 05:46:19 +01:00
2024-04-08 21:00:40 -04:00
// LoadConfiguration loads the full configuration composed of the main configuration file
2023-01-08 17:53:37 -05:00
// and all composed configuration files
func LoadConfiguration ( configPath string ) ( * Config , error ) {
var configBytes [] byte
var fileInfo os . FileInfo
var usedConfigPath string
// Figure out what config path we'll use (either configPath or the default config path)
for _ , configurationPath := range [] string { configPath , DefaultConfigurationFilePath , DefaultFallbackConfigurationFilePath } {
if len ( configurationPath ) == 0 {
continue
2023-01-07 05:46:19 +01:00
}
2023-01-08 17:53:37 -05:00
var err error
fileInfo , err = os . Stat ( configurationPath )
if err != nil {
continue
}
2023-01-10 00:24:56 -05:00
usedConfigPath = configurationPath
2023-01-08 17:53:37 -05:00
break
2019-12-04 16:44:35 -05:00
}
2023-01-08 17:53:37 -05:00
if len ( usedConfigPath ) == 0 {
return nil , ErrConfigFileNotFound
}
var config * Config
if fileInfo . IsDir () {
err := walkConfigDir ( configPath , func ( path string , d fs . DirEntry , err error ) error {
if err != nil {
2024-11-13 23:54:00 -05:00
return fmt . Errorf ( "error walking path %s: %w" , path , err )
2023-01-08 17:53:37 -05:00
}
2025-02-03 02:37:55 +01:00
if strings . Contains ( path , ".." ) {
logr . Warnf ( "[config.LoadConfiguration] Ignoring configuration from %s" , path )
return nil
}
2024-11-13 23:54:00 -05:00
logr . Infof ( "[config.LoadConfiguration] Reading configuration from %s" , path )
2023-01-08 17:53:37 -05:00
data , err := os . ReadFile ( path )
if err != nil {
return fmt . Errorf ( "error reading configuration from file %s: %w" , path , err )
}
configBytes , err = deepmerge . YAML ( configBytes , data )
return err
})
if err != nil {
return nil , fmt . Errorf ( "error reading configuration from directory %s: %w" , usedConfigPath , err )
2019-12-04 16:44:35 -05:00
}
2023-01-08 17:53:37 -05:00
} else {
2024-11-13 23:54:00 -05:00
logr . Infof ( "[config.LoadConfiguration] Reading configuration from configFile=%s" , usedConfigPath )
2023-01-08 17:53:37 -05:00
if data , err := os . ReadFile ( usedConfigPath ); err != nil {
2024-11-13 23:54:00 -05:00
return nil , fmt . Errorf ( "error reading configuration from directory %s: %w" , usedConfigPath , err )
2023-01-08 17:53:37 -05:00
} else {
configBytes = data
}
}
if len ( configBytes ) == 0 {
return nil , ErrConfigFileNotFound
}
config , err := parseAndValidateConfigBytes ( configBytes )
if err != nil {
2024-11-13 23:54:00 -05:00
return nil , fmt . Errorf ( "error parsing config: %w" , err )
2019-12-04 16:44:35 -05:00
}
2023-01-08 17:53:37 -05:00
config . configPath = usedConfigPath
config . UpdateLastFileModTime ()
2024-11-13 23:54:00 -05:00
return config , nil
2023-01-07 03:45:43 -05:00
}
2019-12-04 16:44:35 -05:00
2023-01-08 17:53:37 -05:00
// walkConfigDir is a wrapper for filepath.WalkDir that strips directories and non-config files
func walkConfigDir ( path string , fn fs . WalkDirFunc ) error {
if len ( path ) == 0 {
// If the user didn't provide a directory, we'll just use the default config file, so we can return nil now.
return nil
2019-09-06 00:01:48 -04:00
}
2023-01-08 17:53:37 -05:00
return filepath . WalkDir ( path , func ( path string , d fs . DirEntry , err error ) error {
if err != nil {
return nil
}
if d == nil || d . IsDir () {
return nil
}
ext := filepath . Ext ( path )
if ext != ".yml" && ext != ".yaml" {
return nil
}
return fn ( path , d , err )
})
2019-09-06 00:01:48 -04:00
}
2021-10-24 14:51:21 -04:00
// parseAndValidateConfigBytes parses a Gatus configuration file into a Config struct and validates its parameters
2019-10-19 22:03:55 -04:00
func parseAndValidateConfigBytes ( yamlBytes [] byte ) ( config * Config , err error ) {
2023-02-11 22:43:13 -05:00
// Replace $$ with __GATUS_LITERAL_DOLLAR_SIGN__ to prevent os.ExpandEnv from treating "$$" as if it was an
// environment variable. This allows Gatus to support literal "$" in the configuration file.
yamlBytes = [] byte ( strings . ReplaceAll ( string ( yamlBytes ), "$$" , "__GATUS_LITERAL_DOLLAR_SIGN__" ))
2019-12-04 17:27:27 -05:00
// Expand environment variables
2021-10-24 18:34:39 -04:00
yamlBytes = [] byte ( os . ExpandEnv ( string ( yamlBytes )))
2023-02-11 22:43:13 -05:00
// Replace __GATUS_LITERAL_DOLLAR_SIGN__ with "$" to restore the literal "$" in the configuration file
yamlBytes = [] byte ( strings . ReplaceAll ( string ( yamlBytes ), "__GATUS_LITERAL_DOLLAR_SIGN__" , "$" ))
2019-12-04 17:27:27 -05:00
// Parse configuration file
2021-10-24 14:51:21 -04:00
if err = yaml . Unmarshal ( yamlBytes , & config ); err != nil {
2020-10-16 12:12:00 -04:00
return
}
2021-10-23 16:47:12 -04:00
// Check if the configuration file at least has endpoints configured
2025-09-16 16:56:36 -04:00
if config == nil || ( len ( config . Endpoints ) == 0 && len ( config . Suites ) == 0 ) {
err = ErrNoEndpointOrSuiteInConfig
2019-10-19 22:03:55 -04:00
} else {
2024-11-13 23:54:00 -05:00
// XXX: Remove this in v6.0.0
if config . Debug {
logr . Warn ( "WARNING: The 'debug' configuration has been deprecated and will be removed in v6.0.0" )
logr . Warn ( "WARNING: Please use the GATUS_LOG_LEVEL environment variable instead" )
}
// XXX: End of v6.0.0 removals
2025-09-29 23:01:27 -04:00
ValidateAlertingConfig ( config . Alerting , config . Endpoints , config . ExternalEndpoints )
if err := ValidateSecurityConfig ( config ); err != nil {
2021-05-18 22:29:15 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateEndpointsConfig ( config ); err != nil {
2021-05-18 22:29:15 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateWebConfig ( config ); err != nil {
2021-05-18 22:29:15 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateUIConfig ( config ); err != nil {
2021-09-11 01:51:14 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateMaintenanceConfig ( config ); err != nil {
2021-09-22 00:04:51 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateStorageConfig ( config ); err != nil {
2021-05-18 22:29:15 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateRemoteConfig ( config ); err != nil {
2022-07-28 20:07:53 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateConnectivityConfig ( config ); err != nil {
2023-05-02 22:41:22 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateTunnelingConfig ( config ); err != nil {
2025-09-28 14:26:12 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateAnnouncementsConfig ( config ); err != nil {
2025-08-16 09:54:50 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateSuitesConfig ( config ); err != nil {
2025-09-05 15:39:12 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
if err := ValidateUniqueKeys ( config ); err != nil {
2025-09-05 15:39:12 -04:00
return nil , err
}
2025-09-29 23:01:27 -04:00
ValidateAndSetConcurrencyDefaults ( config )
2025-05-17 16:10:28 -04:00
// Cross-config changes
config . UI . MaximumNumberOfResults = config . Storage . MaximumNumberOfResults
2019-09-06 20:25:31 -04:00
}
2019-10-19 21:42:03 -04:00
return
2019-09-06 00:01:48 -04:00
}
2020-09-22 17:46:40 -04:00
2025-09-29 23:01:27 -04:00
func ValidateConnectivityConfig ( config * Config ) error {
2023-05-02 22:41:22 -04:00
if config . Connectivity != nil {
return config . Connectivity . ValidateAndSetDefaults ()
}
return nil
}
2025-09-29 23:01:27 -04:00
// ValidateTunnelingConfig validates the tunneling configuration and resolves tunnel references
// NOTE: This must be called after ValidateEndpointsConfig and ValidateSuitesConfig
2025-09-28 14:26:12 -04:00
// because it resolves tunnel references in endpoint and suite client configurations
2025-09-29 23:01:27 -04:00
func ValidateTunnelingConfig ( config * Config ) error {
2025-09-28 14:26:12 -04:00
if config . Tunneling != nil {
if err := config . Tunneling . ValidateAndSetDefaults (); err != nil {
return err
}
// Resolve tunnel references in all endpoints
for _ , ep := range config . Endpoints {
if err := resolveTunnelForClientConfig ( config , ep . ClientConfig ); err != nil {
return fmt . Errorf ( "endpoint '%s': %w" , ep . Key (), err )
}
}
// Resolve tunnel references in suite endpoints
for _ , s := range config . Suites {
for _ , ep := range s . Endpoints {
if err := resolveTunnelForClientConfig ( config , ep . ClientConfig ); err != nil {
return fmt . Errorf ( "suite '%s' endpoint '%s': %w" , s . Key (), ep . Key (), err )
}
}
}
// TODO: Add tunnel support for alert providers when needed
}
return nil
}
// resolveTunnelForClientConfig resolves tunnel references in a client configuration
func resolveTunnelForClientConfig ( config * Config , clientConfig * client . Config ) error {
if clientConfig == nil || clientConfig . Tunnel == "" {
return nil
}
// Validate tunnel name
tunnelName := strings . TrimSpace ( clientConfig . Tunnel )
if tunnelName == "" {
return fmt . Errorf ( "tunnel name cannot be empty" )
}
if config . Tunneling == nil {
return fmt . Errorf ( "tunnel '%s' referenced but no tunneling configuration defined" , tunnelName )
}
_ , exists := config . Tunneling . Tunnels [ tunnelName ]
if ! exists {
return fmt . Errorf ( "tunnel '%s' not found in tunneling configuration" , tunnelName )
}
// Get or create the SSH tunnel instance and store it directly in client config
tunnel , err := config . Tunneling . GetTunnel ( tunnelName )
if err != nil {
return fmt . Errorf ( "failed to get tunnel '%s': %w" , tunnelName , err )
}
clientConfig . ResolvedTunnel = tunnel
return nil
}
2025-09-29 23:01:27 -04:00
func ValidateAnnouncementsConfig ( config * Config ) error {
2025-08-16 09:54:50 -04:00
if config . Announcements != nil {
if err := announcement . ValidateAndSetDefaults ( config . Announcements ); err != nil {
return err
}
// Sort announcements by timestamp (newest first) for API response
announcement . SortByTimestamp ( config . Announcements )
}
return nil
}
2025-09-29 23:01:27 -04:00
func ValidateRemoteConfig ( config * Config ) error {
2022-07-28 20:07:53 -04:00
if config . Remote != nil {
if err := config . Remote . ValidateAndSetDefaults (); err != nil {
return err
}
}
return nil
}
2025-09-29 23:01:27 -04:00
func ValidateStorageConfig ( config * Config ) error {
2021-02-02 23:06:34 -05:00
if config . Storage == nil {
2021-07-15 22:07:30 -04:00
config . Storage = & storage . Config {
2025-05-17 16:10:28 -04:00
Type : storage . TypeMemory ,
MaximumNumberOfResults : storage . DefaultMaximumNumberOfResults ,
MaximumNumberOfEvents : storage . DefaultMaximumNumberOfEvents ,
2021-07-15 22:07:30 -04:00
}
2021-10-28 19:35:46 -04:00
} else {
if err := config . Storage . ValidateAndSetDefaults (); err != nil {
return err
}
2021-02-02 23:06:34 -05:00
}
2021-05-18 22:29:15 -04:00
return nil
2021-02-02 23:06:34 -05:00
}
2025-09-29 23:01:27 -04:00
func ValidateMaintenanceConfig ( config * Config ) error {
2021-09-22 00:04:51 -04:00
if config . Maintenance == nil {
config . Maintenance = maintenance . GetDefaultConfig ()
} else {
if err := config . Maintenance . ValidateAndSetDefaults (); err != nil {
return err
}
}
return nil
}
2025-09-29 23:01:27 -04:00
func ValidateUIConfig ( config * Config ) error {
2021-09-11 01:51:14 -04:00
if config . UI == nil {
2021-09-22 00:47:51 -04:00
config . UI = ui . GetDefaultConfig ()
2021-09-11 01:51:14 -04:00
} else {
2021-09-22 00:47:51 -04:00
if err := config . UI . ValidateAndSetDefaults (); err != nil {
2021-09-11 01:51:14 -04:00
return err
}
}
return nil
}
2025-09-29 23:01:27 -04:00
func ValidateWebConfig ( config * Config ) error {
2020-11-19 19:39:48 +01:00
if config . Web == nil {
2021-09-22 00:47:51 -04:00
config . Web = web . GetDefaultConfig ()
2020-11-19 19:39:48 +01:00
} else {
2021-09-22 00:47:51 -04:00
return config . Web . ValidateAndSetDefaults ()
2020-11-19 19:39:48 +01:00
}
2021-05-18 22:29:15 -04:00
return nil
2020-11-19 19:39:48 +01:00
}
2025-09-29 23:01:27 -04:00
func ValidateEndpointsConfig ( config * Config ) error {
2024-05-15 21:29:45 -04:00
duplicateValidationMap := make ( map [ string ] bool )
// Validate endpoints
2024-05-09 22:56:16 -04:00
for _ , ep := range config . Endpoints {
2025-09-29 23:01:27 -04:00
logr . Debugf ( "[config.ValidateEndpointsConfig] Validating endpoint with key %s" , ep . Key ())
2024-05-15 21:29:45 -04:00
if endpointKey := ep . Key (); duplicateValidationMap [ endpointKey ] {
return fmt . Errorf ( "invalid endpoint %s: name and group combination must be unique" , ep . Key ())
} else {
duplicateValidationMap [ endpointKey ] = true
}
2024-05-09 22:56:16 -04:00
if err := ep . ValidateAndSetDefaults (); err != nil {
2024-05-15 21:29:45 -04:00
return fmt . Errorf ( "invalid endpoint %s: %w" , ep . Key (), err )
2021-05-18 22:29:15 -04:00
}
2020-09-22 17:46:40 -04:00
}
2025-09-29 23:01:27 -04:00
logr . Infof ( "[config.ValidateEndpointsConfig] Validated %d endpoints" , len ( config . Endpoints ))
2024-05-15 21:29:45 -04:00
// Validate external endpoints
2024-05-09 22:56:16 -04:00
for _ , ee := range config . ExternalEndpoints {
2025-09-29 23:01:27 -04:00
logr . Debugf ( "[config.ValidateEndpointsConfig] Validating external endpoint '%s'" , ee . Key ())
2024-05-15 21:29:45 -04:00
if endpointKey := ee . Key (); duplicateValidationMap [ endpointKey ] {
return fmt . Errorf ( "invalid external endpoint %s: name and group combination must be unique" , ee . Key ())
} else {
duplicateValidationMap [ endpointKey ] = true
2024-04-08 21:00:40 -04:00
}
2024-05-09 22:56:16 -04:00
if err := ee . ValidateAndSetDefaults (); err != nil {
2024-05-15 21:29:45 -04:00
return fmt . Errorf ( "invalid external endpoint %s: %w" , ee . Key (), err )
2024-04-08 21:00:40 -04:00
}
}
2025-09-29 23:01:27 -04:00
logr . Infof ( "[config.ValidateEndpointsConfig] Validated %d external endpoints" , len ( config . ExternalEndpoints ))
2024-04-08 21:00:40 -04:00
return nil
}
2025-09-29 23:01:27 -04:00
func ValidateSuitesConfig ( config * Config ) error {
2025-09-05 15:39:12 -04:00
if config . Suites == nil || len ( config . Suites ) == 0 {
2025-09-29 23:01:27 -04:00
logr . Info ( "[config.ValidateSuitesConfig] No suites configured" )
2025-09-05 15:39:12 -04:00
return nil
}
suiteNames := make ( map [ string ] bool )
for _ , suite := range config . Suites {
// Check for duplicate suite names
if suiteNames [ suite . Name ] {
return fmt . Errorf ( "duplicate suite name: %s" , suite . Key ())
}
suiteNames [ suite . Name ] = true
// Validate the suite configuration
if err := suite . ValidateAndSetDefaults (); err != nil {
return fmt . Errorf ( "invalid suite '%s': %w" , suite . Key (), err )
}
// Check that endpoints referenced in Store mappings use valid placeholders
for _ , suiteEndpoint := range suite . Endpoints {
if suiteEndpoint . Store != nil {
for contextKey , placeholder := range suiteEndpoint . Store {
// Basic validation that the context key is a valid identifier
if len ( contextKey ) == 0 {
return fmt . Errorf ( "suite '%s' endpoint '%s' has empty context key in store mapping" , suite . Key (), suiteEndpoint . Key ())
}
if len ( placeholder ) == 0 {
return fmt . Errorf ( "suite '%s' endpoint '%s' has empty placeholder in store mapping for key '%s'" , suite . Key (), suiteEndpoint . Key (), contextKey )
}
}
}
}
}
2025-09-29 23:01:27 -04:00
logr . Infof ( "[config.ValidateSuitesConfig] Validated %d suite(s)" , len ( config . Suites ))
2025-09-05 15:39:12 -04:00
return nil
}
2025-09-29 23:01:27 -04:00
func ValidateUniqueKeys ( config * Config ) error {
2025-09-05 15:39:12 -04:00
keyMap := make ( map [ string ] string ) // key -> description for error messages
// Check all endpoints
for _ , ep := range config . Endpoints {
epKey := ep . Key ()
if existing , exists := keyMap [ epKey ]; exists {
return fmt . Errorf ( "duplicate key '%s': endpoint '%s' conflicts with %s" , epKey , ep . Key (), existing )
}
keyMap [ epKey ] = fmt . Sprintf ( "endpoint '%s'" , ep . Key ())
}
// Check all external endpoints
for _ , ee := range config . ExternalEndpoints {
eeKey := ee . Key ()
if existing , exists := keyMap [ eeKey ]; exists {
return fmt . Errorf ( "duplicate key '%s': external endpoint '%s' conflicts with %s" , eeKey , ee . Key (), existing )
}
keyMap [ eeKey ] = fmt . Sprintf ( "external endpoint '%s'" , ee . Key ())
}
// Check all suites
for _ , suite := range config . Suites {
suiteKey := suite . Key ()
if existing , exists := keyMap [ suiteKey ]; exists {
return fmt . Errorf ( "duplicate key '%s': suite '%s' conflicts with %s" , suiteKey , suite . Key (), existing )
}
keyMap [ suiteKey ] = fmt . Sprintf ( "suite '%s'" , suite . Key ())
// Check endpoints within suites (they generate keys using suite group + endpoint name)
for _ , ep := range suite . Endpoints {
epKey := key . ConvertGroupAndNameToKey ( suite . Group , ep . Name )
if existing , exists := keyMap [ epKey ]; exists {
return fmt . Errorf ( "duplicate key '%s': endpoint '%s' in suite '%s' conflicts with %s" , epKey , epKey , suite . Key (), existing )
}
keyMap [ epKey ] = fmt . Sprintf ( "endpoint '%s' in suite '%s'" , epKey , suite . Key ())
}
}
return nil
}
2025-09-29 23:01:27 -04:00
func ValidateSecurityConfig ( config * Config ) error {
2020-10-14 19:22:58 -04:00
if config . Security != nil {
2025-09-30 13:38:17 -04:00
if ! config . Security . ValidateAndSetDefaults () {
2025-09-29 23:01:27 -04:00
logr . Debug ( "[config.ValidateSecurityConfig] Basic security configuration has been validated" )
2021-05-18 22:29:15 -04:00
return ErrInvalidSecurityConfig
2020-10-14 19:22:58 -04:00
}
}
2021-05-18 22:29:15 -04:00
return nil
2020-10-14 19:22:58 -04:00
}
2025-09-29 23:01:27 -04:00
// ValidateAlertingConfig validates the alerting configuration
2021-10-23 16:47:12 -04:00
// Note that the alerting configuration has to be validated before the endpoint configuration, because the default alert
2024-05-09 22:56:16 -04:00
// returned by provider.AlertProvider.GetDefaultAlert() must be parsed before endpoint.Endpoint.ValidateAndSetDefaults()
2021-05-15 21:31:32 -04:00
// sets the default alert values when none are set.
2025-09-29 23:01:27 -04:00
func ValidateAlertingConfig ( alertingConfig * alerting . Config , endpoints [] * endpoint . Endpoint , externalEndpoints [] * endpoint . ExternalEndpoint ) {
2021-05-18 22:29:15 -04:00
if alertingConfig == nil {
2025-09-29 23:01:27 -04:00
logr . Info ( "[config.ValidateAlertingConfig] Alerting is not configured" )
2020-09-22 17:46:40 -04:00
return
}
2021-05-18 22:29:15 -04:00
alertTypes := [] alert . Type {
2023-10-26 05:52:43 +02:00
alert . TypeAWSSES ,
2025-12-27 06:09:28 +05:30
alert . TypeClickUp ,
2021-05-18 22:29:15 -04:00
alert . TypeCustom ,
feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier ( #1224 )
2025-08-25 13:22:17 -04:00
alert . TypeDatadog ,
2021-05-18 22:29:15 -04:00
alert . TypeDiscord ,
2024-05-15 21:29:45 -04:00
alert . TypeEmail ,
2022-12-15 23:32:04 -05:00
alert . TypeGitHub ,
2023-05-31 03:57:15 +02:00
alert . TypeGitLab ,
2024-08-22 05:51:45 +08:00
alert . TypeGitea ,
2022-10-15 22:56:38 +01:00
alert . TypeGoogleChat ,
2023-11-04 01:21:52 +03:00
alert . TypeGotify ,
2025-07-08 12:21:56 -04:00
alert . TypeHomeAssistant ,
feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier ( #1224 )
2025-08-25 13:22:17 -04:00
alert . TypeIFTTT ,
2025-07-08 12:21:56 -04:00
alert . TypeIlert ,
2025-06-11 15:26:08 -04:00
alert . TypeIncidentIO ,
feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier ( #1224 )
2025-08-25 13:22:17 -04:00
alert . TypeLine ,
2022-07-19 10:05:27 -07:00
alert . TypeMatrix ,
2021-05-18 22:29:15 -04:00
alert . TypeMattermost ,
alert . TypeMessagebird ,
2025-10-03 16:51:26 -04:00
alert . TypeN8N ,
feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier ( #1224 )
2025-08-25 13:22:17 -04:00
alert . TypeNewRelic ,
2022-10-04 23:26:34 -04:00
alert . TypeNtfy ,
2021-12-09 21:18:44 -05:00
alert . TypeOpsgenie ,
2021-05-18 22:29:15 -04:00
alert . TypePagerDuty ,
feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier ( #1224 )
2025-08-25 13:22:17 -04:00
alert . TypePlivo ,
2023-01-29 23:32:16 +01:00
alert . TypePushover ,
feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier ( #1224 )
2025-08-25 13:22:17 -04:00
alert . TypeRocketChat ,
alert . TypeSendGrid ,
alert . TypeSignal ,
alert . TypeSIGNL4 ,
2021-05-18 22:29:15 -04:00
alert . TypeSlack ,
feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier ( #1224 )
2025-08-25 13:22:17 -04:00
alert . TypeSplunk ,
alert . TypeSquadcast ,
2021-07-29 19:54:40 -04:00
alert . TypeTeams ,
2024-11-01 07:59:29 +08:00
alert . TypeTeamsWorkflows ,
2021-05-18 22:29:15 -04:00
alert . TypeTelegram ,
alert . TypeTwilio ,
feat(alerting): Add new providers for Datadog, IFTTT, Line, NewRelic, Plivo, RocketChat, SendGrid, Signal, SIGNL4, Splunk, Squadcast, Vonage, Webex and Zapier ( #1224 )
2025-08-25 13:22:17 -04:00
alert . TypeVonage ,
alert . TypeWebex ,
alert . TypeZapier ,
2024-09-04 04:21:08 +01:00
alert . TypeZulip ,
2020-09-22 17:46:40 -04:00
}
2021-05-18 22:29:15 -04:00
var validProviders , invalidProviders [] alert . Type
2020-09-24 19:52:59 -04:00
for _ , alertType := range alertTypes {
2021-05-18 22:29:15 -04:00
alertProvider := alertingConfig . GetAlertingProviderByAlertType ( alertType )
2020-09-24 19:52:59 -04:00
if alertProvider != nil {
2024-12-16 20:32:13 -05:00
if err := alertProvider . Validate (); err == nil {
2021-05-15 21:31:32 -04:00
// Parse alerts with the provider's default alert
if alertProvider . GetDefaultAlert () != nil {
2024-05-09 22:56:16 -04:00
for _ , ep := range endpoints {
for alertIndex , endpointAlert := range ep . Alerts {
2021-10-23 16:47:12 -04:00
if alertType == endpointAlert . Type {
2025-09-29 23:01:27 -04:00
logr . Debugf ( "[config.ValidateAlertingConfig] Parsing alert %d with default alert for provider=%s in endpoint with key=%s" , alertIndex , alertType , ep . Key ())
2024-12-16 20:32:13 -05:00
provider . MergeProviderDefaultAlertIntoEndpointAlert ( alertProvider . GetDefaultAlert (), endpointAlert )
// Validate the endpoint alert's overrides, if applicable
if len ( endpointAlert . ProviderOverride ) > 0 {
if err = alertProvider . ValidateOverrides ( ep . Group , endpointAlert ); err != nil {
2025-09-29 23:01:27 -04:00
logr . Warnf ( "[config.ValidateAlertingConfig] endpoint with key=%s has invalid overrides for provider=%s: %s" , ep . Key (), alertType , err . Error ())
2024-12-16 20:32:13 -05:00
}
}
2024-05-15 21:29:45 -04:00
}
}
}
for _ , ee := range externalEndpoints {
for alertIndex , endpointAlert := range ee . Alerts {
if alertType == endpointAlert . Type {
2025-09-29 23:01:27 -04:00
logr . Debugf ( "[config.ValidateAlertingConfig] Parsing alert %d with default alert for provider=%s in endpoint with key=%s" , alertIndex , alertType , ee . Key ())
2024-12-16 20:32:13 -05:00
provider . MergeProviderDefaultAlertIntoEndpointAlert ( alertProvider . GetDefaultAlert (), endpointAlert )
// Validate the endpoint alert's overrides, if applicable
if len ( endpointAlert . ProviderOverride ) > 0 {
if err = alertProvider . ValidateOverrides ( ee . Group , endpointAlert ); err != nil {
2025-09-29 23:01:27 -04:00
logr . Warnf ( "[config.ValidateAlertingConfig] endpoint with key=%s has invalid overrides for provider=%s: %s" , ee . Key (), alertType , err . Error ())
2024-12-16 20:32:13 -05:00
}
}
2021-05-15 21:31:32 -04:00
}
}
}
}
2020-09-24 19:52:59 -04:00
validProviders = append ( validProviders , alertType )
} else {
2025-09-29 23:01:27 -04:00
logr . Warnf ( "[config.ValidateAlertingConfig] Ignoring provider=%s due to error=%s" , alertType , err . Error ())
2020-09-24 19:52:59 -04:00
invalidProviders = append ( invalidProviders , alertType )
2022-12-15 20:54:38 -05:00
alertingConfig . SetAlertingProviderToNil ( alertProvider )
2020-09-24 19:52:59 -04:00
}
} else {
invalidProviders = append ( invalidProviders , alertType )
}
2020-09-22 17:46:40 -04:00
}
2025-09-29 23:01:27 -04:00
logr . Infof ( "[config.ValidateAlertingConfig] configuredProviders=%s; ignoredProviders=%s" , validProviders , invalidProviders )
2020-09-22 17:46:40 -04:00
}
2025-09-05 15:39:12 -04:00
2025-09-29 23:01:27 -04:00
func ValidateAndSetConcurrencyDefaults ( config * Config ) {
2025-09-05 15:39:12 -04:00
if config . DisableMonitoringLock {
config . Concurrency = 0
logr . Warn ( "WARNING: The 'disable-monitoring-lock' configuration has been deprecated and will be removed in v6.0.0" )
logr . Warn ( "WARNING: Please set 'concurrency: 0' instead" )
2025-09-29 23:01:27 -04:00
logr . Debug ( "[config.ValidateAndSetConcurrencyDefaults] DisableMonitoringLock is true, setting unlimited (0) concurrency" )
2025-09-05 15:39:12 -04:00
} else if config . Concurrency <= 0 && ! config . DisableMonitoringLock {
config . Concurrency = DefaultConcurrency
2025-09-29 23:01:27 -04:00
logr . Debugf ( "[config.ValidateAndSetConcurrencyDefaults] Setting default concurrency to %d" , config . Concurrency )
2025-09-05 15:39:12 -04:00
} else {
2025-09-29 23:01:27 -04:00
logr . Debugf ( "[config.ValidateAndSetConcurrencyDefaults] Using configured concurrency of %d" , config . Concurrency )
2025-09-05 15:39:12 -04:00
}
}