BlockerHelper/backends/pfsense.pm

filter: Statementstmt 33.4% 121 / 362 Branchbran 35.2% 48 / 136 Conditioncond 43.3% 13 / 30 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 73.9% 17 / 23 Podpod 100% 13 / 13 totaltotal 36.9% 215 / 582 CC 79 SCAR 37.9
CC
79
Cov
34%
new
96.4
ban
45.3
unban
45.3
CRAP 44.3
« BlockerHelper/backends/netscaler.pm ↑ Summary BlockerHelper/backends/routeros_api.pm »
1 package Net::Firewall::BlockerHelper::backends::pfsense;
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::pfsense - pfSense firewall alias backend for Net::Firewall::BlockerHelper.
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 => 'pfsense',
28 name => 'ssh',
29 options => {
30 host => 'fw.example.org',
31 key => 'someAPIkey',
32 },
33 );
34  
35 $fw_helper->init_backend;
36 $fw_helper->ban(ban => '1.2.3.4');
37 $fw_helper->unban(ban => '1.2.3.4');
38  
39 =head1 DESCRIPTION
40  
41 This backend blocks IPs by managing the membership of a pfSense firewall
42 alias via the pfSense REST API package (jaredhendrickson13's pfSense-API),
43 using its v2 endpoints and L<LWP::UserAgent>.
44  
45 A single host type alias holds all of the banned IPs. Both IPv4 and IPv6
46 addresses are added to the same alias, as a pfSense host alias holds an
47 array of IP address strings regardless of family.
48  
49 The alias membership is rendered from state: the full set of banned IPs is
50 sent on every change, followed by a firewall apply so pfSense reloads the
51 running configuration.
52  
53 The alias B<must> already exist in pfSense and be referenced by a firewall
54 rule that does the actual blocking. This backend only manages the contents
55 of the alias, not the alias itself nor the rule referencing it.
56  
57 Auth is via the C<X-API-Key> header, set from the C<key> option.
58  
59 L<LWP::UserAgent> is only loaded at run time, so it is only required if
60 this backend is actually used. For https, L<LWP::Protocol::https> must be
61 present as well.
62  
63 =head1 NOTES
64  
65 This backend was written going off the API docs and actual testing is
66 needed to double check a few things as the exact behavior is not clear.
67  
68 ban_cidr sends the alias update with the type still set to host and it is
69 not clear whether the API will accept CIDR entries in a host type alias
70 or if a network type alias is required, in which case the type used will
71 need changing.
72  
73 =head1 METHODS
74  
75 =head2 new
76  
77 Initiates the the object.
78  
79 - options :: Backend specific options. See below.
80 - Default :: {}
81  
82 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
83 - default :: kur
84  
85 - name :: Name of this specific instance. This must be specified.
86 - default :: undef
87  
88 Ports and protocols are not supported by this backend and specifying either
89 is an error.
90  
91 The options hash accepts the following.
92  
93 - host :: pfSense hostname or IP the API is reached at. May include a
94 port, eg 'fw.example.org:8443'. This must be specified.
95 - Default :: undef
96  
97 - key :: The pfSense REST API key, sent via the X-API-Key header. This
98 must be specified.
99 - Default :: undef
100  
101 - alias :: The name of the alias the IPs are added to. The alias must
102 already exist in pfSense.
103 - Default :: <prefix>_<name>
104  
105 - timeout :: HTTP timeout in seconds.
106 - Default :: 30
107  
108 - insecure :: If true, certificate verification is disabled so
109 self-signed certs are accepted.
110 - Default :: 0
111  
112 All errors are considered fatal, meaning if new fails it will die.
113  
114 =cut
115  
116 4 ▶sub new {
Subroutine new: called CC=26 SCAR=45.7
Pod documented
117 4 my ( $blank, %opts ) = @_;
118  
119 4 my $self = {
120 perror => undef,
121 error => undef,
122 errorLine => undef,
123 errorFilename => undef,
124 errorString => "",
125 errorExtra => {
126 all_errors_fatal => 1,
127 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
128 # is kept for the name documented in its POD
129 all_fatal => 1,
130 flags => {
131 1 => 'notInited',
132 6 => 'invalidPrefixSpecified',
133 7 => 'invalidName',
134 8 => 'optionsNotHash',
135 9 => 'noBanItem',
136 10 => 'banItemNotIP',
137 12 => 'backendInitError',
138 13 => 'banFailed',
139 14 => 'unbanFailed',
140 15 => 'listFailed',
141 16 => 'reInitFailed',
142 17 => 'teardownFailed',
143 18 => 'alreadyInited',
144 23 => 'initFailed',
145 24 => 'checkFailed',
146 25 => 'flushFailed',
147 26 => 'portsNotSupported',
148 27 => 'protocolsNotSupported',
149 30 => 'hostNotDefined',
150 31 => 'keyNotDefined',
151 32 => 'banCidrFailed',
152 33 => 'unbanCidrFailed',
153 34 => 'cidrItemNotCidr',
154 35 => 'cidrNotSupported',
155 36 => 'listCidrFailed',
156 },
157 fatal_flags => {},
158 perror_not_fatal => 0,
159 },
160 options => {},
161 ports => [],
162 protocols => [],
163 testing => undef,
164 test_data => undef,
165 prefix => 'kur',
166 frontend_obj => undef,
167 inited => 0,
168 banned => {},
169 banned_cidr => {},
170 cidr_supported => 1,
171 ua => undef,
172 };
173 4 bless $self;
174  
175 # blocking is per IP; the alias has no concept of ports or protocols
176 4 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) eq 'ARRAY' && defined( $opts{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]) 0 4 4
Condition33%2/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
040
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/3
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
defined $opts{"ports"} ref $opts{"ports"} eq "ARRAY" defined $opts{"ports"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"ports"}B= ref $opts{"ports"} eq "ARRAY"C= defined $opts{"ports"}[0]
ABCresult
0XX0
10X0
1100
1111
177 0 $self->{perror} = 1;
178 0 $self->{error} = 26;
179 0 $self->{errorString} = 'the pfsense backend blocks whole IPs and does not support ports';
180 0 $self->warn;
181 }
182 4 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) eq 'ARRAY' && defined( $opts{protocols}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]) 0 4 4
Condition33%2/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
040
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
defined $opts{"protocols"} ref $opts{"protocols"} eq "ARRAY" defined $opts{"protocols"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"protocols"}B= ref $opts{"protocols"} eq "ARRAY"C= defined $opts{"protocols"}[0]
ABCresult
0XX0
10X0
1100
1111
183 0 $self->{perror} = 1;
184 0 $self->{error} = 27;
185 0 $self->{errorString} = 'the pfsense backend blocks whole IPs and does not support protocols';
186 0 $self->warn;
187 }
188  
189 # make sure prefix is sane if defiend
190 4 ▶ if ( defined( $opts{prefix} ) && $opts{prefix} !~ /^[a-zA-Z0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'prefix'} and not $opts{'prefix'} =~ /^[a-zA-Z0-9]+$/) { } 0 4 4
elsif (defined $opts{'prefix'}) { } 4 0 4
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
040
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
191 0 $self->{perror} = 1;
192 0 $self->{error} = 6;
193 $self->{errorString}
194 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
195 0 $self->warn;
196 } elsif ( defined( $opts{prefix} ) ) {
197 4 $self->{prefix} = $opts{prefix};
198 }
199  
200 # make sure we have a name and that it is valid
201 4 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 4 4
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 4 4
202 0 $self->{perror} = 1;
203 0 $self->{error} = 7;
204 0 $self->{errorString} = 'name is undef';
205 0 $self->warn;
206 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
207 0 $self->{perror} = 1;
208 0 $self->{error} = 7;
209 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
210 0 $self->warn;
211 }
212 4 $self->{name} = $opts{name};
213  
214 # used internally for testing
215 4 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 4 0 4
216 4 $self->{testing} = $opts{testing};
217 }
218 4 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 4 0 4
219 4 $self->{frontend_obj} = $opts{frontend_obj};
220 }
221  
222 4 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 4 0 4
223 4 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 4 4
224 0 $self->{perror} = 1;
225 0 $self->{error} = 8;
226 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
227 0 $self->warn;
228 }
229 4 $self->{options} = $opts{options};
230 }
231  
232 # host and key are both required to be able to talk to the API
233 4 ▶ if ( !defined( $self->{options}{host} ) || $self->{options}{host} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq "") 1 3 4
Condition66%2/3
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
not defined $self->{"options"}{"host"} $self->{"options"}{"host"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"host"}B= $self->{"options"}{"host"} eq ""
ABresult
1X1
011
000
234 1 $self->{perror} = 1;
235 1 $self->{error} = 30;
236 1 $self->{errorString} = 'host is not defined or is blank';
237 1 $self->warn;
238 }
239 3 ▶ if ( !defined( $self->{options}{key} ) || $self->{options}{key} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"key"} or $self->{"options"}{"key"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"key"} or $self->{"options"}{"key"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"key"} or $self->{"options"}{"key"} eq ""
not defined $self->{"options"}{"key"} $self->{"options"}{"key"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"key"}B= $self->{"options"}{"key"} eq ""
ABresult
1X1
011
000
240 1 $self->{perror} = 1;
241 1 $self->{error} = 31;
242 1 $self->{errorString} = 'key is not defined or is blank';
243 1 $self->warn;
244 }
245  
246 # default the alias name to <prefix>_<name>
247 2 ▶ if ( !defined( $self->{options}{alias} ) || $self->{options}{alias} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"alias"} or $self->{"options"}{"alias"} eq "") 1 1 2
Condition66%2/3
not defined $self->{"options"}{"alias"} or $self->{"options"}{"alias"} eq ""
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $self->{"options"}{"alias"} or $self->{"options"}{"alias"} eq ""
not defined $self->{"options"}{"alias"} $self->{"options"}{"alias"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"alias"}B= $self->{"options"}{"alias"} eq ""
ABresult
1X1
011
000
248 1 ▶ $self->{options}{alias} = $self->{prefix} . '_' . ( defined( $self->{name} ) ? $self->{name} : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'name'} ? : 1 0 1
249 }
250  
251 2 ▶ if ( !defined( $self->{options}{insecure} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"insecure"}) 2 0 2
252 2 $self->{options}{insecure} = 0;
253 }
254  
255 2 ▶ if ( !defined( $self->{options}{timeout} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'options'}{'timeout'}) { } 2 0 2
elsif (not $self->{'options'}{'timeout'} =~ /^[0-9]+$/) { } 0 0 0
256 2 $self->{options}{timeout} = 30;
257 } elsif ( $self->{options}{timeout} !~ /^[0-9]+$/ ) {
258 0 $self->{perror} = 1;
259 0 $self->{error} = 12;
260 0 $self->{errorString} = 'the option timeout, "' . $self->{options}{timeout} . '", is not an int';
261 0 $self->warn;
262 }
263  
264 2 return $self;
265 } ## end sub new
266  
267 # Internal helper. Returns the scheme and host of the pfSense box, which every
268 # API URL is built on.
269 #
270 # The scheme is fixed at https rather than configurable, since the pfSense API
271 # is expected to be run over TLS.
272 #
273 # Takes no arguments; the host comes from the options.
274 #
275 # Returns the base URL as a string, with no trailing slash.
276 #
277 # $self->_base; # https://pf.example.org
278 20 ▶sub _base {
Subroutine _base: called CC=1 SCAR=0.0
279 20 my ($self) = @_;
280  
281 20 return 'https://' . $self->{options}{host};
282 }
283  
284 # Internal helper. Returns the URL that reads back the alias, used by init and
285 # check to confirm it is there.
286 #
287 # Since this backend creates nothing, the alias existing is the whole of its
288 # setup, and an alias deleted out from under a running process is what this
289 # detects. init runs it so a misconfigured alias name fails immediately rather
290 # than at the first ban, and check runs it to decide whether the self heal
291 # path should re-push the current bans.
292 #
293 # The alias name is percent encoded going into the query string, as it is a
294 # configured value rather than something derived.
295 #
296 # Takes no arguments; the alias name comes from the options.
297 #
298 # Returns the URL as a string, for a GET whose exit is only inspected for
299 # success.
300 #
301 # $self->_probe_url;
302 # # https://pf.example.org/api/v2/firewall/alias?name=blocklist
303 8 ▶sub _probe_url {
Subroutine _probe_url: called CC=1 SCAR=0.0
304 8 my ($self) = @_;
305  
306 8 return $self->_base . '/api/v2/firewall/alias?name=' . $self->_uri_escape( $self->{options}{alias} );
307 }
308  
309 # Internal helper. Renders the complete pfSense alias body from the current
310 # bans, which is what gets pushed to replace the alias contents.
311 #
312 # The alias is replace-in-full: there is no add or remove one entry call, so
313 # every ban and unban rewrites the whole membership from the internal state.
314 #
315 # Single addresses and CIDR ranges are held in two separate internal lists but
316 # go into the same alias, since pfSense does not distinguish them here. Unlike
317 # most of the other backends they are merged and sorted together into one run
318 # rather than emitted as two, so the ordering is a single lexical sort across
319 # both.
320 #
321 # Note the type is sent as 'host' even when the alias holds ranges. That is
322 # what the alias is configured as, and pfSense accepts CIDR entries in it.
323 #
324 # Args:
325 #
326 # ips - Optional arrayref of entries to render instead of the current
327 # state. Each entry is a plain string, an address or a CIDR range.
328 # Pass an empty arrayref to render an empty alias, which is how
329 # teardown and flush clear it. When omitted or undef, the current
330 # bans are used, which is what every normal call does.
331 #
332 # Returns the alias body as a JSON string, ready to be the content of the
333 # update request.
334 #
335 # # with 10.0.0.1 and 10.0.0.0/8 banned
336 # $self->_render;
337 # # {"address":["10.0.0.0/8","10.0.0.1"],"id":"blocklist","type":"host"}
338 #
339 # # explicitly empty, as teardown does
340 # $self->_render( [] );
341 # # {"address":[],"id":"blocklist","type":"host"}
342 6 ▶sub _render {
Subroutine _render: called CC=2 SCAR=7.0
343 6 my ( $self, $ips ) = @_;
344  
345 6 ▶ if ( !defined($ips) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ips) 6 0 6
346 # the alias holds both single IPs and CIDR ranges
347 6 $ips = [ sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ];
348 }
349  
350 return $self->_json->encode(
351 {
352 id => $self->{options}{alias},
353 6 type => 'host',
354 address => $ips,
355 }
356 );
357 } ## end sub _render
358  
359 # Internal helper. Performs one HTTP request against the pfSense API. Every
360 # call this backend makes to the box goes through here.
361 #
362 # The user agent is built on first use and cached on the object, so a run of
363 # requests shares one agent. LWP::UserAgent is loaded with require at that
364 # point rather than at compile time, since only the HTTP backends need it;
365 # failing to load it dies with an explanation naming this backend. The
366 # insecure option turns off certificate verification, which is there because a
367 # pfSense box commonly presents a self signed certificate.
368 #
369 # Any HTTP level failure dies rather than setting an error. The callers wrap
370 # this in eval and turn the exception into the appropriate error code, which
371 # is what lets one helper serve paths that report ban, unban, and teardown
372 # failures differently.
373 #
374 # Never called in testing mode; those paths record the request instead.
375 #
376 # Args:
377 #
378 # method - The HTTP method, as a plain string, such as 'GET' or 'PATCH'.
379 #
380 # url - The full URL to request, as a plain string, built on _base.
381 #
382 # body - Optional request body, as an already encoded JSON string,
383 # normally from _render. undef for methods that carry no body.
384 #
385 # Returns the decoded response body as whatever structure the JSON held,
386 # normally a hashref. Dies on any non success HTTP status.
387 #
388 # $self->_request( 'GET', $self->_probe_url );
389 #
390 # # the usual shape at the call sites
391 # eval { $self->_request( 'PATCH', $url, $self->_render ); };
392 # if ($@) { ... raise banFailed ... }
393 0 ▶sub _request {
Subroutine _request: not called CC=5 SCAR=34.0
394 0 my ( $self, $method, $url, $body ) = @_;
395  
396 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
397 0 local $@;
398 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent; my(%ua_opts) = ("agent", 'Net::Firewall::BlockerHelper/' . $Net::Firewall::BlockerHelper::backends::pfsense::VERSION, "timeout", $self->{"options"}{"timeout"}); if ($self->{"options"}{"insecure"}) { $ua_opts{"ssl_opts"} = {"verify_hostname", 0, "SSL_verify_mode", 0}; } ; $self->{"ua"} = "LWP::UserAgent"->new(%ua_opts); 1 } } or die 'failed to load LWP::UserAgent, which the pfsense backend requires... ' . $@ 0 0 0
399 0 require LWP::UserAgent;
400 my %ua_opts = (
401 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
402 timeout => $self->{options}{timeout},
403 0 );
404 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
405 0 $ua_opts{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
406 }
407 0 $self->{ua} = LWP::UserAgent->new(%ua_opts);
408 0 1;
409 } or die( 'failed to load LWP::UserAgent, which the pfsense backend requires... ' . $@ );
410 } ## end if ( !defined( $self->{ua...}))
411  
412 0 my @headers = ( 'Content-Type' => 'application/json', 'X-API-Key' => $self->{options}{key} );
413  
414 0 require HTTP::Request;
415 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
416  
417 0 my $response = $self->{ua}->request($request);
418  
419 0 my $decoded;
420 0 local $@;
421 0 eval { $decoded = $self->_json->decode( $response->decoded_content ); };
422  
423 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
424 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line );
425 }
426  
427 0 return $decoded;
428 } ## end sub _request
429  
430 =head2 init
431  
432 Initiates the backend. Verifies the alias exists by fetching it.
433  
434 No arguments are taken.
435  
436 May called a second time, it will error.
437  
438 $backend->init;
439  
440 =cut
441  
442 2 ▶sub init {
Subroutine init: called CC=4 SCAR=20.3
Pod documented
443 2 my ( $self, %opts ) = @_;
444  
445 2 $self->errorblank;
446  
447 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
448 0 $self->{error} = 18;
449 0 $self->{errorString} = 'backend has already been inited';
450 0 $self->warn;
451 }
452  
453 2 my $url = $self->_probe_url;
454  
455 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
456 2 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
457 } else {
458 0 local $@;
459 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or do { $self->{"error"} = 23; $self->{"errorString"} = 'init failed. probing the alias failed... ' . $@; $self->warn } 0 0 0
460 0 $self->{error} = 23;
461 0 $self->{errorString} = 'init failed. probing the alias failed... ' . $@;
462 0 $self->warn;
463 };
464 }
465  
466 2 $self->{inited} = 1;
467 } ## end sub init
468  
469 =head2 ban
470  
471 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
472 and lowercased, then the full alias membership including it is rendered and
473 sent via a PATCH to C</api/v2/firewall/alias>, followed by a POST to
474 C</api/v2/firewall/apply>. If the API calls fail, the IP is removed from
475 the internal ban list again. Banning an already banned IP is a noop.
476  
477 $backend->ban(ban => $ip);
478  
479 =cut
480  
481 2 ▶sub ban {
Subroutine ban: called CC=10 SCAR=38.1
Pod documented
482 2 my ( $self, %opts ) = @_;
483  
484 2 $self->errorblank;
485  
486 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
487 0 $self->{error} = 1;
488 0 $self->{errorString} = 'backend has not been inited';
489 0 $self->warn;
490 0 return;
491 }
492  
493 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::pfsense::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/) { } 0 2 2
494 0 $self->{error} = 9;
495 0 $self->{errorString} = 'Nothing specified for the value ban';
496 0 $self->warn;
497 0 return;
498 } elsif ( ref( $opts{ban} ) ne '' ) {
499 0 $self->{error} = 10;
500 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
501 0 $self->warn;
502 0 return;
503 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/
l!l&&r!l&&!r
200
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/
ABresult
1X1
011
000
504 && $opts{ban} !~ /\A$IPv6_re\z/ )
505 {
506 0 $self->{error} = 10;
507 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
508 0 $self->warn;
509 0 return;
510 }
511  
512 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
513 2 $opts{ban} = lc( $opts{ban} );
514  
515 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
516 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
517 0 $self->{frontend_obj}->{test_data} = 'already banned';
518 }
519 0 return;
520 }
521  
522 2 $self->{banned}{ $opts{ban} } = 1;
523  
524 2 my $body = $self->_render;
525  
526 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
527 $self->{frontend_obj}->{test_data} = [
528 2 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
529 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
530 ];
531 } else {
532 0 local $@;
533 eval {
534 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
535 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
536 0 1;
537 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PATCH", $self->_base . '/api/v2/firewall/alias', $body); $self->_request("POST", $self->_base . '/api/v2/firewall/apply', ""); 1 } } or do { delete $self->{"banned"}{$opts{"ban"}}; $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
538 0 delete( $self->{banned}{ $opts{ban} } );
539 0 $self->{error} = 13;
540 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
541 0 $self->warn;
542 0 return;
543 };
544 } ## end else [ if ( $self->{testing} )]
545 } ## end sub ban
546  
547 =head2 unban
548  
549 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
550 and lowercased, then the alias membership is re-rendered without it and
551 sent via a PATCH to C</api/v2/firewall/alias>, followed by a POST to
552 C</api/v2/firewall/apply>. If the API calls fail, the IP is restored to the
553 internal ban list. Unbanning an IP that is not banned is a noop.
554  
555 $backend->unban(ban => $ip);
556  
557 =cut
558  
559 2 ▶sub unban {
Subroutine unban: called CC=10 SCAR=38.1
Pod documented
560 2 my ( $self, %opts ) = @_;
561  
562 2 $self->errorblank;
563  
564 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
565 0 $self->{error} = 1;
566 0 $self->{errorString} = 'backend has not been inited';
567 0 $self->warn;
568 0 return;
569 }
570  
571 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::pfsense::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/) { } 0 2 2
572 0 $self->{error} = 9;
573 0 $self->{errorString} = 'Nothing specified for the value ban';
574 0 $self->warn;
575 0 return;
576 } elsif ( ref( $opts{ban} ) ne '' ) {
577 0 $self->{error} = 10;
578 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
579 0 $self->warn;
580 0 return;
581 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/
l!l&&r!l&&!r
200
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/
ABresult
1X1
011
000
582 && $opts{ban} !~ /\A$IPv6_re\z/ )
583 {
584 0 $self->{error} = 10;
585 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
586 0 $self->warn;
587 0 return;
588 }
589  
590 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
591 2 $opts{ban} = lc( $opts{ban} );
592  
593 2 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 2 2
594 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
595 0 $self->{frontend_obj}->{test_data} = 'not banned';
596 }
597 0 return;
598 }
599  
600 2 delete( $self->{banned}{ $opts{ban} } );
601  
602 2 my $body = $self->_render;
603  
604 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
605 $self->{frontend_obj}->{test_data} = [
606 2 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
607 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
608 ];
609 } else {
610 0 local $@;
611 eval {
612 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
613 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
614 0 1;
615 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PATCH", $self->_base . '/api/v2/firewall/alias', $body); $self->_request("POST", $self->_base . '/api/v2/firewall/apply', ""); 1 } } or do { $self->{"banned"}{$opts{"ban"}} = 1; $self->{"error"} = 14; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
616 0 $self->{banned}{ $opts{ban} } = 1;
617 0 $self->{error} = 14;
618 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
619 0 $self->warn;
620 0 return;
621 };
622 } ## end else [ if ( $self->{testing} )]
623 } ## end sub unban
624  
625 =head2 ban_cidr
626  
627 Bans a CIDR range by adding it to the alias membership and applying the
628 change via the same PATCH and apply calls used by L</ban>. A pfSense host
629 alias holds CIDR ranges in the same manner as single addresses. The value
630 of ban is validated as being a IPv4 or IPv6 CIDR range and lowercased.
631 Banning an already banned range is a noop.
632  
633 $backend->ban_cidr(ban => '1.2.3.0/24');
634  
635 =cut
636  
637 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=9 SCAR=36.3
Pod documented
638 1 my ( $self, %opts ) = @_;
639  
640 1 $self->errorblank;
641  
642 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
643 0 $self->{error} = 1;
644 0 $self->{errorString} = 'backend has not been inited';
645 0 $self->warn;
646 0 return;
647 }
648  
649 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 1 1
650 0 $self->{error} = 9;
651 0 $self->{errorString} = 'Nothing specified for the value ban';
652 0 $self->warn;
653 0 return;
654 } elsif ( ref( $opts{ban} ) ne '' ) {
655 0 $self->{error} = 34;
656 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
657 0 $self->warn;
658 0 return;
659 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
660 0 $self->{error} = 34;
661 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
662 0 $self->warn;
663 0 return;
664 }
665  
666 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
667 1 $opts{ban} = lc( $opts{ban} );
668  
669 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
670 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
671 0 $self->{frontend_obj}->{test_data} = 'already banned';
672 }
673 0 return;
674 }
675  
676 1 $self->{banned_cidr}{ $opts{ban} } = 1;
677  
678 1 my $body = $self->_render;
679  
680 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
681 $self->{frontend_obj}->{test_data} = [
682 1 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
683 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
684 ];
685 } else {
686 0 local $@;
687 eval {
688 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
689 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
690 0 1;
691 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PATCH", $self->_base . '/api/v2/firewall/alias', $body); $self->_request("POST", $self->_base . '/api/v2/firewall/apply', ""); 1 } } or do { delete $self->{"banned_cidr"}{$opts{"ban"}}; $self->{"error"} = 32; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
692 0 delete( $self->{banned_cidr}{ $opts{ban} } );
693 0 $self->{error} = 32;
694 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
695 0 $self->warn;
696 0 return;
697 };
698 } ## end else [ if ( $self->{testing} )]
699 } ## end sub ban_cidr
700  
701 =head2 unban_cidr
702  
703 Unbans a CIDR range by removing it from the alias membership and applying
704 the change. The value of ban is validated as being a IPv4 or IPv6 CIDR
705 range and lowercased. Unbanning a range that is not banned is a noop.
706  
707 $backend->unban_cidr(ban => '1.2.3.0/24');
708  
709 =cut
710  
711 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=9 SCAR=36.3
Pod documented
712 1 my ( $self, %opts ) = @_;
713  
714 1 $self->errorblank;
715  
716 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
717 0 $self->{error} = 1;
718 0 $self->{errorString} = 'backend has not been inited';
719 0 $self->warn;
720 0 return;
721 }
722  
723 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
724 0 $self->{error} = 9;
725 0 $self->{errorString} = 'Nothing specified for the value ban';
726 0 $self->warn;
727 0 return;
728 } elsif ( ref( $opts{ban} ) ne '' ) {
729 0 $self->{error} = 34;
730 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
731 0 $self->warn;
732 0 return;
733 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
734 0 $self->{error} = 34;
735 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
736 0 $self->warn;
737 0 return;
738 }
739  
740 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
741 1 $opts{ban} = lc( $opts{ban} );
742  
743 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
744 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
745 0 $self->{frontend_obj}->{test_data} = 'not banned';
746 }
747 0 return;
748 }
749  
750 1 delete( $self->{banned_cidr}{ $opts{ban} } );
751  
752 1 my $body = $self->_render;
753  
754 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
755 $self->{frontend_obj}->{test_data} = [
756 1 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
757 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
758 ];
759 } else {
760 0 local $@;
761 eval {
762 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
763 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
764 0 1;
765 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PATCH", $self->_base . '/api/v2/firewall/alias', $body); $self->_request("POST", $self->_base . '/api/v2/firewall/apply', ""); 1 } } or do { $self->{"banned_cidr"}{$opts{"ban"}} = 1; $self->{"error"} = 33; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
766 0 $self->{banned_cidr}{ $opts{ban} } = 1;
767 0 $self->{error} = 33;
768 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
769 0 $self->warn;
770 0 return;
771 };
772 } ## end else [ if ( $self->{testing} )]
773 } ## end sub unban_cidr
774  
775 =head2 list_cidr
776  
777 List banned CIDR ranges. Returns an array of the currently banned CIDR
778 ranges. Single IPs are not included; for those see L</list>.
779  
780 my @banned_cidrs = $backend->list_cidr;
781  
782 =cut
783  
784 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
785 2 my ( $self, %opts ) = @_;
786  
787 2 $self->errorblank;
788  
789 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
790 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
791 }
792  
793 2 return keys( %{ $self->{banned_cidr} } );
794 }
795  
796 =head2 list
797  
798 List banned IPs. Returns an array of the currently banned single IPs. CIDR
799 ranges are not included; for those see L</list_cidr>.
800  
801 my @banned = $backend->list;
802  
803 =cut
804  
805 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
806 0 my ( $self, %opts ) = @_;
807  
808 0 $self->errorblank;
809  
810 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
811 0 $self->{frontend_obj}->{test_data} = 'list';
812 }
813  
814 0 return keys( %{ $self->{banned} } );
815 }
816  
817 =head2 re_init
818  
819 Tells the backend to re-init it's self.
820  
821 This will call teardown and init again. After that it will re-add all
822 previously added bans by rendering the full alias membership and applying.
823  
824 $backend->re_init;
825  
826 =cut
827  
828 0 ▶sub re_init {
Subroutine re_init: not called CC=3 SCAR=24.8
Pod documented
829 0 my ( $self, %opts ) = @_;
830  
831 0 $self->errorblank;
832  
833 # teardown is best effort here as a partially or fully wiped setup is
834 # exactly what re_init needs to recover from; init cleans up any remnants
835 {
836 0 local $@;
837 0 eval { $self->teardown; };
838 }
839 0 $self->init;
840  
841 0 my $body = $self->_render;
842  
843 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
844 $self->{frontend_obj}->{test_data} = [
845 0 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
846 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
847 ];
848 } else {
849 0 local $@;
850 eval {
851 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
852 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
853 0 1;
854 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PATCH", $self->_base . '/api/v2/firewall/alias', $body); $self->_request("POST", $self->_base . '/api/v2/firewall/apply', ""); 1 } } or do { $self->{"error"} = 16; $self->{"errorString"} = 're_init failed re-adding the bans... ' . $@; $self->warn } 0 0 0
855 0 $self->{error} = 16;
856 0 $self->{errorString} = 're_init failed re-adding the bans... ' . $@;
857 0 $self->warn;
858 };
859 } ## end else [ if ( $self->{testing} )]
860  
861 0 $self->{inited} = 1;
862 } ## end sub re_init
863  
864 =head2 teardown
865  
866 Tears down the setup for the backend by rendering the alias with an empty
867 membership and applying. The internal list of bans is kept, so a following
868 re_init will re-add them. The alias itself and the rule referencing it are
869 left in place.
870  
871 $backend->teardown;
872  
873 =cut
874  
875 0 ▶sub teardown {
Subroutine teardown: not called CC=3 SCAR=24.8
Pod documented
876 0 my ( $self, %opts ) = @_;
877  
878 0 $self->errorblank;
879  
880 0 $self->{inited} = 0;
881  
882 0 my $body = $self->_render( [] );
883  
884 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
885 $self->{frontend_obj}->{test_data} = [
886 0 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
887 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
888 ];
889 } else {
890 0 local $@;
891 eval {
892 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
893 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
894 0 1;
895 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PATCH", $self->_base . '/api/v2/firewall/alias', $body); $self->_request("POST", $self->_base . '/api/v2/firewall/apply', ""); 1 } } or do { $self->{"error"} = 17; $self->{"errorString"} = 'teardown failed clearing the alias... ' . $@; $self->warn } 0 0 0
896 0 $self->{error} = 17;
897 0 $self->{errorString} = 'teardown failed clearing the alias... ' . $@;
898 0 $self->warn;
899 };
900 } ## end else [ if ( $self->{testing} )]
901 } ## end sub teardown
902  
903 =head2 stop
904  
905 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
906 concept.
907  
908 $backend->stop;
909  
910 =cut
911  
912 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
913 0 my ( $self, %opts ) = @_;
914  
915 0 return $self->teardown(%opts);
916 }
917  
918 =head2 check
919  
920 Verifies the alias is still reachable by fetching it. Returns a true value
921 if so and a false value otherwise. This is the equivalent of fail2ban's
922 C<actioncheck>.
923  
924 if ( !$backend->check ) {
925 $backend->re_init;
926 }
927  
928 =cut
929  
930 6 ▶sub check {
Subroutine check: called CC=3 SCAR=14.7
Pod documented
931 6 my ( $self, %opts ) = @_;
932  
933 6 $self->errorblank;
934  
935 6 my $url = $self->_probe_url;
936  
937 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
938 6 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
939 6 return 1;
940 }
941  
942 0 local $@;
943 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or return 0 0 0 0
944  
945 0 return 1;
946 } ## end sub check
947  
948 =head2 flush
949  
950 Removes all currently banned IPs at once by rendering the alias with an
951 empty membership, applying, and forgetting them. The alias and the rule
952 referencing it are left in place. This is the equivalent of fail2ban's
953 C<actionflush>.
954  
955 $backend->flush;
956  
957 =cut
958  
959 0 ▶sub flush {
Subroutine flush: not called CC=4 SCAR=30.0
Pod documented
960 0 my ( $self, %opts ) = @_;
961  
962 0 $self->errorblank;
963  
964 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
965 0 $self->{error} = 1;
966 0 $self->{errorString} = 'backend has not been inited';
967 0 $self->warn;
968 0 return;
969 }
970  
971 0 my $body = $self->_render( [] );
972  
973 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
974 $self->{frontend_obj}->{test_data} = [
975 0 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
976 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
977 ];
978 } else {
979 0 local $@;
980 eval {
981 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
982 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
983 0 1;
984 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PATCH", $self->_base . '/api/v2/firewall/alias', $body); $self->_request("POST", $self->_base . '/api/v2/firewall/apply', ""); 1 } } or do { $self->{"error"} = 25; $self->{"errorString"} = 'flush failed clearing the alias... ' . $@; $self->warn } 0 0 0
985 0 $self->{error} = 25;
986 0 $self->{errorString} = 'flush failed clearing the alias... ' . $@;
987 0 $self->warn;
988 };
989 } ## end else [ if ( $self->{testing} )]
990  
991 0 $self->{banned} = {};
992 0 $self->{banned_cidr} = {};
993 } ## end sub flush
994  
995 =head1 ERROR CODES / FLAGS
996  
997 Error handling is provided by L<Error::Helper>. All
998 errors are considered fatal.
999  
1000 =head2 1, notInited
1001  
1002 The backend has not been inited yet.
1003  
1004 =head2 6, invalidPrefixSpecified
1005  
1006 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1007  
1008 =head2 7, invalidName
1009  
1010 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1011  
1012 =head2 8, optionsNotHash
1013  
1014 The item passed to new for options is not a hash.
1015  
1016 =head2 9, noBanItem
1017  
1018 No IP or CIDR range specified to ban or unban.
1019  
1020 =head2 10, banItemNotIP
1021  
1022 The item to ban is not an IP. Either wrong ref type or regexp
1023 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1024  
1025 =head2 12, backendInitError
1026  
1027 Failed to init the backend.
1028  
1029 =head2 13, banFailed
1030  
1031 Failed to ban the item.
1032  
1033 =head2 14, unbanFailed
1034  
1035 Failed to unban the item.
1036  
1037 =head2 15, listFailed
1038  
1039 Failed to get a list of bans.
1040  
1041 =head2 16, reInitFailed
1042  
1043 Failed to re_init the backend.
1044  
1045 =head2 17, teardownFailed
1046  
1047 Failed to teardown the backend.
1048  
1049 =head2 18, alreadyInited
1050  
1051 init called, but the backend has already been inited.
1052  
1053 =head2 23, initFailed
1054  
1055 init failed. Probing the alias failed.
1056  
1057 =head2 24, checkFailed
1058  
1059 The backend check raised an error.
1060  
1061 =head2 25, flushFailed
1062  
1063 Failed to flush the bans.
1064  
1065 =head2 26, portsNotSupported
1066  
1067 Ports were specified, but the backend does not support ports.
1068  
1069 =head2 27, protocolsNotSupported
1070  
1071 Protocols were specified, but the backend does not support protocols.
1072  
1073 =head2 30, hostNotDefined
1074  
1075 The option host is undef or blank.
1076  
1077 =head2 31, keyNotDefined
1078  
1079 The option key is undef or blank.
1080  
1081 =head2 32, banCidrFailed
1082  
1083 Failed to ban the CIDR range.
1084  
1085 =head2 33, unbanCidrFailed
1086  
1087 Failed to unban the CIDR range.
1088  
1089 =head2 34, cidrItemNotCidr
1090  
1091 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1092 IPv4 or IPv6 address followed by a prefix length valid for its family.
1093  
1094 =head2 35, cidrNotSupported
1095  
1096 The backend does not support CIDR bans.
1097  
1098 =head2 36, listCidrFailed
1099  
1100 Failed to get a list of CIDR bans.
1101  
1102 =head1 AUTHOR
1103  
1104 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1105  
1106 =head1 BUGS
1107  
1108 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1109 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
1110  
1111 =head1 LICENSE AND COPYRIGHT
1112  
1113 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1114  
1115 This is free software, licensed under:
1116  
1117 The GNU Lesser General Public License, Version 2.1, February 1999
1118  
1119  
1120 =cut
1121  
1122 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/netscaler.pm ↑ Summary BlockerHelper/backends/routeros_api.pm »