Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: support ssl connection for connect database (#1571)

h3n4lde31150e2e19
diff --git a/api/data_source.go b/api/data_source.go
index 7e13958167..cdd1b27639 100644
--- a/api/data_source.go
+++ b/api/data_source.go
@@ -75,6 +75,9 @@ type DataSource struct {
Username string `jsonapi:"attr,username"`
// Do not return the password to client
Password string
+ SslCa string
+ SslCert string
+ SslKey string
}
// DataSourceCreate is the API message for creating a data source.
@@ -92,6 +95,9 @@ type DataSourceCreate struct {
Type DataSourceType `jsonapi:"attr,type"`
Username string `jsonapi:"attr,username"`
Password string `jsonapi:"attr,password"`
+ SslCa string `jsonapi:"attr,sslCa"`
+ SslCert string `jsonapi:"attr,sslCert"`
+ SslKey string `jsonapi:"attr,sslKey"`
// If true, syncs the schema after creating the data source. The client
// may set to false if the target data source's instance contains too many databases
// to avoid the request timeout.
@@ -130,6 +136,9 @@ type DataSourcePatch struct {
Username *string `jsonapi:"attr,username"`
Password *string `jsonapi:"attr,password"`
UseEmptyPassword *bool `jsonapi:"attr,useEmptyPassword"`
+ SslCa *string `jsonapi:"attr,sslCa"`
+ SslCert *string `jsonapi:"attr,sslCert"`
+ SslKey *string `jsonapi:"attr,sslKey"`
// If true, syncs the schema after patching the data source. The client
// may set to false if the target data source's instance contains too many databases
// to avoid the request timeout.
diff --git a/api/instance.go b/api/instance.go
index 9f4b5e5921..2235353873 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -55,6 +55,9 @@ type InstanceCreate struct {
Port string `jsonapi:"attr,port"`
Username string `jsonapi:"attr,username"`
Password string `jsonapi:"attr,password"`
+ SslCa string `jsonapi:"attr,sslCa"`
+ SslCert string `jsonapi:"attr,sslCert"`
+ SslKey string `jsonapi:"attr,sslKey"`
// If true, syncs the schema after adding the instance. The client
// may set to false if the target instance contains too many databases
// to avoid the request timeout.
diff --git a/plugin/db/ssl.go b/plugin/db/ssl.go
index f90532f6ee..ac2aa43994 100644
--- a/plugin/db/ssl.go
+++ b/plugin/db/ssl.go
@@ -4,7 +4,6 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
- "os"
)
// TLSConfig is the configuration for SSL connection.
@@ -20,11 +19,7 @@ func (tc TLSConfig) GetSslConfig() (*tls.Config, error) {
return nil, nil
}
rootCertPool := x509.NewCertPool()
- pem, err := os.ReadFile(tc.SslCA)
- if err != nil {
- return nil, err
- }
- if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
+ if ok := rootCertPool.AppendCertsFromPEM([]byte(tc.SslCA)); !ok {
return nil, fmt.Errorf("rootCertPool.AppendCertsFromPEM() failed to append server CA pem")
}
@@ -36,7 +31,7 @@ func (tc TLSConfig) GetSslConfig() (*tls.Config, error) {
}
if tc.SslCert != "" && tc.SslKey != "" {
var clientCert []tls.Certificate
- certs, err := tls.LoadX509KeyPair(tc.SslCert, tc.SslKey)
+ certs, err := tls.X509KeyPair([]byte(tc.SslCert), []byte(tc.SslKey))
if err != nil {
return nil, err
}
diff --git a/store/data_source.go b/store/data_source.go
index 8ce3096fd5..66e31fc890 100644
--- a/store/data_source.go
+++ b/store/data_source.go
@@ -30,6 +30,9 @@ type dataSourceRaw struct {
Type api.DataSourceType
Username string
Password string
+ SslCa string
+ SslCert string
+ SslKey string
}
// toDataSource creates an instance of DataSource based on the dataSourceRaw.
@@ -53,6 +56,9 @@ func (raw *dataSourceRaw) toDataSource() *api.DataSource {
Type: raw.Type,
Username: raw.Username,
Password: raw.Password,
+ SslCa: raw.SslCa,
+ SslCert: raw.SslCert,
+ SslKey: raw.SslKey,
}
}
@@ -238,10 +244,13 @@ func (s *Store) createDataSourceImpl(ctx context.Context, tx *sql.Tx, create *ap
name,
type,
username,
- password
+ password,
+ ssl_key,
+ ssl_cert,
+ ssl_ca
)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, database_id, name, type, username, password
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, database_id, name, type, username, password, ssl_key, ssl_cert, ssl_ca
`,
create.CreatorID,
create.CreatorID,
@@ -251,6 +260,9 @@ func (s *Store) createDataSourceImpl(ctx context.Context, tx *sql.Tx, create *ap
create.Type,
create.Username,
create.Password,
+ create.SslKey,
+ create.SslCert,
+ create.SslCa,
)
if err != nil {
@@ -272,6 +284,9 @@ func (s *Store) createDataSourceImpl(ctx context.Context, tx *sql.Tx, create *ap
&dataSourceRaw.Type,
&dataSourceRaw.Username,
&dataSourceRaw.Password,
+ &dataSourceRaw.SslKey,
+ &dataSourceRaw.SslCert,
+ &dataSourceRaw.SslCa,
); err != nil {
return nil, FormatError(err)
}
@@ -307,7 +322,10 @@ func (s *Store) findDataSourceImpl(ctx context.Context, tx *sql.Tx, find *api.Da
name,
type,
username,
- password
+ password,
+ ssl_key,
+ ssl_cert,
+ ssl_ca
FROM data_source
WHERE `+strings.Join(where, " AND "),
args...,
@@ -333,6 +351,9 @@ func (s *Store) findDataSourceImpl(ctx context.Context, tx *sql.Tx, find *api.Da
&dataSourceRaw.Type,
&dataSourceRaw.Username,
&dataSourceRaw.Password,
+ &dataSourceRaw.SslKey,
+ &dataSourceRaw.SslCert,
+ &dataSourceRaw.SslCa,
); err != nil {
return nil, FormatError(err)
}
@@ -356,7 +377,15 @@ func (s *Store) patchDataSourceImpl(ctx context.Context, tx *sql.Tx, patch *api.
if v := patch.Password; v != nil {
set, args = append(set, fmt.Sprintf("password = $%d", len(args)+1)), append(args, *v)
}
-
+ if v := patch.SslCa; v != nil {
+ set, args = append(set, fmt.Sprintf("ssl_ca= $%d", len(args)+1)), append(args, *v)
+ }
+ if v := patch.SslKey; v != nil {
+ set, args = append(set, fmt.Sprintf("ssl_key= $%d", len(args)+1)), append(args, *v)
+ }
+ if v := patch.SslCert; v != nil {
+ set, args = append(set, fmt.Sprintf("ssl_cert= $%d", len(args)+1)), append(args, *v)
+ }
args = append(args, patch.ID)
// Execute update query with RETURNING.
@@ -364,7 +393,7 @@ func (s *Store) patchDataSourceImpl(ctx context.Context, tx *sql.Tx, patch *api.
UPDATE data_source
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, database_id, name, type, username, password
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, database_id, name, type, username, password, ssl_key, ssl_cert, ssl_ca
`, len(args)),
args...,
)
@@ -387,6 +416,9 @@ func (s *Store) patchDataSourceImpl(ctx context.Context, tx *sql.Tx, patch *api.
&dataSourceRaw.Type,
&dataSourceRaw.Username,
&dataSourceRaw.Password,
+ &dataSourceRaw.SslKey,
+ &dataSourceRaw.SslCert,
+ &dataSourceRaw.SslCa,
); err != nil {
return nil, FormatError(err)
}
diff --git a/store/instance.go b/store/instance.go
index 94e2ad0140..5c6d2aeadf 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -295,6 +295,9 @@ func (s *Store) createInstanceRaw(ctx context.Context, create *api.InstanceCreat
Type: api.Admin,
Username: create.Username,
Password: create.Password,
+ SslKey: create.SslKey,
+ SslCert: create.SslCert,
+ SslCa: create.SslCa,
}
if err := s.createDataSourceRawTx(ctx, tx.PTx, adminDataSourceCreate); err != nil {
return nil, err