Module: LogStash::Config::Mixin::DSL
- Includes:
- Util::EnvironmentVariables
- Defined in:
- lib/logstash/config/mixin.rb
Overview
def config_init
Constant Summary
Constants included from Util::EnvironmentVariables
Util::EnvironmentVariables::ENV_PLACEHOLDER_REGEX
Instance Attribute Summary collapse
-
#flags ⇒ Object
Returns the value of attribute flags.
Instance Method Summary collapse
-
#config(name, opts = {}) ⇒ Object
Define a new configuration setting.
-
#config_name(name = nil) ⇒ Object
(also: #config_plugin)
If name is given, set the name and return it.
-
#default(name, value) ⇒ Object
def config.
- #default?(name) ⇒ Boolean
- #get_config ⇒ Object
-
#get_default(name) ⇒ Object
def get_config.
- #hash_or_array(value) ⇒ Object
-
#inherited(subclass) ⇒ Object
This is called whenever someone subclasses a class that has this mixin.
-
#milestone(m = nil) ⇒ Object
Deprecated: Declare the version of the plugin inside the gemspec.
- #options(opts) ⇒ Object
-
#plugin_status(status = nil) ⇒ Object
Deprecated: Declare the version of the plugin inside the gemspec.
-
#print_version_notice ⇒ Object
TODO: Remove in 6.0.
- #process_parameter_value(value, config_settings) ⇒ Object
-
#secure_params!(params) ⇒ Object
def validate_value.
-
#validate(params) ⇒ Object
def inherited.
- #validate_check_invalid_parameter_names(params) ⇒ Object
- #validate_check_parameter_values(params) ⇒ Object
-
#validate_check_required_parameter(config_key, config_opts, k, v) ⇒ Object
def validate_check_invalid_parameter_names.
- #validate_check_required_parameter_names(params) ⇒ Object
- #validate_value(value, validator) ⇒ Object
-
#validator_find(key) ⇒ Object
def validate_check_parameter_values.
Methods included from Util::EnvironmentVariables
#deep_replace, #replace_env_placeholders
Instance Attribute Details
#flags ⇒ Object
Returns the value of attribute flags.
149 150 151 |
# File 'lib/logstash/config/mixin.rb', line 149 def flags @flags end |
Instance Method Details
#config(name, opts = {}) ⇒ Object
Define a new configuration setting
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/logstash/config/mixin.rb', line 172 def config(name, opts={}) @config ||= Hash.new # TODO(sissel): verify 'name' is of type String, Symbol, or Regexp name = name.to_s if name.is_a?(Symbol) @config[name] = opts # ok if this is empty if name.is_a?(String) define_method(name) { instance_variable_get("@#{name}") } define_method("#{name}=") { |v| instance_variable_set("@#{name}", v) } end end |
#config_name(name = nil) ⇒ Object Also known as: config_plugin
If name is given, set the name and return it. If no name given (nil), return the current name.
153 154 155 156 |
# File 'lib/logstash/config/mixin.rb', line 153 def config_name(name = nil) @config_name = name if !name.nil? @config_name end |
#default(name, value) ⇒ Object
def config
185 186 187 188 |
# File 'lib/logstash/config/mixin.rb', line 185 def default(name, value) @defaults ||= {} @defaults[name.to_s] = value end |
#default?(name) ⇒ Boolean
198 199 200 |
# File 'lib/logstash/config/mixin.rb', line 198 def default?(name) return @defaults && @defaults.include?(name) end |
#get_config ⇒ Object
190 191 192 |
# File 'lib/logstash/config/mixin.rb', line 190 def get_config return @config end |
#get_default(name) ⇒ Object
def get_config
194 195 196 |
# File 'lib/logstash/config/mixin.rb', line 194 def get_default(name) return @defaults && @defaults[name] end |
#hash_or_array(value) ⇒ Object
559 560 561 562 563 564 |
# File 'lib/logstash/config/mixin.rb', line 559 def hash_or_array(value) if !value.is_a?(Hash) value = [*value] # coerce scalar to array if necessary end return value end |
#inherited(subclass) ⇒ Object
This is called whenever someone subclasses a class that has this mixin.
214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/logstash/config/mixin.rb', line 214 def inherited(subclass) # Copy our parent's config to a subclass. # This method is invoked whenever someone subclasses us, like: # class Foo < Bar ... subconfig = Hash.new if !@config.nil? @config.each do |key, val| subconfig[key] = val end end subclass.instance_variable_set("@config", subconfig) @@version_notice_given = false end |
#milestone(m = nil) ⇒ Object
Deprecated: Declare the version of the plugin inside the gemspec.
167 168 169 |
# File 'lib/logstash/config/mixin.rb', line 167 def milestone(m = nil) self.logger.debug(I18n.t('logstash.plugin.deprecated_milestone', :plugin => config_name)) end |
#options(opts) ⇒ Object
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/logstash/config/mixin.rb', line 202 def (opts) # add any options from this class prefix = self.name.split("::").last.downcase @flags.each do |flag| flagpart = flag[:args].first.gsub(/^--/,"") # TODO(sissel): logger things here could help debugging. opts.on("--#{prefix}-#{flagpart}", *flag[:args][1..-1], &flag[:block]) end end |
#plugin_status(status = nil) ⇒ Object
Deprecated: Declare the version of the plugin inside the gemspec.
161 162 163 |
# File 'lib/logstash/config/mixin.rb', line 161 def plugin_status(status = nil) milestone(status) end |
#print_version_notice ⇒ Object
TODO: Remove in 6.0
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/logstash/config/mixin.rb', line 243 def print_version_notice return if @@version_notice_given begin plugin_version = LogStash::Util::PluginVersion.find_plugin_version!(@plugin_type, @config_name) if plugin_version < PLUGIN_VERSION_1_0_0 if plugin_version < PLUGIN_VERSION_0_9_0 self.logger.info(I18n.t("logstash.plugin.version.0-1-x", :type => @plugin_type, :name => @config_name, :LOGSTASH_VERSION => LOGSTASH_VERSION)) else self.logger.info(I18n.t("logstash.plugin.version.0-9-x", :type => @plugin_type, :name => @config_name, :LOGSTASH_VERSION => LOGSTASH_VERSION)) end end rescue LogStash::PluginNoVersionError # This can happen because of one of the following: # - The plugin is loaded from the plugins.path and contains no gemspec. # - The plugin is defined in a universal plugin, so the loaded plugin doesn't correspond to an actual gemspec. ensure @@version_notice_given = true end end |
#process_parameter_value(value, config_settings) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/logstash/config/mixin.rb', line 323 def process_parameter_value(value, config_settings) config_val = config_settings[:validate] if config_settings[:list] value = Array(value) # coerce scalars to lists # Empty lists are converted to nils return true, nil if value.empty? validated_items = value.map {|v| validate_value(v, config_val)} is_valid = validated_items.all? {|sr| sr[0] } processed_value = validated_items.map {|sr| sr[1]} else is_valid, processed_value = validate_value(value, config_val) end return [is_valid, processed_value] end |
#secure_params!(params) ⇒ Object
def validate_value
550 551 552 553 554 555 556 557 |
# File 'lib/logstash/config/mixin.rb', line 550 def secure_params!(params) params.each do |key, value| if [:uri, :password].include? @config[key][:validate] is_valid, processed_value = process_parameter_value(value, @config[key]) params[key] = processed_value end end end |
#validate(params) ⇒ Object
def inherited
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/logstash/config/mixin.rb', line 228 def validate(params) @plugin_name = config_name @plugin_type = ancestors.find { |a| a.name =~ /::Base$/ }.config_name is_valid = true print_version_notice is_valid &&= validate_check_invalid_parameter_names(params) is_valid &&= validate_check_required_parameter_names(params) is_valid &&= validate_check_parameter_values(params) return is_valid end |
#validate_check_invalid_parameter_names(params) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/logstash/config/mixin.rb', line 271 def validate_check_invalid_parameter_names(params) invalid_params = params.keys # Filter out parameters that match regexp keys. # These are defined in plugins like this: # config /foo.*/ => ... @config.each_key do |config_key| if config_key.is_a?(Regexp) invalid_params.reject! { |k| k =~ config_key } elsif config_key.is_a?(String) invalid_params.reject! { |k| k == config_key } end end if invalid_params.size > 0 invalid_params.each do |name| self.logger.error("Unknown setting '#{name}' for #{@plugin_name}") end return false end # if invalid_params.size > 0 return true end |
#validate_check_parameter_values(params) ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/logstash/config/mixin.rb', line 341 def validate_check_parameter_values(params) # Filter out parameters that match regexp keys. # These are defined in plugins like this: # config /foo.*/ => ... all_params_valid = true params.each do |key, value| @config.keys.each do |config_key| next unless (config_key.is_a?(Regexp) && key =~ config_key) \ || (config_key.is_a?(String) && key == config_key) config_settings = @config[config_key] is_valid, processed_value = process_parameter_value(value, config_settings) if is_valid # Accept coerced value if valid # Used for converting values in the config to proper objects. params[key] = processed_value else self.logger.error(I18n.t("logstash.runner.configuration.setting_invalid", :plugin => @plugin_name, :type => @plugin_type, :setting => key, :value => value.inspect, :value_type => config_settings[:validate], :note => processed_value)) end all_params_valid &&= is_valid break # done with this param key end # config.each end # params.each return all_params_valid end |
#validate_check_required_parameter(config_key, config_opts, k, v) ⇒ Object
def validate_check_invalid_parameter_names
293 294 295 296 297 298 299 |
# File 'lib/logstash/config/mixin.rb', line 293 def validate_check_required_parameter(config_key, config_opts, k, v) if config_key.is_a?(Regexp) (k =~ config_key && v) elsif config_key.is_a?(String) k && v end end |
#validate_check_required_parameter_names(params) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/logstash/config/mixin.rb', line 301 def validate_check_required_parameter_names(params) is_valid = true @config.each do |config_key, config| next unless config[:required] if config_key.is_a?(Regexp) && !params.keys.any? { |k| k =~ config_key } is_valid = false end value = params[config_key] if value.nil? || (config[:list] && Array(value).empty?) self.logger.error(I18n.t("logstash.runner.configuration.setting_missing", :setting => config_key, :plugin => @plugin_name, :type => @plugin_type)) is_valid = false end end return is_valid end |
#validate_value(value, validator) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/logstash/config/mixin.rb', line 387 def validate_value(value, validator) # Validator comes from the 'config' pieces of plugins. # They look like this # config :mykey => lambda do |value| ... end # (see LogStash::Inputs::File for example) result = nil value = deep_replace(value) if validator.nil? return true, value elsif validator.is_a?(Array) value = [*value] if value.size > 1 return false, "Expected one of #{validator.inspect}, got #{value.inspect}" end if !validator.include?(value.first) return false, "Expected one of #{validator.inspect}, got #{value.inspect}" end result = value.first elsif validator.is_a?(Symbol) # TODO(sissel): Factor this out into a coercion method? # TODO(sissel): Document this stuff. value = hash_or_array(value) case validator when :codec if value.first.is_a?(String) value = LogStash::Plugin.lookup("codec", value.first).new return true, value else value = value.first return true, value end when :hash if value.is_a?(Hash) return true, value end if value.size % 2 == 1 return false, "This field must contain an even number of items, got #{value.size}" end # Convert the array the config parser produces into a hash. result = {} value.each_slice(2) do |key, value| entry = result[key] if entry.nil? result[key] = value else if entry.is_a?(Array) entry << value else result[key] = [entry, value] end end end when :array result = value when :string if value.size > 1 # only one value wanted return false, "Expected string, got #{value.inspect}" end result = value.first when :number if value.size > 1 # only one value wanted return false, "Expected number, got #{value.inspect} (type #{value.class})" end v = value.first case v when Numeric result = v when String if v.to_s.to_f.to_s != v.to_s \ && v.to_s.to_i.to_s != v.to_s return false, "Expected number, got #{v.inspect} (type #{v})" end if v.include?(".") # decimal value, use float. result = v.to_f else result = v.to_i end end # case v when :boolean if value.size > 1 # only one value wanted return false, "Expected boolean, got #{value.inspect}" end bool_value = value.first if !!bool_value == bool_value # is_a does not work for booleans # we have Boolean and not a string result = bool_value else if bool_value !~ /^(true|false)$/ return false, "Expected boolean 'true' or 'false', got #{bool_value.inspect}" end result = (bool_value == "true") end when :ipaddr if value.size > 1 # only one value wanted return false, "Expected IPaddr, got #{value.inspect}" end octets = value.split(".") if octets.length != 4 return false, "Expected IPaddr, got #{value.inspect}" end octets.each do |o| if o.to_i < 0 or o.to_i > 255 return false, "Expected IPaddr, got #{value.inspect}" end end result = value.first when :password if value.size > 1 return false, "Expected password (one value), got #{value.size} values?" end result = value.first.is_a?(::LogStash::Util::Password) ? value.first : ::LogStash::Util::Password.new(value.first) when :uri if value.size > 1 return false, "Expected uri (one value), got #{value.size} values?" end result = value.first.is_a?(::LogStash::Util::SafeURI) ? value.first : ::LogStash::Util::SafeURI.new(value.first) when :path if value.size > 1 # Only 1 value wanted return false, "Expected path (one value), got #{value.size} values?" end # Paths must be absolute #if !Pathname.new(value.first).absolute? #return false, "Require absolute path, got relative path #{value.first}?" #end if !File.exists?(value.first) # Check if the file exists return false, "File does not exist or cannot be opened #{value.first}" end result = value.first when :bytes begin bytes = Integer(value.first) rescue nil result = bytes || Filesize.from(value.first).to_i rescue ArgumentError return false, "Unparseable filesize: #{value.first}. possible units (KiB, MiB, ...) e.g. '10 KiB'. doc reference: http://www.elastic.co/guide/en/logstash/current/configuration.html#bytes" end else return false, "Unknown validator symbol #{validator}" end # case validator else return false, "Unknown validator #{validator.class}" end # Return the validator for later use, like with type coercion. return true, result end |
#validator_find(key) ⇒ Object
def validate_check_parameter_values
377 378 379 380 381 382 383 384 385 |
# File 'lib/logstash/config/mixin.rb', line 377 def validator_find(key) @config.each do |config_key, config_val| if (config_key.is_a?(Regexp) && key =~ config_key) \ || (config_key.is_a?(String) && key == config_key) return config_val end end # @config.each return nil end |