| 1 | # | ||||||||||||||||||||||||||||
| 2 | # $Id: Lookup.pm,v 69a3d7308875 2023/04/05 15:11:02 gomor $ | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | package OPP::Proc::Lookup; | ||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base qw(OPP::Proc); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | __PACKAGE__->cgBuildIndices; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use File::Slurp qw(read_file); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Text::CSV_XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Net::IPv4Addr qw(ipv4_in_network); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Load CSV data here, one time | ||||||||||||||||||||||||||||
| 18 | 1 | my $csv; | |||||||||||||||||||||||||||
| 19 | my $match_fields; # Prepare for supporting matching with AND filters | ||||||||||||||||||||||||||||
| 20 | 1 | my $lookup_field; # Support only last column as a match | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub _load { | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_load: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 24 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Load CSV lookup: | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | unless (defined($csv)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my $csvxs = Text::CSV_XS->new({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | binary => 1, | ||||||||||||||||||||||||||||
| 30 | sep_char => ',', | ||||||||||||||||||||||||||||
| 31 | allow_loose_quotes => 1, | ||||||||||||||||||||||||||||
| 32 | allow_loose_escapes => 1, | ||||||||||||||||||||||||||||
| 33 | escape_char => '"', | ||||||||||||||||||||||||||||
| 34 | }) or die("lookup: cannot initiate Text::CSV_XS\n"); | ||||||||||||||||||||||||||||
| 35 | #my @lines = read_file($file) or die("lookup: cannot read or empty file: $file\n"); | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | open(my $fd, '<', $file) or die("lookup: cannot open file: $file\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # First line is considered the header | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $header = $csvxs->getline($fd) or die("lookup: cannot get header\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | die("lookup: no line found\n") unless defined $header; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | #print STDERR Data::Dumper::Dumper($header)."\n"; | ||||||||||||||||||||||||||||
| 42 | 0 | my $lookup = pop @$header; # Last field is the data to add | |||||||||||||||||||||||||||
| 43 | 0 | $lookup_field = $lookup; | |||||||||||||||||||||||||||
| 44 | 0 | $match_fields = [ sort { $a cmp $b } @$header ]; | |||||||||||||||||||||||||||
| 45 | 0 | $header = join('+', sort { $a cmp $b } @$header); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | while (my $line = $csvxs->getline($fd)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | my $last = pop @$line; # Last field is the data to add | |||||||||||||||||||||||||||
| 49 | 0 | my $key = join('+', sort { $a cmp $b } @$line); | |||||||||||||||||||||||||||
| 50 | 0 | $csv->{$header}{$key} = { $lookup_field => $last }; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | #print STDERR Data::Dumper::Dumper($csv)."\n"; | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | return [ $csv, $match_fields, $lookup_field ]; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # | ||||||||||||||||||||||||||||
| 60 | # echo domain,mytags > lookup.csv | ||||||||||||||||||||||||||||
| 61 | # echo amazonaws.com,aws >> lookup.csv | ||||||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||||||
| 63 | # | lookup lookup.csv | ||||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||
| 65 | # echo ip,mytags | ||||||||||||||||||||||||||||
| 66 | # echo 8.8.8.0/24,google | ||||||||||||||||||||||||||||
| 67 | # | ||||||||||||||||||||||||||||
| 68 | # | lookup lookup.csv cidr=ip | ||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||
| 70 | sub process { | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=17 SCAR=57.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 0 | my ($input) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my $options = $self->options; | |||||||||||||||||||||||||||
| 75 | 0 | my $file = $options->{0}; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | die("lookup: file not given\n") unless defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | die("lookup: file not found: $file\n") unless -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $cidr = $options->{cidr} || 'ip'; # Use ip field by default for cidr matches | ||||||||||||||||||||||||||
|
Condition0%0/2
$options->{'cidr'} || 'ip'
MC/DC0%0/1
$options->{'cidr'} || 'ip'
$options->{'cidr'}
Truth table0%0/2
A
A= $options->{'cidr'}
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | $self->_load($file); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Touch nothing when matching fields are not found in input: | ||||||||||||||||||||||||||||
| 84 | 0 | for my $field (@$match_fields) { | |||||||||||||||||||||||||||
| 85 | 0 | my $values = $self->value($input, $field); | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | unless (defined($values)) { # Field not found here | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | $self->output->add($input); | |||||||||||||||||||||||||||
| 88 | 0 | return 1; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # All fields to match against were found in input, we can search a match: | ||||||||||||||||||||||||||||
| 93 | 0 | for my $field (@$match_fields) { | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $values = $self->value($input, $field) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($field eq $cidr) { # CIDR match mode | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | for my $v (@$values) { | |||||||||||||||||||||||||||
| 97 | 0 | for my $this (keys %{$csv->{$cidr}}) { | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (ipv4_in_network($this, $v)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | for my $k (keys %{$csv->{$field}{$this}}) { | |||||||||||||||||||||||||||
| 100 | 0 | $self->set($input, $k, $csv->{$field}{$this}{$k}); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | else { # Exact match mode | ||||||||||||||||||||||||||||
| 107 | 0 | for my $v (@$values) { | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (defined($csv->{$field}) && defined($csv->{$field}{$v})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $csv->{$field} and defined $csv->{$field}{$v}
MC/DC0%0/2
defined $csv->{$field} and defined $csv->{$field}{$v}
defined $csv->{$field}
defined $csv->{$field}{$v}
Truth table0%0/3
A and B
A= defined $csv->{$field}B= defined $csv->{$field}{$v}
| |||||||||||||||||||||||||||||
| 109 | 0 | for my $k (keys %{$csv->{$field}{$v}}) { | |||||||||||||||||||||||||||
| 110 | 0 | $self->set($input, $k, $csv->{$field}{$v}{$k}); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | $self->output->add($input); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | return 1; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||