lib/App/PaloAlto/PolicyVerify.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package App::PaloAlto::PolicyVerify;
6 $App::PaloAlto::PolicyVerify::VERSION = '0.0.2';
7  
8 # PODNAME
9 # ABSTRACT: Test firewall rules using log files.
10  
11 0 ▶use Device::Firewall::PaloAlto;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Getopt::Long qw(GetOptionsFromArray);
13 use Text::CSV;
14  
15 sub new {
16 my $class = shift;
17 my %obj;
18  
19 # Set default arguments. Paloa Alto defaults are set by the Device::Firewall::PaloAlto module.
20 my %arguments = (
21 vr => 'default',
22 vsys => 1,
23 sepchar => ',',
24 fields => '0,1,2,3,4',
25 @_
26 );
27  
28 # Extract and check the fields
29 $obj{fields} = [ split( ',', $arguments{fields} ) ];
30 if ( ( my $nfields = @{ $obj{fields} } ) != 5 ) {
31 die
32 "'fields' argument has $nfields comma-separated values; needs to be 5 - e.g. '2,4,7,8,9'";
33 }
34  
35 # Set up the firewall object
36 my %pa_args = %arguments{qw(uri username password insecure)};
37 $pa_args{verify_hostname} = delete $pa_args{insecure};
38 $obj{vr} = $arguments{vr};
39  
40 $obj{fw} = Device::Firewall::PaloAlto->new(%pa_args)->auth()
41 or die $obj{fw}->error;
42  
43 # Set up the CSV object
44 $obj{csv} =
45 Text::CSV->new( { binary => 1, sep_char => $arguments{sepchar} } )
46 or die Text::CSV->error_diag();
47  
48 # Open the logfile;
49 open( $obj{fh}, '<:encoding(utf8)', $arguments{logfile} )
50 or die "Could not open file '$arguments{logfile}'";
51  
52 return bless \%obj, $class;
53 }
54  
55 sub sep {
56 my $self = shift;
57 my ($sep_char) = @_;
58  
59 $self->{csv}->sep_char($sep_char);
60  
61 return $self;
62 }
63  
64 sub logfile {
65 my $self = shift;
66 my $filepath = shift;
67  
68 open( $self->{fh}, '<:encoding(utf8)', $filepath )
69 or die "Could not open file '$filepath'";
70  
71 return $self;
72 }
73  
74 sub fields {
75 my $self = shift;
76 my %column_numbers = @_;
77  
78 die "Five fields are required" unless ( keys %column_numbers == 5 );
79  
80 $self->{fields} =
81 [ @column_numbers{qw(src_ip dst_ip src_port dst_port protocol)} ];
82  
83 return $self;
84 }
85  
86 use constant {
87 SRC_IP => 0,
88 DST_IP => 1,
89 SRC_PORT => 2,
90 DST_PORT => 3,
91 PROTO => 4
92 };
93  
94 {
95  
96 # Cache for src/dst IP, dst port, and proto flows.
97 my %run_cache;
98  
99 sub run {
100 my $self = shift;
101 my $flow_cache_key;
102  
103 while ( my $row = $self->{csv}->getline( $self->{fh} ) ) {
104  
105 # Extract the flow fields
106 my @flow_info = @{$row}[ @{ $self->{fields} } ];
107  
108 my $num_fields = grep { defined $_ } @flow_info;
109 if ( $num_fields != 5 ) {
110 die
111 "Only $num_fields fields extracted based on the separating character. We expect 5";
112 }
113  
114 # Set up the cache key and return an entry if it exists
115 # Note that we don't cache based on source port.
116 $flow_cache_key =
117 join( ':', @flow_info[ SRC_IP, DST_IP, DST_PORT, PROTO ] );
118  
119 # Pull the result out of cache, or we test
120 # the security policy
121 my $result =
122 $run_cache{flow_cache_key} || $self->_test_sec_policy(@flow_info);
123 next unless $result;
124  
125 # Add the result to the cache if needed.
126 $run_cache{$flow_cache_key} //= $result;
127  
128 say $result->rulename . ','
129 . $result->action . ','
130 . $result->index . ',';
131 }
132  
133 }
134  
135 }
136  
137 sub _test_sec_policy {
138 my $self = shift;
139 my @flow_info = @_;
140  
141 # Find the zones for the source and dst IPs
142 my $src_zone = $self->ip_to_zone( $flow_info[SRC_IP] ) or return;
143 my $dst_zone = $self->ip_to_zone( $flow_info[DST_IP] ) or return;
144  
145 # Find the security policy
146 my $result = $self->{fw}->test->sec_policy(
147 from => $src_zone,
148 to => $dst_zone,
149 src_ip => $flow_info[SRC_IP],
150 dst_ip => $flow_info[DST_IP],
151 protocol => $flow_info[PROTO]
152 );
153  
154 return $result;
155 }
156  
157 {
158 # Cache for FIB entries so we don't continuously make calls out to
159 # fib_lookup
160 my %fib_cache;
161  
162 # Cache the interfaces on the firewall
163 my $interfaces;
164  
165 sub ip_to_zone {
166 my $self = shift;
167 my ($ip) = @_;
168  
169 # Find the egress interface from the FIB
170 # We check if the entry exists because we want to know about
171 # undefined routes that don't exist.
172 my $fib_entry;
173 if ( exists $fib_cache{$ip} ) {
174 $fib_entry = $fib_cache{$ip};
175 }
176 else {
177 $fib_entry = $fib_cache{$ip} = $self->{fw}->test->fib_lookup(
178 ip => $ip,
179 virtual_router => $self->{vr}
180 );
181 }
182 warn "No valid route for IP '$ip', skipping..." and return
183 unless $fib_entry;
184  
185 # FIXME: we're diving straight into the Device::Firewall::PaloAlto::Test::FIB's
186 # internal structure. Once its interface is better defined we'll go through that.
187 my $fib_interface = $fib_entry->{entries}[0]{interface};
188  
189 # Find the zone the interface is tethered in
190 $interfaces //= $self->{fw}->op->interfaces;
191 warn $interfaces->error and return unless $interfaces;
192  
193 my $interface = $interfaces->interface($fib_interface);
194 warn $interface->error unless $interface;
195  
196 my $zone = $interface->zone;
197 warn
198 "Interface '$fib_interface' does not appear to be in a zone, skipping..."
199 and return
200 unless $zone;
201  
202 return $zone;
203 }
204  
205 }
206  
207 1;
208  
209 __END__
↑ Summary