@@ -27,7 +27,9 @@ import (
2727 "github.com/apache/answer/internal/base/reason"
2828 "github.com/apache/answer/internal/base/validator"
2929 "github.com/apache/answer/pkg/checker"
30+ "github.com/apache/answer/pkg/dir"
3031 "github.com/segmentfault/pacman/errors"
32+ "github.com/segmentfault/pacman/log"
3133 "xorm.io/xorm/schemas"
3234)
3335
@@ -40,12 +42,17 @@ type CheckConfigFileResp struct {
4042
4143// CheckDatabaseReq check database
4244type CheckDatabaseReq struct {
43- DbType string `validate:"required,oneof=postgres sqlite3 mysql" json:"db_type"`
44- DbUsername string `json:"db_username"`
45- DbPassword string `json:"db_password"`
46- DbHost string `json:"db_host"`
47- DbName string `json:"db_name"`
48- DbFile string `json:"db_file"`
45+ DbType string `validate:"required,oneof=postgres sqlite3 mysql" json:"db_type"`
46+ DbUsername string `json:"db_username"`
47+ DbPassword string `json:"db_password"`
48+ DbHost string `json:"db_host"`
49+ DbName string `json:"db_name"`
50+ DbFile string `json:"db_file"`
51+ Ssl bool `json:"ssl_enabled"`
52+ SslMode string `json:"ssl_mode"`
53+ SslCrt string `json:"pem_file"`
54+ SslKey string `json:"key_file"`
55+ SslCrtClient string `json:"cert_file"`
4956}
5057
5158// GetConnection get connection string
@@ -59,8 +66,25 @@ func (r *CheckDatabaseReq) GetConnection() string {
5966 }
6067 if r .DbType == string (schemas .POSTGRES ) {
6168 host , port := parsePgSQLHostPort (r .DbHost )
62- return fmt .Sprintf ("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable" ,
63- host , port , r .DbUsername , r .DbPassword , r .DbName )
69+ if ! r .Ssl {
70+ return fmt .Sprintf ("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable" ,
71+ host , port , r .DbUsername , r .DbPassword , r .DbName )
72+ } else if r .SslMode == "require" {
73+ return fmt .Sprintf ("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s" ,
74+ host , port , r .DbUsername , r .DbPassword , r .DbName , r .SslMode )
75+ } else if r .SslMode == "verify-ca" || r .SslMode == "verify-full" {
76+ if dir .CheckFileExist (r .SslCrt ) {
77+ log .Warnf ("ssl crt file not exist: %s" , r .SslCrt )
78+ }
79+ if dir .CheckFileExist (r .SslCrtClient ) {
80+ log .Warnf ("ssl crt client file not exist: %s" , r .SslCrtClient )
81+ }
82+ if dir .CheckFileExist (r .SslKey ) {
83+ log .Warnf ("ssl key file not exist: %s" , r .SslKey )
84+ }
85+ return fmt .Sprintf ("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s sslrootcert=%s sslcert=%s sslkey=%s" ,
86+ host , port , r .DbUsername , r .DbPassword , r .DbName , r .SslMode , r .SslCrt , r .SslCrtClient , r .SslKey )
87+ }
6488 }
6589 return ""
6690}
0 commit comments