From 99449583e1aa0e4d25ae9009488eccc09bd9876a Mon Sep 17 00:00:00 2001 From: Soren Mathiasen Date: Wed, 14 Feb 2024 15:57:09 +0100 Subject: [PATCH] fix: adding ssl as an option --- mongo.go | 11 +++++++++-- mongo_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/mongo.go b/mongo.go index a5e2571..d4106c9 100644 --- a/mongo.go +++ b/mongo.go @@ -34,6 +34,7 @@ func NewMongoDB(config providers.ConfigProvider, resourceName string) (*MongoDB, return nil, err } + log.Println("Checking connection to mongodb database") err = client.Ping(ctx, nil) if err != nil { return nil, err @@ -51,10 +52,16 @@ func createConnectionString(config providers.ConfigProvider, resourceName string protocol := getProtocol(resInfo) dbName := getDBName(resInfo, resourceName) + url := "" if protocol == "mongodb+srv" { - return fmt.Sprintf("%s://%s:%s@%s/%s", protocol, resInfo.Credentials["username"], resInfo.Credentials["password"], resInfo.Host, dbName) + "?authSource=admin", nil + url = fmt.Sprintf("%s://%s:%s@%s/%s", protocol, resInfo.Credentials["username"], resInfo.Credentials["password"], resInfo.Host, dbName) + "?authSource=admin" + } else { + url = fmt.Sprintf("%s://%s:%s@%s:%s/%s", protocol, resInfo.Credentials["username"], resInfo.Credentials["password"], resInfo.Host, resInfo.Port, dbName) + "?authSource=admin&directConnection=true" } - return fmt.Sprintf("%s://%s:%s@%s:%s/%s", protocol, resInfo.Credentials["username"], resInfo.Credentials["password"], resInfo.Host, resInfo.Port, dbName) + "?authSource=admin&directConnection=true", nil + if resInfo.Options["ssl"] != nil { + url += fmt.Sprintf("&ssl=%s", resInfo.Options["ssl"]) + } + return url, nil } func getProtocol(resInfo *providers.ResourceInfo) string { diff --git a/mongo_test.go b/mongo_test.go index 9576c23..fef750b 100644 --- a/mongo_test.go +++ b/mongo_test.go @@ -97,6 +97,46 @@ func TestCreateConnectionStringProtocolMongodb(t *testing.T) { assert.NoError(t, err) assert.Equal(t, urlWithPort, actual) }) + + t.Run("verify that mongodb connection string contains ssl true", func(t *testing.T) { + resInfo := &providers.ResourceInfo{ + Host: "localhost", + Port: "27017", + Credentials: map[string]string{"username": "user", "password": "password"}, + Options: map[string]interface{}{"ssl": "true"}, + } + dbName := "test" + urlWithPort := "mongodb://user:password@localhost:27017/test?authSource=admin&directConnection=true&ssl=true" + + config := &ConfigProviderMock{ + GetResourceInfoFunc: func(resourceType, resourcePort, resourceName string) (*providers.ResourceInfo, error) { + return resInfo, nil + }, + } + actual, err := createConnectionString(config, dbName) + assert.NoError(t, err) + assert.Equal(t, urlWithPort, actual) + }) + + t.Run("verify that mongodb connection string contains ssl false", func(t *testing.T) { + resInfo := &providers.ResourceInfo{ + Host: "localhost", + Port: "27017", + Credentials: map[string]string{"username": "user", "password": "password"}, + Options: map[string]interface{}{"ssl": "false"}, + } + dbName := "test" + urlWithPort := "mongodb://user:password@localhost:27017/test?authSource=admin&directConnection=true&ssl=false" + + config := &ConfigProviderMock{ + GetResourceInfoFunc: func(resourceType, resourcePort, resourceName string) (*providers.ResourceInfo, error) { + return resInfo, nil + }, + } + actual, err := createConnectionString(config, dbName) + assert.NoError(t, err) + assert.Equal(t, urlWithPort, actual) + }) } func TestCreateConnectionString(t *testing.T) {