Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: postgresql mutual tls and use system ca (#13160)
h3n4lab9f5bce5189
diff --git a/backend/plugin/db/pg/dump.go b/backend/plugin/db/pg/dump.goindex c14ed626a4..169dc06eca 100644--- a/backend/plugin/db/pg/dump.go+++ b/backend/plugin/db/pg/dump.go@@ -132,22 +132,34 @@ func (driver *Driver) execPgDump(ctx context.Context, args []string, out io.Writcmd := exec.CommandContext(ctx, pgDumpPath, args...)sslMode := getSSLMode(driver.config.TLSConfig, driver.config.SSHConfig)- cmd.Env = append(cmd.Env, fmt.Sprintf("PGSSLMODE=%s", sslMode))- if driver.config.TLSConfig.SslCA != "" {- caTempFile, err := os.CreateTemp("", "pg-ssl-ca-")- if err != nil {- return err- }- defer os.Remove(caTempFile.Name())- if _, err := caTempFile.WriteString(driver.config.TLSConfig.SslCA); err != nil {- return err- }- if err := caTempFile.Close(); err != nil {- return err+ // Unfortunately, pg_dump doesn't directly support use system certificate directly. Instead, it supprots+ // specify one cert file path in PGSSLROOTCERT environment variable.+ // MacOS(dev-env):+ // 1. The system certs are stored in the Keychain utility, and it is not recommended to access them outside of Keychain.+ // 2. The user self-signed ca should be add in Keychain utility and mark it is trusted.+ // Debian(our docker image based):+ // 1. The system certs are mostly stored in the /etc/ssl/certs/ca-certificates.crt, and some are stored in the /usr/share/ca-certificates/xxx.crt.+ // 2. The user self-signed ca should be added in /usr/share/ca-certificates/xxx.crt.+ // We should expose this option to user. For now, using require ssl mode to trust server certificate anyway.+ if driver.config.TLSConfig.UseSSL {+ sslMode = SSLModeRequire+ if driver.config.TLSConfig.SslCA != "" {+ caTempFile, err := os.CreateTemp("", "pg-ssl-ca-")+ if err != nil {+ return err+ }+ defer os.Remove(caTempFile.Name())+ if _, err := caTempFile.WriteString(driver.config.TLSConfig.SslCA); err != nil {+ return err+ }+ if err := caTempFile.Close(); err != nil {+ return err+ }+ cmd.Env = append(cmd.Env, fmt.Sprintf("PGSSLROOTCERT=%s", caTempFile.Name()))}- cmd.Env = append(cmd.Env, fmt.Sprintf("PGSSLROOTCERT=%s", caTempFile.Name()))}+ cmd.Env = append(cmd.Env, fmt.Sprintf("PGSSLMODE=%s", sslMode))if driver.config.TLSConfig.SslCert != "" {certTempFile, err := os.CreateTemp("", "pg-ssl-cert-")if err != nil {diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.goindex 56d0f211d5..4d876717b3 100644--- a/backend/plugin/db/pg/pg.go+++ b/backend/plugin/db/pg/pg.go@@ -12,8 +12,6 @@ import ("strings""time"- // Import pg driver.- // init() in pgx/v5/stdlib will register it's pgx driver."cloud.google.com/go/cloudsqlconn""github.com/aws/aws-sdk-go-v2/config""github.com/aws/aws-sdk-go-v2/feature/rds/auth"diff --git a/backend/plugin/db/pg/util.go b/backend/plugin/db/pg/util.goindex e1628cdae4..3d978a9c73 100644--- a/backend/plugin/db/pg/util.go+++ b/backend/plugin/db/pg/util.go@@ -19,12 +19,12 @@ const (func getSSLMode(tlsConfig db.TLSConfig, sshConfig db.SSHConfig) sslMode {sslMode := SSLModePrefer- if tlsConfig.SslCA != "" || tlsConfig.SslCert != "" || tlsConfig.SslKey != "" {+ if tlsConfig.UseSSL {sslMode = SSLModeVerifyFull- // If users use TLS/SSL with SSH tunneling together, the TLS/SSL handshake will be established between the localhost and the remote server.- // Then, we connect to the db server via the SSH tunnel(localhost), so we do not verify the SAN/CN of the certificate to avoid the hostname mismatch error.- if sshConfig.Host != "" {- sslMode = SSLModeVerifyCA+ if tlsConfig.SslCA != "" {+ if sshConfig.Host != "" {+ sslMode = SSLModeVerifyCA+ }}}return sslMode