BlockerHelper/backends/cisco_fmc.pm

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