Commit 8e3cb50b authored by Calvin Leung Huang's avatar Calvin Leung Huang Committed by Brian Kassouf
Browse files

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
parent 1faa5fc0
Showing with 31 additions and 8 deletions
+31 -8
...@@ -12,6 +12,8 @@ import ( ...@@ -12,6 +12,8 @@ import (
"github.com/hashicorp/vault/logical/framework" "github.com/hashicorp/vault/logical/framework"
) )
const databaseConfigPath = "database/dbs/"
func Factory(conf *logical.BackendConfig) (logical.Backend, error) { func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
return Backend(conf).Setup(conf) return Backend(conf).Setup(conf)
} }
...@@ -41,6 +43,8 @@ func Backend(conf *logical.BackendConfig) *databaseBackend { ...@@ -41,6 +43,8 @@ func Backend(conf *logical.BackendConfig) *databaseBackend {
}, },
Clean: b.closeAllDBs, Clean: b.closeAllDBs,
Invalidate: b.invalidate,
} }
b.logger = conf.Logger b.logger = conf.Logger
...@@ -123,9 +127,32 @@ func (b *databaseBackend) Role(s logical.Storage, n string) (*roleEntry, error) ...@@ -123,9 +127,32 @@ func (b *databaseBackend) Role(s logical.Storage, n string) (*roleEntry, error)
return &result, nil 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 = ` 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 After mounting this backend, configure it using the endpoints within
the "config/" path. the "database/dbs/" path.
` `
...@@ -40,13 +40,9 @@ func (b *databaseBackend) pathConnectionReset(req *logical.Request, data *framew ...@@ -40,13 +40,9 @@ func (b *databaseBackend) pathConnectionReset(req *logical.Request, data *framew
b.Lock() b.Lock()
defer b.Unlock() defer b.Unlock()
db, ok := b.connections[name] b.clearConnection(name)
if ok {
db.Close()
delete(b.connections, name)
}
db, err := b.getOrCreateDBObj(req.Storage, name) _, err := b.getOrCreateDBObj(req.Storage, name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment