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"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testMSQLImagePull sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMSSQL_Initialize(t *testing.T) {
|
2017-04-13 21:40:59 +00:00
|
|
|
if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" {
|
2017-04-13 21:30:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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 {
|
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) {
|
2017-04-13 21:40:59 +00:00
|
|
|
if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" {
|
2017-04-13 21:30:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMSSQL_RevokeUser(t *testing.T) {
|
2017-04-13 21:40:59 +00:00
|
|
|
if os.Getenv("MSSQL_URL") == "" || os.Getenv("VAULT_ACC") != "1" {
|
2017-04-13 21:30:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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}}];
|
|
|
|
`
|