Skip to content
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

Translate ip on cidr keys to values #31

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/logstash/filters/translate.rb
Expand Up @@ -3,6 +3,7 @@
require "logstash/namespace"
require "json"
require "csv"
require "netaddr"

java_import 'java.util.concurrent.locks.ReentrantReadWriteLock'

Expand Down Expand Up @@ -106,10 +107,14 @@ class LogStash::Filters::Translate < LogStash::Filters::Base
# keys as regular expressions. A large dictionary could be expensive to match in this case.
config :exact, :validate => :boolean, :default => true

# If you'd like to treat dictionary keys as regular expressions, set `exact => true`.
# If you'd like to treat dictionary keys as regular expressions, set `regex => true`.
# Note: this is activated only when `exact => true`.
config :regex, :validate => :boolean, :default => false

# If you'd like to treat dictionary keys as ip cidr, set `cidr => true`.
# Note: this is activated only when `exact => true`.
config :cidr, :validate => :boolean, :default => false

# In case no translation occurs in the event (no matches), this will add a default
# translation string, which will always populate `field`, if the match failed.
#
Expand Down Expand Up @@ -187,6 +192,12 @@ def filter(event)
event[@destination] = lock_for_read { @dictionary[key] }
matched = true
end
elsif @cidr
key = @dictionary.keys.detect{ |k| NetAddr::CIDR.create(k).matches?(source) }
if key
event[@destination] = lock_for_read { @dictionary[key] }
matched = true
end
elsif @dictionary.include?(source)
event[@destination] = lock_for_read { @dictionary[source] }
matched = true
Expand Down
4 changes: 2 additions & 2 deletions logstash-filter-translate.gemspec
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|

# Gem dependencies
s.add_runtime_dependency "logstash-core-plugin-api", "~> 1.0"
s.add_runtime_dependency "netaddr", "~> 1.5"

s.add_development_dependency 'logstash-devutils'
s.add_development_dependency 'logstash-devutils', '~> 0'
end

135 changes: 135 additions & 0 deletions spec/filters/translate_spec.rb
Expand Up @@ -81,6 +81,141 @@
end
end

describe "cidr net translation" do

let(:config) do
{
"field" => "ip",
"destination" => "translation",
"dictionary" => [ "10.0.0.1/32", "Host",
"10.0.1.1/24", "Net" ],
"exact" => true,
"cidr" => true
}
end

let(:event) { LogStash::Event.new("ip" => "10.0.1.43") }

it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("Net")
end
end

describe "cidr host translation" do

let(:config) do
{
"field" => "ip",
"destination" => "translation",
"dictionary" => [ "10.0.0.1/32", "Host",
"10.0.1.1/24", "Net" ],
"exact" => true,
"cidr" => true
}
end

let(:event) { LogStash::Event.new("ip" => "10.0.0.1") }

it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("Host")
end
end

describe "cidr duplicate translation first hit" do

let(:config) do
{
"field" => "ip",
"destination" => "translation",
"dictionary" => [ "10.0.0.1/8", "Host",
"10.0.1.1/24", "Net" ],
"exact" => true,
"cidr" => true
}
end

let(:event) { LogStash::Event.new("ip" => "10.0.1.43") }

it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("Host")
end
end

describe "cidr wrong ip" do

let(:config) do
{
"field" => "ip",
"destination" => "translation",
"dictionary" => [ "10.0.0.1/16", "Host",
"10.0.1.1/24", "Net" ],
"exact" => true,
"cidr" => false,
"fallback" => "no match"
}
end

let(:event) { LogStash::Event.new("ip" => "260.0.1.43") }

it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("no match")
end
end

describe "cidr wrong translation" do

let(:config) do
{
"field" => "ip",
"destination" => "translation",
"dictionary" => [ "10.0.0.1/33", "Host",
"10.0.1.1/24", "Net" ],
"exact" => true,
"cidr" => false,
"fallback" => "no match"
}
end

let(:event) { LogStash::Event.new("ip" => "10.0.0.1") }

it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("no match")
end
end

describe "cidr fallback" do

let(:config) do
{
"field" => "ip",
"destination" => "translation",
"dictionary" => [ "10.0.0.1/32", "Host",
"10.0.1.1/24", "Net" ],
"exact" => true,
"cidr" => true,
"fallback" => "no match"
}
end

let(:event) { LogStash::Event.new("ip" => "10.0.0.43") }

it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("no match")
end
end

describe "fallback value" do

context "static configuration" do
Expand Down