open-vault/builtin/logical/database/backend.go

161 lines
3.5 KiB
Go
Raw Normal View History

2016-12-19 18:15:58 +00:00
package database
import (
"fmt"
2016-12-19 18:15:58 +00:00
"strings"
"sync"
log "github.com/mgutz/logxi/v1"
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
const databaseConfigPath = "database/config/"
2016-12-19 18:15:58 +00:00
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
return Backend(conf).Setup(conf)
}
func Backend(conf *logical.BackendConfig) *databaseBackend {
var b databaseBackend
b.Backend = &framework.Backend{
Help: strings.TrimSpace(backendHelp),
Paths: []*framework.Path{
pathConfigurePluginConnection(&b),
2016-12-19 18:15:58 +00:00
pathListRoles(&b),
pathRoles(&b),
pathRoleCreate(&b),
pathResetConnection(&b),
2016-12-19 18:15:58 +00:00
},
Secrets: []*framework.Secret{
secretCreds(&b),
},
2017-03-10 06:35:45 +00:00
Clean: b.closeAllDBs,
Invalidate: b.invalidate,
2016-12-19 18:15:58 +00:00
}
b.logger = conf.Logger
2017-04-24 20:59:12 +00:00
b.connections = make(map[string]dbplugin.Database)
2016-12-19 18:15:58 +00:00
return &b
}
type databaseBackend struct {
2017-04-24 20:59:12 +00:00
connections map[string]dbplugin.Database
2016-12-19 18:15:58 +00:00
logger log.Logger
*framework.Backend
sync.Mutex
2016-12-19 18:15:58 +00:00
}
// resetAllDBs closes all connections from all database types
2017-03-10 06:35:45 +00:00
func (b *databaseBackend) closeAllDBs() {
2016-12-19 18:15:58 +00:00
b.Lock()
defer b.Unlock()
for _, db := range b.connections {
db.Close()
}
2017-04-11 00:12:52 +00:00
2017-04-24 20:59:12 +00:00
b.connections = make(map[string]dbplugin.Database)
2016-12-19 18:15:58 +00:00
}
// This function is used to retrieve a database object either from the cached
// connection map or by using the database config in storage. The caller of this
// function needs to hold the backend's lock.
2017-04-24 20:59:12 +00:00
func (b *databaseBackend) getOrCreateDBObj(s logical.Storage, name string) (dbplugin.Database, error) {
// if the object already is built and cached, return it
db, ok := b.connections[name]
if ok {
return db, nil
}
2017-04-13 17:33:34 +00:00
config, err := b.DatabaseConfig(s, name)
if err != nil {
return nil, err
}
2017-04-06 19:20:10 +00:00
db, err = dbplugin.PluginFactory(config.PluginName, b.System(), b.logger)
if err != nil {
return nil, err
}
err = db.Initialize(config.ConnectionDetails, true)
if err != nil {
return nil, err
}
b.connections[name] = db
return db, nil
}
2017-04-13 17:33:34 +00:00
func (b *databaseBackend) DatabaseConfig(s logical.Storage, name string) (*DatabaseConfig, error) {
entry, err := s.Get(fmt.Sprintf("config/%s", name))
if err != nil {
return nil, fmt.Errorf("failed to read connection configuration with name: %s", name)
}
if entry == nil {
return nil, fmt.Errorf("failed to find entry for connection with name: %s", name)
}
var config DatabaseConfig
if err := entry.DecodeJSON(&config); err != nil {
return nil, err
}
return &config, nil
}
2016-12-19 18:15:58 +00:00
func (b *databaseBackend) Role(s logical.Storage, n string) (*roleEntry, error) {
entry, err := s.Get("role/" + n)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var result roleEntry
if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}
return &result, nil
}
func (b *databaseBackend) invalidate(key string) {
b.Lock()
defer b.Unlock()
switch {
case strings.HasPrefix(key, databaseConfigPath):
name := strings.TrimPrefix(key, databaseConfigPath)
b.clearConnection(name)
}
}
// clearConnection closes the database connection and
// removes it from the b.connections map.
func (b *databaseBackend) clearConnection(name string) {
db, ok := b.connections[name]
if ok {
db.Close()
delete(b.connections, name)
}
}
2016-12-19 18:15:58 +00:00
const backendHelp = `
The database backend supports using many different databases
as secret backends, including but not limited to:
cassandra, msslq, mysql, postgres
2016-12-19 18:15:58 +00:00
After mounting this backend, configure it using the endpoints within
2017-04-12 23:41:06 +00:00
the "database/config/" path.
2016-12-19 18:15:58 +00:00
`