Skip to content

Commit c6872b9

Browse files
authored
Fix issue with templates being erroneously overwritten (#861)
When customers were managing their own templates, but not using ilm, templates could still be overwritten. This commit fixes the logic to only overwrite templates when ilm is in use
1 parent e782a58 commit c6872b9

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

lib/logstash/outputs/elasticsearch/common.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def setup_after_successful_connection
4848
end
4949
if successful_connection?
5050
install_template
51-
setup_ilm if ilm_enabled?
51+
setup_ilm if ilm_in_use?
5252
end
5353
end
5454
end

lib/logstash/outputs/elasticsearch/ilm.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ module Ilm
44
ILM_POLICY_PATH = "default-ilm-policy.json"
55

66
def setup_ilm
7-
return unless ilm_enabled?
8-
if default_index?(@index) || !default_rollover_alias?(@ilm_rollover_alias)
7+
return unless ilm_in_use?
98
logger.warn("Overwriting supplied index #{@index} with rollover alias #{@ilm_rollover_alias}") unless default_index?(@index)
109
@index = @ilm_rollover_alias
1110
maybe_create_rollover_alias
1211
maybe_create_ilm_policy
13-
end
1412
end
1513

1614
def default_rollover_alias?(rollover_alias)
1715
rollover_alias == LogStash::Outputs::ElasticSearch::DEFAULT_ROLLOVER_ALIAS
1816
end
1917

20-
def ilm_enabled?
18+
def ilm_alias_set?
19+
default_index?(@index) || !default_rollover_alias?(@ilm_rollover_alias)
20+
end
21+
22+
def ilm_in_use?
2123
return @ilm_actually_enabled if defined?(@ilm_actually_enabled)
2224
@ilm_actually_enabled =
2325
begin
@@ -28,7 +30,7 @@ def ilm_enabled?
2830
@logger.info("Index Lifecycle Management is set to 'auto', but will be disabled - #{error}")
2931
false
3032
else
31-
true
33+
ilm_alias_set?
3234
end
3335
else
3436
@logger.info("Index Lifecycle Management is set to 'auto', but will be disabled - Your Elasticsearch cluster is before 7.0.0, which is the minimum version required to automatically run Index Lifecycle Management")
@@ -37,7 +39,7 @@ def ilm_enabled?
3739
elsif @ilm_enabled.to_s == 'true'
3840
ilm_ready, error = ilm_ready?
3941
raise LogStash::ConfigurationError,"Index Lifecycle Management is set to enabled in Logstash, but cannot be used - #{error}" unless ilm_ready
40-
true
42+
ilm_alias_set?
4143
else
4244
false
4345
end

lib/logstash/outputs/elasticsearch/template_manager.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def self.install_template(plugin)
1111

1212

1313
template = get_template(plugin.template, plugin.maximum_seen_major_version)
14-
add_ilm_settings_to_template(plugin, template) if plugin.ilm_enabled?
14+
add_ilm_settings_to_template(plugin, template) if plugin.ilm_in_use?
1515
plugin.logger.info("Attempting to install template", :manage_template => template)
1616
install(plugin.client, template_name(plugin), template, plugin.template_overwrite)
1717
rescue => e
@@ -43,7 +43,7 @@ def self.add_ilm_settings_to_template(plugin, template)
4343
# if not and ILM is enabled, use the rollover alias
4444
# else use the default value of template_name
4545
def self.template_name(plugin)
46-
plugin.ilm_enabled? && !plugin.original_params.key?('template_name') ? plugin.ilm_rollover_alias : plugin.template_name
46+
plugin.ilm_in_use? && !plugin.original_params.key?('template_name') ? plugin.ilm_rollover_alias : plugin.template_name
4747
end
4848

4949
def self.default_template_path(es_major_version)

spec/integration/outputs/ilm_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,24 @@
395395
it_behaves_like 'an ILM enabled Logstash'
396396
end
397397

398+
context 'with a set index and a custom index pattern' do
399+
if ESHelper.es_version_satisfies?(">= 7.0")
400+
let (:template) { "spec/fixtures/template-with-policy-es7x.json" }
401+
else
402+
let (:template) { "spec/fixtures/template-with-policy-es6x.json" }
403+
end
404+
405+
let (:settings) { super.merge("template" => template,
406+
"index" => "overwrite-4")}
407+
408+
it 'should not overwrite the index patterns' do
409+
subject.register
410+
sleep(1)
411+
expect(@es.indices.get_template(name: "logstash")["logstash"]).to have_index_pattern("overwrite-*")
412+
end
413+
end
414+
415+
398416
context 'with a custom template' do
399417
let (:ilm_rollover_alias) { "the_cat_in_the_hat" }
400418
let (:index) { ilm_rollover_alias }

0 commit comments

Comments
 (0)