BlockerHelper/backends/azure.pm

filter: Statementstmt 42.5% 120 / 282 Branchbran 37.2% 41 / 110 Conditioncond 43.3% 13 / 30 MC/DCmcdc 20.0% 4 / 20 Subroutinesub 73.0% 19 / 26 Podpod 100% 13 / 13 totaltotal 43.6% 210 / 481 CC 68 SCAR 30.8
CC
68
Cov
41%
unban
90.0
ban
28.0
ban_cidr
24.5
CRAP 21.8
« BlockerHelper/backends/npf.pm ↑ Summary BlockerHelper/backends/hosts_deny.pm »
1 package Net::Firewall::BlockerHelper::backends::azure;
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::azure - Azure NSG backend via the az CLI.
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 => 'azure',
28 name => 'ssh',
29 options => {
30 resource_group => 'my-rg',
31 nsg => 'my-nsg',
32 rule => 'blocklist',
33 },
34 );
35  
36 $fw_helper->init_backend;
37 $fw_helper->ban( ban => '1.2.3.4' );
38 $fw_helper->unban( ban => '1.2.3.4' );
39  
40 =head1 DESCRIPTION
41  
42 Blocks IPs on Azure by maintaining the C<--source-address-prefixes> of a
43 single deny rule in a Network Security Group, using the C<az> CLI. The full
44 set of currently banned IPs is rendered into the rule on every change, so
45 ban/unban are idempotent. Both IPv4 and IPv6 share the one rule.
46  
47 This backend manages only the rule's source prefixes. The NSG, the inbound
48 deny security rule, and the NSG's association with the subnets or NICs to be
49 protected must already exist.
50  
51 Requires the C<az> CLI in the C<PATH>, logged in with rights to update the
52 rule.
53  
54 =head1 NOTES
55  
56 This backend was written going off the az CLI docs and actual testing is
57 needed to double check a few things as the exact behavior is not clear.
58  
59 When the ban list goes empty, as at teardown or flush, the rule update
60 ends up passing C<--source-address-prefixes> with no value, and whether
61 the az CLI accepts that or errors needs checking.
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 Initiates the backend object. Nothing is contacted at this point; that is done by L</init>.
68  
69 - options :: Backend specific options. See below.
70 - name :: Required by Net::Firewall::BlockerHelper, otherwise unused.
71  
72 The options hash accepts the following.
73  
74 - az_cmd :: Path to the az binary.
75 - Default :: az
76  
77 - resource_group :: Resource group of the NSG. Required.
78 - Default :: undef
79  
80 - nsg :: Network Security Group name. Required.
81 - Default :: undef
82  
83 - rule :: Security rule whose source prefixes are managed. Required.
84 - Default :: undef
85  
86 - subscription :: Optional subscription; appended as --subscription when set.
87 - Default :: undef
88  
89 All errors are considered fatal, meaning if new fails it will die.
90  
91 =cut
92  
93 sub new {
94 5 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=14 SCAR=28.2
Pod documented
95  
96 5 my $self = {
97 perror => undef,
98 error => undef,
99 errorLine => undef,
100 errorFilename => undef,
101 errorString => "",
102 errorExtra => {
103 all_errors_fatal => 1,
104 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
105 # is kept for the name documented in its POD
106 all_fatal => 1,
107 flags => {
108 1 => 'notInited',
109 8 => 'optionsNotHash',
110 9 => 'noBanItem',
111 10 => 'banItemNotIP',
112 12 => 'backendInitError',
113 13 => 'banFailed',
114 14 => 'unbanFailed',
115 15 => 'listFailed',
116 16 => 'reInitFailed',
117 17 => 'teardownFailed',
118 18 => 'alreadyInited',
119 24 => 'checkFailed',
120 25 => 'flushFailed',
121 30 => 'resourceGroupNotDefined',
122 31 => 'nsgNotDefined',
123 32 => 'ruleNotDefined',
124 33 => 'banCidrFailed',
125 34 => 'unbanCidrFailed',
126 35 => 'cidrItemNotCidr',
127 36 => 'cidrNotSupported',
128 37 => 'listCidrFailed',
129 },
130 fatal_flags => {},
131 perror_not_fatal => 0,
132 },
133 options => {},
134 ports => [],
135 protocols => [],
136 testing => undef,
137 test_data => undef,
138 prefix => 'kur',
139 name => undef,
140 frontend_obj => undef,
141 inited => 0,
142 banned => {},
143 banned_cidr => {},
144 cidr_supported => 1,
145 };
146 5 bless $self;
147  
148 5 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 5 0 5
149 5 $self->{testing} = $opts{testing};
150 }
151 5 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 5 0 5
152 5 $self->{frontend_obj} = $opts{frontend_obj};
153 }
154 5 ▶ if ( defined( $opts{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"name"}) 5 0 5
155 5 $self->{name} = $opts{name};
156 }
157  
158 5 ▶ if ( defined( $opts{options} ) && ref( $opts{options} ) ne 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'options'} and ref $opts{'options'} ne 'HASH') { } 0 5 5
elsif (defined $opts{'options'}) { } 5 0 5
Condition33%1/3
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
!ll&&!rl&&r
050
MC/DC0%0/2
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
defined $opts{"options"} ref $opts{"options"} ne "HASH"
Truth table33%1/3
A and B
A= defined $opts{"options"}B= ref $opts{"options"} ne "HASH"
ABresult
0X0
100
111
159 0 $self->{perror} = 1;
160 0 $self->{error} = 8;
161 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
162 0 $self->warn;
163 } elsif ( defined( $opts{options} ) ) {
164 5 $self->{options} = $opts{options};
165 }
166  
167 5 ▶ if ( !defined( $self->{options}{resource_group} ) || $self->{options}{resource_group} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"resource_group"} or $self->{"options"}{"resource_group"} eq "") 1 4 5
Condition66%2/3
not defined $self->{"options"}{"resource_group"} or $self->{"options"}{"resource_group"} eq ""
l!l&&r!l&&!r
104
MC/DC50%1/2
not defined $self->{"options"}{"resource_group"} or $self->{"options"}{"resource_group"} eq ""
not defined $self->{"options"}{"resource_group"} $self->{"options"}{"resource_group"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"resource_group"}B= $self->{"options"}{"resource_group"} eq ""
ABresult
1X1
011
000
168 1 $self->{perror} = 1;
169 1 $self->{error} = 30;
170 1 $self->{errorString} = 'the option resource_group is undef or blank';
171 1 $self->warn;
172 }
173 4 ▶ if ( !defined( $self->{options}{nsg} ) || $self->{options}{nsg} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"nsg"} or $self->{"options"}{"nsg"} eq "") 1 3 4
Condition66%2/3
not defined $self->{"options"}{"nsg"} or $self->{"options"}{"nsg"} eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $self->{"options"}{"nsg"} or $self->{"options"}{"nsg"} eq ""
not defined $self->{"options"}{"nsg"} $self->{"options"}{"nsg"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"nsg"}B= $self->{"options"}{"nsg"} eq ""
ABresult
1X1
011
000
174 1 $self->{perror} = 1;
175 1 $self->{error} = 31;
176 1 $self->{errorString} = 'the option nsg is undef or blank';
177 1 $self->warn;
178 }
179 3 ▶ if ( !defined( $self->{options}{rule} ) || $self->{options}{rule} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"rule"} or $self->{"options"}{"rule"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"rule"} or $self->{"options"}{"rule"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"rule"} or $self->{"options"}{"rule"} eq ""
not defined $self->{"options"}{"rule"} $self->{"options"}{"rule"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"rule"}B= $self->{"options"}{"rule"} eq ""
ABresult
1X1
011
000
180 1 $self->{perror} = 1;
181 1 $self->{error} = 32;
182 1 $self->{errorString} = 'the option rule is undef or blank';
183 1 $self->warn;
184 }
185  
186 2 ▶ $self->{options}{az_cmd} = 'az' if ( !defined( $self->{options}{az_cmd} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"az_cmd"} 2 0 2
187  
188 2 return $self;
189 } ## end sub new
190  
191 # Internal helper. Returns the trailing --subscription argument when configured,
192 # or an empty string.
193 sub _suffix {
194 14 ▶ my ($self) = @_;
Subroutine _suffix: called CC=3 SCAR=11.0
195  
196 14 ▶ if ( defined( $self->{options}{subscription} ) && $self->{options}{subscription} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"subscription"} and $self->{"options"}{"subscription"} ne "") 3 11 14
Condition66%2/3
defined $self->{"options"}{"subscription"} and $self->{"options"}{"subscription"} ne ""
!ll&&!rl&&r
1103
MC/DC50%1/2
defined $self->{"options"}{"subscription"} and $self->{"options"}{"subscription"} ne ""
defined $self->{"options"}{"subscription"} $self->{"options"}{"subscription"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"options"}{"subscription"}B= $self->{"options"}{"subscription"} ne ""
ABresult
0X0
100
111
197 3 return ' --subscription ' . $self->{options}{subscription};
198 }
199 11 return '';
200 }
201  
202  
203 # Internal helper. Returns the shared --resource-group/--nsg-name/--name
204 # arguments identifying the rule.
205 sub _base {
206 14 ▶ my ($self) = @_;
Subroutine _base: called CC=1 SCAR=0.0
207  
208 return
209 ' --resource-group '
210 . $self->{options}{resource_group}
211 . ' --nsg-name '
212 . $self->{options}{nsg}
213 . ' --name '
214 14 . $self->{options}{rule};
215 } ## end sub _base
216  
217 # Internal helper. Returns the current banned IPs and CIDR ranges, sorted,
218 # joined with single spaces. Single IPs are rendered as a CIDR (/32 for IPv4,
219 # /128 for IPv6); CIDR ranges already carry a prefix and are used as is.
220 sub _prefixes {
221 6 ▶ my ($self) = @_;
Subroutine _prefixes: called CC=4 SCAR=13.9
222  
223 6 my @prefixes;
224 6 foreach my $ip ( sort( keys( %{ $self->{banned} } ) ) ) {
225 13 ▶ push( @prefixes, $ip . ( ( $ip =~ /\A$IPv4_re\z/ ) ? '/32' : '/128' ) );
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ ? : 10 3 13
226 }
227 # CIDR ranges already carry a prefix and are added verbatim
228 6 foreach my $cidr ( sort( keys( %{ $self->{banned_cidr} } ) ) ) {
229 1 push( @prefixes, $cidr );
230 }
231  
232 6 return join( ' ', @prefixes );
233 } ## end sub _prefixes
234  
235 # Internal helper. Returns the az command that sets the rule's source prefixes
236 # to the currently banned set.
237 sub _update_command {
238 6 ▶ my ($self) = @_;
Subroutine _update_command: called CC=1 SCAR=0.0
239  
240 return
241 $self->{options}{az_cmd}
242 6 . ' network nsg rule update'
243 . $self->_base
244 . ' --source-address-prefixes '
245 . $self->_prefixes
246 . $self->_suffix;
247 } ## end sub _update_command
248  
249 # Internal helper. Returns the az command used to verify the rule exists.
250 sub _show_command {
251 8 ▶ my ($self) = @_;
Subroutine _show_command: called CC=1 SCAR=0.0
252  
253 8 return $self->{options}{az_cmd} . ' network nsg rule show' . $self->_base . $self->_suffix;
254 }
255  
256 # Internal helper. Runs a command unless testing, raising the passed error flag
257 # on a non-zero exit.
258 sub _run {
259 0 ▶ my ( $self, $command, $error_flag ) = @_;
Subroutine _run: not called CC=2 SCAR=17.9
260  
261 0 my $output = `$command 2>&1`;
262 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
263 0 $self->{error} = $error_flag;
264 0 $self->{errorString} = 'command "' . $command . '" failed... ' . $output;
265 0 $self->warn;
266 }
267  
268 0 return;
269 } ## end sub _run
270  
271 =head2 init
272  
273 Initiates the backend, verifying the configured security rule exists via
274 C<az network nsg rule show>. Nothing is created; the NSG and rule must
275 already exist.
276  
277 =cut
278  
279 sub init {
280 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=3 SCAR=12.7
Pod documented
281  
282 2 $self->errorblank;
283  
284 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
285 0 $self->{error} = 18;
286 0 $self->{errorString} = 'backend has already been inited';
287 0 $self->warn;
288 }
289  
290 2 my @commands = ( $self->_show_command );
291  
292 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
293 2 $self->{frontend_obj}->{test_data} = \@commands;
294 } else {
295 0 $self->_run( $commands[0], 12 );
296 }
297  
298 2 $self->{inited} = 1;
299 } ## end sub init
300  
301 =head2 ban
302  
303 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
304 and lowercased, then added to the ban list. The full set of banned IPs
305 (rendered as /32 or /128 prefixes) and CIDR ranges is then pushed to the
306 rule via C<az network nsg rule update --source-address-prefixes>. Banning
307 an already banned IP is a noop.
308  
309 $fw_helper->ban( ban => $ip );
310  
311 =cut
312  
313 sub ban {
314 4 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=9 SCAR=33.3
Pod documented
315  
316 4 $self->errorblank;
317  
318 4 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 4 4
319 0 $self->{error} = 1;
320 0 $self->{errorString} = 'backend has not been inited';
321 0 $self->warn;
322 0 return;
323 }
324  
325 4 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 4 4
elsif (ref $opts{'ban'} ne '') { } 0 4 4
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/) { } 0 4 4
326 0 $self->{error} = 9;
327 0 $self->{errorString} = 'Nothing specified for the value ban';
328 0 $self->warn;
329 0 return;
330 } elsif ( ref( $opts{ban} ) ne '' ) {
331 0 $self->{error} = 10;
332 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
333 0 $self->warn;
334 0 return;
335 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
l!l&&r!l&&!r
310
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
ABresult
1X1
011
000
336 && $opts{ban} !~ /\A$IPv6_re\z/ )
337 {
338 0 $self->{error} = 10;
339 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
340 0 $self->warn;
341 0 return;
342 }
343  
344 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
345 4 $opts{ban} = lc( $opts{ban} );
346  
347 4 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 4 4
348 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
349 0 $self->{frontend_obj}->{test_data} = 'already banned';
350 }
351 0 return;
352 }
353  
354 4 $self->{banned}{ $opts{ban} } = 1;
355  
356 4 my $command = $self->_update_command;
357 4 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 4 0 4
358 4 $self->{frontend_obj}->{test_data} = $command;
359 } else {
360 0 $self->_run( $command, 13 );
361 }
362 } ## end sub ban
363  
364 =head2 unban
365  
366 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
367 and lowercased, then removed from the ban list. The rule's source prefixes
368 are re-rendered without it via C<az network nsg rule update>. Unbanning an
369 IP that is not banned is a noop.
370  
371 $fw_helper->unban( ban => $ip );
372  
373 =cut
374  
375 sub unban {
376 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban: not called CC=9 SCAR=45.0
Pod documented
377  
378 0 $self->errorblank;
379  
380 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
381 0 $self->{error} = 1;
382 0 $self->{errorString} = 'backend has not been inited';
383 0 $self->warn;
384 0 return;
385 }
386  
387 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/) { } 0 0 0
388 0 $self->{error} = 9;
389 0 $self->{errorString} = 'Nothing specified for the value ban';
390 0 $self->warn;
391 0 return;
392 } elsif ( ref( $opts{ban} ) ne '' ) {
393 0 $self->{error} = 10;
394 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
395 0 $self->warn;
396 0 return;
397 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
ABresult
1X1
011
000
398 && $opts{ban} !~ /\A$IPv6_re\z/ )
399 {
400 0 $self->{error} = 10;
401 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
402 0 $self->warn;
403 0 return;
404 }
405  
406 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
407 0 $opts{ban} = lc( $opts{ban} );
408  
409 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
410 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
411 0 $self->{frontend_obj}->{test_data} = 'not banned';
412 }
413 0 return;
414 }
415  
416 0 delete( $self->{banned}{ $opts{ban} } );
417  
418 0 my $command = $self->_update_command;
419 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
420 0 $self->{frontend_obj}->{test_data} = $command;
421 } else {
422 0 $self->_run( $command, 14 );
423 }
424 } ## end sub unban
425  
426 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
427 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
428 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
429 # IPv6). Returns false otherwise.
430 sub _valid_cidr {
431 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
432  
433 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
434  
435 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
436 2 my ( $addr, $prefix ) = ( $1, $2 );
437 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
438 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
439 }
440  
441 0 return 0;
442 } ## end sub _valid_cidr
443  
444 =head2 ban_cidr
445  
446 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
447 CIDR and lowercased, then added verbatim to the rule's source prefixes via
448 C<az network nsg rule update>. Banning an already banned range is a noop.
449  
450 $fw_helper->ban_cidr( ban => '1.2.3.0/24' );
451  
452 =cut
453  
454 sub ban_cidr {
455 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=8 SCAR=32.0
Pod documented
456  
457 1 $self->errorblank;
458  
459 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
460 0 $self->{error} = 1;
461 0 $self->{errorString} = 'backend has not been inited';
462 0 $self->warn;
463 0 return;
464 }
465  
466 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
467 0 $self->{error} = 9;
468 0 $self->{errorString} = 'Nothing specified for the value ban';
469 0 $self->warn;
470 0 return;
471 } elsif ( ref( $opts{ban} ) ne '' ) {
472 0 $self->{error} = 35;
473 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
474 0 $self->warn;
475 0 return;
476 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
477 0 $self->{error} = 35;
478 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
479 0 $self->warn;
480 0 return;
481 }
482  
483 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
484 1 $opts{ban} = lc( $opts{ban} );
485  
486 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
487 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
488 0 $self->{frontend_obj}->{test_data} = 'already banned';
489 }
490 0 return;
491 }
492  
493 1 $self->{banned_cidr}{ $opts{ban} } = 1;
494  
495 1 my $command = $self->_update_command;
496 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
497 1 $self->{frontend_obj}->{test_data} = $command;
498 } else {
499 0 $self->_run( $command, 33 );
500 }
501 } ## end sub ban_cidr
502  
503 =head2 unban_cidr
504  
505 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
506 CIDR and lowercased, then the rule's source prefixes are re-rendered
507 without it via C<az network nsg rule update>. Unbanning a range that is not
508 banned is a noop.
509  
510 $fw_helper->unban_cidr( ban => '1.2.3.0/24' );
511  
512 =cut
513  
514 sub unban_cidr {
515 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=8 SCAR=32.0
Pod documented
516  
517 1 $self->errorblank;
518  
519 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
520 0 $self->{error} = 1;
521 0 $self->{errorString} = 'backend has not been inited';
522 0 $self->warn;
523 0 return;
524 }
525  
526 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
527 0 $self->{error} = 9;
528 0 $self->{errorString} = 'Nothing specified for the value ban';
529 0 $self->warn;
530 0 return;
531 } elsif ( ref( $opts{ban} ) ne '' ) {
532 0 $self->{error} = 35;
533 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
534 0 $self->warn;
535 0 return;
536 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
537 0 $self->{error} = 35;
538 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
539 0 $self->warn;
540 0 return;
541 }
542  
543 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
544 1 $opts{ban} = lc( $opts{ban} );
545  
546 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
547 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
548 0 $self->{frontend_obj}->{test_data} = 'not banned';
549 }
550 0 return;
551 }
552  
553 1 delete( $self->{banned_cidr}{ $opts{ban} } );
554  
555 1 my $command = $self->_update_command;
556 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
557 1 $self->{frontend_obj}->{test_data} = $command;
558 } else {
559 0 $self->_run( $command, 34 );
560 }
561 } ## end sub unban_cidr
562  
563 =head2 list_cidr
564  
565 List banned CIDR ranges.
566  
567 my @banned_cidrs = $fw_helper->list_cidr;
568  
569 =cut
570  
571 sub list_cidr {
572 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
573  
574 2 $self->errorblank;
575  
576 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
577 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
578 }
579  
580 2 return keys( %{ $self->{banned_cidr} } );
581 } ## end sub list_cidr
582  
583 =head2 list
584  
585 List banned IPs. Returns an array of the currently banned single IPs from
586 the in-memory ban list; the rule is not queried. CIDR ranges are not
587 included; for those see L</list_cidr>.
588  
589 my @banned = $fw_helper->list;
590  
591 =cut
592  
593 sub list {
594 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
595  
596 0 $self->errorblank;
597  
598 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
599 0 $self->{frontend_obj}->{test_data} = 'list';
600 }
601  
602 0 return keys( %{ $self->{banned} } );
603 } ## end sub list
604  
605 =head2 re_init
606  
607 Tears down (errors ignored) and re-initiates the backend, then re-applies
608 the full retained set of banned IPs and CIDR ranges to the rule via
609 C<az network nsg rule update>.
610  
611 =cut
612  
613 sub re_init {
614 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=3 SCAR=24.8
Pod documented
615  
616 0 $self->errorblank;
617  
618 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
619 0 $self->{error} = 1;
620 0 $self->{errorString} = 'backend has not been inited';
621 0 $self->warn;
622 0 return;
623 }
624  
625 {
626 0 local $@;
627 0 eval { $self->teardown; };
628 }
629 0 $self->init;
630  
631 0 my $command = $self->_update_command;
632 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
633 0 $self->{frontend_obj}->{test_data} = [$command];
634 } else {
635 0 $self->_run( $command, 13 );
636 }
637  
638 0 $self->{inited} = 1;
639 } ## end sub re_init
640  
641 =head2 teardown
642  
643 Empties the rule's source prefixes. The internal ban list is kept so a
644 following re_init restores them.
645  
646 =cut
647  
648 sub teardown {
649 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=2 SCAR=17.9
Pod documented
650  
651 0 $self->errorblank;
652  
653 0 $self->{inited} = 0;
654  
655 # render the update with an empty ban set without disturbing the retained
656 # lists, so a following re_init restores both single IPs and CIDR ranges
657 0 my %saved = %{ $self->{banned} };
658 0 my %saved_cidr = %{ $self->{banned_cidr} };
659 0 $self->{banned} = {};
660 0 $self->{banned_cidr} = {};
661 0 my $command = $self->_update_command;
662 0 $self->{banned} = \%saved;
663 0 $self->{banned_cidr} = \%saved_cidr;
664  
665 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
666 0 $self->{frontend_obj}->{test_data} = [$command];
667 } else {
668 0 $self->_run( $command, 17 );
669 }
670 } ## end sub teardown
671  
672 =head2 stop
673  
674 Alias for L</teardown>.
675  
676 =cut
677  
678 sub stop {
679 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
680  
681 0 return $self->teardown(%opts);
682 }
683  
684 =head2 check
685  
686 Verifies the rule still exists by running C<az network nsg rule show>.
687 Returns 1 if the command exits zero and 0 otherwise.
688  
689 =cut
690  
691 sub check {
692 6 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=13.0
Pod documented
693  
694 6 $self->errorblank;
695  
696 6 my $command = $self->_show_command;
697  
698 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
699 6 $self->{frontend_obj}->{test_data} = [$command];
700 6 return 1;
701 }
702  
703 0 my $output = `$command 2>&1`;
704 0 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
705 } ## end sub check
706  
707 =head2 flush
708  
709 Removes all bans at once by clearing both the single IP and CIDR ban lists
710 and re-rendering the rule's now empty source prefixes via
711 C<az network nsg rule update>.
712  
713 =cut
714  
715 sub flush {
716 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=3 SCAR=24.8
Pod documented
717  
718 0 $self->errorblank;
719  
720 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
721 0 $self->{error} = 1;
722 0 $self->{errorString} = 'backend has not been inited';
723 0 $self->warn;
724 0 return;
725 }
726  
727 # the rule is re-rendered from both sets, so clear both to empty it
728 0 $self->{banned} = {};
729 0 $self->{banned_cidr} = {};
730  
731 0 my $command = $self->_update_command;
732 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
733 0 $self->{frontend_obj}->{test_data} = [$command];
734 } else {
735 0 $self->_run( $command, 25 );
736 }
737 } ## end sub flush
738  
739 =head1 ERROR CODES / FLAGS
740  
741 Error handling is provided by L<Error::Helper>. All
742 errors are considered fatal.
743  
744 =head2 1, notInited
745  
746 The backend has not been inited yet.
747  
748 =head2 8, optionsNotHash
749  
750 The item passed to new for options is not a hash.
751  
752 =head2 9, noBanItem
753  
754 No IP or CIDR range specified to ban or unban.
755  
756 =head2 10, banItemNotIP
757  
758 The item to ban is not an IP. Either wrong ref type or regexp
759 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
760  
761 =head2 12, backendInitError
762  
763 Failed to init the backend.
764  
765 =head2 13, banFailed
766  
767 Failed to ban the item.
768  
769 =head2 14, unbanFailed
770  
771 Failed to unban the item.
772  
773 =head2 15, listFailed
774  
775 Failed to get a list of bans.
776  
777 =head2 16, reInitFailed
778  
779 Failed to re_init the backend.
780  
781 =head2 17, teardownFailed
782  
783 Failed to teardown the backend.
784  
785 =head2 18, alreadyInited
786  
787 init called, but the backend has already been inited.
788  
789 =head2 24, checkFailed
790  
791 The backend check raised an error.
792  
793 =head2 25, flushFailed
794  
795 Failed to flush the bans.
796  
797 =head2 30, resourceGroupNotDefined
798  
799 The option resource_group is undef or blank.
800  
801 =head2 31, nsgNotDefined
802  
803 The option nsg is undef or blank.
804  
805 =head2 32, ruleNotDefined
806  
807 The option rule is undef or blank.
808  
809 =head2 33, banCidrFailed
810  
811 Failed to ban the CIDR range.
812  
813 =head2 34, unbanCidrFailed
814  
815 Failed to unban the CIDR range.
816  
817 =head2 35, cidrItemNotCidr
818  
819 The item to ban is not a CIDR range. Either wrong ref type or it is not an
820 IPv4 or IPv6 address followed by a prefix length valid for its family.
821  
822 =head2 36, cidrNotSupported
823  
824 The backend does not support CIDR bans.
825  
826 =head2 37, listCidrFailed
827  
828 Failed to get a list of CIDR bans.
829  
830 =head1 AUTHOR
831  
832 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
833  
834 =head1 LICENSE AND COPYRIGHT
835  
836 This software is Copyright (c) 2026 by Zane C. Bowers-Hadley.
837  
838 This is free software, licensed under:
839  
840 The GNU Lesser General Public License, Version 2.1, February 1999
841  
842 =cut
843  
844 1 1; # End of Net::Firewall::BlockerHelper::backends::azure
« BlockerHelper/backends/npf.pm ↑ Summary BlockerHelper/backends/hosts_deny.pm »