Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTPS support #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}}
Expand Down
16 changes: 14 additions & 2 deletions src/greenyet/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
32 changes: 23 additions & 9 deletions src/greenyet/status.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns greenyet.status
(:require [cheshire.core :as j]
[greenyet.parse :as parse]
[org.httpkit.client :as http]))
[clj-http.client :as http]
[greenyet.config :as config]
))

(defn- status-color-from-components [components]
(let [colors (map :color components)
Expand Down Expand Up @@ -48,13 +50,22 @@
(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
: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]
(let [known-status-codes (set (or (:known-status-codes config)
Expand All @@ -63,11 +74,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)}))))
Expand All @@ -76,4 +87,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)))
))
56 changes: 27 additions & 29 deletions test/greenyet/status_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -143,35 +149,31 @@

(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]
(is (= "OK"
(: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]
(is (= "Status 500: Internal Error"
(: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]
(is (= "Status 302: Found"
(: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]
Expand All @@ -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]
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down