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: handle use system ca for mongodb (#13083)
h3n4la41bb0d274fe
diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.goindex 2a441a888f..1f0d34817c 100644--- a/backend/plugin/db/mongodb/mongodb.go+++ b/backend/plugin/db/mongodb/mongodb.go@@ -112,19 +112,24 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ db.Execut"--quiet",}- if driver.connCfg.TLSConfig.SslCA != "" {+ if driver.connCfg.TLSConfig.UseSSL {mongoshArgs = append(mongoshArgs, "--tls")- // Write the tlsCAFile to a temporary file, and use the temporary file as the value of --tlsCAFile.- // The reason is that the --tlsCAFile option of mongosh does not support the value of the certificate directly.- caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.ConnectionDatabase, uuid.New().String())- defer func() {- // While error occurred in mongosh, the temporary file may not created, so we ignore the error here.- _ = os.Remove(caFileName)- }()- if err := os.WriteFile(caFileName, []byte(driver.connCfg.TLSConfig.SslCA), 0400); err != nil {- return 0, errors.Wrap(err, "failed to write tlsCAFile to temporary file")++ if driver.connCfg.TLSConfig.SslCA == "" {+ mongoshArgs = append(mongoshArgs, "--tlsUseSystemCA")+ } else {+ // Write the tlsCAFile to a temporary file, and use the temporary file as the value of --tlsCAFile.+ // The reason is that the --tlsCAFile option of mongosh does not support the value of the certificate directly.+ caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.ConnectionDatabase, uuid.New().String())+ defer func() {+ // While error occurred in mongosh, the temporary file may not created, so we ignore the error here.+ _ = os.Remove(caFileName)+ }()+ if err := os.WriteFile(caFileName, []byte(driver.connCfg.TLSConfig.SslCA), 0400); err != nil {+ return 0, errors.Wrap(err, "failed to write tlsCAFile to temporary file")+ }+ mongoshArgs = append(mongoshArgs, "--tlsCAFile", caFileName)}- mongoshArgs = append(mongoshArgs, "--tlsCAFile", caFileName)}// First, we create a temporary file to store the statement.@@ -243,19 +248,23 @@ func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement stri"false",}- if driver.connCfg.TLSConfig.SslCA != "" {+ if driver.connCfg.TLSConfig.UseSSL {mongoshArgs = append(mongoshArgs, "--tls")- // Write the tlsCAFile to a temporary file, and use the temporary file as the value of --tlsCAFile.- // The reason is that the --tlsCAFile option of mongosh does not support the value of the certificate directly.- caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.ConnectionDatabase, uuid.New().String())- defer func() {- // While error occurred in mongosh, the temporary file may not created, so we ignore the error here.- _ = os.Remove(caFileName)- }()- if err := os.WriteFile(caFileName, []byte(driver.connCfg.TLSConfig.SslCA), 0400); err != nil {- return nil, errors.Wrap(err, "failed to write tlsCAFile to temporary file")+ if driver.connCfg.TLSConfig.SslCA == "" {+ mongoshArgs = append(mongoshArgs, "--tlsUseSystemCA")+ } else {+ // Write the tlsCAFile to a temporary file, and use the temporary file as the value of --tlsCAFile.+ // The reason is that the --tlsCAFile option of mongosh does not support the value of the certificate directly.+ caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.ConnectionDatabase, uuid.New().String())+ defer func() {+ // While error occurred in mongosh, the temporary file may not created, so we ignore the error here.+ _ = os.Remove(caFileName)+ }()+ if err := os.WriteFile(caFileName, []byte(driver.connCfg.TLSConfig.SslCA), 0400); err != nil {+ return nil, errors.Wrap(err, "failed to write tlsCAFile to temporary file")+ }+ mongoshArgs = append(mongoshArgs, "--tlsCAFile", caFileName)}- mongoshArgs = append(mongoshArgs, "--tlsCAFile", caFileName)}queryResultFileName := fmt.Sprintf("mongodb-query-%s-%s", driver.connCfg.ConnectionDatabase, uuid.New().String())