Skip to content

Commit 5aff626

Browse files
committed
Enable SSL flag
1 parent 5b72f62 commit 5aff626

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Usage of pdd:
2222
--database string Database name (default "postgres")
2323
--user string Database user (default "postgres")
2424
--pass string Database password (default "postgres")
25+
--enable-ssl Use TSL/SSL for connection
2526
--dial-timeout duration Dial timeout for establishing new connections (default 5s)
2627
--read-timeout duration Timeout for socket reads. If reached, commands will fail (default 30s)
2728
--max-retry int Maximum number of retries before giving up.

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func main() {
6060
flag.StringVar(&dbc.Database, "database", database.DefaultDatabase, "Database name")
6161
flag.StringVar(&dbc.User, "user", database.DefaultUser, "Database user")
6262
flag.StringVar(&dbc.Password, "pass", database.DefaultPassword, "Database password")
63+
flag.BoolVar(&dbc.EnableSSL, "enable-ssl", false, "Use TSL/SSL for connection")
6364
flag.DurationVar(&dbc.DialTimeout, "dial-timeout", database.DefaultDialTimeout, "Dial timeout for establishing new connections")
6465
flag.DurationVar(&dbc.ReadTimeout, "read-timeout", database.DefaultReadTimeout, "Timeout for socket reads. If reached, commands will fail")
6566
flag.IntVar(&dbc.MaxRetries, "max-retry", database.DefaultMaxRetries, "Maximum number of retries before giving up.")

database/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Config struct {
1919
Database string
2020
User string
2121
Password string
22+
EnableSSL bool
2223
MaxRetries int
2324
DialTimeout time.Duration
2425
ReadTimeout time.Duration

database/database.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package database
22

33
import (
44
"context"
5+
"crypto/tls"
56
"fmt"
67
"io"
78
"strings"
@@ -31,6 +32,13 @@ type db struct {
3132

3233
// ConnectDB connects to a database using provided options.
3334
func ConnectDB(logger log.Logger, cfg *Config) (DB, error) {
35+
var tlsConfig tls.Config
36+
37+
if cfg.EnableSSL {
38+
serverName := strings.Split(cfg.Addr, ":")[0]
39+
tlsConfig = tls.Config{ServerName: serverName}
40+
}
41+
3442
pgdb := pg.Connect(
3543
&pg.Options{
3644
Addr: cfg.Addr,
@@ -40,6 +48,7 @@ func ConnectDB(logger log.Logger, cfg *Config) (DB, error) {
4048
MaxRetries: cfg.MaxRetries,
4149
DialTimeout: cfg.DialTimeout,
4250
ReadTimeout: cfg.ReadTimeout,
51+
TLSConfig: &tlsConfig,
4352
},
4453
)
4554

0 commit comments

Comments
 (0)