Skip to content

Commit 0e37a1c

Browse files
yaauiejsvd
andauthored
ensure execution_context is propagated to additional_codecs (#152)
* ensure execution_context is propagated to additional_codecs * more closely specify the desired behaviour for pipeline.ecs_compatibility * version bump * restructure conditional too complex to be a guard clause. Co-authored-by: João Duarte <[email protected]> Co-authored-by: João Duarte <[email protected]>
1 parent d20a3e0 commit 0e37a1c

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.5.1
2+
- Fix: codecs provided with `additional_codecs` now correctly run in the pipeline's context, which means that they respect the `pipeline.ecs_compatibility` setting [#152](https://github.com/logstash-plugins/logstash-input-http/pull/152)
3+
14
## 3.5.0
25
- Feat: TLSv1.3 support [#146](https://github.com/logstash-plugins/logstash-input-http/pull/146)
36

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.5.0
1+
3.5.1

lib/logstash/inputs/http.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class LogStash::Inputs::Http < LogStash::Inputs::Base
127127
config :verify_mode, :validate => ['none', 'peer', 'force_peer'], :default => 'none',
128128
:deprecated => "Set 'ssl_verify_mode' instead."
129129

130+
attr_reader :codecs
131+
130132
public
131133
def register
132134

@@ -140,7 +142,7 @@ def register
140142
@codecs = Hash.new
141143

142144
@additional_codecs.each do |content_type, codec|
143-
@codecs[content_type] = LogStash::Plugin.lookup("codec", codec).new
145+
@codecs[content_type] = initialize_codec(codec)
144146
end
145147

146148
require "logstash/inputs/http/message_handler"
@@ -333,4 +335,13 @@ def error_details(e, trace = false)
333335
error_details
334336
end
335337

338+
def initialize_codec(codec_name)
339+
codec_klass = LogStash::Plugin.lookup("codec", codec_name)
340+
if defined?(::LogStash::Plugins::Contextualizer)
341+
::LogStash::Plugins::Contextualizer.initialize_plugin(execution_context, codec_klass)
342+
else
343+
codec_klass.new
344+
end
345+
end
346+
336347
end # class LogStash::Inputs::Http

spec/inputs/http_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,4 +656,68 @@ def setup_server_client(url = self.url)
656656

657657
end
658658
end
659+
end if false
660+
661+
# If we have a setting called `pipeline.ecs_compatibility`, we need to
662+
# ensure that our additional_codecs are instantiated with the proper
663+
# execution context in order to ensure that the pipeline setting is
664+
# respected.
665+
if LogStash::SETTINGS.registered?('pipeline.ecs_compatibility')
666+
667+
def with_setting(name, value, &block)
668+
setting = LogStash::SETTINGS.get_setting(name)
669+
was_set, orignial_value = setting.set?, setting.value
670+
setting.set(value)
671+
672+
yield(true)
673+
674+
ensure
675+
was_set ? setting.set(orignial_value) : setting.reset
676+
end
677+
678+
def setting_value_supported?(name, value)
679+
with_setting(name, value) { true }
680+
rescue
681+
false
682+
end
683+
684+
describe LogStash::Inputs::Http do
685+
context 'additional_codecs' do
686+
let(:port) { rand(1025...5000) }
687+
688+
%w(disabled v1 v8).each do |spec|
689+
if setting_value_supported?('pipeline.ecs_compatibility', spec)
690+
context "with `pipeline.ecs_compatibility: #{spec}`" do
691+
around(:each) { |example| with_setting('pipeline.ecs_compatibility', spec, &example) }
692+
693+
it 'propagates the ecs_compatibility pipeline setting to the additional_codecs' do
694+
input("input { http { port => #{port} additional_codecs => { 'application/json' => 'json' 'text/plain' => 'plain' } } }") do |pipeline, queue|
695+
http_input = pipeline.inputs.first
696+
expect(http_input).to be_a_kind_of(described_class) # precondition
697+
698+
http_input.codecs.each do |key, value|
699+
aggregate_failures("Codec for `#{key}`") do
700+
expect(value.ecs_compatibility).to eq(spec.to_sym)
701+
end
702+
end
703+
end
704+
end
705+
end
706+
end
707+
end
708+
709+
it 'propagates the execution context from the input to the codecs' do
710+
input("input { http { port => #{port} } }") do |pipeline, queue|
711+
http_input = pipeline.inputs.first
712+
expect(http_input).to be_a_kind_of(described_class) # precondition
713+
714+
http_input.codecs.each do |key, value|
715+
aggregate_failures("Codec for `#{key}`") do
716+
expect(value.execution_context).to be http_input.execution_context
717+
end
718+
end
719+
end
720+
end
721+
end
722+
end
659723
end

0 commit comments

Comments
 (0)