Skip to content

Commit 1b72aac

Browse files
yaauieandsel
authored andcommitted
plugin alias: favor concrete plugin to resolving alias of same name
Because `Registry#namespace_lookup` will load the _first_ valid plugin it finds, we cannot rely on it finding the concrete class when an alias of the same name exists. Instead we need to revert `Registry#is_a_plugin?` to its original definition so that `namespace_lookup` will only select the specific plugin that has been requested, and introduce a new `Registry#is_a_plugin_or_alias?` to use in the one context where we are willing to accept either.
1 parent 2c70378 commit 1b72aac

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

logstash-core/lib/logstash/plugins/registry.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def load_plugin_class(type, plugin_name)
237237
public
238238
def lookup_pipeline_plugin(type, name)
239239
LogStash::PLUGIN_REGISTRY.lookup(type, name) do |plugin_klass, plugin_name|
240-
is_a_plugin?(plugin_klass, type.to_java, plugin_name)
240+
is_a_plugin_or_alias?(plugin_klass, type.to_java, plugin_name)
241241
end
242242
rescue LoadError, NameError => e
243243
logger.debug("Problems loading the plugin with", :type => type, :name => name)
@@ -282,20 +282,30 @@ def namespace_lookup(type, name)
282282
# the namespace can contain constants which are not for plugins classes (do not respond to :config_name)
283283
# namespace.constants is the shallow collection of all constants symbols in namespace
284284
# note that below namespace.const_get(c) should never result in a NameError since c is from the constants collection
285-
klass_sym = namespace.constants.find { |c| is_a_plugin?(namespace.const_get(c), type.to_java, name) }
285+
klass_sym = namespace.constants.find { |c| is_a_plugin?(namespace.const_get(c), name) }
286286
klass_sym && namespace.const_get(klass_sym)
287287
end
288288

289289
# check if klass is a valid plugin for name
290290
# @param klass [Class] plugin class
291291
# @param name [String] plugin name
292-
# @param type [String] plugin type input|codec|filter|output
293292
# @return [Boolean] true if klass is a valid plugin for name
294-
def is_a_plugin?(klass, type, name)
293+
def is_a_plugin?(klass, name)
295294
(klass.class == Java::JavaLang::Class && klass.simple_name.downcase == name.gsub('_','')) ||
296295
(klass.class == Java::JavaClass && klass.simple_name.downcase == name.gsub('_','')) ||
297296
(klass.ancestors.include?(LogStash::Plugin) && klass.respond_to?(:config_name) &&
298-
klass.config_name == @alias_registry.resolve_alias(type.to_java, name))
297+
klass.config_name == name)
298+
end
299+
300+
# check if klass is a valid plugin for name,
301+
# including alias resolution
302+
def is_a_plugin_or_alias?(klass, type, plugin_name)
303+
return true if is_a_plugin?(klass, plugin_name)
304+
305+
resolved_plugin_name = @alias_registry.resolve_alias(type, plugin_name)
306+
return true if is_a_plugin?(klass, resolved_plugin_name)
307+
308+
false
299309
end
300310

301311
def add_plugin(type, name, klass)

0 commit comments

Comments
 (0)