Database refactor invalidate (#2566)

* WIP on invalidate function

* cassandraConnectionProducer has Close()

* Delete database from connections map on successful db.Close()

* Move clear connection into its own func

* Use const for database config path
This commit is contained in:
Calvin Leung Huang
2017-04-04 14:32:42 -04:00
committed by Brian Kassouf
parent 1faa5fc020
commit 8e3cb50bfc
2 changed files with 31 additions and 8 deletions

View File

@@ -12,6 +12,8 @@ import (
"github.com/hashicorp/vault/logical/framework"
)
const databaseConfigPath = "database/dbs/"
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
return Backend(conf).Setup(conf)
}
@@ -41,6 +43,8 @@ func Backend(conf *logical.BackendConfig) *databaseBackend {
},
Clean: b.closeAllDBs,
Invalidate: b.invalidate,
}
b.logger = conf.Logger
@@ -123,9 +127,32 @@ func (b *databaseBackend) Role(s logical.Storage, n string) (*roleEntry, error)
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)
}
}
const backendHelp = `
The PostgreSQL backend dynamically generates database users.
The database backend supports using many different databases
as secret backends, including but not limited to:
cassandra, msslq, mysql, postgres
After mounting this backend, configure it using the endpoints within
the "config/" path.
the "database/dbs/" path.
`

View File

@@ -40,13 +40,9 @@ func (b *databaseBackend) pathConnectionReset(req *logical.Request, data *framew
b.Lock()
defer b.Unlock()
db, ok := b.connections[name]
if ok {
db.Close()
delete(b.connections, name)
}
b.clearConnection(name)
db, err := b.getOrCreateDBObj(req.Storage, name)
_, err := b.getOrCreateDBObj(req.Storage, name)
if err != nil {
return nil, err
}