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
This commit is contained in:
Nicola Murino
2020-11-25 14:18:12 +01:00
parent 6699f5c2cc
commit 2054dfd83d

View File

@@ -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 { if filepath.IsAbs(config.CredentialsPath) {
return err 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 {