BlockerHelper/backends/checkpoint.pm

filter: Statementstmt 33.2% 134 / 403 Branchbran 32.2% 51 / 158 Conditioncond 33.3% 15 / 45 MC/DCmcdc 10.7% 3 / 28 Subroutinesub 76.9% 20 / 26 Podpod 100% 13 / 13 totaltotal 35.0% 236 / 673 CC 105 SCAR 38.8
CC
105
Cov
33%
_request
90.0
new
80.6
re_init
56.0
CRAP 48.6
« BlockerHelper/backends/cloudflare.pm ↑ Summary BlockerHelper/backends/fortigate.pm »
1 package Net::Firewall::BlockerHelper::backends::checkpoint;
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::checkpoint - Check Point Management (web_api) backend.
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 => 'checkpoint',
28 name => 'ssh',
29 options => {
30 host => 'mgmt.example.org',
31 user => 'automation',
32 password => $checkpoint_password,
33 group => 'blocklist',
34 },
35 );
36  
37 $fw_helper->init_backend;
38 $fw_helper->ban( ban => '1.2.3.4' );
39 $fw_helper->unban( ban => '1.2.3.4' );
40  
41 =head1 DESCRIPTION
42  
43 Blocks IPs on a Check Point Security Management server via its C<web_api>. For
44 each banned IP a host object is created and added as a member of a group object;
45 a rule in the policy referencing that group then drops the traffic. The group
46 object (default C<< <prefix>_<name> >>) and the drop rule referencing it must
47 already exist; this backend creates neither, only the host objects added to the
48 group.
49  
50 Authentication is session based. L</init> POSTs to C<< /web_api/login >> with the
51 configured user and password and stores the returned session id. Every following
52 call sends that session id in the C<X-chkp-sid> header. All calls are POSTs
53 carrying JSON bodies.
54  
55 The management server is reached at C<< https://<host> >> (port 443 by default).
56  
57 Blocking is per IP; ports and protocols belong on the referencing rule, so
58 specifying them here is an error.
59  
60 B<Enforcement note:> C<publish> only saves the change to the management
61 database. Actually enforcing the block on the gateways additionally requires an
62 C<install-policy>, which is intentionally out of scope for this backend.
63  
64 L<LWP::UserAgent> is loaded at run time, so it is only required when this backend
65 is actually used. For https, L<LWP::Protocol::https> must be present as well.
66  
67 =head1 NOTES
68  
69 This backend was written going off the API docs and actual testing is
70 needed to double check a few things as the exact behavior is not clear.
71  
72 ban_cidr creates a host object with the CIDR as its ip-address and the
73 C</> is kept in the generated object name, and it is not clear whether
74 the web_api will accept either; a network object may be required instead.
75  
76 =head1 METHODS
77  
78 =head2 new
79  
80 - options :: Backend specific options. See below.
81 - prefix :: Prefix to use. Must match /^[a-zA-Z0-9]+$/. Default kur.
82 - name :: Name of this instance. Required.
83  
84 Ports and protocols are not supported and specifying either is an error.
85  
86 The options hash accepts the following.
87  
88 - host :: Check Point management server host. Required.
89 - Default :: undef
90  
91 - user :: web_api user to log in as. Required.
92 - Default :: undef
93  
94 - password :: password for the web_api user. Required.
95 - Default :: undef
96  
97 - group :: group object the host objects are added to.
98 - Default :: <prefix>_<name>
99  
100 - timeout :: HTTP timeout in seconds.
101 - Default :: 30
102  
103 - insecure :: If true, skip TLS certificate verification.
104 - Default :: 0
105  
106 All errors are considered fatal, meaning if new fails it will die.
107  
108 =cut
109  
110 sub new {
111 4 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=26 SCAR=43.9
Pod documented
112  
113 4 my $self = {
114 perror => undef,
115 error => undef,
116 errorLine => undef,
117 errorFilename => undef,
118 errorString => "",
119 errorExtra => {
120 all_errors_fatal => 1,
121 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
122 # is kept for the name documented in its POD
123 all_fatal => 1,
124 flags => {
125 1 => 'notInited',
126 6 => 'invalidPrefixSpecified',
127 7 => 'invalidName',
128 8 => 'optionsNotHash',
129 9 => 'noBanItem',
130 10 => 'banItemNotIP',
131 12 => 'backendInitError',
132 13 => 'banFailed',
133 14 => 'unbanFailed',
134 15 => 'listFailed',
135 16 => 'reInitFailed',
136 17 => 'teardownFailed',
137 18 => 'alreadyInited',
138 23 => 'initFailed',
139 24 => 'checkFailed',
140 25 => 'flushFailed',
141 26 => 'portsNotSupported',
142 27 => 'protocolsNotSupported',
143 30 => 'hostNotDefined',
144 31 => 'userNotDefined',
145 32 => 'passwordNotDefined',
146 33 => 'banCidrFailed',
147 34 => 'unbanCidrFailed',
148 35 => 'cidrItemNotCidr',
149 36 => 'cidrNotSupported',
150 37 => 'listCidrFailed',
151 },
152 fatal_flags => {},
153 perror_not_fatal => 0,
154 },
155 options => {},
156 ports => [],
157 protocols => [],
158 testing => undef,
159 test_data => undef,
160 prefix => 'kur',
161 name => undef,
162 frontend_obj => undef,
163 inited => 0,
164 banned => {},
165 banned_cidr => {},
166 cidr_supported => 1,
167 ua => undef,
168 sid => undef,
169 };
170 4 bless $self;
171  
172 # blocking is per IP; ports/protocols belong on the referencing rule
173 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
174 0 $self->{perror} = 1;
175 0 $self->{error} = 26;
176 0 $self->{errorString} = 'the checkpoint backend blocks whole IPs and does not support ports';
177 0 $self->warn;
178 }
179 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
180 0 $self->{perror} = 1;
181 0 $self->{error} = 27;
182 0 $self->{errorString} = 'the checkpoint backend blocks whole IPs and does not support protocols';
183 0 $self->warn;
184 }
185  
186 # make sure prefix is sane if defined
187 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
188 0 $self->{perror} = 1;
189 0 $self->{error} = 6;
190 $self->{errorString}
191 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
192 0 $self->warn;
193 } elsif ( defined( $opts{prefix} ) ) {
194 4 $self->{prefix} = $opts{prefix};
195 }
196  
197 # make sure we have a name and that it is valid
198 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
199 0 $self->{perror} = 1;
200 0 $self->{error} = 7;
201 0 $self->{errorString} = 'name is undef';
202 0 $self->warn;
203 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
204 0 $self->{perror} = 1;
205 0 $self->{error} = 7;
206 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
207 0 $self->warn;
208 }
209 4 $self->{name} = $opts{name};
210  
211 4 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 4 0 4
212 4 $self->{testing} = $opts{testing};
213 }
214 4 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 4 0 4
215 4 $self->{frontend_obj} = $opts{frontend_obj};
216 }
217  
218 4 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 4 0 4
219 4 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 4 4
220 0 $self->{perror} = 1;
221 0 $self->{error} = 8;
222 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
223 0 $self->warn;
224 }
225 4 $self->{options} = $opts{options};
226 }
227  
228 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
229 1 $self->{perror} = 1;
230 1 $self->{error} = 30;
231 1 $self->{errorString} = 'the option host is undef or blank';
232 1 $self->warn;
233 }
234 3 ▶ if ( !defined( $self->{options}{user} ) || $self->{options}{user} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq ""
not defined $self->{"options"}{"user"} $self->{"options"}{"user"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"user"}B= $self->{"options"}{"user"} eq ""
ABresult
1X1
011
000
235 1 $self->{perror} = 1;
236 1 $self->{error} = 31;
237 1 $self->{errorString} = 'the option user is undef or blank';
238 1 $self->warn;
239 }
240 2 ▶ if ( !defined( $self->{options}{password} ) || $self->{options}{password} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq "") 1 1 2
Condition66%2/3
not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq ""
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq ""
not defined $self->{"options"}{"password"} $self->{"options"}{"password"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"password"}B= $self->{"options"}{"password"} eq ""
ABresult
1X1
011
000
241 1 $self->{perror} = 1;
242 1 $self->{error} = 32;
243 1 $self->{errorString} = 'the option password is undef or blank';
244 1 $self->warn;
245 }
246  
247 # defaults
248 1 ▶ $self->{options}{timeout} = 30 if ( !defined( $self->{options}{timeout} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"timeout"} 1 0 1
249 1 ▶ $self->{options}{insecure} = 0 if ( !defined( $self->{options}{insecure} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"insecure"} 1 0 1
250 1 ▶ if ( defined( $self->{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"name"}) 1 0 1
251 1 ▶ $self->{options}{group} = $self->{prefix} . '_' . $self->{name} if ( !defined( $self->{options}{group} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"group"} 1 0 1
252 }
253  
254 1 return $self;
255 } ## end sub new
256  
257 # Internal helper. Returns the base management server URL.
258 sub _base_url {
259 13 ▶ my ($self) = @_;
Subroutine _base_url: called CC=1 SCAR=0.0
260  
261 13 return 'https://' . $self->{options}{host};
262 }
263  
264 # Internal helper. Returns the host object name for the IP, a deterministic
265 # mangling of the prefix, name, and IP that avoids characters disallowed in
266 # object names.
267 sub _obj_name {
268 4 ▶ my ( $self, $ip ) = @_;
Subroutine _obj_name: called CC=1 SCAR=0.0
269  
270 4 my $name = $self->{prefix} . '_' . $self->{name} . '_' . $ip;
271 # dots and colons are not valid in Check Point object names
272 4 $name =~ s/[.:]/-/g;
273  
274 4 return $name;
275 } ## end sub _obj_name
276  
277 # Internal helper. Returns a canonical JSON::PP encoder/decoder.
278 sub _json {
279 4 ▶ my ($self) = @_;
Subroutine _json: called CC=1 SCAR=0.0
280  
281 4 require JSON::PP;
282 4 return JSON::PP->new->canonical->utf8;
283 }
284  
285 # Internal helper. Performs a POST via LWP::UserAgent, sending the session id in
286 # the X-chkp-sid header when one is set, returning the decoded JSON body (or
287 # undef for an empty body) and dying with an explanation on any HTTP level
288 # failure. Never called in testing mode.
289 sub _request {
290 0 ▶ my ( $self, $method, $url, $body ) = @_;
Subroutine _request: not called CC=9 SCAR=45.0
291  
292 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
293 0 local $@;
294 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent; my(%args) = ("agent", 'Net::Firewall::BlockerHelper/' . $Net::Firewall::BlockerHelper::backends::checkpoint::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 checkpoint backend requires... ' . $@ 0 0 0
295 0 require LWP::UserAgent;
296 my %args = (
297 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
298 timeout => $self->{options}{timeout},
299 0 );
300 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
301 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
302 }
303 0 $self->{ua} = LWP::UserAgent->new(%args);
304 0 1;
305 } or die( 'failed to load LWP::UserAgent, which the checkpoint backend requires... ' . $@ );
306 }
307  
308 0 my @headers = ('Content-Type' => 'application/json');
309 0 ▶ if ( defined( $self->{sid} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"sid"}) 0 0 0
310 0 push( @headers, 'X-chkp-sid' => $self->{sid} );
311 }
312  
313 0 require HTTP::Request;
314 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
315  
316 0 my $response = $self->{ua}->request($request);
317  
318 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
319 0 my $detail = $response->decoded_content;
320 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
321 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
322 }
323  
324 0 my $decoded;
325 0 my $content = $response->decoded_content;
326 0 ▶ if ( defined($content) && $content ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and $content ne "") 0 0 0
Condition0%0/3
defined $content and $content ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and $content ne ""
defined $content $content ne ""
Truth table0%0/3
A and B
A= defined $contentB= $content ne ""
ABresult
0X0
100
111
327 0 local $@;
328 0 eval { $decoded = $self->_json->decode($content); };
329 }
330  
331 0 return $decoded;
332 } ## end sub _request
333  
334 # Internal helper. Returns the two request descriptors used to ban an IP: create
335 # the host object and add it to the group, then publish.
336 sub _ban_requests {
337 2 ▶ my ( $self, $ip ) = @_;
Subroutine _ban_requests: called CC=1 SCAR=0.0
338  
339 my $add_host = $self->_json->encode(
340 {
341 name => $self->_obj_name($ip),
342 'ip-address' => $ip,
343 2 groups => [ $self->{options}{group} ],
344 }
345 );
346  
347 return (
348 2 { method => 'POST', url => $self->_base_url . '/web_api/add-host', content => $add_host },
349 { method => 'POST', url => $self->_base_url . '/web_api/publish', content => '{}' },
350 );
351 } ## end sub _ban_requests
352  
353 # Internal helper. Returns the two request descriptors used to unban an IP: delete
354 # the host object, then publish.
355 sub _unban_requests {
356 2 ▶ my ( $self, $ip ) = @_;
Subroutine _unban_requests: called CC=1 SCAR=0.0
357  
358 2 my $delete_host = $self->_json->encode( { name => $self->_obj_name($ip) } );
359  
360 return (
361 2 { method => 'POST', url => $self->_base_url . '/web_api/delete-host', content => $delete_host },
362 { method => 'POST', url => $self->_base_url . '/web_api/publish', content => '{}' },
363 );
364 } ## end sub _unban_requests
365  
366 =head2 init
367  
368 Initiates the backend. Logs into the web_api by POSTing the configured user and
369 password to C<< /web_api/login >> and stores the returned session id, which is
370 sent in the C<X-chkp-sid> header on every following call.
371  
372 Note that nothing is created remotely by this; the group object must already
373 exist. See L</DESCRIPTION>.
374  
375 =cut
376  
377 sub init {
378 1 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=6 SCAR=29.6
Pod documented
379  
380 1 $self->errorblank;
381  
382 1 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 1 1
383 0 $self->{error} = 18;
384 0 $self->{errorString} = 'backend has already been inited';
385 0 $self->warn;
386 }
387  
388 1 my $url = $self->_base_url . '/web_api/login';
389  
390 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
391 # the login body carries the password, so it is intentionally not recorded
392 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $url } ];
393 } else {
394 my $body = $self->_json->encode(
395 {
396 user => $self->{options}{user},
397 password => $self->{options}{password},
398 }
399 0 );
400 0 local $@;
401 eval {
402 0 my $decoded = $self->_request( 'POST', $url, $body );
403 0 ▶ if ( !defined($decoded) || !defined( $decoded->{sid} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $decoded and defined $decoded->{"sid"}) 0 0 0
Condition0%0/3
defined $decoded and defined $decoded->{"sid"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $decoded and defined $decoded->{"sid"}
defined $decoded defined $decoded->{"sid"}
Truth table0%0/3
A and B
A= defined $decodedB= defined $decoded->{"sid"}
ABresult
0X0
100
111
404 0 die('login response did not contain a session id (sid)');
405 }
406 0 $self->{sid} = $decoded->{sid};
407 0 1;
408 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $decoded = $self->_request("POST", $url, $body); unless (defined $decoded and defined $decoded->{"sid"}) { die "login response did not contain a session id (sid)"; } ; $self->{"sid"} = $decoded->{"sid"}; 1 } } or do { $self->{"error"} = 23; $self->{"errorString"} = 'init failed. logging into the web_api failed... ' . $@; $self->warn } 0 0 0
409 0 $self->{error} = 23;
410 0 $self->{errorString} = 'init failed. logging into the web_api failed... ' . $@;
411 0 $self->warn;
412 };
413 } ## end else [ if ( $self->{testing} ) ]
414  
415 1 $self->{inited} = 1;
416 } ## end sub init
417  
418 =head2 ban
419  
420 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address and
421 lowercased. A host object named C<< <prefix>_<name>_<ip> >> (dots and colons
422 replaced with C<->) is created via C<< /web_api/add-host >> with the group as a
423 member group, then the change is saved via C<< /web_api/publish >>. Banning an
424 already banned IP is a noop.
425  
426 $backend->ban(ban => $ip);
427  
428 =cut
429  
430 sub ban {
431 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=11 SCAR=39.7
Pod documented
432  
433 1 $self->errorblank;
434  
435 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
436 0 $self->{error} = 1;
437 0 $self->{errorString} = 'backend has not been inited';
438 0 $self->warn;
439 0 return;
440 }
441  
442 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::checkpoint::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/) { } 0 1 1
443 0 $self->{error} = 9;
444 0 $self->{errorString} = 'Nothing specified for the value ban';
445 0 $self->warn;
446 0 return;
447 } elsif ( ref( $opts{ban} ) ne '' ) {
448 0 $self->{error} = 10;
449 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
450 0 $self->warn;
451 0 return;
452 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/
ABresult
1X1
011
000
453 && $opts{ban} !~ /\A$IPv6_re\z/ )
454 {
455 0 $self->{error} = 10;
456 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
457 0 $self->warn;
458 0 return;
459 }
460  
461 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
462 1 $opts{ban} = lc( $opts{ban} );
463  
464 1 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 1 1
465 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
466 0 $self->{frontend_obj}->{test_data} = 'already banned';
467 }
468 0 return;
469 }
470  
471 1 my @requests = $self->_ban_requests( $opts{ban} );
472  
473 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
474 1 $self->{frontend_obj}->{test_data} = \@requests;
475 } else {
476 0 local $@;
477 eval {
478 0 foreach my $req (@requests) {
479 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
480 }
481 0 1;
482 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $req (@requests) { $self->_request($req->{"method"}, $req->{"url"}, $req->{"content"}); } ; 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
483 0 $self->{error} = 13;
484 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
485 0 $self->warn;
486 0 return;
487 };
488 } ## end else [ if ( $self->{testing} ) ]
489  
490 1 $self->{banned}{ $opts{ban} } = 1;
491 } ## end sub ban
492  
493 =head2 unban
494  
495 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address and
496 lowercased. The host object created by L</ban> is deleted via
497 C<< /web_api/delete-host >>, then the change is saved via
498 C<< /web_api/publish >>. Unbanning an IP that is not banned is a noop.
499  
500 $backend->unban(ban => $ip);
501  
502 =cut
503  
504 sub unban {
505 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=11 SCAR=39.7
Pod documented
506  
507 1 $self->errorblank;
508  
509 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
510 0 $self->{error} = 1;
511 0 $self->{errorString} = 'backend has not been inited';
512 0 $self->warn;
513 0 return;
514 }
515  
516 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::checkpoint::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/) { } 0 1 1
517 0 $self->{error} = 9;
518 0 $self->{errorString} = 'Nothing specified for the value ban';
519 0 $self->warn;
520 0 return;
521 } elsif ( ref( $opts{ban} ) ne '' ) {
522 0 $self->{error} = 10;
523 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
524 0 $self->warn;
525 0 return;
526 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/
ABresult
1X1
011
000
527 && $opts{ban} !~ /\A$IPv6_re\z/ )
528 {
529 0 $self->{error} = 10;
530 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
531 0 $self->warn;
532 0 return;
533 }
534  
535 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
536 1 $opts{ban} = lc( $opts{ban} );
537  
538 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
539 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
540 0 $self->{frontend_obj}->{test_data} = 'not banned';
541 }
542 0 return;
543 }
544  
545 1 my @requests = $self->_unban_requests( $opts{ban} );
546  
547 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
548 1 $self->{frontend_obj}->{test_data} = \@requests;
549 } else {
550 0 local $@;
551 eval {
552 0 foreach my $req (@requests) {
553 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
554 }
555 0 1;
556 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $req (@requests) { $self->_request($req->{"method"}, $req->{"url"}, $req->{"content"}); } ; 1 } } or do { $self->{"error"} = 14; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
557 0 $self->{error} = 14;
558 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
559 0 $self->warn;
560 0 return;
561 };
562 } ## end else [ if ( $self->{testing} ) ]
563  
564 1 delete( $self->{banned}{ $opts{ban} } );
565 } ## end sub unban
566  
567 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
568 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
569 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
570 # IPv6). Returns false otherwise.
571 sub _valid_cidr {
572 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
573  
574 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
575  
576 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
577 2 my ( $addr, $prefix ) = ( $1, $2 );
578 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
579 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::checkpoint::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
580 }
581  
582 0 return 0;
583 } ## end sub _valid_cidr
584  
585 =head2 ban_cidr
586  
587 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 CIDR
588 range and lowercased, then handled the same way as L</ban>, a host object with
589 the CIDR as its ip-address being created via C<< /web_api/add-host >> with the
590 group as a member group and the change saved via C<< /web_api/publish >>.
591 Banning an already banned CIDR range is a noop.
592  
593 $backend->ban_cidr(ban => '1.2.3.0/24');
594  
595 =cut
596  
597 sub ban_cidr {
598 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=10 SCAR=38.0
Pod documented
599  
600 1 $self->errorblank;
601  
602 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
603 0 $self->{error} = 1;
604 0 $self->{errorString} = 'backend has not been inited';
605 0 $self->warn;
606 0 return;
607 }
608  
609 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
610 0 $self->{error} = 9;
611 0 $self->{errorString} = 'Nothing specified for the value ban';
612 0 $self->warn;
613 0 return;
614 } elsif ( ref( $opts{ban} ) ne '' ) {
615 0 $self->{error} = 35;
616 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
617 0 $self->warn;
618 0 return;
619 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
620 0 $self->{error} = 35;
621 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
622 0 $self->warn;
623 0 return;
624 }
625  
626 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
627 1 $opts{ban} = lc( $opts{ban} );
628  
629 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
630 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
631 0 $self->{frontend_obj}->{test_data} = 'already banned';
632 }
633 0 return;
634 }
635  
636 1 my @requests = $self->_ban_requests( $opts{ban} );
637  
638 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
639 1 $self->{frontend_obj}->{test_data} = \@requests;
640 } else {
641 0 local $@;
642 eval {
643 0 foreach my $req (@requests) {
644 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
645 }
646 0 1;
647 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $req (@requests) { $self->_request($req->{"method"}, $req->{"url"}, $req->{"content"}); } ; 1 } } or do { $self->{"error"} = 33; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
648 0 $self->{error} = 33;
649 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
650 0 $self->warn;
651 0 return;
652 };
653 } ## end else [ if ( $self->{testing} ) ]
654  
655 1 $self->{banned_cidr}{ $opts{ban} } = 1;
656 } ## end sub ban_cidr
657  
658 =head2 unban_cidr
659  
660 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 CIDR
661 range and lowercased. The host object created by L</ban_cidr> is deleted via
662 C<< /web_api/delete-host >>, then the change is saved via
663 C<< /web_api/publish >>. Unbanning a CIDR range that is not banned is a noop.
664  
665 $backend->unban_cidr(ban => '1.2.3.0/24');
666  
667 =cut
668  
669 sub unban_cidr {
670 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=10 SCAR=38.0
Pod documented
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} = 35;
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} = 35;
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
unless ($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} = 'not banned';
704 }
705 0 return;
706 }
707  
708 1 my @requests = $self->_unban_requests( $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} = \@requests;
712 } else {
713 0 local $@;
714 eval {
715 0 foreach my $req (@requests) {
716 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
717 }
718 0 1;
719 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $req (@requests) { $self->_request($req->{"method"}, $req->{"url"}, $req->{"content"}); } ; 1 } } or do { $self->{"error"} = 34; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
720 0 $self->{error} = 34;
721 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
722 0 $self->warn;
723 0 return;
724 };
725 } ## end else [ if ( $self->{testing} ) ]
726  
727 1 delete( $self->{banned_cidr}{ $opts{ban} } );
728 } ## end sub unban_cidr
729  
730 =head2 list_cidr
731  
732 List banned CIDR ranges. Returns an array of the currently banned CIDR ranges
733 from internal state; nothing is queried from the management server. Single IPs
734 are not included; for those see L</list>.
735  
736 my @banned_cidrs = $backend->list_cidr;
737  
738 =cut
739  
740 sub list_cidr {
741 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
742  
743 2 $self->errorblank;
744  
745 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
746 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
747 }
748  
749 2 return keys( %{ $self->{banned_cidr} } );
750 }
751  
752 =head2 list
753  
754 List banned IPs. Returns an array of the currently banned single IPs from
755 internal state; nothing is queried from the management server. CIDR ranges are
756 not included; for those see L</list_cidr>.
757  
758 my @banned = $backend->list;
759  
760 =cut
761  
762 sub list {
763 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
764  
765 0 $self->errorblank;
766  
767 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
768 0 $self->{frontend_obj}->{test_data} = 'list';
769 }
770  
771 0 return keys( %{ $self->{banned} } );
772 }
773  
774 =head2 re_init
775  
776 Tears down and re-inits. L</teardown> is called best effort, a new session is
777 logged in via L</init>, and then the host object and group membership is
778 re-created for every retained single IP and CIDR ban.
779  
780 =cut
781  
782 sub re_init {
783 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=7 SCAR=40.3
Pod documented
784  
785 0 $self->errorblank;
786  
787 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
788 0 $self->{error} = 1;
789 0 $self->{errorString} = 'backend has not been inited';
790 0 $self->warn;
791 0 return;
792 }
793  
794 # teardown is best effort here as a partially or fully wiped setup is
795 # exactly what re_init needs to recover from
796 {
797 0 local $@;
798 0 eval { $self->teardown; };
799 }
800 0 $self->init;
801  
802 # both single IPs and CIDR ranges are re-added the same way
803 0 my @to_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
804  
805 0 my @re_init_test_data;
806 0 foreach my $item (@to_ban) {
807 0 my @requests = $self->_ban_requests($item);
808  
809 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
810 0 push( @re_init_test_data, @requests );
811 } else {
812 0 local $@;
813 eval {
814 0 foreach my $req (@requests) {
815 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
816 }
817 0 1;
818 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $req (@requests) { $self->_request($req->{"method"}, $req->{"url"}, $req->{"content"}); } ; 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $item . '" failed... ' . $@; $self->warn } 0 0 0
819 0 $self->{error} = 13;
820 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
821 0 $self->warn;
822 };
823 } ## end else [ if ( $self->{testing} ) ]
824 } ## end foreach my $item ( keys( %{ ...}))
825  
826 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
827 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
828 }
829  
830 0 $self->{inited} = 1;
831 } ## end sub re_init
832  
833 =head2 teardown
834  
835 Tears down the setup by deleting the host object for each currently banned IP
836 and CIDR range and publishing. The internal list of bans is kept, so a
837 following re_init will re-add them.
838  
839 =cut
840  
841 sub teardown {
842 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=6 SCAR=37.4
Pod documented
843  
844 0 $self->errorblank;
845  
846 0 $self->{inited} = 0;
847  
848 # both single IPs and CIDR ranges have host objects that need removing
849 0 my @to_remove = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
850  
851 0 my @requests;
852 0 foreach my $item ( sort(@to_remove) ) {
853 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
854 0 push( @requests, $self->_unban_requests($item) );
855 } else {
856 0 local $@;
857 eval {
858 0 foreach my $req ( $self->_unban_requests($item) ) {
859 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
860 }
861 0 1;
862 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $req ($self->_unban_requests($item)) { $self->_request($req->{"method"}, $req->{"url"}, $req->{"content"}); } ; 1 } } or do { $self->{"error"} = 17; $self->{"errorString"} = 'teardown failed removing "' . $item . '"... ' . $@; $self->warn } 0 0 0
863 0 $self->{error} = 17;
864 0 $self->{errorString} = 'teardown failed removing "' . $item . '"... ' . $@;
865 0 $self->warn;
866 };
867 } ## end else [ if ( $self->{testing} ) ]
868 } ## end foreach my $item ( sort(@to_remove) )
869  
870 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
871 0 $self->{frontend_obj}->{test_data} = \@requests;
872 }
873 } ## end sub teardown
874  
875 =head2 stop
876  
877 Alias for L</teardown>.
878  
879 =cut
880  
881 sub stop {
882 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
883  
884 0 return $self->teardown(%opts);
885 }
886  
887 =head2 check
888  
889 Verifies the session is still usable by POSTing to C<< /web_api/keepalive >>.
890 Returns a true value if so and a false value otherwise.
891  
892 =cut
893  
894 sub check {
895 4 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=14.7
Pod documented
896  
897 4 $self->errorblank;
898  
899 4 my $url = $self->_base_url . '/web_api/keepalive';
900  
901 4 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 4 0 4
902 4 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $url, content => '{}' } ];
903 4 return 1;
904 }
905  
906 0 local $@;
907 0 ▶ eval { $self->_request( 'POST', $url, '{}' ); 1; } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("POST", $url, "{}"); 1 } } or return 0 0 0 0
908  
909 0 return 1;
910 } ## end sub check
911  
912 =head2 flush
913  
914 Removes all currently banned IPs and CIDR ranges at once by deleting their
915 host objects and publishing, then forgetting them.
916  
917 =cut
918  
919 sub flush {
920 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=7 SCAR=40.3
Pod documented
921  
922 0 $self->errorblank;
923  
924 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
925 0 $self->{error} = 1;
926 0 $self->{errorString} = 'backend has not been inited';
927 0 $self->warn;
928 0 return;
929 }
930  
931 # both single IPs and CIDR ranges have host objects that need removing
932 0 my @to_remove = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
933  
934 0 my @requests;
935 0 foreach my $item ( sort(@to_remove) ) {
936 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
937 0 push( @requests, $self->_unban_requests($item) );
938 } else {
939 0 local $@;
940 eval {
941 0 foreach my $req ( $self->_unban_requests($item) ) {
942 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
943 }
944 0 1;
945 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $req ($self->_unban_requests($item)) { $self->_request($req->{"method"}, $req->{"url"}, $req->{"content"}); } ; 1 } } or do { $self->{"error"} = 25; $self->{"errorString"} = 'flush failed removing "' . $item . '"... ' . $@; $self->warn } 0 0 0
946 0 $self->{error} = 25;
947 0 $self->{errorString} = 'flush failed removing "' . $item . '"... ' . $@;
948 0 $self->warn;
949 };
950 } ## end else [ if ( $self->{testing} ) ]
951 } ## end foreach my $item ( sort(@to_remove) )
952  
953 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
954 0 $self->{frontend_obj}->{test_data} = \@requests;
955 }
956  
957 0 $self->{banned} = {};
958 0 $self->{banned_cidr} = {};
959 } ## end sub flush
960  
961 =head1 ERROR CODES / FLAGS
962  
963 Error handling is provided by L<Error::Helper>. All
964 errors are considered fatal.
965  
966 =head2 1, notInited
967  
968 The backend has not been inited yet.
969  
970 =head2 6, invalidPrefixSpecified
971  
972 The specified prefix did not match /^[a-zA-Z0-9]+$/.
973  
974 =head2 7, invalidName
975  
976 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
977  
978 =head2 8, optionsNotHash
979  
980 The item passed to new for options is not a hash.
981  
982 =head2 9, noBanItem
983  
984 No IP or CIDR range specified to ban or unban.
985  
986 =head2 10, banItemNotIP
987  
988 The item to ban is not an IP. Either wrong ref type or regexp
989 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
990  
991 =head2 12, backendInitError
992  
993 Failed to init the backend.
994  
995 =head2 13, banFailed
996  
997 Failed to ban the item.
998  
999 =head2 14, unbanFailed
1000  
1001 Failed to unban the item.
1002  
1003 =head2 15, listFailed
1004  
1005 Failed to get a list of bans.
1006  
1007 =head2 16, reInitFailed
1008  
1009 Failed to re_init the backend.
1010  
1011 =head2 17, teardownFailed
1012  
1013 Failed to teardown the backend.
1014  
1015 =head2 18, alreadyInited
1016  
1017 init called, but the backend has already been inited.
1018  
1019 =head2 23, initFailed
1020  
1021 init failed. Logging into the web_api failed.
1022  
1023 =head2 24, checkFailed
1024  
1025 The backend check raised an error.
1026  
1027 =head2 25, flushFailed
1028  
1029 Failed to flush the bans.
1030  
1031 =head2 26, portsNotSupported
1032  
1033 The checkpoint backend blocks whole IPs and does not support ports.
1034  
1035 =head2 27, protocolsNotSupported
1036  
1037 The checkpoint backend blocks whole IPs and does not support protocols.
1038  
1039 =head2 30, hostNotDefined
1040  
1041 The option host is undef or blank.
1042  
1043 =head2 31, userNotDefined
1044  
1045 The option user is undef or blank.
1046  
1047 =head2 32, passwordNotDefined
1048  
1049 The option password is undef or blank.
1050  
1051 =head2 33, banCidrFailed
1052  
1053 Failed to ban the CIDR range.
1054  
1055 =head2 34, unbanCidrFailed
1056  
1057 Failed to unban the CIDR range.
1058  
1059 =head2 35, cidrItemNotCidr
1060  
1061 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1062 IPv4 or IPv6 address followed by a prefix length valid for its family.
1063  
1064 =head2 36, cidrNotSupported
1065  
1066 The backend does not support CIDR bans.
1067  
1068 =head2 37, listCidrFailed
1069  
1070 Failed to get a list of CIDR bans.
1071  
1072 =head1 AUTHOR
1073  
1074 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1075  
1076 =head1 LICENSE AND COPYRIGHT
1077  
1078 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1079  
1080 This is free software, licensed under:
1081  
1082 The GNU Lesser General Public License, Version 2.1, February 1999
1083  
1084 =cut
1085  
1086 1 1; # End of Net::Firewall::BlockerHelper::backends::checkpoint
« BlockerHelper/backends/cloudflare.pm ↑ Summary BlockerHelper/backends/fortigate.pm »