瀏覽代碼

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 年之前
父節點
當前提交
2054dfd83d
共有 1 個文件被更改,包括 12 次插入29 次删除
  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
 	var err error
 	config = cnf
 	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
 		return err
 	}
 	}
 	err = createProvider(basePath)
 	err = createProvider(basePath)
@@ -1092,7 +1095,12 @@ func saveGCSCredentials(user *User) error {
 	if err != nil {
 	if err != nil {
 		return &ValidationError{err: fmt.Sprintf("could not marshal GCS credentials: %v", err)}
 		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 {
 	if err != nil {
 		return &ValidationError{err: fmt.Sprintf("could not save GCS credentials: %v", err)}
 		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() {
 func checkDataprovider() {
 	err := provider.checkAvailability()
 	err := provider.checkAvailability()
 	if err != nil {
 	if err != nil {