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
Branches unavailable v1.10.2 v1.10.1 v1.10.0 v1.10.0-rc1 v1.9.6 v1.9.5 v1.9.4 v1.9.3 v1.9.2 v1.9.1 v1.9.0 v1.9.0-rc1 v1.8.11 v1.8.10 v1.8.9 v1.8.8 v1.8.7 v1.8.6 v1.8.5 v1.8.4 v1.8.3 v1.8.2 v1.8.1 v1.8.0 v1.8.0-rc2 v1.8.0-rc1 v1.7.10 v1.7.9 v1.7.8 v1.7.7 v1.7.6 v1.7.5 v1.7.4 v1.7.3 v1.7.2 v1.7.1 v1.7.0 v1.7.0-rc2 v1.7.0-rc1 v1.6.7 v1.6.6 v1.6.5 v1.6.4 v1.6.3 v1.6.2 v1.6.1 v1.6.0 v1.6.0-rc v1.5.9 v1.5.8 v1.5.7 v1.5.6 v1.5.5 v1.5.4 v1.5.3 v1.5.2 v1.5.1 v1.5.0 v1.5.0-rc v1.4.7 v1.4.6 v1.4.5 v1.4.4 v1.4.3 v1.4.2 v1.4.1 v1.4.0 v1.4.0-rc1 v1.4.0-beta1 v1.3.10 v1.3.9 v1.3.8 v1.3.7 v1.3.6 v1.3.5 v1.3.4 v1.3.3 v1.3.2 v1.3.1 v1.3.0 v1.3.0-beta1 v1.2.7 v1.2.6 v1.2.5 v1.2.4 v1.2.3 v1.2.2 v1.2.1 v1.2.0 v1.2.0-rc1 v1.2.0-beta2 v1.2.0-beta1 v1.1.5 v1.1.4 v1.1.3 v1.1.2 v1.1.1 v1.1.0 v1.1.0-beta2 v1.1.0-beta1 v1.0.3 v1.0.2 v1.0.1 v1.0.0 v1.0.0-rc1 v1.0.0-beta2 v1.0.0-beta1 v0.11.6 v0.11.5 v0.11.4 v0.11.3 v0.11.2 v0.11.1 v0.11.0 v0.11.0-beta1 v0.10.4 v0.10.3 v0.10.2 v0.10.1 v0.10.0 v0.10.0-rc1 v0.9.6 v0.9.5 v0.9.4 v0.9.3 v0.9.2 v0.9.1 v0.9.0 v0.8.3 v0.8.2 v0.8.1 v0.8.0 v0.8.0-rc1 v0.8.0-beta1 v0.7.3 v0.7.2 v0.7.1 sdk/v0.4.1 sdk/v0.4.0 sdk/v0.3.0 sdk/v0.2.1 sdk/v0.2.0 sdk/v0.1.13 sdk/v0.1.12 sdk/v0.1.11 sdk/v0.1.10 sdk/v0.1.9 sdk/v0.1.8 old-stable-website old-stable-website-20210728 main-creation last-go-modable api/v1.5.0 api/v1.4.1 api/v1.4.0 api/v1.3.1 api/v1.3.0 api/v1.2.0 api/v1.1.1 api/v1.1.0 api/v1.0.4 api/v1.0.3 api/v1.0.2 api/v1.0.1 api/auth/userpass/v0.1.0 api/auth/ldap/v0.1.0 api/auth/kubernetes/v0.1.0 api/auth/gcp/v0.1.0 api/auth/azure/v0.1.0 api/auth/aws/v0.1.0 api/auth/approle/v0.1.1 api/auth/approle/v0.1.0
No related merge requests found
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