Skip to content

Commit

Permalink
Locking updates in database backend (#3774)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishoffman authored and jefferai committed Jan 18, 2018
1 parent 95eea30 commit c7b4fc3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
7 changes: 4 additions & 3 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 @@ -83,9 +83,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)
return nil, err
}
Expand All @@ -98,6 +97,8 @@ func (b *databaseBackend) pathCredsCreateRead() framework.OperationFunc {
"role": name,
})
resp.Secret.TTL = role.DefaultTTL

unlockFunc()
return resp, nil
}
}
Expand Down
15 changes: 7 additions & 8 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 @@ -71,14 +71,14 @@ func (b *databaseBackend) secretCredsRenew() framework.OperationFunc {
// 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 +109,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 @@ -127,14 +127,13 @@ func (b *databaseBackend) secretCredsRevoke() framework.OperationFunc {
}
}

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
}
}

0 comments on commit c7b4fc3

Please sign in to comment.