mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 19:17:58 +00:00
Remove fmt strings and replace with inline queries (#13799)
* removed fmt strings and replaced with inline SQL | added unit tests * changelog++
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
||||
dbtesting "github.com/hashicorp/vault/sdk/database/dbplugin/v5/testing"
|
||||
"github.com/hashicorp/vault/sdk/helper/dbtxn"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestInitialize(t *testing.T) {
|
||||
@@ -262,7 +263,10 @@ func TestUpdateUser_password(t *testing.T) {
|
||||
dbtesting.AssertInitializeCircleCiTest(t, db, initReq)
|
||||
defer dbtesting.AssertClose(t, db)
|
||||
|
||||
createTestMSSQLUser(t, connURL, dbUser, initPassword, testMSSQLLogin)
|
||||
err := createTestMSSQLUser(connURL, dbUser, initPassword, testMSSQLLogin)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create user: %s", err)
|
||||
}
|
||||
|
||||
assertCredsExist(t, connURL, dbUser, initPassword)
|
||||
|
||||
@@ -326,7 +330,10 @@ func TestDeleteUser(t *testing.T) {
|
||||
dbtesting.AssertInitializeCircleCiTest(t, db, initReq)
|
||||
defer dbtesting.AssertClose(t, db)
|
||||
|
||||
createTestMSSQLUser(t, connURL, dbUser, initPassword, testMSSQLLogin)
|
||||
err := createTestMSSQLUser(connURL, dbUser, initPassword, testMSSQLLogin)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create user: %s", err)
|
||||
}
|
||||
|
||||
assertCredsExist(t, connURL, dbUser, initPassword)
|
||||
|
||||
@@ -350,6 +357,44 @@ func TestDeleteUser(t *testing.T) {
|
||||
assertCredsDoNotExist(t, connURL, dbUser, initPassword)
|
||||
}
|
||||
|
||||
func TestSQLSanitization(t *testing.T) {
|
||||
cleanup, connURL := mssqlhelper.PrepareMSSQLTestContainer(t)
|
||||
defer cleanup()
|
||||
|
||||
injectionString := "vaultuser]"
|
||||
dbUser := "vaultuser"
|
||||
initPassword := "p4$sw0rd"
|
||||
|
||||
initReq := dbplugin.InitializeRequest{
|
||||
Config: map[string]interface{}{
|
||||
"connection_url": connURL,
|
||||
},
|
||||
VerifyConnection: true,
|
||||
}
|
||||
|
||||
db := new()
|
||||
|
||||
dbtesting.AssertInitializeCircleCiTest(t, db, initReq)
|
||||
defer dbtesting.AssertClose(t, db)
|
||||
|
||||
err := createTestMSSQLUser(connURL, dbUser, initPassword, testMSSQLLogin)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create user: %s", err)
|
||||
}
|
||||
|
||||
assertCredsExist(t, connURL, dbUser, initPassword)
|
||||
|
||||
deleteReq := dbplugin.DeleteUserRequest{
|
||||
Username: injectionString,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
_, err = db.DeleteUser(ctx, deleteReq)
|
||||
|
||||
assert.EqualError(t, err, "mssql: Cannot alter the login 'vaultuser]', because it does not exist or you do not have permission.")
|
||||
}
|
||||
|
||||
func assertCredsExist(t testing.TB, connURL, username, password string) {
|
||||
t.Helper()
|
||||
err := testCredsExist(connURL, username, password)
|
||||
@@ -378,18 +423,18 @@ func testCredsExist(connURL, username, password string) error {
|
||||
return db.Ping()
|
||||
}
|
||||
|
||||
func createTestMSSQLUser(t *testing.T, connURL string, username, password, query string) {
|
||||
func createTestMSSQLUser(connURL string, username, password, query string) error {
|
||||
db, err := sql.Open("mssql", connURL)
|
||||
defer db.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Start a transaction
|
||||
ctx := context.Background()
|
||||
tx, err := db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = tx.Rollback()
|
||||
@@ -400,12 +445,13 @@ func createTestMSSQLUser(t *testing.T, connURL string, username, password, query
|
||||
"password": password,
|
||||
}
|
||||
if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
|
||||
t.Fatal(err)
|
||||
return err
|
||||
}
|
||||
// Commit the transaction
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatal(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const testMSSQLRole = `
|
||||
@@ -413,11 +459,6 @@ CREATE LOGIN [{{name}}] WITH PASSWORD = '{{password}}';
|
||||
CREATE USER [{{name}}] FOR LOGIN [{{name}}];
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA::dbo TO [{{name}}];`
|
||||
|
||||
const testMSSQLDrop = `
|
||||
DROP USER [{{name}}];
|
||||
DROP LOGIN [{{name}}];
|
||||
`
|
||||
|
||||
const testMSSQLLogin = `
|
||||
CREATE LOGIN [{{name}}] WITH PASSWORD = '{{password}}';
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user