BlockerHelper/backends/pfsense.pm

filter: Statementstmt 34.6% 132 / 381 Branchbran 34.9% 51 / 146 Conditioncond 38.4% 15 / 39 MC/DCmcdc 12.5% 3 / 24 Subroutinesub 76.9% 20 / 26 Podpod 100% 13 / 13 totaltotal 37.2% 234 / 629 CC 87 SCAR 37.3
CC
87
Cov
35%
new
96.4
ban
45.3
unban
45.3
CRAP 41.8
« BlockerHelper/backends/shorewall.pm ↑ Summary BlockerHelper/backends/f5_bigip.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 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::pfsense - pfSense firewall alias backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 1 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => '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 sub new {
117 4 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=26 SCAR=45.7
Pod documented
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 base URL for the pfSense host.
268 sub _base {
269 20 ▶ my ($self) = @_;
Subroutine _base: called CC=1 SCAR=0.0
270  
271 20 return 'https://' . $self->{options}{host};
272 }
273  
274 # Internal helper. Minimal percent encoder so URI::Escape is not needed.
275 sub _uri_escape {
276 8 ▶ my ( $self, $string ) = @_;
Subroutine _uri_escape: called CC=1 SCAR=0.2
277  
278 8 $string =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
279  
280 8 return $string;
281 }
282  
283 # Internal helper. Returns a canonical JSON::PP encoder/decoder.
284 sub _json {
285 6 ▶ my ($self) = @_;
Subroutine _json: called CC=1 SCAR=0.0
286  
287 6 require JSON::PP;
288 6 return JSON::PP->new->canonical->utf8;
289 }
290  
291 # Internal helper. Returns the URL used to verify the alias exists.
292 sub _probe_url {
293 8 ▶ my ($self) = @_;
Subroutine _probe_url: called CC=1 SCAR=0.0
294  
295 8 return $self->_base . '/api/v2/firewall/alias?name=' . $self->_uri_escape( $self->{options}{alias} );
296 }
297  
298 # Internal helper. Renders the alias body from the current set of banned IPs.
299 # The address field is a sorted array of IP strings. An optional argument may
300 # be passed to render an explicit list of IPs instead of the current state,
301 # used by teardown to render an empty membership.
302 #
303 # my $body = $self->_render;
304 # my $body = $self->_render( [] );
305 sub _render {
306 6 ▶ my ( $self, $ips ) = @_;
Subroutine _render: called CC=2 SCAR=7.0
307  
308 6 ▶ if ( !defined($ips) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ips) 6 0 6
309 # the alias holds both single IPs and CIDR ranges
310 6 $ips = [ sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ];
311 }
312  
313 return $self->_json->encode(
314 {
315 id => $self->{options}{alias},
316 6 type => 'host',
317 address => $ips,
318 }
319 );
320 } ## end sub _render
321  
322 # Internal helper. Performs a HTTP request via LWP::UserAgent, returning the
323 # decoded JSON on success and dying with a explanation on any HTTP level
324 # failure. Never called in testing mode.
325 sub _request {
326 0 ▶ my ( $self, $method, $url, $body ) = @_;
Subroutine _request: not called CC=5 SCAR=34.0
327  
328 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
329 0 local $@;
330 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
331 0 require LWP::UserAgent;
332 my %ua_opts = (
333 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
334 timeout => $self->{options}{timeout},
335 0 );
336 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
337 0 $ua_opts{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
338 }
339 0 $self->{ua} = LWP::UserAgent->new(%ua_opts);
340 0 1;
341 } or die( 'failed to load LWP::UserAgent, which the pfsense backend requires... ' . $@ );
342 } ## end if ( !defined( $self->{ua...}))
343  
344 0 my @headers = ( 'Content-Type' => 'application/json', 'X-API-Key' => $self->{options}{key} );
345  
346 0 require HTTP::Request;
347 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
348  
349 0 my $response = $self->{ua}->request($request);
350  
351 0 my $decoded;
352 0 local $@;
353 0 eval { $decoded = $self->_json->decode( $response->decoded_content ); };
354  
355 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
356 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line );
357 }
358  
359 0 return $decoded;
360 } ## end sub _request
361  
362 =head2 init
363  
364 Initiates the backend. Verifies the alias exists by fetching it.
365  
366 No arguments are taken.
367  
368 May called a second time, it will error.
369  
370 $backend->init;
371  
372 =cut
373  
374 sub init {
375 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=20.3
Pod documented
376  
377 2 $self->errorblank;
378  
379 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
380 0 $self->{error} = 18;
381 0 $self->{errorString} = 'backend has already been inited';
382 0 $self->warn;
383 }
384  
385 2 my $url = $self->_probe_url;
386  
387 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
388 2 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
389 } else {
390 0 local $@;
391 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
392 0 $self->{error} = 23;
393 0 $self->{errorString} = 'init failed. probing the alias failed... ' . $@;
394 0 $self->warn;
395 };
396 }
397  
398 2 $self->{inited} = 1;
399 } ## end sub init
400  
401 =head2 ban
402  
403 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
404 and lowercased, then the full alias membership including it is rendered and
405 sent via a PATCH to C</api/v2/firewall/alias>, followed by a POST to
406 C</api/v2/firewall/apply>. If the API calls fail, the IP is removed from
407 the internal ban list again. Banning an already banned IP is a noop.
408  
409 $backend->ban(ban => $ip);
410  
411 =cut
412  
413 sub ban {
414 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=38.1
Pod documented
415  
416 2 $self->errorblank;
417  
418 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
419 0 $self->{error} = 1;
420 0 $self->{errorString} = 'backend has not been inited';
421 0 $self->warn;
422 0 return;
423 }
424  
425 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
426 0 $self->{error} = 9;
427 0 $self->{errorString} = 'Nothing specified for the value ban';
428 0 $self->warn;
429 0 return;
430 } elsif ( ref( $opts{ban} ) ne '' ) {
431 0 $self->{error} = 10;
432 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
433 0 $self->warn;
434 0 return;
435 ▶ } 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
436 && $opts{ban} !~ /\A$IPv6_re\z/ )
437 {
438 0 $self->{error} = 10;
439 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
440 0 $self->warn;
441 0 return;
442 }
443  
444 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
445 2 $opts{ban} = lc( $opts{ban} );
446  
447 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
448 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
449 0 $self->{frontend_obj}->{test_data} = 'already banned';
450 }
451 0 return;
452 }
453  
454 2 $self->{banned}{ $opts{ban} } = 1;
455  
456 2 my $body = $self->_render;
457  
458 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
459 $self->{frontend_obj}->{test_data} = [
460 2 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
461 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
462 ];
463 } else {
464 0 local $@;
465 eval {
466 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
467 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
468 0 1;
469 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
470 0 delete( $self->{banned}{ $opts{ban} } );
471 0 $self->{error} = 13;
472 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
473 0 $self->warn;
474 0 return;
475 };
476 } ## end else [ if ( $self->{testing} )]
477 } ## end sub ban
478  
479 =head2 unban
480  
481 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
482 and lowercased, then the alias membership is re-rendered without it and
483 sent via a PATCH to C</api/v2/firewall/alias>, followed by a POST to
484 C</api/v2/firewall/apply>. If the API calls fail, the IP is restored to the
485 internal ban list. Unbanning an IP that is not banned is a noop.
486  
487 $backend->unban(ban => $ip);
488  
489 =cut
490  
491 sub unban {
492 2 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=38.1
Pod documented
493  
494 2 $self->errorblank;
495  
496 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
497 0 $self->{error} = 1;
498 0 $self->{errorString} = 'backend has not been inited';
499 0 $self->warn;
500 0 return;
501 }
502  
503 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
504 0 $self->{error} = 9;
505 0 $self->{errorString} = 'Nothing specified for the value ban';
506 0 $self->warn;
507 0 return;
508 } elsif ( ref( $opts{ban} ) ne '' ) {
509 0 $self->{error} = 10;
510 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
511 0 $self->warn;
512 0 return;
513 ▶ } 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
514 && $opts{ban} !~ /\A$IPv6_re\z/ )
515 {
516 0 $self->{error} = 10;
517 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
518 0 $self->warn;
519 0 return;
520 }
521  
522 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
523 2 $opts{ban} = lc( $opts{ban} );
524  
525 2 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 2 2
526 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
527 0 $self->{frontend_obj}->{test_data} = 'not banned';
528 }
529 0 return;
530 }
531  
532 2 delete( $self->{banned}{ $opts{ban} } );
533  
534 2 my $body = $self->_render;
535  
536 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
537 $self->{frontend_obj}->{test_data} = [
538 2 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
539 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
540 ];
541 } else {
542 0 local $@;
543 eval {
544 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
545 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
546 0 1;
547 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
548 0 $self->{banned}{ $opts{ban} } = 1;
549 0 $self->{error} = 14;
550 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
551 0 $self->warn;
552 0 return;
553 };
554 } ## end else [ if ( $self->{testing} )]
555 } ## end sub unban
556  
557 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
558 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
559 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
560 # IPv6). Returns false otherwise.
561 sub _valid_cidr {
562 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
563  
564 2 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 2 2
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table33%1/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
565  
566 2 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 2 0 2
567 2 my ( $addr, $prefix ) = ( $1, $2 );
568 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
569 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::pfsense::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
570 }
571  
572 0 return 0;
573 } ## end sub _valid_cidr
574  
575 =head2 ban_cidr
576  
577 Bans a CIDR range by adding it to the alias membership and applying the
578 change via the same PATCH and apply calls used by L</ban>. A pfSense host
579 alias holds CIDR ranges in the same manner as single addresses. The value
580 of ban is validated as being a IPv4 or IPv6 CIDR range and lowercased.
581 Banning an already banned range is a noop.
582  
583 $backend->ban_cidr(ban => '1.2.3.0/24');
584  
585 =cut
586  
587 sub ban_cidr {
588 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=9 SCAR=36.3
Pod documented
589  
590 1 $self->errorblank;
591  
592 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
593 0 $self->{error} = 1;
594 0 $self->{errorString} = 'backend has not been inited';
595 0 $self->warn;
596 0 return;
597 }
598  
599 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
600 0 $self->{error} = 9;
601 0 $self->{errorString} = 'Nothing specified for the value ban';
602 0 $self->warn;
603 0 return;
604 } elsif ( ref( $opts{ban} ) ne '' ) {
605 0 $self->{error} = 34;
606 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
607 0 $self->warn;
608 0 return;
609 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
610 0 $self->{error} = 34;
611 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
612 0 $self->warn;
613 0 return;
614 }
615  
616 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
617 1 $opts{ban} = lc( $opts{ban} );
618  
619 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
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 1 $self->{banned_cidr}{ $opts{ban} } = 1;
627  
628 1 my $body = $self->_render;
629  
630 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
631 $self->{frontend_obj}->{test_data} = [
632 1 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
633 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
634 ];
635 } else {
636 0 local $@;
637 eval {
638 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
639 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
640 0 1;
641 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
642 0 delete( $self->{banned_cidr}{ $opts{ban} } );
643 0 $self->{error} = 32;
644 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
645 0 $self->warn;
646 0 return;
647 };
648 } ## end else [ if ( $self->{testing} )]
649 } ## end sub ban_cidr
650  
651 =head2 unban_cidr
652  
653 Unbans a CIDR range by removing it from the alias membership and applying
654 the change. The value of ban is validated as being a IPv4 or IPv6 CIDR
655 range and lowercased. Unbanning a range that is not banned is a noop.
656  
657 $backend->unban_cidr(ban => '1.2.3.0/24');
658  
659 =cut
660  
661 sub unban_cidr {
662 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=9 SCAR=36.3
Pod documented
663  
664 1 $self->errorblank;
665  
666 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
667 0 $self->{error} = 1;
668 0 $self->{errorString} = 'backend has not been inited';
669 0 $self->warn;
670 0 return;
671 }
672  
673 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
674 0 $self->{error} = 9;
675 0 $self->{errorString} = 'Nothing specified for the value ban';
676 0 $self->warn;
677 0 return;
678 } elsif ( ref( $opts{ban} ) ne '' ) {
679 0 $self->{error} = 34;
680 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
681 0 $self->warn;
682 0 return;
683 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
684 0 $self->{error} = 34;
685 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
686 0 $self->warn;
687 0 return;
688 }
689  
690 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
691 1 $opts{ban} = lc( $opts{ban} );
692  
693 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
694 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
695 0 $self->{frontend_obj}->{test_data} = 'not banned';
696 }
697 0 return;
698 }
699  
700 1 delete( $self->{banned_cidr}{ $opts{ban} } );
701  
702 1 my $body = $self->_render;
703  
704 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
705 $self->{frontend_obj}->{test_data} = [
706 1 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
707 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
708 ];
709 } else {
710 0 local $@;
711 eval {
712 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
713 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
714 0 1;
715 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
716 0 $self->{banned_cidr}{ $opts{ban} } = 1;
717 0 $self->{error} = 33;
718 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
719 0 $self->warn;
720 0 return;
721 };
722 } ## end else [ if ( $self->{testing} )]
723 } ## end sub unban_cidr
724  
725 =head2 list_cidr
726  
727 List banned CIDR ranges. Returns an array of the currently banned CIDR
728 ranges. Single IPs are not included; for those see L</list>.
729  
730 my @banned_cidrs = $backend->list_cidr;
731  
732 =cut
733  
734 sub list_cidr {
735 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
736  
737 2 $self->errorblank;
738  
739 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
740 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
741 }
742  
743 2 return keys( %{ $self->{banned_cidr} } );
744 }
745  
746 =head2 list
747  
748 List banned IPs. Returns an array of the currently banned single IPs. CIDR
749 ranges are not included; for those see L</list_cidr>.
750  
751 my @banned = $backend->list;
752  
753 =cut
754  
755 sub list {
756 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
757  
758 0 $self->errorblank;
759  
760 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
761 0 $self->{frontend_obj}->{test_data} = 'list';
762 }
763  
764 0 return keys( %{ $self->{banned} } );
765 }
766  
767 =head2 re_init
768  
769 Tells the backend to re-init it's self.
770  
771 This will call teardown and init again. After that it will re-add all
772 previously added bans by rendering the full alias membership and applying.
773  
774 $backend->re_init;
775  
776 =cut
777  
778 sub re_init {
779 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=4 SCAR=30.0
Pod documented
780  
781 0 $self->errorblank;
782  
783 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
784 0 $self->{error} = 1;
785 0 $self->{errorString} = 'backend has not been inited';
786 0 $self->warn;
787 0 return;
788 }
789  
790 # teardown is best effort here as a partially or fully wiped setup is
791 # exactly what re_init needs to recover from; init cleans up any remnants
792 {
793 0 local $@;
794 0 eval { $self->teardown; };
795 }
796 0 $self->init;
797  
798 0 my $body = $self->_render;
799  
800 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
801 $self->{frontend_obj}->{test_data} = [
802 0 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
803 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
804 ];
805 } else {
806 0 local $@;
807 eval {
808 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
809 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
810 0 1;
811 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
812 0 $self->{error} = 16;
813 0 $self->{errorString} = 're_init failed re-adding the bans... ' . $@;
814 0 $self->warn;
815 };
816 } ## end else [ if ( $self->{testing} )]
817  
818 0 $self->{inited} = 1;
819 } ## end sub re_init
820  
821 =head2 teardown
822  
823 Tears down the setup for the backend by rendering the alias with an empty
824 membership and applying. The internal list of bans is kept, so a following
825 re_init will re-add them. The alias itself and the rule referencing it are
826 left in place.
827  
828 $backend->teardown;
829  
830 =cut
831  
832 sub teardown {
833 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=3 SCAR=24.8
Pod documented
834  
835 0 $self->errorblank;
836  
837 0 $self->{inited} = 0;
838  
839 0 my $body = $self->_render( [] );
840  
841 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
842 $self->{frontend_obj}->{test_data} = [
843 0 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
844 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
845 ];
846 } else {
847 0 local $@;
848 eval {
849 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
850 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
851 0 1;
852 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
853 0 $self->{error} = 17;
854 0 $self->{errorString} = 'teardown failed clearing the alias... ' . $@;
855 0 $self->warn;
856 };
857 } ## end else [ if ( $self->{testing} )]
858 } ## end sub teardown
859  
860 =head2 stop
861  
862 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
863 concept.
864  
865 $backend->stop;
866  
867 =cut
868  
869 sub stop {
870 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
871  
872 0 return $self->teardown(%opts);
873 }
874  
875 =head2 check
876  
877 Verifies the alias is still reachable by fetching it. Returns a true value
878 if so and a false value otherwise. This is the equivalent of fail2ban's
879 C<actioncheck>.
880  
881 if ( !$backend->check ) {
882 $backend->re_init;
883 }
884  
885 =cut
886  
887 sub check {
888 6 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=14.7
Pod documented
889  
890 6 $self->errorblank;
891  
892 6 my $url = $self->_probe_url;
893  
894 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
895 6 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
896 6 return 1;
897 }
898  
899 0 local $@;
900 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
901  
902 0 return 1;
903 } ## end sub check
904  
905 =head2 flush
906  
907 Removes all currently banned IPs at once by rendering the alias with an
908 empty membership, applying, and forgetting them. The alias and the rule
909 referencing it are left in place. This is the equivalent of fail2ban's
910 C<actionflush>.
911  
912 $backend->flush;
913  
914 =cut
915  
916 sub flush {
917 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=4 SCAR=30.0
Pod documented
918  
919 0 $self->errorblank;
920  
921 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
922 0 $self->{error} = 1;
923 0 $self->{errorString} = 'backend has not been inited';
924 0 $self->warn;
925 0 return;
926 }
927  
928 0 my $body = $self->_render( [] );
929  
930 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
931 $self->{frontend_obj}->{test_data} = [
932 0 { method => 'PATCH', url => $self->_base . '/api/v2/firewall/alias', content => $body },
933 { method => 'POST', url => $self->_base . '/api/v2/firewall/apply' },
934 ];
935 } else {
936 0 local $@;
937 eval {
938 0 $self->_request( 'PATCH', $self->_base . '/api/v2/firewall/alias', $body );
939 0 $self->_request( 'POST', $self->_base . '/api/v2/firewall/apply', '' );
940 0 1;
941 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
942 0 $self->{error} = 25;
943 0 $self->{errorString} = 'flush failed clearing the alias... ' . $@;
944 0 $self->warn;
945 };
946 } ## end else [ if ( $self->{testing} )]
947  
948 0 $self->{banned} = {};
949 0 $self->{banned_cidr} = {};
950 } ## end sub flush
951  
952 =head1 ERROR CODES / FLAGS
953  
954 Error handling is provided by L<Error::Helper>. All
955 errors are considered fatal.
956  
957 =head2 1, notInited
958  
959 The backend has not been inited yet.
960  
961 =head2 6, invalidPrefixSpecified
962  
963 The specified prefix did not match /^[a-zA-Z0-9]+$/.
964  
965 =head2 7, invalidName
966  
967 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
968  
969 =head2 8, optionsNotHash
970  
971 The item passed to new for options is not a hash.
972  
973 =head2 9, noBanItem
974  
975 No IP or CIDR range specified to ban or unban.
976  
977 =head2 10, banItemNotIP
978  
979 The item to ban is not an IP. Either wrong ref type or regexp
980 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
981  
982 =head2 12, backendInitError
983  
984 Failed to init the backend.
985  
986 =head2 13, banFailed
987  
988 Failed to ban the item.
989  
990 =head2 14, unbanFailed
991  
992 Failed to unban the item.
993  
994 =head2 15, listFailed
995  
996 Failed to get a list of bans.
997  
998 =head2 16, reInitFailed
999  
1000 Failed to re_init the backend.
1001  
1002 =head2 17, teardownFailed
1003  
1004 Failed to teardown the backend.
1005  
1006 =head2 18, alreadyInited
1007  
1008 init called, but the backend has already been inited.
1009  
1010 =head2 23, initFailed
1011  
1012 init failed. Probing the alias failed.
1013  
1014 =head2 24, checkFailed
1015  
1016 The backend check raised an error.
1017  
1018 =head2 25, flushFailed
1019  
1020 Failed to flush the bans.
1021  
1022 =head2 26, portsNotSupported
1023  
1024 Ports were specified, but the backend does not support ports.
1025  
1026 =head2 27, protocolsNotSupported
1027  
1028 Protocols were specified, but the backend does not support protocols.
1029  
1030 =head2 30, hostNotDefined
1031  
1032 The option host is undef or blank.
1033  
1034 =head2 31, keyNotDefined
1035  
1036 The option key is undef or blank.
1037  
1038 =head2 32, banCidrFailed
1039  
1040 Failed to ban the CIDR range.
1041  
1042 =head2 33, unbanCidrFailed
1043  
1044 Failed to unban the CIDR range.
1045  
1046 =head2 34, cidrItemNotCidr
1047  
1048 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1049 IPv4 or IPv6 address followed by a prefix length valid for its family.
1050  
1051 =head2 35, cidrNotSupported
1052  
1053 The backend does not support CIDR bans.
1054  
1055 =head2 36, listCidrFailed
1056  
1057 Failed to get a list of CIDR bans.
1058  
1059 =head1 AUTHOR
1060  
1061 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1062  
1063 =head1 BUGS
1064  
1065 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1066 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
1067  
1068 =head1 LICENSE AND COPYRIGHT
1069  
1070 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1071  
1072 This is free software, licensed under:
1073  
1074 The GNU Lesser General Public License, Version 2.1, February 1999
1075  
1076  
1077 =cut
1078  
1079 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/shorewall.pm ↑ Summary BlockerHelper/backends/f5_bigip.pm »