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

LIR: merge hash attributes of same name to support legacy configurations #8602

Closed
Closed
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
18 changes: 15 additions & 3 deletions logstash-core/lib/logstash/compiler/lscl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,21 @@ def expr_attributes
else
[k,v]
end
}.reduce({}) do |hash,kv|
k,v = kv
hash[k] = v
}.reduce({}) do |hash, kv|
k, v = kv
existing = hash[k]
if existing.nil?
hash[k] = v
elsif existing.kind_of?(::Hash)
# For legacy reasons, a config can contain multiple `AST::Attribute`s with the same name
# and a hash-type value (e.g., "match" in the grok filter), which are merged into a single
# hash value; e.g., `{"match" => {"baz" => "bar"}, "match" => {"foo" => "bulb"}}` is
# interpreted as `{"match" => {"baz" => "bar", "foo" => "blub"}}`.
# (NOTE: this bypasses `AST::Hash`'s ability to detect duplicate keys)
hash[k] = existing.merge(v)
else
hash[k] = existing + v
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this also backports an intermediate fix that supports the similar behaviour when multiple Attributes with the same name produce Arrays

end
hash
end

Expand Down
29 changes: 29 additions & 0 deletions logstash-core/spec/logstash/compiler/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,35 @@ def j
expect(c_plugin).to ir_eql(j.iPlugin(INPUT, "generator", expected_plugin_args))
end
end

describe "a filter plugin that repeats a Hash directive" do
let(:source) { "input { } filter { #{plugin_source} } output { } " }
subject(:c_plugin) { compiled[:filter] }

let(:plugin_source) do
%q[
grok {
match => { "message" => "%{WORD:word}" }
match => { "examplefield" => "%{NUMBER:num}" }
break_on_match => false
}
]
end

let(:expected_plugin_args) do
{
"match" => {
"message" => "%{WORD:word}",
"examplefield" => "%{NUMBER:num}"
},
"break_on_match" => "false"
}
end

it "should merge the contents of the individual directives" do
expect(c_plugin).to ir_eql(j.iPlugin(FILTER, "grok", expected_plugin_args))
end
end
end

context "inputs" do
Expand Down