lib/eris/log/context/GeoIP.pm

filter: Statementstmt 32.8% 21 / 64 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.4% 5 / 11 Podpod 50.0% 1 / 2 totaltotal 25.2% 27 / 107 CC 16 SCAR 45.5
CC
16
Cov
23%
contextualize_message
182.0
_build_geo_lookup
20.0
_build_priority
2.0
CRAP 94.6
« lib/eris/log/context/sshd.pm ↑ Summary lib/eris/log/context/attacks/url.pm »
1 package eris::log::context::GeoIP;
2 # ABSTRACT: Apply MaxMind GeoIP Data to events
3  
4 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use GeoIP2::Database::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Types::Standard qw(Any Bool Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 with qw(
11 eris::role::context
12 );
13  
14 1 our $VERSION = '0.008'; # VERSION
15  
16  
17 0 ▶sub _build_priority { 1000 }
Subroutine _build_priority: not called CC=1 SCAR=6.9
18  
19  
20 0 ▶sub _build_field { '_exists_' }
Subroutine _build_field: not called CC=1 SCAR=6.9
21  
22  
23 0 ▶sub _build_matcher { qr/_ip$/ }
Subroutine _build_matcher: not called CC=1 SCAR=6.9
24  
25  
26 1 has 'geo_db' => (
27 is => 'ro',
28 isa => Str,
29 default => '/usr/share/GeoIP/GeoLite2-City.mmdb',
30 );
31  
32  
33 1 has 'geo_lookup' => (
34 is => 'ro',
35 isa => Any,
36 lazy => 1,
37 builder => '_build_geo_lookup',
38 );
39 sub _build_geo_lookup {
40 0 ▶ my ($self) = @_;
Subroutine _build_geo_lookup: not called CC=4 SCAR=30.0
41  
42 0 my $g;
43 eval {
44 0 $g = GeoIP2::Database::Reader->new(
45 file => $self->geo_db,
46 locales => [ 'en' ],
47 );
48 0 1;
49 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $g = "GeoIP2::Database::Reader"->new("file", $self->geo_db, "locales", ["en"]); 1 } } or do { my $err = $@; warn sprintf("Failed loading GeoIP Database '%s' with error: %s", $self->geo_db || "unspecified", $err || "unknown error") } 0 0 0
50 0 my $err = $@;
51 0 ▶ warn sprintf "Failed loading GeoIP Database '%s' with error: %s",
Condition0%0/4
$self->geo_db || 'unspecified'
l!l
00
$err || 'unknown error'
l!l
00
MC/DC0%0/1
$self->geo_db || 'unspecified'
$self->geo_db
MC/DC0%0/1
$err || 'unknown error'
$err
Truth table0%0/2
A
A= $self->geo_db
Aresult
11
00
Truth table0%0/2
A
A= $err
Aresult
11
00
52 ( $self->geo_db || 'unspecified' ),
53 ( $err || 'unknown error');
54 };
55 0 return $g;
56 }
57  
58  
59 1 has 'warnings' => (
60 is => 'rw',
61 isa => Bool,
62 default => 0,
63 );
64  
65  
66 sub sample_messages {
67 0 ▶ my @msgs = split /\r?\n/, <<EOF;
Subroutine sample_messages: not called CC=1 SCAR=6.9
Pod undocumented
68 EOF
69 0 return @msgs;
70 }
71  
72  
73 sub contextualize_message {
74 0 ▶ my ($self,$log) = @_;
Subroutine contextualize_message: not called CC=13 SCAR=52.0
Pod documented
75  
76 0 my $geo = $self->geo_lookup;
77 0 ▶ return unless $geo;
Branch: 1 branch
BranchTrueFalseTotal
unless $geo 0 0 0
78  
79 0 my %add = ();
80 0 my $ctxt = $log->context;
81  
82 0 foreach my $f ( keys %{ $ctxt } ) {
83 0 ▶ next unless $f =~ /^(.*)_ip$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /^(.*)_ip$/ 0 0 0
84 0 my $add_key = $1 . "_geoip";
85 0 my %geo = ();
86 eval {
87 0 my $city = $self->geo_lookup->city( ip => $ctxt->{$f} );
88 0 $geo{city} = $city->city->name;
89 0 $geo{country} = $city->country->iso_code;
90 0 $geo{continent} = $city->continent->code;
91 0 my $loc = $city->location();
92 0 $geo{location} = join(',', $loc->latitude, $loc->longitude);
93 0 my @traits = ();
94 0 my $traits = $city->traits;
95 0 ▶ if( $traits->is_anonymous_proxy ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($traits->is_anonymous_proxy) { } 0 0 0
elsif ($traits->is_legitimate_proxy) { } 0 0 0
elsif ($traits->is_satellite_provider) { } 0 0 0
96 0 push @traits, qw(anonymous proxy);
97 }
98 elsif( $traits->is_legitimate_proxy ) {
99 0 push @traits, 'proxy';
100 }
101 elsif( $traits->is_satellite_provider ) {
102 0 push @traits, 'satellite';
103 }
104 0 ▶ $geo{traits} = \@traits if @traits;
Branch: 1 branch
BranchTrueFalseTotal
if @traits 0 0 0
105 0 my $pc = $city->postal->code;
106 0 ▶ $geo{postal_code} = $pc if $pc;
Branch: 1 branch
BranchTrueFalseTotal
if $pc 0 0 0
107 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $city = $self->geo_lookup->city("ip", $ctxt->{$f}); $geo{"city"} = $city->city->name; $geo{"country"} = $city->country->iso_code; $geo{"continent"} = $city->continent->code; my $loc = $city->location; $geo{"location"} = join(",", $loc->latitude, $loc->longitude); my(@traits) = (); my $traits = $city->traits; if ($traits->is_anonymous_proxy) { push @traits, ("anonymous", "proxy"); } elsif ($traits->is_legitimate_proxy) { push @traits, "proxy"; } elsif ($traits->is_satellite_provider) { push @traits, "satellite"; } ; $geo{"traits"} = \@traits if @traits; my $pc = $city->postal->code; $geo{"postal_code"} = $pc if $pc } } or do { my $err = $@; warn sprintf('Geo lookup failed on %s: %s', $ctxt->{$f}, $err) if $self->warnings } 0 0 0
108 0 my $err = $@;
109 0 ▶ warn sprintf("Geo lookup failed on %s: %s", $ctxt->{$f}, $err) if $self->warnings;
Branch: 1 branch
BranchTrueFalseTotal
if $self->warnings 0 0 0
110 };
111 0 ▶ $add{$add_key} = \%geo if keys %geo;
Branch: 1 branch
BranchTrueFalseTotal
if keys %geo 0 0 0
112 }
113  
114 0 ▶ $log->add_context($self->name,\%add) if keys %add;
Branch: 1 branch
BranchTrueFalseTotal
if keys %add 0 0 0
115 }
116  
117  
118 1 1;
119  
120 __END__
121  
122 =pod
123  
124 =encoding UTF-8
125  
126 =head1 NAME
127  
128 eris::log::context::GeoIP - Apply MaxMind GeoIP Data to events
129  
130 =head1 VERSION
131  
132 version 0.008
133  
134 =head1 SYNOPSIS
135  
136 Use this module to tag geo location data to events with matching
137 field names. You'll probably need to configure the C<geo_db> attribute.
138  
139 =head1 ATTRIBUTES
140  
141 =head2 priority
142  
143 Defaults to 1000, run last.
144  
145 =head2 field
146  
147 Defaults to '_exists_'
148  
149 =head2 matcher
150  
151 A regex matching any string ending in '_ip'.
152  
153 =head2 geo_db
154  
155 The file location for the GeoIP version 2 databases, defaults to
156 '/usr/share/GeoIP/GeoLite2-City.mmdb'. Set in the config:
157  
158 ---
159 contexts:
160 configs:
161 GeoIP:
162 geo_db: '/var/lib/geoip/GeoIP2-Full.mmdb'
163  
164 =head2 geo_lookup
165  
166 This is an instance of a L<GeoIP2::Database::Reader> used to lookup
167 GeoIP data for an IP
168  
169 =head2 warnings
170  
171 Should warnings about this context failing initialization be displayed.
172  
173 Defaults to false so you won't get spew when the C<geo_db> is missing.
174  
175 =head1 METHODS
176  
177 =head2 contextualize_message
178  
179 Inspects the L<eris::log> context for any fields ending in '(.*)_ip'. If found,
180 a new key "${1}_geoip" is created to contain a HashRef with the following data:
181  
182 city, country, continent, location, traits, postal_code
183  
184 The only special elements being, location which is "latitude,longitude" and traits, which
185 is an array containing the following possible tags: anonymous, proxy, and/or satellite.
186  
187 =for Pod::Coverage sample_messages
188  
189 =head1 SEE ALSO
190  
191 L<eris::log::contextualizer>, L<eris::role::context>, L<GeoIP2::Database::Reader>
192  
193 =head1 AUTHOR
194  
195 Brad Lhotsky <brad@divisionbyzero.net>
196  
197 =head1 COPYRIGHT AND LICENSE
198  
199 This software is Copyright (c) 2015 by Brad Lhotsky.
200  
201 This is free software, licensed under:
202  
203 The (three-clause) BSD License
204  
205 =cut
« lib/eris/log/context/sshd.pm ↑ Summary lib/eris/log/context/attacks/url.pm »