BlockerHelper/backends/fortigate.pm

filter: Statementstmt 38.1% 170 / 446 Branchbran 37.1% 69 / 186 Conditioncond 38.1% 16 / 42 MC/DCmcdc 11.5% 3 / 26 Subroutinesub 85.0% 34 / 40 Podpod 100% 13 / 13 totaltotal 40.5% 305 / 753 CC 120 SCAR 38.6
CC
120
Cov
38%
re_init
132.0
new
86.5
_request
72.0
CRAP 47.6
« BlockerHelper/backends/checkpoint.pm ↑ Summary BlockerHelper/backends/cisco_fmc.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 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::fortigate - Fortinet FortiGate backend via the REST API.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 1 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => '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 sub new {
106 4 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=26 SCAR=44.6
Pod documented
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. True if the IP is IPv4.
247 sub _is_v4 {
248 15 ▶ my ( $self, $ip ) = @_;
Subroutine _is_v4: called CC=2 SCAR=6.9
249  
250 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
251 }
252  
253 # Internal helper. Returns the firewall address object name for the IP, a
254 # deterministic mangling of the prefix, name, and IP that avoids characters
255 # FortiOS disallows in object names.
256 sub _addr_name {
257 8 ▶ my ( $self, $ip ) = @_;
Subroutine _addr_name: called CC=1 SCAR=0.0
258  
259 8 my $name = $self->{prefix} . '_' . $self->{name} . '_' . $ip;
260 # dots and colons are not valid in FortiOS object names
261 8 $name =~ s/[.:]/-/g;
262  
263 8 return $name;
264 } ## end sub _addr_name
265  
266 # Internal helper. Returns the address group name for the IP's family.
267 sub _group_name {
268 4 ▶ my ( $self, $ip ) = @_;
Subroutine _group_name: called CC=2 SCAR=6.9
269  
270 4 ▶ return $self->_is_v4($ip) ? $self->{options}{group4} : $self->{options}{group6};
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_v4($ip) ? : 3 1 4
271 }
272  
273 # Internal helper. Builds a cmdb REST URL for the passed path, appending the
274 # vdom query parameter when configured.
275 sub _cmdb_url {
276 20 ▶ my ( $self, $path ) = @_;
Subroutine _cmdb_url: called CC=3 SCAR=11.0
277  
278 20 my $url = $self->{options}{scheme} . '://' . $self->{options}{host} . '/api/v2/cmdb/' . $path;
279  
280 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
281 16 $url .= '?vdom=' . $self->_uri_escape( $self->{options}{vdom} );
282 }
283  
284 20 return $url;
285 } ## end sub _cmdb_url
286  
287 # Internal helper. Returns the firewall address menu for the IP's family.
288 sub _address_path {
289 4 ▶ my ( $self, $ip ) = @_;
Subroutine _address_path: called CC=2 SCAR=6.9
290  
291 4 ▶ return $self->_is_v4($ip) ? 'firewall/address' : 'firewall/address6';
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_v4($ip) ? : 3 1 4
292 }
293  
294 # Internal helper. Returns the firewall address group menu for the IP's family.
295 sub _addrgrp_path {
296 4 ▶ my ( $self, $ip ) = @_;
Subroutine _addrgrp_path: called CC=2 SCAR=6.9
297  
298 4 ▶ return $self->_is_v4($ip) ? 'firewall/addrgrp' : 'firewall/addrgrp6';
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_v4($ip) ? : 3 1 4
299 }
300  
301 # Internal helper. Minimal percent encoder so URI::Escape is not needed.
302 sub _uri_escape {
303 34 ▶ my ( $self, $string ) = @_;
Subroutine _uri_escape: called CC=1 SCAR=0.2
304  
305 34 $string =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
306  
307 34 return $string;
308 }
309  
310 # Internal helper. Returns a canonical JSON::PP encoder/decoder.
311 sub _json {
312 8 ▶ my ($self) = @_;
Subroutine _json: called CC=1 SCAR=0.0
313  
314 8 require JSON::PP;
315 8 return JSON::PP->new->canonical->utf8;
316 }
317  
318 # Internal helper. Builds the JSON body creating the firewall address object
319 # for the IP, a single host in the family appropriate field.
320 sub _address_body {
321 3 ▶ my ( $self, $ip ) = @_;
Subroutine _address_body: called CC=2 SCAR=6.9
322  
323 3 ▶ if ( $self->_is_v4($ip) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_v4($ip)) 2 1 3
324 2 return $self->_json->encode( { name => $self->_addr_name($ip), subnet => $ip . '/32' } );
325 }
326  
327 1 return $self->_json->encode( { name => $self->_addr_name($ip), ip6 => $ip . '/128' } );
328 } ## end sub _address_body
329  
330 # Internal helper. Performs a HTTP request via LWP::UserAgent using bearer token
331 # auth, returning the decoded JSON body (or undef for an empty body) and dying
332 # with an explanation on any HTTP level failure. Never called in testing mode.
333 sub _request {
334 0 ▶ my ( $self, $method, $url, $body ) = @_;
Subroutine _request: not called CC=8 SCAR=42.8
335  
336 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
337 0 local $@;
338 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
339 0 require LWP::UserAgent;
340 my %args = (
341 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
342 timeout => $self->{options}{timeout},
343 0 );
344 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
345 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
346 }
347 0 $self->{ua} = LWP::UserAgent->new(%args);
348 0 1;
349 } or die( 'failed to load LWP::UserAgent, which the fortigate backend requires... ' . $@ );
350 }
351  
352 my @headers = (
353 'Content-Type' => 'application/json',
354 'Authorization' => 'Bearer ' . $self->{options}{token},
355 0 );
356  
357 0 require HTTP::Request;
358 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
359  
360 0 my $response = $self->{ua}->request($request);
361  
362 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
363 0 my $detail = $response->decoded_content;
364 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
365 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
366 }
367  
368 0 my $decoded;
369 0 my $content = $response->decoded_content;
370 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
371 0 local $@;
372 0 eval { $decoded = $self->_json->decode($content); };
373 }
374  
375 0 return $decoded;
376 } ## end sub _request
377  
378 # Internal helper. Returns the two request descriptors used to ban an IP:
379 # create the address object, then add it to the group.
380 sub _ban_requests {
381 3 ▶ my ( $self, $ip ) = @_;
Subroutine _ban_requests: called CC=1 SCAR=0.0
382  
383 3 my $member = $self->_json->encode( { name => $self->_addr_name($ip) } );
384  
385 return (
386 3 { method => 'POST', url => $self->_cmdb_url( $self->_address_path($ip) ), content => $self->_address_body($ip) },
387 {
388 method => 'POST',
389 url => $self->_cmdb_url( $self->_addrgrp_path($ip) . '/' . $self->_uri_escape( $self->_group_name($ip) ) . '/member' ),
390 content => $member,
391 },
392 );
393 } ## end sub _ban_requests
394  
395 # Internal helper. Returns the two request descriptors used to unban an IP:
396 # remove it from the group, then delete the address object.
397 sub _unban_requests {
398 1 ▶ my ( $self, $ip ) = @_;
Subroutine _unban_requests: called CC=1 SCAR=0.0
399  
400 return (
401 {
402 1 method => 'DELETE',
403 url => $self->_cmdb_url(
404 $self->_addrgrp_path($ip) . '/'
405 . $self->_uri_escape( $self->_group_name($ip) )
406 . '/member/'
407 . $self->_uri_escape( $self->_addr_name($ip) )
408 ),
409 },
410 { method => 'DELETE', url => $self->_cmdb_url( $self->_address_path($ip) . '/' . $self->_uri_escape( $self->_addr_name($ip) ) ) },
411 );
412 } ## end sub _unban_requests
413  
414 # Internal helper. True if the CIDR's address part is IPv4.
415 sub _cidr_is_v4 {
416 7 ▶ my ( $self, $cidr ) = @_;
Subroutine _cidr_is_v4: called CC=2 SCAR=7.0
417  
418 7 my $addr = $cidr;
419 7 $addr =~ s!/[0-9]+\z!!;
420  
421 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
422 } ## end sub _cidr_is_v4
423  
424 # Internal helper. Returns the address group name for the CIDR's family.
425 sub _cidr_group_name {
426 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _cidr_group_name: called CC=2 SCAR=7.2
427  
428 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
429 }
430  
431 # Internal helper. Returns the firewall address menu for the CIDR's family.
432 sub _cidr_address_path {
433 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _cidr_address_path: called CC=2 SCAR=7.2
434  
435 2 ▶ return $self->_cidr_is_v4($cidr) ? 'firewall/address' : 'firewall/address6';
Branch: 1 branch
BranchTrueFalseTotal
$self->_cidr_is_v4($cidr) ? : 2 0 2
436 }
437  
438 # Internal helper. Returns the firewall address group menu for the CIDR's family.
439 sub _cidr_addrgrp_path {
440 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _cidr_addrgrp_path: called CC=2 SCAR=7.2
441  
442 2 ▶ return $self->_cidr_is_v4($cidr) ? 'firewall/addrgrp' : 'firewall/addrgrp6';
Branch: 1 branch
BranchTrueFalseTotal
$self->_cidr_is_v4($cidr) ? : 2 0 2
443 }
444  
445 # Internal helper. Returns the firewall address object name for the CIDR, a
446 # deterministic mangling of the prefix, name, and CIDR that avoids characters
447 # FortiOS disallows in object names.
448 sub _cidr_addr_name {
449 4 ▶ my ( $self, $cidr ) = @_;
Subroutine _cidr_addr_name: called CC=1 SCAR=0.0
450  
451 4 my $name = $self->{prefix} . '_' . $self->{name} . '_' . $cidr;
452 # dots, colons, and slashes are not valid in FortiOS object names
453 4 $name =~ s/[.:\/]/-/g;
454  
455 4 return $name;
456 } ## end sub _cidr_addr_name
457  
458 # Internal helper. Builds the JSON body creating the firewall address object for
459 # the CIDR, a subnet in the family appropriate field.
460 sub _cidr_address_body {
461 1 ▶ my ( $self, $cidr ) = @_;
Subroutine _cidr_address_body: called CC=2 SCAR=7.6
462  
463 1 ▶ if ( $self->_cidr_is_v4($cidr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cidr_is_v4($cidr)) 1 0 1
464 1 return $self->_json->encode( { name => $self->_cidr_addr_name($cidr), subnet => $cidr } );
465 }
466  
467 0 return $self->_json->encode( { name => $self->_cidr_addr_name($cidr), ip6 => $cidr } );
468 } ## end sub _cidr_address_body
469  
470 # Internal helper. Returns the two request descriptors used to ban a CIDR:
471 # create the address object, then add it to the group.
472 sub _ban_cidr_requests {
473 1 ▶ my ( $self, $cidr ) = @_;
Subroutine _ban_cidr_requests: called CC=1 SCAR=0.0
474  
475 1 my $member = $self->_json->encode( { name => $self->_cidr_addr_name($cidr) } );
476  
477 return (
478 {
479 1 method => 'POST',
480 url => $self->_cmdb_url( $self->_cidr_address_path($cidr) ),
481 content => $self->_cidr_address_body($cidr),
482 },
483 {
484 method => 'POST',
485 url => $self->_cmdb_url(
486 $self->_cidr_addrgrp_path($cidr) . '/' . $self->_uri_escape( $self->_cidr_group_name($cidr) ) . '/member'
487 ),
488 content => $member,
489 },
490 );
491 } ## end sub _ban_cidr_requests
492  
493 # Internal helper. Returns the two request descriptors used to unban a CIDR:
494 # remove it from the group, then delete the address object.
495 sub _unban_cidr_requests {
496 1 ▶ my ( $self, $cidr ) = @_;
Subroutine _unban_cidr_requests: called CC=1 SCAR=0.0
497  
498 return (
499 {
500 1 method => 'DELETE',
501 url => $self->_cmdb_url(
502 $self->_cidr_addrgrp_path($cidr) . '/'
503 . $self->_uri_escape( $self->_cidr_group_name($cidr) )
504 . '/member/'
505 . $self->_uri_escape( $self->_cidr_addr_name($cidr) )
506 ),
507 },
508 {
509 method => 'DELETE',
510 url => $self->_cmdb_url(
511 $self->_cidr_address_path($cidr) . '/' . $self->_uri_escape( $self->_cidr_addr_name($cidr) )
512 ),
513 },
514 );
515 } ## end sub _unban_cidr_requests
516  
517 =head2 init
518  
519 Initiates the backend. Verifies the token and reachability by fetching the
520 IPv4 address group.
521  
522 =cut
523  
524 sub init {
525 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=20.3
Pod documented
526  
527 2 $self->errorblank;
528  
529 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
530 0 $self->{error} = 18;
531 0 $self->{errorString} = 'backend has already been inited';
532 0 $self->warn;
533 }
534  
535 2 my $url = $self->_cmdb_url( 'firewall/addrgrp/' . $self->_uri_escape( $self->{options}{group4} ) );
536  
537 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
538 2 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
539 } else {
540 0 local $@;
541 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
542 0 $self->{error} = 23;
543 0 $self->{errorString} = 'init failed. probing the address group failed... ' . $@;
544 0 $self->warn;
545 };
546 }
547  
548 2 $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
554 and lowercased. A firewall address object named
555 C<E<lt>prefixE<gt>_E<lt>nameE<gt>_E<lt>ipE<gt>> (dots and colons replaced
556 with dashes) is created via a POST to C<firewall/address> (IPv4) or
557 C<firewall/address6> (IPv6) and then added as a member of the family's
558 address group via a POST to the group's C<member> endpoint. Banning an
559 already banned IP is a noop.
560  
561 $backend->ban(ban => $ip);
562  
563 =cut
564  
565 sub ban {
566 3 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=11 SCAR=39.1
Pod documented
567  
568 3 $self->errorblank;
569  
570 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
571 0 $self->{error} = 1;
572 0 $self->{errorString} = 'backend has not been inited';
573 0 $self->warn;
574 0 return;
575 }
576  
577 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
578 0 $self->{error} = 9;
579 0 $self->{errorString} = 'Nothing specified for the value ban';
580 0 $self->warn;
581 0 return;
582 } elsif ( ref( $opts{ban} ) ne '' ) {
583 0 $self->{error} = 10;
584 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
585 0 $self->warn;
586 0 return;
587 ▶ } 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
588 && $opts{ban} !~ /\A$IPv6_re\z/ )
589 {
590 0 $self->{error} = 10;
591 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
592 0 $self->warn;
593 0 return;
594 }
595  
596 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
597 3 $opts{ban} = lc( $opts{ban} );
598  
599 3 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 3 3
600 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
601 0 $self->{frontend_obj}->{test_data} = 'already banned';
602 }
603 0 return;
604 }
605  
606 3 my @requests = $self->_ban_requests( $opts{ban} );
607  
608 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
609 3 $self->{frontend_obj}->{test_data} = \@requests;
610 } else {
611 0 local $@;
612 eval {
613 0 foreach my $req (@requests) {
614 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
615 }
616 0 1;
617 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
618 0 $self->{error} = 13;
619 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
620 0 $self->warn;
621 0 return;
622 };
623 } ## end else [ if ( $self->{testing} ) ]
624  
625 3 $self->{banned}{ $opts{ban} } = 1;
626 } ## end sub ban
627  
628 =head2 unban
629  
630 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
631 and lowercased. The IP's address object is removed from the family's address
632 group via a DELETE on the group's C<member> endpoint and then the address
633 object itself is deleted via a DELETE on C<firewall/address> (IPv4) or
634 C<firewall/address6> (IPv6). Unbanning an IP that is not banned is a noop.
635  
636 $backend->unban(ban => $ip);
637  
638 =cut
639  
640 sub unban {
641 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=11 SCAR=39.7
Pod documented
642  
643 1 $self->errorblank;
644  
645 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
646 0 $self->{error} = 1;
647 0 $self->{errorString} = 'backend has not been inited';
648 0 $self->warn;
649 0 return;
650 }
651  
652 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
653 0 $self->{error} = 9;
654 0 $self->{errorString} = 'Nothing specified for the value ban';
655 0 $self->warn;
656 0 return;
657 } elsif ( ref( $opts{ban} ) ne '' ) {
658 0 $self->{error} = 10;
659 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
660 0 $self->warn;
661 0 return;
662 ▶ } 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
663 && $opts{ban} !~ /\A$IPv6_re\z/ )
664 {
665 0 $self->{error} = 10;
666 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
667 0 $self->warn;
668 0 return;
669 }
670  
671 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
672 1 $opts{ban} = lc( $opts{ban} );
673  
674 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
675 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
676 0 $self->{frontend_obj}->{test_data} = 'not banned';
677 }
678 0 return;
679 }
680  
681 1 my @requests = $self->_unban_requests( $opts{ban} );
682  
683 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
684 1 $self->{frontend_obj}->{test_data} = \@requests;
685 } else {
686 0 local $@;
687 eval {
688 0 foreach my $req (@requests) {
689 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
690 }
691 0 1;
692 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
693 0 $self->{error} = 14;
694 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
695 0 $self->warn;
696 0 return;
697 };
698 } ## end else [ if ( $self->{testing} ) ]
699  
700 1 delete( $self->{banned}{ $opts{ban} } );
701 } ## end sub unban
702  
703 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
704 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
705 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
706 # IPv6). Returns false otherwise.
707 sub _valid_cidr {
708 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
709  
710 2 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 2 2
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table33%1/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
711  
712 2 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 2 0 2
713 2 my ( $addr, $prefix ) = ( $1, $2 );
714 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
715 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::fortigate::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
716 }
717  
718 0 return 0;
719 } ## end sub _valid_cidr
720  
721 =head2 ban_cidr
722  
723 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
724 CIDR and lowercased. A subnet firewall address object named
725 C<E<lt>prefixE<gt>_E<lt>nameE<gt>_E<lt>cidrE<gt>> (dots, colons, and slashes
726 replaced with dashes) is created via a POST to C<firewall/address> (IPv4) or
727 C<firewall/address6> (IPv6) and then added as a member of the family's
728 address group. Banning an already banned CIDR is a noop.
729  
730 $backend->ban_cidr(ban => '1.2.3.0/24');
731  
732 =cut
733  
734 sub ban_cidr {
735 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=10 SCAR=38.0
Pod documented
736  
737 1 $self->errorblank;
738  
739 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
740 0 $self->{error} = 1;
741 0 $self->{errorString} = 'backend has not been inited';
742 0 $self->warn;
743 0 return;
744 }
745  
746 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
747 0 $self->{error} = 9;
748 0 $self->{errorString} = 'Nothing specified for the value ban';
749 0 $self->warn;
750 0 return;
751 } elsif ( ref( $opts{ban} ) ne '' ) {
752 0 $self->{error} = 34;
753 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
754 0 $self->warn;
755 0 return;
756 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
757 0 $self->{error} = 34;
758 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
759 0 $self->warn;
760 0 return;
761 }
762  
763 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
764 1 $opts{ban} = lc( $opts{ban} );
765  
766 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
767 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
768 0 $self->{frontend_obj}->{test_data} = 'already banned';
769 }
770 0 return;
771 }
772  
773 1 my @requests = $self->_ban_cidr_requests( $opts{ban} );
774  
775 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
776 1 $self->{frontend_obj}->{test_data} = \@requests;
777 } else {
778 0 local $@;
779 eval {
780 0 foreach my $req (@requests) {
781 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
782 }
783 0 1;
784 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
785 0 $self->{error} = 32;
786 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
787 0 $self->warn;
788 0 return;
789 };
790 } ## end else [ if ( $self->{testing} ) ]
791  
792 1 $self->{banned_cidr}{ $opts{ban} } = 1;
793 } ## end sub ban_cidr
794  
795 =head2 unban_cidr
796  
797 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
798 CIDR and lowercased. The CIDR's address object is removed from the family's
799 address group via a DELETE on the group's C<member> endpoint and then the
800 address object itself is deleted. Unbanning a CIDR that is not banned is a
801 noop.
802  
803 $backend->unban_cidr(ban => '1.2.3.0/24');
804  
805 =cut
806  
807 sub unban_cidr {
808 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=10 SCAR=38.0
Pod documented
809  
810 1 $self->errorblank;
811  
812 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
813 0 $self->{error} = 1;
814 0 $self->{errorString} = 'backend has not been inited';
815 0 $self->warn;
816 0 return;
817 }
818  
819 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
820 0 $self->{error} = 9;
821 0 $self->{errorString} = 'Nothing specified for the value ban';
822 0 $self->warn;
823 0 return;
824 } elsif ( ref( $opts{ban} ) ne '' ) {
825 0 $self->{error} = 34;
826 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
827 0 $self->warn;
828 0 return;
829 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
830 0 $self->{error} = 34;
831 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
832 0 $self->warn;
833 0 return;
834 }
835  
836 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
837 1 $opts{ban} = lc( $opts{ban} );
838  
839 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
840 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
841 0 $self->{frontend_obj}->{test_data} = 'not banned';
842 }
843 0 return;
844 }
845  
846 1 my @requests = $self->_unban_cidr_requests( $opts{ban} );
847  
848 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
849 1 $self->{frontend_obj}->{test_data} = \@requests;
850 } else {
851 0 local $@;
852 eval {
853 0 foreach my $req (@requests) {
854 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
855 }
856 0 1;
857 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
858 0 $self->{error} = 33;
859 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
860 0 $self->warn;
861 0 return;
862 };
863 } ## end else [ if ( $self->{testing} ) ]
864  
865 1 delete( $self->{banned_cidr}{ $opts{ban} } );
866 } ## end sub unban_cidr
867  
868 =head2 list_cidr
869  
870 List banned CIDR ranges. Returns an array of the currently banned CIDRs from
871 internal state; the FortiGate is not queried. Single IPs are not included;
872 for those see L</list>.
873  
874 my @banned_cidrs = $backend->list_cidr;
875  
876 =cut
877  
878 sub list_cidr {
879 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
880  
881 2 $self->errorblank;
882  
883 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
884 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
885 }
886  
887 2 return keys( %{ $self->{banned_cidr} } );
888 }
889  
890 =head2 list
891  
892 List banned IPs. Returns an array of the currently banned single IPs from
893 internal state; the FortiGate is not queried. CIDR ranges are not included;
894 for those see L</list_cidr>.
895  
896 my @banned = $backend->list;
897  
898 =cut
899  
900 sub list {
901 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
902  
903 0 $self->errorblank;
904  
905 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
906 0 $self->{frontend_obj}->{test_data} = 'list';
907 }
908  
909 0 return keys( %{ $self->{banned} } );
910 }
911  
912 =head2 re_init
913  
914 Tears down and re-inits, then re-adds all previously added bans. Teardown is
915 best effort, as a partially or fully wiped setup is what re_init recovers
916 from. Each retained IP and CIDR ban is then re-applied by recreating its
917 address object and group membership.
918  
919 =cut
920  
921 sub re_init {
922 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=11 SCAR=48.8
Pod documented
923  
924 0 $self->errorblank;
925  
926 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
927 0 $self->{error} = 1;
928 0 $self->{errorString} = 'backend has not been inited';
929 0 $self->warn;
930 0 return;
931 }
932  
933 # teardown is best effort here as a partially or fully wiped setup is
934 # exactly what re_init needs to recover from
935 {
936 0 local $@;
937 0 eval { $self->teardown; };
938 }
939 0 $self->init;
940  
941 0 my @re_init_test_data;
942 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
943 0 my @requests = $self->_ban_requests($item);
944  
945 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
946 0 push( @re_init_test_data, @requests );
947 } else {
948 0 local $@;
949 eval {
950 0 foreach my $req (@requests) {
951 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
952 }
953 0 1;
954 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
955 0 $self->{error} = 13;
956 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
957 0 $self->warn;
958 };
959 } ## end else [ if ( $self->{testing} ) ]
960 } ## end foreach my $item ( keys( %{ ...}))
961  
962 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
963 0 my @requests = $self->_ban_cidr_requests($item);
964  
965 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
966 0 push( @re_init_test_data, @requests );
967 } else {
968 0 local $@;
969 eval {
970 0 foreach my $req (@requests) {
971 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
972 }
973 0 1;
974 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
975 0 $self->{error} = 13;
976 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
977 0 $self->warn;
978 };
979 } ## end else [ if ( $self->{testing} ) ]
980 } ## end foreach my $item ( keys( %{ ...}))
981  
982 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
983 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
984 }
985  
986 0 $self->{inited} = 1;
987 } ## end sub re_init
988  
989 =head2 teardown
990  
991 Tears down the setup by removing the group membership and address object for
992 each currently banned IP and CIDR. The internal list of bans is kept, so a
993 following re_init will re-add them.
994  
995 =cut
996  
997 sub teardown {
998 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=7 SCAR=40.3
Pod documented
999  
1000 0 $self->errorblank;
1001  
1002 0 $self->{inited} = 0;
1003  
1004 0 my @requests;
1005 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ), sort( keys( %{ $self->{banned_cidr} } ) ) ) {
1006 my @item_requests
1007 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
1008 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1009 0 push( @requests, @item_requests );
1010 } else {
1011 0 local $@;
1012 eval {
1013 0 foreach my $req (@item_requests) {
1014 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1015 }
1016 0 1;
1017 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
1018 0 $self->{error} = 17;
1019 0 $self->{errorString} = 'teardown failed removing "' . $item . '"... ' . $@;
1020 0 $self->warn;
1021 };
1022 } ## end else [ if ( $self->{testing} ) ]
1023 } ## end foreach my $item ( sort( keys...))
1024  
1025 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1026 0 $self->{frontend_obj}->{test_data} = \@requests;
1027 }
1028 } ## end sub teardown
1029  
1030 =head2 stop
1031  
1032 Alias for L</teardown>.
1033  
1034 =cut
1035  
1036 sub stop {
1037 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
1038  
1039 0 return $self->teardown(%opts);
1040 }
1041  
1042 =head2 check
1043  
1044 Verifies the endpoint and token are still usable by fetching the IPv4 address
1045 group. Returns a true value if so and a false value otherwise.
1046  
1047 =cut
1048  
1049 sub check {
1050 6 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=14.7
Pod documented
1051  
1052 6 $self->errorblank;
1053  
1054 6 my $url = $self->_cmdb_url( 'firewall/addrgrp/' . $self->_uri_escape( $self->{options}{group4} ) );
1055  
1056 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
1057 6 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
1058 6 return 1;
1059 }
1060  
1061 0 local $@;
1062 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
1063  
1064 0 return 1;
1065 } ## end sub check
1066  
1067 =head2 flush
1068  
1069 Removes all currently banned IPs and CIDRs at once by removing their group
1070 membership and address objects and forgetting them.
1071  
1072 =cut
1073  
1074 sub flush {
1075 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=8 SCAR=42.8
Pod documented
1076  
1077 0 $self->errorblank;
1078  
1079 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1080 0 $self->{error} = 1;
1081 0 $self->{errorString} = 'backend has not been inited';
1082 0 $self->warn;
1083 0 return;
1084 }
1085  
1086 0 my @requests;
1087 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ), sort( keys( %{ $self->{banned_cidr} } ) ) ) {
1088 my @item_requests
1089 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
1090 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1091 0 push( @requests, @item_requests );
1092 } else {
1093 0 local $@;
1094 eval {
1095 0 foreach my $req (@item_requests) {
1096 0 $self->_request( $req->{method}, $req->{url}, $req->{content} );
1097 }
1098 0 1;
1099 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
1100 0 $self->{error} = 25;
1101 0 $self->{errorString} = 'flush failed removing "' . $item . '"... ' . $@;
1102 0 $self->warn;
1103 };
1104 } ## end else [ if ( $self->{testing} ) ]
1105 } ## end foreach my $item ( sort( keys...))
1106  
1107 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1108 0 $self->{frontend_obj}->{test_data} = \@requests;
1109 }
1110  
1111 0 $self->{banned} = {};
1112 0 $self->{banned_cidr} = {};
1113 } ## end sub flush
1114  
1115 =head1 ERROR CODES / FLAGS
1116  
1117 Error handling is provided by L<Error::Helper>. All
1118 errors are considered fatal.
1119  
1120 =head2 1, notInited
1121  
1122 The backend has not been inited yet.
1123  
1124 =head2 6, invalidPrefixSpecified
1125  
1126 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1127  
1128 =head2 7, invalidName
1129  
1130 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1131  
1132 =head2 8, optionsNotHash
1133  
1134 The item passed to new for options is not a hash.
1135  
1136 =head2 9, noBanItem
1137  
1138 No IP or CIDR range specified to ban or unban.
1139  
1140 =head2 10, banItemNotIP
1141  
1142 The item to ban is not an IP. Either wrong ref type or regexp
1143 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1144  
1145 =head2 12, backendInitError
1146  
1147 Failed to init the backend.
1148  
1149 =head2 13, banFailed
1150  
1151 Failed to ban the item.
1152  
1153 =head2 14, unbanFailed
1154  
1155 Failed to unban the item.
1156  
1157 =head2 15, listFailed
1158  
1159 Failed to get a list of bans.
1160  
1161 =head2 16, reInitFailed
1162  
1163 Failed to re_init the backend.
1164  
1165 =head2 17, teardownFailed
1166  
1167 Failed to teardown the backend.
1168  
1169 =head2 18, alreadyInited
1170  
1171 init called, but the backend has already been inited.
1172  
1173 =head2 23, initFailed
1174  
1175 Init failed. Probing the address group failed.
1176  
1177 =head2 24, checkFailed
1178  
1179 The backend check raised an error.
1180  
1181 =head2 25, flushFailed
1182  
1183 Failed to flush the bans.
1184  
1185 =head2 26, portsNotSupported
1186  
1187 The fortigate backend blocks whole IPs and does not support ports.
1188  
1189 =head2 27, protocolsNotSupported
1190  
1191 The fortigate backend blocks whole IPs and does not support protocols.
1192  
1193 =head2 30, hostNotDefined
1194  
1195 The option host is undef or blank.
1196  
1197 =head2 31, tokenNotDefined
1198  
1199 The option token is undef or blank.
1200  
1201 =head2 32, banCidrFailed
1202  
1203 Failed to ban the CIDR range.
1204  
1205 =head2 33, unbanCidrFailed
1206  
1207 Failed to unban the CIDR range.
1208  
1209 =head2 34, cidrItemNotCidr
1210  
1211 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1212 IPv4 or IPv6 address followed by a prefix length valid for its family.
1213  
1214 =head2 35, cidrNotSupported
1215  
1216 The backend does not support CIDR bans.
1217  
1218 =head2 36, listCidrFailed
1219  
1220 Failed to get a list of CIDR bans.
1221  
1222 =head1 AUTHOR
1223  
1224 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1225  
1226 =head1 LICENSE AND COPYRIGHT
1227  
1228 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1229  
1230 This is free software, licensed under:
1231  
1232 The GNU Lesser General Public License, Version 2.1, February 1999
1233  
1234 =cut
1235  
1236 1 1; # End of Net::Firewall::BlockerHelper::backends::fortigate
« BlockerHelper/backends/checkpoint.pm ↑ Summary BlockerHelper/backends/cisco_fmc.pm »