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

Fix event json parse error when event fields contain quotation marks etc... #18

Open
wants to merge 6 commits into
base: main
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
20 changes: 19 additions & 1 deletion lib/logstash/outputs/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ def register
@content_type = "application/x-www-form-urlencoded"
end # def register

private
def recursive_sprintf(event, node)
case node
when String
ret = event.sprintf(node)
when Array
ret = node.map do |obj|
recursive_sprintf( event, obj )
end
when Hash
ret = Hash.new
node.each_pair do |key, value|
ret[key] = recursive_sprintf( event, value )
end
end
ret
end

public
def receive(event)
return unless output?(event)
Expand All @@ -61,7 +79,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?
Expand Down
4 changes: 2 additions & 2 deletions logstash-output-slack.gemspec
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions spec/outputs/slack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down