Skip to content

Commit

Permalink
Merge pull request #58 from AtmosSystem/logging
Browse files Browse the repository at this point in the history
Logging
  • Loading branch information
xerp committed Jul 3, 2019
2 parents 1fea3ab + 6a4f4a2 commit ca922c8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
4 changes: 2 additions & 2 deletions project.clj
@@ -1,4 +1,4 @@
(defproject atmos-kernel "0.7.0"
(defproject atmos-kernel "0.7.5"
:description "Core of Atmos System"
:url "https://github.com/AtmosSystem/Kernel"
:license {:name "Eclipse Public License"
Expand All @@ -18,7 +18,7 @@
[ring/ring-codec "1.1.2"]
[ring-cors "0.1.12"]
;authorization-deps
[buddy/buddy-auth "2.1.0"]
[buddy/buddy-auth "2.2.0"]
;logs-deps
[ch.qos.logback/logback-classic "1.2.3"]]
:repositories [["releases" {:url "https://clojars.org/repo"
Expand Down
8 changes: 4 additions & 4 deletions src/atmos_kernel/configuration.clj
@@ -1,12 +1,12 @@
(ns atmos-kernel.configuration
(:require [aero.core :refer [read-config]]))

(def ^:private resource-dir "resources")
(:require [aero.core :refer [read-config]]
[clojure.java.io :as io]))

(defn read-resource
"Load a resource configuration"
[file extension]
(read-config (str resource-dir "/" (name file) "." (name extension))))
(let [source (io/resource (str (name file) "." (name extension)))]
(read-config source)))

(defn read-edn
"Load a edn resource"
Expand Down
25 changes: 23 additions & 2 deletions src/atmos_kernel/core.clj
@@ -1,4 +1,5 @@
(ns atmos-kernel.core)
(ns atmos-kernel.core
(:import (org.slf4j LoggerFactory)))

(defn keyword-map
"Convert the keys of map (and subsequent maps) to clojure keyword"
Expand All @@ -22,4 +23,24 @@
(defn throw-exception
"Throw an exception"
[message cause]
(throw (ex-info message {:cause cause})))
(throw (ex-info message {:cause cause})))


(defn log
[logger-name log-type log-data]
(let [logger (LoggerFactory/getLogger (name logger-name))
log-type (keyword log-type)
log-data (str log-data)]
(case log-type
:info (.info logger log-data)
:debug (.debug logger log-data)
:trace (.trace logger log-data)
:warn (.warn logger log-data)
:error (.error logger log-data)
logger)))

(defmacro log-data
[logger-name log-type data & body]
`(do
(log ~logger-name ~log-type ~data)
~@body))
3 changes: 1 addition & 2 deletions src/atmos_kernel/web/ring.clj
Expand Up @@ -5,7 +5,6 @@
[buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]
[ring.middleware.defaults :refer [wrap-defaults]]))


(defn wrap-secure-json-web-api
"Wrap a JSON Web API"
[routes options auth-backend]
Expand All @@ -20,6 +19,6 @@

(defmacro def-json-web-api
([name routes options]
`(def ~name (wrap-secure-json-web-api ~routes ~options nil)))
`(def-json-web-api ~name ~routes ~options nil))
([name routes options auth-backend]
`(def ~name (wrap-secure-json-web-api ~routes ~options ~auth-backend))))
16 changes: 10 additions & 6 deletions src/atmos_kernel/web/route.clj
@@ -1,5 +1,6 @@
(ns atmos-kernel.web.route
(:require [clojure.string :refer [join lower-case]]
[atmos-kernel.core :refer [log-data]]
[atmos-kernel.web.security.auth :refer [handle-request]]
[atmos-kernel.web.response :refer [atmos-response handle-exception]]
[compojure.core :refer [GET POST PUT DELETE]]))
Expand All @@ -10,19 +11,22 @@
([http-method authentication-needed? route-path args body]
(let [route-path (let [route-path (join "/" route-path)]
(str "/" (if-not (empty? route-path) route-path)))
request (if (vector? args) (last (conj args :as 'request)) 'request)]
args (if (vector? args) (conj args :as 'request) args)
request-obj (if (vector? args) (last args) 'request)]

`(~http-method ~route-path ~args
(try
(handle-request ~request ~authentication-needed?
(handle-request ~request-obj ~authentication-needed?
(try
(atmos-response ~body)

(catch Exception e#
(handle-exception e# ~request))))
(catch Exception inner-exception#
(log-data :atmos-kernel :error inner-exception#
(handle-exception inner-exception# ~request-obj)))))

(catch Exception ex#
(handle-exception ex# ~request)))))))
(catch Exception external-exception#
(log-data :atmos-kernel :warn external-exception#
(handle-exception external-exception# ~request-obj))))))))

(defmacro atmos-GET
[path args body & {:keys [authentication-needed?]
Expand Down
26 changes: 17 additions & 9 deletions src/atmos_kernel/web/security/auth.clj
@@ -1,7 +1,8 @@
(ns atmos-kernel.web.security.auth
(:require [buddy.auth.backends :as backends]
[buddy.auth :refer [authenticated? throw-unauthorized]]
[atmos-kernel.configuration :refer [read-edn]]))
[atmos-kernel.configuration :refer [read-edn]]
[atmos-kernel.core :refer [log-data]]))

(def basic-auth backends/basic)
(def session-auth backends/session)
Expand All @@ -19,18 +20,25 @@
(defprotocol AuthHandlerProtocol
(get-authentication [request auth-data]))

(defn- auth-backend-fn
[request auth-data]
(get-authentication request auth-data))

(defn atmos-auth-backend
[auth-backend]
(if auth-backend
(auth-backend {:realm "ATMOS"
:authfn (fn [request auth-data]
(get-authentication request auth-data))})))
(let [auth-backend-data {:realm "ATMOS"
:token-name "Bearer"
:authfn auth-backend-fn}]
(auth-backend auth-backend-data))))


(defmacro handle-request
[request authentication-needed? body]
`(if ~authentication-needed?
(if-not (authenticated? ~request)
(throw-unauthorized)
~body)
~body))
`(log-data :atmos-kernel :debug {:request ~request
:authentication-needed? ~authentication-needed?}
(if ~authentication-needed?
(if-not (authenticated? ~request)
(throw-unauthorized)
~body)
~body)))

0 comments on commit ca922c8

Please sign in to comment.