Skip to content

Commit

Permalink
Allow specifying HTTP basic authentication credentials when writing t…
Browse files Browse the repository at this point in the history
…o Elasticsearch (#754)

* Allow specifiying HTTP basic authentication credentials when writing to Elasticsearch.

- Tested against Elasticsearch 5.x
- Still backwards-compatible with 2.x (ignores the extra info)

* Allow specifying HTTP basic authentication credentials when writing to Elasticsearch.

- Tested against Elasticsearch 5.x w/X-Pack
- Still backwards-compatible with 2.x (ignores the extra info)
  • Loading branch information
dhruvbansal authored and pyr committed Feb 7, 2017
1 parent 122d178 commit 796c5a8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/riemann/elasticsearch.clj
Expand Up @@ -22,12 +22,15 @@

(defn- post
"POST to Elasticsearch."
[esindex formatted-event]
(let [http-options {:body (json/generate-string formatted-event)
:content-type :json
:conn-timeout 5000
:socket-timeout 5000
:throw-entire-message? true}]
[credentials esindex formatted-event]
(let [base-http-options {:body (json/generate-string formatted-event)
:content-type :json
:conn-timeout 5000
:socket-timeout 5000
:throw-entire-message? true}
http-options (if credentials
(assoc base-http-options :basic-auth credentials)
base-http-options)]
(http/post esindex http-options)))

(defn elasticsearch
Expand All @@ -41,6 +44,8 @@
:es-index Index name, default is \"riemann\".
:index-suffix Index-suffix, default is \"-yyyy.MM.dd\".
:type Type to send to index, default is \"event\".
:username Username to authenticate with.
:password Password to authenticate with.
Example:
Expand All @@ -63,6 +68,8 @@
event-formatter (if (first maybe-formatter) (first maybe-formatter) format-event)]

(fn[event] (post
(if (and (:username opts) (:password opts))
[(:username opts) (:password opts)])
(format "%s/%s%s/%s"
(:es-endpoint opts)
(:es-index opts)
Expand Down
40 changes: 40 additions & 0 deletions test/riemann/elasticsearch_test.clj
Expand Up @@ -70,3 +70,43 @@
:conn-timeout 5000
:socket-timeout 5000
:throw-entire-message? true}]))))))

(deftest ^:elasticsearch elasticsearch-valid-credentials-test
(with-mock [calls clj-http.client/post]
(let [elastic (elasticsearch {:es-endpoint "https://example-elastic.com"
:es-index "test-riemann"
:index-suffix "-yyyy.MM"
:type "test-type"
:username "foo"
:password "bar"})
json-event (json/generate-string output-event)]

(testing "correct basic-auth constructed from valid credentials"
(elastic input-event)
(is (= (last @calls)
["https://example-elastic.com/test-riemann-2016.01/test-type"
{:body json-event
:content-type :json
:conn-timeout 5000
:socket-timeout 5000
:throw-entire-message? true
:basic-auth ["foo" "bar"]}]))))))

(deftest ^:elasticsearch elasticsearch-invalid-credentials-test
(with-mock [calls clj-http.client/post]
(let [elastic (elasticsearch {:es-endpoint "https://example-elastic.com"
:es-index "test-riemann"
:index-suffix "-yyyy.MM"
:type "test-type"
:username "foo"}) ; password is missing
json-event (json/generate-string output-event)]

(testing "invalid credentials are ignored"
(elastic input-event)
(is (= (last @calls)
["https://example-elastic.com/test-riemann-2016.01/test-type"
{:body json-event
:content-type :json
:conn-timeout 5000
:socket-timeout 5000
:throw-entire-message? true}]))))))

0 comments on commit 796c5a8

Please sign in to comment.