Skip to content

Commit f58a5f4

Browse files
committed
add db ssl config
1 parent debe5b5 commit f58a5f4

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

admin/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,13 @@ func init() {
444444
EnvVars: []string{"DB_CONN_RETRY"},
445445
Destination: &dbConfigValues.ConnRetry,
446446
},
447+
&cli.BoolFlag{
448+
Name: "db-ssl-mode",
449+
Value: false,
450+
Usage: "Whether to use SSL mode to connect to database or not, the default value is false",
451+
EnvVars: []string{"DB_SSL_MODE"},
452+
Destination: &dbConfigValues.SSLMode,
453+
},
447454
&cli.BoolFlag{
448455
Name: "tls",
449456
Aliases: []string{"t"},

api/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ func init() {
365365
EnvVars: []string{"DB_CONN_RETRY"},
366366
Destination: &dbConfigValues.ConnRetry,
367367
},
368+
&cli.BoolFlag{
369+
Name: "db-ssl-mode",
370+
Value: false,
371+
Usage: "Whether to use SSL mode to connect to database or not, the default value is false",
372+
EnvVars: []string{"DB_SSL_MODE"},
373+
Destination: &dbConfigValues.SSLMode,
374+
},
368375
&cli.BoolFlag{
369376
Name: "tls",
370377
Aliases: []string{"t"},

backend/backend.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
const (
1414
// DBString to format connection string to database for postgres
15-
DBString = "host=%s port=%s dbname=%s user=%s password=%s sslmode=require"
15+
DBString = "host=%s port=%s dbname=%s user=%s password=%s sslmode=%s"
1616
// DBKey to identify the configuration JSON key
1717
DBKey = "db"
1818
)
@@ -35,6 +35,7 @@ type JSONConfigurationDB struct {
3535
MaxOpenConns int `json:"maxOpenConns"`
3636
ConnMaxLifetime int `json:"connMaxLifetime"`
3737
ConnRetry int `json:"connRetry"`
38+
SSLMode bool `json:"sslMode"`
3839
}
3940

4041
// LoadConfiguration to load the DB configuration file and assign to variables
@@ -56,8 +57,12 @@ func LoadConfiguration(file, key string) (JSONConfigurationDB, error) {
5657

5758
// PrepareDSN to generate DB connection string
5859
func PrepareDSN(config JSONConfigurationDB) string {
60+
sslMode := "disable"
61+
if config.SSLMode {
62+
sslMode = "require"
63+
}
5964
return fmt.Sprintf(
60-
DBString, config.Host, config.Port, config.Name, config.Username, config.Password)
65+
DBString, config.Host, config.Port, config.Name, config.Username, config.Password, sslMode)
6166
}
6267

6368
// GetDB to get PostgreSQL DB using GORM

tls/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,13 @@ func init() {
392392
EnvVars: []string{"DB_CONN_RETRY"},
393393
Destination: &dbConfigValues.ConnRetry,
394394
},
395+
&cli.BoolFlag{
396+
Name: "db-ssl-mode",
397+
Value: false,
398+
Usage: "Whether to use SSL mode to connect to database or not, the default value is false",
399+
EnvVars: []string{"DB_SSL_MODE"},
400+
Destination: &dbConfigValues.SSLMode,
401+
},
395402
&cli.BoolFlag{
396403
Name: "tls",
397404
Aliases: []string{"t"},

0 commit comments

Comments
 (0)