BlockerHelper/backends/cisco_fmc.pm

filter: Statementstmt 33.5% 131 / 391 Branchbran 32.6% 51 / 156 Conditioncond 38.4% 15 / 39 MC/DCmcdc 16.6% 4 / 24 Subroutinesub 70.8% 17 / 24 Podpod 100% 13 / 13 totaltotal 35.7% 231 / 647 CC 94 SCAR 38.7
CC
94
Cov
34%
_request
90.0
new
84.7
_generate_token
72.0
CRAP 48.2
« BlockerHelper/backends/checkpoint.pm ↑ Summary BlockerHelper/backends/fortigate.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 qw( Error::Helper Net::Firewall::BlockerHelper::Util );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::cisco_fmc - Cisco Firepower Management Center backend via the REST API.
13  
14 =head1 VERSION
15  
16 Version 0.2.0
17  
18 =cut
19  
20 1 our $VERSION = '0.2.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => '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 5 ▶sub new {
Subroutine new: called CC=29 SCAR=44.4
Pod documented
108 5 my ( $blank, %opts ) = @_;
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 the URL of the FMC authentication endpoint that
265 # issues API tokens.
266 #
267 # FMC does not accept credentials on the calls that do the work. A short lived
268 # token has to be obtained first and then sent on every following request, so
269 # this endpoint is hit once at init and again whenever a token needs
270 # refreshing. Note it lives under the fmc_platform API rather than the
271 # fmc_config one the object calls use.
272 #
273 # Takes no arguments; the host comes from the options.
274 #
275 # Returns the endpoint as a string.
276 #
277 # $self->_token_url;
278 # # https://fmc.example.org/api/fmc_platform/v1/auth/generatetoken
279 1 ▶sub _token_url {
Subroutine _token_url: called CC=1 SCAR=0.0
280 1 my ($self) = @_;
281  
282 1 return 'https://' . $self->{options}{host} . '/api/fmc_platform/v1/auth/generatetoken';
283 }
284  
285 # Internal helper. Returns the URL of the FMC Network Group object this
286 # instance manages.
287 #
288 # This is the single object every ban and unban rewrites; the backend does not
289 # create it, and it is expected to already exist and be referenced by an
290 # access control policy. The URL is built from the domain UUID and the group's
291 # own UUID rather than from names, since that is how the fmc_config API
292 # addresses objects.
293 #
294 # Takes no arguments; the host, domain, and group id all come from the
295 # options.
296 #
297 # Returns the URL as a string, used for both the GET that reads the object and
298 # the PUT that replaces it.
299 #
300 # $self->_group_url;
301 # # https://fmc.example.org/api/fmc_config/v1/domain/e276abec-.../object/networkgroups/0050568A-...
302 10 ▶sub _group_url {
Subroutine _group_url: called CC=1 SCAR=0.0
303 10 my ($self) = @_;
304  
305 return
306 'https://'
307 . $self->{options}{host}
308 . '/api/fmc_config/v1/domain/'
309 . $self->{options}{domain}
310 . '/object/networkgroups/'
311 10 . $self->{options}{group_id};
312 } ## end sub _group_url
313  
314 # Internal helper. Renders the complete Network Group object body from the
315 # current bans, which is what gets PUT back to replace it.
316 #
317 # FMC offers no incremental add or remove for group literals, so every ban and
318 # unban rewrites the whole object. The name and id have to be included in the
319 # body as well as in the URL, since a PUT replaces the object outright and
320 # omitting them would blank them.
321 #
322 # Single addresses and ranges both become literals but with different types,
323 # Host for an address and Network for a range, which is why the two internal
324 # lists are walked separately. They are emitted as two sorted runs rather than
325 # one merged sorted list, so the hosts come first and the networks after. That
326 # is stable, which is what matters for the test_data comparisons, just not
327 # globally sorted.
328 #
329 # Takes no arguments; the bans come from the object's banned and banned_cidr
330 # hashes.
331 #
332 # Returns the object body as a JSON string, ready to be the content of a PUT
333 # to _group_url. With nothing banned the literals array is empty, which is how
334 # teardown and flush clear the group.
335 #
336 # # with 10.0.0.1 and 10.0.0.0/8 banned
337 # $self->_render;
338 # # {"id":"0050568A-...","literals":[{"type":"Host","value":"10.0.0.1"},{"type":"Network","value":"10.0.0.0/8"}],"name":"blocklist"}
339 #
340 # # with nothing banned
341 # # {"id":"0050568A-...","literals":[],"name":"blocklist"}
342 5 ▶sub _render {
Subroutine _render: called CC=3 SCAR=11.0
343 5 my ($self) = @_;
344  
345 5 my @literals;
346 5 foreach my $ip ( sort( keys( %{ $self->{banned} } ) ) ) {
347 6 push( @literals, { type => 'Host', value => $ip } );
348 }
349 5 foreach my $cidr ( sort( keys( %{ $self->{banned_cidr} } ) ) ) {
350 1 push( @literals, { type => 'Network', value => $cidr } );
351 }
352  
353 return $self->_json->encode(
354 {
355 name => $self->{options}{group_name},
356 id => $self->{options}{group_id},
357 5 literals => \@literals,
358 }
359 );
360 } ## end sub _render
361  
362 # Internal helper. Performs one HTTP request against the FMC configuration
363 # API. Every call this backend makes other than the token fetch itself goes
364 # through here.
365 #
366 # Authentication is the token obtained by _generate_token, sent in the
367 # X-auth-access-token header. The header is only added when a token is
368 # actually set, so a request made before authentication simply goes out
369 # unauthenticated and fails at the server rather than dying on an undef here.
370 #
371 # Note there is no automatic token refresh. FMC tokens expire, and when one
372 # does the next call fails like any other error rather than being retried, so
373 # a long lived process relies on the self heal path to re-init and
374 # reauthenticate.
375 #
376 # The user agent is normally already built and cached on the object by
377 # _generate_token, since that runs first; it is built here on first use if
378 # not. LWP::UserAgent is loaded with require rather than at compile time,
379 # since only the HTTP backends need it. The insecure option turns off
380 # certificate verification, which is there because an FMC commonly presents a
381 # self signed certificate.
382 #
383 # Any HTTP level failure dies rather than setting an error. The callers wrap
384 # this in eval and turn the exception into the appropriate error code, which
385 # is what lets one helper serve paths that report ban, unban, and teardown
386 # failures differently.
387 #
388 # A body that fails to decode as JSON is not fatal; the decode runs inside its
389 # own eval and leaves the result undef.
390 #
391 # Never called in testing mode; those paths record the request instead.
392 #
393 # Args:
394 #
395 # method - The HTTP method, as a plain string: 'GET' to read the Network
396 # Group, 'PUT' to replace it.
397 #
398 # url - The full URL to request, as a plain string, normally
399 # _group_url.
400 #
401 # body - Optional request body, as an already encoded JSON string,
402 # normally from _render. undef for a GET.
403 #
404 # Returns the decoded response body as whatever structure the JSON held,
405 # normally a hashref, or undef when the response body was empty or did not
406 # parse. Dies on any non success HTTP status.
407 #
408 # my $decoded = $self->_request( 'GET', $self->_group_url );
409 #
410 # # the usual shape at the call sites
411 # eval { $self->_request( 'PUT', $self->_group_url, $self->_render ); };
412 # if ($@) { ... raise banFailed ... }
413 0 ▶sub _request {
Subroutine _request: not called CC=9 SCAR=45.0
414 0 my ( $self, $method, $url, $body ) = @_;
415  
416 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
417 0 local $@;
418 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
419 0 require LWP::UserAgent;
420 my %args = (
421 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
422 timeout => $self->{options}{timeout},
423 0 );
424 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
425 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
426 }
427 0 $self->{ua} = LWP::UserAgent->new(%args);
428 0 1;
429 } or die( 'failed to load LWP::UserAgent, which the cisco_fmc backend requires... ' . $@ );
430 }
431  
432 0 my @headers = ( 'Content-Type' => 'application/json' );
433 0 ▶ if ( defined( $self->{token} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"token"}) 0 0 0
434 0 push( @headers, 'X-auth-access-token' => $self->{token} );
435 }
436  
437 0 require HTTP::Request;
438 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
439  
440 0 my $response = $self->{ua}->request($request);
441  
442 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
443 0 my $detail = $response->decoded_content;
444 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
445 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
446 }
447  
448 0 my $decoded;
449 0 my $content = $response->decoded_content;
450 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
451 0 local $@;
452 0 eval { $decoded = $self->_json->decode($content); };
453 }
454  
455 0 return $decoded;
456 } ## end sub _request
457  
458 # Internal helper. Authenticates against FMC and stores the resulting API
459 # token on the object for the following requests to use.
460 #
461 # FMC's authentication is unusual in two ways worth knowing. The credentials
462 # go up as HTTP basic auth, but only to this one endpoint, and the token comes
463 # back in a response header rather than in the body, so there is nothing to
464 # parse: the X-auth-access-token header is the whole answer.
465 #
466 # The user agent is built here on first use and cached on the object, since
467 # this normally runs before any other request. LWP::UserAgent is loaded with
468 # require at that point rather than at compile time, since only the HTTP
469 # backends need it; failing to load it dies with an explanation naming this
470 # backend. The insecure option turns off certificate verification, which is
471 # there because an FMC commonly presents a self signed certificate.
472 #
473 # MIME::Base64 is required at call time for the same reason. Note the second
474 # argument to encode_base64 is an empty string, which suppresses the line
475 # wrapping it would otherwise insert and which would corrupt the header.
476 #
477 # A response that succeeds but carries no token is treated as a failure rather
478 # than being allowed through, since storing an empty token would turn one
479 # clear error here into an authentication failure on every following call.
480 #
481 # Dies on any failure rather than setting an error; the callers wrap it in
482 # eval. Never called in testing mode.
483 #
484 # Takes no arguments; the credentials come from the options.
485 #
486 # Returns nothing. The result is the side effect of setting the token
487 # attribute on the object, which _request then sends as X-auth-access-token.
488 #
489 # $self->_generate_token;
490 # # $self->{token} is now set
491 #
492 # # the usual shape at the call sites
493 # eval { $self->_generate_token; };
494 # if ($@) { ... raise backendInitError ... }
495 0 ▶sub _generate_token {
Subroutine _generate_token: not called CC=8 SCAR=42.8
496 0 my ($self) = @_;
497  
498 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
499 0 local $@;
500 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
501 0 require LWP::UserAgent;
502 my %args = (
503 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
504 timeout => $self->{options}{timeout},
505 0 );
506 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
507 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
508 }
509 0 $self->{ua} = LWP::UserAgent->new(%args);
510 0 1;
511 } or die( 'failed to load LWP::UserAgent, which the cisco_fmc backend requires... ' . $@ );
512 }
513  
514 0 require MIME::Base64;
515 0 my $basic = MIME::Base64::encode_base64( $self->{options}{user} . ':' . $self->{options}{password}, '' );
516  
517 0 require HTTP::Request;
518 0 my $request = HTTP::Request->new( 'POST', $self->_token_url, [ 'Authorization' => 'Basic ' . $basic ] );
519  
520 0 my $response = $self->{ua}->request($request);
521  
522 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
523 0 my $detail = $response->decoded_content;
524 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
525 0 die( 'POST ' . $self->_token_url . ' failed... HTTP status... ' . $response->status_line . $detail );
526 }
527  
528 0 my $token = $response->header('X-auth-access-token');
529 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
530 0 die( 'POST ' . $self->_token_url . ' returned no X-auth-access-token header' );
531 }
532  
533 0 $self->{token} = $token;
534  
535 0 return;
536 } ## end sub _generate_token
537  
538 =head2 init
539  
540 Initiates the backend. POSTs the configured user and password via HTTP Basic
541 auth to the token generation endpoint,
542 C</api/fmc_platform/v1/auth/generatetoken>, and stores the returned
543 C<X-auth-access-token> header for use on all subsequent requests. Nothing is
544 done to the Network Group object itself.
545  
546 =cut
547  
548 1 ▶sub init {
Subroutine init: called CC=4 SCAR=20.9
Pod documented
549 1 my ( $self, %opts ) = @_;
550  
551 1 $self->errorblank;
552  
553 1 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 1 1
554 0 $self->{error} = 18;
555 0 $self->{errorString} = 'backend has already been inited';
556 0 $self->warn;
557 }
558  
559 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
560 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_token_url } ];
561 } else {
562 0 local $@;
563 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
564 0 $self->{error} = 23;
565 0 $self->{errorString} = 'init failed. generating an auth token failed... ' . $@;
566 0 $self->warn;
567 };
568 }
569  
570 1 $self->{inited} = 1;
571 } ## end sub init
572  
573 =head2 ban
574  
575 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address and
576 lowercased, then added to the ban list. The full Network Group object,
577 containing every current ban as a C<Host> literal, is rendered and PUT to the
578 networkgroups endpoint. Banning an already banned IP is a noop. If the PUT
579 fails, the IP is removed from the ban list again.
580  
581 $backend->ban(ban => $ip);
582  
583 =cut
584  
585 2 ▶sub ban {
Subroutine ban: called CC=10 SCAR=38.0
Pod documented
586 2 my ( $self, %opts ) = @_;
587  
588 2 $self->errorblank;
589  
590 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
591 0 $self->{error} = 1;
592 0 $self->{errorString} = 'backend has not been inited';
593 0 $self->warn;
594 0 return;
595 }
596  
597 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
598 0 $self->{error} = 9;
599 0 $self->{errorString} = 'Nothing specified for the value ban';
600 0 $self->warn;
601 0 return;
602 } elsif ( ref( $opts{ban} ) ne '' ) {
603 0 $self->{error} = 10;
604 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
605 0 $self->warn;
606 0 return;
607 ▶ } 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
608 && $opts{ban} !~ /\A$IPv6_re\z/ )
609 {
610 0 $self->{error} = 10;
611 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
612 0 $self->warn;
613 0 return;
614 }
615  
616 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
617 2 $opts{ban} = lc( $opts{ban} );
618  
619 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
620 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
621 0 $self->{frontend_obj}->{test_data} = 'already banned';
622 }
623 0 return;
624 }
625  
626 2 $self->{banned}{ $opts{ban} } = 1;
627  
628 2 my $body = $self->_render;
629  
630 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
631 2 $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_group_url, content => $body } ];
632 } else {
633 0 local $@;
634 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
635 0 delete( $self->{banned}{ $opts{ban} } );
636 0 $self->{error} = 13;
637 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
638 0 $self->warn;
639 0 return;
640 };
641 }
642 } ## end sub ban
643  
644 =head2 unban
645  
646 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
647 and lowercased, then removed from the ban list. The full Network Group object
648 is re-rendered without it and PUT to the networkgroups endpoint. Unbanning an
649 IP that is not banned is a noop. If the PUT fails, the IP is restored to the
650 ban list.
651  
652 $backend->unban(ban => $ip);
653  
654 =cut
655  
656 1 ▶sub unban {
Subroutine unban: called CC=10 SCAR=38.0
Pod documented
657 1 my ( $self, %opts ) = @_;
658  
659 1 $self->errorblank;
660  
661 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
662 0 $self->{error} = 1;
663 0 $self->{errorString} = 'backend has not been inited';
664 0 $self->warn;
665 0 return;
666 }
667  
668 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
669 0 $self->{error} = 9;
670 0 $self->{errorString} = 'Nothing specified for the value ban';
671 0 $self->warn;
672 0 return;
673 } elsif ( ref( $opts{ban} ) ne '' ) {
674 0 $self->{error} = 10;
675 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
676 0 $self->warn;
677 0 return;
678 ▶ } 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
679 && $opts{ban} !~ /\A$IPv6_re\z/ )
680 {
681 0 $self->{error} = 10;
682 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
683 0 $self->warn;
684 0 return;
685 }
686  
687 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
688 1 $opts{ban} = lc( $opts{ban} );
689  
690 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
691 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
692 0 $self->{frontend_obj}->{test_data} = 'not banned';
693 }
694 0 return;
695 }
696  
697 1 delete( $self->{banned}{ $opts{ban} } );
698  
699 1 my $body = $self->_render;
700  
701 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
702 1 $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_group_url, content => $body } ];
703 } else {
704 0 local $@;
705 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
706 0 $self->{banned}{ $opts{ban} } = 1;
707 0 $self->{error} = 14;
708 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
709 0 $self->warn;
710 0 return;
711 };
712 }
713 } ## end sub unban
714  
715 =head2 ban_cidr
716  
717 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 CIDR
718 range and lowercased, then added to the CIDR ban list. The full Network Group
719 object, with the range as a C<Network> literal, is rendered and PUT to the
720 networkgroups endpoint. Banning an already banned range is a noop. If the PUT
721 fails, the range is removed from the ban list again.
722  
723 $backend->ban_cidr(ban => '1.2.3.0/24');
724  
725 =cut
726  
727 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=9 SCAR=36.2
Pod documented
728 1 my ( $self, %opts ) = @_;
729  
730 1 $self->errorblank;
731  
732 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
733 0 $self->{error} = 1;
734 0 $self->{errorString} = 'backend has not been inited';
735 0 $self->warn;
736 0 return;
737 }
738  
739 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
740 0 $self->{error} = 9;
741 0 $self->{errorString} = 'Nothing specified for the value ban';
742 0 $self->warn;
743 0 return;
744 } elsif ( ref( $opts{ban} ) ne '' ) {
745 0 $self->{error} = 36;
746 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
747 0 $self->warn;
748 0 return;
749 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
750 0 $self->{error} = 36;
751 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
752 0 $self->warn;
753 0 return;
754 }
755  
756 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
757 1 $opts{ban} = lc( $opts{ban} );
758  
759 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
760 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
761 0 $self->{frontend_obj}->{test_data} = 'already banned';
762 }
763 0 return;
764 }
765  
766 1 $self->{banned_cidr}{ $opts{ban} } = 1;
767  
768 1 my $body = $self->_render;
769  
770 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
771 1 $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_group_url, content => $body } ];
772 } else {
773 0 local $@;
774 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
775 0 delete( $self->{banned_cidr}{ $opts{ban} } );
776 0 $self->{error} = 34;
777 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
778 0 $self->warn;
779 0 return;
780 };
781 }
782 } ## end sub ban_cidr
783  
784 =head2 unban_cidr
785  
786 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
787 CIDR range and lowercased, then removed from the CIDR ban list. The full
788 Network Group object is re-rendered without it and PUT to the networkgroups
789 endpoint. Unbanning a range that is not banned is a noop. If the PUT fails,
790 the range is restored to the ban list.
791  
792 $backend->unban_cidr(ban => '1.2.3.0/24');
793  
794 =cut
795  
796 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=9 SCAR=36.2
Pod documented
797 1 my ( $self, %opts ) = @_;
798  
799 1 $self->errorblank;
800  
801 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
802 0 $self->{error} = 1;
803 0 $self->{errorString} = 'backend has not been inited';
804 0 $self->warn;
805 0 return;
806 }
807  
808 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
809 0 $self->{error} = 9;
810 0 $self->{errorString} = 'Nothing specified for the value ban';
811 0 $self->warn;
812 0 return;
813 } elsif ( ref( $opts{ban} ) ne '' ) {
814 0 $self->{error} = 36;
815 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
816 0 $self->warn;
817 0 return;
818 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
819 0 $self->{error} = 36;
820 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
821 0 $self->warn;
822 0 return;
823 }
824  
825 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
826 1 $opts{ban} = lc( $opts{ban} );
827  
828 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
829 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
830 0 $self->{frontend_obj}->{test_data} = 'not banned';
831 }
832 0 return;
833 }
834  
835 1 delete( $self->{banned_cidr}{ $opts{ban} } );
836  
837 1 my $body = $self->_render;
838  
839 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
840 1 $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_group_url, content => $body } ];
841 } else {
842 0 local $@;
843 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
844 0 $self->{banned_cidr}{ $opts{ban} } = 1;
845 0 $self->{error} = 35;
846 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
847 0 $self->warn;
848 0 return;
849 };
850 }
851 } ## end sub unban_cidr
852  
853 =head2 list_cidr
854  
855 List banned CIDR ranges. Returns an array of the currently banned ranges from
856 internal state; the FMC is not queried. Single IPs are not included; for those
857 see L</list>.
858  
859 my @banned_cidrs = $backend->list_cidr;
860  
861 =cut
862  
863 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
864 2 my ( $self, %opts ) = @_;
865  
866 2 $self->errorblank;
867  
868 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
869 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
870 }
871  
872 2 return keys( %{ $self->{banned_cidr} } );
873 }
874  
875 =head2 list
876  
877 List banned IPs. Returns an array of the currently banned single IPs from
878 internal state; the FMC is not queried. CIDR ranges are not included; for
879 those see L</list_cidr>.
880  
881 my @banned = $backend->list;
882  
883 =cut
884  
885 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
886 0 my ( $self, %opts ) = @_;
887  
888 0 $self->errorblank;
889  
890 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
891 0 $self->{frontend_obj}->{test_data} = 'list';
892 }
893  
894 0 return keys( %{ $self->{banned} } );
895 }
896  
897 =head2 re_init
898  
899 Tears down and re-inits, then re-adds all previously added bans by rendering
900 and PUTing the full Network Group object.
901  
902 =cut
903  
904 0 ▶sub re_init {
Subroutine re_init: not called CC=3 SCAR=24.8
Pod documented
905 0 my ( $self, %opts ) = @_;
906  
907 0 $self->errorblank;
908  
909 # teardown is best effort here as a partially or fully wiped setup is
910 # exactly what re_init needs to recover from
911 {
912 0 local $@;
913 0 eval { $self->teardown; };
914 }
915 0 $self->init;
916  
917 0 my $body = $self->_render;
918  
919 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
920 0 $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_group_url, content => $body } ];
921 } else {
922 0 local $@;
923 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
924 0 $self->{error} = 16;
925 0 $self->{errorString} = 're_init failed rendering the group... ' . $@;
926 0 $self->warn;
927 };
928 }
929  
930 0 $self->{inited} = 1;
931 } ## end sub re_init
932  
933 =head2 teardown
934  
935 Tears down the setup by PUTing the Network Group object with an empty literal
936 set. The internal list of bans is kept, so a following re_init will re-add
937 them.
938  
939 =cut
940  
941 0 ▶sub teardown {
Subroutine teardown: not called CC=3 SCAR=24.8
Pod documented
942 0 my ( $self, %opts ) = @_;
943  
944 0 $self->errorblank;
945  
946 0 $self->{inited} = 0;
947  
948 # render an empty literal set without disturbing the internal ban list
949 my $body = $self->_json->encode(
950 {
951 name => $self->{options}{group_name},
952 id => $self->{options}{group_id},
953 0 literals => [],
954 }
955 );
956  
957 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
958 0 $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_group_url, content => $body } ];
959 } else {
960 0 local $@;
961 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
962 0 $self->{error} = 17;
963 0 $self->{errorString} = 'teardown failed clearing the group... ' . $@;
964 0 $self->warn;
965 };
966 }
967 } ## end sub teardown
968  
969 =head2 stop
970  
971 Alias for L</teardown>.
972  
973 =cut
974  
975 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
976 0 my ( $self, %opts ) = @_;
977  
978 0 return $self->teardown(%opts);
979 }
980  
981 =head2 check
982  
983 Verifies the endpoint and token are still usable by fetching the Network Group
984 object. Returns a true value if so and a false value otherwise.
985  
986 =cut
987  
988 5 ▶sub check {
Subroutine check: called CC=3 SCAR=15.4
Pod documented
989 5 my ( $self, %opts ) = @_;
990  
991 5 $self->errorblank;
992  
993 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
994 5 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $self->_group_url } ];
995 5 return 1;
996 }
997  
998 0 local $@;
999 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
1000  
1001 0 return 1;
1002 } ## end sub check
1003  
1004 =head2 flush
1005  
1006 Removes all currently banned IPs and CIDR ranges at once by PUTing the
1007 Network Group object with an empty literal set and forgetting them.
1008  
1009 =cut
1010  
1011 0 ▶sub flush {
Subroutine flush: not called CC=4 SCAR=30.0
Pod documented
1012 0 my ( $self, %opts ) = @_;
1013  
1014 0 $self->errorblank;
1015  
1016 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1017 0 $self->{error} = 1;
1018 0 $self->{errorString} = 'backend has not been inited';
1019 0 $self->warn;
1020 0 return;
1021 }
1022  
1023 0 $self->{banned} = {};
1024 0 $self->{banned_cidr} = {};
1025  
1026 0 my $body = $self->_render;
1027  
1028 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1029 0 $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_group_url, content => $body } ];
1030 } else {
1031 0 local $@;
1032 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
1033 0 $self->{error} = 25;
1034 0 $self->{errorString} = 'flush failed clearing the group... ' . $@;
1035 0 $self->warn;
1036 };
1037 }
1038 } ## end sub flush
1039  
1040 =head1 ERROR CODES / FLAGS
1041  
1042 Error handling is provided by L<Error::Helper>. All
1043 errors are considered fatal.
1044  
1045 =head2 1, notInited
1046  
1047 The backend has not been inited yet.
1048  
1049 =head2 6, invalidPrefixSpecified
1050  
1051 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1052  
1053 =head2 7, invalidName
1054  
1055 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1056  
1057 =head2 8, optionsNotHash
1058  
1059 The item passed to new for options is not a hash.
1060  
1061 =head2 9, noBanItem
1062  
1063 No IP or CIDR range specified to ban or unban.
1064  
1065 =head2 10, banItemNotIP
1066  
1067 The item to ban is not an IP. Either wrong ref type or regexp
1068 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1069  
1070 =head2 12, backendInitError
1071  
1072 Failed to init the backend.
1073  
1074 =head2 13, banFailed
1075  
1076 Failed to ban the item.
1077  
1078 =head2 14, unbanFailed
1079  
1080 Failed to unban the item.
1081  
1082 =head2 15, listFailed
1083  
1084 Failed to get a list of bans.
1085  
1086 =head2 16, reInitFailed
1087  
1088 Failed to re_init the backend.
1089  
1090 =head2 17, teardownFailed
1091  
1092 Failed to teardown the backend.
1093  
1094 =head2 18, alreadyInited
1095  
1096 init called, but the backend has already been inited.
1097  
1098 =head2 23, initFailed
1099  
1100 init failed. Generating an auth token failed.
1101  
1102 =head2 24, checkFailed
1103  
1104 The backend check raised an error.
1105  
1106 =head2 25, flushFailed
1107  
1108 Failed to flush the bans.
1109  
1110 =head2 26, portsNotSupported
1111  
1112 The cisco_fmc backend blocks whole IPs and does not support ports.
1113  
1114 =head2 27, protocolsNotSupported
1115  
1116 The cisco_fmc backend blocks whole IPs and does not support protocols.
1117  
1118 =head2 30, hostNotDefined
1119  
1120 The option host is undef or blank.
1121  
1122 =head2 31, userNotDefined
1123  
1124 The option user is undef or blank.
1125  
1126 =head2 32, passwordNotDefined
1127  
1128 The option password is undef or blank.
1129  
1130 =head2 33, groupIdNotDefined
1131  
1132 The option group_id is undef or blank.
1133  
1134 =head2 34, banCidrFailed
1135  
1136 Failed to ban the CIDR range.
1137  
1138 =head2 35, unbanCidrFailed
1139  
1140 Failed to unban the CIDR range.
1141  
1142 =head2 36, cidrItemNotCidr
1143  
1144 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1145 IPv4 or IPv6 address followed by a prefix length valid for its family.
1146  
1147 =head2 37, cidrNotSupported
1148  
1149 The backend does not support CIDR bans.
1150  
1151 =head2 38, listCidrFailed
1152  
1153 Failed to get a list of CIDR bans.
1154  
1155 =head1 AUTHOR
1156  
1157 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1158  
1159 =head1 LICENSE AND COPYRIGHT
1160  
1161 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1162  
1163 This is free software, licensed under:
1164  
1165 The GNU Lesser General Public License, Version 2.1, February 1999
1166  
1167 =cut
1168  
1169 1 1; # End of Net::Firewall::BlockerHelper::backends::cisco_fmc
« BlockerHelper/backends/checkpoint.pm ↑ Summary BlockerHelper/backends/fortigate.pm »