BlockerHelper/backends/panos.pm

filter: Statementstmt 36.0% 137 / 380 Branchbran 36.5% 57 / 156 Conditioncond 35.5% 16 / 45 MC/DCmcdc 11.1% 3 / 27 Subroutinesub 81.4% 22 / 27 Podpod 100% 13 / 13 totaltotal 38.2% 248 / 648 CC 98 SCAR 37.0
CC
98
Cov
36%
new
80.6
_request
72.0
unban
43.8
CRAP 40.3
« BlockerHelper/backends/f5_bigip.pm ↑ Summary BlockerHelper/backends/akamai.pm »
1 package Net::Firewall::BlockerHelper::backends::panos;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::panos - Palo Alto Networks PAN-OS backend via the User-ID XML API.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 1 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => 'panos',
28 name => 'ssh',
29 options => {
30 host => 'fw.example.org',
31 key => $panos_api_key,
32 tag => 'blocklist',
33 },
34 );
35  
36 $fw_helper->init_backend;
37 $fw_helper->ban( ban => '1.2.3.4' );
38 $fw_helper->unban( ban => '1.2.3.4' );
39  
40 =head1 DESCRIPTION
41  
42 Blocks IPs on a Palo Alto Networks PAN-OS firewall by registering them with a
43 tag through the User-ID XML API. A Dynamic Address Group (DAG) on the firewall
44 is configured to match that tag, and a security policy references the DAG.
45 Banning an IP is then a matter of registering the IP-to-tag mapping; the DAG
46 membership, and therefore the enforcement, updates without a commit.
47  
48 This backend manages only the IP registrations. The Dynamic Address Group
49 matching the tag and the security policy referencing it must already exist on
50 the firewall.
51  
52 Auth is via a PAN-OS API key (the C<key> option), which you generate out of
53 band (the C<type=keygen> API call or the web UI). The same tag holds both
54 IPv4 and IPv6 registrations.
55  
56 Blocking is per IP; ports and protocols belong on the referencing policy, so
57 specifying them here is an error.
58  
59 L<LWP::UserAgent> is loaded at run time, so it is only required when this
60 backend is actually used. For https, L<LWP::Protocol::https> must be present
61 as well.
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 Initiates the object.
68  
69 - options :: Backend specific options. See below.
70 - prefix :: Prefix to use. Must match /^[a-zA-Z0-9]+$/. Default kur.
71 - name :: Name of this instance. Required.
72  
73 Ports and protocols are not supported and specifying either is an error.
74  
75 The options hash accepts the following.
76  
77 - host :: PAN-OS host, optionally host:port. Required.
78 - Default :: undef
79  
80 - key :: PAN-OS API key. Required.
81 - Default :: undef
82  
83 - tag :: The tag registered IPs are given, matched by the Dynamic Address
84 Group.
85 - Default :: <prefix>_<name>
86  
87 - vsys :: Optional vsys the registrations are scoped to (eg vsys1).
88 - Default :: undef
89  
90 - scheme :: 'https' or 'http'.
91 - Default :: https
92  
93 - insecure :: If true, skip TLS certificate verification.
94 - Default :: 0
95  
96 - timeout :: HTTP timeout in seconds.
97 - Default :: 30
98  
99 All errors are considered fatal, meaning if new fails it will die.
100  
101 =cut
102  
103 sub new {
104 4 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=25 SCAR=43.9
Pod documented
105  
106 4 my $self = {
107 perror => undef,
108 error => undef,
109 errorLine => undef,
110 errorFilename => undef,
111 errorString => "",
112 errorExtra => {
113 all_errors_fatal => 1,
114 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
115 # is kept for the name documented in its POD
116 all_fatal => 1,
117 flags => {
118 1 => 'notInited',
119 6 => 'invalidPrefixSpecified',
120 7 => 'invalidName',
121 8 => 'optionsNotHash',
122 9 => 'noBanItem',
123 10 => 'banItemNotIP',
124 12 => 'backendInitError',
125 13 => 'banFailed',
126 14 => 'unbanFailed',
127 15 => 'listFailed',
128 16 => 'reInitFailed',
129 17 => 'teardownFailed',
130 18 => 'alreadyInited',
131 23 => 'initFailed',
132 24 => 'checkFailed',
133 25 => 'flushFailed',
134 26 => 'portsNotSupported',
135 27 => 'protocolsNotSupported',
136 30 => 'hostNotDefined',
137 31 => 'keyNotDefined',
138 32 => 'banCidrFailed',
139 33 => 'unbanCidrFailed',
140 34 => 'cidrItemNotCidr',
141 35 => 'cidrNotSupported',
142 36 => 'listCidrFailed',
143 },
144 fatal_flags => {},
145 perror_not_fatal => 0,
146 },
147 options => {},
148 ports => [],
149 protocols => [],
150 testing => undef,
151 test_data => undef,
152 prefix => 'kur',
153 name => undef,
154 frontend_obj => undef,
155 inited => 0,
156 banned => {},
157 banned_cidr => {},
158 cidr_supported => 1,
159 ua => undef,
160 };
161 4 bless $self;
162  
163 # blocking is per IP; ports/protocols belong on the referencing policy
164 4 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) eq 'ARRAY' && defined( $opts{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]) 0 4 4
Condition33%2/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
040
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/3
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
defined $opts{"ports"} ref $opts{"ports"} eq "ARRAY" defined $opts{"ports"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"ports"}B= ref $opts{"ports"} eq "ARRAY"C= defined $opts{"ports"}[0]
ABCresult
0XX0
10X0
1100
1111
165 0 $self->{perror} = 1;
166 0 $self->{error} = 26;
167 0 $self->{errorString} = 'the panos backend registers whole IPs and does not support ports';
168 0 $self->warn;
169 }
170 4 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) eq 'ARRAY' && defined( $opts{protocols}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]) 0 4 4
Condition33%2/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
040
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
defined $opts{"protocols"} ref $opts{"protocols"} eq "ARRAY" defined $opts{"protocols"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"protocols"}B= ref $opts{"protocols"} eq "ARRAY"C= defined $opts{"protocols"}[0]
ABCresult
0XX0
10X0
1100
1111
171 0 $self->{perror} = 1;
172 0 $self->{error} = 27;
173 0 $self->{errorString} = 'the panos backend registers whole IPs and does not support protocols';
174 0 $self->warn;
175 }
176  
177 # make sure prefix is sane if defined
178 4 ▶ if ( defined( $opts{prefix} ) && $opts{prefix} !~ /^[a-zA-Z0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'prefix'} and not $opts{'prefix'} =~ /^[a-zA-Z0-9]+$/) { } 0 4 4
elsif (defined $opts{'prefix'}) { } 4 0 4
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
040
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
179 0 $self->{perror} = 1;
180 0 $self->{error} = 6;
181 $self->{errorString}
182 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
183 0 $self->warn;
184 } elsif ( defined( $opts{prefix} ) ) {
185 4 $self->{prefix} = $opts{prefix};
186 }
187  
188 # make sure we have a name and that it is valid
189 4 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 4 4
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 4 4
190 0 $self->{perror} = 1;
191 0 $self->{error} = 7;
192 0 $self->{errorString} = 'name is undef';
193 0 $self->warn;
194 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
195 0 $self->{perror} = 1;
196 0 $self->{error} = 7;
197 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
198 0 $self->warn;
199 }
200 4 $self->{name} = $opts{name};
201  
202 4 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 4 0 4
203 4 $self->{testing} = $opts{testing};
204 }
205 4 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 4 0 4
206 4 $self->{frontend_obj} = $opts{frontend_obj};
207 }
208  
209 4 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 4 0 4
210 4 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 4 4
211 0 $self->{perror} = 1;
212 0 $self->{error} = 8;
213 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
214 0 $self->warn;
215 }
216 4 $self->{options} = $opts{options};
217 }
218  
219 4 ▶ if ( !defined( $self->{options}{host} ) || $self->{options}{host} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq "") 1 3 4
Condition66%2/3
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
not defined $self->{"options"}{"host"} $self->{"options"}{"host"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"host"}B= $self->{"options"}{"host"} eq ""
ABresult
1X1
011
000
220 1 $self->{perror} = 1;
221 1 $self->{error} = 30;
222 1 $self->{errorString} = 'the option host is undef or blank';
223 1 $self->warn;
224 }
225 3 ▶ if ( !defined( $self->{options}{key} ) || $self->{options}{key} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"key"} or $self->{"options"}{"key"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"key"} or $self->{"options"}{"key"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"key"} or $self->{"options"}{"key"} eq ""
not defined $self->{"options"}{"key"} $self->{"options"}{"key"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"key"}B= $self->{"options"}{"key"} eq ""
ABresult
1X1
011
000
226 1 $self->{perror} = 1;
227 1 $self->{error} = 31;
228 1 $self->{errorString} = 'the option key is undef or blank';
229 1 $self->warn;
230 }
231  
232 # defaults
233 2 ▶ $self->{options}{scheme} = 'https' if ( !defined( $self->{options}{scheme} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"scheme"} 2 0 2
234 2 ▶ $self->{options}{insecure} = 0 if ( !defined( $self->{options}{insecure} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"insecure"} 2 0 2
235 2 ▶ $self->{options}{timeout} = 30 if ( !defined( $self->{options}{timeout} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"timeout"} 2 0 2
236 2 ▶ if ( defined( $self->{name} ) && !defined( $self->{options}{tag} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"name"} and not defined $self->{"options"}{"tag"}) 1 1 2
Condition66%2/3
defined $self->{"name"} and not defined $self->{"options"}{"tag"}
!ll&&!rl&&r
011
MC/DC50%1/2
defined $self->{"name"} and not defined $self->{"options"}{"tag"}
defined $self->{"name"} not defined $self->{"options"}{"tag"}
Truth table66%2/3
A and not(B)
A= defined $self->{"name"}B= not defined $self->{"options"}{"tag"}
ABresult
0X0
100
111
237 1 $self->{options}{tag} = $self->{prefix} . '_' . $self->{name};
238 }
239  
240 2 return $self;
241 } ## end sub new
242  
243 # Internal helper. Returns the PAN-OS API endpoint.
244 sub _url {
245 14 ▶ my ($self) = @_;
Subroutine _url: called CC=1 SCAR=0.0
246  
247 14 return $self->{options}{scheme} . '://' . $self->{options}{host} . '/api/';
248 }
249  
250 # Internal helper. Minimal percent encoder so URI::Escape is not needed.
251 sub _uri_escape {
252 53 ▶ my ( $self, $string ) = @_;
Subroutine _uri_escape: called CC=1 SCAR=0.0
253  
254 53 $string =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
255  
256 53 return $string;
257 }
258  
259 # Internal helper. Builds an x-www-form-urlencoded body from the passed
260 # parameters, in a fixed key order so the output is stable.
261 sub _form_body {
262 14 ▶ my ( $self, %params ) = @_;
Subroutine _form_body: called CC=3 SCAR=11.0
263  
264 14 my @pairs;
265 14 foreach my $key (qw(type action key cmd vsys)) {
266 70 ▶ next if ( !defined( $params{$key} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{$key} 17 53 70
267 53 push( @pairs, $key . '=' . $self->_uri_escape( $params{$key} ) );
268 }
269  
270 14 return join( '&', @pairs );
271 } ## end sub _form_body
272  
273 # Internal helper. Builds the User-ID uid-message XML for the given action
274 # ('register' or 'unregister') and IP, tagging it with the configured tag.
275 sub _uid_message {
276 6 ▶ my ( $self, $action, $ip ) = @_;
Subroutine _uid_message: called CC=1 SCAR=0.0
277  
278 return
279 '<uid-message><version>2.0</version><type>update</type><payload><'
280 . $action
281 . '><entry ip="'
282 . $ip
283 . '"><tag><member>'
284 . $self->{options}{tag}
285 6 . '</member></tag></entry></'
286 . $action
287 . '></payload></uid-message>';
288 } ## end sub _uid_message
289  
290 # Internal helper. Builds the form body for registering/unregistering an IP.
291 sub _register_body {
292 6 ▶ my ( $self, $action, $ip ) = @_;
Subroutine _register_body: called CC=2 SCAR=6.9
293  
294 return $self->_form_body(
295 type => 'user-id',
296 key => $self->{options}{key},
297 cmd => $self->_uid_message( $action, $ip ),
298 6 ▶ ( defined( $self->{options}{vsys} ) ? ( vsys => $self->{options}{vsys} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'vsys'} ? : 5 1 6
299 );
300 } ## end sub _register_body
301  
302 # Internal helper. Builds the form body for a lightweight op command used to
303 # verify the key and connectivity.
304 sub _probe_body {
305 8 ▶ my ($self) = @_;
Subroutine _probe_body: called CC=2 SCAR=6.9
306  
307 return $self->_form_body(
308 type => 'op',
309 key => $self->{options}{key},
310 cmd => '<show><system><info></info></system></show>',
311 8 ▶ ( defined( $self->{options}{vsys} ) ? ( vsys => $self->{options}{vsys} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'vsys'} ? : 6 2 8
312 );
313 } ## end sub _probe_body
314  
315 # Internal helper. POSTs the form body to the PAN-OS API via LWP::UserAgent,
316 # returning the response body on success and dying otherwise. PAN-OS answers
317 # with XML; a top level status="success" is treated as success. Never
318 # called in testing mode.
319 sub _request {
320 0 ▶ my ( $self, $body ) = @_;
Subroutine _request: not called CC=8 SCAR=42.8
321  
322 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
323 0 local $@;
324 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent; my(%args) = ("agent", 'Net::Firewall::BlockerHelper/' . $Net::Firewall::BlockerHelper::backends::panos::VERSION, "timeout", $self->{"options"}{"timeout"}); if ($self->{"options"}{"insecure"}) { $args{"ssl_opts"} = {"verify_hostname", 0, "SSL_verify_mode", 0}; } ; $self->{"ua"} = "LWP::UserAgent"->new(%args); 1 } } or die 'failed to load LWP::UserAgent, which the panos backend requires... ' . $@ 0 0 0
325 0 require LWP::UserAgent;
326 my %args = (
327 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
328 timeout => $self->{options}{timeout},
329 0 );
330 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
331 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
332 }
333 0 $self->{ua} = LWP::UserAgent->new(%args);
334 0 1;
335 } or die( 'failed to load LWP::UserAgent, which the panos backend requires... ' . $@ );
336 }
337  
338 0 my @headers = ( 'Content-Type' => 'application/x-www-form-urlencoded' );
339  
340 0 require HTTP::Request;
341 0 my $request = HTTP::Request->new( 'POST', $self->_url, \@headers, $body );
342  
343 0 my $response = $self->{ua}->request($request);
344 0 my $content = $response->decoded_content;
345  
346 0 ▶ if ( !$response->is_success || !defined($content) || $content !~ /status="success"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success and defined $content and $content =~ /status="success"/) 0 0 0
Condition0%0/6
$response->is_success and defined $content and $content =~ /status="success"/
!ll&&!rl&&r
000
$response->is_success and defined $content
!ll&&!rl&&r
000
MC/DC0%0/3
$response->is_success and defined $content and $content =~ /status="success"/
$response->is_success defined $content $content =~ /status="success"/
Truth table0%0/4
A and B and C
A= $response->is_successB= defined $contentC= $content =~ /status="success"/
ABCresult
0XX0
10X0
1100
1111
347 0 ▶ die( 'PAN-OS API call failed... HTTP status... '
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 0 0 0
348 . $response->status_line
349 . ' body... '
350 . ( defined($content) ? $content : '' ) );
351 }
352  
353 0 return $content;
354 } ## end sub _request
355  
356 =head2 init
357  
358 Initiates the backend. Verifies the key and connectivity with an op command.
359  
360 =cut
361  
362 sub init {
363 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=20.9
Pod documented
364  
365 2 $self->errorblank;
366  
367 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
368 0 $self->{error} = 18;
369 0 $self->{errorString} = 'backend has already been inited';
370 0 $self->warn;
371 }
372  
373 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
374 2 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $self->_probe_body } ];
375 } else {
376 0 local $@;
377 0 ▶ eval { $self->_request( $self->_probe_body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_probe_body); 1 } } or do { $self->{"error"} = 23; $self->{"errorString"} = 'init failed. probing the API failed... ' . $@; $self->warn } 0 0 0
378 0 $self->{error} = 23;
379 0 $self->{errorString} = 'init failed. probing the API failed... ' . $@;
380 0 $self->warn;
381 };
382 }
383  
384 2 $self->{inited} = 1;
385 } ## end sub init
386  
387 =head2 ban
388  
389 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
390 and lowercased, then registered with the tag by POSTing a User-ID
391 C<register> uid-message to the XML API. Banning an already banned IP is a
392 noop.
393  
394 $backend->ban(ban => $ip);
395  
396 =cut
397  
398 sub ban {
399 3 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=37.2
Pod documented
400  
401 3 $self->errorblank;
402  
403 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
404 0 $self->{error} = 1;
405 0 $self->{errorString} = 'backend has not been inited';
406 0 $self->warn;
407 0 return;
408 }
409  
410 3 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 3 3
elsif (ref $opts{'ban'} ne '') { } 0 3 3
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/) { } 0 3 3
411 0 $self->{error} = 9;
412 0 $self->{errorString} = 'Nothing specified for the value ban';
413 0 $self->warn;
414 0 return;
415 } elsif ( ref( $opts{ban} ) ne '' ) {
416 0 $self->{error} = 10;
417 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
418 0 $self->warn;
419 0 return;
420 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/
l!l&&r!l&&!r
210
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/
ABresult
1X1
011
000
421 && $opts{ban} !~ /\A$IPv6_re\z/ )
422 {
423 0 $self->{error} = 10;
424 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
425 0 $self->warn;
426 0 return;
427 }
428  
429 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
430 3 $opts{ban} = lc( $opts{ban} );
431  
432 3 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 3 3
433 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
434 0 $self->{frontend_obj}->{test_data} = 'already banned';
435 }
436 0 return;
437 }
438  
439 3 my $body = $self->_register_body( 'register', $opts{ban} );
440  
441 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
442 3 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $body } ];
443 } else {
444 0 local $@;
445 0 ▶ eval { $self->_request($body); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
446 0 $self->{error} = 13;
447 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
448 0 $self->warn;
449 0 return;
450 };
451 }
452  
453 3 $self->{banned}{ $opts{ban} } = 1;
454 } ## end sub ban
455  
456 =head2 unban
457  
458 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
459 and lowercased, then unregistered from the tag by POSTing a User-ID
460 C<unregister> uid-message to the XML API. Unbanning an IP that is not
461 banned is a noop.
462  
463 $backend->unban(ban => $ip);
464  
465 =cut
466  
467 sub unban {
468 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=37.8
Pod documented
469  
470 1 $self->errorblank;
471  
472 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
473 0 $self->{error} = 1;
474 0 $self->{errorString} = 'backend has not been inited';
475 0 $self->warn;
476 0 return;
477 }
478  
479 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/) { } 0 1 1
480 0 $self->{error} = 9;
481 0 $self->{errorString} = 'Nothing specified for the value ban';
482 0 $self->warn;
483 0 return;
484 } elsif ( ref( $opts{ban} ) ne '' ) {
485 0 $self->{error} = 10;
486 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
487 0 $self->warn;
488 0 return;
489 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/
ABresult
1X1
011
000
490 && $opts{ban} !~ /\A$IPv6_re\z/ )
491 {
492 0 $self->{error} = 10;
493 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
494 0 $self->warn;
495 0 return;
496 }
497  
498 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
499 1 $opts{ban} = lc( $opts{ban} );
500  
501 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
502 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
503 0 $self->{frontend_obj}->{test_data} = 'not banned';
504 }
505 0 return;
506 }
507  
508 1 my $body = $self->_register_body( 'unregister', $opts{ban} );
509  
510 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
511 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $body } ];
512 } else {
513 0 local $@;
514 0 ▶ eval { $self->_request($body); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($body); 1 } } or do { $self->{"error"} = 14; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
515 0 $self->{error} = 14;
516 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
517 0 $self->warn;
518 0 return;
519 };
520 }
521  
522 1 delete( $self->{banned}{ $opts{ban} } );
523 } ## end sub unban
524  
525 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
526 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
527 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
528 # IPv6). Returns false otherwise.
529 sub _valid_cidr {
530 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
531  
532 2 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 2 2
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table33%1/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
533  
534 2 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 2 0 2
535 2 my ( $addr, $prefix ) = ( $1, $2 );
536 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
537 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::panos::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
538 }
539  
540 0 return 0;
541 } ## end sub _valid_cidr
542  
543 =head2 ban_cidr
544  
545 Bans a CIDR range by registering it with the tag via a User-ID C<register>
546 uid-message. PAN-OS User-ID accepts a network prefix in the same manner as
547 a single address. The value of ban is validated as being a IPv4 or IPv6
548 CIDR range and lowercased. Banning an already banned range is a noop.
549  
550 $backend->ban_cidr(ban => '1.2.3.0/24');
551  
552 =cut
553  
554 sub ban_cidr {
555 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=9 SCAR=36.0
Pod documented
556  
557 1 $self->errorblank;
558  
559 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
560 0 $self->{error} = 1;
561 0 $self->{errorString} = 'backend has not been inited';
562 0 $self->warn;
563 0 return;
564 }
565  
566 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 1 1
567 0 $self->{error} = 9;
568 0 $self->{errorString} = 'Nothing specified for the value ban';
569 0 $self->warn;
570 0 return;
571 } elsif ( ref( $opts{ban} ) ne '' ) {
572 0 $self->{error} = 34;
573 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
574 0 $self->warn;
575 0 return;
576 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
577 0 $self->{error} = 34;
578 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
579 0 $self->warn;
580 0 return;
581 }
582  
583 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
584 1 $opts{ban} = lc( $opts{ban} );
585  
586 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
587 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
588 0 $self->{frontend_obj}->{test_data} = 'already banned';
589 }
590 0 return;
591 }
592  
593 1 my $body = $self->_register_body( 'register', $opts{ban} );
594  
595 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
596 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $body } ];
597 } else {
598 0 local $@;
599 0 ▶ eval { $self->_request($body); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($body); 1 } } or do { $self->{"error"} = 32; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
600 0 $self->{error} = 32;
601 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
602 0 $self->warn;
603 0 return;
604 };
605 }
606  
607 1 $self->{banned_cidr}{ $opts{ban} } = 1;
608 } ## end sub ban_cidr
609  
610 =head2 unban_cidr
611  
612 Unbans a CIDR range by unregistering it from the tag via a User-ID
613 C<unregister> uid-message. The value of ban is validated as being a IPv4 or
614 IPv6 CIDR range and lowercased. Unbanning a range that is not banned is a
615 noop.
616  
617 $backend->unban_cidr(ban => '1.2.3.0/24');
618  
619 =cut
620  
621 sub unban_cidr {
622 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=9 SCAR=36.0
Pod documented
623  
624 1 $self->errorblank;
625  
626 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
627 0 $self->{error} = 1;
628 0 $self->{errorString} = 'backend has not been inited';
629 0 $self->warn;
630 0 return;
631 }
632  
633 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 1 1
634 0 $self->{error} = 9;
635 0 $self->{errorString} = 'Nothing specified for the value ban';
636 0 $self->warn;
637 0 return;
638 } elsif ( ref( $opts{ban} ) ne '' ) {
639 0 $self->{error} = 34;
640 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
641 0 $self->warn;
642 0 return;
643 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
644 0 $self->{error} = 34;
645 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
646 0 $self->warn;
647 0 return;
648 }
649  
650 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
651 1 $opts{ban} = lc( $opts{ban} );
652  
653 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
654 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
655 0 $self->{frontend_obj}->{test_data} = 'not banned';
656 }
657 0 return;
658 }
659  
660 1 my $body = $self->_register_body( 'unregister', $opts{ban} );
661  
662 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
663 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $body } ];
664 } else {
665 0 local $@;
666 0 ▶ eval { $self->_request($body); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($body); 1 } } or do { $self->{"error"} = 33; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
667 0 $self->{error} = 33;
668 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
669 0 $self->warn;
670 0 return;
671 };
672 }
673  
674 1 delete( $self->{banned_cidr}{ $opts{ban} } );
675 } ## end sub unban_cidr
676  
677 =head2 list_cidr
678  
679 List banned CIDR ranges. Returns an array of the currently banned CIDR
680 ranges. Single IPs are not included; for those see L</list>.
681  
682 my @banned_cidrs = $backend->list_cidr;
683  
684 =cut
685  
686 sub list_cidr {
687 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
688  
689 2 $self->errorblank;
690  
691 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
692 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
693 }
694  
695 2 return keys( %{ $self->{banned_cidr} } );
696 }
697  
698 =head2 list
699  
700 List banned IPs. Returns an array of the currently banned single IPs. CIDR
701 ranges are not included; for those see L</list_cidr>.
702  
703 my @banned = $backend->list;
704  
705 =cut
706  
707 sub list {
708 1 ▶ my ( $self, %opts ) = @_;
Subroutine list: called CC=2 SCAR=7.0
Pod documented
709  
710 1 $self->errorblank;
711  
712 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
713 1 $self->{frontend_obj}->{test_data} = 'list';
714 }
715  
716 1 return keys( %{ $self->{banned} } );
717 }
718  
719 =head2 re_init
720  
721 Tears down and re-inits, then re-registers all previously added bans.
722 Registrations are runtime state on the firewall, so this is how they are
723 restored after a reboot or a User-ID cache flush.
724  
725 =cut
726  
727 sub re_init {
728 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=6 SCAR=37.4
Pod documented
729  
730 0 $self->errorblank;
731  
732 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
733 0 $self->{error} = 1;
734 0 $self->{errorString} = 'backend has not been inited';
735 0 $self->warn;
736 0 return;
737 }
738  
739 # teardown is best effort here as a partially or fully wiped setup is
740 # exactly what re_init needs to recover from
741 {
742 0 local $@;
743 0 eval { $self->teardown; };
744 }
745 0 $self->init;
746  
747 # both single IPs and CIDR ranges are re-registered the same way
748 0 my @to_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
749  
750 0 my @re_init_test_data;
751 0 foreach my $item (@to_ban) {
752 0 my $body = $self->_register_body( 'register', $item );
753  
754 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
755 0 push( @re_init_test_data, { method => 'POST', url => $self->_url, content => $body } );
756 } else {
757 0 local $@;
758 0 ▶ eval { $self->_request($body); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $item . '" failed... ' . $@; $self->warn } 0 0 0
759 0 $self->{error} = 13;
760 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
761 0 $self->warn;
762 };
763 }
764 } ## end foreach my $item ( keys( %{ ...}))
765  
766 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
767 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
768 }
769  
770 0 $self->{inited} = 1;
771 } ## end sub re_init
772  
773 =head2 teardown
774  
775 Tears down the setup by unregistering each currently banned IP. The internal
776 list of bans is kept, so a following re_init will re-add them.
777  
778 =cut
779  
780 sub teardown {
781 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
782  
783 0 $self->errorblank;
784  
785 0 $self->{inited} = 0;
786  
787 0 my @requests;
788 0 foreach my $item ( sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ) {
789 0 my $body = $self->_register_body( 'unregister', $item );
790 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
791 0 push( @requests, { method => 'POST', url => $self->_url, content => $body } );
792 } else {
793 0 local $@;
794 0 ▶ eval { $self->_request($body); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($body); 1 } } or do { $self->{"error"} = 17; $self->{"errorString"} = 'teardown failed unregistering "' . $item . '"... ' . $@; $self->warn } 0 0 0
795 0 $self->{error} = 17;
796 0 $self->{errorString} = 'teardown failed unregistering "' . $item . '"... ' . $@;
797 0 $self->warn;
798 };
799 }
800 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
801  
802 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
803 0 $self->{frontend_obj}->{test_data} = \@requests;
804 }
805 } ## end sub teardown
806  
807 =head2 stop
808  
809 Alias for L</teardown>.
810  
811 =cut
812  
813 sub stop {
814 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
815  
816 0 return $self->teardown(%opts);
817 }
818  
819 =head2 check
820  
821 Verifies the key and connectivity are still usable with an op command.
822 Returns a true value if so and a false value otherwise.
823  
824 =cut
825  
826 sub check {
827 6 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=15.4
Pod documented
828  
829 6 $self->errorblank;
830  
831 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
832 6 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $self->_probe_body } ];
833 6 return 1;
834 }
835  
836 0 local $@;
837 0 ▶ eval { $self->_request( $self->_probe_body ); 1; } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_probe_body); 1 } } or return 0 0 0 0
838  
839 0 return 1;
840 } ## end sub check
841  
842 =head2 flush
843  
844 Removes all currently banned IPs at once by unregistering them and forgetting
845 them.
846  
847 =cut
848  
849 sub flush {
850 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=6 SCAR=37.4
Pod documented
851  
852 0 $self->errorblank;
853  
854 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
855 0 $self->{error} = 1;
856 0 $self->{errorString} = 'backend has not been inited';
857 0 $self->warn;
858 0 return;
859 }
860  
861 0 my @requests;
862 0 foreach my $item ( sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ) {
863 0 my $body = $self->_register_body( 'unregister', $item );
864 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
865 0 push( @requests, { method => 'POST', url => $self->_url, content => $body } );
866 } else {
867 0 local $@;
868 0 ▶ eval { $self->_request($body); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($body); 1 } } or do { $self->{"error"} = 25; $self->{"errorString"} = 'flush failed unregistering "' . $item . '"... ' . $@; $self->warn } 0 0 0
869 0 $self->{error} = 25;
870 0 $self->{errorString} = 'flush failed unregistering "' . $item . '"... ' . $@;
871 0 $self->warn;
872 };
873 }
874 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
875  
876 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
877 0 $self->{frontend_obj}->{test_data} = \@requests;
878 }
879  
880 0 $self->{banned} = {};
881 0 $self->{banned_cidr} = {};
882 } ## end sub flush
883  
884 =head1 ERROR CODES / FLAGS
885  
886 Error handling is provided by L<Error::Helper>. All
887 errors are considered fatal.
888  
889 =head2 1, notInited
890  
891 The backend has not been inited yet.
892  
893 =head2 6, invalidPrefixSpecified
894  
895 The specified prefix did not match /^[a-zA-Z0-9]+$/.
896  
897 =head2 7, invalidName
898  
899 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
900  
901 =head2 8, optionsNotHash
902  
903 The item passed to new for options is not a hash.
904  
905 =head2 9, noBanItem
906  
907 No IP or CIDR range specified to ban or unban.
908  
909 =head2 10, banItemNotIP
910  
911 The item to ban is not an IP. Either wrong ref type or regexp
912 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
913  
914 =head2 12, backendInitError
915  
916 Failed to init the backend.
917  
918 =head2 13, banFailed
919  
920 Failed to ban the item.
921  
922 =head2 14, unbanFailed
923  
924 Failed to unban the item.
925  
926 =head2 15, listFailed
927  
928 Failed to get a list of bans.
929  
930 =head2 16, reInitFailed
931  
932 Failed to re_init the backend.
933  
934 =head2 17, teardownFailed
935  
936 Failed to teardown the backend.
937  
938 =head2 18, alreadyInited
939  
940 init called, but the backend has already been inited.
941  
942 =head2 23, initFailed
943  
944 init failed. Probing the API with an op command failed.
945  
946 =head2 24, checkFailed
947  
948 The backend check raised an error.
949  
950 =head2 25, flushFailed
951  
952 Failed to flush the bans.
953  
954 =head2 26, portsNotSupported
955  
956 Ports were specified, but the backend does not support ports.
957  
958 =head2 27, protocolsNotSupported
959  
960 Protocols were specified, but the backend does not support protocols.
961  
962 =head2 30, hostNotDefined
963  
964 The option host is undef or blank.
965  
966 =head2 31, keyNotDefined
967  
968 The option key is undef or blank.
969  
970 =head2 32, banCidrFailed
971  
972 Failed to ban the CIDR range.
973  
974 =head2 33, unbanCidrFailed
975  
976 Failed to unban the CIDR range.
977  
978 =head2 34, cidrItemNotCidr
979  
980 The item to ban is not a CIDR range. Either wrong ref type or it is not an
981 IPv4 or IPv6 address followed by a prefix length valid for its family.
982  
983 =head2 35, cidrNotSupported
984  
985 The backend does not support CIDR bans.
986  
987 =head2 36, listCidrFailed
988  
989 Failed to get a list of CIDR bans.
990  
991 =head1 AUTHOR
992  
993 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
994  
995 =head1 LICENSE AND COPYRIGHT
996  
997 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
998  
999 This is free software, licensed under:
1000  
1001 The GNU Lesser General Public License, Version 2.1, February 1999
1002  
1003 =cut
1004  
1005 1 1; # End of Net::Firewall::BlockerHelper::backends::panos
« BlockerHelper/backends/f5_bigip.pm ↑ Summary BlockerHelper/backends/akamai.pm »