Skip to content

Commit

Permalink
EVEREST-1680 ConnectionURL fix
Browse files Browse the repository at this point in the history
  • Loading branch information
oksana-grishchenko committed Nov 20, 2024
1 parent f1f1971 commit b5fb1ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions api/database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,24 +411,29 @@ func (e *EverestServer) connectionURL(ctx context.Context, db *everestv1alpha1.D
if db.Status.Hostname == "" {
return nil
}
url := url.URL{User: url.UserPassword(user, url.QueryEscape(password))}
var url string
defaultHost := net.JoinHostPort(db.Status.Hostname, fmt.Sprint(db.Status.Port))
switch db.Spec.Engine.Type {
case everestv1alpha1.DatabaseEnginePXC:
url.Scheme = "jdbc:mysql"
url.Host = net.JoinHostPort(db.Status.Hostname, fmt.Sprint(db.Status.Port))
url = queryEscapedUrl("jdbc:mysql", user, password, defaultHost)
case everestv1alpha1.DatabaseEnginePSMDB:
hosts, err := psmdbHosts(ctx, db, e.kubeClient.GetPods)
if err != nil {
e.l.Error(err)
return nil
}
url.Scheme = "mongodb"
url.Host = hosts
url = queryEscapedUrl("mongodb", user, password, hosts)
case everestv1alpha1.DatabaseEnginePostgresql:
url.Scheme = "postgres"
url.Host = net.JoinHostPort(db.Status.Hostname, fmt.Sprint(db.Status.Port))
url = queryEscapedUrl("postgres", user, password, defaultHost)
}
return pointer.ToString(url.String())
return pointer.ToString(url)
}

// Using own format instead of url.URL bc it uses the password encoding policy which does not encode char like ','
// however such char may appear in the db passwords

Check failure on line 433 in api/database_cluster.go

View workflow job for this annotation

GitHub Actions / Check (1.23.x, false)

Comment should end in a period (godot)
func queryEscapedUrl(scheme, user, password, hosts string) string {

Check failure on line 434 in api/database_cluster.go

View workflow job for this annotation

GitHub Actions / Check (1.23.x, false)

var-naming: func queryEscapedUrl should be queryEscapedURL (revive)
format := "%s://%s:%s@%s"
return fmt.Sprintf(format, scheme, user, url.QueryEscape(password), hosts)
}

func psmdbHosts(
Expand Down
4 changes: 2 additions & 2 deletions api/database_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestConnectionURL(t *testing.T) {
},
user: "postgres",
password: "55aBDedMF;So|C?^3x|h.dDC",
expected: "postgres://postgres:55aBDedMF%253BSo%257CC%253F%255E3x%257Ch[email protected]:5432",
expected: "postgres://postgres:55aBDedMF%3BSo%7CC%3F%5E3x%7Ch[email protected]:5432",
},
{
name: "pxc",
Expand All @@ -234,7 +234,7 @@ func TestConnectionURL(t *testing.T) {
},
user: "root",
password: ",0#3PdCIc=9CS(do2",
expected: "jdbc:mysql://root:%252C0%25233PdCIc%253D9CS%2528do2@mysql-29o-haproxy.everest:3306",
expected: "jdbc:mysql://root:%2C0%233PdCIc%3D9CS%28do2@mysql-29o-haproxy.everest:3306",
},
}

Expand Down

0 comments on commit b5fb1ca

Please sign in to comment.