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

added rollback statement support for mysql database #6

Merged
Merged
Changes from all 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
17 changes: 15 additions & 2 deletions plugins/database/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

stdmysql "github.com/go-sql-driver/mysql"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-secure-stdlib/strutil"
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
"github.com/hashicorp/vault/sdk/database/helper/dbutil"
Expand All @@ -24,6 +25,9 @@ const (
ALTER USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';
`

defaultMySQLRollbackStmt = `
DROP USER '{{username}}'@'%';
`
mySQLTypeName = "mysql"

DefaultUserNameTemplate = `{{ printf "v-%s-%s-%s-%s" (.DisplayName | truncate 10) (.RoleName | truncate 10) (random 20) (unix_time) | truncate 32 }}`
Expand Down Expand Up @@ -130,8 +134,17 @@ func (m *MySQL) NewUser(ctx context.Context, req dbplugin.NewUserRequest) (dbplu
"expiration": expirationStr,
}

if err := m.executePreparedStatementsWithMap(ctx, req.Statements.Commands, queryMap); err != nil {
return dbplugin.NewUserResponse{}, err
createErr := m.executePreparedStatementsWithMap(ctx, req.Statements.Commands, queryMap)
if createErr != nil {
rollbackStmts := req.RollbackStatements.Commands
if len(rollbackStmts) == 0 {
rollbackStmts = []string{defaultMySQLRollbackStmt}
}
rollbackErr := m.executePreparedStatementsWithMap(ctx, rollbackStmts, queryMap)
if rollbackErr != nil {
return dbplugin.NewUserResponse{}, multierror.Append(createErr, rollbackErr)
}
return dbplugin.NewUserResponse{}, createErr
}

resp := dbplugin.NewUserResponse{
Expand Down
Loading