Skip to content

Commit 9084743

Browse files
authored
Use proper conn string parsing to handle for passwords with special chars (#39)
Backed with specs in CI
1 parent f82dbc4 commit 9084743

File tree

7 files changed

+41
-24
lines changed

7 files changed

+41
-24
lines changed

.github/workflows/integration.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
PG_HOST: localhost
3636
PG_PORT: 5433
3737
PG_USER: myuser
38-
PG_PASSWORD: mypassword
38+
PG_PASSWORD: mypassword!@#%1234
3939
PG_DB: mydb
4040
working-directory: .
4141
run: |
@@ -69,12 +69,12 @@ jobs:
6969
PG_HOST: localhost
7070
PG_PORT: 5433
7171
PG_USER: myuser
72-
PG_PASSWORD: mypassword
72+
PG_PASSWORD: mypassword!@#%1234
7373
PG_DB: mydb
7474
TARGET_PG_HOST: localhost
7575
TARGET_PG_PORT: 5434
7676
TARGET_PG_USER: targetuser
77-
TARGET_PG_PASSWORD: targetpassword
77+
TARGET_PG_PASSWORD: targetpassword!@#1234
7878
TARGET_PG_DB: targetdb
7979
working-directory: .
8080
run: |
@@ -120,12 +120,12 @@ jobs:
120120
PG_HOST: localhost
121121
PG_PORT: 5433
122122
PG_USER: myuser
123-
PG_PASSWORD: mypassword
123+
PG_PASSWORD: mypassword!@#%1234
124124
PG_DB: mydb
125125
TARGET_PG_HOST: localhost
126126
TARGET_PG_PORT: 5434
127127
TARGET_PG_USER: targetuser
128-
TARGET_PG_PASSWORD: targetpassword
128+
TARGET_PG_PASSWORD: targetpassword!@#1234
129129
TARGET_PG_DB: targetdb
130130
working-directory: .
131131
run: |
@@ -159,12 +159,12 @@ jobs:
159159
PG_HOST: localhost
160160
PG_PORT: 5433
161161
PG_USER: myuser
162-
PG_PASSWORD: mypassword
162+
PG_PASSWORD: mypassword!@#%1234
163163
PG_DB: mydb
164164
TARGET_PG_HOST: localhost
165165
TARGET_PG_PORT: 5434
166166
TARGET_PG_USER: targetuser
167-
TARGET_PG_PASSWORD: targetpassword
167+
TARGET_PG_PASSWORD: targetpassword!@#1234
168168
TARGET_PG_DB: targetdb
169169
working-directory: .
170170
run: |
@@ -198,12 +198,12 @@ jobs:
198198
PG_HOST: localhost
199199
PG_PORT: 5433
200200
PG_USER: myuser
201-
PG_PASSWORD: mypassword
201+
PG_PASSWORD: mypassword!@#%1234
202202
PG_DB: mydb
203203
TARGET_PG_HOST: localhost
204204
TARGET_PG_PORT: 5434
205205
TARGET_PG_USER: targetuser
206-
TARGET_PG_PASSWORD: targetpassword
206+
TARGET_PG_PASSWORD: targetpassword!@#1234
207207
TARGET_PG_DB: targetdb
208208
working-directory: .
209209
run: |
@@ -237,12 +237,12 @@ jobs:
237237
PG_HOST: localhost
238238
PG_PORT: 5433
239239
PG_USER: myuser
240-
PG_PASSWORD: mypassword
240+
PG_PASSWORD: mypassword!@#%1234
241241
PG_DB: mydb
242242
TARGET_PG_HOST: localhost
243243
TARGET_PG_PORT: 5434
244244
TARGET_PG_USER: targetuser
245-
TARGET_PG_PASSWORD: targetpassword
245+
TARGET_PG_PASSWORD: targetpassword!@#1234
246246
TARGET_PG_DB: targetdb
247247
working-directory: .
248248
run: |

internal/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services:
66
container_name: pg_logical_replication
77
environment:
88
POSTGRES_USER: myuser
9-
POSTGRES_PASSWORD: mypassword
9+
POSTGRES_PASSWORD: mypassword!@#%1234
1010
POSTGRES_DB: mydb
1111
volumes:
1212
- postgres_data:/var/lib/postgresql/data
@@ -27,7 +27,7 @@ services:
2727
container_name: pg_target
2828
environment:
2929
POSTGRES_USER: targetuser
30-
POSTGRES_PASSWORD: targetpassword
30+
POSTGRES_PASSWORD: targetpassword!@#1234
3131
POSTGRES_DB: targetdb
3232
volumes:
3333
- target_postgres_data:/var/lib/postgresql/data

internal/scripts/e2e_common.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
PG_HOST="${PG_HOST:-localhost}"
44
PG_PORT="${PG_PORT:-5433}"
55
PG_USER="${PG_USER:-myuser}"
6-
PG_PASSWORD="${PG_PASSWORD:-mypassword}"
6+
PG_PASSWORD="${PG_PASSWORD:-mypassword!@#%1234}"
77
PG_DB="${PG_DB:-mydb}"
88

99
TARGET_PG_HOST="${TARGET_PG_HOST:-localhost}"
1010
TARGET_PG_PORT="${TARGET_PG_PORT:-5434}"
1111
TARGET_PG_USER="${TARGET_PG_USER:-targetuser}"
12-
TARGET_PG_PASSWORD="${TARGET_PG_PASSWORD:-targetpassword}"
12+
TARGET_PG_PASSWORD="${TARGET_PG_PASSWORD:-targetpassword!@#1234}"
1313
TARGET_PG_DB="${TARGET_PG_DB:-targetdb}"
1414

1515
NATS_URL="${NATS_URL:-nats://localhost:4222}"

internal/scripts/e2e_order_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class OrderTest
2323
PG_HOST = 'localhost'
2424
PG_PORT = 5433
2525
PG_USER = 'myuser'
26-
PG_PASSWORD = 'mypassword'
26+
PG_PASSWORD = 'mypassword!@#%1234'
2727
PG_DB = 'mydb'
2828

2929
TARGET_PG_HOST = 'localhost'
3030
TARGET_PG_PORT = 5434
3131
TARGET_PG_USER = 'targetuser'
32-
TARGET_PG_PASSWORD = 'targetpassword'
32+
TARGET_PG_PASSWORD = 'targetpassword!@#1234'
3333
TARGET_PG_DB = 'targetdb'
3434

3535
# NATS configuration

internal/scripts/e2e_resume_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class ResumeTest
2424
PG_HOST = 'localhost'
2525
PG_PORT = 5433
2626
PG_USER = 'myuser'
27-
PG_PASSWORD = 'mypassword'
27+
PG_PASSWORD = 'mypassword!@#%1234'
2828
PG_DB = 'mydb'
2929

3030
TARGET_PG_HOST = 'localhost'
3131
TARGET_PG_PORT = 5434
3232
TARGET_PG_USER = 'targetuser'
33-
TARGET_PG_PASSWORD = 'targetpassword'
33+
TARGET_PG_PASSWORD = 'targetpassword!@#1234'
3434
TARGET_PG_DB = 'targetdb'
3535

3636
NATS_URL = 'nats://localhost:4222'

pkg/replicator/replication_connection.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/jackc/pglogrepl"
8+
"github.com/jackc/pgx/v5"
89
"github.com/jackc/pgx/v5/pgconn"
910
"github.com/jackc/pgx/v5/pgproto3"
1011
)
@@ -25,14 +26,24 @@ func NewReplicationConnection(config Config) ReplicationConnection {
2526

2627
// Connect establishes a connection to the PostgreSQL database for replication.
2728
func (rc *PostgresReplicationConnection) Connect(ctx context.Context) error {
28-
connString := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?replication=database",
29-
rc.Config.User, rc.Config.Password, rc.Config.Host, rc.Config.Port, rc.Config.Database)
29+
config, err := pgx.ParseConfig(fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s",
30+
rc.Config.Host,
31+
rc.Config.Port,
32+
rc.Config.Database,
33+
rc.Config.User,
34+
rc.Config.Password))
35+
if err != nil {
36+
return fmt.Errorf("failed to parse connection config: %v", err)
37+
}
3038

31-
conn, err := pgconn.Connect(ctx, connString)
39+
config.RuntimeParams["replication"] = "database"
40+
41+
conn, err := pgx.ConnectConfig(ctx, config)
3242
if err != nil {
3343
return fmt.Errorf("failed to connect to PostgreSQL: %v", err)
3444
}
35-
rc.Conn = conn
45+
46+
rc.Conn = conn.PgConn()
3647
return nil
3748
}
3849

pkg/replicator/standard_connection.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ type StandardConnectionImpl struct {
1616

1717
// NewStandardConnection creates a new StandardConnectionImpl instance and establishes a connection.
1818
func NewStandardConnection(config Config) (*StandardConnectionImpl, error) {
19-
connString := fmt.Sprintf("postgres://%s:%s@%s:%d/%s", config.User, config.Password, config.Host, config.Port, config.Database)
19+
connString := fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s",
20+
config.Host,
21+
config.Port,
22+
config.Database,
23+
config.User,
24+
config.Password)
25+
2026
poolConfig, err := pgxpool.ParseConfig(connString)
2127
if err != nil {
2228
return nil, fmt.Errorf("unable to parse connection string: %v", err)

0 commit comments

Comments
 (0)