BlockerHelper/backends/vyos.pm

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