Skip to content

Commit 3dd5122

Browse files
sechmannsindrerh2mortenlj
committed
feat: support updating passwords regardless of url format
Co-authored-by: Sindre Rødseth Hansen <[email protected]> Co-authored-by: Morten Lied Johansen <[email protected]>
1 parent a58af75 commit 3dd5122

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

pkg/postgres/access.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func sqlExecAsAppUser(ctx context.Context, appName, namespace, cluster, database
4646
return err
4747
}
4848

49-
db, err := sql.Open("cloudsqlpostgres", connectionInfo.ConnectionString())
49+
db, err := sql.Open("cloudsqlpostgres", connectionInfo.ProxyConnectionString())
5050
if err != nil {
5151
return err
5252
}

pkg/postgres/dbinfo.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"net/url"
78
"strings"
89

910
naisv1 "github.com/nais/liberator/pkg/apis/nais.io/v1"
@@ -143,12 +144,18 @@ func (i *DBInfo) dbConnectionMultiDB(ctx context.Context) (*ConnectionInfo, erro
143144
}
144145

145146
func createConnectionInfo(secret corev1.Secret, instance string) *ConnectionInfo {
147+
u, err := url.Parse(getSecretDataValue(secret, "_URL"))
148+
if err != nil {
149+
panic(err)
150+
}
151+
146152
return &ConnectionInfo{
147153
username: getSecretDataValue(secret, "_USERNAME"),
148154
password: getSecretDataValue(secret, "_PASSWORD"),
149155
dbName: getSecretDataValue(secret, "_DATABASE"),
150156
port: getSecretDataValue(secret, "_PORT"),
151157
host: getSecretDataValue(secret, "_HOST"),
158+
url: u,
152159
instance: instance,
153160
}
154161
}
@@ -271,18 +278,20 @@ type ConnectionInfo struct {
271278
instance string
272279
port string
273280
host string
281+
url *url.URL
274282
}
275283

276-
func (c *ConnectionInfo) ConnectionString() string {
284+
func (c *ConnectionInfo) ProxyConnectionString() string {
277285
return fmt.Sprintf("host=%v user=%v dbname=%v password=%v sslmode=disable", c.instance, c.username, c.dbName, c.password)
278286
}
279287

280288
func (c *ConnectionInfo) JDBCURL() string {
281-
return fmt.Sprintf("postgres://%v:%v@%v:%v/%v", c.username, c.password, c.host, c.port, c.dbName)
289+
return c.url.String()
282290
}
283291

284292
func (c *ConnectionInfo) SetPassword(password string) {
285293
c.password = password
294+
c.url.User = url.UserPassword(c.username, password)
286295
}
287296

288297
func getSecretDataValue(secret corev1.Secret, suffix string) string {

pkg/postgres/iam.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func ListUsers(ctx context.Context, appName, cluster, namespace, database string
211211
return err
212212
}
213213

214-
db, err := sql.Open("cloudsqlpostgres", connectionInfo.ConnectionString())
214+
db, err := sql.Open("cloudsqlpostgres", connectionInfo.ProxyConnectionString())
215215
if err != nil {
216216
return err
217217
}
@@ -253,7 +253,7 @@ func AddUser(ctx context.Context, appName, username, password, cluster, namespac
253253
return err
254254
}
255255

256-
db, err := sql.Open("cloudsqlpostgres", connectionInfo.ConnectionString())
256+
db, err := sql.Open("cloudsqlpostgres", connectionInfo.ProxyConnectionString())
257257
if err != nil {
258258
return err
259259
}

0 commit comments

Comments
 (0)