Merge pull request #477 from Icinga/feature/more-config-validation

Check for required database and Redis config options during config validation
This commit is contained in:
Julian Brost 2022-05-13 16:09:00 +02:00 committed by GitHub
commit da3fa88707
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View file

@ -47,7 +47,7 @@ func (d *Database) Open(logger *logging.Logger) (*icingadb.DB, error) {
config.User = d.User
config.Passwd = d.Password
if strings.HasPrefix(d.Host, "/") {
if d.isUnixAddr() {
config.Net = "unix"
config.Addr = d.Host
} else {
@ -151,9 +151,25 @@ func (d *Database) Validate() error {
return unknownDbType(d.Type)
}
if d.Host == "" {
return errors.New("database host missing")
}
if d.User == "" {
return errors.New("database user missing")
}
if d.Database == "" {
return errors.New("database name missing")
}
return d.Options.Validate()
}
func (d *Database) isUnixAddr() bool {
return strings.HasPrefix(d.Host, "/")
}
func unknownDbType(t string) error {
return errors.Errorf(`unknown database type %q, must be one of: "mysql", "pgsql"`, t)
}

View file

@ -103,5 +103,9 @@ func dialWithLogging(dialer ctxDialerFunc, logger *logging.Logger) ctxDialerFunc
// Validate checks constraints in the supplied Redis configuration and returns an error if they are violated.
func (r *Redis) Validate() error {
if r.Address == "" {
return errors.New("Redis address missing")
}
return r.Options.Validate()
}