2020-12-24 14:32:19 +01:00
|
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;;
|
2021-04-06 23:25:34 +02:00
|
|
|
;; Copyright (c) UXBOX Labs SL
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(ns app.rpc
|
|
|
|
(:require
|
|
|
|
[app.common.data :as d]
|
2021-01-25 09:29:41 +01:00
|
|
|
[app.common.exceptions :as ex]
|
2020-12-24 14:32:19 +01:00
|
|
|
[app.common.spec :as us]
|
|
|
|
[app.db :as db]
|
2021-05-14 12:38:28 +02:00
|
|
|
[app.loggers.audit :as audit]
|
2020-12-24 14:32:19 +01:00
|
|
|
[app.metrics :as mtx]
|
2021-01-25 09:29:41 +01:00
|
|
|
[app.rlimits :as rlm]
|
2021-04-06 23:25:34 +02:00
|
|
|
[app.util.logging :as l]
|
2020-12-24 14:32:19 +01:00
|
|
|
[app.util.services :as sv]
|
|
|
|
[clojure.spec.alpha :as s]
|
|
|
|
[cuerdas.core :as str]
|
|
|
|
[integrant.core :as ig]))
|
|
|
|
|
|
|
|
(defn- default-handler
|
2021-01-31 19:25:26 +01:00
|
|
|
[_]
|
2020-12-24 14:32:19 +01:00
|
|
|
(ex/raise :type :not-found))
|
|
|
|
|
2021-02-22 12:48:21 +01:00
|
|
|
(defn- run-hook
|
|
|
|
[hook-fn response]
|
|
|
|
(ex/ignoring (hook-fn))
|
|
|
|
response)
|
|
|
|
|
2020-12-24 14:32:19 +01:00
|
|
|
(defn- rpc-query-handler
|
|
|
|
[methods {:keys [profile-id] :as request}]
|
|
|
|
(let [type (keyword (get-in request [:path-params :type]))
|
2021-04-13 16:29:38 +02:00
|
|
|
|
|
|
|
data (d/merge (:params request)
|
|
|
|
(:body-params request)
|
|
|
|
(:uploads request))
|
|
|
|
|
2020-12-24 14:32:19 +01:00
|
|
|
data (if profile-id
|
|
|
|
(assoc data :profile-id profile-id)
|
|
|
|
(dissoc data :profile-id))
|
2021-04-13 16:29:38 +02:00
|
|
|
|
2020-12-24 14:32:19 +01:00
|
|
|
result ((get methods type default-handler) data)
|
|
|
|
mdata (meta result)]
|
|
|
|
|
|
|
|
(cond->> {:status 200 :body result}
|
2021-03-05 17:30:49 +01:00
|
|
|
(fn? (:transform-response mdata))
|
|
|
|
((:transform-response mdata) request))))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defn- rpc-mutation-handler
|
|
|
|
[methods {:keys [profile-id] :as request}]
|
|
|
|
(let [type (keyword (get-in request [:path-params :type]))
|
|
|
|
data (d/merge (:params request)
|
|
|
|
(:body-params request)
|
|
|
|
(:uploads request))
|
|
|
|
data (if profile-id
|
|
|
|
(assoc data :profile-id profile-id)
|
|
|
|
(dissoc data :profile-id))
|
|
|
|
result ((get methods type default-handler) data)
|
|
|
|
mdata (meta result)]
|
|
|
|
(cond->> {:status 200 :body result}
|
2021-02-22 12:48:21 +01:00
|
|
|
(fn? (:transform-response mdata))
|
|
|
|
((:transform-response mdata) request)
|
|
|
|
|
|
|
|
(fn? (:before-complete mdata))
|
|
|
|
(run-hook (:before-complete mdata)))))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
2021-01-25 09:29:41 +01:00
|
|
|
(defn- wrap-with-metrics
|
2021-01-25 20:14:40 +01:00
|
|
|
[cfg f mdata]
|
|
|
|
(mtx/wrap-summary f (::mobj cfg) [(::sv/name mdata)]))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
2021-01-25 09:29:41 +01:00
|
|
|
;; Wrap the rpc handler with a semaphore if it is specified in the
|
|
|
|
;; metadata asocciated with the handler.
|
|
|
|
(defn- wrap-with-rlimits
|
|
|
|
[cfg f mdata]
|
|
|
|
(if-let [key (:rlimit mdata)]
|
|
|
|
(let [rlinst (get-in cfg [:rlimits key])]
|
|
|
|
(when-not rlinst
|
|
|
|
(ex/raise :type :internal
|
|
|
|
:code :rlimit-not-configured
|
|
|
|
:hint (str/fmt "%s rlimit not configured" key)))
|
2021-04-06 23:25:34 +02:00
|
|
|
(l/trace :action "add rlimit"
|
|
|
|
:handler (::sv/name mdata))
|
2021-01-25 09:29:41 +01:00
|
|
|
(fn [cfg params]
|
|
|
|
(rlm/execute rlinst (f cfg params))))
|
|
|
|
f))
|
|
|
|
|
2021-05-09 14:06:27 +02:00
|
|
|
|
2021-01-25 09:29:41 +01:00
|
|
|
(defn- wrap-impl
|
2021-05-14 12:38:28 +02:00
|
|
|
[{:keys [audit] :as cfg} f mdata]
|
2021-05-09 14:06:27 +02:00
|
|
|
(let [f (wrap-with-rlimits cfg f mdata)
|
|
|
|
f (wrap-with-metrics cfg f mdata)
|
|
|
|
spec (or (::sv/spec mdata) (s/spec any?))
|
|
|
|
auth? (:auth mdata true)]
|
2021-05-14 12:38:28 +02:00
|
|
|
|
|
|
|
(l/trace :action "register" :name (::sv/name mdata))
|
2020-12-24 14:32:19 +01:00
|
|
|
(fn [params]
|
2021-05-09 14:06:27 +02:00
|
|
|
(when (and auth? (not (uuid? (:profile-id params))))
|
2021-01-08 14:12:56 +01:00
|
|
|
(ex/raise :type :authentication
|
|
|
|
:code :authentication-required
|
2021-01-08 14:31:24 +01:00
|
|
|
:hint "authentication required for this endpoint"))
|
2021-05-09 14:06:27 +02:00
|
|
|
(let [params (us/conform spec params)
|
|
|
|
result (f cfg params)
|
2021-05-14 12:38:28 +02:00
|
|
|
resultm (meta result)]
|
|
|
|
(when (and (::type cfg) (fn? audit))
|
|
|
|
(let [profile-id (or (:profile-id params)
|
|
|
|
(:profile-id result)
|
|
|
|
(::audit/profile-id resultm))
|
|
|
|
props (d/merge params (::audit/props resultm))]
|
|
|
|
(audit :submit {:type (::type cfg)
|
|
|
|
:name (::sv/name mdata)
|
|
|
|
:profile-id profile-id
|
2021-05-19 23:05:05 +02:00
|
|
|
:props props})))
|
2021-05-09 14:06:27 +02:00
|
|
|
result))))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defn- process-method
|
2021-01-25 20:14:40 +01:00
|
|
|
[cfg vfn]
|
2020-12-24 14:32:19 +01:00
|
|
|
(let [mdata (meta vfn)]
|
|
|
|
[(keyword (::sv/name mdata))
|
2021-01-25 20:14:40 +01:00
|
|
|
(wrap-impl cfg (deref vfn) mdata)]))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defn- resolve-query-methods
|
|
|
|
[cfg]
|
2021-01-25 20:14:40 +01:00
|
|
|
(let [mobj (mtx/create
|
|
|
|
{:name "rpc_query_timing"
|
|
|
|
:labels ["name"]
|
|
|
|
:registry (get-in cfg [:metrics :registry])
|
2021-02-22 12:52:53 +01:00
|
|
|
:type :histogram
|
2021-01-25 20:14:40 +01:00
|
|
|
:help "Timing of query services."})
|
2021-05-14 12:38:28 +02:00
|
|
|
cfg (assoc cfg ::mobj mobj ::type "query")]
|
2021-01-25 20:14:40 +01:00
|
|
|
(->> (sv/scan-ns 'app.rpc.queries.projects
|
|
|
|
'app.rpc.queries.files
|
|
|
|
'app.rpc.queries.teams
|
|
|
|
'app.rpc.queries.comments
|
|
|
|
'app.rpc.queries.profile
|
|
|
|
'app.rpc.queries.recent-files
|
2021-04-13 16:29:38 +02:00
|
|
|
'app.rpc.queries.viewer
|
2021-04-29 13:04:19 +02:00
|
|
|
'app.rpc.queries.fonts
|
2021-04-13 16:29:38 +02:00
|
|
|
'app.rpc.queries.svg)
|
2021-01-25 20:14:40 +01:00
|
|
|
(map (partial process-method cfg))
|
|
|
|
(into {}))))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defn- resolve-mutation-methods
|
|
|
|
[cfg]
|
2021-01-25 20:14:40 +01:00
|
|
|
(let [mobj (mtx/create
|
|
|
|
{:name "rpc_mutation_timing"
|
|
|
|
:labels ["name"]
|
|
|
|
:registry (get-in cfg [:metrics :registry])
|
2021-02-22 12:52:53 +01:00
|
|
|
:type :histogram
|
2021-01-25 20:14:40 +01:00
|
|
|
:help "Timing of mutation services."})
|
2021-05-14 12:38:28 +02:00
|
|
|
cfg (assoc cfg ::mobj mobj ::type "mutation")]
|
2021-01-25 20:14:40 +01:00
|
|
|
(->> (sv/scan-ns 'app.rpc.mutations.demo
|
|
|
|
'app.rpc.mutations.media
|
|
|
|
'app.rpc.mutations.profile
|
|
|
|
'app.rpc.mutations.files
|
|
|
|
'app.rpc.mutations.comments
|
|
|
|
'app.rpc.mutations.projects
|
|
|
|
'app.rpc.mutations.viewer
|
|
|
|
'app.rpc.mutations.teams
|
2021-02-25 14:54:00 +01:00
|
|
|
'app.rpc.mutations.management
|
2021-02-18 14:07:13 +01:00
|
|
|
'app.rpc.mutations.ldap
|
2021-04-29 13:04:19 +02:00
|
|
|
'app.rpc.mutations.fonts
|
2021-01-25 20:14:40 +01:00
|
|
|
'app.rpc.mutations.verify-token)
|
|
|
|
(map (partial process-method cfg))
|
|
|
|
(into {}))))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(s/def ::storage some?)
|
|
|
|
(s/def ::session map?)
|
|
|
|
(s/def ::tokens fn?)
|
2021-05-14 12:38:28 +02:00
|
|
|
(s/def ::audit (s/nilable fn?))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defmethod ig/pre-init-spec ::rpc [_]
|
2021-05-14 12:38:28 +02:00
|
|
|
(s/keys :req-un [::storage ::session ::tokens ::audit
|
2021-05-09 14:06:27 +02:00
|
|
|
::mtx/metrics ::rlm/rlimits ::db/pool]))
|
2020-12-24 14:32:19 +01:00
|
|
|
|
|
|
|
(defmethod ig/init-key ::rpc
|
|
|
|
[_ cfg]
|
|
|
|
(let [mq (resolve-query-methods cfg)
|
|
|
|
mm (resolve-mutation-methods cfg)]
|
|
|
|
{:methods {:query mq :mutation mm}
|
|
|
|
:query-handler #(rpc-query-handler mq %)
|
|
|
|
:mutation-handler #(rpc-mutation-handler mm %)}))
|