2016-12-19 18:15:58 +00:00
package database
import (
2017-04-13 00:35:02 +00:00
"errors"
2016-12-19 18:15:58 +00:00
"fmt"
"github.com/fatih/structs"
2017-04-06 19:20:10 +00:00
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
2016-12-19 18:15:58 +00:00
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
2017-04-13 00:35:02 +00:00
var (
respErrEmptyPluginName = logical . ErrorResponse ( "empty plugin name" )
respErrEmptyName = logical . ErrorResponse ( "Empty name attribute given" )
)
2017-04-11 18:50:34 +00:00
// pathResetConnection configures a path to reset a plugin.
2017-02-16 00:51:59 +00:00
func pathResetConnection ( b * databaseBackend ) * framework . Path {
return & framework . Path {
Pattern : fmt . Sprintf ( "reset/%s" , framework . GenericNameRegex ( "name" ) ) ,
Fields : map [ string ] * framework . FieldSchema {
"name" : & framework . FieldSchema {
Type : framework . TypeString ,
2017-04-13 00:35:02 +00:00
Description : "Name of this database connection" ,
2017-02-16 00:51:59 +00:00
} ,
} ,
Callbacks : map [ logical . Operation ] framework . OperationFunc {
2017-04-11 18:50:34 +00:00
logical . UpdateOperation : b . pathConnectionReset ( ) ,
2017-02-16 00:51:59 +00:00
} ,
2017-04-11 18:50:34 +00:00
HelpSynopsis : pathResetConnectionHelpSyn ,
HelpDescription : pathResetConnectionHelpDesc ,
2017-02-16 00:51:59 +00:00
}
}
2017-04-11 18:50:34 +00:00
// pathConnectionReset resets a plugin by closing the existing instance and
// creating a new one.
func ( b * databaseBackend ) pathConnectionReset ( ) framework . OperationFunc {
return func ( req * logical . Request , data * framework . FieldData ) ( * logical . Response , error ) {
name := data . Get ( "name" ) . ( string )
if name == "" {
2017-04-13 00:35:02 +00:00
return respErrEmptyName , nil
2017-04-11 18:50:34 +00:00
}
// Grab the mutex lock
b . Lock ( )
defer b . Unlock ( )
2017-02-16 00:51:59 +00:00
2017-04-13 00:35:02 +00:00
// Close plugin and delete the entry in the connections cache.
2017-04-11 18:50:34 +00:00
b . clearConnection ( name )
2017-02-16 00:51:59 +00:00
2017-04-13 00:35:02 +00:00
// Execute plugin again, we don't need the object so throw away.
2017-04-11 18:50:34 +00:00
_ , err := b . getOrCreateDBObj ( req . Storage , name )
if err != nil {
return nil , err
}
2017-02-16 00:51:59 +00:00
2017-04-11 18:50:34 +00:00
return nil , nil
2017-03-13 21:39:55 +00:00
}
2017-02-16 00:51:59 +00:00
}
2017-03-22 00:19:30 +00:00
// pathConfigurePluginConnection returns a configured framework.Path setup to
// operate on plugins.
2017-03-10 05:31:29 +00:00
func pathConfigurePluginConnection ( b * databaseBackend ) * framework . Path {
2016-12-19 18:15:58 +00:00
return & framework . Path {
2017-04-11 01:38:34 +00:00
Pattern : fmt . Sprintf ( "config/%s" , framework . GenericNameRegex ( "name" ) ) ,
2016-12-19 18:15:58 +00:00
Fields : map [ string ] * framework . FieldSchema {
"name" : & framework . FieldSchema {
Type : framework . TypeString ,
2017-04-13 00:35:02 +00:00
Description : "Name of this database connection" ,
2016-12-19 18:15:58 +00:00
} ,
2017-04-04 00:52:29 +00:00
"plugin_name" : & framework . FieldSchema {
2017-03-10 22:10:42 +00:00
Type : framework . TypeString ,
2017-04-11 18:50:34 +00:00
Description : ` The name of a builtin or previously registered
plugin known to vault . This endpoint will create an instance of
that plugin type . ` ,
2017-03-10 22:10:42 +00:00
} ,
2017-04-13 00:35:02 +00:00
"verify_connection" : & framework . FieldSchema {
Type : framework . TypeBool ,
Default : true ,
Description : ` If true , the connection details are verified by
actually connecting to the database . Defaults to true . ` ,
} ,
2016-12-19 18:15:58 +00:00
} ,
Callbacks : map [ logical . Operation ] framework . OperationFunc {
2017-04-11 01:38:34 +00:00
logical . UpdateOperation : b . connectionWriteHandler ( ) ,
logical . ReadOperation : b . connectionReadHandler ( ) ,
logical . DeleteOperation : b . connectionDeleteHandler ( ) ,
2016-12-19 18:15:58 +00:00
} ,
HelpSynopsis : pathConfigConnectionHelpSyn ,
HelpDescription : pathConfigConnectionHelpDesc ,
}
}
// pathConnectionRead reads out the connection configuration
2017-03-10 05:31:29 +00:00
func ( b * databaseBackend ) connectionReadHandler ( ) framework . OperationFunc {
return func ( req * logical . Request , data * framework . FieldData ) ( * logical . Response , error ) {
name := data . Get ( "name" ) . ( string )
2017-04-13 00:35:02 +00:00
if name == "" {
return respErrEmptyName , nil
}
2016-12-19 18:15:58 +00:00
2017-04-13 00:35:02 +00:00
entry , err := req . Storage . Get ( fmt . Sprintf ( "config/%s" , name ) )
2017-03-10 05:31:29 +00:00
if err != nil {
2017-04-13 00:35:02 +00:00
return nil , errors . New ( "failed to read connection configuration" )
2017-03-10 05:31:29 +00:00
}
if entry == nil {
return nil , nil
}
2016-12-19 18:15:58 +00:00
2017-04-05 23:20:31 +00:00
var config DatabaseConfig
2017-03-10 05:31:29 +00:00
if err := entry . DecodeJSON ( & config ) ; err != nil {
return nil , err
}
return & logical . Response {
Data : structs . New ( config ) . Map ( ) ,
} , nil
2016-12-19 18:15:58 +00:00
}
}
2017-03-22 00:19:30 +00:00
// connectionDeleteHandler deletes the connection configuration
func ( b * databaseBackend ) connectionDeleteHandler ( ) framework . OperationFunc {
return func ( req * logical . Request , data * framework . FieldData ) ( * logical . Response , error ) {
name := data . Get ( "name" ) . ( string )
if name == "" {
2017-04-13 00:35:02 +00:00
return respErrEmptyName , nil
2017-03-22 00:19:30 +00:00
}
2017-04-13 00:35:02 +00:00
err := req . Storage . Delete ( fmt . Sprintf ( "config/%s" , name ) )
2017-03-22 00:19:30 +00:00
if err != nil {
2017-04-13 00:35:02 +00:00
return nil , errors . New ( "failed to delete connection configuration" )
2017-03-22 00:19:30 +00:00
}
2017-03-22 16:54:19 +00:00
b . Lock ( )
defer b . Unlock ( )
2017-03-22 00:19:30 +00:00
if _ , ok := b . connections [ name ] ; ok {
err = b . connections [ name ] . Close ( )
if err != nil {
return nil , err
}
2017-04-13 00:35:02 +00:00
delete ( b . connections , name )
}
2017-03-22 00:19:30 +00:00
return nil , nil
}
}
// connectionWriteHandler returns a handler function for creating and updating
// both builtin and plugin database types.
2017-04-05 23:20:31 +00:00
func ( b * databaseBackend ) connectionWriteHandler ( ) framework . OperationFunc {
2017-03-10 05:31:29 +00:00
return func ( req * logical . Request , data * framework . FieldData ) ( * logical . Response , error ) {
2017-04-13 00:35:02 +00:00
pluginName := data . Get ( "plugin_name" ) . ( string )
if pluginName == "" {
return respErrEmptyPluginName , nil
2017-03-10 05:31:29 +00:00
}
2016-12-19 18:15:58 +00:00
2017-03-10 05:31:29 +00:00
name := data . Get ( "name" ) . ( string )
2017-03-13 21:39:55 +00:00
if name == "" {
2017-04-13 00:35:02 +00:00
return respErrEmptyName , nil
2017-03-13 21:39:55 +00:00
}
verifyConnection := data . Get ( "verify_connection" ) . ( bool )
2016-12-19 18:15:58 +00:00
2017-04-13 00:35:02 +00:00
config := & DatabaseConfig {
ConnectionDetails : data . Raw ,
PluginName : pluginName ,
}
2016-12-19 18:15:58 +00:00
2017-04-06 19:20:10 +00:00
db , err := dbplugin . PluginFactory ( config . PluginName , b . System ( ) , b . logger )
2017-03-21 23:05:59 +00:00
if err != nil {
return logical . ErrorResponse ( fmt . Sprintf ( "Error creating database object: %s" , err ) ) , nil
}
2016-12-19 18:15:58 +00:00
2017-04-10 22:36:59 +00:00
err = db . Initialize ( config . ConnectionDetails , verifyConnection )
2017-03-21 23:05:59 +00:00
if err != nil {
2017-04-10 22:36:59 +00:00
db . Close ( )
return logical . ErrorResponse ( fmt . Sprintf ( "Error creating database object: %s" , err ) ) , nil
2016-12-19 18:15:58 +00:00
}
2017-02-16 00:51:59 +00:00
2017-04-13 00:35:02 +00:00
// Grab the mutex lock
b . Lock ( )
defer b . Unlock ( )
2017-03-21 23:05:59 +00:00
if _ , ok := b . connections [ name ] ; ok {
2017-04-04 01:30:38 +00:00
// Close and remove the old connection
err := b . connections [ name ] . Close ( )
2017-03-10 05:31:29 +00:00
if err != nil {
2017-04-10 22:36:59 +00:00
db . Close ( )
2017-03-21 23:05:59 +00:00
return nil , err
2017-03-10 05:31:29 +00:00
}
2017-03-21 23:05:59 +00:00
2017-04-04 01:30:38 +00:00
delete ( b . connections , name )
2017-03-10 05:31:29 +00:00
}
2016-12-19 18:15:58 +00:00
2017-04-04 01:30:38 +00:00
// Save the new connection
b . connections [ name ] = db
2017-03-10 05:31:29 +00:00
// Store it
2017-04-13 00:35:02 +00:00
entry , err := logical . StorageEntryJSON ( fmt . Sprintf ( "config/%s" , name ) , config )
2017-03-08 22:46:53 +00:00
if err != nil {
2017-03-10 05:31:29 +00:00
return nil , err
2017-03-08 22:46:53 +00:00
}
2017-03-10 05:31:29 +00:00
if err := req . Storage . Put ( entry ) ; err != nil {
return nil , err
2016-12-19 18:15:58 +00:00
}
2017-03-10 05:31:29 +00:00
resp := & logical . Response { }
2017-04-13 00:35:02 +00:00
resp . AddWarning ( "Read access to this endpoint should be controlled via ACLs as it will return the connection details as is, including passwords, if any." )
2016-12-19 18:15:58 +00:00
2017-03-10 05:31:29 +00:00
return resp , nil
}
2016-12-19 18:15:58 +00:00
}
const pathConfigConnectionHelpSyn = `
2017-04-11 18:50:34 +00:00
Configure connection details to a database plugin .
2016-12-19 18:15:58 +00:00
`
const pathConfigConnectionHelpDesc = `
2017-04-11 18:50:34 +00:00
This path configures the connection details used to connect to a particular
database . This path runs the provided plugin name and passes the configured
connection details to the plugin . See the documentation for the plugin specified
for a full list of accepted connection details .
2016-12-19 18:15:58 +00:00
2017-04-11 18:50:34 +00:00
In addition to the database specific connection details , this endpoing also
accepts :
* "plugin_name" ( required ) - The name of a builtin or previously registered
plugin known to vault . This endpoint will create an instance of that
plugin type .
2017-04-13 00:35:02 +00:00
* "verify_connection" ( default : true ) - A boolean value denoting if the plugin should verify
2017-04-11 18:50:34 +00:00
it is able to connect to the database using the provided connection
details .
`
const pathResetConnectionHelpSyn = `
Resets a database plugin .
`
2016-12-19 18:15:58 +00:00
2017-04-11 18:50:34 +00:00
const pathResetConnectionHelpDesc = `
This path resets the database connection by closing the existing database plugin
instance and running a new one .
2016-12-19 18:15:58 +00:00
`