From 2e6405a4372ce773491c286b31e26c6310fc5b61 Mon Sep 17 00:00:00 2001 From: gjahn <gregor.jahn@bht-berlin.de> Date: Tue, 9 Jan 2024 20:51:08 +0100 Subject: [PATCH] Database password is now read from file Although, password file is checked during config validation, sth may have changed the file content in the short period of time between validation and DB client initialization, hence exiting in case of an error --- state/persistent.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/state/persistent.go b/state/persistent.go index 1c85757..2e431d1 100644 --- a/state/persistent.go +++ b/state/persistent.go @@ -5,6 +5,8 @@ import ( "runtime" "context" "time" + "os" + log "log/slog" "webservice/configuration" @@ -21,11 +23,18 @@ type Persistent struct { func NewPersistentStore( c *configuration.Config ) *Persistent { + content, err := os.ReadFile( c.DatabasePassword ) + if err != nil { + log.Error( fmt.Sprintf( "Database password not able to be read: %v", err ) ) + os.Exit( 1 ) + } + dbPassword := string( content ) + return &Persistent{ client: db.NewClient( &db.Options{ Addr: fmt.Sprintf( "%s:%d", c.DatabaseHost, c.DatabasePort ), Username: c.DatabaseUsername, - Password: c.DatabasePassword, + Password: dbPassword, DB: c.DatabaseName, DialTimeout: time.Second * 3, -- GitLab