From 80cc2eb64e503e1df063aceb2a3bb450aa319614 Mon Sep 17 00:00:00 2001 From: Patrick Downey Date: Thu, 22 Nov 2018 22:04:56 +0000 Subject: [PATCH] Ensure if host field is a string before using it as default subject When using Elasticbeats with `add_host_metadata` the `host` field is a Hash not a String. Ensuring it's a `String` before using it as the default subject value will prevent logstash from crashing. --- lib/logstash/outputs/sns.rb | 2 +- spec/outputs/sns_spec.rb | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/logstash/outputs/sns.rb b/lib/logstash/outputs/sns.rb index c9c703b..091bcd7 100644 --- a/lib/logstash/outputs/sns.rb +++ b/lib/logstash/outputs/sns.rb @@ -114,7 +114,7 @@ def event_subject(event) sns_subject elsif sns_subject LogStash::Json.dump(sns_subject) - elsif event.get("host") + elsif event.get("host").is_a?(String) event.get("host") else NO_SUBJECT diff --git a/spec/outputs/sns_spec.rb b/spec/outputs/sns_spec.rb index 0e0f3c3..e0765b9 100644 --- a/spec/outputs/sns_spec.rb +++ b/spec/outputs/sns_spec.rb @@ -79,11 +79,16 @@ expect(subject.send(:event_subject, event)).to eql(LogStash::Json.dump(["foo", "bar"])) end - it "should return the host if 'sns_subject' not set" do + it "should return the host if 'sns_subject' not set and host is a string" do event = LogStash::Event.new("host" => "foo") expect(subject.send(:event_subject, event)).to eql("foo") end + it "should return the 'NO SUBJECT' if host not a string" do + event = LogStash::Event.new("host" => { "name" => "foo" }) + expect(subject.send(:event_subject, event)).to eql(LogStash::Outputs::Sns::NO_SUBJECT) + end + it "should return 'NO SUBJECT' when subject cannot be determined" do event = LogStash::Event.new("foo" => "bar") expect(subject.send(:event_subject, event)).to eql(LogStash::Outputs::Sns::NO_SUBJECT)