Skip to content

Commit

Permalink
style: error handling, imports, signature line wraps
Browse files Browse the repository at this point in the history
  • Loading branch information
austingebauer committed Apr 22, 2020
1 parent 003c803 commit 03c73d9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
8 changes: 2 additions & 6 deletions builtin/logical/database/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ func (b *databaseBackend) invalidate(ctx context.Context, key string) {
}
}

func (b *databaseBackend) GetConnection(ctx context.Context, s logical.Storage,
name string) (*dbPluginInstance, error) {

func (b *databaseBackend) GetConnection(ctx context.Context, s logical.Storage, name string) (*dbPluginInstance, error) {
config, err := b.DatabaseConfig(ctx, s, name)
if err != nil {
return nil, err
Expand All @@ -237,9 +235,7 @@ func (b *databaseBackend) GetConnection(ctx context.Context, s logical.Storage,
return b.GetConnectionWithConfig(ctx, name, config)
}

func (b *databaseBackend) GetConnectionWithConfig(ctx context.Context, name string,
config *DatabaseConfig) (*dbPluginInstance, error) {

func (b *databaseBackend) GetConnectionWithConfig(ctx context.Context, name string, config *DatabaseConfig) (*dbPluginInstance, error) {
b.RLock()
unlockFunc := b.RUnlock
defer func() { unlockFunc() }()
Expand Down
20 changes: 9 additions & 11 deletions builtin/logical/database/path_rotate_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,15 @@ func (b *databaseBackend) pathRotateCredentialsUpdate() framework.OperationFunc
Username: userName,
Password: newPassword,
}
_, _, err = db.SetCredentials(ctx, statements, userConfig)

// Fall back to using RotateRootCredentials if unimplemented
if err != nil && status.Code(err) == codes.Unimplemented {
config.ConnectionDetails, err = db.RotateRootCredentials(ctx,
config.RootCredentialsRotateStatements)
}

// Handle any error from the root credential rotation
if err != nil {
return nil, err
if _, _, err := db.SetCredentials(ctx, statements, userConfig); err != nil {
if status.Code(err) == codes.Unimplemented {
// Fall back to using RotateRootCredentials if unimplemented
config.ConnectionDetails, err = db.RotateRootCredentials(ctx,
config.RootCredentialsRotateStatements)
}
if err != nil {
return nil, err
}
}

// Update storage with the new root credentials
Expand Down
33 changes: 15 additions & 18 deletions builtin/logical/database/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package database
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/database/dbplugin"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/hashicorp/vault/sdk/database/dbplugin"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// WAL storage key used for the rollback of root database credentials
Expand All @@ -26,9 +26,7 @@ type rotateRootCredentialsWAL struct {
// to rotate the root credentials of a database. It is responsible
// for rolling back root database credentials when doing so would
// reconcile the credentials with Vault storage.
func (b *databaseBackend) walRollback(ctx context.Context, req *logical.Request, kind string,
data interface{}) error {

func (b *databaseBackend) walRollback(ctx context.Context, req *logical.Request, kind string, data interface{}) error {
if kind != rotateRootWALKey {
return errors.New("unknown type to rollback")
}
Expand Down Expand Up @@ -78,9 +76,7 @@ func (b *databaseBackend) walRollback(ctx context.Context, req *logical.Request,
// the connection associated with the passed WAL entry. It will creates
// a connection to the database using the WAL entry new password in
// order to alter the password to be the WAL entry old password.
func (b *databaseBackend) rollbackDatabaseCredentials(ctx context.Context, config *DatabaseConfig,
entry rotateRootCredentialsWAL) error {

func (b *databaseBackend) rollbackDatabaseCredentials(ctx context.Context, config *DatabaseConfig, entry rotateRootCredentialsWAL) error {
// Attempt to get a connection with the WAL entry new password.
config.ConnectionDetails["password"] = entry.NewPassword
dbc, err := b.GetConnectionWithConfig(ctx, entry.ConnectionName, config)
Expand All @@ -101,15 +97,16 @@ func (b *databaseBackend) rollbackDatabaseCredentials(ctx context.Context, confi
Username: entry.UserName,
Password: entry.OldPassword,
}
_, _, err = dbc.SetCredentials(ctx, statements, userConfig)

// If the database plugin doesn't implement SetCredentials,
// the root credentials can't be rolled back. This means
// the root credential rotation happened via the plugin
// RotateRootCredentials RPC.
if err != nil && status.Code(err) == codes.Unimplemented {
return nil
if _, _, err := dbc.SetCredentials(ctx, statements, userConfig); err != nil {
// If the database plugin doesn't implement SetCredentials, the root
// credentials can't be rolled back. This means the root credential
// rotation happened via the plugin RotateRootCredentials RPC.
if status.Code(err) == codes.Unimplemented {
return nil
}

return err
}

return err
return nil
}
6 changes: 2 additions & 4 deletions sdk/database/dbplugin/databasemiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (
"sync"
"time"

"google.golang.org/grpc/status"

"github.com/hashicorp/errwrap"

metrics "github.com/armon/go-metrics"
"github.com/hashicorp/errwrap"
log "github.com/hashicorp/go-hclog"
"google.golang.org/grpc/status"
)

// ---- Tracing Middleware Domain ----
Expand Down

0 comments on commit 03c73d9

Please sign in to comment.