Browse Source

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
Nicola Murino 4 years ago
parent
commit
2054dfd83d
1 changed files with 12 additions and 29 deletions
  1. 12 29
      dataprovider/dataprovider.go

+ 12 - 29
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 {