The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Mongo x509 authentication and error reporting (#74050)
technomancy7e57fd8c319e
diff --git a/e2e/test/scenarios/admin/databases.cy.spec.js b/e2e/test/scenarios/admin/databases.cy.spec.jsindex 1e161156f88..d0011b6efae 100644--- a/e2e/test/scenarios/admin/databases.cy.spec.js+++ b/e2e/test/scenarios/admin/databases.cy.spec.js@@ -322,7 +322,7 @@ describe("admin > database > add", () => {cy.button("Save", { timeout: 7000 }).should("not.be.disabled").click();- cy.findByText(/Exception authenticating MongoCredential/);+ cy.findByText(/Authentication failed/);cy.button("Failed");cy.findByLabelText("Paste your connection string")diff --git a/mage/src/mage/start_db.clj b/mage/src/mage/start_db.cljindex 154d0f63b36..16e3c009e18 100644--- a/mage/src/mage/start_db.clj+++ b/mage/src/mage/start_db.clj@@ -93,6 +93,8 @@[_db container-name resolved-version port]["docker" "run""-d"+ "-e" "MONGO_INITDB_ROOT_USERNAME=metabase"+ "-e" "MONGO_INITDB_ROOT_PASSWORD=metasample123""-p" (str port ":27017")"--name" container-name(str "mongo:" resolved-version)])diff --git a/modules/drivers/mongo/src/metabase/driver/mongo.clj b/modules/drivers/mongo/src/metabase/driver/mongo.cljindex 357e5908a93..99517aa8d48 100644--- a/modules/drivers/mongo/src/metabase/driver/mongo.clj+++ b/modules/drivers/mongo/src/metabase/driver/mongo.clj@@ -25,6 +25,7 @@[metabase.util.performance :refer [some mapv empty? get-in]][taoensso.nippy :as nippy])(:import+ (com.mongodb MongoCommandException MongoSecurityException)(com.mongodb.client MongoClient MongoDatabase)(org.bson.types Binary ObjectId)))@@ -49,18 +50,25 @@(defmethod driver/can-connect? :mongo[_ db-details]- (mongo.connection/with-mongo-client [^MongoClient c db-details]- (let [db-names (mongo.util/list-database-names c)- db-name (mongo.db/db-name db-details)- db (mongo.util/database c db-name)- db-stats (mongo.util/run-command db {:dbStats 1} :keywordize true)]- (and- ;; 1. check db.dbStats command completes successfully- (= (float (:ok db-stats))- 1.0)- ;; 2. check the database is actually on the server- ;; (this is required because (1) is true even if the database doesn't exist)- (boolean (some #(= % db-name) db-names))))))+ (try+ (mongo.connection/with-mongo-client [^MongoClient c db-details]+ (let [db-names (mongo.util/list-database-names c)+ db-name (mongo.db/db-name db-details)+ db (mongo.util/database c db-name)+ db-stats (mongo.util/run-command db {:dbStats 1} :keywordize true)]+ (and+ ;; 1. check db.dbStats command completes successfully+ (= (float (:ok db-stats))+ 1.0)+ ;; 2. check the database is actually on the server+ ;; (this is required because (1) is true even if the database doesn't exist)+ (boolean (some #(= % db-name) db-names)))))+ (catch MongoSecurityException e+ (let [cause (.getCause e)]+ ;; the outer wrapper exception has a useless "Exception authenticating" message+ (if (instance? MongoCommandException cause)+ (throw cause)+ (throw e))))))(defmethod driver/humanize-connection-error-message:mongodiff --git a/modules/drivers/mongo/src/metabase/driver/mongo/connection.clj b/modules/drivers/mongo/src/metabase/driver/mongo/connection.cljindex 01d044ea04f..da99d123ac0 100644--- a/modules/drivers/mongo/src/metabase/driver/mongo/connection.clj+++ b/modules/drivers/mongo/src/metabase/driver/mongo/connection.clj@@ -70,7 +70,7 @@`MongoClientSettings$Builder` first. Then credentials are set and ssl context is updated in the `builder` object.Afterwards, `MongoClientSettings` are built using `.build`."^MongoClientSettings- [{:keys [authdb user pass use-conn-uri ssl] :as db-details}]+ [{:keys [authdb user pass use-conn-uri ssl additional-options] :as db-details}](let [connection-string (-> db-detailsdb-details->connection-stringConnectionString.)@@ -83,9 +83,13 @@;; manually verified that's not necessary.(when (seq user)(.credential builder- (MongoCredential/createCredential user- (or (not-empty authdb) "admin")- (char-array pass))))+ (if (some-> additional-options+ u/lower-case-en+ (str/index-of "authmechanism=mongodb-x509"))+ (MongoCredential/createMongoX509Credential user)+ (MongoCredential/createCredential user+ (or (not-empty authdb) "admin")+ (char-array pass)))))(when ssl(maybe-add-ssl-context-to-builder! builder db-details)))(.build builder)))diff --git a/modules/drivers/mongo/test/metabase/driver/mongo/connection_test.clj b/modules/drivers/mongo/test/metabase/driver/mongo/connection_test.cljindex ae36f1c7122..f4bf9daca85 100644--- a/modules/drivers/mongo/test/metabase/driver/mongo/connection_test.clj+++ b/modules/drivers/mongo/test/metabase/driver/mongo/connection_test.clj@@ -1,5 +1,6 @@(ns ^:mb/driver-tests metabase.driver.mongo.connection-test(:require+ [clojure.java.io :as io][clojure.string :as str][clojure.test :refer :all][metabase.config.core :as config]@@ -50,7 +51,20 @@(-> db-details(assoc :host "localhost")mongo.db/details-normalized- mongo.connection/db-details->connection-string))))))+ mongo.connection/db-details->connection-string))))+ (testing "uses x509 credentials"+ (let [db-details (assoc db-details+ :additional-options "authMechanism=MONGODB-X509"+ :use-conn-uri false+ :ssl true+ :ssl-use-client-auth true+ :client-ssl-key (-> "ssl/mongo/metabase.key" io/resource slurp)+ :client-ssl-cert (-> "ssl/mongo/metabase.crt" io/resource slurp)+ :ssl-cert (-> "ssl/mongo/metaca.crt" io/resource slurp))+ settings (mongo.connection/db-details->mongo-client-settings db-details)+ ^MongoCredential credential (.getCredential settings)]+ (is (= "test-user" (.getUserName credential)))+ (is (= "MONGODB-X509" (.getMechanism credential)))))))(deftest ^:parallel srv-connection-properties-test(testing "connection properties when using SRV"diff --git a/modules/drivers/mongo/test/metabase/driver/mongo_test.clj b/modules/drivers/mongo/test/metabase/driver/mongo_test.cljindex 412d5029bfe..3a5a6dd3652 100644--- a/modules/drivers/mongo/test/metabase/driver/mongo_test.clj+++ b/modules/drivers/mongo/test/metabase/driver/mongo_test.clj@@ -45,10 +45,8 @@;; ## Tests for connection functions(deftest can-connect-test?- (mt/test-driver- :mongo- (mt/dataset test-data- (mt/db)+ (mt/test-driver :mongo+ (mt/dataset test-data (mt/db)(doseq [{:keys [details expected message]} [{:details {:host "localhost":port 3000:dbname "bad-db-name"}@@ -86,6 +84,33 @@(driver.u/can-connect-with-details? :mongo ssl-details))(str message)))))))+;; openssl x509 -in test_resources/ssl/mongo/metabase.crt -inform PEM -subject -nameopt RFC2253+(def cert-subject+ "emailAddress=metabase@localhost,CN=localhost,OU=metabase,O=Metabase Inc.,L=San Francisco,ST=CA,C=US")++(deftest can-connect?-with-x509+ (if (tdm/ssl-required?)+ (mt/test-driver :mongo+ (mt/dataset test-data (mt/db)+ (mongo.connection/with-mongo-client [c (mt/db)]+ (try+ ;; allow connecting with the client cert+ ;; https://www.mongodb.com/docs/manual/tutorial/configure-x509-client-authentication/#add-x-509-certificate-subject-as-a-user+ (mongo.util/run-command (mongo.util/database c "$external")+ {:createUser cert-subject+ :roles [{:role "readWrite" :db "test-data"}]})+ (let [details {:host "localhost"+ :user cert-subject+ :dbname "test-data"+ :additional-options "authMechanism=MONGODB-X509"}+ ssl-details (tdm/conn-details details)]+ (is (driver.u/can-connect-with-details? :mongo ssl-details)+ "should allow connecting with MONGODB-X509"))+ (finally+ (mongo.util/run-command (mongo.util/database c "$external")+ {:dropUser cert-subject}))))))+ (is true "Don't try X509 if mongo isn't configured with the necessary certs")))+(deftest database-supports?-test(mt/test-driver :mongo(doseq [{:keys [dbms_version expected]}diff --git a/test_resources/ssl/mongo/README.md b/test_resources/ssl/mongo/README.mdindex 8bf4ab22c14..70fed578a97 100644--- a/test_resources/ssl/mongo/README.md+++ b/test_resources/ssl/mongo/README.md@@ -14,3 +14,8 @@ certificates needed for the backend tests are copied into the this directory.Although the source of truth is the docker image, the client certificates arechecked in because there are tests like the SSL connection factory tests whichneed valid certificates and keys.++## To test against the server with TLS++ MB_MONGO_TEST_USER=metabase MB_MONGO_TEST_PASSWORD=metasample123 MB_TEST_MONGO_REQUIRES_SSL=1 DRIVERS=mongo clj -X:dev:ci:ee:ee-dev:drivers:drivers-dev:test+diff --git a/test_resources/ssl/mongo/run-server.sh b/test_resources/ssl/mongo/run-server.shindex c6f1081a805..8f8d944d0cd 100755--- a/test_resources/ssl/mongo/run-server.sh+++ b/test_resources/ssl/mongo/run-server.sh@@ -22,23 +22,23 @@ doneshift $((OPTIND-1))[ "${1:-}" = "--" ] && shift-if [ "${ssl}" == "" ]-then+if [ "${ssl}" == "" ]; thenssl="tls"fiparams=()-if [ "${ssl}" == "tls" ]-then+if [ "${ssl}" == "tls" ]; thenparams=(--tlsMode requireTLS --tlsCertificateKeyFile /etc/mongo/metamongo.pem --tlsCAFile /etc/mongo/metaca.crt)fiecho "Running mongod version ${version} in SSL mode ${ssl}"-docker run -d -it --rm -p 27017:27017 --name metamongo metabase/qa-databases:mongo-sample-${version} \+docker run -d -it --rm -p 27017:27017 --name metamongo \+ -e MONGO_INITDB_ROOT_USERNAME=metabase \+ -e MONGO_INITDB_ROOT_PASSWORD=metasample123 \+ metabase/qa-databases:mongo-sample-${version} \mongod --dbpath /data/db2/ "${params[@]}"-for f in metabase.crt metabase.key metaca.crt-do+for f in metabase.crt metabase.key metaca.crt; dodocker cp metamongo:/etc/mongo/$f .done