Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locking updates in database backend #3774

Merged
merged 3 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions builtin/logical/database/path_creds_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (b *databaseBackend) pathCredsCreateRead() framework.OperationFunc {

// Grab the read lock
b.RLock()
var unlockFunc func() = b.RUnlock
unlockFunc := b.RUnlock

// Get the Database object
db, ok := b.getDBObj(role.DBName)
Expand All @@ -66,11 +66,15 @@ func (b *databaseBackend) pathCredsCreateRead() framework.OperationFunc {
b.Lock()
unlockFunc = b.Unlock

// Create a new DB object
db, err = b.createDBObj(ctx, req.Storage, role.DBName)
if err != nil {
unlockFunc()
return nil, fmt.Errorf("cound not retrieve db with name: %s, got error: %s", role.DBName, err)
// Check again
db, ok = b.getDBObj(role.DBName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the createDBObj function does this check at the beginning of the function, so doesn't necessarily need to happen here.

if !ok {
// Create a new DB object
db, err = b.createDBObj(ctx, req.Storage, role.DBName)
if err != nil {
unlockFunc()
return nil, fmt.Errorf("cound not retrieve db with name: %s, got error: %s", role.DBName, err)
}
}
}

Expand All @@ -83,9 +87,8 @@ func (b *databaseBackend) pathCredsCreateRead() framework.OperationFunc {

// Create the user
username, password, err := db.CreateUser(ctx, role.Statements, usernameConfig, expiration)
// Unlock
unlockFunc()
if err != nil {
unlockFunc()
b.closeIfShutdown(role.DBName, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than giving up the lock and requiring it in the closeIfShutdown function what if we unlocked after the closeIfShutdown call. That'll protect against another thread getting the lock before we reset the plugin.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally went down that route but we don't always have the write lock when this method gets called.

return nil, err
}
Expand All @@ -98,6 +101,8 @@ func (b *databaseBackend) pathCredsCreateRead() framework.OperationFunc {
"role": name,
})
resp.Secret.TTL = role.DefaultTTL

unlockFunc()
return resp, nil
}
}
Expand Down
43 changes: 25 additions & 18 deletions builtin/logical/database/secret_creds.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (b *databaseBackend) secretCredsRenew() framework.OperationFunc {

// Grab the read lock
b.RLock()
var unlockFunc func() = b.RUnlock
unlockFunc := b.RUnlock

// Get the Database object
db, ok := b.getDBObj(role.DBName)
Expand All @@ -60,25 +60,29 @@ func (b *databaseBackend) secretCredsRenew() framework.OperationFunc {
b.Lock()
unlockFunc = b.Unlock

// Create a new DB object
db, err = b.createDBObj(ctx, req.Storage, role.DBName)
if err != nil {
unlockFunc()
return nil, fmt.Errorf("cound not retrieve db with name: %s, got error: %s", role.DBName, err)
// Check again
db, ok = b.getDBObj(role.DBName)
if !ok {
// Create a new DB object
db, err = b.createDBObj(ctx, req.Storage, role.DBName)
if err != nil {
unlockFunc()
return nil, fmt.Errorf("cound not retrieve db with name: %s, got error: %s", role.DBName, err)
}
}
}

// Make sure we increase the VALID UNTIL endpoint for this user.
if expireTime := resp.Secret.ExpirationTime(); !expireTime.IsZero() {
err := db.RenewUser(ctx, role.Statements, username, expireTime)
// Unlock
unlockFunc()
if err != nil {
unlockFunc()
b.closeIfShutdown(role.DBName, err)
return nil, err
}
}

unlockFunc()
return resp, nil
}
}
Expand Down Expand Up @@ -109,7 +113,7 @@ func (b *databaseBackend) secretCredsRevoke() framework.OperationFunc {

// Grab the read lock
b.RLock()
var unlockFunc func() = b.RUnlock
unlockFunc := b.RUnlock

// Get our connection
db, ok := b.getDBObj(role.DBName)
Expand All @@ -119,22 +123,25 @@ func (b *databaseBackend) secretCredsRevoke() framework.OperationFunc {
b.Lock()
unlockFunc = b.Unlock

// Create a new DB object
db, err = b.createDBObj(ctx, req.Storage, role.DBName)
if err != nil {
unlockFunc()
return nil, fmt.Errorf("cound not retrieve db with name: %s, got error: %s", role.DBName, err)
// Check again
db, ok = b.getDBObj(role.DBName)
if !ok {
// Create a new DB object
db, err = b.createDBObj(ctx, req.Storage, role.DBName)
if err != nil {
unlockFunc()
return nil, fmt.Errorf("cound not retrieve db with name: %s, got error: %s", role.DBName, err)
}
}
}

err = db.RevokeUser(ctx, role.Statements, username)
// Unlock
unlockFunc()
if err != nil {
if err := db.RevokeUser(ctx, role.Statements, username); err != nil {
unlockFunc()
b.closeIfShutdown(role.DBName, err)
return nil, err
}

unlockFunc()
return resp, nil
}
}