Verify connections regardless of if this connections is already existing

This commit is contained in:
Brian Kassouf 2017-03-21 16:05:59 -07:00
parent a4e5e0f8c9
commit 83ff132705
1 changed files with 23 additions and 37 deletions

View File

@ -223,53 +223,39 @@ func (b *databaseBackend) connectionWriteHandler(factory dbs.Factory) framework.
b.Lock() b.Lock()
defer b.Unlock() defer b.Unlock()
var db dbs.DatabaseType db, err := factory(config, b.System())
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("Error creating database object: %s", err)), nil
}
err = db.Initialize(config.ConnectionDetails)
if err != nil {
if !strings.Contains(err.Error(), "Error Initializing Connection") {
return logical.ErrorResponse(fmt.Sprintf("Error creating database object: %s", err)), nil
}
if verifyConnection {
return logical.ErrorResponse(err.Error()), nil
}
}
if _, ok := b.connections[name]; ok { if _, ok := b.connections[name]; ok {
// Don't update connection until the reset api is hit, close for
// now.
err = db.Close()
if err != nil {
return nil, err
}
// Don't allow the connection type to change // Don't allow the connection type to change
if b.connections[name].Type() != connType { if b.connections[name].Type() != connType {
return logical.ErrorResponse("Can not change type of existing connection."), nil return logical.ErrorResponse("Can not change type of existing connection."), nil
} }
} else { } else {
db, err = factory(config, b.System()) // Save the new connection
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("Error creating database object: %s", err)), nil
}
err := db.Initialize(config.ConnectionDetails)
if err != nil {
if !strings.Contains(err.Error(), "Error Initializing Connection") {
return logical.ErrorResponse(fmt.Sprintf("Error creating database object: %s", err)), nil
}
if verifyConnection {
return logical.ErrorResponse(err.Error()), nil
}
}
b.connections[name] = db b.connections[name] = db
} }
/* TODO:
// Don't check the connection_url if verification is disabled
verifyConnection := data.Get("verify_connection").(bool)
if verifyConnection {
// Verify the string
db, err := sql.Open("postgres", connURL)
if err != nil {
return logical.ErrorResponse(fmt.Sprintf(
"Error validating connection info: %s", err)), nil
}
defer db.Close()
if err := db.Ping(); err != nil {
return logical.ErrorResponse(fmt.Sprintf(
"Error validating connection info: %s", err)), nil
}
}
*/
// Store it // Store it
entry, err := logical.StorageEntryJSON(fmt.Sprintf("dbs/%s", name), config) entry, err := logical.StorageEntryJSON(fmt.Sprintf("dbs/%s", name), config)
if err != nil { if err != nil {