Skip to content

Commit

Permalink
Properly encode user/pass in DSN (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshalsall authored Feb 21, 2022
1 parent c40c3f6 commit a35cf1e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"net/url"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -91,13 +92,13 @@ func (cfg *Config) FindTopicKey(topicName string) TopicKey {
return topic.Key
}

func (cfg *Config) GetDBConnectionString() string {
func (cfg *Config) DSN() string {
sslMode := "disable"
if cfg.TLSEnable {
sslMode = "verify-full"
}

return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s", cfg.DB.User, cfg.DB.Pass, cfg.DB.Host, cfg.DB.Port, cfg.DB.Schema, sslMode)
return fmt.Sprintf("postgres://%s@%s:%d/%s?sslmode=%s", url.UserPassword(cfg.DB.User, cfg.DB.Pass), cfg.DB.Host, cfg.DB.Port, cfg.DB.Schema, sslMode)
}

// MainTopics will return a slice containing the main topic names from
Expand Down
18 changes: 16 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,26 @@ func TestConfig_GetDBConnectionString(t *testing.T) {
},
want: "postgres://root:pass123@postgres-db:5002/data?sslmode=verify-full",
},
{
name: "with password that should be encoded",
cfg: Config{
DB: Database{
Host: "postgres-db",
Port: 5002,
Schema: "data",
User: "root",
Pass: "pass%123",
},
TLSEnable: true,
},
want: "postgres://root:pass%25123@postgres-db:5002/data?sslmode=verify-full",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.cfg.GetDBConnectionString(); got != tt.want {
t.Errorf("GetDBConnectionString(): %s, want %s", got, tt.want)
if got := tt.cfg.DSN(); got != tt.want {
t.Errorf("DSN(): %s, want %s", got, tt.want)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion consumer_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Start(cfg *config.Config, ctx context.Context, hs HandlerMap, logger log.Lo
}

func setupKafkaConsumerDbCollection(cfg *config.Config, logger log.Logger, fch chan model.Failure, hs HandlerMap, srmCfg *sarama.Config) (collection, error) {
db, err := data.NewDB(cfg.GetDBConnectionString(), logger)
db, err := data.NewDB(cfg.DSN(), logger)
if err != nil {
return nil, fmt.Errorf("could not connect to DB: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion integration/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {
log.Fatalf("failed to start Sarama producer: %s", err)
}

db, err = data.NewDB(cfg.GetDBConnectionString(), ourlog.NullLogger{})
db, err = data.NewDB(cfg.DSN(), ourlog.NullLogger{})
if err != nil {
log.Fatalf("failed to connect to the DB: %s", err)
}
Expand Down

0 comments on commit a35cf1e

Please sign in to comment.