BlockerHelper/backends/opnsense.pm

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