From cf923164c5a2f2bb5097afaa32c32e3faa5cf748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cam=20Sa=C3=BCl?= <cammsaul@gmail.com> Date: Wed, 30 Sep 2015 11:28:13 -0700 Subject: [PATCH] Combine middleware namespaces since they're all fairly small. --- src/metabase/api/routes.clj | 4 +-- src/metabase/core.clj | 53 +++++++++++++++---------------- src/metabase/middleware.clj | 2 +- test/metabase/api/notify_test.clj | 6 ++-- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/metabase/api/routes.clj b/src/metabase/api/routes.clj index 1125a457d30..f1fee83d1f5 100644 --- a/src/metabase/api/routes.clj +++ b/src/metabase/api/routes.clj @@ -21,11 +21,11 @@ (def ^:private +apikey "Wrap API-ROUTES so they may only be accessed with proper apikey credentials." - middlware/enforce-api-key) + middleware/enforce-api-key) (def ^:private +auth "Wrap API-ROUTES so they may only be accessed with proper authentiaction credentials." - middlware/enforce-authentication) + middleware/enforce-authentication) (defroutes routes (context "/activity" [] (+auth activity/routes)) diff --git a/src/metabase/core.clj b/src/metabase/core.clj index 0086c7b9723..61e8b5e1ea5 100644 --- a/src/metabase/core.clj +++ b/src/metabase/core.clj @@ -1,8 +1,7 @@ ;; -*- comment-column: 35; -*- (ns metabase.core (:gen-class) - (:require [clojure.java.browse :refer [browse-url]] - [clojure.string :as s] + (:require [clojure.string :as s] [clojure.tools.logging :as log] [colorize.core :as color] [ring.adapter.jetty :as ring-jetty] @@ -13,17 +12,15 @@ [keyword-params :refer [wrap-keyword-params]] [params :refer [wrap-params]] [session :refer [wrap-session]]) - [medley.core :as medley] + [medley.core :as m] (metabase [config :as config] [db :as db] [driver :as driver] [events :as events] + [middleware :as mb-middleware] [routes :as routes] [setup :as setup] [task :as task]) - (metabase.middleware [auth :as auth] - [log-api-call :refer :all] - [format :refer :all]) (metabase.models [setting :refer [defsetting]] [database :refer [Database]] [user :refer [User]]))) @@ -54,21 +51,21 @@ (def app "The primary entry point to the HTTP server" (-> routes/routes - (log-api-call :request :response) - add-security-headers ; [METABASE] Add HTTP headers to API responses to prevent them from being cached - format-response ; [METABASE] Do formatting before converting to JSON so serializer doesn't barf - (wrap-json-body ; extracts json POST body and makes it avaliable on request + (mb-middleware/log-api-call :request :response) + mb-middleware/add-security-headers ; [METABASE] Add HTTP headers to API responses to prevent them from being cached + mb-middleware/format-response ; [METABASE] Do formatting before converting to JSON so serializer doesn't barf + (wrap-json-body ; extracts json POST body and makes it avaliable on request {:keywords? true}) - wrap-json-response ; middleware to automatically serialize suitable objects as JSON in responses - wrap-keyword-params ; converts string keys in :params to keyword keys - wrap-params ; parses GET and POST params as :query-params/:form-params and both as :params - auth/bind-current-user ; Binds *current-user* and *current-user-id* if :metabase-user-id is non-nil - auth/wrap-current-user-id ; looks for :metabase-session-id and sets :metabase-user-id if Session ID is valid - auth/wrap-api-key ; looks for a Metabase API Key on the request and assocs as :metabase-api-key - auth/wrap-session-id ; looks for a Metabase Session ID and assoc as :metabase-session-id - wrap-cookies ; Parses cookies in the request map and assocs as :cookies - wrap-session ; reads in current HTTP session and sets :session/key - wrap-gzip)) ; GZIP response if client can handle it + wrap-json-response ; middleware to automatically serialize suitable objects as JSON in responses + wrap-keyword-params ; converts string keys in :params to keyword keys + wrap-params ; parses GET and POST params as :query-params/:form-params and both as :params + mb-middleware/bind-current-user ; Binds *current-user* and *current-user-id* if :metabase-user-id is non-nil + mb-middleware/wrap-current-user-id ; looks for :metabase-session-id and sets :metabase-user-id if Session ID is valid + mb-middleware/wrap-api-key ; looks for a Metabase API Key on the request and assocs as :metabase-api-key + mb-middleware/wrap-session-id ; looks for a Metabase Session ID and assoc as :metabase-session-id + wrap-cookies ; Parses cookies in the request map and assocs as :cookies + wrap-session ; reads in current HTTP session and sets :session/key + wrap-gzip)) ; GZIP response if client can handle it (defn- -init-create-setup-token "Create and set a new setup token, and open the setup URL on the user's system." @@ -129,14 +126,14 @@ "Start the embedded Jetty web server." [] (when-not @jetty-instance - (let [jetty-config (cond-> (medley/filter-vals identity {:port (config/config-int :mb-jetty-port) - :host (config/config-str :mb-jetty-host) - :max-threads (config/config-int :mb-jetty-maxthreads) - :min-threads (config/config-int :mb-jetty-minthreads) - :max-queued (config/config-int :mb-jetty-maxqueued) - :max-idle-time (config/config-int :mb-jetty-maxidletime)}) - (config/config-str :mb-jetty-join) (assoc :join? (config/config-bool :mb-jetty-join)) - (config/config-str :mb-jetty-daemon) (assoc :daemon? (config/config-bool :mb-jetty-daemon)))] + (let [jetty-config (cond-> (m/filter-vals identity {:port (config/config-int :mb-jetty-port) + :host (config/config-str :mb-jetty-host) + :max-threads (config/config-int :mb-jetty-maxthreads) + :min-threads (config/config-int :mb-jetty-minthreads) + :max-queued (config/config-int :mb-jetty-maxqueued) + :max-idle-time (config/config-int :mb-jetty-maxidletime)}) + (config/config-str :mb-jetty-join) (assoc :join? (config/config-bool :mb-jetty-join)) + (config/config-str :mb-jetty-daemon) (assoc :daemon? (config/config-bool :mb-jetty-daemon)))] (log/info "Launching Embedded Jetty Webserver with config:\n" (with-out-str (clojure.pprint/pprint jetty-config))) (->> (ring-jetty/run-jetty app jetty-config) (reset! jetty-instance))))) diff --git a/src/metabase/middleware.clj b/src/metabase/middleware.clj index cdd46cd0390..2432a1df06b 100644 --- a/src/metabase/middleware.clj +++ b/src/metabase/middleware.clj @@ -12,7 +12,7 @@ [metabase.db :refer [sel]] (metabase.models [interface :refer [api-serialize]] [session :refer [Session]] - [user :refer [User current-user-fields]]) + [user :refer [User]]) [metabase.util :as u])) ;;; # ------------------------------------------------------------ UTIL FNS ------------------------------------------------------------ diff --git a/test/metabase/api/notify_test.clj b/test/metabase/api/notify_test.clj index 26c58f18488..bf937c6ee31 100644 --- a/test/metabase/api/notify_test.clj +++ b/test/metabase/api/notify_test.clj @@ -1,15 +1,15 @@ (ns metabase.api.notify-test (:require [clj-http.lite.client :as client] [expectations :refer :all] - (metabase. [http-client :as http] - [middleware :as middleware]))) + (metabase [http-client :as http] + [middleware :as middleware]))) ;; ## /api/notify/* AUTHENTICATION Tests ;; We assume that all endpoints for a given context are enforced by the same middleware, so we don't run the same ;; authentication test on every single individual endpoint -(expect (get middlware/response-forbidden :body) (http/client :post 403 "notify/db/100")) +(expect (get middleware/response-forbidden :body) (http/client :post 403 "notify/db/100")) ;; ## POST /api/notify/db/:id -- GitLab