Skip to content

Commit

Permalink
Merge pull request #2 from amontalban/allow_sslmode_config
Browse files Browse the repository at this point in the history
Allow configuration of sslmode for DB connections
  • Loading branch information
Navid2zp authored Oct 13, 2021
2 parents 102740d + ae71996 commit c4046e9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
10 changes: 9 additions & 1 deletion config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Monitor:
Password: 123456
DBName: pg_auto_failover

# sslmode for connecting to the monitor service (Defaults to 'disable')
# Reference: https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS
SSLMode: disable

# A list of coordinator nodes that you want to be monitored for changes in their worker nodes.
Coordinators:
- DBName: postgres
Expand All @@ -15,6 +19,10 @@ Coordinators:
# Formation that the node can be found with in monitor.
Formation: default

# sslmode for connecting to the coordinator nodes (Defaults to 'disable')
# Reference: https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS
SSLMode: disable

# Service settings
Settings:
# Check interval for changes (in ms).
Expand All @@ -31,4 +39,4 @@ API:

# The secret key that will be used to authorize requests.
# All requests require this string in their header as SECRET.
Secret: SECRET_STRING
Secret: SECRET_STRING
5 changes: 4 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package config

import (
"fmt"
"github.com/spf13/viper"
"log"
"os"
"path/filepath"
"strings"

"github.com/spf13/viper"
)

// ServiceConfig represents the config data for the application.
Expand All @@ -17,12 +18,14 @@ type ServiceConfig struct {
User string
Password string
DBName string
SSLMode string `default:"disable"`
}
Coordinators []struct {
Formation string
Username string
Password string
DBName string
SSLMode string `default:"disable"`
}
Settings struct {
CheckInterval int
Expand Down
6 changes: 3 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ func (d *database) connect(coordinatorNode *Coordinator) error {
if d.db == nil {
d.host = coordinatorNode.Host
d.port = coordinatorNode.Port
d.db, err = openDBConnection(d.host, d.username, d.dbname, d.password, d.port)
d.db, err = openDBConnection(d.host, d.username, d.dbname, d.password, d.port, d.sslmode)
return err
}
if d.host != coordinatorNode.Host || d.port != coordinatorNode.Port {
logger.CoordinatorChanged(coordinatorNode.Host, d.host, d.dbname, coordinatorNode.Port, d.port)
d.host = coordinatorNode.Host
d.port = coordinatorNode.Port
d.db, err = openDBConnection(d.host, d.username, d.dbname, d.password, d.port)
d.db, err = openDBConnection(d.host, d.username, d.dbname, d.password, d.port, d.sslmode)
return err
}
if d.db.Ping() != nil {
logger.CoordinatorConnectionLost(d.host, d.dbname, d.username, d.port)
d.db, err = openDBConnection(d.host, d.username, d.dbname, d.password, d.port)
d.db, err = openDBConnection(d.host, d.username, d.dbname, d.password, d.port, d.sslmode)
return err
}
return nil
Expand Down
12 changes: 9 additions & 3 deletions core/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"fmt"

"github.com/Navid2zp/citus-failover/config"
"github.com/jmoiron/sqlx"
)
Expand All @@ -13,11 +14,13 @@ type database struct {
username string
password string
dbname string
sslmode string `default:"disable"`
db *sqlx.DB
}

// databases holds the list of all databases to be monitored
var databases []*database

// monitorDB is database instance for monitor
var monitorDB *sqlx.DB

Expand All @@ -28,12 +31,13 @@ func openMonitoringConnection() {
monitorDB, err = sqlx.Connect(
"postgres",
fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
config.Config.Monitor.Host,
config.Config.Monitor.Port,
config.Config.Monitor.User,
config.Config.Monitor.Password,
config.Config.Monitor.DBName,
config.Config.Monitor.SSLMode,
),
)
if err != nil {
Expand All @@ -51,23 +55,25 @@ func setupDatabases() {
username: db.Username,
password: db.Password,
dbname: db.DBName,
sslmode: db.SSLMode,
db: nil,
}
databases = append(databases, &coordinator)
}
}

// openDBConnection opens a database connection.
func openDBConnection(host, username, dbname, password string, port int) (*sqlx.DB, error) {
func openDBConnection(host, username, dbname, password string, port int, sslmode string) (*sqlx.DB, error) {
return sqlx.Connect(
"postgres",
fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
host,
port,
username,
password,
dbname,
sslmode,
),
)
}
Expand Down

0 comments on commit c4046e9

Please sign in to comment.