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 the ability to read SNS credentials from the default credential chain #701

Merged
merged 1 commit into from Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/riemann/sns.clj
Expand Up @@ -4,7 +4,8 @@
(your-publisher \"your::arn\"). Or simply call sns-publish or
sns-publish-async directly."
(:import (com.amazonaws AmazonWebServiceClient)
[com.amazonaws.auth BasicAWSCredentials]
(com.amazonaws.auth BasicAWSCredentials
DefaultAWSCredentialsProviderChain)
[com.amazonaws.regions RegionUtils]
[com.amazonaws.handlers AsyncHandler]
(com.amazonaws.services.sns AmazonSNS
Expand Down Expand Up @@ -45,9 +46,12 @@
(RegionUtils/getRegion name))

(defn- aws-credentials [credentials]
(BasicAWSCredentials.
(:access-key credentials)
(:secret-key credentials)))
(if (or (nil? (:access-key credentials))
(nil? (:secret-key credentials)))
(DefaultAWSCredentialsProviderChain.)
(BasicAWSCredentials.
(:access-key credentials)
(:secret-key credentials))))

(defn- aws-client
[klass opts]
Expand Down Expand Up @@ -101,7 +105,7 @@
(doseq [arn arns]
(publish arn))))

(def aws-credential-keys #{:access-key :secret-key :region})
(def aws-credential-keys #{:access-key :secret-key :region})
(def aws-async-keys #{:async :success :error})
(def aws-keys (union aws-credential-keys aws-async-keys))

Expand Down Expand Up @@ -156,19 +160,19 @@
:secret-key \"your-secret-key\"
:subject \"something went wrong\"
:async true}))

By default, riemann uses (riemann.common/subject events) and
(riemann.common/body events) to format messages.
You can set your own subject or body formatter functions by including
:subject or :body in msg-opts. These formatting functions take a sequence of
events and return a string.

(def sns (sns-publisher {} {:body (fn [events]
(def sns (sns-publisher {} {:body (fn [events]
(apply prn-str events))}))"
([] (sns-publisher {}))
([opts]
(let [{aws-opts :aws-opts
async-opts :async-opts
async-opts :async-opts
msg-opts :msg-opts} (sift-opts opts)]
(sns-publisher aws-opts msg-opts async-opts)))
([aws-opts msg-opts & [async-opts]]
Expand Down
16 changes: 13 additions & 3 deletions test/riemann/sns_test.clj
Expand Up @@ -24,10 +24,10 @@
(deftest override-formatting-test
(let [message (#'riemann.sns/compose-message
{:body (fn [events]
(apply str "body "
(apply str "body "
(map :service events)))
:subject (fn [events]
(apply str "subject "
:subject (fn [events]
(apply str "subject "
(map :service events)))
:arn ["my:arn"]}
{:service "foo"})]
Expand Down Expand Up @@ -59,6 +59,14 @@
(stream fake-event))
(is (= @a ["test:arn" fake-event-body fake-event-subject]))))

(deftest sns-publisher-default-chain-test
(let [a (promise)
sns (sns-publisher)
stream (sns "test:arn")]
(with-redefs [riemann.sns/aws-sns-publish #(deliver a [%2 %3 %4])]
(stream fake-event))
(is (= @a ["test:arn" fake-event-body fake-event-subject]))))

(deftest sns-publisher-async-test
(let [a (promise)
done (promise)
Expand Down Expand Up @@ -100,12 +108,14 @@
done (promise)
fail (promise)
sns-sync (sns-publisher aws-opts)
sns-default-chain (sns-publisher {})
sns-async (sns-publisher aws-opts {} {:async true})
sns-callbacks (sns-publisher aws-opts {} {:async true
:success #(deliver done [%2])
:error #(deliver fail [%1])})
event (merge fake-event {:time (unix-time)})]
((sns-sync env-arn) (merge event {:service "sns sync test"}))
((sns-default-chain env-arn) (merge event {:service "sns default credential chain test"}))
((sns-async env-arn) (merge event {:service "sns async test"}))
((sns-callbacks env-arn) (merge event {:service "sns async callback test"}))
((sns-callbacks (str env-arn ":non:existent")) (merge event {:service "sns async callback test"}))
Expand Down