2017-04-13 20:48:32 +00:00
|
|
|
package mssql
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2017-04-13 20:48:32 +00:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
|
2019-01-24 12:24:32 +00:00
|
|
|
"github.com/ory/dockertest"
|
2017-04-13 20:48:32 +00:00
|
|
|
)
|
|
|
|
|
2019-01-24 12:24:32 +00:00
|
|
|
func prepareMSSQLTestContainer(t *testing.T) (cleanup func(), retURL string) {
|
|
|
|
if os.Getenv("MSSQL_URL") != "" {
|
|
|
|
return func() {}, os.Getenv("MSSQL_URL")
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2019-01-24 12:24:32 +00:00
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ro := &dockertest.RunOptions{
|
|
|
|
Repository: "mcr.microsoft.com/mssql/server",
|
|
|
|
Tag: "latest",
|
|
|
|
Env: []string{"ACCEPT_EULA=Y", "SA_PASSWORD=pa$$w0rd!"},
|
|
|
|
}
|
|
|
|
resource, err := pool.RunWithOptions(ro)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start local mssql docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() {
|
|
|
|
err := pool.Purge(resource)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to cleanup local container: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retURL = fmt.Sprintf("sqlserver://SA:pa$$w0rd!@localhost:%s", resource.GetPort("1433/tcp"))
|
|
|
|
|
|
|
|
// exponential backoff-retry
|
|
|
|
if retryErr := pool.Retry(func() error {
|
|
|
|
db, err := sql.Open("sqlserver", retURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return db.Ping()
|
|
|
|
|
|
|
|
}); retryErr != nil {
|
|
|
|
cleanup()
|
|
|
|
t.Fatalf("Could not connect to mssql docker container: %s", err)
|
2017-04-13 21:30:15 +00:00
|
|
|
}
|
2019-01-24 12:24:32 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMSSQL_Initialize(t *testing.T) {
|
|
|
|
cleanup, connURL := prepareMSSQLTestContainer(t)
|
|
|
|
defer cleanup()
|
2017-04-13 20:48:32 +00:00
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
if !db.Initialized {
|
2017-04-13 20:48:32 +00:00
|
|
|
t.Fatal("Database should be initalized")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-06-15 01:59:27 +00:00
|
|
|
|
|
|
|
// Test decoding a string value for max_open_connections
|
|
|
|
connectionDetails = map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"max_open_connections": "5",
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
_, err = db.Init(context.Background(), connectionDetails, true)
|
2017-06-15 01:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMSSQL_CreateUser(t *testing.T) {
|
2019-01-24 12:24:32 +00:00
|
|
|
cleanup, connURL := prepareMSSQLTestContainer(t)
|
|
|
|
defer cleanup()
|
2017-04-13 20:48:32 +00:00
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Test with no configured Creation Statement
|
2017-12-14 22:03:11 +00:00
|
|
|
_, _, err = db.CreateUser(context.Background(), dbplugin.Statements{}, usernameConfig, time.Now().Add(time.Minute))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error when no creation statement is provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testMSSQLRole},
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 16:11:48 +00:00
|
|
|
func TestMSSQL_RotateRootCredentials(t *testing.T) {
|
2019-01-24 12:24:32 +00:00
|
|
|
cleanup, connURL := prepareMSSQLTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-06-12 16:11:48 +00:00
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"username": "sa",
|
|
|
|
"password": "yourStrong(!)Password",
|
|
|
|
}
|
|
|
|
|
|
|
|
db := new()
|
|
|
|
|
|
|
|
connProducer := db.SQLConnectionProducer
|
|
|
|
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !connProducer.Initialized {
|
|
|
|
t.Fatal("Database should be initalized")
|
|
|
|
}
|
|
|
|
|
|
|
|
newConf, err := db.RotateRootCredentials(context.Background(), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if newConf["password"] == "yourStrong(!)Password" {
|
|
|
|
t.Fatal("password was not updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
func TestMSSQL_RevokeUser(t *testing.T) {
|
2019-01-24 12:24:32 +00:00
|
|
|
cleanup, connURL := prepareMSSQLTestContainer(t)
|
|
|
|
defer cleanup()
|
2017-04-13 20:48:32 +00:00
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testMSSQLRole},
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(2*time.Second))
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Test default revoke statements
|
2017-12-14 22:03:11 +00:00
|
|
|
err = db.RevokeUser(context.Background(), statements, username)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err == nil {
|
|
|
|
t.Fatal("Credentials were not revoked")
|
|
|
|
}
|
2017-05-01 22:43:21 +00:00
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err = db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(2*time.Second))
|
2017-05-01 22:43:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Test custom revoke statement
|
2018-03-21 19:05:56 +00:00
|
|
|
statements.Revocation = []string{testMSSQLDrop}
|
2017-12-14 22:03:11 +00:00
|
|
|
err = db.RevokeUser(context.Background(), statements, username)
|
2017-05-01 22:43:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err == nil {
|
|
|
|
t.Fatal("Credentials were not revoked")
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCredsExist(t testing.TB, connURL, username, password string) error {
|
|
|
|
// Log in with the new creds
|
2017-04-13 21:30:15 +00:00
|
|
|
parts := strings.Split(connURL, "@")
|
|
|
|
connURL = fmt.Sprintf("sqlserver://%s:%s@%s", username, password, parts[1])
|
2017-04-13 20:48:32 +00:00
|
|
|
db, err := sql.Open("mssql", connURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
return db.Ping()
|
|
|
|
}
|
|
|
|
|
|
|
|
const testMSSQLRole = `
|
|
|
|
CREATE LOGIN [{{name}}] WITH PASSWORD = '{{password}}';
|
|
|
|
CREATE USER [{{name}}] FOR LOGIN [{{name}}];
|
|
|
|
GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA::dbo TO [{{name}}];`
|
2017-05-01 22:43:21 +00:00
|
|
|
|
|
|
|
const testMSSQLDrop = `
|
|
|
|
DROP USER [{{name}}];
|
|
|
|
DROP LOGIN [{{name}}];
|
|
|
|
`
|