Skip to content

Commit

Permalink
Merge pull request #75 from TykTechnologies/TT-8971
Browse files Browse the repository at this point in the history
[TT-8971] Fixing connection strings with +srv bug
  • Loading branch information
mativm02 authored May 19, 2023
2 parents c52b899 + 8e0dbe1 commit 5116e3a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
10 changes: 6 additions & 4 deletions persistent/internal/helper/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func IsCosmosDB(connectionString string) bool {
// ParsePassword parses the password from the connection string and URL encodes it.
// It's useful when the password contains special characters.
// Example: mongodb://user:p@ssword@localhost:27017/db -> mongodb://user:p%40word@40localhost:27017/db
// If there's any conflict, the function returns the original connection string.
func ParsePassword(connectionString string) string {
// Find the last '@' (the delimiter between credentials and host)
at := strings.LastIndex(connectionString, "@")
Expand All @@ -46,22 +47,23 @@ func ParsePassword(connectionString string) string {
return connectionString
}

scheme := credentialsAndSchemeParts[0] // here we extract the scheme
credentials := credentialsAndSchemeParts[1]

// Split the username and password
credentialsParts := strings.SplitN(credentials, ":", 2)
if len(credentialsParts) != 2 {
credentialsParts := strings.Split(credentials, ":")
if len(credentialsParts) < 2 {
return connectionString
}

username := credentialsParts[0]
password := credentialsParts[1]
password := strings.Join(credentialsParts[1:], ":")

// URL encode the password
encodedPassword := url.QueryEscape(password)

// Construct the new connection string
newConnectionString := fmt.Sprintf("mongodb://%s:%s@%s", username, encodedPassword, hostAndDB)
newConnectionString := fmt.Sprintf("%s://%s:%s@%s", scheme, username, encodedPassword, hostAndDB)

return newConnectionString
}
30 changes: 30 additions & 0 deletions persistent/internal/helper/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,36 @@ func TestParsePassword(t *testing.T) {
originalConnString: "mongodb://user:p@ss:/?#[]wor/d@localhost:27017,localhost:27018",
expectedConnString: "mongodb://user:p%40ss%3A%2F%3F%23%5B%5Dwor%2Fd@localhost:27017,localhost:27018",
},
{
name: "srv connection string",
originalConnString: "mongodb+srv://tyk:[email protected]/tyk?w=majority",
expectedConnString: "mongodb+srv://tyk:[email protected]/tyk?w=majority",
},
{
name: "srv connection string with special characters",
originalConnString: "mongodb+srv://tyk:p@[email protected]/tyk?w=majority",
expectedConnString: "mongodb+srv://tyk:p%[email protected]/tyk?w=majority",
},
{
name: "connection string without username",
originalConnString: "mongodb://:password@localhost:27017/test",
expectedConnString: "mongodb://:password@localhost:27017/test",
},
{
name: "connection string without password",
originalConnString: "mongodb://user:@localhost:27017/test",
expectedConnString: "mongodb://user:@localhost:27017/test",
},
{
name: "connection string without host",
originalConnString: "mongodb://user:password@/test",
expectedConnString: "mongodb://user:password@/test",
},
{
name: "connection string without database",
originalConnString: "mongodb://user:password@localhost:27017",
expectedConnString: "mongodb://user:password@localhost:27017",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 5116e3a

Please sign in to comment.