BlockerHelper/backends/fortigate.pm

filter: Statementstmt 37.2% 159 / 427 Branchbran 37.5% 66 / 176 Conditioncond 42.4% 14 / 33 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 83.7% 31 / 37 Podpod 100% 13 / 13 totaltotal 40.5% 286 / 706 CC 112 SCAR 38.6
CC
112
Cov
38%
re_init
110.0
new
86.5
_request
72.0
CRAP 47.6
« BlockerHelper/backends/cisco_fmc.pm ↑ Summary BlockerHelper/backends/netscaler.pm »
1 package Net::Firewall::BlockerHelper::backends::fortigate;
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::fortigate - Fortinet FortiGate backend via the REST API.
13  
14 =head1 VERSION
15  
16 Version 0.2.0
17  
18 =cut
19  
20 1 our $VERSION = '0.2.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => 'fortigate',
28 name => 'ssh',
29 options => {
30 host => 'fw.example.org',
31 token => $fortigate_api_token,
32 group4 => 'blocklist',
33 group6 => 'blocklist6',
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 Fortinet FortiGate via the FortiOS REST API. For each banned
44 IP a firewall address object is created and added as a member of a firewall
45 address group; a firewall policy referencing that group then drops the
46 traffic. IPv4 addresses use C<firewall/address> and C<firewall/addrgrp>, IPv6
47 use C<firewall/address6> and C<firewall/addrgrp6>.
48  
49 This backend manages the address objects and their group membership. The
50 address group(s) and the policy referencing them must already exist on the
51 FortiGate.
52  
53 Auth is via a REST API token (the C<token> option), sent as a bearer token.
54 An optional C<vdom> scopes the calls to a virtual domain.
55  
56 Blocking is per IP; ports and protocols belong on the referencing policy, so
57 specifying them here is an error.
58  
59 L<LWP::UserAgent> is loaded at run time, so it is only required when this
60 backend is actually used. For https, L<LWP::Protocol::https> must be present
61 as well.
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 Initiates the object. Takes the following.
68  
69 - options :: Backend specific options. See below.
70 - prefix :: Prefix to use. Must match /^[a-zA-Z0-9]+$/. Default kur.
71 - name :: Name of this instance. Required.
72  
73 Ports and protocols are not supported and specifying either is an error.
74  
75 The options hash accepts the following.
76  
77 - host :: FortiGate host, optionally host:port. Required.
78 - Default :: undef
79  
80 - token :: REST API token, sent as a bearer token. Required.
81 - Default :: undef
82  
83 - group4 :: IPv4 firewall address group the banned addresses are added to.
84 - Default :: <prefix>_<name>
85  
86 - group6 :: IPv6 firewall address group the banned addresses are added to.
87 - Default :: <prefix>_<name>
88  
89 - vdom :: Optional virtual domain to scope the calls to.
90 - Default :: undef
91  
92 - scheme :: 'https' or 'http'.
93 - Default :: https
94  
95 - insecure :: If true, skip TLS certificate verification.
96 - Default :: 0
97  
98 - timeout :: HTTP timeout in seconds.
99 - Default :: 30
100  
101 All errors are considered fatal, meaning if new fails it will die.
102  
103 =cut
104  
105 4 ▶sub new {
Subroutine new: called CC=26 SCAR=44.6
Pod documented
106 4 my ( $blank, %opts ) = @_;
107  
108 4 my $self = {
109 perror => undef,
110 error => undef,
111 errorLine => undef,
112 errorFilename => undef,
113 errorString => "",
114 errorExtra => {
115 all_errors_fatal => 1,
116 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
117 # is kept for the name documented in its POD
118 all_fatal => 1,
119 flags => {
120 1 => 'notInited',
121 6 => 'invalidPrefixSpecified',
122 7 => 'invalidName',
123 8 => 'optionsNotHash',
124 9 => 'noBanItem',
125 10 => 'banItemNotIP',
126 12 => 'backendInitError',
127 13 => 'banFailed',
128 14 => 'unbanFailed',
129 15 => 'listFailed',
130 16 => 'reInitFailed',
131 17 => 'teardownFailed',
132 18 => 'alreadyInited',
133 23 => 'initFailed',
134 24 => 'checkFailed',
135 25 => 'flushFailed',
136 26 => 'portsNotSupported',
137 27 => 'protocolsNotSupported',
138 30 => 'hostNotDefined',
139 31 => 'tokenNotDefined',
140 32 => 'banCidrFailed',
141 33 => 'unbanCidrFailed',
142 34 => 'cidrItemNotCidr',
143 35 => 'cidrNotSupported',
144 36 => 'listCidrFailed',
145 },
146 fatal_flags => {},
147 perror_not_fatal => 0,
148 },
149 options => {},
150 ports => [],
151 protocols => [],
152 testing => undef,
153 test_data => undef,
154 prefix => 'kur',
155 name => undef,
156 frontend_obj => undef,
157 inited => 0,
158 banned => {},
159 banned_cidr => {},
160 cidr_supported => 1,
161 ua => undef,
162 };
163 4 bless $self;
164  
165 # blocking is per IP; ports/protocols belong on the referencing policy
166 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
167 0 $self->{perror} = 1;
168 0 $self->{error} = 26;
169 0 $self->{errorString} = 'the fortigate backend blocks whole IPs and does not support ports';
170 0 $self->warn;
171 }
172 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
173 0 $self->{perror} = 1;
174 0 $self->{error} = 27;
175 0 $self->{errorString} = 'the fortigate backend blocks whole IPs and does not support protocols';
176 0 $self->warn;
177 }
178  
179 # make sure prefix is sane if defined
180 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
181 0 $self->{perror} = 1;
182 0 $self->{error} = 6;
183 $self->{errorString}
184 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
185 0 $self->warn;
186 } elsif ( defined( $opts{prefix} ) ) {
187 4 $self->{prefix} = $opts{prefix};
188 }
189  
190 # make sure we have a name and that it is valid
191 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
192 0 $self->{perror} = 1;
193 0 $self->{error} = 7;
194 0 $self->{errorString} = 'name is undef';
195 0 $self->warn;
196 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
197 0 $self->{perror} = 1;
198 0 $self->{error} = 7;
199 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
200 0 $self->warn;
201 }
202 4 $self->{name} = $opts{name};
203  
204 4 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 4 0 4
205 4 $self->{testing} = $opts{testing};
206 }
207 4 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 4 0 4
208 4 $self->{frontend_obj} = $opts{frontend_obj};
209 }
210  
211 4 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 4 0 4
212 4 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 4 4
213 0 $self->{perror} = 1;
214 0 $self->{error} = 8;
215 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
216 0 $self->warn;
217 }
218 4 $self->{options} = $opts{options};
219 }
220  
221 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
222 1 $self->{perror} = 1;
223 1 $self->{error} = 30;
224 1 $self->{errorString} = 'the option host is undef or blank';
225 1 $self->warn;
226 }
227 3 ▶ if ( !defined( $self->{options}{token} ) || $self->{options}{token} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"token"} or $self->{"options"}{"token"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"token"} or $self->{"options"}{"token"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"token"} or $self->{"options"}{"token"} eq ""
not defined $self->{"options"}{"token"} $self->{"options"}{"token"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"token"}B= $self->{"options"}{"token"} eq ""
ABresult
1X1
011
000
228 1 $self->{perror} = 1;
229 1 $self->{error} = 31;
230 1 $self->{errorString} = 'the option token is undef or blank';
231 1 $self->warn;
232 }
233  
234 # defaults
235 2 ▶ $self->{options}{scheme} = 'https' if ( !defined( $self->{options}{scheme} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"scheme"} 2 0 2
236 2 ▶ $self->{options}{insecure} = 0 if ( !defined( $self->{options}{insecure} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"insecure"} 2 0 2
237 2 ▶ $self->{options}{timeout} = 30 if ( !defined( $self->{options}{timeout} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"timeout"} 2 0 2
238 2 ▶ if ( defined( $self->{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"name"}) 2 0 2
239 2 ▶ $self->{options}{group4} = $self->{prefix} . '_' . $self->{name} if ( !defined( $self->{options}{group4} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"group4"} 1 1 2
240 2 ▶ $self->{options}{group6} = $self->{prefix} . '_' . $self->{name} if ( !defined( $self->{options}{group6} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"group6"} 2 0 2
241 }
242  
243 2 return $self;
244 } ## end sub new
245  
246 # Internal helper. Says whether an address is IPv4, which is the branch nearly
247 # every other helper in this backend turns on.
248 #
249 # FortiOS keeps IPv4 and IPv6 in entirely separate parts of its configuration
250 # tree, with different object menus and different body fields, so almost
251 # nothing here can be written family agnostically. Rather than repeat the
252 # match, it lives in one place.
253 #
254 # Args:
255 #
256 # ip - The address to test, as a plain string. Expected to be an already
257 # validated and lowercased IPv4 or IPv6 address. Matched against
258 # $IPv4_re anchored, so anything that is not valid IPv4, including a
259 # CIDR range, comes back false. Ranges have their own _cidr_is_v4.
260 #
261 # Returns 1 for an IPv4 address and 0 for anything else. Always one of those
262 # two values, never undef.
263 #
264 # $self->_is_v4('10.0.0.1'); # 1
265 # $self->_is_v4('2001:db8::1'); # 0
266 15 ▶sub _is_v4 {
Subroutine _is_v4: called CC=2 SCAR=6.9
267 15 my ( $self, $ip ) = @_;
268  
269 15 ▶ return ( $ip =~ /\A$IPv4_re\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ ? : 11 4 15
270 }
271  
272 # Internal helper. Returns the name of the FortiOS firewall address object
273 # that represents one banned address.
274 #
275 # FortiOS has no set or list type that simply holds addresses. Each banned
276 # address has to become a named address object, which is then made a member of
277 # an address group that the policy references. So every ban creates an object,
278 # and it needs a name.
279 #
280 # The name is derived from the prefix, the instance name, and the address
281 # rather than stored, so ban and unban independently arrive at the same name
282 # without any local bookkeeping, and two instances on the same FortiGate do
283 # not collide. Dots and colons are replaced with hyphens because FortiOS
284 # rejects them in object names, which is what makes the mangling necessary
285 # rather than just cosmetic.
286 #
287 # Note the substitution is not reversible: 10.0.0.1 and 10-0-0-1 would produce
288 # the same object name. Nothing in this dist ever bans the latter, so it does
289 # not arise in practice.
290 #
291 # Args:
292 #
293 # ip - The address to name an object for, as a plain string. Expected to
294 # be an already validated and lowercased IPv4 or IPv6 address.
295 #
296 # Returns the object name as a plain string, safe to use in a FortiOS
297 # configuration. Still needs percent encoding before going into a URL, which
298 # the callers do.
299 #
300 # # with prefix "kur" and name "ssh"
301 # $self->_addr_name('10.0.0.1'); # kur_ssh_10-0-0-1
302 # $self->_addr_name('2001:db8::1'); # kur_ssh_2001-db8--1
303 8 ▶sub _addr_name {
Subroutine _addr_name: called CC=1 SCAR=0.0
304 8 my ( $self, $ip ) = @_;
305  
306 8 my $name = $self->{prefix} . '_' . $self->{name} . '_' . $ip;
307 # dots and colons are not valid in FortiOS object names
308 8 $name =~ s/[.:]/-/g;
309  
310 8 return $name;
311 } ## end sub _addr_name
312  
313 # Internal helper. Returns which of the two configured address groups an
314 # address belongs in.
315 #
316 # A FortiOS address group is single family, so blocking both means two groups
317 # on the appliance side, and the caller has to pick the right one for every
318 # membership change. Both groups are expected to already exist and to be
319 # referenced by a policy; this backend adds and removes members but does not
320 # create the groups.
321 #
322 # Args:
323 #
324 # ip - The address whose group is wanted, as a plain string. Expected to
325 # be an already validated and lowercased IPv4 or IPv6 address.
326 #
327 # Returns the group name as a plain string, from either the group4 or the
328 # group6 option. Returns undef if the relevant option was never set, though
329 # new rejects that so it should not arise.
330 #
331 # # with group4 'blocklist4' and group6 'blocklist6'
332 # $self->_group_name('10.0.0.1'); # blocklist4
333 # $self->_group_name('2001:db8::1'); # blocklist6
334 4 ▶sub _group_name {
Subroutine _group_name: called CC=2 SCAR=6.9
335 4 my ( $self, $ip ) = @_;
336  
337 4 ▶ return $self->_is_v4($ip) ? $self->{options}{group4} : $self->{options}{group6};
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_v4($ip) ? : 3 1 4
338 }
339  
340 # Internal helper. Builds the full URL for a FortiOS cmdb REST endpoint. Every
341 # request this backend makes goes through here.
342 #
343 # The cmdb tree is FortiOS's configuration API, so all the object and group
344 # manipulation lives under /api/v2/cmdb/. The vdom query parameter is appended
345 # only when the option is set; on an appliance with virtual domains enabled,
346 # leaving it off means the request lands in whichever vdom the token's account
347 # defaults to, which may not be the intended one.
348 #
349 # The vdom is percent encoded, as it is a configured name rather than
350 # something derived.
351 #
352 # Args:
353 #
354 # path - The cmdb path below /api/v2/cmdb/, as a plain string, such as
355 # 'firewall/address' or
356 # 'firewall/addrgrp/blocklist4/member/kur_ssh_10-0-0-1'. Appended
357 # as is, so any object names embedded in it are expected to have
358 # been percent encoded by the caller already.
359 #
360 # Returns the complete URL as a string. The scheme and host come from the
361 # options.
362 #
363 # $self->_cmdb_url('firewall/address');
364 # # https://fw.example.org/api/v2/cmdb/firewall/address
365 #
366 # # with the vdom option set to root
367 # # https://fw.example.org/api/v2/cmdb/firewall/address?vdom=root
368 20 ▶sub _cmdb_url {
Subroutine _cmdb_url: called CC=3 SCAR=11.0
369 20 my ( $self, $path ) = @_;
370  
371 20 my $url = $self->{options}{scheme} . '://' . $self->{options}{host} . '/api/v2/cmdb/' . $path;
372  
373 20 ▶ if ( defined( $self->{options}{vdom} ) && $self->{options}{vdom} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"vdom"} and $self->{"options"}{"vdom"} ne "") 16 4 20
Condition66%2/3
defined $self->{"options"}{"vdom"} and $self->{"options"}{"vdom"} ne ""
!ll&&!rl&&r
4016
MC/DC50%1/2
defined $self->{"options"}{"vdom"} and $self->{"options"}{"vdom"} ne ""
defined $self->{"options"}{"vdom"} $self->{"options"}{"vdom"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"options"}{"vdom"}B= $self->{"options"}{"vdom"} ne ""
ABresult
0X0
100
111
374 16 $url .= '?vdom=' . $self->_uri_escape( $self->{options}{vdom} );
375 }
376  
377 20 return $url;
378 } ## end sub _cmdb_url
379  
380 # Internal helper. Returns the cmdb path of the address object menu for an
381 # address's family.
382 #
383 # FortiOS keeps IPv4 and IPv6 address objects in two separate menus rather
384 # than in one menu with a family field, so the path itself differs. Creating
385 # an IPv6 object under the IPv4 menu is rejected.
386 #
387 # Args:
388 #
389 # ip - The address whose menu is wanted, as a plain string. Expected to be
390 # an already validated and lowercased IPv4 or IPv6 address.
391 #
392 # Returns the path as a plain string, relative to the cmdb root, ready to be
393 # handed to _cmdb_url.
394 #
395 # $self->_address_path('10.0.0.1'); # firewall/address
396 # $self->_address_path('2001:db8::1'); # firewall/address6
397 4 ▶sub _address_path {
Subroutine _address_path: called CC=2 SCAR=6.9
398 4 my ( $self, $ip ) = @_;
399  
400 4 ▶ return $self->_is_v4($ip) ? 'firewall/address' : 'firewall/address6';
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_v4($ip) ? : 3 1 4
401 }
402  
403 # Internal helper. Returns the cmdb path of the address group menu for an
404 # address's family.
405 #
406 # The group menus are split by family for the same reason the address object
407 # menus are, and an IPv6 group cannot be reached through the IPv4 menu.
408 #
409 # Args:
410 #
411 # ip - The address whose group menu is wanted, as a plain string. Expected
412 # to be an already validated and lowercased IPv4 or IPv6 address.
413 #
414 # Returns the path as a plain string, relative to the cmdb root. Callers
415 # append the group name and the member path to it before handing it to
416 # _cmdb_url.
417 #
418 # $self->_addrgrp_path('10.0.0.1'); # firewall/addrgrp
419 # $self->_addrgrp_path('2001:db8::1'); # firewall/addrgrp6
420 4 ▶sub _addrgrp_path {
Subroutine _addrgrp_path: called CC=2 SCAR=6.9
421 4 my ( $self, $ip ) = @_;
422  
423 4 ▶ return $self->_is_v4($ip) ? 'firewall/addrgrp' : 'firewall/addrgrp6';
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_v4($ip) ? : 3 1 4
424 }
425  
426 # Internal helper. Builds the JSON body that creates the FortiOS address
427 # object for one banned address.
428 #
429 # The address is written as a host prefix, /32 or /128, because a FortiOS
430 # address object of this kind is always a subnet; there is no single host
431 # type. The field it goes in differs by family, subnet for IPv4 and ip6 for
432 # IPv6, which is why this cannot be written once.
433 #
434 # Note the JSON comes back with its keys sorted, since the encoder is
435 # canonical, so the ip6 form has its fields in the opposite order to what the
436 # code reads.
437 #
438 # Args:
439 #
440 # ip - The address to build the body for, as a plain string. Expected to
441 # be an already validated and lowercased IPv4 or IPv6 address.
442 #
443 # Returns the body as a JSON string, ready to be the content of a POST to the
444 # family's address menu.
445 #
446 # $self->_address_body('10.0.0.1');
447 # # {"name":"kur_ssh_10-0-0-1","subnet":"10.0.0.1/32"}
448 #
449 # $self->_address_body('2001:db8::1');
450 # # {"ip6":"2001:db8::1/128","name":"kur_ssh_2001-db8--1"}
451 3 ▶sub _address_body {
Subroutine _address_body: called CC=2 SCAR=6.9
452 3 my ( $self, $ip ) = @_;
453  
454 3 ▶ if ( $self->_is_v4($ip) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_v4($ip)) 2 1 3
455 2 return $self->_json->encode( { name => $self->_addr_name($ip), subnet => $ip . '/32' } );
456 }
457  
458 1 return $self->_json->encode( { name => $self->_addr_name($ip), ip6 => $ip . '/128' } );
459 } ## end sub _address_body
460  
461 # Internal helper. Performs one HTTP request against the FortiOS REST API.
462 # Every call this backend makes to the appliance goes through here.
463 #
464 # The user agent is built on first use and then cached on the object, so a run
465 # of requests shares one agent and its connection rather than building a new
466 # one each time. LWP::UserAgent is loaded with require at that point rather
467 # than at compile time, since only the HTTP backends need it and it is a
468 # recommends rather than a hard prereq; failing to load it dies with an
469 # explanation naming this backend.
470 #
471 # The insecure option turns off certificate verification, which is there
472 # because FortiGates very commonly present a self signed certificate.
473 #
474 # Authentication is a bearer token on every request; there is no session to
475 # establish, unlike the checkpoint and cisco_fmc backends.
476 #
477 # Any HTTP level failure dies rather than setting an error, with the method,
478 # URL, status line, and response body in the message. The callers wrap this in
479 # eval and turn the exception into the appropriate error code, which is what
480 # lets one helper serve paths that need to report ban, unban, and teardown
481 # failures differently.
482 #
483 # A body that fails to decode as JSON is not fatal. The decode runs inside its
484 # own eval and leaves the result undef, because several FortiOS endpoints
485 # answer with an empty body on success and there is nothing to parse.
486 #
487 # Never called in testing mode; those paths record the request descriptors
488 # instead.
489 #
490 # Args:
491 #
492 # method - The HTTP method, as a plain string, such as 'GET', 'POST', or
493 # 'DELETE'.
494 #
495 # url - The full URL to request, as a plain string, normally from
496 # _cmdb_url.
497 #
498 # body - Optional request body, as an already encoded JSON string.
499 # Passed straight to HTTP::Request, so undef is fine for methods
500 # that carry no body.
501 #
502 # Returns the decoded response body as whatever structure the JSON held,
503 # normally a hashref, or undef when the response body was empty or did not
504 # parse. Dies on any non success HTTP status.
505 #
506 # my $decoded = $self->_request( 'GET', $self->_cmdb_url('firewall/addrgrp') );
507 #
508 # $self->_request( 'POST', $self->_cmdb_url('firewall/address'),
509 # $self->_address_body($ip) );
510 #
511 # # the usual shape at the call sites
512 # eval { $self->_request( $r->{method}, $r->{url}, $r->{content} ); };
513 # if ($@) { ... raise banFailed ... }
514 0 ▶sub _request {
Subroutine _request: not called CC=8 SCAR=42.8
515 0 my ( $self, $method, $url, $body ) = @_;
516  
517 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
518 0 local $@;
519 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent; my(%args) = ("agent", 'Net::Firewall::BlockerHelper/' . $Net::Firewall::BlockerHelper::backends::fortigate::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 fortigate backend requires... ' . $@ 0 0 0
520 0 require LWP::UserAgent;
521 my %args = (
522 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
523 timeout => $self->{options}{timeout},
524 0 );
525 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
526 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
527 }
528 0 $self->{ua} = LWP::UserAgent->new(%args);
529 0 1;
530 } or die( 'failed to load LWP::UserAgent, which the fortigate backend requires... ' . $@ );
531 }
532  
533 my @headers = (
534 'Content-Type' => 'application/json',
535 'Authorization' => 'Bearer ' . $self->{options}{token},
536 0 );
537  
538 0 require HTTP::Request;
539 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
540  
541 0 my $response = $self->{ua}->request($request);
542  
543 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
544 0 my $detail = $response->decoded_content;
545 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
546 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
547 }
548  
549 0 my $decoded;
550 0 my $content = $response->decoded_content;
551 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
552 0 local $@;
553 0 eval { $decoded = $self->_json->decode($content); };
554 }
555  
556 0 return $decoded;
557 } ## end sub _request
558  
559 # Internal helper. Returns the pair of requests that ban one address.
560 #
561 # Banning takes two calls because FortiOS separates the address object from
562 # group membership: the object has to exist before it can be made a member, so
563 # the order matters and the two are returned together rather than being
564 # sequenced by the caller.
565 #
566 # Returning descriptors rather than performing the requests is what lets the
567 # testing paths record exactly what would have been sent without a FortiGate
568 # being present.
569 #
570 # Args:
571 #
572 # ip - The address to ban, as a plain string. Expected to be an already
573 # validated and lowercased IPv4 or IPv6 address. The family selects
574 # the menus and the group.
575 #
576 # Returns a two element list of hashrefs, in the order they must be performed,
577 # each of the form:
578 #
579 # {
580 # method => 'POST',
581 # url => 'https://fw.example.org/api/v2/cmdb/firewall/address',
582 # content => '{"name":"kur_ssh_10-0-0-1","subnet":"10.0.0.1/32"}',
583 # }
584 #
585 # my @requests = $self->_ban_requests('10.0.0.1');
586 #
587 # # first, create the address object
588 # # POST https://fw.example.org/api/v2/cmdb/firewall/address
589 # # {"name":"kur_ssh_10-0-0-1","subnet":"10.0.0.1/32"}
590 #
591 # # then, add it to the group
592 # # POST https://fw.example.org/api/v2/cmdb/firewall/addrgrp/blocklist4/member
593 # # {"name":"kur_ssh_10-0-0-1"}
594 3 ▶sub _ban_requests {
Subroutine _ban_requests: called CC=1 SCAR=0.0
595 3 my ( $self, $ip ) = @_;
596  
597 3 my $member = $self->_json->encode( { name => $self->_addr_name($ip) } );
598  
599 return (
600 3 { method => 'POST', url => $self->_cmdb_url( $self->_address_path($ip) ), content => $self->_address_body($ip) },
601 {
602 method => 'POST',
603 url => $self->_cmdb_url( $self->_addrgrp_path($ip) . '/' . $self->_uri_escape( $self->_group_name($ip) ) . '/member' ),
604 content => $member,
605 },
606 );
607 } ## end sub _ban_requests
608  
609 # Internal helper. Returns the pair of requests that unban one address, the
610 # inverse of _ban_requests.
611 #
612 # The order is reversed from the ban: membership is removed first, then the
613 # object is deleted. FortiOS refuses to delete an address object that is still
614 # a member of a group, so deleting first would simply fail.
615 #
616 # Neither request carries a body, since both identify what to act on entirely
617 # through the URL. The object and group names are percent encoded going into
618 # the path.
619 #
620 # Args:
621 #
622 # ip - The address to unban, as a plain string. Expected to be an already
623 # validated and lowercased IPv4 or IPv6 address, and to produce the
624 # same object name it did when banned.
625 #
626 # Returns a two element list of hashrefs, in the order they must be performed,
627 # each with a method and a url key and no content key.
628 #
629 # my @requests = $self->_unban_requests('10.0.0.1');
630 #
631 # # first, drop the group membership
632 # # DELETE https://fw.example.org/api/v2/cmdb/firewall/addrgrp/blocklist4/member/kur_ssh_10-0-0-1
633 #
634 # # then, delete the address object
635 # # DELETE https://fw.example.org/api/v2/cmdb/firewall/address/kur_ssh_10-0-0-1
636 1 ▶sub _unban_requests {
Subroutine _unban_requests: called CC=1 SCAR=0.0
637 1 my ( $self, $ip ) = @_;
638  
639 return (
640 {
641 1 method => 'DELETE',
642 url => $self->_cmdb_url(
643 $self->_addrgrp_path($ip) . '/'
644 . $self->_uri_escape( $self->_group_name($ip) )
645 . '/member/'
646 . $self->_uri_escape( $self->_addr_name($ip) )
647 ),
648 },
649 { method => 'DELETE', url => $self->_cmdb_url( $self->_address_path($ip) . '/' . $self->_uri_escape( $self->_addr_name($ip) ) ) },
650 );
651 } ## end sub _unban_requests
652  
653 # Internal helper. Says whether a CIDR range is IPv4, the range counterpart of
654 # _is_v4.
655 #
656 # A separate sub is needed rather than passing a range to _is_v4 because a
657 # range always carries a "/" and a prefix length, which would never match the
658 # anchored IPv4 regexp, so every range would come back as IPv6. The prefix is
659 # stripped off first and only the address is matched.
660 #
661 # Args:
662 #
663 # cidr - The range to test, as a plain string. Expected to be an already
664 # validated CIDR range such as "10.0.0.0/8" or "2001:db8::/32". A
665 # bare address with no prefix also works, since the strip is a no
666 # op then.
667 #
668 # Returns 1 for a range whose address is IPv4 and 0 for anything else. Always
669 # one of those two values, never undef.
670 #
671 # $self->_cidr_is_v4('10.0.0.0/8'); # 1
672 # $self->_cidr_is_v4('2001:db8::/32'); # 0
673 7 ▶sub _cidr_is_v4 {
Subroutine _cidr_is_v4: called CC=2 SCAR=7.0
674 7 my ( $self, $cidr ) = @_;
675  
676 7 my $addr = $cidr;
677 7 $addr =~ s!/[0-9]+\z!!;
678  
679 7 ▶ return ( $addr =~ /\A$IPv4_re\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ ? : 7 0 7
680 } ## end sub _cidr_is_v4
681  
682 # Internal helper. Returns which of the two configured address groups a CIDR
683 # range belongs in, the range counterpart of _group_name.
684 #
685 # Ranges go into the same two groups the single addresses do; FortiOS does not
686 # distinguish a host object from a subnet object at the group level.
687 #
688 # Args:
689 #
690 # cidr - The range whose group is wanted, as a plain string. Expected to
691 # be an already validated CIDR range.
692 #
693 # Returns the group name as a plain string, from either the group4 or the
694 # group6 option.
695 #
696 # # with group4 'blocklist4' and group6 'blocklist6'
697 # $self->_cidr_group_name('10.0.0.0/8'); # blocklist4
698 # $self->_cidr_group_name('2001:db8::/32'); # blocklist6
699 2 ▶sub _cidr_group_name {
Subroutine _cidr_group_name: called CC=2 SCAR=7.2
700 2 my ( $self, $cidr ) = @_;
701  
702 2 ▶ return $self->_cidr_is_v4($cidr) ? $self->{options}{group4} : $self->{options}{group6};
Branch: 1 branch
BranchTrueFalseTotal
$self->_cidr_is_v4($cidr) ? : 2 0 2
703 }
704  
705 # Internal helper. Returns the cmdb path of the address object menu for a CIDR
706 # range's family, the range counterpart of _address_path.
707 #
708 # The menus are the same ones single addresses use; only the way the family is
709 # determined differs.
710 #
711 # Args:
712 #
713 # cidr - The range whose menu is wanted, as a plain string. Expected to be
714 # an already validated CIDR range.
715 #
716 # Returns the path as a plain string, relative to the cmdb root, ready to be
717 # handed to _cmdb_url.
718 #
719 # $self->_cidr_address_path('10.0.0.0/8'); # firewall/address
720 # $self->_cidr_address_path('2001:db8::/32'); # firewall/address6
721 2 ▶sub _cidr_address_path {
Subroutine _cidr_address_path: called CC=2 SCAR=7.2
722 2 my ( $self, $cidr ) = @_;
723  
724 2 ▶ return $self->_cidr_is_v4($cidr) ? 'firewall/address' : 'firewall/address6';
Branch: 1 branch
BranchTrueFalseTotal
$self->_cidr_is_v4($cidr) ? : 2 0 2
725 }
726  
727 # Internal helper. Returns the cmdb path of the address group menu for a CIDR
728 # range's family, the range counterpart of _addrgrp_path.
729 #
730 # Args:
731 #
732 # cidr - The range whose group menu is wanted, as a plain string. Expected
733 # to be an already validated CIDR range.
734 #
735 # Returns the path as a plain string, relative to the cmdb root. Callers
736 # append the group name and the member path to it.
737 #
738 # $self->_cidr_addrgrp_path('10.0.0.0/8'); # firewall/addrgrp
739 # $self->_cidr_addrgrp_path('2001:db8::/32'); # firewall/addrgrp6
740 2 ▶sub _cidr_addrgrp_path {
Subroutine _cidr_addrgrp_path: called CC=2 SCAR=7.2
741 2 my ( $self, $cidr ) = @_;
742  
743 2 ▶ return $self->_cidr_is_v4($cidr) ? 'firewall/addrgrp' : 'firewall/addrgrp6';
Branch: 1 branch
BranchTrueFalseTotal
$self->_cidr_is_v4($cidr) ? : 2 0 2
744 }
745  
746 # Internal helper. Returns the name of the FortiOS address object representing
747 # one banned CIDR range, the range counterpart of _addr_name.
748 #
749 # Same reasoning as for single addresses: each banned range becomes a named
750 # object that is then made a group member, and the name is derived rather than
751 # stored so ban and unban independently agree on it.
752 #
753 # The mangling has to cover one more character than _addr_name does. As well
754 # as dots and colons, the slash separating the address from the prefix length
755 # is replaced with a hyphen, since FortiOS rejects it in an object name too.
756 #
757 # Note this makes the name ambiguous in a way worth being aware of: the range
758 # 10.0.0.0/8 and a hypothetical address 10.0.0.0.8 would both mangle to the
759 # same object name. Ranges and addresses are validated separately upstream, so
760 # nothing in this dist produces such a collision.
761 #
762 # Args:
763 #
764 # cidr - The range to name an object for, as a plain string. Expected to
765 # be an already validated CIDR range.
766 #
767 # Returns the object name as a plain string, safe to use in a FortiOS
768 # configuration. Still needs percent encoding before going into a URL.
769 #
770 # # with prefix "kur" and name "ssh"
771 # $self->_cidr_addr_name('10.0.0.0/8'); # kur_ssh_10-0-0-0-8
772 # $self->_cidr_addr_name('2001:db8::/32'); # kur_ssh_2001-db8---32
773 4 ▶sub _cidr_addr_name {
Subroutine _cidr_addr_name: called CC=1 SCAR=0.0
774 4 my ( $self, $cidr ) = @_;
775  
776 4 my $name = $self->{prefix} . '_' . $self->{name} . '_' . $cidr;
777 # dots, colons, and slashes are not valid in FortiOS object names
778 4 $name =~ s/[.:\/]/-/g;
779  
780 4 return $name;
781 } ## end sub _cidr_addr_name
782  
783 # Internal helper. Builds the JSON body that creates the FortiOS address
784 # object for one banned CIDR range, the range counterpart of _address_body.
785 #
786 # The difference from the single address version is that no prefix has to be
787 # appended: a range already is a subnet, which is exactly what a FortiOS
788 # address object of this kind holds, so it goes in as it stands. The field
789 # still differs by family, subnet for IPv4 and ip6 for IPv6.
790 #
791 # Note the JSON comes back with its keys sorted, since the encoder is
792 # canonical, so the ip6 form has its fields in the opposite order to what the
793 # code reads.
794 #
795 # Args:
796 #
797 # cidr - The range to build the body for, as a plain string. Expected to
798 # be an already validated CIDR range.
799 #
800 # Returns the body as a JSON string, ready to be the content of a POST to the
801 # family's address menu.
802 #
803 # $self->_cidr_address_body('10.0.0.0/8');
804 # # {"name":"kur_ssh_10-0-0-0-8","subnet":"10.0.0.0/8"}
805 #
806 # $self->_cidr_address_body('2001:db8::/32');
807 # # {"ip6":"2001:db8::/32","name":"kur_ssh_2001-db8---32"}
808 1 ▶sub _cidr_address_body {
Subroutine _cidr_address_body: called CC=2 SCAR=7.6
809 1 my ( $self, $cidr ) = @_;
810  
811 1 ▶ if ( $self->_cidr_is_v4($cidr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cidr_is_v4($cidr)) 1 0 1
812 1 return $self->_json->encode( { name => $self->_cidr_addr_name($cidr), subnet => $cidr } );
813 }
814  
815 0 return $self->_json->encode( { name => $self->_cidr_addr_name($cidr), ip6 => $cidr } );
816 } ## end sub _cidr_address_body
817  
818 # Internal helper. Returns the pair of requests that ban one CIDR range, the
819 # range counterpart of _ban_requests.
820 #
821 # The two step shape and its ordering are the same: the address object has to
822 # exist before it can be added to the group.
823 #
824 # Args:
825 #
826 # cidr - The range to ban, as a plain string. Expected to be an already
827 # validated CIDR range. The family selects the menus and the group.
828 #
829 # Returns a two element list of hashrefs, in the order they must be performed,
830 # each with method, url, and content keys.
831 #
832 # my @requests = $self->_ban_cidr_requests('10.0.0.0/8');
833 #
834 # # first, create the address object
835 # # POST https://fw.example.org/api/v2/cmdb/firewall/address
836 # # {"name":"kur_ssh_10-0-0-0-8","subnet":"10.0.0.0/8"}
837 #
838 # # then, add it to the group
839 # # POST https://fw.example.org/api/v2/cmdb/firewall/addrgrp/blocklist4/member
840 # # {"name":"kur_ssh_10-0-0-0-8"}
841 1 ▶sub _ban_cidr_requests {
Subroutine _ban_cidr_requests: called CC=1 SCAR=0.0
842 1 my ( $self, $cidr ) = @_;
843  
844 1 my $member = $self->_json->encode( { name => $self->_cidr_addr_name($cidr) } );
845  
846 return (
847 {
848 1 method => 'POST',
849 url => $self->_cmdb_url( $self->_cidr_address_path($cidr) ),
850 content => $self->_cidr_address_body($cidr),
851 },
852 {
853 method => 'POST',
854 url => $self->_cmdb_url(
855 $self->_cidr_addrgrp_path($cidr) . '/' . $self->_uri_escape( $self->_cidr_group_name($cidr) ) . '/member'
856 ),
857 content => $member,
858 },
859 );
860 } ## end sub _ban_cidr_requests
861  
862 # Internal helper. Returns the pair of requests that unban one CIDR range, the
863 # range counterpart of _unban_requests.
864 #
865 # As with single addresses the order is reversed from the ban, membership
866 # first and then the object, because FortiOS will not delete an address object
867 # that is still a group member.
868 #
869 # Neither request carries a body. The object and group names are percent
870 # encoded going into the path.
871 #
872 # Args:
873 #
874 # cidr - The range to unban, as a plain string. Expected to be an already
875 # validated CIDR range, and to produce the same object name it did
876 # when banned.
877 #
878 # Returns a two element list of hashrefs, in the order they must be performed,
879 # each with a method and a url key and no content key.
880 #
881 # my @requests = $self->_unban_cidr_requests('10.0.0.0/8');
882 #
883 # # first, drop the group membership
884 # # DELETE https://fw.example.org/api/v2/cmdb/firewall/addrgrp/blocklist4/member/kur_ssh_10-0-0-0-8
885 #
886 # # then, delete the address object
887 # # DELETE https://fw.example.org/api/v2/cmdb/firewall/address/kur_ssh_10-0-0-0-8
888 1 ▶sub _unban_cidr_requests {
Subroutine _unban_cidr_requests: called CC=1 SCAR=0.0
889 1 my ( $self, $cidr ) = @_;
890  
891 return (
892 {
893 1 method => 'DELETE',
894 url => $self->_cmdb_url(
895 $self->_cidr_addrgrp_path($cidr) . '/'
896 . $self->_uri_escape( $self->_cidr_group_name($cidr) )
897 . '/member/'
898 . $self->_uri_escape( $self->_cidr_addr_name($cidr) )
899 ),
900 },
901 {
902 method => 'DELETE',
903 url => $self->_cmdb_url(
904 $self->_cidr_address_path($cidr) . '/' . $self->_uri_escape( $self->_cidr_addr_name($cidr) )
905 ),
906 },
907 );
908 } ## end sub _unban_cidr_requests
909  
910 =head2 init
911  
912 Initiates the backend. Verifies the token and reachability by fetching the
913 IPv4 address group.
914  
915 =cut
916  
917 2 ▶sub init {
Subroutine init: called CC=4 SCAR=20.3
Pod documented
918 2 my ( $self, %opts ) = @_;
919  
920 2 $self->errorblank;
921  
922 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
923 0 $self->{error} = 18;
924 0 $self->{errorString} = 'backend has already been inited';
925 0 $self->warn;
926 }
927  
928 2 my $url = $self->_cmdb_url( 'firewall/addrgrp/' . $self->_uri_escape( $self->{options}{group4} ) );
929  
930 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
931 2 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
932 } else {
933 0 local $@;
934 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or do { $self->{"error"} = 23; $self->{"errorString"} = 'init failed. probing the address group failed... ' . $@; $self->warn } 0 0 0
935 0 $self->{error} = 23;
936 0 $self->{errorString} = 'init failed. probing the address group failed... ' . $@;
937 0 $self->warn;
938 };
939 }
940  
941 2 $self->{inited} = 1;
942 } ## end sub init
943  
944 =head2 ban
945  
946 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
947 and lowercased. A firewall address object named
948 C<E<lt>prefixE<gt>_E<lt>nameE<gt>_E<lt>ipE<gt>> (dots and colons replaced
949 with dashes) is created via a POST to C<firewall/address> (IPv4) or
950 C<firewall/address6> (IPv6) and then added as a member of the family's
951 address group via a POST to the group's C<member> endpoint. Banning an
952 already banned IP is a noop.
953  
954 $backend->ban(ban => $ip);
955  
956 =cut
957  
958 3 ▶sub ban {
Subroutine ban: called CC=11 SCAR=39.1
Pod documented
959 3 my ( $self, %opts ) = @_;
960  
961 3 $self->errorblank;
962  
963 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
964 0 $self->{error} = 1;
965 0 $self->{errorString} = 'backend has not been inited';
966 0 $self->warn;
967 0 return;
968 }
969  
970 3 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 3 3
elsif (ref $opts{'ban'} ne '') { } 0 3 3
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/) { } 0 3 3
971 0 $self->{error} = 9;
972 0 $self->{errorString} = 'Nothing specified for the value ban';
973 0 $self->warn;
974 0 return;
975 } elsif ( ref( $opts{ban} ) ne '' ) {
976 0 $self->{error} = 10;
977 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
978 0 $self->warn;
979 0 return;
980 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/
l!l&&r!l&&!r
210
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/
ABresult
1X1
011
000
981 && $opts{ban} !~ /\A$IPv6_re\z/ )
982 {
983 0 $self->{error} = 10;
984 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
985 0 $self->warn;
986 0 return;
987 }
988  
989 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
990 3 $opts{ban} = lc( $opts{ban} );
991  
992 3 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 3 3
993 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
994 0 $self->{frontend_obj}->{test_data} = 'already banned';
995 }
996 0 return;
997 }
998  
999 3 my @requests = $self->_ban_requests( $opts{ban} );
1000  
1001 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
1002 3 $self->{frontend_obj}->{test_data} = \@requests;
1003 } else {
1004 0 local $@;
1005 eval {
1006 0 foreach my $req (@requests) {
1007 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1008 }
1009 0 1;
1010 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
1011 0 $self->{error} = 13;
1012 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
1013 0 $self->warn;
1014 0 return;
1015 };
1016 } ## end else [ if ( $self->{testing} ) ]
1017  
1018 3 $self->{banned}{ $opts{ban} } = 1;
1019 } ## end sub ban
1020  
1021 =head2 unban
1022  
1023 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
1024 and lowercased. The IP's address object is removed from the family's address
1025 group via a DELETE on the group's C<member> endpoint and then the address
1026 object itself is deleted via a DELETE on C<firewall/address> (IPv4) or
1027 C<firewall/address6> (IPv6). Unbanning an IP that is not banned is a noop.
1028  
1029 $backend->unban(ban => $ip);
1030  
1031 =cut
1032  
1033 1 ▶sub unban {
Subroutine unban: called CC=11 SCAR=39.7
Pod documented
1034 1 my ( $self, %opts ) = @_;
1035  
1036 1 $self->errorblank;
1037  
1038 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
1039 0 $self->{error} = 1;
1040 0 $self->{errorString} = 'backend has not been inited';
1041 0 $self->warn;
1042 0 return;
1043 }
1044  
1045 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::fortigate::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/) { } 0 1 1
1046 0 $self->{error} = 9;
1047 0 $self->{errorString} = 'Nothing specified for the value ban';
1048 0 $self->warn;
1049 0 return;
1050 } elsif ( ref( $opts{ban} ) ne '' ) {
1051 0 $self->{error} = 10;
1052 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
1053 0 $self->warn;
1054 0 return;
1055 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/
ABresult
1X1
011
000
1056 && $opts{ban} !~ /\A$IPv6_re\z/ )
1057 {
1058 0 $self->{error} = 10;
1059 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
1060 0 $self->warn;
1061 0 return;
1062 }
1063  
1064 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
1065 1 $opts{ban} = lc( $opts{ban} );
1066  
1067 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
1068 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1069 0 $self->{frontend_obj}->{test_data} = 'not banned';
1070 }
1071 0 return;
1072 }
1073  
1074 1 my @requests = $self->_unban_requests( $opts{ban} );
1075  
1076 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
1077 1 $self->{frontend_obj}->{test_data} = \@requests;
1078 } else {
1079 0 local $@;
1080 eval {
1081 0 foreach my $req (@requests) {
1082 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1083 }
1084 0 1;
1085 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
1086 0 $self->{error} = 14;
1087 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
1088 0 $self->warn;
1089 0 return;
1090 };
1091 } ## end else [ if ( $self->{testing} ) ]
1092  
1093 1 delete( $self->{banned}{ $opts{ban} } );
1094 } ## end sub unban
1095  
1096 =head2 ban_cidr
1097  
1098 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
1099 CIDR and lowercased. A subnet firewall address object named
1100 C<E<lt>prefixE<gt>_E<lt>nameE<gt>_E<lt>cidrE<gt>> (dots, colons, and slashes
1101 replaced with dashes) is created via a POST to C<firewall/address> (IPv4) or
1102 C<firewall/address6> (IPv6) and then added as a member of the family's
1103 address group. Banning an already banned CIDR is a noop.
1104  
1105 $backend->ban_cidr(ban => '1.2.3.0/24');
1106  
1107 =cut
1108  
1109 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=10 SCAR=38.0
Pod documented
1110 1 my ( $self, %opts ) = @_;
1111  
1112 1 $self->errorblank;
1113  
1114 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
1115 0 $self->{error} = 1;
1116 0 $self->{errorString} = 'backend has not been inited';
1117 0 $self->warn;
1118 0 return;
1119 }
1120  
1121 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
1122 0 $self->{error} = 9;
1123 0 $self->{errorString} = 'Nothing specified for the value ban';
1124 0 $self->warn;
1125 0 return;
1126 } elsif ( ref( $opts{ban} ) ne '' ) {
1127 0 $self->{error} = 34;
1128 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
1129 0 $self->warn;
1130 0 return;
1131 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
1132 0 $self->{error} = 34;
1133 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
1134 0 $self->warn;
1135 0 return;
1136 }
1137  
1138 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
1139 1 $opts{ban} = lc( $opts{ban} );
1140  
1141 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
1142 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1143 0 $self->{frontend_obj}->{test_data} = 'already banned';
1144 }
1145 0 return;
1146 }
1147  
1148 1 my @requests = $self->_ban_cidr_requests( $opts{ban} );
1149  
1150 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
1151 1 $self->{frontend_obj}->{test_data} = \@requests;
1152 } else {
1153 0 local $@;
1154 eval {
1155 0 foreach my $req (@requests) {
1156 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1157 }
1158 0 1;
1159 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"} = 32; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
1160 0 $self->{error} = 32;
1161 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
1162 0 $self->warn;
1163 0 return;
1164 };
1165 } ## end else [ if ( $self->{testing} ) ]
1166  
1167 1 $self->{banned_cidr}{ $opts{ban} } = 1;
1168 } ## end sub ban_cidr
1169  
1170 =head2 unban_cidr
1171  
1172 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
1173 CIDR and lowercased. The CIDR's address object is removed from the family's
1174 address group via a DELETE on the group's C<member> endpoint and then the
1175 address object itself is deleted. Unbanning a CIDR that is not banned is a
1176 noop.
1177  
1178 $backend->unban_cidr(ban => '1.2.3.0/24');
1179  
1180 =cut
1181  
1182 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=10 SCAR=38.0
Pod documented
1183 1 my ( $self, %opts ) = @_;
1184  
1185 1 $self->errorblank;
1186  
1187 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
1188 0 $self->{error} = 1;
1189 0 $self->{errorString} = 'backend has not been inited';
1190 0 $self->warn;
1191 0 return;
1192 }
1193  
1194 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
1195 0 $self->{error} = 9;
1196 0 $self->{errorString} = 'Nothing specified for the value ban';
1197 0 $self->warn;
1198 0 return;
1199 } elsif ( ref( $opts{ban} ) ne '' ) {
1200 0 $self->{error} = 34;
1201 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
1202 0 $self->warn;
1203 0 return;
1204 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
1205 0 $self->{error} = 34;
1206 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
1207 0 $self->warn;
1208 0 return;
1209 }
1210  
1211 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
1212 1 $opts{ban} = lc( $opts{ban} );
1213  
1214 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
1215 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1216 0 $self->{frontend_obj}->{test_data} = 'not banned';
1217 }
1218 0 return;
1219 }
1220  
1221 1 my @requests = $self->_unban_cidr_requests( $opts{ban} );
1222  
1223 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
1224 1 $self->{frontend_obj}->{test_data} = \@requests;
1225 } else {
1226 0 local $@;
1227 eval {
1228 0 foreach my $req (@requests) {
1229 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1230 }
1231 0 1;
1232 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"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
1233 0 $self->{error} = 33;
1234 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
1235 0 $self->warn;
1236 0 return;
1237 };
1238 } ## end else [ if ( $self->{testing} ) ]
1239  
1240 1 delete( $self->{banned_cidr}{ $opts{ban} } );
1241 } ## end sub unban_cidr
1242  
1243 =head2 list_cidr
1244  
1245 List banned CIDR ranges. Returns an array of the currently banned CIDRs from
1246 internal state; the FortiGate is not queried. Single IPs are not included;
1247 for those see L</list>.
1248  
1249 my @banned_cidrs = $backend->list_cidr;
1250  
1251 =cut
1252  
1253 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
1254 2 my ( $self, %opts ) = @_;
1255  
1256 2 $self->errorblank;
1257  
1258 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
1259 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
1260 }
1261  
1262 2 return keys( %{ $self->{banned_cidr} } );
1263 }
1264  
1265 =head2 list
1266  
1267 List banned IPs. Returns an array of the currently banned single IPs from
1268 internal state; the FortiGate is not queried. CIDR ranges are not included;
1269 for those see L</list_cidr>.
1270  
1271 my @banned = $backend->list;
1272  
1273 =cut
1274  
1275 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
1276 0 my ( $self, %opts ) = @_;
1277  
1278 0 $self->errorblank;
1279  
1280 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1281 0 $self->{frontend_obj}->{test_data} = 'list';
1282 }
1283  
1284 0 return keys( %{ $self->{banned} } );
1285 }
1286  
1287 =head2 re_init
1288  
1289 Tears down and re-inits, then re-adds all previously added bans. Teardown is
1290 best effort, as a partially or fully wiped setup is what re_init recovers
1291 from. Each retained IP and CIDR ban is then re-applied by recreating its
1292 address object and group membership.
1293  
1294 =cut
1295  
1296 0 ▶sub re_init {
Subroutine re_init: not called CC=10 SCAR=47.0
Pod documented
1297 0 my ( $self, %opts ) = @_;
1298  
1299 0 $self->errorblank;
1300  
1301 # teardown is best effort here as a partially or fully wiped setup is
1302 # exactly what re_init needs to recover from
1303 {
1304 0 local $@;
1305 0 eval { $self->teardown; };
1306 }
1307 0 $self->init;
1308  
1309 0 my @re_init_test_data;
1310 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
1311 0 my @requests = $self->_ban_requests($item);
1312  
1313 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1314 0 push( @re_init_test_data, @requests );
1315 } else {
1316 0 local $@;
1317 eval {
1318 0 foreach my $req (@requests) {
1319 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1320 }
1321 0 1;
1322 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
1323 0 $self->{error} = 13;
1324 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
1325 0 $self->warn;
1326 };
1327 } ## end else [ if ( $self->{testing} ) ]
1328 } ## end foreach my $item ( keys( %{ ...}))
1329  
1330 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
1331 0 my @requests = $self->_ban_cidr_requests($item);
1332  
1333 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1334 0 push( @re_init_test_data, @requests );
1335 } else {
1336 0 local $@;
1337 eval {
1338 0 foreach my $req (@requests) {
1339 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1340 }
1341 0 1;
1342 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
1343 0 $self->{error} = 13;
1344 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
1345 0 $self->warn;
1346 };
1347 } ## end else [ if ( $self->{testing} ) ]
1348 } ## end foreach my $item ( keys( %{ ...}))
1349  
1350 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1351 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
1352 }
1353  
1354 0 $self->{inited} = 1;
1355 } ## end sub re_init
1356  
1357 =head2 teardown
1358  
1359 Tears down the setup by removing the group membership and address object for
1360 each currently banned IP and CIDR. The internal list of bans is kept, so a
1361 following re_init will re-add them.
1362  
1363 =cut
1364  
1365 0 ▶sub teardown {
Subroutine teardown: not called CC=7 SCAR=40.3
Pod documented
1366 0 my ( $self, %opts ) = @_;
1367  
1368 0 $self->errorblank;
1369  
1370 0 $self->{inited} = 0;
1371  
1372 0 my @requests;
1373 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ), sort( keys( %{ $self->{banned_cidr} } ) ) ) {
1374 my @item_requests
1375 0 ▶ = $self->{banned_cidr}{$item} ? $self->_unban_cidr_requests($item) : $self->_unban_requests($item);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'banned_cidr'}{$item} ? : 0 0 0
1376 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1377 0 push( @requests, @item_requests );
1378 } else {
1379 0 local $@;
1380 eval {
1381 0 foreach my $req (@item_requests) {
1382 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1383 }
1384 0 1;
1385 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $req (@item_requests) { $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
1386 0 $self->{error} = 17;
1387 0 $self->{errorString} = 'teardown failed removing "' . $item . '"... ' . $@;
1388 0 $self->warn;
1389 };
1390 } ## end else [ if ( $self->{testing} ) ]
1391 } ## end foreach my $item ( sort( keys...))
1392  
1393 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1394 0 $self->{frontend_obj}->{test_data} = \@requests;
1395 }
1396 } ## end sub teardown
1397  
1398 =head2 stop
1399  
1400 Alias for L</teardown>.
1401  
1402 =cut
1403  
1404 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
1405 0 my ( $self, %opts ) = @_;
1406  
1407 0 return $self->teardown(%opts);
1408 }
1409  
1410 =head2 check
1411  
1412 Verifies the endpoint and token are still usable by fetching the IPv4 address
1413 group. Returns a true value if so and a false value otherwise.
1414  
1415 =cut
1416  
1417 6 ▶sub check {
Subroutine check: called CC=3 SCAR=14.7
Pod documented
1418 6 my ( $self, %opts ) = @_;
1419  
1420 6 $self->errorblank;
1421  
1422 6 my $url = $self->_cmdb_url( 'firewall/addrgrp/' . $self->_uri_escape( $self->{options}{group4} ) );
1423  
1424 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
1425 6 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
1426 6 return 1;
1427 }
1428  
1429 0 local $@;
1430 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or return 0 0 0 0
1431  
1432 0 return 1;
1433 } ## end sub check
1434  
1435 =head2 flush
1436  
1437 Removes all currently banned IPs and CIDRs at once by removing their group
1438 membership and address objects and forgetting them.
1439  
1440 =cut
1441  
1442 0 ▶sub flush {
Subroutine flush: not called CC=8 SCAR=42.8
Pod documented
1443 0 my ( $self, %opts ) = @_;
1444  
1445 0 $self->errorblank;
1446  
1447 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1448 0 $self->{error} = 1;
1449 0 $self->{errorString} = 'backend has not been inited';
1450 0 $self->warn;
1451 0 return;
1452 }
1453  
1454 0 my @requests;
1455 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ), sort( keys( %{ $self->{banned_cidr} } ) ) ) {
1456 my @item_requests
1457 0 ▶ = $self->{banned_cidr}{$item} ? $self->_unban_cidr_requests($item) : $self->_unban_requests($item);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'banned_cidr'}{$item} ? : 0 0 0
1458 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1459 0 push( @requests, @item_requests );
1460 } else {
1461 0 local $@;
1462 eval {
1463 0 foreach my $req (@item_requests) {
1464 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1465 }
1466 0 1;
1467 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $req (@item_requests) { $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
1468 0 $self->{error} = 25;
1469 0 $self->{errorString} = 'flush failed removing "' . $item . '"... ' . $@;
1470 0 $self->warn;
1471 };
1472 } ## end else [ if ( $self->{testing} ) ]
1473 } ## end foreach my $item ( sort( keys...))
1474  
1475 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1476 0 $self->{frontend_obj}->{test_data} = \@requests;
1477 }
1478  
1479 0 $self->{banned} = {};
1480 0 $self->{banned_cidr} = {};
1481 } ## end sub flush
1482  
1483 =head1 ERROR CODES / FLAGS
1484  
1485 Error handling is provided by L<Error::Helper>. All
1486 errors are considered fatal.
1487  
1488 =head2 1, notInited
1489  
1490 The backend has not been inited yet.
1491  
1492 =head2 6, invalidPrefixSpecified
1493  
1494 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1495  
1496 =head2 7, invalidName
1497  
1498 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1499  
1500 =head2 8, optionsNotHash
1501  
1502 The item passed to new for options is not a hash.
1503  
1504 =head2 9, noBanItem
1505  
1506 No IP or CIDR range specified to ban or unban.
1507  
1508 =head2 10, banItemNotIP
1509  
1510 The item to ban is not an IP. Either wrong ref type or regexp
1511 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1512  
1513 =head2 12, backendInitError
1514  
1515 Failed to init the backend.
1516  
1517 =head2 13, banFailed
1518  
1519 Failed to ban the item.
1520  
1521 =head2 14, unbanFailed
1522  
1523 Failed to unban the item.
1524  
1525 =head2 15, listFailed
1526  
1527 Failed to get a list of bans.
1528  
1529 =head2 16, reInitFailed
1530  
1531 Failed to re_init the backend.
1532  
1533 =head2 17, teardownFailed
1534  
1535 Failed to teardown the backend.
1536  
1537 =head2 18, alreadyInited
1538  
1539 init called, but the backend has already been inited.
1540  
1541 =head2 23, initFailed
1542  
1543 Init failed. Probing the address group failed.
1544  
1545 =head2 24, checkFailed
1546  
1547 The backend check raised an error.
1548  
1549 =head2 25, flushFailed
1550  
1551 Failed to flush the bans.
1552  
1553 =head2 26, portsNotSupported
1554  
1555 The fortigate backend blocks whole IPs and does not support ports.
1556  
1557 =head2 27, protocolsNotSupported
1558  
1559 The fortigate backend blocks whole IPs and does not support protocols.
1560  
1561 =head2 30, hostNotDefined
1562  
1563 The option host is undef or blank.
1564  
1565 =head2 31, tokenNotDefined
1566  
1567 The option token is undef or blank.
1568  
1569 =head2 32, banCidrFailed
1570  
1571 Failed to ban the CIDR range.
1572  
1573 =head2 33, unbanCidrFailed
1574  
1575 Failed to unban the CIDR range.
1576  
1577 =head2 34, cidrItemNotCidr
1578  
1579 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1580 IPv4 or IPv6 address followed by a prefix length valid for its family.
1581  
1582 =head2 35, cidrNotSupported
1583  
1584 The backend does not support CIDR bans.
1585  
1586 =head2 36, listCidrFailed
1587  
1588 Failed to get a list of CIDR bans.
1589  
1590 =head1 AUTHOR
1591  
1592 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1593  
1594 =head1 LICENSE AND COPYRIGHT
1595  
1596 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1597  
1598 This is free software, licensed under:
1599  
1600 The GNU Lesser General Public License, Version 2.1, February 1999
1601  
1602 =cut
1603  
1604 1 1; # End of Net::Firewall::BlockerHelper::backends::fortigate
« BlockerHelper/backends/cisco_fmc.pm ↑ Summary BlockerHelper/backends/netscaler.pm »