From 8611a27497044f9fd2c271011f187fdbe8f71723 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 | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/state/persistent.go b/state/persistent.go index 1c85757..fc38cbc 100644 --- a/state/persistent.go +++ b/state/persistent.go @@ -5,6 +5,9 @@ import ( "runtime" "context" "time" + "os" + "strings" + log "log/slog" "webservice/configuration" @@ -21,11 +24,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