From 5081ded974172dd4fcfb10021d5d1664a23d2f50 Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Tue, 10 May 2022 16:20:10 +0200 Subject: [PATCH] Check for required database and Redis config options during config validation --- pkg/config/database.go | 18 +++++++++++++++++- pkg/config/redis.go | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/config/database.go b/pkg/config/database.go index 799aa83b..9766af5b 100644 --- a/pkg/config/database.go +++ b/pkg/config/database.go @@ -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) } diff --git a/pkg/config/redis.go b/pkg/config/redis.go index 420c2d84..6607ec84 100644 --- a/pkg/config/redis.go +++ b/pkg/config/redis.go @@ -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() }