Skip to content

Commit 737f5c1

Browse files
committed
Add flag to enable disabling of 'host' field auto-population
Add `add_hostname` flag - when enabled, the `host` field will be populated by the value supplied by the beat in the `hostname` field if that field is present. The flag defaults to true to retain backwards compatibility with earlier versions of the beats input. The field is intentionally added as a deprecated field - auto populating the `host` field in this way will be removed in future versions of the beats input. Fixes #340
1 parent 7369365 commit 737f5c1

File tree

4 files changed

+129
-16
lines changed

4 files changed

+129
-16
lines changed

docs/index.asciidoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
9090
[cols="<,<,<",options="header",]
9191
|=======================================================================
9292
|Setting |Input type|Required
93+
| <<plugins-{type}s-{plugin}-add_hostname>> |<<boolean, boolean>>|No
9394
| <<plugins-{type}s-{plugin}-cipher_suites>> |<<array,array>>|No
9495
| <<plugins-{type}s-{plugin}-client_inactivity_timeout>> |<<number,number>>|No
9596
| <<plugins-{type}s-{plugin}-host>> |<<string,string>>|No
@@ -112,6 +113,18 @@ input plugins.
112113

113114
&nbsp;
114115

116+
[id="plugins-{type}s-{plugin}-add_hostname"]
117+
===== `add_hostname`
118+
119+
added[5.2.0, Field was added to allow users to not automatically add the `host` field in events.]
120+
deprecated[5.2.0, In future versions of this plugin, this setting will be removed, and the 'hosts' field will not be added to events.]
121+
122+
* Value type is <<boolean,boolean>>
123+
* Default value is `false`
124+
125+
Flag to determine whether to add `host` field to event using the value supplied by the beat in the `hostname` field.
126+
127+
115128
[id="plugins-{type}s-{plugin}-cipher_suites"]
116129
===== `cipher_suites`
117130

lib/logstash/inputs/beats.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class LogStash::Inputs::Beats < LogStash::Inputs::Base
8282
#
8383
config :ssl_certificate_authorities, :validate => :array, :default => []
8484

85+
# Flag to determine whether to add host information (provided by the beat in the 'hostname' field) to the event
86+
config :add_hostname, :validate => :boolean, :default => true, :deprecated => 'Host field will not be automatically populated by future version of the Beats input'
87+
88+
8589
# By default the server doesn't do any client verification.
8690
#
8791
# `peer` will make the server ask the client to provide a certificate.

lib/logstash/inputs/beats/event_transform_common.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def initialize(input)
1212
# Copies the beat.hostname field into the host field unless
1313
# the host field is already defined
1414
def copy_beat_hostname(event)
15+
return unless @input.add_hostname
1516
host = event.get("[beat][hostname]")
1617

1718
if host && event.get("host").nil?

spec/support/shared_examples.rb

Lines changed: 111 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,126 @@
2525
expect(subject.get("tags")).to include(tag)
2626
end
2727

28-
context "when the `beats.hostname` doesnt exist on the event" do
29-
let(:already_exist) { "already_exist" }
30-
let(:event_map) { super.merge({ "host" => already_exist }) }
28+
context 'when add_hostname is true' do
29+
let(:config) { super.merge({'add_hostname' => true})}
3130

32-
it "doesnt change the value" do
33-
expect(subject.get("host")).to eq(already_exist)
31+
context 'when a host is provided in beat.host.name' do
32+
let(:already_exist) { "already_exist" }
33+
let(:producer_host) { "newhost01" }
34+
let(:event_map) { super.merge({ "beat" => { "host" => {"name" => producer_host }}}) }
35+
36+
context "when no `host` key already exists on the event" do
37+
it "does not set the host value" do
38+
expect(subject.get("host")).to be_nil
39+
end
40+
end
41+
42+
context "when `host` key exists on the event" do
43+
let(:already_exist) { "already_exist" }
44+
let(:event_map) { super.merge({ "host" => already_exist }) }
45+
46+
it "doesn't override it" do
47+
expect(subject.get("host")).to eq(already_exist)
48+
end
49+
end
3450
end
35-
end
3651

37-
context "when the `beat.hostname` exist in the event" do
38-
let(:producer_host) { "newhost01" }
39-
let(:event_map) { super.merge({ "beat" => { "hostname" => producer_host }}) }
52+
context "when a host is set in `beat.hostname`" do
53+
let(:producer_host) { "newhost01" }
54+
let(:event_map) { super.merge({ "beat" => { "hostname" => producer_host }}) }
4055

41-
context "when `host` key doesn't exist on the event" do
42-
it "copy the `beat.hostname` to `host` or backward compatibility" do
43-
expect(subject.get("host")).to eq(producer_host)
56+
context "when no `host` key already exists on the event" do
57+
it "copies the value in `beat.hostname` to `host`" do
58+
expect(subject.get("host")).to eq(producer_host)
59+
end
60+
end
61+
62+
context "when `host` key exists on the event" do
63+
let(:already_exist) { "already_exist" }
64+
let(:event_map) { super.merge({ "host" => already_exist }) }
65+
66+
it "doesn't override it" do
67+
expect(subject.get("host")).to eq(already_exist)
68+
end
69+
end
70+
end
71+
72+
context "when no host is provided in beat" do
73+
context "when no `host` key already exists on the event" do
74+
it "does not set the host" do
75+
expect(subject.get("host")).to be_nil
76+
end
77+
end
78+
79+
context "when `host` key already exists on the event" do
80+
let(:already_exist) { "already_exist" }
81+
let(:event_map) { super.merge({ "host" => already_exist }) }
82+
83+
it "doesn't override it" do
84+
expect(subject.get("host")).to eq(already_exist)
85+
end
4486
end
4587
end
88+
end
4689

47-
context "when `host` key exists on the event" do
90+
context 'when add hostname is false' do
91+
let(:config) { super.merge({'add_hostname' => false})}
92+
93+
context 'when a host is provided in beat.host.name' do
4894
let(:already_exist) { "already_exist" }
49-
let(:event_map) { super.merge({ "host" => already_exist }) }
95+
let(:producer_host) { "newhost01" }
96+
let(:event_map) { super.merge({ "beat" => { "host" => {"name" => producer_host }}}) }
97+
98+
context "when no `host` key already exists on the event" do
99+
it "does not set the host" do
100+
expect(subject.get("host")).to be_nil
101+
end
102+
end
103+
104+
context "when `host` key already exists on the event" do
105+
let(:already_exist) { "already_exist" }
106+
let(:event_map) { super.merge({ "host" => already_exist }) }
107+
108+
it "doesn't override it" do
109+
expect(subject.get("host")).to eq(already_exist)
110+
end
111+
end
112+
end
113+
114+
context "when a host is provided in `beat.hostname`" do
115+
let(:producer_host) { "newhost01" }
116+
let(:event_map) { super.merge({ "beat" => { "hostname" => producer_host }}) }
117+
118+
context "when no `host` key already exists on the event" do
119+
it "does not set the host" do
120+
expect(subject.get("host")).to be_nil
121+
end
122+
end
123+
124+
context "when `host` key already exists on the event" do
125+
let(:already_exist) { "already_exist" }
126+
let(:event_map) { super.merge({ "host" => already_exist }) }
127+
128+
it "doesn't override it" do
129+
expect(subject.get("host")).to eq(already_exist)
130+
end
131+
end
132+
end
133+
134+
context "when no host is provided in beat" do
135+
context "when no `host` key already exists on the event" do
136+
it "does not set the host" do
137+
expect(subject.get("host")).to be_nil
138+
end
139+
end
140+
141+
context "when `host` key already exists on the event" do
142+
let(:already_exist) { "already_exist" }
143+
let(:event_map) { super.merge({ "host" => already_exist }) }
50144

51-
it "doesn't override it" do
52-
expect(subject.get("host")).to eq(already_exist)
145+
it "doesn't override it" do
146+
expect(subject.get("host")).to eq(already_exist)
147+
end
53148
end
54149
end
55150
end

0 commit comments

Comments
 (0)