BlockerHelper/backends/juniper_srx.pm

filter: Statementstmt 33.6% 137 / 407 Branchbran 33.3% 52 / 156 Conditioncond 40.4% 17 / 42 MC/DCmcdc 11.5% 3 / 26 Subroutinesub 78.5% 22 / 28 Podpod 100% 13 / 13 totaltotal 36.3% 244 / 672 CC 97 SCAR 37.7
CC
97
Cov
34%
re_init
90.0
new
82.4
unban
44.6
CRAP 43.5
« BlockerHelper/backends/routeros_api.pm ↑ Summary BlockerHelper/backends/shorewall.pm »
1 package Net::Firewall::BlockerHelper::backends::juniper_srx;
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::juniper_srx - Juniper SRX backend via the Junos REST 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 => 'juniper_srx',
28 name => 'ssh',
29 options => {
30 host => 'srx.example.org',
31 user => 'blocker',
32 password => $password,
33 address_set => 'blocklist',
34 },
35 );
36  
37 $fw_helper->init_backend;
38 $fw_helper->ban( ban => '1.2.3.4' );
39 $fw_helper->unban( ban => '1.2.3.4' );
40  
41 =head1 DESCRIPTION
42  
43 Blocks IPs on a Juniper SRX by managing entries in a global address book and a
44 global address-set through the Junos REST API (the C</rpc> endpoint, which
45 takes XML RPCs). A security policy referencing the address-set must already
46 exist on the device.
47  
48 For each banned IP an address book entry is created and added to the
49 configured address-set. Changes are applied by loading the configuration in
50 set format and then committing it, both as POSTs to C<< https://<host>/rpc >>.
51  
52 Auth is via HTTP basic auth (the C<user> and C<password> options).
53  
54 Blocking is per IP; ports and protocols belong on the referencing policy, so
55 specifying them here is an error.
56  
57 L<LWP::UserAgent> is loaded at run time, so it is only required when this
58 backend is actually used. For https, L<LWP::Protocol::https> must be present as
59 well.
60  
61 =head1 METHODS
62  
63 =head2 new
64  
65 - options :: Backend specific options. See below.
66 - prefix :: Prefix to use. Must match /^[a-zA-Z0-9]+$/. Default kur.
67 - name :: Name of this instance. Required.
68  
69 Ports and protocols are not supported and specifying either is an error.
70  
71 The options hash accepts the following.
72  
73 - host :: SRX host, optionally host:port. Required.
74 - Default :: undef
75  
76 - user :: REST API user for basic auth. Required.
77 - Default :: undef
78  
79 - password :: REST API password for basic auth. Required.
80 - Default :: undef
81  
82 - address_set :: Name of the global address-set a security policy
83 references and that banned IPs are added to.
84 - Default :: <prefix>_<name>
85  
86 - insecure :: If true, skip TLS certificate verification.
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 4 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=26 SCAR=44.1
Pod documented
98  
99 4 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 => 'userNotDefined',
131 32 => 'passwordNotDefined',
132 33 => 'banCidrFailed',
133 34 => 'unbanCidrFailed',
134 35 => 'cidrItemNotCidr',
135 36 => 'cidrNotSupported',
136 37 => 'listCidrFailed',
137 },
138 fatal_flags => {},
139 perror_not_fatal => 0,
140 },
141 options => {},
142 ports => [],
143 protocols => [],
144 testing => undef,
145 test_data => undef,
146 prefix => 'kur',
147 name => undef,
148 frontend_obj => undef,
149 inited => 0,
150 banned => {},
151 banned_cidr => {},
152 cidr_supported => 1,
153 ua => undef,
154 };
155 4 bless $self;
156  
157 # blocking is per IP; ports/protocols belong on the referencing policy
158 4 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) eq 'ARRAY' && defined( $opts{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]) 0 4 4
Condition33%2/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
040
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/3
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
defined $opts{"ports"} ref $opts{"ports"} eq "ARRAY" defined $opts{"ports"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"ports"}B= ref $opts{"ports"} eq "ARRAY"C= defined $opts{"ports"}[0]
ABCresult
0XX0
10X0
1100
1111
159 0 $self->{perror} = 1;
160 0 $self->{error} = 26;
161 0 $self->{errorString} = 'the juniper_srx backend blocks whole IPs and does not support ports';
162 0 $self->warn;
163 }
164 4 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) eq 'ARRAY' && defined( $opts{protocols}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]) 0 4 4
Condition33%2/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
040
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
defined $opts{"protocols"} ref $opts{"protocols"} eq "ARRAY" defined $opts{"protocols"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"protocols"}B= ref $opts{"protocols"} eq "ARRAY"C= defined $opts{"protocols"}[0]
ABCresult
0XX0
10X0
1100
1111
165 0 $self->{perror} = 1;
166 0 $self->{error} = 27;
167 0 $self->{errorString} = 'the juniper_srx backend blocks whole IPs and does not support protocols';
168 0 $self->warn;
169 }
170  
171 # make sure prefix is sane if defined
172 4 ▶ if ( defined( $opts{prefix} ) && $opts{prefix} !~ /^[a-zA-Z0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'prefix'} and not $opts{'prefix'} =~ /^[a-zA-Z0-9]+$/) { } 0 4 4
elsif (defined $opts{'prefix'}) { } 4 0 4
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
040
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
173 0 $self->{perror} = 1;
174 0 $self->{error} = 6;
175 $self->{errorString}
176 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
177 0 $self->warn;
178 } elsif ( defined( $opts{prefix} ) ) {
179 4 $self->{prefix} = $opts{prefix};
180 }
181  
182 # make sure we have a name and that it is valid
183 4 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 4 4
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 4 4
184 0 $self->{perror} = 1;
185 0 $self->{error} = 7;
186 0 $self->{errorString} = 'name is undef';
187 0 $self->warn;
188 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
189 0 $self->{perror} = 1;
190 0 $self->{error} = 7;
191 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
192 0 $self->warn;
193 }
194 4 $self->{name} = $opts{name};
195  
196 4 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 4 0 4
197 4 $self->{testing} = $opts{testing};
198 }
199 4 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 4 0 4
200 4 $self->{frontend_obj} = $opts{frontend_obj};
201 }
202  
203 4 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 4 0 4
204 4 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 4 4
205 0 $self->{perror} = 1;
206 0 $self->{error} = 8;
207 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
208 0 $self->warn;
209 }
210 4 $self->{options} = $opts{options};
211 }
212  
213 4 ▶ if ( !defined( $self->{options}{host} ) || $self->{options}{host} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq "") 1 3 4
Condition66%2/3
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
not defined $self->{"options"}{"host"} $self->{"options"}{"host"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"host"}B= $self->{"options"}{"host"} eq ""
ABresult
1X1
011
000
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 3 ▶ if ( !defined( $self->{options}{user} ) || $self->{options}{user} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq ""
not defined $self->{"options"}{"user"} $self->{"options"}{"user"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"user"}B= $self->{"options"}{"user"} eq ""
ABresult
1X1
011
000
220 1 $self->{perror} = 1;
221 1 $self->{error} = 31;
222 1 $self->{errorString} = 'the option user is undef or blank';
223 1 $self->warn;
224 }
225 2 ▶ if ( !defined( $self->{options}{password} ) || $self->{options}{password} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq "") 1 1 2
Condition66%2/3
not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq ""
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq ""
not defined $self->{"options"}{"password"} $self->{"options"}{"password"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"password"}B= $self->{"options"}{"password"} eq ""
ABresult
1X1
011
000
226 1 $self->{perror} = 1;
227 1 $self->{error} = 32;
228 1 $self->{errorString} = 'the option password is undef or blank';
229 1 $self->warn;
230 }
231  
232 # defaults
233 1 ▶ $self->{options}{insecure} = 0 if ( !defined( $self->{options}{insecure} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"insecure"} 1 0 1
234 1 ▶ $self->{options}{timeout} = 30 if ( !defined( $self->{options}{timeout} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"timeout"} 1 0 1
235 1 ▶ if ( defined( $self->{name} ) && !defined( $self->{options}{address_set} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"name"} and not defined $self->{"options"}{"address_set"}) 1 0 1
Condition33%1/3
defined $self->{"name"} and not defined $self->{"options"}{"address_set"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->{"name"} and not defined $self->{"options"}{"address_set"}
defined $self->{"name"} not defined $self->{"options"}{"address_set"}
Truth table33%1/3
A and not(B)
A= defined $self->{"name"}B= not defined $self->{"options"}{"address_set"}
ABresult
0X0
100
111
236 1 $self->{options}{address_set} = $self->{prefix} . '_' . $self->{name};
237 }
238  
239 1 return $self;
240 } ## end sub new
241  
242 # Internal helper. Returns the Junos REST API RPC endpoint.
243 sub _url {
244 16 ▶ my ($self) = @_;
Subroutine _url: called CC=1 SCAR=0.0
245  
246 16 return 'https://' . $self->{options}{host} . '/rpc';
247 }
248  
249 # Internal helper. Returns the address book object name used for the passed IP
250 # or CIDR, built as <prefix>_<name>_<ip> with dots, colons, and slashes
251 # replaced by '-' so it is a valid Junos identifier.
252 sub _obj_name {
253 5 ▶ my ( $self, $ip ) = @_;
Subroutine _obj_name: called CC=1 SCAR=0.0
254  
255 5 my $obj = $self->{prefix} . '_' . $self->{name} . '_' . $ip;
256 5 $obj =~ s/[.:\/]/-/g;
257  
258 5 return $obj;
259 }
260  
261 # Internal helper. Wraps the passed set command lines (an arrayref) in a
262 # load-configuration RPC in set/text format.
263 sub _load_body {
264 5 ▶ my ( $self, $setlines ) = @_;
Subroutine _load_body: called CC=1 SCAR=0.0
265  
266 return
267 '<load-configuration action="set" format="text"><configuration-set>'
268 5 . join( "\n", @{$setlines} )
269 . '</configuration-set></load-configuration>';
270 }
271  
272 # Internal helper. Returns the commit-configuration RPC.
273 sub _commit_body {
274 5 ▶ my ($self) = @_;
Subroutine _commit_body: called CC=1 SCAR=0.0
275  
276 5 return '<commit-configuration/>';
277 }
278  
279 # Internal helper. Returns the set command lines (arrayref) that add the passed
280 # IP to the global address book and address-set.
281 sub _ban_setlines {
282 2 ▶ my ( $self, $ip ) = @_;
Subroutine _ban_setlines: called CC=2 SCAR=6.9
283  
284 2 my $obj = $self->_obj_name($ip);
285 2 ▶ my $mask = ( $ip =~ /\A$IPv4_re\z/ ) ? '32' : '128';
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ ? : 1 1 2
286  
287 return [
288 'set security address-book global address ' . $obj . ' ' . $ip . '/' . $mask,
289 2 'set security address-book global address-set ' . $self->{options}{address_set} . ' address ' . $obj,
290 ];
291 } ## end sub _ban_setlines
292  
293 # Internal helper. Returns the set command lines (arrayref) that delete the
294 # passed IP's address book object. Deleting the address also removes it from the
295 # address-set.
296 sub _unban_setlines {
297 2 ▶ my ( $self, $ip ) = @_;
Subroutine _unban_setlines: called CC=1 SCAR=0.0
298  
299 2 return [ 'delete security address-book global address ' . $self->_obj_name($ip) ];
300 }
301  
302 # Internal helper. POSTs the passed XML RPC body to the Junos REST API using
303 # HTTP basic auth, returning the raw response body on success and dying
304 # otherwise. Never called in testing mode.
305 sub _request {
306 0 ▶ my ( $self, $body ) = @_;
Subroutine _request: not called CC=6 SCAR=37.4
307  
308 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
309 0 local $@;
310 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent; my(%args) = ("agent", 'Net::Firewall::BlockerHelper/' . $Net::Firewall::BlockerHelper::backends::juniper_srx::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 juniper_srx backend requires... ' . $@ 0 0 0
311 0 require LWP::UserAgent;
312 my %args = (
313 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
314 timeout => $self->{options}{timeout},
315 0 );
316 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
317 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
318 }
319 0 $self->{ua} = LWP::UserAgent->new(%args);
320 0 1;
321 } or die( 'failed to load LWP::UserAgent, which the juniper_srx backend requires... ' . $@ );
322 }
323  
324 0 require MIME::Base64;
325 my $auth = 'Basic '
326 0 . MIME::Base64::encode_base64( $self->{options}{user} . ':' . $self->{options}{password}, '' );
327  
328 0 my @headers = ( 'Content-Type' => 'application/xml', 'Authorization' => $auth );
329  
330 0 require HTTP::Request;
331 0 my $request = HTTP::Request->new( 'POST', $self->_url, \@headers, $body );
332  
333 0 my $response = $self->{ua}->request($request);
334  
335 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
336 0 my $detail = $response->decoded_content;
337 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
338 0 die( 'POST ' . $self->_url . ' failed... HTTP status... ' . $response->status_line . $detail );
339 }
340  
341 0 return $response->decoded_content;
342 } ## end sub _request
343  
344 =head2 init
345  
346 Initiates the backend. Verifies the credentials and connectivity with a
347 get-software-information RPC.
348  
349 =cut
350  
351 sub init {
352 1 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=20.9
Pod documented
353  
354 1 $self->errorblank;
355  
356 1 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 1 1
357 0 $self->{error} = 18;
358 0 $self->{errorString} = 'backend has already been inited';
359 0 $self->warn;
360 }
361  
362 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
363 $self->{frontend_obj}->{test_data}
364 1 = [ { method => 'POST', url => $self->_url, content => '<get-software-information/>' } ];
365 } else {
366 0 local $@;
367 0 ▶ eval { $self->_request('<get-software-information/>'); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("<get-software-information/>"); 1 } } or do { $self->{"error"} = 23; $self->{"errorString"} = 'init failed. probing the API failed... ' . $@; $self->warn } 0 0 0
368 0 $self->{error} = 23;
369 0 $self->{errorString} = 'init failed. probing the API failed... ' . $@;
370 0 $self->warn;
371 };
372 }
373  
374 1 $self->{inited} = 1;
375 } ## end sub init
376  
377 =head2 ban
378  
379 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address and
380 lowercased. A global address book entry named C<< <prefix>_<name>_<ip> >>
381 (dots and colons replaced with '-') is created with a /32 or /128 mask as
382 appropriate and added to the configured address-set, via a load-configuration
383 RPC in set format followed by a commit-configuration RPC, both POSTed to the
384 REST API. Banning an already banned IP is a noop.
385  
386 $backend->ban(ban => $ip);
387  
388 =cut
389  
390 sub ban {
391 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=37.4
Pod documented
392  
393 2 $self->errorblank;
394  
395 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
396 0 $self->{error} = 1;
397 0 $self->{errorString} = 'backend has not been inited';
398 0 $self->warn;
399 0 return;
400 }
401  
402 2 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 2 2
elsif (ref $opts{'ban'} ne '') { } 0 2 2
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/) { } 0 2 2
403 0 $self->{error} = 9;
404 0 $self->{errorString} = 'Nothing specified for the value ban';
405 0 $self->warn;
406 0 return;
407 } elsif ( ref( $opts{ban} ) ne '' ) {
408 0 $self->{error} = 10;
409 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
410 0 $self->warn;
411 0 return;
412 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/
l!l&&r!l&&!r
110
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/
ABresult
1X1
011
000
413 && $opts{ban} !~ /\A$IPv6_re\z/ )
414 {
415 0 $self->{error} = 10;
416 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
417 0 $self->warn;
418 0 return;
419 }
420  
421 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
422 2 $opts{ban} = lc( $opts{ban} );
423  
424 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
425 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
426 0 $self->{frontend_obj}->{test_data} = 'already banned';
427 }
428 0 return;
429 }
430  
431 2 my $setlines = $self->_ban_setlines( $opts{ban} );
432  
433 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
434 $self->{frontend_obj}->{test_data} = [
435 2 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
436 { method => 'POST', url => $self->_url, content => $self->_commit_body },
437 ];
438 } else {
439 0 local $@;
440 eval {
441 0 $self->_request( $self->_load_body($setlines) );
442 0 $self->_request( $self->_commit_body );
443 0 1;
444 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_load_body($setlines)); $self->_request($self->_commit_body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
445 0 $self->{error} = 13;
446 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
447 0 $self->warn;
448 0 return;
449 };
450 }
451  
452 2 $self->{banned}{ $opts{ban} } = 1;
453 } ## end sub ban
454  
455 =head2 unban
456  
457 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
458 and lowercased. The IP's address book object is deleted via a
459 load-configuration RPC followed by a commit-configuration RPC; deleting the
460 address also removes it from the address-set. Unbanning an IP that is not
461 banned is a noop.
462  
463 $backend->unban(ban => $ip);
464  
465 =cut
466  
467 sub unban {
468 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=38.0
Pod documented
469  
470 1 $self->errorblank;
471  
472 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
473 0 $self->{error} = 1;
474 0 $self->{errorString} = 'backend has not been inited';
475 0 $self->warn;
476 0 return;
477 }
478  
479 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::juniper_srx::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/) { } 0 1 1
480 0 $self->{error} = 9;
481 0 $self->{errorString} = 'Nothing specified for the value ban';
482 0 $self->warn;
483 0 return;
484 } elsif ( ref( $opts{ban} ) ne '' ) {
485 0 $self->{error} = 10;
486 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
487 0 $self->warn;
488 0 return;
489 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/
ABresult
1X1
011
000
490 && $opts{ban} !~ /\A$IPv6_re\z/ )
491 {
492 0 $self->{error} = 10;
493 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
494 0 $self->warn;
495 0 return;
496 }
497  
498 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
499 1 $opts{ban} = lc( $opts{ban} );
500  
501 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
502 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
503 0 $self->{frontend_obj}->{test_data} = 'not banned';
504 }
505 0 return;
506 }
507  
508 1 my $setlines = $self->_unban_setlines( $opts{ban} );
509  
510 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
511 $self->{frontend_obj}->{test_data} = [
512 1 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
513 { method => 'POST', url => $self->_url, content => $self->_commit_body },
514 ];
515 } else {
516 0 local $@;
517 eval {
518 0 $self->_request( $self->_load_body($setlines) );
519 0 $self->_request( $self->_commit_body );
520 0 1;
521 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_load_body($setlines)); $self->_request($self->_commit_body); 1 } } or do { $self->{"error"} = 14; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
522 0 $self->{error} = 14;
523 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
524 0 $self->warn;
525 0 return;
526 };
527 }
528  
529 1 delete( $self->{banned}{ $opts{ban} } );
530 } ## end sub unban
531  
532 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
533 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
534 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
535 # IPv6). Returns false otherwise.
536 sub _valid_cidr {
537 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
538  
539 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
540  
541 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
542 2 my ( $addr, $prefix ) = ( $1, $2 );
543 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
544 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::juniper_srx::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
545 }
546  
547 0 return 0;
548 } ## end sub _valid_cidr
549  
550 # Internal helper. Returns the set command lines (arrayref) that add the passed
551 # CIDR range to the global address book and address-set. The CIDR is used
552 # verbatim as the address prefix, so the address object already carries its own
553 # prefix length rather than a per-host /32 or /128 mask.
554 sub _ban_cidr_setlines {
555 1 ▶ my ( $self, $cidr ) = @_;
Subroutine _ban_cidr_setlines: called CC=1 SCAR=0.0
556  
557 1 my $obj = $self->_obj_name($cidr);
558  
559 return [
560 'set security address-book global address ' . $obj . ' ' . $cidr,
561 1 'set security address-book global address-set ' . $self->{options}{address_set} . ' address ' . $obj,
562 ];
563 } ## end sub _ban_cidr_setlines
564  
565 =head2 ban_cidr
566  
567 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 CIDR
568 and lowercased. An address book object is created the same way as for a single
569 IP, but with the CIDR used verbatim as the address prefix, and is added to the
570 address-set, then the change is committed. Banning an already banned CIDR
571 range is a noop.
572  
573 $backend->ban_cidr(ban => '1.2.3.0/24');
574  
575 =cut
576  
577 sub ban_cidr {
578 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=9 SCAR=36.2
Pod documented
579  
580 1 $self->errorblank;
581  
582 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
583 0 $self->{error} = 1;
584 0 $self->{errorString} = 'backend has not been inited';
585 0 $self->warn;
586 0 return;
587 }
588  
589 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
590 0 $self->{error} = 9;
591 0 $self->{errorString} = 'Nothing specified for the value ban';
592 0 $self->warn;
593 0 return;
594 } elsif ( ref( $opts{ban} ) ne '' ) {
595 0 $self->{error} = 35;
596 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
597 0 $self->warn;
598 0 return;
599 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
600 0 $self->{error} = 35;
601 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
602 0 $self->warn;
603 0 return;
604 }
605  
606 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
607 1 $opts{ban} = lc( $opts{ban} );
608  
609 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
610 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
611 0 $self->{frontend_obj}->{test_data} = 'already banned';
612 }
613 0 return;
614 }
615  
616 1 my $setlines = $self->_ban_cidr_setlines( $opts{ban} );
617  
618 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
619 $self->{frontend_obj}->{test_data} = [
620 1 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
621 { method => 'POST', url => $self->_url, content => $self->_commit_body },
622 ];
623 } else {
624 0 local $@;
625 eval {
626 0 $self->_request( $self->_load_body($setlines) );
627 0 $self->_request( $self->_commit_body );
628 0 1;
629 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_load_body($setlines)); $self->_request($self->_commit_body); 1 } } or do { $self->{"error"} = 33; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
630 0 $self->{error} = 33;
631 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
632 0 $self->warn;
633 0 return;
634 };
635 }
636  
637 1 $self->{banned_cidr}{ $opts{ban} } = 1;
638 } ## end sub ban_cidr
639  
640 =head2 unban_cidr
641  
642 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
643 CIDR and lowercased. The range's address book object is deleted, then the
644 change is committed; deleting the address also removes it from the
645 address-set. Unbanning a CIDR range that is not banned is a noop.
646  
647 $backend->unban_cidr(ban => '1.2.3.0/24');
648  
649 =cut
650  
651 sub unban_cidr {
652 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=9 SCAR=36.2
Pod documented
653  
654 1 $self->errorblank;
655  
656 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
657 0 $self->{error} = 1;
658 0 $self->{errorString} = 'backend has not been inited';
659 0 $self->warn;
660 0 return;
661 }
662  
663 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
664 0 $self->{error} = 9;
665 0 $self->{errorString} = 'Nothing specified for the value ban';
666 0 $self->warn;
667 0 return;
668 } elsif ( ref( $opts{ban} ) ne '' ) {
669 0 $self->{error} = 35;
670 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
671 0 $self->warn;
672 0 return;
673 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
674 0 $self->{error} = 35;
675 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
676 0 $self->warn;
677 0 return;
678 }
679  
680 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
681 1 $opts{ban} = lc( $opts{ban} );
682  
683 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
684 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
685 0 $self->{frontend_obj}->{test_data} = 'not banned';
686 }
687 0 return;
688 }
689  
690 1 my $setlines = $self->_unban_setlines( $opts{ban} );
691  
692 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
693 $self->{frontend_obj}->{test_data} = [
694 1 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
695 { method => 'POST', url => $self->_url, content => $self->_commit_body },
696 ];
697 } else {
698 0 local $@;
699 eval {
700 0 $self->_request( $self->_load_body($setlines) );
701 0 $self->_request( $self->_commit_body );
702 0 1;
703 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_load_body($setlines)); $self->_request($self->_commit_body); 1 } } or do { $self->{"error"} = 34; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
704 0 $self->{error} = 34;
705 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
706 0 $self->warn;
707 0 return;
708 };
709 }
710  
711 1 delete( $self->{banned_cidr}{ $opts{ban} } );
712 } ## end sub unban_cidr
713  
714 =head2 list_cidr
715  
716 List banned CIDR ranges. Returns an array of the currently banned CIDR ranges
717 from internal state; the device is not queried. Single IPs are not included;
718 for those see L</list>.
719  
720 my @banned_cidrs = $backend->list_cidr;
721  
722 =cut
723  
724 sub list_cidr {
725 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
726  
727 2 $self->errorblank;
728  
729 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
730 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
731 }
732  
733 2 return keys( %{ $self->{banned_cidr} } );
734 }
735  
736 =head2 list
737  
738 List banned IPs. Returns an array of the currently banned single IPs from
739 internal state; the device is not queried. CIDR ranges are not included; for
740 those see L</list_cidr>.
741  
742 my @banned = $backend->list;
743  
744 =cut
745  
746 sub list {
747 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
748  
749 0 $self->errorblank;
750  
751 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
752 0 $self->{frontend_obj}->{test_data} = 'list';
753 }
754  
755 0 return keys( %{ $self->{banned} } );
756 }
757  
758 =head2 re_init
759  
760 Tears down and re-inits, then re-adds all previously added bans. Teardown is
761 best effort, as a partially or fully wiped setup is what re_init recovers
762 from. Each retained IP and CIDR range is then re-added via its own
763 load-configuration and commit-configuration RPC pair.
764  
765 =cut
766  
767 sub re_init {
768 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=9 SCAR=45.0
Pod documented
769  
770 0 $self->errorblank;
771  
772 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
773 0 $self->{error} = 1;
774 0 $self->{errorString} = 'backend has not been inited';
775 0 $self->warn;
776 0 return;
777 }
778  
779 # teardown is best effort here as a partially or fully wiped setup is
780 # exactly what re_init needs to recover from
781 {
782 0 local $@;
783 0 eval { $self->teardown; };
784 }
785 0 $self->init;
786  
787 0 my @re_init_test_data;
788 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
789 0 my $setlines = $self->_ban_setlines($item);
790  
791 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
792 0 push(
793 @re_init_test_data,
794 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
795 { method => 'POST', url => $self->_url, content => $self->_commit_body },
796 );
797 } else {
798 0 local $@;
799 eval {
800 0 $self->_request( $self->_load_body($setlines) );
801 0 $self->_request( $self->_commit_body );
802 0 1;
803 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_load_body($setlines)); $self->_request($self->_commit_body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $item . '" failed... ' . $@; $self->warn } 0 0 0
804 0 $self->{error} = 13;
805 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
806 0 $self->warn;
807 };
808 }
809 } ## end foreach my $item ( keys( %{ ...}))
810  
811 # CIDR ranges carry their own prefix, so they use the CIDR set lines
812 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
813 0 my $setlines = $self->_ban_cidr_setlines($item);
814  
815 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
816 0 push(
817 @re_init_test_data,
818 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
819 { method => 'POST', url => $self->_url, content => $self->_commit_body },
820 );
821 } else {
822 0 local $@;
823 eval {
824 0 $self->_request( $self->_load_body($setlines) );
825 0 $self->_request( $self->_commit_body );
826 0 1;
827 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_load_body($setlines)); $self->_request($self->_commit_body); 1 } } or do { $self->{"error"} = 33; $self->{"errorString"} = 'banning "' . $item . '" failed... ' . $@; $self->warn } 0 0 0
828 0 $self->{error} = 33;
829 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
830 0 $self->warn;
831 };
832 }
833 } ## end foreach my $item ( keys( %{ ...}))
834  
835 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
836 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
837 }
838  
839 0 $self->{inited} = 1;
840 } ## end sub re_init
841  
842 =head2 teardown
843  
844 Tears down the setup by deleting the address book object of each currently
845 banned IP and CIDR range, committing after each deletion. The internal list of
846 bans is kept, so a following re_init will re-add them.
847  
848 =cut
849  
850 sub teardown {
851 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
852  
853 0 $self->errorblank;
854  
855 0 $self->{inited} = 0;
856  
857 0 my @requests;
858 0 foreach my $item ( sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ) {
859 0 my $setlines = $self->_unban_setlines($item);
860 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
861 0 push(
862 @requests,
863 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
864 { method => 'POST', url => $self->_url, content => $self->_commit_body },
865 );
866 } else {
867 0 local $@;
868 eval {
869 0 $self->_request( $self->_load_body($setlines) );
870 0 $self->_request( $self->_commit_body );
871 0 1;
872 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_load_body($setlines)); $self->_request($self->_commit_body); 1 } } or do { $self->{"error"} = 17; $self->{"errorString"} = 'teardown failed removing "' . $item . '"... ' . $@; $self->warn } 0 0 0
873 0 $self->{error} = 17;
874 0 $self->{errorString} = 'teardown failed removing "' . $item . '"... ' . $@;
875 0 $self->warn;
876 };
877 }
878 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
879  
880 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
881 0 $self->{frontend_obj}->{test_data} = \@requests;
882 }
883 } ## end sub teardown
884  
885 =head2 stop
886  
887 Alias for L</teardown>.
888  
889 =cut
890  
891 sub stop {
892 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
893  
894 0 return $self->teardown(%opts);
895 }
896  
897 =head2 check
898  
899 Verifies the credentials and connectivity are still usable with a
900 get-software-information RPC. Returns a true value if so and a false value
901 otherwise.
902  
903 =cut
904  
905 sub check {
906 5 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=15.4
Pod documented
907  
908 5 $self->errorblank;
909  
910 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
911 $self->{frontend_obj}->{test_data}
912 5 = [ { method => 'POST', url => $self->_url, content => '<get-software-information/>' } ];
913 5 return 1;
914 }
915  
916 0 local $@;
917 0 ▶ eval { $self->_request('<get-software-information/>'); 1; } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("<get-software-information/>"); 1 } } or return 0 0 0 0
918  
919 0 return 1;
920 } ## end sub check
921  
922 =head2 flush
923  
924 Removes all currently banned IPs and CIDR ranges at once by deleting their
925 address book objects, committing after each deletion, and clearing the
926 internal ban lists.
927  
928 =cut
929  
930 sub flush {
931 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=6 SCAR=37.4
Pod documented
932  
933 0 $self->errorblank;
934  
935 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
936 0 $self->{error} = 1;
937 0 $self->{errorString} = 'backend has not been inited';
938 0 $self->warn;
939 0 return;
940 }
941  
942 0 my @requests;
943 0 foreach my $item ( sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ) {
944 0 my $setlines = $self->_unban_setlines($item);
945 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
946 0 push(
947 @requests,
948 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
949 { method => 'POST', url => $self->_url, content => $self->_commit_body },
950 );
951 } else {
952 0 local $@;
953 eval {
954 0 $self->_request( $self->_load_body($setlines) );
955 0 $self->_request( $self->_commit_body );
956 0 1;
957 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request($self->_load_body($setlines)); $self->_request($self->_commit_body); 1 } } or do { $self->{"error"} = 25; $self->{"errorString"} = 'flush failed removing "' . $item . '"... ' . $@; $self->warn } 0 0 0
958 0 $self->{error} = 25;
959 0 $self->{errorString} = 'flush failed removing "' . $item . '"... ' . $@;
960 0 $self->warn;
961 };
962 }
963 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
964  
965 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
966 0 $self->{frontend_obj}->{test_data} = \@requests;
967 }
968  
969 0 $self->{banned} = {};
970 0 $self->{banned_cidr} = {};
971 } ## end sub flush
972  
973 =head1 ERROR CODES / FLAGS
974  
975 Error handling is provided by L<Error::Helper>. All
976 errors are considered fatal.
977  
978 =head2 1, notInited
979  
980 The backend has not been inited yet.
981  
982 =head2 6, invalidPrefixSpecified
983  
984 The specified prefix did not match /^[a-zA-Z0-9]+$/.
985  
986 =head2 7, invalidName
987  
988 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
989  
990 =head2 8, optionsNotHash
991  
992 The item passed to new for options is not a hash.
993  
994 =head2 9, noBanItem
995  
996 No IP or CIDR range specified to ban or unban.
997  
998 =head2 10, banItemNotIP
999  
1000 The item to ban is not an IP. Either wrong ref type or regexp
1001 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1002  
1003 =head2 12, backendInitError
1004  
1005 Failed to init the backend.
1006  
1007 =head2 13, banFailed
1008  
1009 Failed to ban the item.
1010  
1011 =head2 14, unbanFailed
1012  
1013 Failed to unban the item.
1014  
1015 =head2 15, listFailed
1016  
1017 Failed to get a list of bans.
1018  
1019 =head2 16, reInitFailed
1020  
1021 Failed to re_init the backend.
1022  
1023 =head2 17, teardownFailed
1024  
1025 Failed to teardown the backend.
1026  
1027 =head2 18, alreadyInited
1028  
1029 init called, but the backend has already been inited.
1030  
1031 =head2 23, initFailed
1032  
1033 Probing the API during init failed.
1034  
1035 =head2 24, checkFailed
1036  
1037 The backend check raised an error.
1038  
1039 =head2 25, flushFailed
1040  
1041 Failed to flush the bans.
1042  
1043 =head2 26, portsNotSupported
1044  
1045 The juniper_srx backend blocks whole IPs and does not support ports.
1046  
1047 =head2 27, protocolsNotSupported
1048  
1049 The juniper_srx backend blocks whole IPs and does not support protocols.
1050  
1051 =head2 30, hostNotDefined
1052  
1053 The option host is undef or blank.
1054  
1055 =head2 31, userNotDefined
1056  
1057 The option user is undef or blank.
1058  
1059 =head2 32, passwordNotDefined
1060  
1061 The option password is undef or blank.
1062  
1063 =head2 33, banCidrFailed
1064  
1065 Failed to ban the CIDR range.
1066  
1067 =head2 34, unbanCidrFailed
1068  
1069 Failed to unban the CIDR range.
1070  
1071 =head2 35, cidrItemNotCidr
1072  
1073 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1074 IPv4 or IPv6 address followed by a prefix length valid for its family.
1075  
1076 =head2 36, cidrNotSupported
1077  
1078 The backend does not support CIDR bans.
1079  
1080 =head2 37, listCidrFailed
1081  
1082 Failed to get a list of CIDR bans.
1083  
1084 =head1 AUTHOR
1085  
1086 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1087  
1088 =head1 LICENSE AND COPYRIGHT
1089  
1090 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1091  
1092 This is free software, licensed under:
1093  
1094 The GNU Lesser General Public License, Version 2.1, February 1999
1095  
1096 =cut
1097  
1098 1 1; # End of Net::Firewall::BlockerHelper::backends::juniper_srx
« BlockerHelper/backends/routeros_api.pm ↑ Summary BlockerHelper/backends/shorewall.pm »