From 2054dfd83d89bc044cf9c533886f0e016e3c2ed2 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Wed, 25 Nov 2020 14:18:12 +0100 Subject: [PATCH] create the credential directory when needed The credentials dir is currently required only for GCS users if prefer database credential setting is false, so defer its creation and don't fail to start the services if this directory is missing --- dataprovider/dataprovider.go | 41 +++++++++++------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/dataprovider/dataprovider.go b/dataprovider/dataprovider.go index b3b4a317..c2563bb5 100644 --- a/dataprovider/dataprovider.go +++ b/dataprovider/dataprovider.go @@ -385,10 +385,13 @@ func Initialize(cnf Config, basePath string) error { var err error config = cnf - if err = validateHooks(); err != nil { - return err + if filepath.IsAbs(config.CredentialsPath) { + credentialsDirPath = config.CredentialsPath + } else { + credentialsDirPath = filepath.Join(basePath, config.CredentialsPath) } - if err = validateCredentialsDir(basePath, cnf.PreferDatabaseCredentials); err != nil { + + if err = validateHooks(); err != nil { return err } err = createProvider(basePath) @@ -1092,7 +1095,12 @@ func saveGCSCredentials(user *User) error { if err != nil { return &ValidationError{err: fmt.Sprintf("could not marshal GCS credentials: %v", err)} } - err = ioutil.WriteFile(user.getGCSCredentialsFilePath(), creds, 0600) + credentialsFilePath := user.getGCSCredentialsFilePath() + err = os.MkdirAll(filepath.Dir(credentialsFilePath), 0700) + if err != nil { + return &ValidationError{err: fmt.Sprintf("could not create GCS credentials dir: %v", err)} + } + err = ioutil.WriteFile(credentialsFilePath, creds, 0600) if err != nil { return &ValidationError{err: fmt.Sprintf("could not save GCS credentials: %v", err)} } @@ -1430,31 +1438,6 @@ func startAvailabilityTimer() { }() } -func validateCredentialsDir(basePath string, preferDbCredentials bool) error { - if filepath.IsAbs(config.CredentialsPath) { - credentialsDirPath = config.CredentialsPath - } else { - credentialsDirPath = filepath.Join(basePath, config.CredentialsPath) - } - // if we want to store credentials inside the database just stop here - // we just populate credentialsDirPath to be able to use existing users - // with credential files - if preferDbCredentials { - return nil - } - fi, err := os.Stat(credentialsDirPath) - if err == nil { - if !fi.IsDir() { - return errors.New("Credential path is not a valid directory") - } - return nil - } - if !os.IsNotExist(err) { - return err - } - return os.MkdirAll(credentialsDirPath, 0700) -} - func checkDataprovider() { err := provider.checkAvailability() if err != nil {