2017-04-13 20:48:32 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2017-04-13 20:48:32 +00:00
|
|
|
"database/sql"
|
2021-02-11 21:08:32 +00:00
|
|
|
"fmt"
|
2017-04-13 20:48:32 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-05 18:52:56 +00:00
|
|
|
stdmysql "github.com/go-sql-driver/mysql"
|
2020-01-16 22:51:10 +00:00
|
|
|
mysqlhelper "github.com/hashicorp/vault/helper/testhelpers/mysql"
|
2020-10-15 19:20:12 +00:00
|
|
|
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
2021-02-11 21:08:32 +00:00
|
|
|
dbtesting "github.com/hashicorp/vault/sdk/database/dbplugin/v5/testing"
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/credsutil"
|
2019-07-05 18:52:56 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/dbutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
2021-02-11 21:08:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-04-13 20:48:32 +00:00
|
|
|
)
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
var _ dbplugin.Database = (*MySQL)(nil)
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
func TestMySQL_Initialize(t *testing.T) {
|
2021-02-11 21:08:32 +00:00
|
|
|
type testCase struct {
|
|
|
|
rootPassword string
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
tests := map[string]testCase{
|
|
|
|
"non-special characters in root password": {
|
|
|
|
rootPassword: "B44a30c4C04D0aAaE140",
|
|
|
|
},
|
|
|
|
"special characters in root password": {
|
|
|
|
rootPassword: "#secret!%25#{@}",
|
|
|
|
},
|
2020-10-09 17:32:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
testInitialize(t, test.rootPassword)
|
|
|
|
})
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
2021-02-11 21:08:32 +00:00
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
func testInitialize(t *testing.T, rootPassword string) {
|
|
|
|
cleanup, connURL := mysqlhelper.PrepareTestContainer(t, false, rootPassword)
|
|
|
|
defer cleanup()
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
mySQLConfig, err := stdmysql.ParseDSN(connURL)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
2021-02-11 21:08:32 +00:00
|
|
|
panic(fmt.Sprintf("Test failure: connection URL is invalid: %s", err))
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
2021-02-11 21:08:32 +00:00
|
|
|
rootUser := mySQLConfig.User
|
|
|
|
mySQLConfig.User = "{{username}}"
|
|
|
|
mySQLConfig.Passwd = "{{password}}"
|
|
|
|
tmplConnURL := mySQLConfig.FormatDSN()
|
2017-06-15 01:59:27 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
type testCase struct {
|
|
|
|
initRequest dbplugin.InitializeRequest
|
|
|
|
expectedResp dbplugin.InitializeResponse
|
|
|
|
|
|
|
|
expectErr bool
|
|
|
|
expectInitialized bool
|
2017-06-15 01:59:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
tests := map[string]testCase{
|
|
|
|
"missing connection_url": {
|
|
|
|
initRequest: dbplugin.InitializeRequest{
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
|
|
|
expectedResp: dbplugin.InitializeResponse{},
|
|
|
|
expectErr: true,
|
|
|
|
expectInitialized: false,
|
|
|
|
},
|
|
|
|
"basic config": {
|
|
|
|
initRequest: dbplugin.InitializeRequest{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
},
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
|
|
|
expectedResp: dbplugin.InitializeResponse{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
expectInitialized: true,
|
|
|
|
},
|
|
|
|
"username and password replacement in connection_url": {
|
|
|
|
initRequest: dbplugin.InitializeRequest{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"connection_url": tmplConnURL,
|
|
|
|
"username": rootUser,
|
|
|
|
"password": rootPassword,
|
|
|
|
},
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
|
|
|
expectedResp: dbplugin.InitializeResponse{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"connection_url": tmplConnURL,
|
|
|
|
"username": rootUser,
|
|
|
|
"password": rootPassword,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
expectInitialized: true,
|
|
|
|
},
|
|
|
|
"invalid username template": {
|
|
|
|
initRequest: dbplugin.InitializeRequest{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"username_template": "{{.FieldThatDoesNotExist}}",
|
|
|
|
},
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
|
|
|
expectedResp: dbplugin.InitializeResponse{},
|
|
|
|
expectErr: true,
|
|
|
|
expectInitialized: false,
|
|
|
|
},
|
|
|
|
"bad username template": {
|
|
|
|
initRequest: dbplugin.InitializeRequest{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"username_template": "{{ .DisplayName", // Explicitly bad template
|
|
|
|
},
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
|
|
|
expectedResp: dbplugin.InitializeResponse{},
|
|
|
|
expectErr: true,
|
|
|
|
expectInitialized: false,
|
|
|
|
},
|
|
|
|
"custom username template": {
|
|
|
|
initRequest: dbplugin.InitializeRequest{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"username_template": "foo-{{random 10}}-{{.DisplayName}}",
|
|
|
|
},
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
|
|
|
expectedResp: dbplugin.InitializeResponse{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"username_template": "foo-{{random 10}}-{{.DisplayName}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
expectInitialized: true,
|
|
|
|
},
|
2020-10-09 17:32:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
db := newMySQL(DefaultUserNameTemplate)
|
|
|
|
defer dbtesting.AssertClose(t, db)
|
|
|
|
initResp, err := db.Initialize(context.Background(), test.initRequest)
|
|
|
|
if test.expectErr && err == nil {
|
|
|
|
t.Fatalf("err expected, got nil")
|
|
|
|
}
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Fatalf("no error expected, got: %s", err)
|
|
|
|
}
|
|
|
|
require.Equal(t, test.expectedResp, initResp)
|
|
|
|
require.Equal(t, test.expectInitialized, db.Initialized, "Initialized variable not set correctly")
|
|
|
|
})
|
2017-06-15 01:59:27 +00:00
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
func TestMySQL_NewUser_nonLegacy(t *testing.T) {
|
|
|
|
displayName := "token"
|
|
|
|
roleName := "testrole"
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
type testCase struct {
|
|
|
|
usernameTemplate string
|
2020-10-09 17:32:38 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
newUserReq dbplugin.NewUserRequest
|
|
|
|
|
|
|
|
expectedUsernameRegex string
|
|
|
|
expectErr bool
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testCase{
|
|
|
|
"name statements": {
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
GRANT SELECT ON *.* TO '{{name}}'@'%';`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: "09g8hanbdfkVSM",
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
2020-10-09 17:32:38 +00:00
|
|
|
},
|
2021-02-11 21:08:32 +00:00
|
|
|
|
|
|
|
expectedUsernameRegex: `^v-token-testrole-[a-zA-Z0-9]{15}$`,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
"username statements": {
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
GRANT SELECT ON *.* TO '{{username}}'@'%';`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: "09g8hanbdfkVSM",
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
2020-10-09 17:32:38 +00:00
|
|
|
},
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
expectedUsernameRegex: `^v-token-testrole-[a-zA-Z0-9]{15}$`,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
"prepared name statements": {
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
set @grants=CONCAT("GRANT SELECT ON ", "*", ".* TO '{{name}}'@'%'");
|
|
|
|
PREPARE grantStmt from @grants;
|
|
|
|
EXECUTE grantStmt;
|
|
|
|
DEALLOCATE PREPARE grantStmt;`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: "09g8hanbdfkVSM",
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
|
|
|
},
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
expectedUsernameRegex: `^v-token-testrole-[a-zA-Z0-9]{15}$`,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
"prepared username statements": {
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
set @grants=CONCAT("GRANT SELECT ON ", "*", ".* TO '{{username}}'@'%'");
|
|
|
|
PREPARE grantStmt from @grants;
|
|
|
|
EXECUTE grantStmt;
|
|
|
|
DEALLOCATE PREPARE grantStmt;`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: "09g8hanbdfkVSM",
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
|
|
|
},
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
expectedUsernameRegex: `^v-token-testrole-[a-zA-Z0-9]{15}$`,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
"custom username template": {
|
|
|
|
usernameTemplate: "foo-{{random 10}}-{{.RoleName | uppercase}}",
|
2017-08-11 01:28:18 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
GRANT SELECT ON *.* TO '{{username}}'@'%';`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: "09g8hanbdfkVSM",
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
|
|
|
},
|
2020-10-09 17:32:38 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
expectedUsernameRegex: `^foo-[a-zA-Z0-9]{10}-TESTROLE$`,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
}
|
2017-08-11 01:28:18 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
cleanup, connURL := mysqlhelper.PrepareTestContainer(t, false, "secret")
|
|
|
|
defer cleanup()
|
2017-11-28 18:19:49 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"username_template": test.usernameTemplate,
|
|
|
|
}
|
2017-11-28 18:19:49 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
initReq := dbplugin.InitializeRequest{
|
|
|
|
Config: connectionDetails,
|
|
|
|
VerifyConnection: true,
|
|
|
|
}
|
2017-11-28 18:19:49 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
db := newMySQL(DefaultUserNameTemplate)
|
|
|
|
defer db.Close()
|
|
|
|
_, err := db.Initialize(context.Background(), initReq)
|
|
|
|
require.NoError(t, err)
|
2020-10-09 17:32:38 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
userResp, err := db.NewUser(context.Background(), test.newUserReq)
|
|
|
|
if test.expectErr && err == nil {
|
|
|
|
t.Fatalf("err expected, got nil")
|
|
|
|
}
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Fatalf("no error expected, got: %s", err)
|
|
|
|
}
|
|
|
|
require.Regexp(t, test.expectedUsernameRegex, userResp.Username)
|
2017-11-28 18:19:49 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
err = mysqlhelper.TestCredsExist(t, connURL, userResp.Username, test.newUserReq.Password)
|
|
|
|
require.NoError(t, err, "Failed to connect with credentials")
|
|
|
|
})
|
|
|
|
}
|
2017-08-11 01:28:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
func TestMySQL_NewUser_legacy(t *testing.T) {
|
|
|
|
displayName := "token"
|
|
|
|
roleName := "testrole"
|
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
type testCase struct {
|
2021-02-11 21:08:32 +00:00
|
|
|
usernameTemplate string
|
|
|
|
|
|
|
|
newUserReq dbplugin.NewUserRequest
|
|
|
|
|
|
|
|
expectedUsernameRegex string
|
|
|
|
expectErr bool
|
2020-02-03 20:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testCase{
|
2021-02-11 21:08:32 +00:00
|
|
|
"name statements": {
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
GRANT SELECT ON *.* TO '{{name}}'@'%';`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: "09g8hanbdfkVSM",
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
2020-02-03 20:57:28 +00:00
|
|
|
},
|
2021-02-11 21:08:32 +00:00
|
|
|
|
|
|
|
expectedUsernameRegex: `^v-test-[a-zA-Z0-9]{9}$`,
|
|
|
|
expectErr: false,
|
2020-02-03 20:57:28 +00:00
|
|
|
},
|
2021-02-11 21:08:32 +00:00
|
|
|
"username statements": {
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
GRANT SELECT ON *.* TO '{{username}}'@'%';`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: "09g8hanbdfkVSM",
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
2020-02-03 20:57:28 +00:00
|
|
|
},
|
2021-02-11 21:08:32 +00:00
|
|
|
|
|
|
|
expectedUsernameRegex: `^v-test-[a-zA-Z0-9]{9}$`,
|
|
|
|
expectErr: false,
|
2020-02-03 20:57:28 +00:00
|
|
|
},
|
2021-02-11 21:08:32 +00:00
|
|
|
"prepared name statements": {
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
set @grants=CONCAT("GRANT SELECT ON ", "*", ".* TO '{{name}}'@'%'");
|
|
|
|
PREPARE grantStmt from @grants;
|
|
|
|
EXECUTE grantStmt;
|
|
|
|
DEALLOCATE PREPARE grantStmt;`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: "09g8hanbdfkVSM",
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
2020-02-03 20:57:28 +00:00
|
|
|
},
|
2021-02-11 21:08:32 +00:00
|
|
|
|
|
|
|
expectedUsernameRegex: `^v-test-[a-zA-Z0-9]{9}$`,
|
|
|
|
expectErr: false,
|
2020-02-03 20:57:28 +00:00
|
|
|
},
|
2021-02-11 21:08:32 +00:00
|
|
|
"prepared username statements": {
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
set @grants=CONCAT("GRANT SELECT ON ", "*", ".* TO '{{username}}'@'%'");
|
|
|
|
PREPARE grantStmt from @grants;
|
|
|
|
EXECUTE grantStmt;
|
|
|
|
DEALLOCATE PREPARE grantStmt;`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: "09g8hanbdfkVSM",
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
2020-02-03 20:57:28 +00:00
|
|
|
},
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
expectedUsernameRegex: `^v-test-[a-zA-Z0-9]{9}$`,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
"custom username template": {
|
|
|
|
usernameTemplate: `{{printf "foo-%s-%s" (random 5) (.RoleName | uppercase) | truncate 16}}`,
|
2017-08-11 01:28:18 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
2020-10-15 19:20:12 +00:00
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
2021-02-11 21:08:32 +00:00
|
|
|
DisplayName: displayName,
|
|
|
|
RoleName: roleName,
|
2020-10-09 17:32:38 +00:00
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2021-02-11 21:08:32 +00:00
|
|
|
Commands: []string{
|
|
|
|
`CREATE USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
GRANT SELECT ON *.* TO '{{username}}'@'%';`,
|
|
|
|
},
|
2020-10-09 17:32:38 +00:00
|
|
|
},
|
2021-02-11 21:08:32 +00:00
|
|
|
Password: "09g8hanbdfkVSM",
|
2020-10-09 17:32:38 +00:00
|
|
|
Expiration: time.Now().Add(time.Minute),
|
2021-02-11 21:08:32 +00:00
|
|
|
},
|
2017-08-11 01:28:18 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
expectedUsernameRegex: `^foo-[a-zA-Z0-9]{5}-TESTRO$`,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
}
|
2017-08-11 01:28:18 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
cleanup, connURL := mysqlhelper.PrepareTestContainer(t, false, "secret")
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"username_template": test.usernameTemplate,
|
2020-02-03 20:57:28 +00:00
|
|
|
}
|
2017-08-11 01:28:18 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
initReq := dbplugin.InitializeRequest{
|
|
|
|
Config: connectionDetails,
|
|
|
|
VerifyConnection: true,
|
2020-02-03 20:57:28 +00:00
|
|
|
}
|
2017-08-11 01:28:18 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
db := newMySQL(DefaultLegacyUserNameTemplate)
|
|
|
|
defer db.Close()
|
|
|
|
_, err := db.Initialize(context.Background(), initReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
userResp, err := db.NewUser(context.Background(), test.newUserReq)
|
|
|
|
if test.expectErr && err == nil {
|
|
|
|
t.Fatalf("err expected, got nil")
|
2020-02-03 20:57:28 +00:00
|
|
|
}
|
2021-02-11 21:08:32 +00:00
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Fatalf("no error expected, got: %s", err)
|
|
|
|
}
|
|
|
|
require.Regexp(t, test.expectedUsernameRegex, userResp.Username)
|
|
|
|
|
|
|
|
err = mysqlhelper.TestCredsExist(t, connURL, userResp.Username, test.newUserReq.Password)
|
|
|
|
require.NoError(t, err, "Failed to connect with credentials")
|
2020-02-03 20:57:28 +00:00
|
|
|
})
|
2017-08-11 01:28:18 +00:00
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
func TestMySQL_RotateRootCredentials(t *testing.T) {
|
2020-02-03 20:57:28 +00:00
|
|
|
type testCase struct {
|
|
|
|
statements []string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testCase{
|
|
|
|
"empty statements": {
|
|
|
|
statements: nil,
|
|
|
|
},
|
|
|
|
"default username": {
|
|
|
|
statements: []string{defaultMySQLRotateCredentialsSQL},
|
|
|
|
},
|
|
|
|
"default name": {
|
2021-04-08 16:43:39 +00:00
|
|
|
statements: []string{
|
|
|
|
`
|
2020-02-03 20:57:28 +00:00
|
|
|
ALTER USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, connURL := mysqlhelper.PrepareTestContainer(t, false, "secret")
|
2020-02-03 20:57:28 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"username": "root",
|
|
|
|
"password": "secret",
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
// Give a timeout just in case the test decides to be problematic
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
2018-03-21 19:05:56 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
initReq := dbplugin.InitializeRequest{
|
2020-10-09 17:32:38 +00:00
|
|
|
Config: connectionDetails,
|
|
|
|
VerifyConnection: true,
|
|
|
|
}
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
db := newMySQL(DefaultUserNameTemplate)
|
|
|
|
defer db.Close()
|
2020-10-09 17:32:38 +00:00
|
|
|
_, err := db.Initialize(context.Background(), initReq)
|
2020-02-03 20:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
if !db.Initialized {
|
|
|
|
t.Fatal("Database should be initialized")
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
updateReq := dbplugin.UpdateUserRequest{
|
2020-10-09 17:32:38 +00:00
|
|
|
Username: "root",
|
2020-10-15 19:20:12 +00:00
|
|
|
Password: &dbplugin.ChangePassword{
|
2020-10-09 17:32:38 +00:00
|
|
|
NewPassword: "different_sercret",
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2020-10-09 17:32:38 +00:00
|
|
|
Commands: test.statements,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.UpdateUser(ctx, updateReq)
|
2020-02-03 20:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2020-10-09 17:32:38 +00:00
|
|
|
err = mysqlhelper.TestCredsExist(t, connURL, updateReq.Username, updateReq.Password.NewPassword)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify old password doesn't work
|
|
|
|
if err := mysqlhelper.TestCredsExist(t, connURL, updateReq.Username, "secret"); err == nil {
|
|
|
|
t.Fatalf("Should not be able to connect with initial credentials")
|
2020-02-03 20:57:28 +00:00
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
err = db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
})
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
func TestMySQL_DeleteUser(t *testing.T) {
|
2020-02-03 20:57:28 +00:00
|
|
|
type testCase struct {
|
|
|
|
revokeStmts []string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testCase{
|
|
|
|
"empty statements": {
|
|
|
|
revokeStmts: nil,
|
|
|
|
},
|
|
|
|
"default name": {
|
|
|
|
revokeStmts: []string{defaultMysqlRevocationStmts},
|
|
|
|
},
|
|
|
|
"default username": {
|
2021-04-08 16:43:39 +00:00
|
|
|
revokeStmts: []string{
|
|
|
|
`
|
2020-02-03 20:57:28 +00:00
|
|
|
REVOKE ALL PRIVILEGES, GRANT OPTION FROM '{{username}}'@'%';
|
|
|
|
DROP USER '{{username}}'@'%'`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shared test container for speed - there should not be any overlap between the tests
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, connURL := mysqlhelper.PrepareTestContainer(t, false, "secret")
|
2018-03-21 19:05:56 +00:00
|
|
|
defer cleanup()
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
initReq := dbplugin.InitializeRequest{
|
2020-10-09 17:32:38 +00:00
|
|
|
Config: connectionDetails,
|
|
|
|
VerifyConnection: true,
|
|
|
|
}
|
2017-06-06 13:49:49 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
db := newMySQL(DefaultUserNameTemplate)
|
|
|
|
defer db.Close()
|
2020-10-09 17:32:38 +00:00
|
|
|
_, err := db.Initialize(context.Background(), initReq)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-10-09 17:32:38 +00:00
|
|
|
password, err := credsutil.RandomAlphaNumeric(32, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate password: %s", err)
|
2020-02-03 20:57:28 +00:00
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
createReq := dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
2020-10-09 17:32:38 +00:00
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2021-04-08 16:43:39 +00:00
|
|
|
Commands: []string{
|
|
|
|
`
|
2020-10-09 17:32:38 +00:00
|
|
|
CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
GRANT SELECT ON *.* TO '{{name}}'@'%';`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: password,
|
|
|
|
Expiration: time.Now().Add(time.Minute),
|
2020-02-03 20:57:28 +00:00
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
// Give a timeout just in case the test decides to be problematic
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
userResp, err := db.NewUser(ctx, createReq)
|
2020-02-03 20:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
if err := mysqlhelper.TestCredsExist(t, connURL, userResp.Username, password); err != nil {
|
2020-02-03 20:57:28 +00:00
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
deleteReq := dbplugin.DeleteUserRequest{
|
2020-10-09 17:32:38 +00:00
|
|
|
Username: userResp.Username,
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2020-10-09 17:32:38 +00:00
|
|
|
Commands: test.revokeStmts,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = db.DeleteUser(context.Background(), deleteReq)
|
2020-02-03 20:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
if err := mysqlhelper.TestCredsExist(t, connURL, userResp.Username, password); err == nil {
|
|
|
|
t.Fatalf("Credentials were not revoked!")
|
2020-02-03 20:57:28 +00:00
|
|
|
}
|
|
|
|
})
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
func TestMySQL_UpdateUser(t *testing.T) {
|
2020-02-03 20:57:28 +00:00
|
|
|
type testCase struct {
|
|
|
|
rotateStmts []string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testCase{
|
|
|
|
"empty statements": {
|
|
|
|
rotateStmts: nil,
|
|
|
|
},
|
|
|
|
"custom statement name": {
|
|
|
|
rotateStmts: []string{`
|
|
|
|
ALTER USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';`},
|
|
|
|
},
|
|
|
|
"custom statement username": {
|
|
|
|
rotateStmts: []string{`
|
|
|
|
ALTER USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';`},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, connURL := mysqlhelper.PrepareTestContainer(t, false, "secret")
|
2020-02-03 20:57:28 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// create the database user and verify we can access
|
|
|
|
dbUser := "vaultstatictest"
|
|
|
|
initPassword := "password"
|
|
|
|
|
|
|
|
createStatements := `
|
|
|
|
CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
GRANT SELECT ON *.* TO '{{name}}'@'%';`
|
|
|
|
|
|
|
|
createTestMySQLUser(t, connURL, dbUser, initPassword, createStatements)
|
2020-10-09 17:32:38 +00:00
|
|
|
if err := mysqlhelper.TestCredsExist(t, connURL, dbUser, initPassword); err != nil {
|
2020-02-03 20:57:28 +00:00
|
|
|
t.Fatalf("Could not connect with credentials: %s", err)
|
|
|
|
}
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
initReq := dbplugin.InitializeRequest{
|
2020-10-09 17:32:38 +00:00
|
|
|
Config: connectionDetails,
|
|
|
|
VerifyConnection: true,
|
|
|
|
}
|
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
// Give a timeout just in case the test decides to be problematic
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
db := newMySQL(DefaultUserNameTemplate)
|
|
|
|
defer db.Close()
|
2020-10-09 17:32:38 +00:00
|
|
|
_, err := db.Initialize(context.Background(), initReq)
|
2020-02-03 20:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
newPassword, err := credsutil.RandomAlphaNumeric(32, false)
|
2020-02-03 20:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate password: %s", err)
|
|
|
|
}
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
updateReq := dbplugin.UpdateUserRequest{
|
2020-02-03 20:57:28 +00:00
|
|
|
Username: dbUser,
|
2020-10-15 19:20:12 +00:00
|
|
|
Password: &dbplugin.ChangePassword{
|
2020-10-09 17:32:38 +00:00
|
|
|
NewPassword: newPassword,
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2020-10-09 17:32:38 +00:00
|
|
|
Commands: test.rotateStmts,
|
|
|
|
},
|
|
|
|
},
|
2020-02-03 20:57:28 +00:00
|
|
|
}
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
_, err = db.UpdateUser(ctx, updateReq)
|
2020-02-03 20:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
// verify new password works
|
|
|
|
if err := mysqlhelper.TestCredsExist(t, connURL, dbUser, newPassword); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2020-02-03 20:57:28 +00:00
|
|
|
// verify old password doesn't work
|
|
|
|
if err := mysqlhelper.TestCredsExist(t, connURL, dbUser, initPassword); err == nil {
|
|
|
|
t.Fatalf("Should not be able to connect with initial credentials")
|
|
|
|
}
|
|
|
|
})
|
2020-01-07 15:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 18:52:56 +00:00
|
|
|
func createTestMySQLUser(t *testing.T, connURL, username, password, query string) {
|
|
|
|
t.Helper()
|
|
|
|
db, err := sql.Open("mysql", connURL)
|
|
|
|
defer db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
|
|
|
ctx := context.Background()
|
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// copied from mysql.go
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(query, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
query = dbutil.QueryHelper(query, map[string]string{
|
|
|
|
"name": username,
|
|
|
|
"password": password,
|
|
|
|
})
|
|
|
|
|
|
|
|
stmt, err := tx.PrepareContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*stdmysql.MySQLError); ok && e.Number == 1295 {
|
|
|
|
_, err = tx.ExecContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
stmt.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stmt.ExecContext(ctx); err != nil {
|
|
|
|
stmt.Close()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
stmt.Close()
|
|
|
|
}
|
|
|
|
}
|