Skip to content

Commit

Permalink
plugins/database: Allow both {{name}} and {{username}} in MySQL & Pos…
Browse files Browse the repository at this point in the history
…tgres (#8240)

* Allow {{name}} or {{username}} in psql templates

* Fix default rotation bug; allow {{user}} and {{username}}
  • Loading branch information
pcman312 authored Feb 3, 2020
1 parent 9611555 commit be05261
Show file tree
Hide file tree
Showing 4 changed files with 690 additions and 486 deletions.
6 changes: 5 additions & 1 deletion plugins/database/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (m *MySQL) CreateUser(ctx context.Context, statements dbplugin.Statements,

queryMap := map[string]string{
"name": username,
"username": username,
"password": password,
"expiration": expirationStr,
}
Expand Down Expand Up @@ -187,6 +188,7 @@ func (m *MySQL) RevokeUser(ctx context.Context, statements dbplugin.Statements,
// 1295: This command is not supported in the prepared statement protocol yet
// Reference https://mariadb.com/kb/en/mariadb/prepare-statement/
query = strings.Replace(query, "{{name}}", username, -1)
query = strings.Replace(query, "{{username}}", username, -1)
_, err = tx.ExecContext(ctx, query)
if err != nil {
return err
Expand Down Expand Up @@ -244,6 +246,7 @@ func (m *MySQL) RotateRootCredentials(ctx context.Context, statements []string)
// 1295: This command is not supported in the prepared statement protocol yet
// Reference https://mariadb.com/kb/en/mariadb/prepare-statement/
query = strings.Replace(query, "{{username}}", m.Username, -1)
query = strings.Replace(query, "{{name}}", m.Username, -1)
query = strings.Replace(query, "{{password}}", password, -1)

if _, err := tx.ExecContext(ctx, query); err != nil {
Expand Down Expand Up @@ -283,10 +286,11 @@ func (m *MySQL) SetCredentials(ctx context.Context, statements dbplugin.Statemen

queryMap := map[string]string{
"name": username,
"username": username,
"password": password,
}

if err := m.executePreparedStatmentsWithMap(ctx, statements.Rotation, queryMap); err != nil {
if err := m.executePreparedStatmentsWithMap(ctx, rotateStatements, queryMap); err != nil {
return "", "", err
}
return username, password, nil
Expand Down
Loading

0 comments on commit be05261

Please sign in to comment.