BlockerHelper/backends/juniper_srx.pm

filter: Statementstmt 33.4% 132 / 395 Branchbran 33.5% 49 / 146 Conditioncond 45.4% 15 / 33 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 77.7% 21 / 27 Podpod 100% 13 / 13 totaltotal 36.7% 233 / 634 CC 89 SCAR 37.7
CC
89
Cov
34%
new
82.4
re_init
72.0
unban
44.6
CRAP 43.4
« BlockerHelper/backends/routeros_api.pm ↑ Summary BlockerHelper/backends/f5_bigip.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 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::juniper_srx - Juniper SRX backend via the Junos REST 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 => '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 4 ▶sub new {
Subroutine new: called CC=26 SCAR=44.1
Pod documented
97 4 my ( $blank, %opts ) = @_;
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 endpoint every request is posted
243 # to.
244 #
245 # There is only one URL. Unlike the REST APIs that expose a resource tree,
246 # Junos takes an RPC: what to do is carried entirely in the XML body posted
247 # here, not in the path. That is why this takes no arguments and why nothing
248 # in this backend ever builds a URL per operation.
249 #
250 # The scheme is fixed at https rather than configurable, since the Junos REST
251 # service is expected to be run over TLS.
252 #
253 # Takes no arguments; the host comes from the options.
254 #
255 # Returns the endpoint as a string.
256 #
257 # $self->_url; # https://srx.example.org/rpc
258 16 ▶sub _url {
Subroutine _url: called CC=1 SCAR=0.0
259 16 my ($self) = @_;
260  
261 16 return 'https://' . $self->{options}{host} . '/rpc';
262 }
263  
264 # Internal helper. Returns the name of the Junos address book object
265 # representing one banned address or range.
266 #
267 # Junos has no set type that simply holds addresses. Each ban becomes a named
268 # address object in the global address book, which is then added to an
269 # address-set that a security policy references. So every ban needs a name.
270 #
271 # The name is derived from the prefix, the instance name, and the target
272 # rather than stored, so ban and unban independently arrive at the same name
273 # without local bookkeeping, and two instances on the same SRX do not collide.
274 # Dots, colons, and slashes are replaced with hyphens because none of them are
275 # valid in a Junos identifier.
276 #
277 # Unlike the fortigate backend, one sub serves both single addresses and
278 # ranges, since the mangling already covers the slash a range carries.
279 #
280 # Args:
281 #
282 # ip - The address or CIDR range to name an object for, as a plain string.
283 # Expected to be already validated and lowercased. Both
284 # "10.0.0.1" and "10.0.0.0/8" are fine.
285 #
286 # Returns the object name as a plain string, valid as a Junos identifier.
287 #
288 # # with prefix "kur" and name "ssh"
289 # $self->_obj_name('10.0.0.1'); # kur_ssh_10-0-0-1
290 # $self->_obj_name('2001:db8::1'); # kur_ssh_2001-db8--1
291 # $self->_obj_name('10.0.0.0/8'); # kur_ssh_10-0-0-0-8
292 5 ▶sub _obj_name {
Subroutine _obj_name: called CC=1 SCAR=0.0
293 5 my ( $self, $ip ) = @_;
294  
295 5 my $obj = $self->{prefix} . '_' . $self->{name} . '_' . $ip;
296 5 $obj =~ s/[.:\/]/-/g;
297  
298 5 return $obj;
299 }
300  
301 # Internal helper. Wraps a batch of Junos set commands into the
302 # load-configuration RPC that applies them.
303 #
304 # Junos configuration changes go in as an RPC rather than as REST calls
305 # against a resource tree. Using set/text format means the payload is the same
306 # set commands an operator would type at the CLI, which is far easier to
307 # generate and to read back in test_data than the equivalent XML
308 # configuration tree would be.
309 #
310 # The lines are newline joined into one configuration-set element, so a batch
311 # loads as a single change. That matters for the ban path, which creates the
312 # address object and adds it to the address-set together.
313 #
314 # Note this only loads the change into the candidate configuration. Nothing
315 # takes effect until a separate commit, which is what _commit_body is for.
316 #
317 # Args:
318 #
319 # setlines - An arrayref of Junos set command lines, as plain strings with
320 # no trailing newline, such as those from _ban_setlines. May
321 # hold a single line. Emitted in the order given.
322 #
323 # Returns the RPC as an XML string, ready to be the body of a post to _url.
324 #
325 # $self->_load_body( $self->_ban_setlines('10.0.0.1') );
326 # # <load-configuration action="set" format="text"><configuration-set>set security address-book global address kur_ssh_10-0-0-1 10.0.0.1/32
327 # # set security address-book global address-set blocklist address kur_ssh_10-0-0-1</configuration-set></load-configuration>
328 5 ▶sub _load_body {
Subroutine _load_body: called CC=1 SCAR=0.0
329 5 my ( $self, $setlines ) = @_;
330  
331 return
332 '<load-configuration action="set" format="text"><configuration-set>'
333 5 . join( "\n", @{$setlines} )
334 . '</configuration-set></load-configuration>';
335 }
336  
337 # Internal helper. Returns the RPC that commits the candidate configuration.
338 #
339 # Junos is a two phase configuration system: a load only stages changes into
340 # the candidate configuration, and nothing takes effect until it is committed.
341 # Every path that loads something therefore posts this immediately afterwards;
342 # skipping it would leave the ban staged and inert, and would also leave the
343 # candidate dirty for whoever logs in next.
344 #
345 # It is a bare RPC with no arguments and no options, so it is a constant
346 # rather than something built.
347 #
348 # Takes no arguments.
349 #
350 # Returns the RPC as an XML string, ready to be the body of a post to _url.
351 #
352 # $self->_commit_body; # <commit-configuration/>
353 #
354 # # the usual shape: load then commit
355 # $self->_request( $self->_load_body($setlines) );
356 # $self->_request( $self->_commit_body );
357 5 ▶sub _commit_body {
Subroutine _commit_body: called CC=1 SCAR=0.0
358 5 my ($self) = @_;
359  
360 5 return '<commit-configuration/>';
361 }
362  
363 # Internal helper. Returns the Junos set commands that ban one address.
364 #
365 # Two lines are needed, and they belong together: the address book object has
366 # to be created before it can be referenced by the address-set. Both go into a
367 # single load, so the change is applied as one unit.
368 #
369 # The address is written with an explicit host prefix, /32 or /128 by family,
370 # because a Junos address book entry is always a prefix; there is no bare host
371 # form.
372 #
373 # Args:
374 #
375 # ip - The address to ban, as a plain string. Expected to be an already
376 # validated and lowercased IPv4 or IPv6 address. The family is
377 # decided by matching against $IPv4_re and only selects the mask.
378 #
379 # Returns an arrayref of set command lines, in the order they must be applied,
380 # ready to hand to _load_body.
381 #
382 # $self->_ban_setlines('10.0.0.1');
383 # # [
384 # # 'set security address-book global address kur_ssh_10-0-0-1 10.0.0.1/32',
385 # # 'set security address-book global address-set blocklist address kur_ssh_10-0-0-1',
386 # # ]
387 #
388 # $self->_ban_setlines('2001:db8::1');
389 # # [
390 # # 'set security address-book global address kur_ssh_2001-db8--1 2001:db8::1/128',
391 # # 'set security address-book global address-set blocklist address kur_ssh_2001-db8--1',
392 # # ]
393 2 ▶sub _ban_setlines {
Subroutine _ban_setlines: called CC=2 SCAR=6.9
394 2 my ( $self, $ip ) = @_;
395  
396 2 my $obj = $self->_obj_name($ip);
397 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
398  
399 return [
400 'set security address-book global address ' . $obj . ' ' . $ip . '/' . $mask,
401 2 'set security address-book global address-set ' . $self->{options}{address_set} . ' address ' . $obj,
402 ];
403 } ## end sub _ban_setlines
404  
405 # Internal helper. Returns the Junos set commands that unban one address.
406 #
407 # Only one line is needed, unlike the two the ban takes. Deleting an address
408 # book object also removes it from any address-set referencing it, so an
409 # explicit membership removal would be redundant. This is the opposite of the
410 # fortigate backend, where the membership has to be dropped first.
411 #
412 # Args:
413 #
414 # ip - The address to unban, as a plain string. Expected to be an already
415 # validated and lowercased IPv4 or IPv6 address, and to produce the
416 # same object name it did when banned. Note the family is not
417 # consulted at all here, since the object name is all that is needed.
418 #
419 # Returns an arrayref holding the single delete command, ready to hand to
420 # _load_body.
421 #
422 # $self->_unban_setlines('10.0.0.1');
423 # # [ 'delete security address-book global address kur_ssh_10-0-0-1' ]
424 2 ▶sub _unban_setlines {
Subroutine _unban_setlines: called CC=1 SCAR=0.0
425 2 my ( $self, $ip ) = @_;
426  
427 2 return [ 'delete security address-book global address ' . $self->_obj_name($ip) ];
428 }
429  
430 # Internal helper. Posts one XML RPC to the Junos REST API. Every call this
431 # backend makes to the appliance goes through here.
432 #
433 # Because Junos takes an RPC rather than exposing a resource tree, this needs
434 # no method or URL argument: it is always a POST to the one endpoint, and what
435 # happens is decided entirely by the body.
436 #
437 # The user agent is built on first use and cached on the object, so a run of
438 # requests shares one agent rather than building a new one each time.
439 # LWP::UserAgent is loaded with require at that point rather than at compile
440 # time, since only the HTTP backends need it; failing to load it dies with an
441 # explanation naming this backend. The insecure option turns off certificate
442 # verification, which is there because an SRX commonly presents a self signed
443 # certificate.
444 #
445 # Authentication is HTTP basic, encoded per request. MIME::Base64 is required
446 # at call time for the same reason LWP is. Note the second argument to
447 # encode_base64 is an empty string, which suppresses the line wrapping it
448 # would otherwise insert and which would corrupt the header.
449 #
450 # Any HTTP level failure dies rather than setting an error. The callers wrap
451 # this in eval and turn the exception into the appropriate error code, which
452 # is what lets one helper serve paths that report ban, unban, and teardown
453 # failures differently.
454 #
455 # Never called in testing mode; those paths record the bodies instead.
456 #
457 # Args:
458 #
459 # body - The complete XML RPC to post, as a plain string, normally from
460 # _load_body or _commit_body.
461 #
462 # Returns the raw response body as a string. Note it is not parsed, unlike the
463 # JSON backends: callers that care inspect the XML themselves.
464 #
465 # # load a change, then commit it
466 # $self->_request( $self->_load_body( $self->_ban_setlines($ip) ) );
467 # $self->_request( $self->_commit_body );
468 #
469 # # the usual shape at the call sites
470 # eval { $self->_request( $self->_commit_body ); };
471 # if ($@) { ... raise banFailed ... }
472 0 ▶sub _request {
Subroutine _request: not called CC=6 SCAR=37.4
473 0 my ( $self, $body ) = @_;
474  
475 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
476 0 local $@;
477 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
478 0 require LWP::UserAgent;
479 my %args = (
480 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
481 timeout => $self->{options}{timeout},
482 0 );
483 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
484 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
485 }
486 0 $self->{ua} = LWP::UserAgent->new(%args);
487 0 1;
488 } or die( 'failed to load LWP::UserAgent, which the juniper_srx backend requires... ' . $@ );
489 }
490  
491 0 require MIME::Base64;
492 my $auth = 'Basic '
493 0 . MIME::Base64::encode_base64( $self->{options}{user} . ':' . $self->{options}{password}, '' );
494  
495 0 my @headers = ( 'Content-Type' => 'application/xml', 'Authorization' => $auth );
496  
497 0 require HTTP::Request;
498 0 my $request = HTTP::Request->new( 'POST', $self->_url, \@headers, $body );
499  
500 0 my $response = $self->{ua}->request($request);
501  
502 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
503 0 my $detail = $response->decoded_content;
504 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
505 0 die( 'POST ' . $self->_url . ' failed... HTTP status... ' . $response->status_line . $detail );
506 }
507  
508 0 return $response->decoded_content;
509 } ## end sub _request
510  
511 =head2 init
512  
513 Initiates the backend. Verifies the credentials and connectivity with a
514 get-software-information RPC.
515  
516 =cut
517  
518 1 ▶sub init {
Subroutine init: called CC=4 SCAR=20.9
Pod documented
519 1 my ( $self, %opts ) = @_;
520  
521 1 $self->errorblank;
522  
523 1 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 1 1
524 0 $self->{error} = 18;
525 0 $self->{errorString} = 'backend has already been inited';
526 0 $self->warn;
527 }
528  
529 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
530 $self->{frontend_obj}->{test_data}
531 1 = [ { method => 'POST', url => $self->_url, content => '<get-software-information/>' } ];
532 } else {
533 0 local $@;
534 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
535 0 $self->{error} = 23;
536 0 $self->{errorString} = 'init failed. probing the API failed... ' . $@;
537 0 $self->warn;
538 };
539 }
540  
541 1 $self->{inited} = 1;
542 } ## end sub init
543  
544 =head2 ban
545  
546 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address and
547 lowercased. A global address book entry named C<< <prefix>_<name>_<ip> >>
548 (dots and colons replaced with '-') is created with a /32 or /128 mask as
549 appropriate and added to the configured address-set, via a load-configuration
550 RPC in set format followed by a commit-configuration RPC, both POSTed to the
551 REST API. Banning an already banned IP is a noop.
552  
553 $backend->ban(ban => $ip);
554  
555 =cut
556  
557 2 ▶sub ban {
Subroutine ban: called CC=10 SCAR=37.4
Pod documented
558 2 my ( $self, %opts ) = @_;
559  
560 2 $self->errorblank;
561  
562 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
563 0 $self->{error} = 1;
564 0 $self->{errorString} = 'backend has not been inited';
565 0 $self->warn;
566 0 return;
567 }
568  
569 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
570 0 $self->{error} = 9;
571 0 $self->{errorString} = 'Nothing specified for the value ban';
572 0 $self->warn;
573 0 return;
574 } elsif ( ref( $opts{ban} ) ne '' ) {
575 0 $self->{error} = 10;
576 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
577 0 $self->warn;
578 0 return;
579 ▶ } 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
580 && $opts{ban} !~ /\A$IPv6_re\z/ )
581 {
582 0 $self->{error} = 10;
583 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
584 0 $self->warn;
585 0 return;
586 }
587  
588 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
589 2 $opts{ban} = lc( $opts{ban} );
590  
591 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
592 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
593 0 $self->{frontend_obj}->{test_data} = 'already banned';
594 }
595 0 return;
596 }
597  
598 2 my $setlines = $self->_ban_setlines( $opts{ban} );
599  
600 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
601 $self->{frontend_obj}->{test_data} = [
602 2 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
603 { method => 'POST', url => $self->_url, content => $self->_commit_body },
604 ];
605 } else {
606 0 local $@;
607 eval {
608 0 $self->_request( $self->_load_body($setlines) );
609 0 $self->_request( $self->_commit_body );
610 0 1;
611 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
612 0 $self->{error} = 13;
613 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
614 0 $self->warn;
615 0 return;
616 };
617 }
618  
619 2 $self->{banned}{ $opts{ban} } = 1;
620 } ## end sub ban
621  
622 =head2 unban
623  
624 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
625 and lowercased. The IP's address book object is deleted via a
626 load-configuration RPC followed by a commit-configuration RPC; deleting the
627 address also removes it from the address-set. Unbanning an IP that is not
628 banned is a noop.
629  
630 $backend->unban(ban => $ip);
631  
632 =cut
633  
634 1 ▶sub unban {
Subroutine unban: called CC=10 SCAR=38.0
Pod documented
635 1 my ( $self, %opts ) = @_;
636  
637 1 $self->errorblank;
638  
639 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
640 0 $self->{error} = 1;
641 0 $self->{errorString} = 'backend has not been inited';
642 0 $self->warn;
643 0 return;
644 }
645  
646 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
647 0 $self->{error} = 9;
648 0 $self->{errorString} = 'Nothing specified for the value ban';
649 0 $self->warn;
650 0 return;
651 } elsif ( ref( $opts{ban} ) ne '' ) {
652 0 $self->{error} = 10;
653 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
654 0 $self->warn;
655 0 return;
656 ▶ } 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
657 && $opts{ban} !~ /\A$IPv6_re\z/ )
658 {
659 0 $self->{error} = 10;
660 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
661 0 $self->warn;
662 0 return;
663 }
664  
665 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
666 1 $opts{ban} = lc( $opts{ban} );
667  
668 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
669 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
670 0 $self->{frontend_obj}->{test_data} = 'not banned';
671 }
672 0 return;
673 }
674  
675 1 my $setlines = $self->_unban_setlines( $opts{ban} );
676  
677 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
678 $self->{frontend_obj}->{test_data} = [
679 1 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
680 { method => 'POST', url => $self->_url, content => $self->_commit_body },
681 ];
682 } else {
683 0 local $@;
684 eval {
685 0 $self->_request( $self->_load_body($setlines) );
686 0 $self->_request( $self->_commit_body );
687 0 1;
688 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
689 0 $self->{error} = 14;
690 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
691 0 $self->warn;
692 0 return;
693 };
694 }
695  
696 1 delete( $self->{banned}{ $opts{ban} } );
697 } ## end sub unban
698  
699 # Internal helper. Returns the Junos set commands that ban one CIDR range, the
700 # range counterpart of _ban_setlines.
701 #
702 # The two line shape and its ordering are the same: the address book object
703 # has to exist before the address-set can reference it.
704 #
705 # The one difference is that no mask is appended. A range already carries its
706 # own prefix length, which is exactly what a Junos address book entry holds,
707 # so it goes in as it stands rather than being given a host /32 or /128.
708 #
709 # There is no matching _unban_cidr_setlines, because _unban_setlines already
710 # covers ranges: it only needs the object name, and _obj_name handles ranges
711 # as well as addresses.
712 #
713 # Args:
714 #
715 # cidr - The range to ban, as a plain string. Expected to be an already
716 # validated and lowercased CIDR range such as "10.0.0.0/8" or
717 # "2001:db8::/32".
718 #
719 # Returns an arrayref of set command lines, in the order they must be applied,
720 # ready to hand to _load_body.
721 #
722 # $self->_ban_cidr_setlines('10.0.0.0/8');
723 # # [
724 # # 'set security address-book global address kur_ssh_10-0-0-0-8 10.0.0.0/8',
725 # # 'set security address-book global address-set blocklist address kur_ssh_10-0-0-0-8',
726 # # ]
727 1 ▶sub _ban_cidr_setlines {
Subroutine _ban_cidr_setlines: called CC=1 SCAR=0.0
728 1 my ( $self, $cidr ) = @_;
729  
730 1 my $obj = $self->_obj_name($cidr);
731  
732 return [
733 'set security address-book global address ' . $obj . ' ' . $cidr,
734 1 'set security address-book global address-set ' . $self->{options}{address_set} . ' address ' . $obj,
735 ];
736 } ## end sub _ban_cidr_setlines
737  
738 =head2 ban_cidr
739  
740 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 CIDR
741 and lowercased. An address book object is created the same way as for a single
742 IP, but with the CIDR used verbatim as the address prefix, and is added to the
743 address-set, then the change is committed. Banning an already banned CIDR
744 range is a noop.
745  
746 $backend->ban_cidr(ban => '1.2.3.0/24');
747  
748 =cut
749  
750 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=9 SCAR=36.2
Pod documented
751 1 my ( $self, %opts ) = @_;
752  
753 1 $self->errorblank;
754  
755 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
756 0 $self->{error} = 1;
757 0 $self->{errorString} = 'backend has not been inited';
758 0 $self->warn;
759 0 return;
760 }
761  
762 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
763 0 $self->{error} = 9;
764 0 $self->{errorString} = 'Nothing specified for the value ban';
765 0 $self->warn;
766 0 return;
767 } elsif ( ref( $opts{ban} ) ne '' ) {
768 0 $self->{error} = 35;
769 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
770 0 $self->warn;
771 0 return;
772 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
773 0 $self->{error} = 35;
774 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
775 0 $self->warn;
776 0 return;
777 }
778  
779 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
780 1 $opts{ban} = lc( $opts{ban} );
781  
782 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
783 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
784 0 $self->{frontend_obj}->{test_data} = 'already banned';
785 }
786 0 return;
787 }
788  
789 1 my $setlines = $self->_ban_cidr_setlines( $opts{ban} );
790  
791 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
792 $self->{frontend_obj}->{test_data} = [
793 1 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
794 { method => 'POST', url => $self->_url, content => $self->_commit_body },
795 ];
796 } else {
797 0 local $@;
798 eval {
799 0 $self->_request( $self->_load_body($setlines) );
800 0 $self->_request( $self->_commit_body );
801 0 1;
802 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
803 0 $self->{error} = 33;
804 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
805 0 $self->warn;
806 0 return;
807 };
808 }
809  
810 1 $self->{banned_cidr}{ $opts{ban} } = 1;
811 } ## end sub ban_cidr
812  
813 =head2 unban_cidr
814  
815 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
816 CIDR and lowercased. The range's address book object is deleted, then the
817 change is committed; deleting the address also removes it from the
818 address-set. Unbanning a CIDR range that is not banned is a noop.
819  
820 $backend->unban_cidr(ban => '1.2.3.0/24');
821  
822 =cut
823  
824 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=9 SCAR=36.2
Pod documented
825 1 my ( $self, %opts ) = @_;
826  
827 1 $self->errorblank;
828  
829 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
830 0 $self->{error} = 1;
831 0 $self->{errorString} = 'backend has not been inited';
832 0 $self->warn;
833 0 return;
834 }
835  
836 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
837 0 $self->{error} = 9;
838 0 $self->{errorString} = 'Nothing specified for the value ban';
839 0 $self->warn;
840 0 return;
841 } elsif ( ref( $opts{ban} ) ne '' ) {
842 0 $self->{error} = 35;
843 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
844 0 $self->warn;
845 0 return;
846 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
847 0 $self->{error} = 35;
848 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
849 0 $self->warn;
850 0 return;
851 }
852  
853 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
854 1 $opts{ban} = lc( $opts{ban} );
855  
856 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
857 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
858 0 $self->{frontend_obj}->{test_data} = 'not banned';
859 }
860 0 return;
861 }
862  
863 1 my $setlines = $self->_unban_setlines( $opts{ban} );
864  
865 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
866 $self->{frontend_obj}->{test_data} = [
867 1 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
868 { method => 'POST', url => $self->_url, content => $self->_commit_body },
869 ];
870 } else {
871 0 local $@;
872 eval {
873 0 $self->_request( $self->_load_body($setlines) );
874 0 $self->_request( $self->_commit_body );
875 0 1;
876 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
877 0 $self->{error} = 34;
878 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
879 0 $self->warn;
880 0 return;
881 };
882 }
883  
884 1 delete( $self->{banned_cidr}{ $opts{ban} } );
885 } ## end sub unban_cidr
886  
887 =head2 list_cidr
888  
889 List banned CIDR ranges. Returns an array of the currently banned CIDR ranges
890 from internal state; the device is not queried. Single IPs are not included;
891 for those see L</list>.
892  
893 my @banned_cidrs = $backend->list_cidr;
894  
895 =cut
896  
897 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
898 2 my ( $self, %opts ) = @_;
899  
900 2 $self->errorblank;
901  
902 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
903 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
904 }
905  
906 2 return keys( %{ $self->{banned_cidr} } );
907 }
908  
909 =head2 list
910  
911 List banned IPs. Returns an array of the currently banned single IPs from
912 internal state; the device is not queried. CIDR ranges are not included; for
913 those see L</list_cidr>.
914  
915 my @banned = $backend->list;
916  
917 =cut
918  
919 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
920 0 my ( $self, %opts ) = @_;
921  
922 0 $self->errorblank;
923  
924 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
925 0 $self->{frontend_obj}->{test_data} = 'list';
926 }
927  
928 0 return keys( %{ $self->{banned} } );
929 }
930  
931 =head2 re_init
932  
933 Tears down and re-inits, then re-adds all previously added bans. Teardown is
934 best effort, as a partially or fully wiped setup is what re_init recovers
935 from. Each retained IP and CIDR range is then re-added via its own
936 load-configuration and commit-configuration RPC pair.
937  
938 =cut
939  
940 0 ▶sub re_init {
Subroutine re_init: not called CC=8 SCAR=42.8
Pod documented
941 0 my ( $self, %opts ) = @_;
942  
943 0 $self->errorblank;
944  
945 # teardown is best effort here as a partially or fully wiped setup is
946 # exactly what re_init needs to recover from
947 {
948 0 local $@;
949 0 eval { $self->teardown; };
950 }
951 0 $self->init;
952  
953 0 my @re_init_test_data;
954 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
955 0 my $setlines = $self->_ban_setlines($item);
956  
957 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
958 0 push(
959 @re_init_test_data,
960 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
961 { method => 'POST', url => $self->_url, content => $self->_commit_body },
962 );
963 } else {
964 0 local $@;
965 eval {
966 0 $self->_request( $self->_load_body($setlines) );
967 0 $self->_request( $self->_commit_body );
968 0 1;
969 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
970 0 $self->{error} = 13;
971 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
972 0 $self->warn;
973 };
974 }
975 } ## end foreach my $item ( keys( %{ ...}))
976  
977 # CIDR ranges carry their own prefix, so they use the CIDR set lines
978 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
979 0 my $setlines = $self->_ban_cidr_setlines($item);
980  
981 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
982 0 push(
983 @re_init_test_data,
984 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
985 { method => 'POST', url => $self->_url, content => $self->_commit_body },
986 );
987 } else {
988 0 local $@;
989 eval {
990 0 $self->_request( $self->_load_body($setlines) );
991 0 $self->_request( $self->_commit_body );
992 0 1;
993 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
994 0 $self->{error} = 33;
995 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
996 0 $self->warn;
997 };
998 }
999 } ## end foreach my $item ( keys( %{ ...}))
1000  
1001 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1002 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
1003 }
1004  
1005 0 $self->{inited} = 1;
1006 } ## end sub re_init
1007  
1008 =head2 teardown
1009  
1010 Tears down the setup by deleting the address book object of each currently
1011 banned IP and CIDR range, committing after each deletion. The internal list of
1012 bans is kept, so a following re_init will re-add them.
1013  
1014 =cut
1015  
1016 0 ▶sub teardown {
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
1017 0 my ( $self, %opts ) = @_;
1018  
1019 0 $self->errorblank;
1020  
1021 0 $self->{inited} = 0;
1022  
1023 0 my @requests;
1024 0 foreach my $item ( sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ) {
1025 0 my $setlines = $self->_unban_setlines($item);
1026 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1027 0 push(
1028 @requests,
1029 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
1030 { method => 'POST', url => $self->_url, content => $self->_commit_body },
1031 );
1032 } else {
1033 0 local $@;
1034 eval {
1035 0 $self->_request( $self->_load_body($setlines) );
1036 0 $self->_request( $self->_commit_body );
1037 0 1;
1038 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
1039 0 $self->{error} = 17;
1040 0 $self->{errorString} = 'teardown failed removing "' . $item . '"... ' . $@;
1041 0 $self->warn;
1042 };
1043 }
1044 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
1045  
1046 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1047 0 $self->{frontend_obj}->{test_data} = \@requests;
1048 }
1049 } ## end sub teardown
1050  
1051 =head2 stop
1052  
1053 Alias for L</teardown>.
1054  
1055 =cut
1056  
1057 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
1058 0 my ( $self, %opts ) = @_;
1059  
1060 0 return $self->teardown(%opts);
1061 }
1062  
1063 =head2 check
1064  
1065 Verifies the credentials and connectivity are still usable with a
1066 get-software-information RPC. Returns a true value if so and a false value
1067 otherwise.
1068  
1069 =cut
1070  
1071 5 ▶sub check {
Subroutine check: called CC=3 SCAR=15.4
Pod documented
1072 5 my ( $self, %opts ) = @_;
1073  
1074 5 $self->errorblank;
1075  
1076 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
1077 $self->{frontend_obj}->{test_data}
1078 5 = [ { method => 'POST', url => $self->_url, content => '<get-software-information/>' } ];
1079 5 return 1;
1080 }
1081  
1082 0 local $@;
1083 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
1084  
1085 0 return 1;
1086 } ## end sub check
1087  
1088 =head2 flush
1089  
1090 Removes all currently banned IPs and CIDR ranges at once by deleting their
1091 address book objects, committing after each deletion, and clearing the
1092 internal ban lists.
1093  
1094 =cut
1095  
1096 0 ▶sub flush {
Subroutine flush: not called CC=6 SCAR=37.4
Pod documented
1097 0 my ( $self, %opts ) = @_;
1098  
1099 0 $self->errorblank;
1100  
1101 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1102 0 $self->{error} = 1;
1103 0 $self->{errorString} = 'backend has not been inited';
1104 0 $self->warn;
1105 0 return;
1106 }
1107  
1108 0 my @requests;
1109 0 foreach my $item ( sort( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ) ) {
1110 0 my $setlines = $self->_unban_setlines($item);
1111 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1112 0 push(
1113 @requests,
1114 { method => 'POST', url => $self->_url, content => $self->_load_body($setlines) },
1115 { method => 'POST', url => $self->_url, content => $self->_commit_body },
1116 );
1117 } else {
1118 0 local $@;
1119 eval {
1120 0 $self->_request( $self->_load_body($setlines) );
1121 0 $self->_request( $self->_commit_body );
1122 0 1;
1123 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
1124 0 $self->{error} = 25;
1125 0 $self->{errorString} = 'flush failed removing "' . $item . '"... ' . $@;
1126 0 $self->warn;
1127 };
1128 }
1129 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
1130  
1131 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1132 0 $self->{frontend_obj}->{test_data} = \@requests;
1133 }
1134  
1135 0 $self->{banned} = {};
1136 0 $self->{banned_cidr} = {};
1137 } ## end sub flush
1138  
1139 =head1 ERROR CODES / FLAGS
1140  
1141 Error handling is provided by L<Error::Helper>. All
1142 errors are considered fatal.
1143  
1144 =head2 1, notInited
1145  
1146 The backend has not been inited yet.
1147  
1148 =head2 6, invalidPrefixSpecified
1149  
1150 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1151  
1152 =head2 7, invalidName
1153  
1154 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1155  
1156 =head2 8, optionsNotHash
1157  
1158 The item passed to new for options is not a hash.
1159  
1160 =head2 9, noBanItem
1161  
1162 No IP or CIDR range specified to ban or unban.
1163  
1164 =head2 10, banItemNotIP
1165  
1166 The item to ban is not an IP. Either wrong ref type or regexp
1167 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1168  
1169 =head2 12, backendInitError
1170  
1171 Failed to init the backend.
1172  
1173 =head2 13, banFailed
1174  
1175 Failed to ban the item.
1176  
1177 =head2 14, unbanFailed
1178  
1179 Failed to unban the item.
1180  
1181 =head2 15, listFailed
1182  
1183 Failed to get a list of bans.
1184  
1185 =head2 16, reInitFailed
1186  
1187 Failed to re_init the backend.
1188  
1189 =head2 17, teardownFailed
1190  
1191 Failed to teardown the backend.
1192  
1193 =head2 18, alreadyInited
1194  
1195 init called, but the backend has already been inited.
1196  
1197 =head2 23, initFailed
1198  
1199 Probing the API during init failed.
1200  
1201 =head2 24, checkFailed
1202  
1203 The backend check raised an error.
1204  
1205 =head2 25, flushFailed
1206  
1207 Failed to flush the bans.
1208  
1209 =head2 26, portsNotSupported
1210  
1211 The juniper_srx backend blocks whole IPs and does not support ports.
1212  
1213 =head2 27, protocolsNotSupported
1214  
1215 The juniper_srx backend blocks whole IPs and does not support protocols.
1216  
1217 =head2 30, hostNotDefined
1218  
1219 The option host is undef or blank.
1220  
1221 =head2 31, userNotDefined
1222  
1223 The option user is undef or blank.
1224  
1225 =head2 32, passwordNotDefined
1226  
1227 The option password is undef or blank.
1228  
1229 =head2 33, banCidrFailed
1230  
1231 Failed to ban the CIDR range.
1232  
1233 =head2 34, unbanCidrFailed
1234  
1235 Failed to unban the CIDR range.
1236  
1237 =head2 35, cidrItemNotCidr
1238  
1239 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1240 IPv4 or IPv6 address followed by a prefix length valid for its family.
1241  
1242 =head2 36, cidrNotSupported
1243  
1244 The backend does not support CIDR bans.
1245  
1246 =head2 37, listCidrFailed
1247  
1248 Failed to get a list of CIDR bans.
1249  
1250 =head1 AUTHOR
1251  
1252 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1253  
1254 =head1 LICENSE AND COPYRIGHT
1255  
1256 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1257  
1258 This is free software, licensed under:
1259  
1260 The GNU Lesser General Public License, Version 2.1, February 1999
1261  
1262 =cut
1263  
1264 1 1; # End of Net::Firewall::BlockerHelper::backends::juniper_srx
« BlockerHelper/backends/routeros_api.pm ↑ Summary BlockerHelper/backends/f5_bigip.pm »