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
|
|
|
func TestMSSQL_Initialize(t *testing.T) {
|
2019-02-05 22:11:06 +00:00
|
|
|
if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" {
|
2019-02-13 14:28:28 +00:00
|
|
|
t.SkipNow()
|
2019-02-05 22:11:06 +00:00
|
|
|
}
|
|
|
|
connURL := os.Getenv("MSSQL_URL")
|
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 {
|
2019-03-19 13:32:45 +00:00
|
|
|
t.Fatal("Database should be initialized")
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02-05 22:11:06 +00:00
|
|
|
if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" {
|
2019-02-13 14:28:28 +00:00
|
|
|
t.SkipNow()
|
2019-02-05 22:11:06 +00:00
|
|
|
}
|
|
|
|
connURL := os.Getenv("MSSQL_URL")
|
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-02-05 22:11:06 +00:00
|
|
|
if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" {
|
2019-02-13 14:28:28 +00:00
|
|
|
t.SkipNow()
|
2019-02-05 22:11:06 +00:00
|
|
|
}
|
|
|
|
connURL := os.Getenv("MSSQL_URL")
|
2019-01-24 12:24:32 +00:00
|
|
|
|
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 {
|
2019-03-19 13:32:45 +00:00
|
|
|
t.Fatal("Database should be initialized")
|
2018-06-12 16:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02-05 22:11:06 +00:00
|
|
|
if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" {
|
2019-02-13 14:28:28 +00:00
|
|
|
t.SkipNow()
|
2019-02-05 22:11:06 +00:00
|
|
|
}
|
|
|
|
connURL := os.Getenv("MSSQL_URL")
|
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}}];
|
|
|
|
`
|