From adcba0ae2c57dd2db0c84b0056d8fe93d148fca9 Mon Sep 17 00:00:00 2001 From: wizcheng Date: Thu, 3 Aug 2017 13:35:34 +0800 Subject: [PATCH 1/2] Switch http client, http-kit to clj-http --- project.clj | 7 +++-- src/greenyet/status.clj | 26 ++++++++++------ test/greenyet/status_test.clj | 56 +++++++++++++++++------------------ 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/project.clj b/project.clj index f126c01..f5867bc 100644 --- a/project.clj +++ b/project.clj @@ -13,13 +13,14 @@ [ring/ring-core "1.4.0"] [ring/ring-jetty-adapter "1.4.0"] [org.clojure/core.async "0.2.374"] - [http-kit "2.1.18"] [clj-time "0.9.0"] [hiccup "1.0.5"] [clj-yaml "0.4.0"] [cheshire "5.4.0"] - [json-path "1.0.0"]] - :profiles {:dev {:dependencies [[http-kit.fake "0.2.1"] + [json-path "1.0.0"] + [clj-http "3.6.1"] + ] + :profiles {:dev {:dependencies [[clj-http-fake "1.0.3"] [ring-mock "0.1.5"]] :resource-paths ["resources" "test/resources"] :jvm-opts ["-Dgreenyet.environment=development"]}} diff --git a/src/greenyet/status.clj b/src/greenyet/status.clj index 16e4d14..deda7e8 100644 --- a/src/greenyet/status.clj +++ b/src/greenyet/status.clj @@ -1,7 +1,8 @@ (ns greenyet.status (:require [cheshire.core :as j] [greenyet.parse :as parse] - [org.httpkit.client :as http])) + [clj-http.client :as http] + )) (defn- status-color-from-components [components] (let [colors (map :color components) @@ -48,13 +49,17 @@ (format "Status %s: %s" (:status response) body) (format "Status %s" (:status response))))) -(defn- http-get [status-url timeout-in-ms callback] +(defn- http-get [status-url timeout-in-ms callback-response callback-error] (http/get status-url {:headers {"Accept" "application/json"} :follow-redirects false :user-agent "greenyet" - :timeout timeout-in-ms} - callback)) + :socket-timeout timeout-in-ms + :conn-timeout timeout-in-ms + :insecure? true + :async? true + :throw-exceptions false} + callback-response callback-error)) (defn- identify-status [response timeout-in-ms config] (let [known-status-codes (set (or (:known-status-codes config) @@ -63,11 +68,11 @@ (cond (:error response) {:color :red :message (format "greenyet: %s" (.getMessage (:error response)))} - (contains? known-status-codes (:status response)) (->> config - (application-status (:body response)) - (http-status-code-trumps-app-status (:status response))) + (contains? known-status-codes (:status (:data response))) (->> config + (application-status (:body (:data response))) + (http-status-code-trumps-app-status (:status (:data response)))) :else {:color :red - :message (message-for-http-response response)}) + :message (message-for-http-response (:data response))}) (catch Exception e {:color :red :message (.getMessage e)})))) @@ -76,4 +81,7 @@ (http-get status-url timeout-in-ms (fn [response] - (callback (identify-status response timeout-in-ms config))))) + (callback (identify-status {:data response} timeout-in-ms config))) + (fn [response] + (callback (identify-status {:error response, :data (:data response)} timeout-in-ms config))) + )) diff --git a/test/greenyet/status_test.clj b/test/greenyet/status_test.clj index e9ddc6c..cf4be3b 100644 --- a/test/greenyet/status_test.clj +++ b/test/greenyet/status_test.clj @@ -2,7 +2,7 @@ (:require [cheshire.core :as j] [clojure.test :refer :all] [greenyet.status :as sut] - [org.httpkit.fake :as fake])) + [clj-http.fake :as fake])) (defn- host-without-color-config [url] {:hostname "the_host" @@ -44,20 +44,28 @@ :components component-config}}) (defmacro with-fake-resource [url resp & body] - `(fake/with-fake-http [{:url ~url} ~resp] + `(fake/with-fake-routes {~url ~resp} ~@body)) +(defn- response-with-status [status body] + (fn [_] {:status status + :body body})) + (defn- json-response [body] - {:status 200 - :headers {"Content-Type" "application/json"} - :body (j/generate-string body)}) + (fn [_] {:status 200 + :headers {"Content-Type" "application/json"} + :body (j/generate-string body)})) + +(defn- json-response-with-status [status body] + (fn [_] {:status status + :headers {"Content-Type" "application/json"} + :body (j/generate-string body)})) (def timeout 42) (deftest test-with-status (testing "should return red for 500" - (with-fake-resource "http://the_host/not_found" {:status 500 - :body ""} + (with-fake-resource "http://the_host/not_found" (response-with-status 500 "") (with-local-vars [status nil] (sut/fetch-status (host-without-color-config "http://the_host/not_found") timeout @@ -66,8 +74,7 @@ (:color status)))))))) (testing "should return green for 200" - (with-fake-resource "http://the_host/found" {:status 200 - :body ""} + (with-fake-resource "http://the_host/found" (response-with-status 200 "") (sut/fetch-status (host-without-color-config "http://the_host/found") timeout (fn [status] @@ -99,8 +106,7 @@ (:color status))))))) (testing "should fail if status key is configured but no JSON is provided" - (with-fake-resource "http://the_host/status.json" {:status 200 - :body "some body"} + (with-fake-resource "http://the_host/status.json" (response-with-status 200 "some body") (sut/fetch-status (host-with-color-config "http://the_host/status.json" "status") timeout (fn [status] @@ -143,8 +149,7 @@ (testing "messages" (testing "simple 200 check" - (with-fake-resource "http://the_host/found" {:status 200 - :body ""} + (with-fake-resource "http://the_host/found" (response-with-status 200 "") (sut/fetch-status (host-without-color-config "http://the_host/found") timeout (fn [status] @@ -152,8 +157,7 @@ (:message status))))))) (testing "for 500" - (with-fake-resource "http://the_host/error" {:status 500 - :body "Internal Error"} + (with-fake-resource "http://the_host/error" (response-with-status 500 "Internal Error") (sut/fetch-status (host-without-color-config "http://the_host/error") timeout (fn [status] @@ -161,8 +165,7 @@ (:message status))))))) (testing "for 302" - (with-fake-resource "http://the_host/redirect" {:status 302 - :body "Found"} + (with-fake-resource "http://the_host/redirect" (response-with-status 302 "Found") (sut/fetch-status (host-without-color-config "http://the_host/redirect") timeout (fn [status] @@ -170,8 +173,7 @@ (:message status))))))) (testing "for internal exception" - (with-fake-resource "http://the_host/status.json" {:status 200 - :body "some body"} + (with-fake-resource "http://the_host/status.json" (response-with-status 200 "some body") (sut/fetch-status (host-with-color-config "http://the_host/status.json" "status") timeout (fn [status] @@ -198,9 +200,9 @@ (first (:message status)))))))) (testing "should indicate JSON parse error" - (with-fake-resource "http://the_host/status.json" {:status 200 - :headers {"Content-Type" "application/json"} - :body "not_json"} + (with-fake-resource "http://the_host/status.json" (fn [_] {:status 200 + :headers {"Content-Type" "application/json"} + :body "not_json"}) (sut/fetch-status (host-with-color-config "http://the_host/status.json" "color") timeout (fn [status] @@ -397,10 +399,8 @@ (testing "custom accepted HTTP return codes" (testing "for 503" - (with-fake-resource "http://the_host/status503" {:status 503 - :headers {"Content-Type" "application/json"} - :body (j/generate-string {:color "green" - :components [{:color "green"}]})} + (with-fake-resource "http://the_host/status503" (json-response-with-status 503 {:color "green" + :components [{:color "green"}]}) (sut/fetch-status {:hostname "the_host" :service "the_service" :status-url "http://the_host/status503" @@ -414,9 +414,7 @@ (is (= [{:color :green :name "components 0" :message nil}] (:components status))))))) (testing "for 300" - (with-fake-resource "http://the_host/status300" {:status 300 - :headers {"Content-Type" "application/json"} - :body (j/generate-string {:color "green"})} + (with-fake-resource "http://the_host/status300" (json-response-with-status 300 {:color "green"}) (sut/fetch-status {:hostname "the_host" :service "the_service" :status-url "http://the_host/status300" From c72a9bb8829aad234e1de96d31916fd84180e216 Mon Sep 17 00:00:00 2001 From: wizcheng Date: Thu, 3 Aug 2017 14:04:28 +0800 Subject: [PATCH 2/2] add config for key store & trust store --- src/greenyet/config.clj | 16 ++++++++++++++-- src/greenyet/status.clj | 8 +++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/greenyet/config.clj b/src/greenyet/config.clj index e8d6a14..7aa11e2 100644 --- a/src/greenyet/config.clj +++ b/src/greenyet/config.clj @@ -5,6 +5,14 @@ (def config-dir (System/getenv "CONFIG_DIR")) +(def key-store (System/getenv "KEY_STORE")) + +(def key-store-pass (System/getenv "KEY_STORE_PASS")) + +(def trust-store (System/getenv "TRUST_STORE")) + +(def trust-store-pass (System/getenv "TRUST_STORE_PASS")) + (def polling-interval-in-ms (or (some-> (System/getenv "POLLING_INTERVAL") Integer/parseInt) (some-> (System/getenv "TIMEOUT") @@ -14,8 +22,12 @@ (def ^:private config-params [["CONFIG_DIR" (or config-dir "")] ["POLLING_INTERVAL" polling-interval-in-ms] - ["PORT" (or (System/getenv "PORT") - 3000)]]) + ["PORT" (or (System/getenv "PORT") 3000)] + ["KEY_STORE" key-store] + ["KEY_STORE_PASS" key-store-pass] + ["TRUST_STORE" trust-store] + ["TRUST_STORE_PASS" trust-store-pass] + ]) (defn config-params-as-string [] (->> config-params diff --git a/src/greenyet/status.clj b/src/greenyet/status.clj index deda7e8..f07e211 100644 --- a/src/greenyet/status.clj +++ b/src/greenyet/status.clj @@ -2,6 +2,7 @@ (:require [cheshire.core :as j] [greenyet.parse :as parse] [clj-http.client :as http] + [greenyet.config :as config] )) (defn- status-color-from-components [components] @@ -58,7 +59,12 @@ :conn-timeout timeout-in-ms :insecure? true :async? true - :throw-exceptions false} + :throw-exceptions false + :keystore config/key-store + :keystore-pass config/key-store-pass + :trust-store config/trust-store + :trust-store-pass config/trust-store-pass + } callback-response callback-error)) (defn- identify-status [response timeout-in-ms config]