BlockerHelper/backends/panos.pm

filter: Statementstmt 35.1% 128 / 364 Branchbran 36.9% 54 / 146 Conditioncond 38.8% 14 / 36 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 80.0% 20 / 25 Podpod 100% 13 / 13 totaltotal 38.3% 232 / 605 CC 90 SCAR 37.3
CC
90
Cov
36%
new
80.6
_request
72.0
unban
43.8
CRAP 41.5
« BlockerHelper/backends/akamai.pm ↑ Summary BlockerHelper/backends/vyos.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 qw( Error::Helper Net::Firewall::BlockerHelper::Util );
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.2.0
17  
18 =cut
19  
20 1 our $VERSION = '0.2.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 4 ▶sub new {
Subroutine new: called CC=25 SCAR=43.9
Pod documented
104 4 my ( $blank, %opts ) = @_;
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 URL of the PAN-OS XML API.
244 #
245 # There is a single endpoint. What happens is decided by the type and cmd form
246 # parameters in the posted body rather than by the path, so unlike the REST
247 # backends there is no URL to build per operation.
248 #
249 # Takes no arguments; the scheme and host come from the options.
250 #
251 # Returns the URL as a string, with its trailing slash, which PAN-OS expects.
252 #
253 # $self->_url; # https://pan.example.org/api/
254 14 ▶sub _url {
Subroutine _url: called CC=1 SCAR=0.0
255 14 my ($self) = @_;
256  
257 14 return $self->{options}{scheme} . '://' . $self->{options}{host} . '/api/';
258 }
259  
260 # Internal helper. Assembles a form encoded request body from named
261 # parameters, used to build every request this backend sends.
262 #
263 # The keys are emitted in a fixed order rather than in hash order, so the same
264 # parameters always produce the same bytes. Without that the body recorded in
265 # test_data would vary run to run and could not be compared against a fixed
266 # expected string.
267 #
268 # Parameters that are undef are skipped rather than emitted empty, which is
269 # how the optional vsys is left out on an appliance that does not use virtual
270 # systems. Note that only the five known keys are ever emitted; anything else
271 # passed in is silently ignored, since the ordering list is what drives the
272 # output.
273 #
274 # Every value is percent encoded, which matters for cmd in particular since it
275 # carries XML.
276 #
277 # Args:
278 #
279 # %params - The parameters to encode. Recognized keys, emitted in this
280 # order, are: type, the API call type such as 'op' or 'user-id';
281 # action; key, the API key; cmd, the XML command; and vsys, the
282 # virtual system. Any may be omitted or undef, in which case it
283 # is left out.
284 #
285 # Returns the body as a form urlencoded string, ready to POST to _url.
286 #
287 # $self->_form_body( type => 'op', key => 'secret', cmd => '<show/>' );
288 # # type=op&key=secret&cmd=%3Cshow%2F%3E
289 14 ▶sub _form_body {
Subroutine _form_body: called CC=3 SCAR=11.0
290 14 my ( $self, %params ) = @_;
291  
292 14 my @pairs;
293 14 foreach my $key (qw(type action key cmd vsys)) {
294 70 ▶ next if ( !defined( $params{$key} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{$key} 17 53 70
295 53 push( @pairs, $key . '=' . $self->_uri_escape( $params{$key} ) );
296 }
297  
298 14 return join( '&', @pairs );
299 } ## end sub _form_body
300  
301 # Internal helper. Builds the User-ID uid-message XML that tags or untags one
302 # address.
303 #
304 # This backend does not write firewall rules or manage an address group.
305 # PAN-OS Dynamic Address Groups are defined by a tag match, so blocking works
306 # by attaching the configured tag to an address through the User-ID API; the
307 # DAG then picks it up and whatever policy references that group applies. That
308 # indirection is why a ban here touches no rule and no group directly.
309 #
310 # The XML is assembled by string concatenation rather than through an XML
311 # writer, so nothing escapes the values. That is safe for the addresses and
312 # configured tag names that actually reach it, but a tag holding an angle
313 # bracket or a quote would produce malformed XML.
314 #
315 # Args:
316 #
317 # action - What to do, as a plain string: 'register' to attach the tag or
318 # 'unregister' to remove it. Used as the element name, so
319 # anything else would produce XML PAN-OS rejects.
320 #
321 # ip - The address to tag, as a plain string. Expected to be an
322 # already validated and lowercased IPv4 or IPv6 address. Both
323 # families go through the same path; PAN-OS does not split tags
324 # by family.
325 #
326 # Returns the uid-message as an XML string, ready to become the cmd parameter
327 # of a user-id request.
328 #
329 # $self->_uid_message( 'register', '10.0.0.1' );
330 # # <uid-message><version>2.0</version><type>update</type><payload><register><entry ip="10.0.0.1"><tag><member>blocklist</member></tag></entry></register></payload></uid-message>
331 #
332 # $self->_uid_message( 'unregister', '10.0.0.1' );
333 # # the same with <unregister> in place of <register>
334 6 ▶sub _uid_message {
Subroutine _uid_message: called CC=1 SCAR=0.0
335 6 my ( $self, $action, $ip ) = @_;
336  
337 return
338 '<uid-message><version>2.0</version><type>update</type><payload><'
339 . $action
340 . '><entry ip="'
341 . $ip
342 . '"><tag><member>'
343 . $self->{options}{tag}
344 6 . '</member></tag></entry></'
345 . $action
346 . '></payload></uid-message>';
347 } ## end sub _uid_message
348  
349 # Internal helper. Builds the complete request body that tags or untags one
350 # address, which is what every ban and unban posts.
351 #
352 # This is just the uid-message wrapped up as a user-id API call with the key
353 # attached. The vsys parameter is only included when the option is set, since
354 # an appliance without virtual systems does not want it.
355 #
356 # Note the API key is part of the body here, so unlike the vyos backend there
357 # is no separation between a secret free operation and a credentialed one; the
358 # testing paths in this backend record what they record with that in mind.
359 #
360 # Args:
361 #
362 # action - What to do, as a plain string, 'register' or 'unregister'.
363 # Passed straight to _uid_message.
364 #
365 # ip - The address to tag, as a plain string. Expected to be an
366 # already validated and lowercased IPv4 or IPv6 address.
367 #
368 # Returns the body as a form urlencoded string, ready to POST to _url.
369 #
370 # $self->_register_body( 'register', '10.0.0.1' );
371 # # type=user-id&key=secret&cmd=%3Cuid-message%3E...%3C%2Fuid-message%3E
372 6 ▶sub _register_body {
Subroutine _register_body: called CC=2 SCAR=6.9
373 6 my ( $self, $action, $ip ) = @_;
374  
375 return $self->_form_body(
376 type => 'user-id',
377 key => $self->{options}{key},
378 cmd => $self->_uid_message( $action, $ip ),
379 6 ▶ ( defined( $self->{options}{vsys} ) ? ( vsys => $self->{options}{vsys} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'vsys'} ? : 5 1 6
380 );
381 } ## end sub _register_body
382  
383 # Internal helper. Builds the request body for a harmless read only call, used
384 # by init and check to confirm the appliance is reachable and the key works.
385 #
386 # Since this backend creates nothing on the appliance, there is no setup whose
387 # presence could be verified. What can be checked is that the API answers and
388 # the key is still valid, so a show system info is issued purely for its
389 # status: nothing parses what comes back.
390 #
391 # It is deliberately the cheapest call that still exercises authentication.
392 # The vsys parameter is included when set, so the probe goes through the same
393 # virtual system the real calls will.
394 #
395 # Takes no arguments; the key and vsys come from the options.
396 #
397 # Returns the body as a form urlencoded string, ready to POST to _url.
398 #
399 # $self->_probe_body;
400 # # type=op&key=secret&cmd=%3Cshow%3E%3Csystem%3E%3Cinfo%3E%3C%2Finfo%3E%3C%2Fsystem%3E%3C%2Fshow%3E
401 8 ▶sub _probe_body {
Subroutine _probe_body: called CC=2 SCAR=6.9
402 8 my ($self) = @_;
403  
404 return $self->_form_body(
405 type => 'op',
406 key => $self->{options}{key},
407 cmd => '<show><system><info></info></system></show>',
408 8 ▶ ( defined( $self->{options}{vsys} ) ? ( vsys => $self->{options}{vsys} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'vsys'} ? : 6 2 8
409 );
410 } ## end sub _probe_body
411  
412 # Internal helper. Performs one HTTP request against the PAN-OS XML API. Every
413 # call this backend makes to the appliance goes through here.
414 #
415 # There is one subtlety that makes this different from the JSON backends:
416 # PAN-OS reports application level failures with an HTTP 200. A rejected key
417 # or a malformed command comes back as a perfectly successful HTTP response
418 # whose XML carries status="error", so checking the HTTP status alone would
419 # treat those as successes and a ban would silently do nothing. The response
420 # body is therefore also checked for a top level status="success", and
421 # anything else is a failure regardless of the HTTP code.
422 #
423 # The user agent is built on first use and cached on the object. LWP::UserAgent
424 # is loaded with require at that point rather than at compile time, since only
425 # the HTTP backends need it; failing to load it dies with an explanation
426 # naming this backend. The insecure option turns off certificate verification,
427 # which is there because a PAN-OS appliance commonly presents a self signed
428 # certificate.
429 #
430 # There is no method or URL argument, since every call is a POST to the one
431 # endpoint. Authentication is the key inside the body rather than a header.
432 #
433 # Any failure, HTTP level or XML level, dies rather than setting an error. The
434 # callers wrap this in eval and turn the exception into the appropriate error
435 # code.
436 #
437 # Never called in testing mode; those paths record the bodies instead.
438 #
439 # Args:
440 #
441 # body - The complete form urlencoded body to post, as a plain string,
442 # normally from _register_body or _probe_body. Carries the API key.
443 #
444 # Returns the raw XML response body as a string. Note it is not parsed beyond
445 # the status check; callers that care inspect it themselves.
446 #
447 # $self->_request( $self->_register_body( 'register', $ip ) );
448 #
449 # # the usual shape at the call sites
450 # eval { $self->_request( $self->_probe_body ); };
451 # if ($@) { ... raise backendInitError ... }
452 0 ▶sub _request {
Subroutine _request: not called CC=8 SCAR=42.8
453 0 my ( $self, $body ) = @_;
454  
455 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
456 0 local $@;
457 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
458 0 require LWP::UserAgent;
459 my %args = (
460 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
461 timeout => $self->{options}{timeout},
462 0 );
463 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
464 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
465 }
466 0 $self->{ua} = LWP::UserAgent->new(%args);
467 0 1;
468 } or die( 'failed to load LWP::UserAgent, which the panos backend requires... ' . $@ );
469 }
470  
471 0 my @headers = ( 'Content-Type' => 'application/x-www-form-urlencoded' );
472  
473 0 require HTTP::Request;
474 0 my $request = HTTP::Request->new( 'POST', $self->_url, \@headers, $body );
475  
476 0 my $response = $self->{ua}->request($request);
477 0 my $content = $response->decoded_content;
478  
479 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
480 0 ▶ die( 'PAN-OS API call failed... HTTP status... '
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 0 0 0
481 . $response->status_line
482 . ' body... '
483 . ( defined($content) ? $content : '' ) );
484 }
485  
486 0 return $content;
487 } ## end sub _request
488  
489 =head2 init
490  
491 Initiates the backend. Verifies the key and connectivity with an op command.
492  
493 =cut
494  
495 2 ▶sub init {
Subroutine init: called CC=4 SCAR=20.9
Pod documented
496 2 my ( $self, %opts ) = @_;
497  
498 2 $self->errorblank;
499  
500 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
501 0 $self->{error} = 18;
502 0 $self->{errorString} = 'backend has already been inited';
503 0 $self->warn;
504 }
505  
506 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
507 2 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $self->_probe_body } ];
508 } else {
509 0 local $@;
510 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
511 0 $self->{error} = 23;
512 0 $self->{errorString} = 'init failed. probing the API failed... ' . $@;
513 0 $self->warn;
514 };
515 }
516  
517 2 $self->{inited} = 1;
518 } ## end sub init
519  
520 =head2 ban
521  
522 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
523 and lowercased, then registered with the tag by POSTing a User-ID
524 C<register> uid-message to the XML API. Banning an already banned IP is a
525 noop.
526  
527 $backend->ban(ban => $ip);
528  
529 =cut
530  
531 3 ▶sub ban {
Subroutine ban: called CC=10 SCAR=37.2
Pod documented
532 3 my ( $self, %opts ) = @_;
533  
534 3 $self->errorblank;
535  
536 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
537 0 $self->{error} = 1;
538 0 $self->{errorString} = 'backend has not been inited';
539 0 $self->warn;
540 0 return;
541 }
542  
543 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
544 0 $self->{error} = 9;
545 0 $self->{errorString} = 'Nothing specified for the value ban';
546 0 $self->warn;
547 0 return;
548 } elsif ( ref( $opts{ban} ) ne '' ) {
549 0 $self->{error} = 10;
550 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
551 0 $self->warn;
552 0 return;
553 ▶ } 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
554 && $opts{ban} !~ /\A$IPv6_re\z/ )
555 {
556 0 $self->{error} = 10;
557 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
558 0 $self->warn;
559 0 return;
560 }
561  
562 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
563 3 $opts{ban} = lc( $opts{ban} );
564  
565 3 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 3 3
566 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
567 0 $self->{frontend_obj}->{test_data} = 'already banned';
568 }
569 0 return;
570 }
571  
572 3 my $body = $self->_register_body( 'register', $opts{ban} );
573  
574 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
575 3 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $body } ];
576 } else {
577 0 local $@;
578 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
579 0 $self->{error} = 13;
580 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
581 0 $self->warn;
582 0 return;
583 };
584 }
585  
586 3 $self->{banned}{ $opts{ban} } = 1;
587 } ## end sub ban
588  
589 =head2 unban
590  
591 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
592 and lowercased, then unregistered from the tag by POSTing a User-ID
593 C<unregister> uid-message to the XML API. Unbanning an IP that is not
594 banned is a noop.
595  
596 $backend->unban(ban => $ip);
597  
598 =cut
599  
600 1 ▶sub unban {
Subroutine unban: called CC=10 SCAR=37.8
Pod documented
601 1 my ( $self, %opts ) = @_;
602  
603 1 $self->errorblank;
604  
605 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
606 0 $self->{error} = 1;
607 0 $self->{errorString} = 'backend has not been inited';
608 0 $self->warn;
609 0 return;
610 }
611  
612 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
613 0 $self->{error} = 9;
614 0 $self->{errorString} = 'Nothing specified for the value ban';
615 0 $self->warn;
616 0 return;
617 } elsif ( ref( $opts{ban} ) ne '' ) {
618 0 $self->{error} = 10;
619 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
620 0 $self->warn;
621 0 return;
622 ▶ } 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
623 && $opts{ban} !~ /\A$IPv6_re\z/ )
624 {
625 0 $self->{error} = 10;
626 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
627 0 $self->warn;
628 0 return;
629 }
630  
631 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
632 1 $opts{ban} = lc( $opts{ban} );
633  
634 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
635 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
636 0 $self->{frontend_obj}->{test_data} = 'not banned';
637 }
638 0 return;
639 }
640  
641 1 my $body = $self->_register_body( 'unregister', $opts{ban} );
642  
643 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
644 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $body } ];
645 } else {
646 0 local $@;
647 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
648 0 $self->{error} = 14;
649 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
650 0 $self->warn;
651 0 return;
652 };
653 }
654  
655 1 delete( $self->{banned}{ $opts{ban} } );
656 } ## end sub unban
657  
658 =head2 ban_cidr
659  
660 Bans a CIDR range by registering it with the tag via a User-ID C<register>
661 uid-message. PAN-OS User-ID accepts a network prefix in the same manner as
662 a single address. The value of ban is validated as being a IPv4 or IPv6
663 CIDR range and lowercased. Banning an already banned range is a noop.
664  
665 $backend->ban_cidr(ban => '1.2.3.0/24');
666  
667 =cut
668  
669 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=9 SCAR=36.0
Pod documented
670 1 my ( $self, %opts ) = @_;
671  
672 1 $self->errorblank;
673  
674 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
675 0 $self->{error} = 1;
676 0 $self->{errorString} = 'backend has not been inited';
677 0 $self->warn;
678 0 return;
679 }
680  
681 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
682 0 $self->{error} = 9;
683 0 $self->{errorString} = 'Nothing specified for the value ban';
684 0 $self->warn;
685 0 return;
686 } elsif ( ref( $opts{ban} ) ne '' ) {
687 0 $self->{error} = 34;
688 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
689 0 $self->warn;
690 0 return;
691 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
692 0 $self->{error} = 34;
693 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
694 0 $self->warn;
695 0 return;
696 }
697  
698 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
699 1 $opts{ban} = lc( $opts{ban} );
700  
701 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
702 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
703 0 $self->{frontend_obj}->{test_data} = 'already banned';
704 }
705 0 return;
706 }
707  
708 1 my $body = $self->_register_body( 'register', $opts{ban} );
709  
710 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
711 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $body } ];
712 } else {
713 0 local $@;
714 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
715 0 $self->{error} = 32;
716 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
717 0 $self->warn;
718 0 return;
719 };
720 }
721  
722 1 $self->{banned_cidr}{ $opts{ban} } = 1;
723 } ## end sub ban_cidr
724  
725 =head2 unban_cidr
726  
727 Unbans a CIDR range by unregistering it from the tag via a User-ID
728 C<unregister> uid-message. The value of ban is validated as being a IPv4 or
729 IPv6 CIDR range and lowercased. Unbanning a range that is not banned is a
730 noop.
731  
732 $backend->unban_cidr(ban => '1.2.3.0/24');
733  
734 =cut
735  
736 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=9 SCAR=36.0
Pod documented
737 1 my ( $self, %opts ) = @_;
738  
739 1 $self->errorblank;
740  
741 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
742 0 $self->{error} = 1;
743 0 $self->{errorString} = 'backend has not been inited';
744 0 $self->warn;
745 0 return;
746 }
747  
748 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
749 0 $self->{error} = 9;
750 0 $self->{errorString} = 'Nothing specified for the value ban';
751 0 $self->warn;
752 0 return;
753 } elsif ( ref( $opts{ban} ) ne '' ) {
754 0 $self->{error} = 34;
755 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
756 0 $self->warn;
757 0 return;
758 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
759 0 $self->{error} = 34;
760 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
761 0 $self->warn;
762 0 return;
763 }
764  
765 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
766 1 $opts{ban} = lc( $opts{ban} );
767  
768 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
769 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
770 0 $self->{frontend_obj}->{test_data} = 'not banned';
771 }
772 0 return;
773 }
774  
775 1 my $body = $self->_register_body( 'unregister', $opts{ban} );
776  
777 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
778 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $body } ];
779 } else {
780 0 local $@;
781 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
782 0 $self->{error} = 33;
783 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
784 0 $self->warn;
785 0 return;
786 };
787 }
788  
789 1 delete( $self->{banned_cidr}{ $opts{ban} } );
790 } ## end sub unban_cidr
791  
792 =head2 list_cidr
793  
794 List banned CIDR ranges. Returns an array of the currently banned CIDR
795 ranges. Single IPs are not included; for those see L</list>.
796  
797 my @banned_cidrs = $backend->list_cidr;
798  
799 =cut
800  
801 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
802 2 my ( $self, %opts ) = @_;
803  
804 2 $self->errorblank;
805  
806 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
807 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
808 }
809  
810 2 return keys( %{ $self->{banned_cidr} } );
811 }
812  
813 =head2 list
814  
815 List banned IPs. Returns an array of the currently banned single IPs. CIDR
816 ranges are not included; for those see L</list_cidr>.
817  
818 my @banned = $backend->list;
819  
820 =cut
821  
822 1 ▶sub list {
Subroutine list: called CC=2 SCAR=7.0
Pod documented
823 1 my ( $self, %opts ) = @_;
824  
825 1 $self->errorblank;
826  
827 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
828 1 $self->{frontend_obj}->{test_data} = 'list';
829 }
830  
831 1 return keys( %{ $self->{banned} } );
832 }
833  
834 =head2 re_init
835  
836 Tears down and re-inits, then re-registers all previously added bans.
837 Registrations are runtime state on the firewall, so this is how they are
838 restored after a reboot or a User-ID cache flush.
839  
840 =cut
841  
842 0 ▶sub re_init {
Subroutine re_init: not called CC=5 SCAR=34.0
Pod documented
843 0 my ( $self, %opts ) = @_;
844  
845 0 $self->errorblank;
846  
847 # teardown is best effort here as a partially or fully wiped setup is
848 # exactly what re_init needs to recover from
849 {
850 0 local $@;
851 0 eval { $self->teardown; };
852 }
853 0 $self->init;
854  
855 # both single IPs and CIDR ranges are re-registered the same way
856 0 my @to_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
857  
858 0 my @re_init_test_data;
859 0 foreach my $item (@to_ban) {
860 0 my $body = $self->_register_body( 'register', $item );
861  
862 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
863 0 push( @re_init_test_data, { method => 'POST', url => $self->_url, content => $body } );
864 } else {
865 0 local $@;
866 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
867 0 $self->{error} = 13;
868 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
869 0 $self->warn;
870 };
871 }
872 } ## end foreach my $item ( keys( %{ ...}))
873  
874 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
875 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
876 }
877  
878 0 $self->{inited} = 1;
879 } ## end sub re_init
880  
881 =head2 teardown
882  
883 Tears down the setup by unregistering each currently banned IP. The internal
884 list of bans is kept, so a following re_init will re-add them.
885  
886 =cut
887  
888 0 ▶sub teardown {
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
889 0 my ( $self, %opts ) = @_;
890  
891 0 $self->errorblank;
892  
893 0 $self->{inited} = 0;
894  
895 0 my @requests;
896 0 foreach my $item ( sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ) {
897 0 my $body = $self->_register_body( 'unregister', $item );
898 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
899 0 push( @requests, { method => 'POST', url => $self->_url, content => $body } );
900 } else {
901 0 local $@;
902 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
903 0 $self->{error} = 17;
904 0 $self->{errorString} = 'teardown failed unregistering "' . $item . '"... ' . $@;
905 0 $self->warn;
906 };
907 }
908 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
909  
910 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
911 0 $self->{frontend_obj}->{test_data} = \@requests;
912 }
913 } ## end sub teardown
914  
915 =head2 stop
916  
917 Alias for L</teardown>.
918  
919 =cut
920  
921 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
922 0 my ( $self, %opts ) = @_;
923  
924 0 return $self->teardown(%opts);
925 }
926  
927 =head2 check
928  
929 Verifies the key and connectivity are still usable with an op command.
930 Returns a true value if so and a false value otherwise.
931  
932 =cut
933  
934 6 ▶sub check {
Subroutine check: called CC=3 SCAR=15.4
Pod documented
935 6 my ( $self, %opts ) = @_;
936  
937 6 $self->errorblank;
938  
939 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
940 6 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_url, content => $self->_probe_body } ];
941 6 return 1;
942 }
943  
944 0 local $@;
945 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
946  
947 0 return 1;
948 } ## end sub check
949  
950 =head2 flush
951  
952 Removes all currently banned IPs at once by unregistering them and forgetting
953 them.
954  
955 =cut
956  
957 0 ▶sub flush {
Subroutine flush: not called CC=6 SCAR=37.4
Pod documented
958 0 my ( $self, %opts ) = @_;
959  
960 0 $self->errorblank;
961  
962 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
963 0 $self->{error} = 1;
964 0 $self->{errorString} = 'backend has not been inited';
965 0 $self->warn;
966 0 return;
967 }
968  
969 0 my @requests;
970 0 foreach my $item ( sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ) {
971 0 my $body = $self->_register_body( 'unregister', $item );
972 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
973 0 push( @requests, { method => 'POST', url => $self->_url, content => $body } );
974 } else {
975 0 local $@;
976 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
977 0 $self->{error} = 25;
978 0 $self->{errorString} = 'flush failed unregistering "' . $item . '"... ' . $@;
979 0 $self->warn;
980 };
981 }
982 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
983  
984 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
985 0 $self->{frontend_obj}->{test_data} = \@requests;
986 }
987  
988 0 $self->{banned} = {};
989 0 $self->{banned_cidr} = {};
990 } ## end sub flush
991  
992 =head1 ERROR CODES / FLAGS
993  
994 Error handling is provided by L<Error::Helper>. All
995 errors are considered fatal.
996  
997 =head2 1, notInited
998  
999 The backend has not been inited yet.
1000  
1001 =head2 6, invalidPrefixSpecified
1002  
1003 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1004  
1005 =head2 7, invalidName
1006  
1007 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1008  
1009 =head2 8, optionsNotHash
1010  
1011 The item passed to new for options is not a hash.
1012  
1013 =head2 9, noBanItem
1014  
1015 No IP or CIDR range specified to ban or unban.
1016  
1017 =head2 10, banItemNotIP
1018  
1019 The item to ban is not an IP. Either wrong ref type or regexp
1020 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1021  
1022 =head2 12, backendInitError
1023  
1024 Failed to init the backend.
1025  
1026 =head2 13, banFailed
1027  
1028 Failed to ban the item.
1029  
1030 =head2 14, unbanFailed
1031  
1032 Failed to unban the item.
1033  
1034 =head2 15, listFailed
1035  
1036 Failed to get a list of bans.
1037  
1038 =head2 16, reInitFailed
1039  
1040 Failed to re_init the backend.
1041  
1042 =head2 17, teardownFailed
1043  
1044 Failed to teardown the backend.
1045  
1046 =head2 18, alreadyInited
1047  
1048 init called, but the backend has already been inited.
1049  
1050 =head2 23, initFailed
1051  
1052 init failed. Probing the API with an op command failed.
1053  
1054 =head2 24, checkFailed
1055  
1056 The backend check raised an error.
1057  
1058 =head2 25, flushFailed
1059  
1060 Failed to flush the bans.
1061  
1062 =head2 26, portsNotSupported
1063  
1064 Ports were specified, but the backend does not support ports.
1065  
1066 =head2 27, protocolsNotSupported
1067  
1068 Protocols were specified, but the backend does not support protocols.
1069  
1070 =head2 30, hostNotDefined
1071  
1072 The option host is undef or blank.
1073  
1074 =head2 31, keyNotDefined
1075  
1076 The option key is undef or blank.
1077  
1078 =head2 32, banCidrFailed
1079  
1080 Failed to ban the CIDR range.
1081  
1082 =head2 33, unbanCidrFailed
1083  
1084 Failed to unban the CIDR range.
1085  
1086 =head2 34, cidrItemNotCidr
1087  
1088 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1089 IPv4 or IPv6 address followed by a prefix length valid for its family.
1090  
1091 =head2 35, cidrNotSupported
1092  
1093 The backend does not support CIDR bans.
1094  
1095 =head2 36, listCidrFailed
1096  
1097 Failed to get a list of CIDR bans.
1098  
1099 =head1 AUTHOR
1100  
1101 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1102  
1103 =head1 LICENSE AND COPYRIGHT
1104  
1105 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1106  
1107 This is free software, licensed under:
1108  
1109 The GNU Lesser General Public License, Version 2.1, February 1999
1110  
1111 =cut
1112  
1113 1 1; # End of Net::Firewall::BlockerHelper::backends::panos
« BlockerHelper/backends/akamai.pm ↑ Summary BlockerHelper/backends/vyos.pm »