From 2fc512d155347c0622ce8103c19caa83dccb18f9 Mon Sep 17 00:00:00 2001 From: gjahn <gregor.jahn@bht-berlin.de> Date: Mon, 27 Nov 2023 22:13:53 +0100 Subject: [PATCH] Database password must be a file path --- configuration/config.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/configuration/config.go b/configuration/config.go index 5787d0d..be25fb6 100644 --- a/configuration/config.go +++ b/configuration/config.go @@ -3,6 +3,8 @@ package configuration import ( "errors" "fmt" + "os" + fp "path/filepath" configParser "github.com/caarlos0/env/v9" ) @@ -60,5 +62,25 @@ func New() ( *Config, error ){ } + if len( cfg.DatabaseHost ) >= 1 && len( cfg.DatabasePassword ) >= 2 { + if ! fp.IsLocal( cfg.DatabasePassword ) && ! fp.IsAbs( cfg.DatabasePassword ) { + return nil, errors.New( + fmt.Sprintln( "Database password must be a file path" ), + ) + } + _, err := os.Stat( cfg.DatabasePassword ) + if err != nil { + if errors.Is( err, os.ErrNotExist ){ + return nil, errors.New( + fmt.Sprintln( "Database password file does not exist" ), + ) + } + return nil, errors.New( + fmt.Sprintln( "Database password file not accessible" ), + ) + } + } + + return &cfg, nil } -- GitLab