BlockerHelper/backends/checkpoint.pm

filter: Statementstmt 32.4% 126 / 388 Branchbran 32.4% 48 / 148 Conditioncond 36.1% 13 / 36 MC/DCmcdc 13.6% 3 / 22 Subroutinesub 75.0% 18 / 24 Podpod 100% 13 / 13 totaltotal 35.0% 221 / 631 CC 97 SCAR 39.1
CC
97
Cov
33%
_request
90.0
new
80.6
flush
56.0
CRAP 50.1
« BlockerHelper/backends/cloudflare.pm ↑ Summary BlockerHelper/backends/cisco_fmc.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 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::checkpoint - Check Point Management (web_api) backend.
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 => '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 4 ▶sub new {
Subroutine new: called CC=26 SCAR=43.9
Pod documented
111 4 my ( $blank, %opts ) = @_;
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 scheme and host of the Check Point management
258 # server, which every web_api endpoint is built on.
259 #
260 # The Check Point web_api is a flat set of POST endpoints under /web_api/
261 # rather than a resource tree, so the callers append the specific command
262 # themselves rather than there being a URL builder per operation.
263 #
264 # The scheme is fixed at https rather than configurable, since the management
265 # API is expected to be run over TLS.
266 #
267 # Takes no arguments; the host comes from the options.
268 #
269 # Returns the base URL as a string, with no trailing slash.
270 #
271 # $self->_base_url; # https://mgmt.example.org
272 # $self->_base_url . '/web_api/add-host'; # the usual shape
273 13 ▶sub _base_url {
Subroutine _base_url: called CC=1 SCAR=0.0
274 13 my ($self) = @_;
275  
276 13 return 'https://' . $self->{options}{host};
277 }
278  
279 # Internal helper. Returns the name of the Check Point host object
280 # representing one banned address.
281 #
282 # Check Point has no set type that simply holds addresses. Each ban becomes a
283 # named host object that is made a member of a group the policy references, so
284 # every ban needs a name.
285 #
286 # The name is derived from the prefix, the instance name, and the address
287 # rather than stored, so ban and unban independently arrive at the same name
288 # without local bookkeeping, and two instances against the same management
289 # server do not collide. Dots and colons are replaced with hyphens because
290 # Check Point rejects them in object names.
291 #
292 # Args:
293 #
294 # ip - The address to name an object for, as a plain string. Expected to
295 # be an already validated and lowercased IPv4 or IPv6 address.
296 #
297 # Returns the object name as a plain string, valid as a Check Point object
298 # name.
299 #
300 # # with prefix "kur" and name "ssh"
301 # $self->_obj_name('10.0.0.1'); # kur_ssh_10-0-0-1
302 # $self->_obj_name('2001:db8::1'); # kur_ssh_2001-db8--1
303 4 ▶sub _obj_name {
Subroutine _obj_name: called CC=1 SCAR=0.0
304 4 my ( $self, $ip ) = @_;
305  
306 4 my $name = $self->{prefix} . '_' . $self->{name} . '_' . $ip;
307 # dots and colons are not valid in Check Point object names
308 4 $name =~ s/[.:]/-/g;
309  
310 4 return $name;
311 } ## end sub _obj_name
312  
313 # Internal helper. Performs one HTTP request against the Check Point web_api.
314 # Every call this backend makes to the management server goes through here.
315 #
316 # Authentication is by session rather than by credential or standing token.
317 # init POSTs to /web_api/login and stores the session id, which this then
318 # sends in the X-chkp-sid header on every following call. The header is only
319 # added when a session id is set, which is what lets the login request itself
320 # go through this same helper before there is one.
321 #
322 # The user agent is built on first use and cached on the object, so a run of
323 # requests shares one agent. LWP::UserAgent is loaded with require at that
324 # point rather than at compile time, since only the HTTP backends need it;
325 # failing to load it dies with an explanation naming this backend. The
326 # insecure option turns off certificate verification, which is there because a
327 # management server commonly presents a self signed certificate.
328 #
329 # Any HTTP level failure dies rather than setting an error. The callers wrap
330 # this in eval and turn the exception into the appropriate error code, which
331 # is what lets one helper serve paths that report ban, unban, and teardown
332 # failures differently.
333 #
334 # A body that fails to decode as JSON is not fatal; the decode runs inside its
335 # own eval and leaves the result undef.
336 #
337 # Never called in testing mode; those paths record the request descriptors
338 # instead.
339 #
340 # Args:
341 #
342 # method - The HTTP method, as a plain string. In practice always 'POST',
343 # since the web_api exposes everything as POST endpoints.
344 #
345 # url - The full URL to request, as a plain string, normally
346 # _base_url with a /web_api/ command appended.
347 #
348 # body - The request body, as an already encoded JSON string. Note the
349 # web_api expects a JSON object even where there is nothing to
350 # say, so callers pass the literal '{}' rather than undef for
351 # those.
352 #
353 # Returns the decoded response body as whatever structure the JSON held,
354 # normally a hashref, or undef when the response body was empty or did not
355 # parse. Dies on any non success HTTP status.
356 #
357 # $self->_request( 'POST', $self->_base_url . '/web_api/publish', '{}' );
358 #
359 # # the usual shape at the call sites
360 # eval { $self->_request( $r->{method}, $r->{url}, $r->{content} ); };
361 # if ($@) { ... raise banFailed ... }
362 0 ▶sub _request {
Subroutine _request: not called CC=9 SCAR=45.0
363 0 my ( $self, $method, $url, $body ) = @_;
364  
365 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
366 0 local $@;
367 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
368 0 require LWP::UserAgent;
369 my %args = (
370 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
371 timeout => $self->{options}{timeout},
372 0 );
373 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
374 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
375 }
376 0 $self->{ua} = LWP::UserAgent->new(%args);
377 0 1;
378 } or die( 'failed to load LWP::UserAgent, which the checkpoint backend requires... ' . $@ );
379 }
380  
381 0 my @headers = ('Content-Type' => 'application/json');
382 0 ▶ if ( defined( $self->{sid} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"sid"}) 0 0 0
383 0 push( @headers, 'X-chkp-sid' => $self->{sid} );
384 }
385  
386 0 require HTTP::Request;
387 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
388  
389 0 my $response = $self->{ua}->request($request);
390  
391 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
392 0 my $detail = $response->decoded_content;
393 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
394 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
395 }
396  
397 0 my $decoded;
398 0 my $content = $response->decoded_content;
399 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
400 0 local $@;
401 0 eval { $decoded = $self->_json->decode($content); };
402 }
403  
404 0 return $decoded;
405 } ## end sub _request
406  
407 # Internal helper. Returns the pair of requests that ban one address.
408 #
409 # Unlike the fortigate backend, creating the object and putting it in the
410 # group is a single call here: add-host takes a groups list, so the membership
411 # comes along with the creation.
412 #
413 # The second call is the one that is easy to overlook. Check Point changes
414 # land in a private session and have no effect until published, so a ban
415 # without the publish would appear to succeed while blocking nothing. That is
416 # why the two are returned together rather than being sequenced by the caller.
417 #
418 # Returning descriptors rather than performing the requests is what lets the
419 # testing paths record exactly what would have been sent without a management
420 # server being present.
421 #
422 # Args:
423 #
424 # ip - The address to ban, as a plain string. Expected to be an already
425 # validated and lowercased IPv4 or IPv6 address. Note the address
426 # goes into the ip-address field for both families; Check Point does
427 # not split hosts by family the way FortiOS does.
428 #
429 # Returns a two element list of hashrefs, in the order they must be performed,
430 # each with method, url, and content keys.
431 #
432 # my @requests = $self->_ban_requests('10.0.0.1');
433 #
434 # # first, create the host and put it in the group
435 # # POST https://mgmt.example.org/web_api/add-host
436 # # {"groups":["blocklist"],"ip-address":"10.0.0.1","name":"kur_ssh_10-0-0-1"}
437 #
438 # # then, publish so it takes effect
439 # # POST https://mgmt.example.org/web_api/publish
440 # # {}
441 2 ▶sub _ban_requests {
Subroutine _ban_requests: called CC=1 SCAR=0.0
442 2 my ( $self, $ip ) = @_;
443  
444 my $add_host = $self->_json->encode(
445 {
446 name => $self->_obj_name($ip),
447 'ip-address' => $ip,
448 2 groups => [ $self->{options}{group} ],
449 }
450 );
451  
452 return (
453 2 { method => 'POST', url => $self->_base_url . '/web_api/add-host', content => $add_host },
454 { method => 'POST', url => $self->_base_url . '/web_api/publish', content => '{}' },
455 );
456 } ## end sub _ban_requests
457  
458 # Internal helper. Returns the pair of requests that unban one address, the
459 # inverse of _ban_requests.
460 #
461 # Deleting the host object also removes it from the group, so there is no
462 # separate membership call, mirroring the way the ban created both at once.
463 # The publish is required here for exactly the same reason it is on the ban:
464 # without it the deletion stays in the private session and the address remains
465 # blocked.
466 #
467 # The delete identifies the object by name alone, which is why the object name
468 # has to be derivable rather than stored.
469 #
470 # Args:
471 #
472 # ip - The address to unban, as a plain string. Expected to be an already
473 # validated and lowercased IPv4 or IPv6 address, and to produce the
474 # same object name it did when banned.
475 #
476 # Returns a two element list of hashrefs, in the order they must be performed,
477 # each with method, url, and content keys.
478 #
479 # my @requests = $self->_unban_requests('10.0.0.1');
480 #
481 # # first, delete the host object
482 # # POST https://mgmt.example.org/web_api/delete-host
483 # # {"name":"kur_ssh_10-0-0-1"}
484 #
485 # # then, publish so it takes effect
486 # # POST https://mgmt.example.org/web_api/publish
487 # # {}
488 2 ▶sub _unban_requests {
Subroutine _unban_requests: called CC=1 SCAR=0.0
489 2 my ( $self, $ip ) = @_;
490  
491 2 my $delete_host = $self->_json->encode( { name => $self->_obj_name($ip) } );
492  
493 return (
494 2 { method => 'POST', url => $self->_base_url . '/web_api/delete-host', content => $delete_host },
495 { method => 'POST', url => $self->_base_url . '/web_api/publish', content => '{}' },
496 );
497 } ## end sub _unban_requests
498  
499 =head2 init
500  
501 Initiates the backend. Logs into the web_api by POSTing the configured user and
502 password to C<< /web_api/login >> and stores the returned session id, which is
503 sent in the C<X-chkp-sid> header on every following call.
504  
505 Note that nothing is created remotely by this; the group object must already
506 exist. See L</DESCRIPTION>.
507  
508 =cut
509  
510 1 ▶sub init {
Subroutine init: called CC=6 SCAR=29.6
Pod documented
511 1 my ( $self, %opts ) = @_;
512  
513 1 $self->errorblank;
514  
515 1 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 1 1
516 0 $self->{error} = 18;
517 0 $self->{errorString} = 'backend has already been inited';
518 0 $self->warn;
519 }
520  
521 1 my $url = $self->_base_url . '/web_api/login';
522  
523 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
524 # the login body carries the password, so it is intentionally not recorded
525 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $url } ];
526 } else {
527 my $body = $self->_json->encode(
528 {
529 user => $self->{options}{user},
530 password => $self->{options}{password},
531 }
532 0 );
533 0 local $@;
534 eval {
535 0 my $decoded = $self->_request( 'POST', $url, $body );
536 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
537 0 die('login response did not contain a session id (sid)');
538 }
539 0 $self->{sid} = $decoded->{sid};
540 0 1;
541 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
542 0 $self->{error} = 23;
543 0 $self->{errorString} = 'init failed. logging into the web_api failed... ' . $@;
544 0 $self->warn;
545 };
546 } ## end else [ if ( $self->{testing} ) ]
547  
548 1 $self->{inited} = 1;
549 } ## end sub init
550  
551 =head2 ban
552  
553 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address and
554 lowercased. A host object named C<< <prefix>_<name>_<ip> >> (dots and colons
555 replaced with C<->) is created via C<< /web_api/add-host >> with the group as a
556 member group, then the change is saved via C<< /web_api/publish >>. Banning an
557 already banned IP is a noop.
558  
559 $backend->ban(ban => $ip);
560  
561 =cut
562  
563 1 ▶sub ban {
Subroutine ban: called CC=11 SCAR=39.7
Pod documented
564 1 my ( $self, %opts ) = @_;
565  
566 1 $self->errorblank;
567  
568 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
569 0 $self->{error} = 1;
570 0 $self->{errorString} = 'backend has not been inited';
571 0 $self->warn;
572 0 return;
573 }
574  
575 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
576 0 $self->{error} = 9;
577 0 $self->{errorString} = 'Nothing specified for the value ban';
578 0 $self->warn;
579 0 return;
580 } elsif ( ref( $opts{ban} ) ne '' ) {
581 0 $self->{error} = 10;
582 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
583 0 $self->warn;
584 0 return;
585 ▶ } 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
586 && $opts{ban} !~ /\A$IPv6_re\z/ )
587 {
588 0 $self->{error} = 10;
589 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
590 0 $self->warn;
591 0 return;
592 }
593  
594 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
595 1 $opts{ban} = lc( $opts{ban} );
596  
597 1 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 1 1
598 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
599 0 $self->{frontend_obj}->{test_data} = 'already banned';
600 }
601 0 return;
602 }
603  
604 1 my @requests = $self->_ban_requests( $opts{ban} );
605  
606 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
607 1 $self->{frontend_obj}->{test_data} = \@requests;
608 } else {
609 0 local $@;
610 eval {
611 0 foreach my $req (@requests) {
612 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
613 }
614 0 1;
615 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
616 0 $self->{error} = 13;
617 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
618 0 $self->warn;
619 0 return;
620 };
621 } ## end else [ if ( $self->{testing} ) ]
622  
623 1 $self->{banned}{ $opts{ban} } = 1;
624 } ## end sub ban
625  
626 =head2 unban
627  
628 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address and
629 lowercased. The host object created by L</ban> is deleted via
630 C<< /web_api/delete-host >>, then the change is saved via
631 C<< /web_api/publish >>. Unbanning an IP that is not banned is a noop.
632  
633 $backend->unban(ban => $ip);
634  
635 =cut
636  
637 1 ▶sub unban {
Subroutine unban: called CC=11 SCAR=39.7
Pod documented
638 1 my ( $self, %opts ) = @_;
639  
640 1 $self->errorblank;
641  
642 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
643 0 $self->{error} = 1;
644 0 $self->{errorString} = 'backend has not been inited';
645 0 $self->warn;
646 0 return;
647 }
648  
649 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
650 0 $self->{error} = 9;
651 0 $self->{errorString} = 'Nothing specified for the value ban';
652 0 $self->warn;
653 0 return;
654 } elsif ( ref( $opts{ban} ) ne '' ) {
655 0 $self->{error} = 10;
656 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
657 0 $self->warn;
658 0 return;
659 ▶ } 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
660 && $opts{ban} !~ /\A$IPv6_re\z/ )
661 {
662 0 $self->{error} = 10;
663 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
664 0 $self->warn;
665 0 return;
666 }
667  
668 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
669 1 $opts{ban} = lc( $opts{ban} );
670  
671 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
672 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
673 0 $self->{frontend_obj}->{test_data} = 'not banned';
674 }
675 0 return;
676 }
677  
678 1 my @requests = $self->_unban_requests( $opts{ban} );
679  
680 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
681 1 $self->{frontend_obj}->{test_data} = \@requests;
682 } else {
683 0 local $@;
684 eval {
685 0 foreach my $req (@requests) {
686 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
687 }
688 0 1;
689 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
690 0 $self->{error} = 14;
691 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
692 0 $self->warn;
693 0 return;
694 };
695 } ## end else [ if ( $self->{testing} ) ]
696  
697 1 delete( $self->{banned}{ $opts{ban} } );
698 } ## end sub unban
699  
700 =head2 ban_cidr
701  
702 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 CIDR
703 range and lowercased, then handled the same way as L</ban>, a host object with
704 the CIDR as its ip-address being created via C<< /web_api/add-host >> with the
705 group as a member group and the change saved via C<< /web_api/publish >>.
706 Banning an already banned CIDR range is a noop.
707  
708 $backend->ban_cidr(ban => '1.2.3.0/24');
709  
710 =cut
711  
712 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=10 SCAR=38.0
Pod documented
713 1 my ( $self, %opts ) = @_;
714  
715 1 $self->errorblank;
716  
717 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
718 0 $self->{error} = 1;
719 0 $self->{errorString} = 'backend has not been inited';
720 0 $self->warn;
721 0 return;
722 }
723  
724 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
725 0 $self->{error} = 9;
726 0 $self->{errorString} = 'Nothing specified for the value ban';
727 0 $self->warn;
728 0 return;
729 } elsif ( ref( $opts{ban} ) ne '' ) {
730 0 $self->{error} = 35;
731 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
732 0 $self->warn;
733 0 return;
734 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
735 0 $self->{error} = 35;
736 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
737 0 $self->warn;
738 0 return;
739 }
740  
741 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
742 1 $opts{ban} = lc( $opts{ban} );
743  
744 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
745 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
746 0 $self->{frontend_obj}->{test_data} = 'already banned';
747 }
748 0 return;
749 }
750  
751 1 my @requests = $self->_ban_requests( $opts{ban} );
752  
753 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
754 1 $self->{frontend_obj}->{test_data} = \@requests;
755 } else {
756 0 local $@;
757 eval {
758 0 foreach my $req (@requests) {
759 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
760 }
761 0 1;
762 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
763 0 $self->{error} = 33;
764 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
765 0 $self->warn;
766 0 return;
767 };
768 } ## end else [ if ( $self->{testing} ) ]
769  
770 1 $self->{banned_cidr}{ $opts{ban} } = 1;
771 } ## end sub ban_cidr
772  
773 =head2 unban_cidr
774  
775 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 CIDR
776 range and lowercased. The host object created by L</ban_cidr> is deleted via
777 C<< /web_api/delete-host >>, then the change is saved via
778 C<< /web_api/publish >>. Unbanning a CIDR range that is not banned is a noop.
779  
780 $backend->unban_cidr(ban => '1.2.3.0/24');
781  
782 =cut
783  
784 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=10 SCAR=38.0
Pod documented
785 1 my ( $self, %opts ) = @_;
786  
787 1 $self->errorblank;
788  
789 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
790 0 $self->{error} = 1;
791 0 $self->{errorString} = 'backend has not been inited';
792 0 $self->warn;
793 0 return;
794 }
795  
796 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
797 0 $self->{error} = 9;
798 0 $self->{errorString} = 'Nothing specified for the value ban';
799 0 $self->warn;
800 0 return;
801 } elsif ( ref( $opts{ban} ) ne '' ) {
802 0 $self->{error} = 35;
803 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
804 0 $self->warn;
805 0 return;
806 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
807 0 $self->{error} = 35;
808 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
809 0 $self->warn;
810 0 return;
811 }
812  
813 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
814 1 $opts{ban} = lc( $opts{ban} );
815  
816 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
817 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
818 0 $self->{frontend_obj}->{test_data} = 'not banned';
819 }
820 0 return;
821 }
822  
823 1 my @requests = $self->_unban_requests( $opts{ban} );
824  
825 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
826 1 $self->{frontend_obj}->{test_data} = \@requests;
827 } else {
828 0 local $@;
829 eval {
830 0 foreach my $req (@requests) {
831 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
832 }
833 0 1;
834 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
835 0 $self->{error} = 34;
836 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
837 0 $self->warn;
838 0 return;
839 };
840 } ## end else [ if ( $self->{testing} ) ]
841  
842 1 delete( $self->{banned_cidr}{ $opts{ban} } );
843 } ## end sub unban_cidr
844  
845 =head2 list_cidr
846  
847 List banned CIDR ranges. Returns an array of the currently banned CIDR ranges
848 from internal state; nothing is queried from the management server. Single IPs
849 are not included; for those see L</list>.
850  
851 my @banned_cidrs = $backend->list_cidr;
852  
853 =cut
854  
855 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
856 2 my ( $self, %opts ) = @_;
857  
858 2 $self->errorblank;
859  
860 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
861 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
862 }
863  
864 2 return keys( %{ $self->{banned_cidr} } );
865 }
866  
867 =head2 list
868  
869 List banned IPs. Returns an array of the currently banned single IPs from
870 internal state; nothing is queried from the management server. CIDR ranges are
871 not included; for those see L</list_cidr>.
872  
873 my @banned = $backend->list;
874  
875 =cut
876  
877 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
878 0 my ( $self, %opts ) = @_;
879  
880 0 $self->errorblank;
881  
882 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
883 0 $self->{frontend_obj}->{test_data} = 'list';
884 }
885  
886 0 return keys( %{ $self->{banned} } );
887 }
888  
889 =head2 re_init
890  
891 Tears down and re-inits. L</teardown> is called best effort, a new session is
892 logged in via L</init>, and then the host object and group membership is
893 re-created for every retained single IP and CIDR ban.
894  
895 =cut
896  
897 0 ▶sub re_init {
Subroutine re_init: not called CC=6 SCAR=37.4
Pod documented
898 0 my ( $self, %opts ) = @_;
899  
900 0 $self->errorblank;
901  
902 # teardown is best effort here as a partially or fully wiped setup is
903 # exactly what re_init needs to recover from
904 {
905 0 local $@;
906 0 eval { $self->teardown; };
907 }
908 0 $self->init;
909  
910 # both single IPs and CIDR ranges are re-added the same way
911 0 my @to_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
912  
913 0 my @re_init_test_data;
914 0 foreach my $item (@to_ban) {
915 0 my @requests = $self->_ban_requests($item);
916  
917 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
918 0 push( @re_init_test_data, @requests );
919 } else {
920 0 local $@;
921 eval {
922 0 foreach my $req (@requests) {
923 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
924 }
925 0 1;
926 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
927 0 $self->{error} = 13;
928 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
929 0 $self->warn;
930 };
931 } ## end else [ if ( $self->{testing} ) ]
932 } ## end foreach my $item ( keys( %{ ...}))
933  
934 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
935 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
936 }
937  
938 0 $self->{inited} = 1;
939 } ## end sub re_init
940  
941 =head2 teardown
942  
943 Tears down the setup by deleting the host object for each currently banned IP
944 and CIDR range and publishing. The internal list of bans is kept, so a
945 following re_init will re-add them.
946  
947 =cut
948  
949 0 ▶sub teardown {
Subroutine teardown: not called CC=6 SCAR=37.4
Pod documented
950 0 my ( $self, %opts ) = @_;
951  
952 0 $self->errorblank;
953  
954 0 $self->{inited} = 0;
955  
956 # both single IPs and CIDR ranges have host objects that need removing
957 0 my @to_remove = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
958  
959 0 my @requests;
960 0 foreach my $item ( sort(@to_remove) ) {
961 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
962 0 push( @requests, $self->_unban_requests($item) );
963 } else {
964 0 local $@;
965 eval {
966 0 foreach my $req ( $self->_unban_requests($item) ) {
967 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
968 }
969 0 1;
970 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
971 0 $self->{error} = 17;
972 0 $self->{errorString} = 'teardown failed removing "' . $item . '"... ' . $@;
973 0 $self->warn;
974 };
975 } ## end else [ if ( $self->{testing} ) ]
976 } ## end foreach my $item ( sort(@to_remove) )
977  
978 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
979 0 $self->{frontend_obj}->{test_data} = \@requests;
980 }
981 } ## end sub teardown
982  
983 =head2 stop
984  
985 Alias for L</teardown>.
986  
987 =cut
988  
989 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
990 0 my ( $self, %opts ) = @_;
991  
992 0 return $self->teardown(%opts);
993 }
994  
995 =head2 check
996  
997 Verifies the session is still usable by POSTing to C<< /web_api/keepalive >>.
998 Returns a true value if so and a false value otherwise.
999  
1000 =cut
1001  
1002 4 ▶sub check {
Subroutine check: called CC=3 SCAR=14.7
Pod documented
1003 4 my ( $self, %opts ) = @_;
1004  
1005 4 $self->errorblank;
1006  
1007 4 my $url = $self->_base_url . '/web_api/keepalive';
1008  
1009 4 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 4 0 4
1010 4 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $url, content => '{}' } ];
1011 4 return 1;
1012 }
1013  
1014 0 local $@;
1015 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
1016  
1017 0 return 1;
1018 } ## end sub check
1019  
1020 =head2 flush
1021  
1022 Removes all currently banned IPs and CIDR ranges at once by deleting their
1023 host objects and publishing, then forgetting them.
1024  
1025 =cut
1026  
1027 0 ▶sub flush {
Subroutine flush: not called CC=7 SCAR=40.3
Pod documented
1028 0 my ( $self, %opts ) = @_;
1029  
1030 0 $self->errorblank;
1031  
1032 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1033 0 $self->{error} = 1;
1034 0 $self->{errorString} = 'backend has not been inited';
1035 0 $self->warn;
1036 0 return;
1037 }
1038  
1039 # both single IPs and CIDR ranges have host objects that need removing
1040 0 my @to_remove = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
1041  
1042 0 my @requests;
1043 0 foreach my $item ( sort(@to_remove) ) {
1044 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1045 0 push( @requests, $self->_unban_requests($item) );
1046 } else {
1047 0 local $@;
1048 eval {
1049 0 foreach my $req ( $self->_unban_requests($item) ) {
1050 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1051 }
1052 0 1;
1053 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
1054 0 $self->{error} = 25;
1055 0 $self->{errorString} = 'flush failed removing "' . $item . '"... ' . $@;
1056 0 $self->warn;
1057 };
1058 } ## end else [ if ( $self->{testing} ) ]
1059 } ## end foreach my $item ( sort(@to_remove) )
1060  
1061 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1062 0 $self->{frontend_obj}->{test_data} = \@requests;
1063 }
1064  
1065 0 $self->{banned} = {};
1066 0 $self->{banned_cidr} = {};
1067 } ## end sub flush
1068  
1069 =head1 ERROR CODES / FLAGS
1070  
1071 Error handling is provided by L<Error::Helper>. All
1072 errors are considered fatal.
1073  
1074 =head2 1, notInited
1075  
1076 The backend has not been inited yet.
1077  
1078 =head2 6, invalidPrefixSpecified
1079  
1080 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1081  
1082 =head2 7, invalidName
1083  
1084 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1085  
1086 =head2 8, optionsNotHash
1087  
1088 The item passed to new for options is not a hash.
1089  
1090 =head2 9, noBanItem
1091  
1092 No IP or CIDR range specified to ban or unban.
1093  
1094 =head2 10, banItemNotIP
1095  
1096 The item to ban is not an IP. Either wrong ref type or regexp
1097 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1098  
1099 =head2 12, backendInitError
1100  
1101 Failed to init the backend.
1102  
1103 =head2 13, banFailed
1104  
1105 Failed to ban the item.
1106  
1107 =head2 14, unbanFailed
1108  
1109 Failed to unban the item.
1110  
1111 =head2 15, listFailed
1112  
1113 Failed to get a list of bans.
1114  
1115 =head2 16, reInitFailed
1116  
1117 Failed to re_init the backend.
1118  
1119 =head2 17, teardownFailed
1120  
1121 Failed to teardown the backend.
1122  
1123 =head2 18, alreadyInited
1124  
1125 init called, but the backend has already been inited.
1126  
1127 =head2 23, initFailed
1128  
1129 init failed. Logging into the web_api failed.
1130  
1131 =head2 24, checkFailed
1132  
1133 The backend check raised an error.
1134  
1135 =head2 25, flushFailed
1136  
1137 Failed to flush the bans.
1138  
1139 =head2 26, portsNotSupported
1140  
1141 The checkpoint backend blocks whole IPs and does not support ports.
1142  
1143 =head2 27, protocolsNotSupported
1144  
1145 The checkpoint backend blocks whole IPs and does not support protocols.
1146  
1147 =head2 30, hostNotDefined
1148  
1149 The option host is undef or blank.
1150  
1151 =head2 31, userNotDefined
1152  
1153 The option user is undef or blank.
1154  
1155 =head2 32, passwordNotDefined
1156  
1157 The option password is undef or blank.
1158  
1159 =head2 33, banCidrFailed
1160  
1161 Failed to ban the CIDR range.
1162  
1163 =head2 34, unbanCidrFailed
1164  
1165 Failed to unban the CIDR range.
1166  
1167 =head2 35, cidrItemNotCidr
1168  
1169 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1170 IPv4 or IPv6 address followed by a prefix length valid for its family.
1171  
1172 =head2 36, cidrNotSupported
1173  
1174 The backend does not support CIDR bans.
1175  
1176 =head2 37, listCidrFailed
1177  
1178 Failed to get a list of CIDR bans.
1179  
1180 =head1 AUTHOR
1181  
1182 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1183  
1184 =head1 LICENSE AND COPYRIGHT
1185  
1186 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1187  
1188 This is free software, licensed under:
1189  
1190 The GNU Lesser General Public License, Version 2.1, February 1999
1191  
1192 =cut
1193  
1194 1 1; # End of Net::Firewall::BlockerHelper::backends::checkpoint
« BlockerHelper/backends/cloudflare.pm ↑ Summary BlockerHelper/backends/cisco_fmc.pm »