-
Notifications
You must be signed in to change notification settings - Fork 80
add ecs support #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kaisecheng
merged 12 commits into
logstash-plugins:master
from
kaisecheng:add_ecs_support
Mar 25, 2021
Merged
add ecs support #179
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4eddd42
add ecs support
kaisecheng cb5ec53
dump version
kaisecheng 780dd4f
remove unuse import
kaisecheng 35a8e2b
update doc
kaisecheng 48a3c4d
ECS skips country_code3
kaisecheng 20cfc17
update doc
kaisecheng 81ed0bd
fix file path
kaisecheng 7bab9db
fix file path
kaisecheng 4147f24
change auto target
kaisecheng a291b6c
update doc
kaisecheng 255ee7a
update doc
kaisecheng ac67591
update test case
kaisecheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
# encoding: utf-8 | ||
require "logstash/devutils/rspec/spec_helper" | ||
require "logstash/filters/geoip" | ||
require_relative 'test_helper' | ||
require 'logstash/plugin_mixins/ecs_compatibility_support/spec_helper' | ||
|
||
CITYDB = ::Dir.glob(::File.expand_path(::File.join("..", "..", "..", "vendor", "GeoLite2-City.mmdb"), __FILE__)).first | ||
ASNDB = ::Dir.glob(::File.expand_path(::File.join("..", "..", "..", "vendor", "GeoLite2-ASN.mmdb"), __FILE__)).first | ||
|
||
describe LogStash::Filters::GeoIP do | ||
let(:options) { {} } | ||
let(:plugin) { LogStash::Filters::GeoIP.new(options) } | ||
|
||
describe "simple ip filter", :aggregate_failures do | ||
|
||
context "when specifying the target", :ecs_compatibility_support do | ||
ecs_compatibility_matrix(:disabled, :v1) do |ecs_select| | ||
|
||
let(:ip) { "8.8.8.8" } | ||
let(:event) { LogStash::Event.new("message" => ip) } | ||
let(:target) { "server" } | ||
let(:common_options) { {"source" => "message", "database" => CITYDB, "target" => target} } | ||
|
||
before(:each) do | ||
allow_any_instance_of(described_class).to receive(:ecs_compatibility).and_return(ecs_compatibility) | ||
plugin.register | ||
end | ||
|
||
context "with city database" do | ||
let(:options) { common_options } | ||
|
||
it "should return geo in target" do | ||
plugin.filter(event) | ||
|
||
expect( event.get ecs_select[disabled: "[#{target}][ip]", v1: "[#{target}][ip]"] ).to eq ip | ||
expect( event.get ecs_select[disabled: "[#{target}][country_code2]", v1: "[#{target}][geo][country_iso_code]"] ).to eq 'US' | ||
expect( event.get ecs_select[disabled: "[#{target}][country_name]", v1: "[#{target}][geo][country_name]"] ).to eq 'United States' | ||
expect( event.get ecs_select[disabled: "[#{target}][continent_code]", v1: "[#{target}][geo][continent_code]"] ).to eq 'NA' | ||
expect( event.get ecs_select[disabled: "[#{target}][location][lat]", v1: "[#{target}][geo][location][lat]"] ).to eq 37.751 | ||
expect( event.get ecs_select[disabled: "[#{target}][location][lon]", v1: "[#{target}][geo][location][lon]"] ).to eq -97.822 | ||
|
||
if ecs_select.active_mode == :disabled | ||
expect( event.get "[#{target}][country_code3]" ).to eq 'US' | ||
else | ||
expect( event.get "[#{target}][geo][country_code3]" ).to be_nil | ||
expect( event.get "[#{target}][country_code3]" ).to be_nil | ||
end | ||
end | ||
end | ||
|
||
|
||
context "with ASN database" do | ||
let(:options) { common_options.merge({"database" => ASNDB}) } | ||
|
||
it "should return geo in target" do | ||
plugin.filter(event) | ||
|
||
expect( event.get ecs_select[disabled: "[#{target}][ip]", v1: "[#{target}][ip]"] ).to eq ip | ||
expect( event.get ecs_select[disabled: "[#{target}][asn]", v1: "[#{target}][as][number]"] ).to eq 15169 | ||
expect( event.get ecs_select[disabled: "[#{target}][as_org]", v1: "[#{target}][as][organization][name]"] ).to eq "Google LLC" | ||
end | ||
end | ||
|
||
context "with customize fields" do | ||
let(:fields) { ["continent_name", "timezone"] } | ||
let(:options) { common_options.merge({"fields" => fields}) } | ||
|
||
it "should return fields" do | ||
plugin.filter(event) | ||
|
||
expect( event.get ecs_select[disabled: "[#{target}][ip]", v1: "[#{target}][ip]"] ).to be_nil | ||
expect( event.get ecs_select[disabled: "[#{target}][country_code2]", v1: "[#{target}][geo][country_iso_code]"] ).to be_nil | ||
expect( event.get ecs_select[disabled: "[#{target}][country_name]", v1: "[#{target}][geo][country_name]"] ).to be_nil | ||
expect( event.get ecs_select[disabled: "[#{target}][continent_code]", v1: "[#{target}][geo][continent_code]"] ).to be_nil | ||
expect( event.get ecs_select[disabled: "[#{target}][location][lat]", v1: "[#{target}][geo][location][lat]"] ).to be_nil | ||
expect( event.get ecs_select[disabled: "[#{target}][location][lon]", v1: "[#{target}][geo][location][lon]"] ).to be_nil | ||
|
||
expect( event.get ecs_select[disabled: "[#{target}][continent_name]", v1: "[#{target}][geo][continent_name]"] ).to eq "North America" | ||
expect( event.get ecs_select[disabled: "[#{target}][timezone]", v1: "[#{target}][geo][timezone]"] ).to eq "America/Chicago" | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
||
context "setup target field" do | ||
let(:ip) { "8.8.8.8" } | ||
let(:event) { LogStash::Event.new("message" => ip) } | ||
let(:common_options) { {"source" => "message", "database" => CITYDB} } | ||
|
||
context "ECS disabled" do | ||
before do | ||
allow_any_instance_of(described_class).to receive(:ecs_compatibility).and_return(:disabled) | ||
plugin.register | ||
plugin.filter(event) | ||
end | ||
|
||
context "`target` is unset" do | ||
let(:options) { common_options } | ||
it "should use 'geoip'" do | ||
expect( event.get "[geoip][ip]" ).to eq ip | ||
end | ||
end | ||
|
||
context "`target` is set" do | ||
let(:target) { 'host' } | ||
let(:options) { common_options.merge({"target" => target}) } | ||
it "should use `target`" do | ||
expect( event.get "[#{target}][ip]" ).to eq ip | ||
end | ||
end | ||
end | ||
|
||
context "ECS mode" do | ||
before do | ||
allow_any_instance_of(described_class).to receive(:ecs_compatibility).and_return(:v1) | ||
end | ||
|
||
context "`target` is unset" do | ||
|
||
context "`source` end with [ip]" do | ||
let(:event) { LogStash::Event.new("host" => {"ip" => ip}) } | ||
let(:options) { common_options.merge({"source" => "[host][ip]"}) } | ||
|
||
it "should use source's parent as target" do | ||
plugin.register | ||
plugin.filter(event) | ||
expect( event.get "[host][geo][country_iso_code]" ).to eq 'US' | ||
end | ||
end | ||
|
||
context "`source` end with [ip] but `target` does not match ECS template" do | ||
let(:event) { LogStash::Event.new("hostname" => {"ip" => ip}) } | ||
let(:options) { common_options.merge({"source" => "[hostname][ip]"}) } | ||
|
||
it "should use source's parent as target with warning" do | ||
expect(plugin.logger).to receive(:warn).with(/ECS expect `target`/) | ||
plugin.register | ||
plugin.filter(event) | ||
expect( event.get "[hostname][geo][country_iso_code]" ).to eq 'US' | ||
end | ||
end | ||
|
||
context "`source` == [ip]" do | ||
let(:event) { LogStash::Event.new("ip" => ip) } | ||
let(:options) { common_options.merge({"source" => "[ip]"}) } | ||
|
||
it "should raise error to require `target`" do | ||
expect { plugin.register }.to raise_error LogStash::ConfigurationError, /requires a `target`/ | ||
end | ||
end | ||
|
||
context "`source` not end with [ip]" do | ||
let(:event) { LogStash::Event.new("host_ip" => ip) } | ||
let(:options) { common_options.merge({"source" => "host_ip"}) } | ||
|
||
it "should raise error to require `target`" do | ||
expect { plugin.register }.to raise_error LogStash::ConfigurationError, /requires a `target`/ | ||
end | ||
end | ||
end | ||
|
||
context "`target` is set" do | ||
let(:event) { LogStash::Event.new("client" => {"ip" => ip}) } | ||
let(:options) { common_options.merge({"source" => "[client][ip]", "target" => target}) } | ||
|
||
context "`target` matches ECS template" do | ||
let(:target) { 'host' } | ||
|
||
it "should use `target`" do | ||
plugin.register | ||
plugin.filter(event) | ||
expect( event.get "[#{target}][geo][country_iso_code]" ).to eq 'US' | ||
end | ||
end | ||
|
||
context "`target` in canonical field reference syntax matches ECS template" do | ||
let(:target) { '[host]' } | ||
|
||
it "should normalize and use `target`" do | ||
expect(plugin.logger).to receive(:warn).never | ||
plugin.register | ||
plugin.filter(event) | ||
expect( event.get "[host][geo][country_iso_code]" ).to eq 'US' | ||
end | ||
end | ||
|
||
context "`target` does not match ECS template" do | ||
let(:target) { 'host_ip' } | ||
|
||
it "should use `target` with warning" do | ||
expect(plugin.logger).to receive(:warn).with(/ECS expect `target`/) | ||
plugin.register | ||
plugin.filter(event) | ||
expect( event.get "[#{target}][geo][country_iso_code]" ).to eq 'US' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.