blib/lib/OPP/Proc/Allowlist.pm

filter: Statementstmt 31.8% 22 / 69 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 1 totaltotal 22.7% 28 / 123 CC 26 SCAR 53.5
CC
26
Cov
20%
process
342.0
_load
90.0
BEGIN
1.0
CRAP 211.3
« blib/lib/OPP/Proc/Blocklist.pm ↑ Summary blib/lib/OPP/Proc/Lookup.pm »
1 #
2 # $Id: Allowlist.pm,v a6091d726551 2023/04/05 15:44:32 gomor $
3 #
4 package OPP::Proc::Allowlist;
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; # Support all fields as a match (AND match)
20  
21 sub _load {
22 0 ▶ my $self = shift;
Subroutine _load: not called CC=9 SCAR=45.0
23 0 my ($file) = @_;
24  
25 # Load CSV lookup:
26 0 ▶ unless (defined($csv)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $csv) 0 0 0
27 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 "allowlist: cannot initiate Text::CSV_XS\n" 0 0 0
28 binary => 1,
29 sep_char => ',',
30 allow_loose_quotes => 1,
31 allow_loose_escapes => 1,
32 escape_char => '"',
33 }) or die("allowlist: cannot initiate Text::CSV_XS\n");
34 #my @lines = read_file($file) or die("allowlist: cannot read or empty file: $file\n");
35 0 ▶ open(my $fd, '<', $file) or die("allowlist: cannot open file: $file\n");
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, "<", $file or die "allowlist: cannot open file: $file\n" 0 0 0
36  
37 # First line is considered the header
38 0 ▶ my $header = $csvxs->getline($fd) or die("allowlist: cannot get header\n");
Branch: 1 branch
BranchTrueFalseTotal
my $header = $csvxs->getline($fd) or die "allowlist: cannot get header\n" 0 0 0
39 0 ▶ die("allowlist: no line found\n") unless defined $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 0 0 0
40 #print STDERR Data::Dumper::Dumper($header)."\n";
41 0 $match_fields = $header; # All fields can be used in a match (AND match)
42  
43 0 ▶ while (my $line = $csvxs->getline($fd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $csvxs->getline($fd)) 0 0 0
44 0 my $h = {};
45 0 my $idx = 0;
46 0 for my $this (@$header) {
47 0 $h->{$this} = $line->[$idx++];
48 }
49 0 ▶ push @$csv, $h if keys %$h;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$h 0 0 0
50 }
51  
52 #print STDERR Data::Dumper::Dumper($csv)."\n";
53 }
54  
55 0 return [ $csv, $match_fields ];
56 }
57  
58 #
59 # echo domain > allowlist.csv
60 # echo amazonaws.com >> allowlist.csv
61 #
62 # | allowlist allowlist.csv
63 # | allowlist allowlist.csv cidr=ip
64 #
65 sub process {
66 0 ▶ my $self = shift;
Subroutine process: not called CC=18 SCAR=58.3
Pod undocumented
67 0 my ($input) = @_;
68  
69 0 my $options = $self->options;
70 0 my $file = $options->{0};
71 0 ▶ die("allowlist: file not given\n") unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
72 0 ▶ die("allowlist: file not found: $file\n") unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
73  
74 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
75  
76 0 my $loaded = $self->_load($file);
77 0 $csv = $loaded->[0];
78 0 $match_fields = $loaded->[1];
79  
80 # Touch nothing when matching fields are not found in input:
81 0 for my $field (@$match_fields) {
82 0 my $values = $self->value($input, $field);
83 0 ▶ unless (defined($values)) { # Field not found here
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $values) 0 0 0
84 0 return 1; # We skip result
85 }
86 }
87  
88 # All fields to match against were found in input, we can search a match:
89 0 my $skip = 0;
90 0 my $total = @$match_fields;
91 0 for my $line (@$csv) {
92 0 for my $field (@$match_fields) {
93 0 my $this_skip = 0;
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 #print STDERR "*** match field [$field] vs v[$v]\n";
98 0 ▶ if (defined($line->{$field}) && ipv4_in_network($line->{$field}, $v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line->{$field} and &ipv4_in_network($line->{$field}, $v)) 0 0 0
Condition0%0/3
defined $line->{$field} and &ipv4_in_network($line->{$field}, $v)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $line->{$field} and &ipv4_in_network($line->{$field}, $v)
defined $line->{$field} &ipv4_in_network($line->{$field}, $v)
Truth table0%0/3
A and B
A= defined $line->{$field}B= &ipv4_in_network($line->{$field}, $v)
ABresult
0X0
100
111
99 0 $this_skip++;
100 }
101 }
102 }
103 else { # Exact field match mode
104 0 for my $v (@$values) {
105 #print STDERR "*** match field [$field] vs v[$v]\n";
106 0 ▶ if (defined($line->{$field}) && $v eq $line->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line->{$field} and $v eq $line->{$field}) 0 0 0
Condition0%0/3
defined $line->{$field} and $v eq $line->{$field}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $line->{$field} and $v eq $line->{$field}
defined $line->{$field} $v eq $line->{$field}
Truth table0%0/3
A and B
A= defined $line->{$field}B= $v eq $line->{$field}
ABresult
0X0
100
111
107 0 $this_skip++;
108 0 last;
109 }
110 }
111 }
112 0 ▶ $skip++ if $this_skip;
Branch: 1 branch
BranchTrueFalseTotal
if $this_skip 0 0 0
113 }
114 }
115  
116 #print STDERR "*** skip[$skip] total[$total]\n";
117  
118 0 ▶ if ($skip == $total) { # All fields have matched, it is allowlisted
Branch: 1 branch
BranchTrueFalseTotal
if ($skip == $total) 0 0 0
119 0 $self->output->add($input);
120 }
121  
122 0 return 1;
123 }
124  
125 1 1;
126  
127 __END__
« blib/lib/OPP/Proc/Blocklist.pm ↑ Summary blib/lib/OPP/Proc/Lookup.pm »