blib/lib/OPP/Proc/Lookup.pm

filter: Statementstmt 31.9% 23 / 72 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 1 totaltotal 25.2% 29 / 115 CC 23 SCAR 52.3
CC
23
Cov
22%
process
306.0
_load
56.0
BEGIN
1.0
CRAP 186.7
« blib/lib/OPP/Proc/Allowlist.pm ↑ Summary blib/lib/OPP.pm »
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
BranchTrueFalseTotal
unless (defined $csv) 0 0 0
28 0 ▶ my $csvxs = Text::CSV_XS->new({
Branch: 1 branch
BranchTrueFalseTotal
my $csvxs = "Text::CSV_XS"->new({"binary", 1, "sep_char", ",", "allow_loose_quotes", 1, "allow_loose_escapes", 1, "escape_char", "\""}) or die "lookup: cannot initiate Text::CSV_XS\n" 0 0 0
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
BranchTrueFalseTotal
open my $fd, "<", $file or die "lookup: cannot open file: $file\n" 0 0 0
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
BranchTrueFalseTotal
my $header = $csvxs->getline($fd) or die "lookup: cannot get header\n" 0 0 0
40 0 ▶ die("lookup: no line found\n") unless defined $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 0 0 0
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
BranchTrueFalseTotal
if (my $line = $csvxs->getline($fd)) 0 0 0
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
BranchTrueFalseTotal
unless defined $file 0 0 0
77 0 ▶ die("lookup: file not found: $file\n") unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
78  
79 0 ▶ my $cidr = $options->{cidr} || 'ip'; # Use ip field by default for cidr matches
Condition0%0/2
$options->{'cidr'} || 'ip'
l!l
00
MC/DC0%0/1
$options->{'cidr'} || 'ip'
$options->{'cidr'}
Truth table0%0/2
A
A= $options->{'cidr'}
Aresult
11
00
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
BranchTrueFalseTotal
unless (defined $values) 0 0 0
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
BranchTrueFalseTotal
my $values = $self->value($input, $field) or next 0 0 0
95 0 ▶ if ($field eq $cidr) { # CIDR match mode
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq $cidr) { } 0 0 0
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
BranchTrueFalseTotal
if (&ipv4_in_network($this, $v)) 0 0 0
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
BranchTrueFalseTotal
if (defined $csv->{$field} and defined $csv->{$field}{$v}) 0 0 0
Condition0%0/3
defined $csv->{$field} and defined $csv->{$field}{$v}
!ll&&!rl&&r
000
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}
ABresult
0X0
100
111
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__
« blib/lib/OPP/Proc/Allowlist.pm ↑ Summary blib/lib/OPP.pm »