2016-12-19 18:15:58 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2017-03-28 18:30:45 +00:00
|
|
|
"fmt"
|
2017-04-26 22:55:34 +00:00
|
|
|
"net/rpc"
|
2016-12-19 18:15:58 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
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"
|
2018-03-21 19:05:56 +00:00
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/dbutil"
|
2016-12-19 18:15:58 +00:00
|
|
|
)
|
|
|
|
|
2017-04-13 00:35:02 +00:00
|
|
|
const databaseConfigPath = "database/config/"
|
2017-04-04 18:32:42 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
type dbPluginInstance struct {
|
|
|
|
sync.RWMutex
|
|
|
|
dbplugin.Database
|
|
|
|
|
|
|
|
id string
|
|
|
|
name string
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbi *dbPluginInstance) Close() error {
|
|
|
|
dbi.Lock()
|
|
|
|
defer dbi.Unlock()
|
|
|
|
|
|
|
|
if dbi.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dbi.closed = true
|
|
|
|
|
|
|
|
return dbi.Database.Close()
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
b := Backend(conf)
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Setup(ctx, conf); err != nil {
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Backend(conf *logical.BackendConfig) *databaseBackend {
|
|
|
|
var b databaseBackend
|
|
|
|
b.Backend = &framework.Backend{
|
|
|
|
Help: strings.TrimSpace(backendHelp),
|
|
|
|
|
2017-11-03 15:43:31 +00:00
|
|
|
PathsSpecial: &logical.Paths{
|
|
|
|
SealWrapStorage: []string{
|
|
|
|
"config/*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-12-19 18:15:58 +00:00
|
|
|
Paths: []*framework.Path{
|
2017-06-07 14:03:17 +00:00
|
|
|
pathListPluginConnection(&b),
|
2017-03-10 05:31:29 +00:00
|
|
|
pathConfigurePluginConnection(&b),
|
2016-12-19 18:15:58 +00:00
|
|
|
pathListRoles(&b),
|
|
|
|
pathRoles(&b),
|
2017-04-25 17:39:17 +00:00
|
|
|
pathCredsCreate(&b),
|
2017-02-16 00:51:59 +00:00
|
|
|
pathResetConnection(&b),
|
2018-03-21 19:05:56 +00:00
|
|
|
pathRotateCredentials(&b),
|
2016-12-19 18:15:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Secrets: []*framework.Secret{
|
|
|
|
secretCreds(&b),
|
|
|
|
},
|
2017-07-28 18:04:46 +00:00
|
|
|
Clean: b.closeAllDBs,
|
|
|
|
Invalidate: b.invalidate,
|
|
|
|
BackendType: logical.TypeLogical,
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.logger = conf.Logger
|
2018-03-21 19:05:56 +00:00
|
|
|
b.connections = make(map[string]*dbPluginInstance)
|
2016-12-19 18:15:58 +00:00
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
|
|
|
type databaseBackend struct {
|
2018-03-21 19:05:56 +00:00
|
|
|
connections map[string]*dbPluginInstance
|
2016-12-19 18:15:58 +00:00
|
|
|
logger log.Logger
|
|
|
|
|
|
|
|
*framework.Backend
|
2017-04-26 22:23:14 +00:00
|
|
|
sync.RWMutex
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *databaseBackend) DatabaseConfig(ctx context.Context, s logical.Storage, name string) (*DatabaseConfig, error) {
|
|
|
|
entry, err := s.Get(ctx, fmt.Sprintf("config/%s", name))
|
2017-04-13 17:33:34 +00:00
|
|
|
if err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
return nil, errwrap.Wrapf("failed to read connection configuration: {{err}}", err)
|
2017-04-13 17:33:34 +00:00
|
|
|
}
|
|
|
|
if entry == nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("failed to find entry for connection with name: %q", name)
|
2017-04-13 17:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var config DatabaseConfig
|
|
|
|
if err := entry.DecodeJSON(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2017-12-19 03:38:47 +00:00
|
|
|
type upgradeStatements struct {
|
|
|
|
// This json tag has a typo in it, the new version does not. This
|
|
|
|
// necessitates this upgrade logic.
|
|
|
|
CreationStatements string `json:"creation_statments"`
|
|
|
|
RevocationStatements string `json:"revocation_statements"`
|
|
|
|
RollbackStatements string `json:"rollback_statements"`
|
|
|
|
RenewStatements string `json:"renew_statements"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type upgradeCheck struct {
|
|
|
|
// This json tag has a typo in it, the new version does not. This
|
|
|
|
// necessitates this upgrade logic.
|
2018-03-21 19:05:56 +00:00
|
|
|
Statements *upgradeStatements `json:"statments,omitempty"`
|
2017-12-19 03:38:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *databaseBackend) Role(ctx context.Context, s logical.Storage, roleName string) (*roleEntry, error) {
|
|
|
|
entry, err := s.Get(ctx, "role/"+roleName)
|
2016-12-19 18:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-19 03:38:47 +00:00
|
|
|
var upgradeCh upgradeCheck
|
|
|
|
if err := entry.DecodeJSON(&upgradeCh); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-19 18:15:58 +00:00
|
|
|
var result roleEntry
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
switch {
|
|
|
|
case upgradeCh.Statements != nil:
|
|
|
|
var stmts dbplugin.Statements
|
|
|
|
if upgradeCh.Statements.CreationStatements != "" {
|
|
|
|
stmts.Creation = []string{upgradeCh.Statements.CreationStatements}
|
|
|
|
}
|
|
|
|
if upgradeCh.Statements.RevocationStatements != "" {
|
|
|
|
stmts.Revocation = []string{upgradeCh.Statements.RevocationStatements}
|
|
|
|
}
|
|
|
|
if upgradeCh.Statements.RollbackStatements != "" {
|
|
|
|
stmts.Rollback = []string{upgradeCh.Statements.RollbackStatements}
|
|
|
|
}
|
|
|
|
if upgradeCh.Statements.RenewStatements != "" {
|
|
|
|
stmts.Renewal = []string{upgradeCh.Statements.RenewStatements}
|
|
|
|
}
|
|
|
|
result.Statements = stmts
|
2017-12-19 03:38:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
// For backwards compatibility, copy the values back into the string form
|
|
|
|
// of the fields
|
|
|
|
result.Statements = dbutil.StatementCompatibilityHelper(result.Statements)
|
|
|
|
|
2016-12-19 18:15:58 +00:00
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *databaseBackend) invalidate(ctx context.Context, key string) {
|
2017-04-04 18:32:42 +00:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(key, databaseConfigPath):
|
|
|
|
name := strings.TrimPrefix(key, databaseConfigPath)
|
2018-03-21 19:05:56 +00:00
|
|
|
b.ClearConnection(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *databaseBackend) GetConnection(ctx context.Context, s logical.Storage, name string) (*dbPluginInstance, error) {
|
|
|
|
b.RLock()
|
|
|
|
unlockFunc := b.RUnlock
|
|
|
|
defer func() { unlockFunc() }()
|
|
|
|
|
|
|
|
db, ok := b.connections[name]
|
|
|
|
if ok {
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade lock
|
|
|
|
b.RUnlock()
|
|
|
|
b.Lock()
|
|
|
|
unlockFunc = b.Unlock
|
|
|
|
|
|
|
|
db, ok = b.connections[name]
|
|
|
|
if ok {
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := b.DatabaseConfig(ctx, s, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbp, err := dbplugin.PluginFactory(ctx, config.PluginName, b.System(), b.logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dbp.Init(ctx, config.ConnectionDetails, true)
|
|
|
|
if err != nil {
|
|
|
|
dbp.Close()
|
|
|
|
return nil, err
|
2017-04-04 18:32:42 +00:00
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
id, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db = &dbPluginInstance{
|
|
|
|
Database: dbp,
|
|
|
|
name: name,
|
|
|
|
id: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
b.connections[name] = db
|
|
|
|
return db, nil
|
2017-04-04 18:32:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
// ClearConnection closes the database connection and
|
2017-04-04 18:32:42 +00:00
|
|
|
// removes it from the b.connections map.
|
2018-03-21 19:05:56 +00:00
|
|
|
func (b *databaseBackend) ClearConnection(name string) error {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
return b.clearConnection(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *databaseBackend) clearConnection(name string) error {
|
2017-04-04 18:32:42 +00:00
|
|
|
db, ok := b.connections[name]
|
|
|
|
if ok {
|
2018-03-21 19:05:56 +00:00
|
|
|
// Ignore error here since the database client is always killed
|
2017-04-04 18:32:42 +00:00
|
|
|
db.Close()
|
|
|
|
delete(b.connections, name)
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
return nil
|
2017-04-04 18:32:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
func (b *databaseBackend) CloseIfShutdown(db *dbPluginInstance, err error) {
|
2017-04-26 22:55:34 +00:00
|
|
|
// Plugin has shutdown, close it so next call can reconnect.
|
2017-12-14 22:03:11 +00:00
|
|
|
switch err {
|
|
|
|
case rpc.ErrShutdown, dbplugin.ErrPluginShutdown:
|
2018-03-21 19:05:56 +00:00
|
|
|
// Put this in a goroutine so that requests can run with the read or write lock
|
|
|
|
// and simply defer the unlock. Since we are attaching the instance and matching
|
2018-04-08 17:34:59 +00:00
|
|
|
// the id in the connection map, we can safely do this.
|
2018-03-21 19:05:56 +00:00
|
|
|
go func() {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
db.Close()
|
|
|
|
|
|
|
|
// Ensure we are deleting the correct connection
|
|
|
|
mapDB, ok := b.connections[db.name]
|
|
|
|
if ok && db.id == mapDB.id {
|
|
|
|
delete(b.connections, db.name)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeAllDBs closes all connections from all database types
|
|
|
|
func (b *databaseBackend) closeAllDBs(ctx context.Context) {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
for _, db := range b.connections {
|
|
|
|
db.Close()
|
2017-04-26 22:55:34 +00:00
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
b.connections = make(map[string]*dbPluginInstance)
|
2017-04-26 22:55:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 18:15:58 +00:00
|
|
|
const backendHelp = `
|
2017-04-04 18:32:42 +00:00
|
|
|
The database backend supports using many different databases
|
|
|
|
as secret backends, including but not limited to:
|
2017-05-04 00:37:34 +00:00
|
|
|
cassandra, mssql, 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
|
|
|
`
|