From 8053abb255082dc14faac4068f25b81d45ecd809 Mon Sep 17 00:00:00 2001 From: Idriss Ketterer Date: Fri, 22 Nov 2019 16:06:20 +0100 Subject: [PATCH 1/6] Fix event json parse error when event fields contain quotation marks --- lib/logstash/outputs/slack.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/logstash/outputs/slack.rb b/lib/logstash/outputs/slack.rb index 047f404..139b348 100644 --- a/lib/logstash/outputs/slack.rb +++ b/lib/logstash/outputs/slack.rb @@ -36,6 +36,22 @@ def register @content_type = "application/x-www-form-urlencoded" end # def register + private + def recursive_sprintf(event, node) + case node + when String then event.sprintf(node) + when Array + node.map! do |obj| + recursive_sprintf( event, node ) + end + when Hash + node.each_pair do |key, value| + node[key] = recursive_sprintf( event, node ) + end + end + node + end + public def receive(event) return unless output?(event) @@ -61,7 +77,7 @@ def receive(event) end if @attachments and @attachments.any? - payload_json['attachments'] = @attachments.map { |x| JSON.parse(event.sprintf(JSON.dump(x))) } + payload_json['attachments'] = recursive_sprintf(event , @attachments) end if event.include?('attachments') and event.get('attachments').is_a?(Array) if event.get('attachments').any? From 85cfe0efc535a384c3b5ce472744938ea866507c Mon Sep 17 00:00:00 2001 From: Idriss Ketterer Date: Fri, 22 Nov 2019 17:14:26 +0100 Subject: [PATCH 2/6] Correcting code --- lib/logstash/outputs/slack.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/logstash/outputs/slack.rb b/lib/logstash/outputs/slack.rb index 139b348..d15818c 100644 --- a/lib/logstash/outputs/slack.rb +++ b/lib/logstash/outputs/slack.rb @@ -42,11 +42,11 @@ def recursive_sprintf(event, node) when String then event.sprintf(node) when Array node.map! do |obj| - recursive_sprintf( event, node ) + recursive_sprintf( event, obj ) end when Hash node.each_pair do |key, value| - node[key] = recursive_sprintf( event, node ) + node[key] = recursive_sprintf( event, value ) end end node From 43b6c9427f756be24413213697385799bd0df2f7 Mon Sep 17 00:00:00 2001 From: Idriss Ketterer Date: Fri, 22 Nov 2019 17:53:09 +0100 Subject: [PATCH 3/6] Gem version and runtime dependency; Fixes #17 according to comment https://github.com/logstash-plugins/logstash-output-slack/issues/17#issuecomment-530306623 --- logstash-output-slack.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/logstash-output-slack.gemspec b/logstash-output-slack.gemspec index 1e62382..222a588 100644 --- a/logstash-output-slack.gemspec +++ b/logstash-output-slack.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-output-slack' - s.version = '2.1.1' + s.version = '7.0.0' s.licenses = ['MIT','Apache License (2.0)'] s.summary = "Write events to Slack" s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program" @@ -21,7 +21,7 @@ Gem::Specification.new do |s| # Gem dependencies s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99" - s.add_runtime_dependency "public_suffix", "< 1.5.0" + s.add_runtime_dependency "public_suffix" s.add_runtime_dependency "logstash-codec-plain" s.add_runtime_dependency "rest-client", '~> 1.8', ">= 1.8.0" From 389ac82343ead8157d7cbeccdd0579d3f71b7ddf Mon Sep 17 00:00:00 2001 From: Idriss Ketterer Date: Fri, 22 Nov 2019 19:33:58 +0100 Subject: [PATCH 4/6] Fix return when string --- lib/logstash/outputs/slack.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/logstash/outputs/slack.rb b/lib/logstash/outputs/slack.rb index d15818c..f3980aa 100644 --- a/lib/logstash/outputs/slack.rb +++ b/lib/logstash/outputs/slack.rb @@ -39,7 +39,8 @@ def register private def recursive_sprintf(event, node) case node - when String then event.sprintf(node) + when String + node = event.sprintf(node) when Array node.map! do |obj| recursive_sprintf( event, obj ) From 6ff821d50d2061c44d9c4ae3c1fa5240d25e1cbd Mon Sep 17 00:00:00 2001 From: Idriss Ketterer Date: Fri, 22 Nov 2019 20:16:45 +0100 Subject: [PATCH 5/6] Add a test for string interpolations in attachments & with quotes --- spec/outputs/slack_spec.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/spec/outputs/slack_spec.rb b/spec/outputs/slack_spec.rb index fe1cbad..f90fb35 100644 --- a/spec/outputs/slack_spec.rb +++ b/spec/outputs/slack_spec.rb @@ -228,6 +228,35 @@ def test_one_event(event, expected_json, expected_url = "http://requestb.in/r9lk test_one_event(event, expected_json) end end + + context "when attachements contain interpolations" do + let(:data) { { + "message" => "This message should show in slack", + "x" => "3", + "image" => "http://example.com/image.png", + "textquot" => "Text with \"quotation marks\"", + } } + + let(:config) { { + "url" => "http://requestb.in/r9lkbzr9", + "attachments" => [ + {"image_url" => "%{image}", + "textquot" => "%{textquot}" + } + ] + } } + + it "uses and formats all provided values" do + expected_json = { + :text => "This message should show in slack", + :attachments => [{ + :image_url => "http://example.com/image.png", + :textquot => "Text with \"quotation marks\"" + }] + } + test_one_event(event, expected_json) + end + end end describe "interpolation in url field" do From 3406ed828fc0c0063dec1c034d47ea624c83f3e0 Mon Sep 17 00:00:00 2001 From: Idriss Ketterer Date: Fri, 13 Dec 2019 15:53:08 +0100 Subject: [PATCH 6/6] Fix bug: don't do the sprintf in-place, but actually return new objects --- lib/logstash/outputs/slack.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/logstash/outputs/slack.rb b/lib/logstash/outputs/slack.rb index f3980aa..dfca9d8 100644 --- a/lib/logstash/outputs/slack.rb +++ b/lib/logstash/outputs/slack.rb @@ -40,17 +40,18 @@ def register def recursive_sprintf(event, node) case node when String - node = event.sprintf(node) + ret = event.sprintf(node) when Array - node.map! do |obj| + ret = node.map do |obj| recursive_sprintf( event, obj ) end when Hash + ret = Hash.new node.each_pair do |key, value| - node[key] = recursive_sprintf( event, value ) + ret[key] = recursive_sprintf( event, value ) end end - node + ret end public