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
Implement connection impersonation for starburst (#64972)
Riley Thompsonaf1be91a28f4
diff --git a/modules/drivers/starburst/resources/metabase-plugin.yaml b/modules/drivers/starburst/resources/metabase-plugin.yamlindex 63fe86b2d60..302f43b0394 100644--- a/modules/drivers/starburst/resources/metabase-plugin.yaml+++ b/modules/drivers/starburst/resources/metabase-plugin.yaml@@ -34,13 +34,6 @@ driver:display-name: Role (optional)required: falsehelper-text: Specify a role to override the database user’s default role.- - name: impersonation- display-name: User impersonation- type: boolean- required: false- description: Impersonate end user when running queries- visible-if:- never-visible: true- name: prepared-optimizeddisplay-name: Optimized prepared statementstype: booleandiff --git a/modules/drivers/starburst/src/metabase/driver/starburst.clj b/modules/drivers/starburst/src/metabase/driver/starburst.cljindex 99a986856b7..ec9a9c25f1a 100644--- a/modules/drivers/starburst/src/metabase/driver/starburst.clj+++ b/modules/drivers/starburst/src/metabase/driver/starburst.clj@@ -21,6 +21,7 @@[metabase.driver.sql.query-processor :as sql.qp][metabase.driver.sql.query-processor.util :as sql.qp.u][metabase.driver.sql.util :as sql.u]+ [metabase.util :as u][metabase.util.date-2 :as u.date][metabase.util.honey-sql-2 :as h2x][metabase.util.i18n :refer [trs]]@@ -66,7 +67,8 @@:connection/multiple-databases true:metadata/key-constraints false:now true- :database-routing true}]+ :database-routing true+ :connection-impersonation true}](defmethod driver/database-supports? [:starburst feature] [_ _ _] supported?))(defn- format-field@@ -553,6 +555,16 @@db-or-id-or-specoptions(fn [^java.sql.Connection conn]+ (when-let [db (cond+ ;; id?+ (integer? db-or-id-or-spec)+ (driver-api/with-metadata-provider db-or-id-or-spec+ (driver-api/database (driver-api/metadata-provider)))+ ;; db?+ (u/id db-or-id-or-spec) db-or-id-or-spec+ ;; otherwise it's a spec and we can't get the db+ :else nil)]+ (sql-jdbc.execute/set-role-if-supported! driver conn db))(try(sql-jdbc.execute/set-best-transaction-level! driver conn)(let [underlying-conn (pooled-conn->starburst-conn conn)]@@ -650,19 +662,6 @@;;; | SQL Statment Operations |;;; +----------------------------------------------------------------------------------------------------------------+-(defn- impersonate-user- [^Connection conn]- (when (str/includes? (.getProperty (.getClientInfo conn) "ClientInfo" "") "impersonate:true")- #_{:clj-kondo/ignore [:deprecated-var]}- (let [email (get (deref (driver-api/current-user)) :email)]- (log/info "[starburst] Using legacy impersonation.")- (.setSessionUser ^TrinoConnection (.unwrap conn TrinoConnection) email))))--(defn- remove-impersonation- [^Connection conn]- (when (str/includes? (.getProperty (.getClientInfo conn) "ClientInfo" "") "impersonate:true")- (.clearSessionUser ^TrinoConnection (.unwrap conn TrinoConnection))))-; Metabase tests require a specific error when an invalid number of parameters are passed(defn- handle-execution-error[^Exception e]@@ -688,19 +687,15 @@; In the end, the exact same message is presented to the user when the number of arguments is; incorrect except we now execute the query to display the error message(defn- proxy-optimized-prepared-statement- [driver ^Connection conn ^PreparedStatement stmt params]+ [driver ^Connection _conn ^PreparedStatement stmt params](let [ps (proxy [java.sql.PreparedStatement] [](executeQuery [](try- (let [rs (.executeQuery stmt)]- (remove-impersonation conn)- rs)+ (.executeQuery stmt)(catch Throwable e (handle-execution-error e))))(execute [](try- (let [rs (.execute stmt)]- (remove-impersonation conn)- rs)+ (.execute stmt)(catch Throwable e (handle-execution-error e))))(setMaxRows [nb] (.setMaxRows stmt nb))(setObject@@ -735,20 +730,16 @@; Default prepared statement where set-parameters! is called before generating the proxy(defn- proxy-prepared-statement- [driver ^Connection conn ^PreparedStatement stmt params]+ [driver ^Connection _conn ^PreparedStatement stmt params](sql-jdbc.execute/set-parameters! driver stmt params)(proxy [java.sql.PreparedStatement] [](executeQuery [](try- (let [rs (.executeQuery stmt)]- (remove-impersonation conn)- rs)+ (.executeQuery stmt)(catch Throwable e (handle-execution-error e))))(execute [](try- (let [rs (.execute stmt)]- (remove-impersonation conn)- rs)+ (.execute stmt)(catch Throwable e (handle-execution-error e))))(setMaxRows [nb] (.setMaxRows stmt nb))(cancel [] (.cancel stmt))@@ -759,7 +750,6 @@[driver ^Connection conn ^String sql params];; with starburst driver, result set holdability must be HOLD_CURSORS_OVER_COMMIT;; defining this method simply to omit setting the holdability- (impersonate-user conn)(let [stmt (.prepareStatement connsqlResultSet/TYPE_FORWARD_ONLY@@ -773,14 +763,12 @@(proxy-prepared-statement driver conn stmt params)(proxy-optimized-prepared-statement driver conn stmt params))(catch Throwable e- (remove-impersonation conn)(.close stmt)(throw e)))))(defmethod sql-jdbc.execute/statement :starburst[_ ^Connection conn];; and similarly for statement (do not set holdability)- (impersonate-user conn)(let [stmt (.createStatement connResultSet/TYPE_FORWARD_ONLYResultSet/CONCUR_READ_ONLY)]@@ -793,8 +781,7 @@(try(let [rs (.execute stmt sql)]rs)- (catch Throwable e (handle-execution-error e))- (finally (remove-impersonation conn))))+ (catch Throwable e (handle-execution-error e))))(getResultSet [] (.getResultSet stmt))(setMaxRows [nb] (.setMaxRows stmt nb))(cancel [] (.cancel stmt))@@ -941,15 +928,6 @@(str v)v))-; The role defined in the database should be ignored only if:-; - The impersonation flag is checked AND-; - The current user is NOT the database user-(defn- remove-role? [details-map]- (and- (:impersonation details-map)- #_{:clj-kondo/ignore [:deprecated-var]}- (not= (get (deref (driver-api/current-user)) :email) (:user details-map))))-(defmethod sql-jdbc.conn/connection-details->spec :starburst[_ details-map](let [props (-> details-map@@ -965,9 +943,7 @@"Metabase %s [%s]"(:tag driver-api/mb-version-info "")driver-api/local-process-uuid))- (cond-> (:impersonation details-map) (assoc :clientInfo "impersonate:true"))(cond-> (:prepared-optimized details-map) (assoc :explicitPrepare "false"))- (dissoc (if (remove-role? details-map) :roles :test));; remove any Metabase specific properties that are not recognized by the starburst JDBC driver, which is;; very picky about properties (throwing an error if any are unrecognized)@@ -1015,18 +991,14 @@[_]:monday)-(defmethod driver.sql/set-role-statement :starburst- [_driver role]- (let [special-chars-pattern #"[^a-zA-Z0-9_]"- needs-quote (re-find special-chars-pattern role)]- (if needs-quote- (format "SET SESSION AUTHORIZATION \"%s\";" role)- (format "SET SESSION AUTHORIZATION %s;" role))))-(defmethod driver.sql/default-database-role :starburst[_driver database](get-in database [:details :user]))+(defmethod driver/set-role! :starburst+ [_driver ^Connection conn role]+ (.setSessionUser ^TrinoConnection (.unwrap conn TrinoConnection) role))+(defmethod sql.qp/->honeysql [:starburst ::sql.qp/cast-to-text][driver [_ expr]](sql.qp/->honeysql driver [::sql.qp/cast expr "varchar"]))diff --git a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj b/modules/drivers/starburst/test/metabase/driver/starburst_test.cljindex 730f4382b7b..706f1c30957 100644--- a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj+++ b/modules/drivers/starburst/test/metabase/driver/starburst_test.clj@@ -3,7 +3,6 @@[clojure.string :as str][clojure.test :refer :all][java-time.api :as t]- [metabase.api.common :as api][metabase.driver :as driver][metabase.driver.common.table-rows-sample :as table-rows-sample][metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]@@ -315,17 +314,6 @@(testing description(is (= [expected (- expected)] (query x y unit))))))))))-(deftest impersonation-properties-test- (testing "Impersonation related properties are set correctly"- (let [details {:host "starburst-server"- :port 7778- :catalog "my-catalog"- :ssl true- :impersonation true}- jdbc-spec (sql-jdbc.conn/connection-details->spec :starburst details)]- (is (= "impersonate:true"- (:clientInfo jdbc-spec))))))-(defn prepared-statements-helper[prepared-optimized](let [details (merge (:details (mt/db))@@ -371,55 +359,6 @@(prepared-statements-helper true)(prepared-statements-helper false))))-(deftest impersonation-query- (mt/test-driver :starburst- (testing "Make sure the right credentials are used depending on the impersonation checkbox"- (binding [api/*current-user* (atom {:email "[email protected]"})]- ;; By default the starburst user should the user defined in the database connection, i.e. "metabase"- (is (= [["metabase"]]- (mt/rows- (qp/process-query- (mt/native-query {:query "SELECT current_user"})))))-- (let [details (assoc (:details (mt/db)) :impersonation true)]- (t2.with-temp/with-temp [:model/Database db {:engine :starburst, :name "Temp starburst JDBC Schema DB", :details details}]- (mt/with-db db- (is (= [["[email protected]"]]- (mt/rows- (qp/process-query- (mt/native-query {:query "SELECT current_user"}))))))))-- ;; If impersonation is set, then the starburst user should be the current Metabase user, i.e. [email protected]- ;; The role is ignored as Metabase users may not have the role defined in the database connection- ;; Because database user = metabase user, the role is passed in the starburst query- ;; This is expected to fail as the starburst container doesn't support roles- (let [details (assoc (:details (mt/db))- :user "[email protected]"- :roles "sysadmin"- :impersonation true)]- (t2.with-temp/with-temp [:model/Database db {:engine :starburst, :name "Temp starburst JDBC Schema DB", :details details}]- (mt/with-db db- (is (thrown-with-msg?- Exception- #"Access Denied: Cannot set role sysadmin"- (qp/process-query- (mt/native-query {:query "SELECT current_user"})))))))-- ;; With impersonation disabled the role is passed in the starburst query- ;; This is expected to fail as the starburst container doesn't support roles- (let [details (assoc (:details (mt/db))- :user "[email protected]"- :user "admin"- :roles "sysadmin"- :impersonation false)]- (t2.with-temp/with-temp [:model/Database db {:engine :starburst, :name "Temp starburst JDBC Schema DB", :details details}]- (mt/with-db db- (is (thrown-with-msg?- Exception- #"Access Denied: Cannot set role sysadmin"- (qp/process-query- (mt/native-query {:query "SELECT current_user"})))))))))))-(deftest optimized-prepared-statement-is-closed-test(mt/test-driver :starburst(testing "can check isClosed on optimized prepared statement"