Skip to content

Commit

Permalink
Merge pull request #5 from kapetacom/ssl
Browse files Browse the repository at this point in the history
fix: adding ssl as an option
  • Loading branch information
sorenmat authored Feb 15, 2024
2 parents 9bb2dc5 + 9944958 commit 30a5a69
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
11 changes: 9 additions & 2 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
40 changes: 40 additions & 0 deletions mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 30a5a69

Please sign in to comment.