BlockerHelper/backends/vyos.pm

filter: Statementstmt 31.8% 118 / 371 Branchbran 34.0% 49 / 144 Conditioncond 40.0% 12 / 30 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 69.2% 18 / 26 Podpod 100% 13 / 13 totaltotal 35.2% 212 / 602 CC 86 SCAR 37.2
CC
86
Cov
33%
new
77.8
_request
72.0
unban
43.8
CRAP 41.3
« BlockerHelper/backends/panos.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 qw( Error::Helper Net::Firewall::BlockerHelper::Util );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::vyos - VyOS backend using the HTTP API.
13  
14 =head1 VERSION
15  
16 Version 0.2.0
17  
18 =cut
19  
20 1 our $VERSION = '0.2.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => '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 5 ▶sub new {
Subroutine new: called CC=24 SCAR=43.5
Pod documented
97 5 my ( $blank, %opts ) = @_;
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 VyOS configuration path identifying one
237 # address's membership in the firewall group.
238 #
239 # The VyOS API addresses configuration by path, an ordered list of nodes
240 # walking down the configuration tree, rather than by URL. Setting or deleting
241 # that path is what adds or removes the address, so this one path serves both
242 # operations.
243 #
244 # The node differs by family, since VyOS keeps IPv4 and IPv6 in separate group
245 # types. Note the family test here matches against $IPv6_re and treats
246 # everything else as IPv4, the opposite way round to most of the dist, which
247 # tests for IPv4 and falls back to IPv6.
248 #
249 # Args:
250 #
251 # ip - The address whose path is wanted, as a plain string. Expected to be
252 # an already validated and lowercased IPv4 or IPv6 address.
253 #
254 # Returns the path as an arrayref of node strings, ready to be the path value
255 # of an operation. The group name comes from the group option.
256 #
257 # $self->_group_path('10.0.0.1');
258 # # [ 'firewall', 'group', 'address-group', 'kur_ssh', 'address', '10.0.0.1' ]
259 #
260 # $self->_group_path('2001:db8::1');
261 # # [ 'firewall', 'group', 'ipv6-address-group', 'kur_ssh', 'address', '2001:db8::1' ]
262 6 ▶sub _group_path {
Subroutine _group_path: called CC=2 SCAR=6.9
263 6 my ( $self, $ip ) = @_;
264  
265 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
266  
267 6 return [ 'firewall', 'group', $node, $self->{options}{group}, 'address', $ip ];
268 }
269  
270 # Internal helper. Builds the JSON operation describing a configuration change
271 # for one address, which is what the VyOS API's data parameter carries.
272 #
273 # Splitting this out from _form_body is what keeps the API key out of
274 # test_data. This half holds everything about the operation and nothing
275 # secret, so the testing paths record it directly; the key is only added by
276 # _form_body, which testing mode never calls. A test can therefore assert on
277 # exactly what would be configured without the recorded data carrying a
278 # credential.
279 #
280 # Args:
281 #
282 # op - The operation, as a plain string: 'set' to add the address to the
283 # group or 'delete' to remove it. Passed through to the API as is, so
284 # anything else would be rejected there rather than here.
285 #
286 # ip - The address to operate on, as a plain string. Expected to be an
287 # already validated and lowercased IPv4 or IPv6 address. The family
288 # selects the group node via _group_path.
289 #
290 # Returns the operation as a JSON string, ready to become the data value.
291 #
292 # $self->_op_json( 'set', '10.0.0.1' );
293 # # {"op":"set","path":["firewall","group","address-group","kur_ssh","address","10.0.0.1"]}
294 #
295 # $self->_op_json( 'delete', '10.0.0.1' );
296 # # {"op":"delete","path":["firewall","group","address-group","kur_ssh","address","10.0.0.1"]}
297 6 ▶sub _op_json {
Subroutine _op_json: called CC=1 SCAR=0.0
298 6 my ( $self, $op, $ip ) = @_;
299  
300 6 return $self->_json->encode( { op => $op, path => $self->_group_path($ip) } );
301 }
302  
303 # Internal helper. Builds the complete form encoded request body for a
304 # configuration change, the JSON operation plus the API key.
305 #
306 # The VyOS API takes both as form fields rather than the key going in a
307 # header, so the credential is part of the body. That is precisely why this is
308 # separate from _op_json and why testing mode never calls it: keeping the key
309 # confined to this sub is what stops it reaching test_data.
310 #
311 # Both values are percent encoded, which matters for the JSON in particular
312 # since it is full of braces, quotes, and colons.
313 #
314 # Args:
315 #
316 # op - The operation, as a plain string, 'set' or 'delete'. Passed
317 # straight to _op_json.
318 #
319 # ip - The address to operate on, as a plain string. Expected to be an
320 # already validated and lowercased IPv4 or IPv6 address.
321 #
322 # Returns the body as a form urlencoded string, ready to POST to _base_url.
323 #
324 # $self->_form_body( 'set', '10.0.0.1' );
325 # # data=%7B%22op%22%3A%22set%22%2C%22path%22%3A%5B...%5D%7D&key=secret
326 0 ▶sub _form_body {
Subroutine _form_body: not called CC=1 SCAR=6.9
327 0 my ( $self, $op, $ip ) = @_;
328  
329 return
330 'data='
331 . $self->_uri_escape( $self->_op_json( $op, $ip ) )
332 . '&key='
333 0 . $self->_uri_escape( $self->{options}{key} );
334 } ## end sub _form_body
335  
336 # Internal helper. Returns the URL of the VyOS HTTP API.
337 #
338 # There is a single endpoint. Everything the API does is decided by the
339 # operation in the posted body rather than by the path, so unlike the REST
340 # backends there is no URL to build per operation.
341 #
342 # The scheme is fixed at https rather than configurable, since the VyOS API is
343 # expected to be run over TLS.
344 #
345 # Takes no arguments; the host comes from the options.
346 #
347 # Returns the URL as a string, with no trailing slash.
348 #
349 # $self->_base_url; # https://vyos.example.org
350 16 ▶sub _base_url {
Subroutine _base_url: called CC=1 SCAR=0.0
351 16 my ($self) = @_;
352  
353 16 return 'https://' . $self->{options}{host};
354 }
355  
356 # Internal helper. Builds the JSON operation that reads back the firewall
357 # group's configuration, used by init and check.
358 #
359 # Since this backend creates nothing, the group existing is the whole of its
360 # setup, and a group deleted out from under a running process is what this
361 # detects. init runs it so a misconfigured group name fails immediately rather
362 # than at the first ban, and check runs it to decide whether the self heal
363 # path should re-push the current bans.
364 #
365 # The path stops at the group rather than descending to an address, and the op
366 # is showConfig rather than set or delete, so nothing is modified.
367 #
368 # Note the path is hardcoded to the IPv4 address-group node. An instance
369 # banning only IPv6 addresses would still have its health checked against the
370 # IPv4 group.
371 #
372 # As with _op_json, this holds nothing secret and is what the testing paths
373 # record; the key is only added by _retrieve_form_body.
374 #
375 # Takes no arguments; the group name comes from the options.
376 #
377 # Returns the operation as a JSON string, ready to become the data value.
378 #
379 # $self->_retrieve_body;
380 # # {"op":"showConfig","path":["firewall","group","address-group","kur_ssh"]}
381 10 ▶sub _retrieve_body {
Subroutine _retrieve_body: called CC=1 SCAR=0.0
382 10 my ($self) = @_;
383  
384 return $self->_json->encode(
385 10 { op => 'showConfig', path => [ 'firewall', 'group', 'address-group', $self->{options}{group} ] } );
386 }
387  
388 # Internal helper. Builds the complete form encoded request body for the
389 # configuration read, the retrieve operation plus the API key.
390 #
391 # This is to _retrieve_body what _form_body is to _op_json, and exists for the
392 # same reason: it is the only place the key is attached to a read, so testing
393 # mode can record the operation without the credential.
394 #
395 # Takes no arguments.
396 #
397 # Returns the body as a form urlencoded string, ready to POST to _base_url.
398 #
399 # $self->_retrieve_form_body;
400 # # data=%7B%22op%22%3A%22showConfig%22%2C%22path%22%3A%5B...%5D%7D&key=secret
401 0 ▶sub _retrieve_form_body {
Subroutine _retrieve_form_body: not called CC=1 SCAR=6.9
402 0 my ($self) = @_;
403  
404 return
405 'data='
406 . $self->_uri_escape( $self->_retrieve_body )
407 . '&key='
408 0 . $self->_uri_escape( $self->{options}{key} );
409 } ## end sub _retrieve_form_body
410  
411 # Internal helper. Performs one HTTP request against the VyOS API. Every call
412 # this backend makes to the router goes through here.
413 #
414 # The user agent is built on first use and cached on the object, so a run of
415 # requests shares one agent. LWP::UserAgent is loaded with require at that
416 # point rather than at compile time, since only the HTTP backends need it;
417 # failing to load it dies with an explanation naming this backend. The
418 # insecure option turns off certificate verification, which is there because a
419 # VyOS router commonly presents a self signed certificate.
420 #
421 # There is no authentication handling here. The API key travels in the form
422 # body rather than in a header, so it is already part of what the caller
423 # passes.
424 #
425 # Any HTTP level failure dies rather than setting an error. The callers wrap
426 # this in eval and turn the exception into the appropriate error code, which
427 # is what lets one helper serve paths that report ban, unban, and teardown
428 # failures differently.
429 #
430 # Never called in testing mode; those paths record the operation JSON instead,
431 # which is what keeps the API key out of test_data.
432 #
433 # Args:
434 #
435 # method - The HTTP method, as a plain string. In practice always 'POST',
436 # since the VyOS API exposes everything as a POST to one
437 # endpoint.
438 #
439 # url - The full URL to request, as a plain string, normally
440 # _base_url with the API path appended.
441 #
442 # body - The form urlencoded request body, as a plain string, from
443 # _form_body or _retrieve_form_body. Carries the API key.
444 #
445 # Returns the raw response body as a string. Dies on any non success HTTP
446 # status.
447 #
448 # $self->_request( 'POST', $url, $self->_form_body( 'set', $ip ) );
449 #
450 # # the usual shape at the call sites
451 # eval { $self->_request( 'POST', $url, $body ); };
452 # if ($@) { ... raise banFailed ... }
453 0 ▶sub _request {
Subroutine _request: not called CC=8 SCAR=42.8
454 0 my ( $self, $method, $url, $body ) = @_;
455  
456 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
457 0 local $@;
458 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
459 0 require LWP::UserAgent;
460 my %args = (
461 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
462 timeout => $self->{options}{timeout},
463 0 );
464 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
465 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
466 }
467 0 $self->{ua} = LWP::UserAgent->new(%args);
468 0 1;
469 } or die( 'failed to load LWP::UserAgent, which the vyos backend requires... ' . $@ );
470 }
471  
472 0 my @headers = ( 'Content-Type' => 'application/x-www-form-urlencoded' );
473  
474 0 require HTTP::Request;
475 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
476  
477 0 my $response = $self->{ua}->request($request);
478  
479 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
480 0 my $detail = $response->decoded_content;
481 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
482 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
483 }
484  
485 0 my $decoded;
486 0 my $content = $response->decoded_content;
487 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
488 0 local $@;
489 0 eval { $decoded = $self->_json->decode($content); };
490 }
491  
492 0 return $decoded;
493 } ## end sub _request
494  
495 =head2 init
496  
497 Initiates the backend. Verifies the credentials and reachability by POSTing
498 a C<showConfig> operation for the address-group to C<< /retrieve >>.
499 Nothing is created on the device; the referencing firewall rule must
500 already exist.
501  
502 $backend->init;
503  
504 =cut
505  
506 3 ▶sub init {
Subroutine init: called CC=4 SCAR=20.3
Pod documented
507 3 my ( $self, %opts ) = @_;
508  
509 3 $self->errorblank;
510  
511 3 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 3 3
512 0 $self->{error} = 18;
513 0 $self->{errorString} = 'backend has already been inited';
514 0 $self->warn;
515 }
516  
517 3 my $url = $self->_base_url . '/retrieve';
518  
519 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
520 $self->{frontend_obj}->{test_data}
521 3 = [ { method => 'POST', url => $url, content => $self->_retrieve_body } ];
522 } else {
523 0 local $@;
524 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
525 0 $self->{error} = 23;
526 0 $self->{errorString} = 'init failed. probing the address-group config failed... ' . $@;
527 0 $self->warn;
528 };
529 }
530  
531 3 $self->{inited} = 1;
532 } ## end sub init
533  
534 =head2 ban
535  
536 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
537 and lowercased, then a C<set> operation for
538 C<< firewall group address-group <group> address <ip> >> (or the
539 C<ipv6-address-group> equivalent) is POSTed to C<< /configure >>. Banning
540 an already banned IP is a noop.
541  
542 $backend->ban(ban => $ip);
543  
544 =cut
545  
546 3 ▶sub ban {
Subroutine ban: called CC=10 SCAR=37.2
Pod documented
547 3 my ( $self, %opts ) = @_;
548  
549 3 $self->errorblank;
550  
551 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
552 0 $self->{error} = 1;
553 0 $self->{errorString} = 'backend has not been inited';
554 0 $self->warn;
555 0 return;
556 }
557  
558 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
559 0 $self->{error} = 9;
560 0 $self->{errorString} = 'Nothing specified for the value ban';
561 0 $self->warn;
562 0 return;
563 } elsif ( ref( $opts{ban} ) ne '' ) {
564 0 $self->{error} = 10;
565 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
566 0 $self->warn;
567 0 return;
568 ▶ } 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
569 && $opts{ban} !~ /\A$IPv6_re\z/ )
570 {
571 0 $self->{error} = 10;
572 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
573 0 $self->warn;
574 0 return;
575 }
576  
577 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
578 3 $opts{ban} = lc( $opts{ban} );
579  
580 3 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 3 3
581 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
582 0 $self->{frontend_obj}->{test_data} = 'already banned';
583 }
584 0 return;
585 }
586  
587 3 my $url = $self->_base_url . '/configure';
588  
589 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
590 $self->{frontend_obj}->{test_data}
591 3 = [ { method => 'POST', url => $url, content => $self->_op_json( 'set', $opts{ban} ) } ];
592 } else {
593 0 local $@;
594 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
595 0 $self->{error} = 13;
596 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
597 0 $self->warn;
598 0 return;
599 };
600 }
601  
602 3 $self->{banned}{ $opts{ban} } = 1;
603 } ## end sub ban
604  
605 =head2 unban
606  
607 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
608 and lowercased, then a C<delete> operation for its address-group entry is
609 POSTed to C<< /configure >>. Unbanning an IP that is not banned is a noop.
610  
611 $backend->unban(ban => $ip);
612  
613 =cut
614  
615 1 ▶sub unban {
Subroutine unban: called CC=10 SCAR=37.8
Pod documented
616 1 my ( $self, %opts ) = @_;
617  
618 1 $self->errorblank;
619  
620 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
621 0 $self->{error} = 1;
622 0 $self->{errorString} = 'backend has not been inited';
623 0 $self->warn;
624 0 return;
625 }
626  
627 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
628 0 $self->{error} = 9;
629 0 $self->{errorString} = 'Nothing specified for the value ban';
630 0 $self->warn;
631 0 return;
632 } elsif ( ref( $opts{ban} ) ne '' ) {
633 0 $self->{error} = 10;
634 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
635 0 $self->warn;
636 0 return;
637 ▶ } 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
638 && $opts{ban} !~ /\A$IPv6_re\z/ )
639 {
640 0 $self->{error} = 10;
641 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
642 0 $self->warn;
643 0 return;
644 }
645  
646 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
647 1 $opts{ban} = lc( $opts{ban} );
648  
649 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
650 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
651 0 $self->{frontend_obj}->{test_data} = 'not banned';
652 }
653 0 return;
654 }
655  
656 1 my $url = $self->_base_url . '/configure';
657  
658 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
659 $self->{frontend_obj}->{test_data}
660 1 = [ { method => 'POST', url => $url, content => $self->_op_json( 'delete', $opts{ban} ) } ];
661 } else {
662 0 local $@;
663 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
664 0 $self->{error} = 14;
665 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
666 0 $self->warn;
667 0 return;
668 };
669 }
670  
671 1 delete( $self->{banned}{ $opts{ban} } );
672 } ## end sub unban
673  
674 =head2 ban_cidr
675  
676 Bans a CIDR range by POSTing a C<set> operation for it to C<< /configure >>,
677 adding it to the address-group in the same manner as a single IP. The value
678 of ban is validated as being a IPv4 or IPv6 CIDR range and lowercased.
679 Banning an already banned range is a noop.
680  
681 $backend->ban_cidr(ban => '1.2.3.0/24');
682  
683 =cut
684  
685 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=9 SCAR=36.0
Pod documented
686 1 my ( $self, %opts ) = @_;
687  
688 1 $self->errorblank;
689  
690 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
691 0 $self->{error} = 1;
692 0 $self->{errorString} = 'backend has not been inited';
693 0 $self->warn;
694 0 return;
695 }
696  
697 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
698 0 $self->{error} = 9;
699 0 $self->{errorString} = 'Nothing specified for the value ban';
700 0 $self->warn;
701 0 return;
702 } elsif ( ref( $opts{ban} ) ne '' ) {
703 0 $self->{error} = 34;
704 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
705 0 $self->warn;
706 0 return;
707 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
708 0 $self->{error} = 34;
709 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
710 0 $self->warn;
711 0 return;
712 }
713  
714 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
715 1 $opts{ban} = lc( $opts{ban} );
716  
717 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
718 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
719 0 $self->{frontend_obj}->{test_data} = 'already banned';
720 }
721 0 return;
722 }
723  
724 1 my $url = $self->_base_url . '/configure';
725  
726 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
727 $self->{frontend_obj}->{test_data}
728 1 = [ { method => 'POST', url => $url, content => $self->_op_json( 'set', $opts{ban} ) } ];
729 } else {
730 0 local $@;
731 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
732 0 $self->{error} = 32;
733 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
734 0 $self->warn;
735 0 return;
736 };
737 }
738  
739 1 $self->{banned_cidr}{ $opts{ban} } = 1;
740 } ## end sub ban_cidr
741  
742 =head2 unban_cidr
743  
744 Unbans a CIDR range by POSTing a C<delete> operation for its address-group
745 entry to C<< /configure >>. The value of ban is validated as being a IPv4
746 or IPv6 CIDR range and lowercased. Unbanning a range that is not banned is
747 a noop.
748  
749 $backend->unban_cidr(ban => '1.2.3.0/24');
750  
751 =cut
752  
753 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=9 SCAR=36.0
Pod documented
754 1 my ( $self, %opts ) = @_;
755  
756 1 $self->errorblank;
757  
758 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
759 0 $self->{error} = 1;
760 0 $self->{errorString} = 'backend has not been inited';
761 0 $self->warn;
762 0 return;
763 }
764  
765 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
766 0 $self->{error} = 9;
767 0 $self->{errorString} = 'Nothing specified for the value ban';
768 0 $self->warn;
769 0 return;
770 } elsif ( ref( $opts{ban} ) ne '' ) {
771 0 $self->{error} = 34;
772 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
773 0 $self->warn;
774 0 return;
775 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
776 0 $self->{error} = 34;
777 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
778 0 $self->warn;
779 0 return;
780 }
781  
782 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
783 1 $opts{ban} = lc( $opts{ban} );
784  
785 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
786 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
787 0 $self->{frontend_obj}->{test_data} = 'not banned';
788 }
789 0 return;
790 }
791  
792 1 my $url = $self->_base_url . '/configure';
793  
794 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
795 $self->{frontend_obj}->{test_data}
796 1 = [ { method => 'POST', url => $url, content => $self->_op_json( 'delete', $opts{ban} ) } ];
797 } else {
798 0 local $@;
799 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
800 0 $self->{error} = 33;
801 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
802 0 $self->warn;
803 0 return;
804 };
805 }
806  
807 1 delete( $self->{banned_cidr}{ $opts{ban} } );
808 } ## end sub unban_cidr
809  
810 =head2 list_cidr
811  
812 List banned CIDR ranges. Returns an array of the currently banned CIDR
813 ranges. Single IPs are not included; for those see L</list>.
814  
815 my @banned_cidrs = $backend->list_cidr;
816  
817 =cut
818  
819 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
820 2 my ( $self, %opts ) = @_;
821  
822 2 $self->errorblank;
823  
824 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
825 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
826 }
827  
828 2 return keys( %{ $self->{banned_cidr} } );
829 }
830  
831 =head2 list
832  
833 List banned IPs. Returns an array of the currently banned single IPs. CIDR
834 ranges are not included; for those see L</list_cidr>.
835  
836 my @banned = $backend->list;
837  
838 =cut
839  
840 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
841 0 my ( $self, %opts ) = @_;
842  
843 0 $self->errorblank;
844  
845 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
846 0 $self->{frontend_obj}->{test_data} = 'list';
847 }
848  
849 0 return keys( %{ $self->{banned} } );
850 }
851  
852 =head2 re_init
853  
854 Tears down and re-inits, then re-adds all previously added bans. teardown
855 is best effort, as a partially or fully wiped setup is what this needs to
856 recover from. Each retained single IP and CIDR ban is re-added via a C<set>
857 operation POSTed to C<< /configure >>.
858  
859 $backend->re_init;
860  
861 =cut
862  
863 0 ▶sub re_init {
Subroutine re_init: not called CC=5 SCAR=34.0
Pod documented
864 0 my ( $self, %opts ) = @_;
865  
866 0 $self->errorblank;
867  
868 # teardown is best effort here as a partially or fully wiped setup is
869 # exactly what re_init needs to recover from
870 {
871 0 local $@;
872 0 eval { $self->teardown; };
873 }
874 0 $self->init;
875  
876 0 my $url = $self->_base_url . '/configure';
877  
878 # single IPs and CIDR ranges are both re-added as address-group set ops
879 0 my @to_ban = ( sort( keys( %{ $self->{banned} } ) ), sort( keys( %{ $self->{banned_cidr} } ) ) );
880  
881 0 my @re_init_test_data;
882 0 foreach my $item (@to_ban) {
883 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
884 0 push( @re_init_test_data, { method => 'POST', url => $url, content => $self->_op_json( 'set', $item ) } );
885 } else {
886 0 local $@;
887 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
888 0 $self->{error} = 13;
889 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
890 0 $self->warn;
891 };
892 }
893 } ## end foreach my $item ( sort( keys...))
894  
895 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
896 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
897 }
898  
899 0 $self->{inited} = 1;
900 } ## end sub re_init
901  
902 =head2 teardown
903  
904 Tears down the setup by deleting the address-group entry for each currently
905 banned IP and CIDR range. The internal list of bans is kept, so a following
906 re_init will re-add them.
907  
908 =cut
909  
910 0 ▶sub teardown {
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
911 0 my ( $self, %opts ) = @_;
912  
913 0 $self->errorblank;
914  
915 0 $self->{inited} = 0;
916  
917 0 my $url = $self->_base_url . '/configure';
918  
919 0 my @requests;
920 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ), sort( keys( %{ $self->{banned_cidr} } ) ) ) {
921 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
922 0 push( @requests, { method => 'POST', url => $url, content => $self->_op_json( 'delete', $item ) } );
923 } else {
924 0 local $@;
925 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
926 0 $self->{error} = 17;
927 0 $self->{errorString} = 'teardown failed removing the entry for "' . $item . '"... ' . $@;
928 0 $self->warn;
929 };
930 }
931 } ## end foreach my $item ( sort( keys...))
932  
933 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
934 0 $self->{frontend_obj}->{test_data} = \@requests;
935 }
936 } ## end sub teardown
937  
938 =head2 stop
939  
940 Alias for L</teardown>.
941  
942 =cut
943  
944 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
945 0 my ( $self, %opts ) = @_;
946  
947 0 return $self->teardown(%opts);
948 }
949  
950 =head2 check
951  
952 Verifies the endpoint and credentials are still usable by retrieving the
953 address-group config. Returns a true value if so and a false value otherwise.
954  
955 =cut
956  
957 7 ▶sub check {
Subroutine check: called CC=3 SCAR=14.7
Pod documented
958 7 my ( $self, %opts ) = @_;
959  
960 7 $self->errorblank;
961  
962 7 my $url = $self->_base_url . '/retrieve';
963  
964 7 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 7 0 7
965 $self->{frontend_obj}->{test_data}
966 7 = [ { method => 'POST', url => $url, content => $self->_retrieve_body } ];
967 7 return 1;
968 }
969  
970 0 local $@;
971 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
972  
973 0 return 1;
974 } ## end sub check
975  
976 =head2 flush
977  
978 Removes all currently banned IPs and CIDR ranges at once by deleting their
979 address-group entries and forgetting them.
980  
981 $backend->flush;
982  
983 =cut
984  
985 0 ▶sub flush {
Subroutine flush: not called CC=6 SCAR=37.4
Pod documented
986 0 my ( $self, %opts ) = @_;
987  
988 0 $self->errorblank;
989  
990 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
991 0 $self->{error} = 1;
992 0 $self->{errorString} = 'backend has not been inited';
993 0 $self->warn;
994 0 return;
995 }
996  
997 0 my $url = $self->_base_url . '/configure';
998  
999 0 my @requests;
1000 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ), sort( keys( %{ $self->{banned_cidr} } ) ) ) {
1001 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1002 0 push( @requests, { method => 'POST', url => $url, content => $self->_op_json( 'delete', $item ) } );
1003 } else {
1004 0 local $@;
1005 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
1006 0 $self->{error} = 25;
1007 0 $self->{errorString} = 'flush failed removing the entry for "' . $item . '"... ' . $@;
1008 0 $self->warn;
1009 };
1010 }
1011 } ## end foreach my $item ( sort( keys...))
1012  
1013 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1014 0 $self->{frontend_obj}->{test_data} = \@requests;
1015 }
1016  
1017 0 $self->{banned} = {};
1018 0 $self->{banned_cidr} = {};
1019 } ## end sub flush
1020  
1021 =head1 ERROR CODES / FLAGS
1022  
1023 Error handling is provided by L<Error::Helper>. All
1024 errors are considered fatal.
1025  
1026 =head2 1, notInited
1027  
1028 The backend has not been inited yet.
1029  
1030 =head2 6, invalidPrefixSpecified
1031  
1032 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1033  
1034 =head2 7, invalidName
1035  
1036 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1037  
1038 =head2 8, optionsNotHash
1039  
1040 The item passed to new for options is not a hash.
1041  
1042 =head2 9, noBanItem
1043  
1044 No IP or CIDR range specified to ban or unban.
1045  
1046 =head2 10, banItemNotIP
1047  
1048 The item to ban is not an IP. Either wrong ref type or regexp
1049 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1050  
1051 =head2 12, backendInitError
1052  
1053 Failed to init the backend.
1054  
1055 =head2 13, banFailed
1056  
1057 Failed to ban the item.
1058  
1059 =head2 14, unbanFailed
1060  
1061 Failed to unban the item.
1062  
1063 =head2 15, listFailed
1064  
1065 Failed to get a list of bans.
1066  
1067 =head2 16, reInitFailed
1068  
1069 Failed to re_init the backend.
1070  
1071 =head2 17, teardownFailed
1072  
1073 Failed to teardown the backend.
1074  
1075 =head2 18, alreadyInited
1076  
1077 init called, but the backend has already been inited.
1078  
1079 =head2 23, initFailed
1080  
1081 init failed. Probing the address-group config failed.
1082  
1083 =head2 24, checkFailed
1084  
1085 The backend check raised an error.
1086  
1087 =head2 25, flushFailed
1088  
1089 Failed to flush the bans.
1090  
1091 =head2 26, portsNotSupported
1092  
1093 Ports were specified, but this backend manages whole IPs and does not
1094 support ports.
1095  
1096 =head2 27, protocolsNotSupported
1097  
1098 Protocols were specified, but this backend manages whole IPs and does not
1099 support protocols.
1100  
1101 =head2 30, hostNotDefined
1102  
1103 The option host is undef or blank.
1104  
1105 =head2 31, keyNotDefined
1106  
1107 The option key is undef or blank.
1108  
1109 =head2 32, banCidrFailed
1110  
1111 Failed to ban the CIDR range.
1112  
1113 =head2 33, unbanCidrFailed
1114  
1115 Failed to unban the CIDR range.
1116  
1117 =head2 34, cidrItemNotCidr
1118  
1119 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1120 IPv4 or IPv6 address followed by a prefix length valid for its family.
1121  
1122 =head2 35, cidrNotSupported
1123  
1124 The backend does not support CIDR bans.
1125  
1126 =head2 36, listCidrFailed
1127  
1128 Failed to get a list of CIDR bans.
1129  
1130 =head1 AUTHOR
1131  
1132 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1133  
1134 =head1 LICENSE AND COPYRIGHT
1135  
1136 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1137  
1138 This is free software, licensed under:
1139  
1140 The GNU Lesser General Public License, Version 2.1, February 1999
1141  
1142 =cut
1143  
1144 1 1; # End of Net::Firewall::BlockerHelper::backends::vyos
« BlockerHelper/backends/panos.pm ↑ Summary BlockerHelper/backends/abuseipdb.pm »