Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Add MSAL4J dependency for SQL Server (#82511)

metamben3200343d9233
diff --git a/docs/databases/connections/sql-server.md b/docs/databases/connections/sql-server.md
index a3811884ea9..24678ccb45a 100644
--- a/docs/databases/connections/sql-server.md
+++ b/docs/databases/connections/sql-server.md
@@ -95,7 +95,14 @@ A fingerprinting query examines the first 10,000 rows from each column and uses
## Connecting to Azure SQL
-To connect to Azure SQL, you'll need to set the port to 1433.
+To connect to Azure SQL, set the port to 1433. Under **Authentication method**, pick how the server will identify Metabase:
+
+- **SQL Server login**: a traditional username and password. Only works when SQL authentication is enabled on the server.
+- **Azure AD - Service principal**: Metabase authenticates as an [Entra ID app registration](https://learn.microsoft.com/en-us/entra/identity-platform/app-objects-and-service-principals). Fill in the **Client ID** and **Client secret** from the app registration, and grant that service principal `CONNECT` on the target database.
+- **Azure AD - Service principal (certificate)**: Same as service principal, but authenticates with the app registration's certificate. Upload the **Client certificate**.
+ - If the certificate needs a password, provide the password in **Certificate password**.
+ - If the private key is stored in a separate file, provide the key in **Client key**. If the key is encrypted, fill in its password in **Client key password**.
+- **Azure AD - Managed identity**: This mode only works when Metabase runs inside Azure. Metabase authenticates using the [managed identity](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview) assigned to the compute that your Metabase is running on (App Service, Container Apps, VM, AKS). To use the default identity, leave **Managed identity client ID** blank. You can also fill in a specific user-assigned managed identity.
## Writable connection
diff --git a/modules/drivers/sqlserver/deps.edn b/modules/drivers/sqlserver/deps.edn
index b315856af31..3870916c128 100644
--- a/modules/drivers/sqlserver/deps.edn
+++ b/modules/drivers/sqlserver/deps.edn
@@ -2,4 +2,22 @@
["src" "resources"]
:deps
- {com.microsoft.sqlserver/mssql-jdbc {:mvn/version "13.4.0.jre11"}}}
+ {com.microsoft.sqlserver/mssql-jdbc {:mvn/version "13.4.0.jre11"}
+ ;; Required at runtime by mssql-jdbc for Entra ID auth modes: ActiveDirectoryPassword,
+ ;; ActiveDirectoryServicePrincipal, ActiveDirectoryServicePrincipalCertificate, ActiveDirectoryInteractive.
+ ;; mssql-jdbc declares this only transitively via azure-identity as <optional>, so it is not
+ ;; bundled in Metabase's uberjar unless we depend on it explicitly. Version 1.23.1 is what
+ ;; azure-identity 1.18.2 (the version pinned in mssql-jdbc 13.4.0's pom) ships with.
+ com.microsoft.azure/msal4j {:mvn/version "1.23.1"}
+ ;; Required at runtime by mssql-jdbc for ActiveDirectoryManagedIdentity / ActiveDirectoryMSI /
+ ;; ActiveDirectoryDefault. SQLServerSecurityUtility.getManagedIdentityCredAuthToken references
+ ;; com.azure.identity.ManagedIdentityCredential and DefaultAzureCredential from this library.
+ ;; Version 1.18.2 matches what mssql-jdbc 13.4.0's pom pins as its <optional> dependency.
+ ;;
+ ;; azure-core-http-netty is excluded following the snowflake driver's pattern
+ ;; (modules/drivers/snowflake/deps.edn): the full reactor-netty + Netty 4.1.x chain is ~20 MB
+ ;; and only exists to serve azure-core's HTTP pipeline. MI's only HTTP call is a single POST to
+ ;; the IMDS endpoint, which azure-core can serve via the JDK HttpClient transport instead.
+ com.azure/azure-identity {:mvn/version "1.18.2"
+ :exclusions [com.azure/azure-core-http-netty]}
+ com.azure/azure-core-http-jdk-httpclient {:mvn/version "1.0.3"}}}
diff --git a/modules/drivers/sqlserver/resources/metabase/sqlserver/metabase-plugin.yaml b/modules/drivers/sqlserver/resources/metabase/sqlserver/metabase-plugin.yaml
index 209ca269e07..ec2d2de1ef7 100644
--- a/modules/drivers/sqlserver/resources/metabase/sqlserver/metabase-plugin.yaml
+++ b/modules/drivers/sqlserver/resources/metabase/sqlserver/metabase-plugin.yaml
@@ -22,8 +22,72 @@ driver:
- name: instance
display-name: Database instance name
placeholder: N/A
- - user
- - password
+ - name: auth-mode
+ display-name: Authentication method
+ type: select
+ default: sql
+ required: true
+ options:
+ - {name: "SQL Server login", value: "sql"}
+ - {name: "Azure AD - Service principal", value: "ad-service-principal"}
+ - {name: "Azure AD - Service principal (certificate)", value: "ad-service-principal-certificate"}
+ - {name: "Azure AD - Managed identity", value: "ad-managed-identity"}
+ - merge:
+ - user
+ - visible-if:
+ auth-mode: sql
+ - merge:
+ - password
+ - visible-if:
+ auth-mode: sql
+ - name: ad-client-id
+ display-name: Client ID
+ helper-text: The Entra application (client) ID of the service principal.
+ placeholder: 00000000-0000-0000-0000-000000000000
+ required: true
+ visible-if:
+ auth-mode: [ad-service-principal, ad-service-principal-certificate]
+ - name: ad-client-secret
+ display-name: Client secret
+ type: password
+ required: true
+ visible-if:
+ auth-mode: ad-service-principal
+ - name: ad-client-certificate
+ display-name: Client certificate
+ helper-text: The Microsoft Entra service principal certificate.
+ type: secret
+ secret-kind: keystore
+ required: true
+ visible-if:
+ auth-mode: ad-service-principal-certificate
+ - name: ad-client-certificate-password
+ display-name: Certificate password
+ helper-text: If the certificate needs a password, provide it here.
+ type: secret
+ secret-kind: password
+ visible-if:
+ auth-mode: ad-service-principal-certificate
+ - name: ad-client-key
+ display-name: Client key
+ helper-text: Only needed if the private key is in a separate file from the certificate.
+ type: secret
+ secret-kind: pem-cert
+ visible-if:
+ auth-mode: ad-service-principal-certificate
+ - name: ad-client-key-password
+ display-name: Client key password
+ helper-text: Only needed if the separate private key file is encrypted.
+ type: secret
+ secret-kind: password
+ visible-if:
+ auth-mode: ad-service-principal-certificate
+ - name: ad-managed-identity-client-id
+ display-name: Managed identity client ID
+ helper-text: Optional. Fill in for a user-assigned managed identity; leave blank to use the default identity attached to the Metabase host.
+ placeholder: 00000000-0000-0000-0000-000000000000
+ visible-if:
+ auth-mode: ad-managed-identity
- name: role
display-name: Default impersonation user (required for impersonation if the connection's user cannot be impersonated)
helper-text: User used to run system queries, required if the connection's user cannot be impersonated, such as the 'sa' user.
diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
index bbe5f1bdd4a..2eb84edf71f 100644
--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
@@ -226,37 +226,68 @@
(type->database-type base-type))
(defmethod sql-jdbc.conn/connection-details->spec :sqlserver
- [_ {:keys [user password db host port instance domain ssl]
- :or {user "dbuser", password "dbpassword", db "", host "localhost"}
+ [_ {:keys [user password db host port instance domain ssl auth-mode
+ ad-client-id ad-client-secret ad-managed-identity-client-id]
+ :or {user "dbuser", password "dbpassword", db "", host "localhost"
+ auth-mode "sql"}
:as details}]
- (-> {:applicationName driver-api/mb-version-and-process-identifier
- :subprotocol "sqlserver"
- ;; it looks like the only thing that actually needs to be passed as the `subname` is the host; everything else
- ;; can be passed as part of the Properties
- :subname (str "//" host)
- ;; everything else gets passed as `java.util.Properties` to the JDBC connection. (passing these as Properties
- ;; instead of part of the `:subname` is preferable because they support things like passwords with special
- ;; characters)
- :database db
- :password password
- ;; Wait up to 10 seconds for connection success. If we get no response by then, consider the connection failed
- :loginTimeout 10
- ;; apparently specifying `domain` with the official SQLServer driver is done like `user:domain\user` as opposed
- ;; to specifying them separately as with jTDS see also:
- ;; https://social.technet.microsoft.com/Forums/sqlserver/en-US/bc1373f5-cb40-479d-9770-da1221a0bc95/connecting-to-sql-server-in-a-different-domain-using-jdbc-driver?forum=sqldataaccess
- :user (str (when domain (str domain "\\"))
- user)
- :encrypt (boolean ssl)
- ;; only crazy people would want this. See https://docs.microsoft.com/en-us/sql/connect/jdbc/configuring-how-java-sql-time-values-are-sent-to-the-server?view=sql-server-ver15
- :sendTimeAsDatetime false}
- ;; only include `port` if it is specified; leave out for dynamic port: see
- ;; https://github.com/metabase/metabase/issues/7597
- ;; only include `instanceName` if supplied — mssql-jdbc treats an empty string as a named instance and
- ;; initiates SQL Server Browser lookup, which breaks Microsoft Fabric / Synapse serverless endpoints
- ;; that drop the connection whenever the property is present (#81270)
- (merge (when port {:port port})
- (when-not (str/blank? instance) {:instanceName instance}))
- (sql-jdbc.common/handle-additional-options details, :seperator-style :semicolon)))
+ (let [base {:applicationName driver-api/mb-version-and-process-identifier
+ :subprotocol "sqlserver"
+ ;; it looks like the only thing that actually needs to be passed as the `subname` is the host;
+ ;; everything else can be passed as part of the Properties. Passing them as Properties (instead of
+ ;; part of the `:subname`) is preferable because they support things like passwords with special
+ ;; characters.
+ :subname (str "//" host)
+ :database db
+ ;; Wait up to 10 seconds for connection success. If we get no response by then, consider the
+ ;; connection failed.
+ :loginTimeout 10
+ :encrypt (boolean ssl)
+ ;; only crazy people would want this. See
+ ;; https://docs.microsoft.com/en-us/sql/connect/jdbc/configuring-how-java-sql-time-values-are-sent-to-the-server?view=sql-server-ver15
+ :sendTimeAsDatetime false}
+ auth-props (case auth-mode
+ ;; https://learn.microsoft.com/en-us/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver17#connect-using-activedirectoryserviceprincipal-authentication-mode
+ "ad-service-principal"
+ {:authentication "ActiveDirectoryServicePrincipal"
+ :user ad-client-id
+ :password ad-client-secret}
+
+ ;; https://learn.microsoft.com/en-us/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver17#connect-using-activedirectoryserviceprincipalcertificate-authentication-mode
+ "ad-service-principal-certificate"
+ (let [cert-file (driver-api/secret-value-as-file! :sqlserver details "ad-client-certificate")
+ cert-password (driver-api/secret-value-as-string :sqlserver details "ad-client-certificate-password")
+ key-file (driver-api/secret-value-as-file! :sqlserver details "ad-client-key")
+ key-password (driver-api/secret-value-as-string :sqlserver details "ad-client-key-password")]
+ (cond-> {:authentication "ActiveDirectoryServicePrincipalCertificate"
+ :user ad-client-id
+ :clientCertificate cert-file}
+ (not (str/blank? cert-password)) (assoc :password cert-password)
+ key-file (assoc :clientKey key-file)
+ (not (str/blank? key-password)) (assoc :clientKeyPassword key-password)))
+
+ ;; https://learn.microsoft.com/en-us/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver17#connect-using-activedirectorymanagedidentity-authentication-mode
+ ;; Missing :user = default identity; set :user for user-assigned managed identity.
+ "ad-managed-identity"
+ (cond-> {:authentication "ActiveDirectoryManagedIdentity"}
+ (not (str/blank? ad-managed-identity-client-id))
+ (assoc :user ad-managed-identity-client-id))
+
+ ;; "sql" (default), nil (legacy connections without auth-mode), or any unknown value.
+ ;; apparently specifying `domain` with the official SQLServer driver is done like
+ ;; `user:domain\user` as opposed to specifying them separately as with jTDS. See also:
+ ;; https://social.technet.microsoft.com/Forums/sqlserver/en-US/bc1373f5-cb40-479d-9770-da1221a0bc95/connecting-to-sql-server-in-a-different-domain-using-jdbc-driver?forum=sqldataaccess
+ {:user (str (when domain (str domain "\\")) user)
+ :password password})]
+ (-> (merge base auth-props)
+ ;; only include `port` if it is specified; leave out for dynamic port: see
+ ;; https://github.com/metabase/metabase/issues/7597
+ ;; only include `instanceName` if supplied — mssql-jdbc treats an empty string as a named instance and
+ ;; initiates SQL Server Browser lookup, which breaks Microsoft Fabric / Synapse serverless endpoints
+ ;; that drop the connection whenever the property is present (#81270)
+ (merge (when port {:port port})
+ (when-not (str/blank? instance) {:instanceName instance}))
+ (sql-jdbc.common/handle-additional-options details, :seperator-style :semicolon))))
(def ^:private disallowed-additional-opts
#"(?i)(?:socketFactoryClass|socketFactoryConstructorArg|trustManagerClass|trustManagerConstructorArg|accessTokenCallbackClass)")
diff --git a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
index 8d246613050..192538b1bc1 100644
--- a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
+++ b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
@@ -227,6 +227,83 @@
{:user "cam", :password "toucans", :db "birddb",
:host "localhost", :instance "MYINSTANCE"}))))))
+(deftest ^:parallel connection-spec-sql-auth-mode-test
+ (testing "auth-mode 'sql' (explicit or missing) → traditional user/password login, no :authentication key"
+ (doseq [details [{:auth-mode "sql", :user "cam", :password "toucans", :db "birddb", :host "localhost"}
+ ;; missing :auth-mode simulates legacy connections saved before the auth-mode field existed
+ {:user "cam", :password "toucans", :db "birddb", :host "localhost"}]]
+ (testing (str "details = " (pr-str details))
+ (let [spec (sql-jdbc.conn/connection-details->spec :sqlserver details)]
+ (is (= "cam" (:user spec)))
+ (is (= "toucans" (:password spec)))
+ (is (not (contains? spec :authentication))))))))
+
+(deftest ^:parallel connection-spec-ad-service-principal-test
+ (testing "auth-mode 'ad-service-principal' → ActiveDirectoryServicePrincipal, principal id in :user, secret in :password"
+ (let [spec (sql-jdbc.conn/connection-details->spec :sqlserver
+ {:auth-mode "ad-service-principal"
+ :ad-client-id "4e027ad8-5e10-4e05-b5a6-c071b9b61b37"
+ :ad-client-secret "the-secret"
+ :host "sql.example.com"
+ :db "metabase_test"})]
+ (is (= "ActiveDirectoryServicePrincipal" (:authentication spec)))
+ (is (= "4e027ad8-5e10-4e05-b5a6-c071b9b61b37" (:user spec)))
+ (is (= "the-secret" (:password spec))))))
+
+(deftest ^:parallel connection-spec-ad-service-principal-certificate-embedded-key-test
+ (testing "auth-mode 'ad-service-principal-certificate': cert with embedded key, cert password goes in :password"
+ (let [spec (sql-jdbc.conn/connection-details->spec :sqlserver
+ {:auth-mode "ad-service-principal-certificate"
+ :ad-client-id "4e027ad8-5e10-4e05-b5a6-c071b9b61b37"
+ :ad-client-certificate-value "cert-bytes"
+ :ad-client-certificate-password-value "cert-pw"
+ :host "sql.example.com"
+ :db "metabase_test"})]
+ (is (= "ActiveDirectoryServicePrincipalCertificate" (:authentication spec)))
+ (is (= "4e027ad8-5e10-4e05-b5a6-c071b9b61b37" (:user spec)))
+ (is (instance? java.io.File (:clientCertificate spec)))
+ (is (= "cert-pw" (:password spec)))
+ (is (not (contains? spec :clientKey)))
+ (is (not (contains? spec :clientKeyPassword))))))
+
+(deftest ^:parallel connection-spec-ad-service-principal-certificate-separate-key-test
+ (testing "auth-mode 'ad-service-principal-certificate': cert + separate encrypted key file → :clientKey + :clientKeyPassword"
+ (let [spec (sql-jdbc.conn/connection-details->spec :sqlserver
+ {:auth-mode "ad-service-principal-certificate"
+ :ad-client-id "4e027ad8-5e10-4e05-b5a6-c071b9b61b37"
+ :ad-client-certificate-value "cert-bytes"
+ :ad-client-certificate-password-value "cert-pw"
+ :ad-client-key-value "key-bytes"
+ :ad-client-key-password-value "key-pw"
+ :host "sql.example.com"
+ :db "metabase_test"})]
+ (is (instance? java.io.File (:clientCertificate spec)))
+ (is (= "cert-pw" (:password spec)))
+ (is (instance? java.io.File (:clientKey spec)))
+ (is (= "key-pw" (:clientKeyPassword spec))))))
+
+(deftest ^:parallel connection-spec-ad-managed-identity-user-assigned-test
+ (testing "auth-mode 'ad-managed-identity' with :ad-managed-identity-client-id → ActiveDirectoryManagedIdentity, MI id in :user"
+ (let [spec (sql-jdbc.conn/connection-details->spec :sqlserver
+ {:auth-mode "ad-managed-identity"
+ :ad-managed-identity-client-id "4e027ad8-5e10-4e05-b5a6-c071b9b61b37"
+ :host "sql.example.com"
+ :db "metabase_test"})]
+ (is (= "ActiveDirectoryManagedIdentity" (:authentication spec)))
+ (is (= "4e027ad8-5e10-4e05-b5a6-c071b9b61b37" (:user spec))))))
+
+(deftest ^:parallel connection-spec-ad-managed-identity-default-test
+ (testing "auth-mode 'ad-managed-identity' with blank/missing :ad-managed-identity-client-id → default identity, no :user"
+ (doseq [mi-client-id [nil "" " "]]
+ (testing (str ":ad-managed-identity-client-id = " (pr-str mi-client-id))
+ (let [spec (sql-jdbc.conn/connection-details->spec :sqlserver
+ {:auth-mode "ad-managed-identity"
+ :ad-managed-identity-client-id mi-client-id
+ :host "sql.example.com"
+ :db "metabase_test"})]
+ (is (= "ActiveDirectoryManagedIdentity" (:authentication spec)))
+ (is (not (contains? spec :user))))))))
+
(deftest ^:parallel reject-details-with-dangerous-additional-options-test
(mt/test-driver :sqlserver
(let [details (:details (mt/db))]