From e44f4b9ed54b2c17c960704dd77a2ddf92d0b705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cam=20Sa=C3=BCl?= Date: Thu, 30 Mar 2017 17:07:02 -0700 Subject: [PATCH 1/2] Prepend http:// to site-url if it has no protocol when setting --- src/metabase/public_settings.clj | 4 +++- test/metabase/publc_settings_test.clj | 28 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/metabase/public_settings.clj b/src/metabase/public_settings.clj index 35f5ae33027ab..107170404c3ab 100644 --- a/src/metabase/public_settings.clj +++ b/src/metabase/public_settings.clj @@ -23,11 +23,13 @@ :default "Metabase") ;; This value is *guaranteed* to never have a trailing slash :D +;; It will also prepend `http://` to the URL if there's not protocol when it comes in (defsetting site-url "The base URL of this Metabase instance, e.g. \"http://metabase.my-company.com\"." :setter (fn [new-value] (setting/set-string! :site-url (when new-value - (s/replace new-value #"/$" ""))))) + (cond->> (s/replace new-value #"/$" "") + (not (s/starts-with? new-value "http")) (str "http://")))))) (defsetting admin-email "The email address users should be referred to if they encounter a problem.") diff --git a/test/metabase/publc_settings_test.clj b/test/metabase/publc_settings_test.clj index dd97384d940c3..22249289c1791 100644 --- a/test/metabase/publc_settings_test.clj +++ b/test/metabase/publc_settings_test.clj @@ -3,9 +3,35 @@ [metabase.public-settings :as public-settings] [metabase.test.util :as tu])) -;; double-check that setting the `site-url` setting will automatically strip off trailing slashes + ;; double-check that setting the `site-url` setting will automatically strip off trailing slashes (expect "http://localhost:3000" (tu/with-temporary-setting-values [site-url nil] (public-settings/site-url "http://localhost:3000/") (public-settings/site-url))) + + ;; double-check that setting the `site-url` setting will prepend `http://` if no protocol was specified +(expect + "http://localhost:3000" + (tu/with-temporary-setting-values [site-url nil] + (public-settings/site-url "localhost:3000") + (public-settings/site-url))) + +(expect + "http://localhost:3000" + (tu/with-temporary-setting-values [site-url nil] + (public-settings/site-url "localhost:3000") + (public-settings/site-url))) + +(expect + "http://localhost:3000" + (tu/with-temporary-setting-values [site-url nil] + (public-settings/site-url "http://localhost:3000") + (public-settings/site-url))) + +;; if https:// was specified it should keep it +(expect + "https://localhost:3000" + (tu/with-temporary-setting-values [site-url nil] + (public-settings/site-url "https://localhost:3000") + (public-settings/site-url))) From 3eca231b1b7ea95ccd7db5b98c9f62479dbd4152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cam=20Sa=C3=BCl?= Date: Thu, 30 Mar 2017 17:07:25 -0700 Subject: [PATCH 2/2] Check whether Metabase DB is set up before attempting to set site-url in middleware --- src/metabase/db.clj | 9 +++++++-- src/metabase/middleware.clj | 9 +++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/metabase/db.clj b/src/metabase/db.clj index 044bb3f922d20..45e46d40ace92 100644 --- a/src/metabase/db.clj +++ b/src/metabase/db.clj @@ -289,6 +289,11 @@ (def ^:private setup-db-has-been-called? (atom false)) +(defn db-is-setup? + "True if the Metabase DB is setup and ready." + ^Boolean [] + @setup-db-has-been-called?) + (def ^:dynamic *allow-potentailly-unsafe-connections* "We want to make *every* database connection made by the drivers safe -- read-only, only connect if DB file exists, etc. At the same time, we'd like to be able to use driver functionality like `can-connect-with-details?` to check whether we can @@ -360,11 +365,11 @@ [& {:keys [db-details auto-migrate] :or {db-details @db-connection-details auto-migrate true}}] - (reset! setup-db-has-been-called? true) (verify-db-connection db-details) (run-schema-migrations! auto-migrate db-details) (create-connection-pool! (jdbc-details db-details)) - (run-data-migrations!)) + (run-data-migrations!) + (reset! setup-db-has-been-called? true)) (defn setup-db-if-needed! "Call `setup-db!` if DB is not already setup; otherwise this does nothing." diff --git a/src/metabase/middleware.clj b/src/metabase/middleware.clj index df27dac042012..8139cf9baacac 100644 --- a/src/metabase/middleware.clj +++ b/src/metabase/middleware.clj @@ -260,10 +260,11 @@ "Middleware to set the `site-url` Setting if it's unset the first time a request is made." [handler] (fn [{{:strs [origin host] :as headers} :headers, :as request}] - (when-not (public-settings/site-url) - (when-let [site-url (or origin host)] - (log/info "Setting Metabase site URL to" site-url) - (public-settings/site-url site-url))) + (when (mdb/db-is-setup?) + (when-not (public-settings/site-url) + (when-let [site-url (or origin host)] + (log/info "Setting Metabase site URL to" site-url) + (public-settings/site-url site-url)))) (handler request)))