From 8c61b47b2c9eeee04878cb70e1675b288466e11c Mon Sep 17 00:00:00 2001 From: mariusheine Date: Mon, 11 Dec 2023 15:45:42 +0000 Subject: [PATCH 1/4] check for couchdb existence when checking for user --- adapters/couchdb_adapter.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/adapters/couchdb_adapter.go b/adapters/couchdb_adapter.go index 3bc585a..5b23d9d 100644 --- a/adapters/couchdb_adapter.go +++ b/adapters/couchdb_adapter.go @@ -29,6 +29,14 @@ func (adapter couchdbAdapter) DeleteDatabase(ctx context.Context, database strin } func (adapter couchdbAdapter) HasDatabaseUserWithAccess(ctx context.Context, database string, username string) (bool, error) { + dbExists, dbExistsErr := adapter.HasDatabase(ctx, database) + if dbExistsErr != nil { + return false, dbExistsErr + } + if !dbExists { + return false, nil + } + sc, err := adapter.db.DB(database).Security(ctx) if err != nil { return false, err From 2192c6bb4c4b794e109301d6f2b6603a87bbdd50 Mon Sep 17 00:00:00 2001 From: mariusheine Date: Mon, 11 Dec 2023 15:49:04 +0000 Subject: [PATCH 2/4] add according testcase for adapters --- adapters/couchdb_adapter_test.go | 14 +++++++++++++- adapters/mongo_adapter_test.go | 14 +++++++++++++- adapters/mssql_adapter_test.go | 14 +++++++++++++- adapters/mysql_adapter_test.go | 14 +++++++++++++- adapters/postgres_adapter_test.go | 14 +++++++++++++- adapters/utils_test.go | 10 ++++++++++ 6 files changed, 75 insertions(+), 5 deletions(-) diff --git a/adapters/couchdb_adapter_test.go b/adapters/couchdb_adapter_test.go index e5d1c24..b2a7eba 100644 --- a/adapters/couchdb_adapter_test.go +++ b/adapters/couchdb_adapter_test.go @@ -9,7 +9,7 @@ import ( "github.com/go-kivik/kivik/v4" ) -func TestCouchDB(t *testing.T) { +func prepareCouchDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) { databaseHost := "localhost" databasePort := "5984" @@ -32,5 +32,17 @@ func TestCouchDB(t *testing.T) { return err } + return ctx, adapter, clientConnectTest +} + +func TestCouchDB(t *testing.T) { + ctx, adapter, clientConnectTest := prepareCouchDB(t) + testHelper(t, ctx, adapter, clientConnectTest) } + +func TestCouchDBCleanup(t *testing.T) { + ctx, adapter, _ := prepareCouchDB(t) + + cleanupTestHelper(t, ctx, adapter) +} diff --git a/adapters/mongo_adapter_test.go b/adapters/mongo_adapter_test.go index 83356dd..ad02561 100644 --- a/adapters/mongo_adapter_test.go +++ b/adapters/mongo_adapter_test.go @@ -10,7 +10,7 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -func TestMongoDB(t *testing.T) { +func prepareMongoDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) { databaseHost := "localhost" databasePort := "27017" @@ -34,5 +34,17 @@ func TestMongoDB(t *testing.T) { return err } + return ctx, adapter, clientConnectTest +} + +func TestMongoDB(t *testing.T) { + ctx, adapter, clientConnectTest := prepareMongoDB(t) + testHelper(t, ctx, adapter, clientConnectTest) } + +func TestMongoDBCleanup(t *testing.T) { + ctx, adapter, _ := prepareMongoDB(t) + + cleanupTestHelper(t, ctx, adapter) +} diff --git a/adapters/mssql_adapter_test.go b/adapters/mssql_adapter_test.go index 270fc07..5027770 100644 --- a/adapters/mssql_adapter_test.go +++ b/adapters/mssql_adapter_test.go @@ -9,7 +9,7 @@ import ( "github.com/anbraten/k8s-external-database-operator/adapters" ) -func TestMsSqlDB(t *testing.T) { +func prepareMsSqlDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) { databaseHost := "localhost" databasePort := "1433" @@ -32,5 +32,17 @@ func TestMsSqlDB(t *testing.T) { return err } + return ctx, adapter, clientConnectTest +} + +func TestMsSqlDB(t *testing.T) { + ctx, adapter, clientConnectTest := prepareMsSqlDB(t) + testHelper(t, ctx, adapter, clientConnectTest) } + +func TestMsSqlDBCleanup(t *testing.T) { + ctx, adapter, _ := prepareMsSqlDB(t) + + cleanupTestHelper(t, ctx, adapter) +} diff --git a/adapters/mysql_adapter_test.go b/adapters/mysql_adapter_test.go index e3b8c42..acb4ece 100644 --- a/adapters/mysql_adapter_test.go +++ b/adapters/mysql_adapter_test.go @@ -9,7 +9,7 @@ import ( "github.com/anbraten/k8s-external-database-operator/adapters" ) -func TestMySqlDB(t *testing.T) { +func prepareMySqlDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) { databaseHost := "localhost" databasePort := "3306" @@ -32,5 +32,17 @@ func TestMySqlDB(t *testing.T) { return err } + return ctx, adapter, clientConnectTest +} + +func TestMySqlDB(t *testing.T) { + ctx, adapter, clientConnectTest := prepareMySqlDB(t) + testHelper(t, ctx, adapter, clientConnectTest) } + +func TestMySqlDBCleanup(t *testing.T) { + ctx, adapter, _ := prepareMySqlDB(t) + + cleanupTestHelper(t, ctx, adapter) +} diff --git a/adapters/postgres_adapter_test.go b/adapters/postgres_adapter_test.go index 6022cab..85c83ce 100644 --- a/adapters/postgres_adapter_test.go +++ b/adapters/postgres_adapter_test.go @@ -9,7 +9,7 @@ import ( "github.com/jackc/pgx/v4" ) -func TestPostgresDB(t *testing.T) { +func preparePostgresDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) { databaseHost := "localhost" databasePort := "5432" @@ -32,5 +32,17 @@ func TestPostgresDB(t *testing.T) { return err } + return ctx, adapter, clientConnectTest +} + +func TestPostgresDB(t *testing.T) { + ctx, adapter, clientConnectTest := preparePostgresDB(t) + testHelper(t, ctx, adapter, clientConnectTest) } + +func TestPostgresDBCleanup(t *testing.T) { + ctx, adapter, _ := preparePostgresDB(t) + + cleanupTestHelper(t, ctx, adapter) +} diff --git a/adapters/utils_test.go b/adapters/utils_test.go index b068df2..d22f315 100644 --- a/adapters/utils_test.go +++ b/adapters/utils_test.go @@ -60,3 +60,13 @@ func testHelper(t *testing.T, ctx context.Context, adapter adapters.DatabaseAdap t.Fatalf("Database user does not exists") } } + +func cleanupTestHelper(t *testing.T, ctx context.Context, adapter adapters.DatabaseAdapter) { + result, err := adapter.HasDatabaseUserWithAccess(ctx, "non-existing-db", "non-existing-user") + if err != nil { + t.Fatalf("Checking for existing database user failed: %s", err) + } + if result { + t.Fatalf("database and user existing but expecting to be non-existing") + } +} From de7de1044e97af513d69a24d37d25322fdfe5ba9 Mon Sep 17 00:00:00 2001 From: mariusheine Date: Mon, 11 Dec 2023 15:49:10 +0000 Subject: [PATCH 3/4] fix mssql adapter --- adapters/mssql_adapter.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/adapters/mssql_adapter.go b/adapters/mssql_adapter.go index 8260b8a..de96c25 100644 --- a/adapters/mssql_adapter.go +++ b/adapters/mssql_adapter.go @@ -41,6 +41,14 @@ func (adapter mssqlAdapter) DeleteDatabase(ctx context.Context, database string) } func (adapter mssqlAdapter) HasDatabaseUserWithAccess(ctx context.Context, database string, username string) (bool, error) { + dbExists, dbExistsErr := adapter.HasDatabase(ctx, database) + if dbExistsErr != nil { + return false, dbExistsErr + } + if !dbExists { + return false, nil + } + var count int query := fmt.Sprintf("USE [%s]; SELECT COUNT(*) FROM sys.database_principals WHERE authentication_type=2 AND name='%s';", database, username) err := adapter.db.QueryRowContext(ctx, query).Scan(&count) From 1d9b3918ee554419a444338b49fa1698a5e19467 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Tue, 12 Dec 2023 10:42:40 +0100 Subject: [PATCH 4/4] Apply suggestions from code review --- adapters/utils_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adapters/utils_test.go b/adapters/utils_test.go index d22f315..3c9d923 100644 --- a/adapters/utils_test.go +++ b/adapters/utils_test.go @@ -69,4 +69,7 @@ func cleanupTestHelper(t *testing.T, ctx context.Context, adapter adapters.Datab if result { t.Fatalf("database and user existing but expecting to be non-existing") } + + // TODO: test db.DeleteDatabaseUser + // TODO: test db.DeleteDatabase }