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: support mongodb mutual tls (#13088)
h3n4l0889b1af6957
diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.goindex 1f0d34817c..ba2b308180 100644--- a/backend/plugin/db/mongodb/mongodb.go+++ b/backend/plugin/db/mongodb/mongodb.go@@ -114,13 +114,15 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ db.Executif driver.connCfg.TLSConfig.UseSSL {mongoshArgs = append(mongoshArgs, "--tls")+ mongoshArgs = append(mongoshArgs, "--tlsAllowInvalidHostnames")+ uuid := uuid.New().String()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())+ caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.ConnectionDatabase, uuid)defer func() {// While error occurred in mongosh, the temporary file may not created, so we ignore the error here._ = os.Remove(caFileName)@@ -130,6 +132,28 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ db.Execut}mongoshArgs = append(mongoshArgs, "--tlsCAFile", caFileName)}++ if driver.connCfg.TLSConfig.SslKey != "" && driver.connCfg.TLSConfig.SslCert != "" {+ clientCertName := fmt.Sprintf("mongodb-tls-client-cert-%s-%s", driver.connCfg.ConnectionDatabase, uuid)+ defer func() {+ // While error occurred in mongosh, the temporary file may not created, so we ignore the error here.+ _ = os.Remove(clientCertName)+ }()+ var sb strings.Builder+ if _, err := sb.WriteString(driver.connCfg.TLSConfig.SslKey); err != nil {+ return 0, errors.Wrapf(err, "failed to write ssl key into string builder")+ }+ if _, err := sb.WriteString("\n"); err != nil {+ return 0, errors.Wrapf(err, "failed to write new line into string builder")+ }+ if _, err := sb.WriteString(driver.connCfg.TLSConfig.SslCert); err != nil {+ return 0, errors.Wrapf(err, "failed to write ssl cert into string builder")+ }+ if err := os.WriteFile(clientCertName, []byte(sb.String()), 0400); err != nil {+ return 0, errors.Wrap(err, "failed to write tlsCAFile to temporary file")+ }+ mongoshArgs = append(mongoshArgs, "--tlsCertificateKeyFile", clientCertName)+ }}// First, we create a temporary file to store the statement.@@ -250,12 +274,15 @@ func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement striif driver.connCfg.TLSConfig.UseSSL {mongoshArgs = append(mongoshArgs, "--tls")+ mongoshArgs = append(mongoshArgs, "--tlsAllowInvalidHostnames")++ uuid := uuid.New().String()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())+ caFileName := fmt.Sprintf("mongodb-tls-ca-%s-%s", driver.connCfg.ConnectionDatabase, uuid)defer func() {// While error occurred in mongosh, the temporary file may not created, so we ignore the error here._ = os.Remove(caFileName)@@ -265,6 +292,28 @@ func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement stri}mongoshArgs = append(mongoshArgs, "--tlsCAFile", caFileName)}++ if driver.connCfg.TLSConfig.SslKey != "" && driver.connCfg.TLSConfig.SslCert != "" {+ clientCertName := fmt.Sprintf("mongodb-tls-client-cert-%s-%s", driver.connCfg.ConnectionDatabase, uuid)+ defer func() {+ // While error occurred in mongosh, the temporary file may not created, so we ignore the error here.+ _ = os.Remove(clientCertName)+ }()+ var sb strings.Builder+ if _, err := sb.WriteString(driver.connCfg.TLSConfig.SslKey); err != nil {+ return nil, errors.Wrapf(err, "failed to write ssl key into string builder")+ }+ if _, err := sb.WriteString("\n"); err != nil {+ return nil, errors.Wrapf(err, "failed to write new line into string builder")+ }+ if _, err := sb.WriteString(driver.connCfg.TLSConfig.SslCert); err != nil {+ return nil, errors.Wrapf(err, "failed to write ssl cert into string builder")+ }+ if err := os.WriteFile(clientCertName, []byte(sb.String()), 0400); err != nil {+ return nil, errors.Wrap(err, "failed to write tlsCAFile to temporary file")+ }+ mongoshArgs = append(mongoshArgs, "--tlsCertificateKeyFile", clientCertName)+ }}queryResultFileName := fmt.Sprintf("mongodb-query-%s-%s", driver.connCfg.ConnectionDatabase, uuid.New().String())diff --git a/frontend/src/components/InstanceForm/DataSourceSection/SslCertificateFormV1.vue b/frontend/src/components/InstanceForm/DataSourceSection/SslCertificateFormV1.vueindex 8476aa4d24..95016c7cd5 100644--- a/frontend/src/components/InstanceForm/DataSourceSection/SslCertificateFormV1.vue+++ b/frontend/src/components/InstanceForm/DataSourceSection/SslCertificateFormV1.vue@@ -77,11 +77,11 @@ const state = reactive<LocalState>({});const hasSSLKeyField = computed(() => {- return ![Engine.MONGODB, Engine.MSSQL].includes(props.engineType);+ return ![Engine.MSSQL].includes(props.engineType);});const hasSSLCertField = computed(() => {- return ![Engine.MONGODB, Engine.MSSQL].includes(props.engineType);+ return ![Engine.MSSQL].includes(props.engineType);});// Sync the latest version to local state when props.value changed.