BlockerHelper/backends/akamai.pm

filter: Statementstmt 33.4% 140 / 418 Branchbran 33.1% 53 / 160 Conditioncond 37.0% 20 / 54 MC/DCmcdc 15.1% 5 / 33 Subroutinesub 72.4% 21 / 29 Podpod 100% 13 / 13 totaltotal 35.6% 252 / 707 CC 102 SCAR 36.9
CC
102
Cov
34%
new
73.2
_request
72.0
unban
45.7
CRAP 40.2
« BlockerHelper/backends/panos.pm ↑ Summary BlockerHelper/backends/vyos.pm »
1 package Net::Firewall::BlockerHelper::backends::akamai;
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::akamai - Akamai Network Lists backend for Net::Firewall::BlockerHelper.
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 => 'akamai',
28 name => 'ssh',
29 options => {
30 host => 'akab-xxxx.luna.akamaiapis.net',
31 client_token => $client_token,
32 client_secret => $client_secret,
33 access_token => $access_token,
34 network_list_id => $network_list_id,
35 },
36 );
37  
38 $fw_helper->init_backend;
39 $fw_helper->ban( ban => '1.2.3.4' );
40 $fw_helper->unban( ban => '1.2.3.4' );
41  
42 =head1 DESCRIPTION
43  
44 Blocks IPs remotely via the Akamai Network Lists API v2, adding and removing
45 elements of an existing network list. Requests are authenticated using
46 Akamai's EdgeGrid (C<EG1-HMAC-SHA256>) request signing scheme.
47  
48 The network list identified by the C<network_list_id> option must already
49 exist. This backend only manages the membership of that list.
50  
51 Blocking is per IP or CIDR range; ports and protocols are not supported and
52 specifying them is an error.
53  
54 B<Note:> Changes to a network list made via the API do not take effect until
55 the list is activated (staging and/or production). Activation is out of scope
56 for this backend and must be performed separately, either via the Akamai
57 control center or the activation endpoints of the Network Lists API.
58 Similarly, membership in the list does not block anything on its own; the
59 list must actually be used by a security policy, eg as the block list of a
60 IP/Geo firewall rule. Wiring the list into a policy is likewise out of scope
61 for this backend.
62  
63 L<LWP::UserAgent> is only loaded at run time, so it is only required if this
64 backend is actually used. For https, L<LWP::Protocol::https> must be present
65 as well.
66  
67 =head1 METHODS
68  
69 =head2 new
70  
71 Initiates the object.
72  
73 - options :: Backend specific options. See below.
74 - Default :: {}
75  
76 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
77 - default :: kur
78  
79 - name :: Name of this specific instance. This must be specified.
80 - default :: undef
81  
82 Ports and protocols are not supported by this backend and specifying either
83 is an error.
84  
85 The options hash accepts the following. The host, client_token,
86 client_secret, access_token, and network_list_id options are all required.
87  
88 - host :: Akamai API hostname, eg akab-xxxx.luna.akamaiapis.net .
89 - Default :: undef
90  
91 - client_token :: EdgeGrid client token.
92 - Default :: undef
93  
94 - client_secret :: EdgeGrid client secret.
95 - Default :: undef
96  
97 - access_token :: EdgeGrid access token.
98 - Default :: undef
99  
100 - network_list_id :: ID of the network list to manage.
101 - Default :: undef
102  
103 - timeout :: HTTP timeout in seconds.
104 - Default :: 30
105  
106 - insecure :: If true, skip TLS certificate verification.
107 - Default :: 0
108  
109 All errors are considered fatal, meaning if new fails it will die.
110  
111 =cut
112  
113 sub new {
114 6 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=28 SCAR=42.9
Pod documented
115  
116 6 my $self = {
117 perror => undef,
118 error => undef,
119 errorLine => undef,
120 errorFilename => undef,
121 errorString => "",
122 errorExtra => {
123 all_errors_fatal => 1,
124 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
125 # is kept for the name documented in its POD
126 all_fatal => 1,
127 flags => {
128 1 => 'notInited',
129 6 => 'invalidPrefixSpecified',
130 7 => 'invalidName',
131 8 => 'optionsNotHash',
132 9 => 'noBanItem',
133 10 => 'banItemNotIP',
134 12 => 'backendInitError',
135 13 => 'banFailed',
136 14 => 'unbanFailed',
137 15 => 'listFailed',
138 16 => 'reInitFailed',
139 17 => 'teardownFailed',
140 18 => 'alreadyInited',
141 23 => 'initFailed',
142 24 => 'checkFailed',
143 25 => 'flushFailed',
144 26 => 'portsNotSupported',
145 27 => 'protocolsNotSupported',
146 30 => 'hostNotDefined',
147 31 => 'clientTokenNotDefined',
148 32 => 'clientSecretNotDefined',
149 33 => 'accessTokenNotDefined',
150 34 => 'networkListIdNotDefined',
151 35 => 'banCidrFailed',
152 36 => 'unbanCidrFailed',
153 37 => 'cidrItemNotCidr',
154 38 => 'cidrNotSupported',
155 39 => 'listCidrFailed',
156 },
157 fatal_flags => {},
158 perror_not_fatal => 0,
159 },
160 options => {},
161 ports => [],
162 protocols => [],
163 testing => undef,
164 test_data => undef,
165 prefix => 'kur',
166 name => undef,
167 frontend_obj => undef,
168 inited => 0,
169 banned => {},
170 banned_cidr => {},
171 cidr_supported => 1,
172 ua => undef,
173 };
174 6 bless $self;
175  
176 # blocking is per IP; the network list has no concept of ports or protocols
177 6 ▶ 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 6 6
Condition33%2/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
060
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
006
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
178 0 $self->{perror} = 1;
179 0 $self->{error} = 26;
180 0 $self->{errorString} = 'the akamai backend blocks whole IPs and does not support ports';
181 0 $self->warn;
182 }
183 6 ▶ 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 6 6
Condition33%2/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
060
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
006
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
184 0 $self->{perror} = 1;
185 0 $self->{error} = 27;
186 0 $self->{errorString} = 'the akamai backend blocks whole IPs and does not support protocols';
187 0 $self->warn;
188 }
189  
190 # make sure prefix is sane if defined
191 6 ▶ 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 6 6
elsif (defined $opts{'prefix'}) { } 6 0 6
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
060
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
192 0 $self->{perror} = 1;
193 0 $self->{error} = 6;
194 $self->{errorString}
195 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
196 0 $self->warn;
197 } elsif ( defined( $opts{prefix} ) ) {
198 6 $self->{prefix} = $opts{prefix};
199 }
200  
201 # make sure we have a name and that it is valid
202 6 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 6 6
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 6 6
203 0 $self->{perror} = 1;
204 0 $self->{error} = 7;
205 0 $self->{errorString} = 'name is undef';
206 0 $self->warn;
207 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
208 0 $self->{perror} = 1;
209 0 $self->{error} = 7;
210 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
211 0 $self->warn;
212 }
213 6 $self->{name} = $opts{name};
214  
215 # used internally for testing
216 6 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 6 0 6
217 6 $self->{testing} = $opts{testing};
218 }
219 6 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 6 0 6
220 6 $self->{frontend_obj} = $opts{frontend_obj};
221 }
222  
223 6 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 6 0 6
224 6 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 6 6
225 0 $self->{perror} = 1;
226 0 $self->{error} = 8;
227 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
228 0 $self->warn;
229 }
230 6 $self->{options} = $opts{options};
231 }
232  
233 # required options
234 6 ▶ 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 5 6
Condition66%2/3
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
l!l&&r!l&&!r
105
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
235 1 $self->{perror} = 1;
236 1 $self->{error} = 30;
237 1 $self->{errorString} = 'the option host is undef or blank';
238 1 $self->warn;
239 }
240 5 ▶ if ( !defined( $self->{options}{client_token} ) || $self->{options}{client_token} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"client_token"} or $self->{"options"}{"client_token"} eq "") 1 4 5
Condition66%2/3
not defined $self->{"options"}{"client_token"} or $self->{"options"}{"client_token"} eq ""
l!l&&r!l&&!r
104
MC/DC50%1/2
not defined $self->{"options"}{"client_token"} or $self->{"options"}{"client_token"} eq ""
not defined $self->{"options"}{"client_token"} $self->{"options"}{"client_token"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"client_token"}B= $self->{"options"}{"client_token"} eq ""
ABresult
1X1
011
000
241 1 $self->{perror} = 1;
242 1 $self->{error} = 31;
243 1 $self->{errorString} = 'the option client_token is undef or blank';
244 1 $self->warn;
245 }
246 4 ▶ if ( !defined( $self->{options}{client_secret} ) || $self->{options}{client_secret} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"client_secret"} or $self->{"options"}{"client_secret"} eq "") 1 3 4
Condition66%2/3
not defined $self->{"options"}{"client_secret"} or $self->{"options"}{"client_secret"} eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $self->{"options"}{"client_secret"} or $self->{"options"}{"client_secret"} eq ""
not defined $self->{"options"}{"client_secret"} $self->{"options"}{"client_secret"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"client_secret"}B= $self->{"options"}{"client_secret"} eq ""
ABresult
1X1
011
000
247 1 $self->{perror} = 1;
248 1 $self->{error} = 32;
249 1 $self->{errorString} = 'the option client_secret is undef or blank';
250 1 $self->warn;
251 }
252 3 ▶ if ( !defined( $self->{options}{access_token} ) || $self->{options}{access_token} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"access_token"} or $self->{"options"}{"access_token"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"access_token"} or $self->{"options"}{"access_token"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"access_token"} or $self->{"options"}{"access_token"} eq ""
not defined $self->{"options"}{"access_token"} $self->{"options"}{"access_token"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"access_token"}B= $self->{"options"}{"access_token"} eq ""
ABresult
1X1
011
000
253 1 $self->{perror} = 1;
254 1 $self->{error} = 33;
255 1 $self->{errorString} = 'the option access_token is undef or blank';
256 1 $self->warn;
257 }
258 2 ▶ if ( !defined( $self->{options}{network_list_id} ) || $self->{options}{network_list_id} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"network_list_id"} or $self->{"options"}{"network_list_id"} eq "") 1 1 2
Condition66%2/3
not defined $self->{"options"}{"network_list_id"} or $self->{"options"}{"network_list_id"} eq ""
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $self->{"options"}{"network_list_id"} or $self->{"options"}{"network_list_id"} eq ""
not defined $self->{"options"}{"network_list_id"} $self->{"options"}{"network_list_id"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"network_list_id"}B= $self->{"options"}{"network_list_id"} eq ""
ABresult
1X1
011
000
259 1 $self->{perror} = 1;
260 1 $self->{error} = 34;
261 1 $self->{errorString} = 'the option network_list_id is undef or blank';
262 1 $self->warn;
263 }
264  
265 # defaults
266 1 ▶ $self->{options}{timeout} = 30 if ( !defined( $self->{options}{timeout} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"timeout"} 1 0 1
267 1 ▶ $self->{options}{insecure} = 0 if ( !defined( $self->{options}{insecure} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"insecure"} 1 0 1
268  
269 1 return $self;
270 } ## end sub new
271  
272 # Internal helper. Returns the base URL for the Akamai API host.
273 sub _base_url {
274 11 ▶ my ($self) = @_;
Subroutine _base_url: called CC=1 SCAR=0.0
275  
276 11 return 'https://' . $self->{options}{host};
277 }
278  
279 # Internal helper. Returns the network list URL used for GETing the list.
280 sub _list_url {
281 11 ▶ my ($self) = @_;
Subroutine _list_url: called CC=1 SCAR=0.0
282  
283 11 return $self->_base_url . '/network-list/v2/network-lists/' . $self->{options}{network_list_id};
284 }
285  
286 # Internal helper. Returns the URL used to append elements to the list.
287 sub _append_url {
288 3 ▶ my ($self) = @_;
Subroutine _append_url: called CC=1 SCAR=0.0
289  
290 3 return $self->_list_url . '/append';
291 }
292  
293 # Internal helper. Returns the URL used to remove a single element from the
294 # list.
295 sub _element_url {
296 2 ▶ my ( $self, $ip ) = @_;
Subroutine _element_url: called CC=1 SCAR=0.0
297  
298 2 return $self->_list_url . '/elements?element=' . $self->_uri_escape($ip);
299 }
300  
301 # Internal helper. Minimal percent encoder so URI::Escape is not needed.
302 sub _uri_escape {
303 2 ▶ my ( $self, $string ) = @_;
Subroutine _uri_escape: called CC=1 SCAR=0.0
304  
305 2 $string =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
306  
307 2 return $string;
308 }
309  
310 # Internal helper. Returns a canonical JSON::PP encoder/decoder.
311 sub _json {
312 3 ▶ my ($self) = @_;
Subroutine _json: called CC=1 SCAR=0.0
313  
314 3 require JSON::PP;
315 3 return JSON::PP->new->canonical->utf8;
316 }
317  
318 # Internal helper. Builds and returns the EdgeGrid EG1-HMAC-SHA256
319 # Authorization header value for the given request. Never called in testing
320 # mode.
321 sub _edgegrid_auth {
322 0 ▶ my ( $self, $method, $url, $body ) = @_;
Subroutine _edgegrid_auth: not called CC=5 SCAR=34.0
323  
324 0 require Digest::SHA;
325 0 require MIME::Base64;
326 0 require POSIX;
327  
328 # EdgeGrid uses a UTC timestamp of the form yyyyMMddTHH:mm:ss+0000
329 0 my $timestamp = POSIX::strftime( '%Y%m%dT%H:%M:%S+0000', gmtime() );
330  
331 # simple random hex nonce
332 0 my $nonce = sprintf( '%08x%08x%08x%08x',
333 int( rand(0xffffffff) ), int( rand(0xffffffff) ),
334 int( rand(0xffffffff) ), int( rand(0xffffffff) ) );
335  
336 # split the URL into host and path+query
337 0 my $host = $self->{options}{host};
338 0 my $path_and_query = $url;
339 0 $path_and_query =~ s{^https?://[^/]+}{};
340 0 ▶ if ( $path_and_query eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_and_query eq "") 0 0 0
341 0 $path_and_query = '/';
342 }
343  
344 # base64 helper that pads to a multiple of 4 with '=' as Digest::SHA omits
345 # the padding
346 my $pad = sub {
347 0 ▶ my ($string) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
348 0 ▶ while ( length($string) % 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($string) % 4) 0 0 0
349 0 $string .= '=';
350 }
351 0 return $string;
352 0 };
353  
354 my $auth_header_without_sig
355 = 'EG1-HMAC-SHA256 '
356 . 'client_token='
357 . $self->{options}{client_token} . ';'
358 . 'access_token='
359 0 . $self->{options}{access_token} . ';'
360 . 'timestamp='
361 . $timestamp . ';'
362 . 'nonce='
363 . $nonce . ';';
364  
365 0 my $signing_key = $pad->( Digest::SHA::hmac_sha256_base64( $timestamp, $self->{options}{client_secret} ) );
366  
367 # content hash is only present for POST requests
368 0 my $content_hash = '';
369 0 ▶ if ( uc($method) eq 'POST' && defined($body) && $body ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $method eq "POST" and defined $body and $body ne "") 0 0 0
Condition0%0/6
uc $method eq "POST" and defined $body and $body ne ""
!ll&&!rl&&r
000
uc $method eq "POST" and defined $body
!ll&&!rl&&r
000
MC/DC0%0/3
uc $method eq "POST" and defined $body and $body ne ""
uc $method eq "POST" defined $body $body ne ""
Truth table0%0/4
A and B and C
A= uc $method eq "POST"B= defined $bodyC= $body ne ""
ABCresult
0XX0
10X0
1100
1111
370 0 $content_hash = $pad->( Digest::SHA::sha256_base64($body) );
371 }
372  
373 0 my $data_to_sign = join( "\t",
374 uc($method),
375 'https',
376 $host,
377 $path_and_query,
378 '', # canonicalized headers, none used here
379 $content_hash,
380 $auth_header_without_sig,
381 );
382  
383 0 my $signature = $pad->( Digest::SHA::hmac_sha256_base64( $data_to_sign, $signing_key ) );
384  
385 0 return $auth_header_without_sig . 'signature=' . $signature;
386 } ## end sub _edgegrid_auth
387  
388 # Internal helper. Performs a HTTP request via LWP::UserAgent, signing it with
389 # an EdgeGrid Authorization header, returning the decoded JSON body (or undef
390 # for an empty body) and dying with an explanation on any HTTP level failure.
391 # Never called in testing mode.
392 sub _request {
393 0 ▶ my ( $self, $method, $url, $body ) = @_;
Subroutine _request: not called CC=8 SCAR=42.8
394  
395 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
396 0 local $@;
397 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent; my(%args) = ("agent", 'Net::Firewall::BlockerHelper/' . $Net::Firewall::BlockerHelper::backends::akamai::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 akamai backend requires... ' . $@ 0 0 0
398 0 require LWP::UserAgent;
399 my %args = (
400 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
401 timeout => $self->{options}{timeout},
402 0 );
403 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
404 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
405 }
406 0 $self->{ua} = LWP::UserAgent->new(%args);
407 0 1;
408 } or die( 'failed to load LWP::UserAgent, which the akamai backend requires... ' . $@ );
409 }
410  
411 0 my $auth = $self->_edgegrid_auth( $method, $url, $body );
412  
413 0 my @headers = ( 'Content-Type' => 'application/json', 'Authorization' => $auth );
414  
415 0 require HTTP::Request;
416 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
417  
418 0 my $response = $self->{ua}->request($request);
419  
420 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
421 0 my $detail = $response->decoded_content;
422 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
423 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
424 }
425  
426 0 my $decoded;
427 0 my $content = $response->decoded_content;
428 0 ▶ if ( defined($content) && $content ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and $content ne "") 0 0 0
Condition0%0/3
defined $content and $content ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and $content ne ""
defined $content $content ne ""
Truth table0%0/3
A and B
A= defined $contentB= $content ne ""
ABresult
0X0
100
111
429 0 local $@;
430 0 eval { $decoded = $self->_json->decode($content); };
431 }
432  
433 0 return $decoded;
434 } ## end sub _request
435  
436 =head2 init
437  
438 Initiates the backend. Verifies the credentials and endpoint by GETing the
439 network list at C</network-list/v2/network-lists/$network_list_id>. Nothing
440 is created remotely; the list must already exist.
441  
442 Note that for bans to have an effect the list must also be activated and in
443 use by a security policy. See L</DESCRIPTION>.
444  
445 $backend->init;
446  
447 =cut
448  
449 sub init {
450 1 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=20.3
Pod documented
451  
452 1 $self->errorblank;
453  
454 1 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 1 1
455 0 $self->{error} = 18;
456 0 $self->{errorString} = 'backend has already been inited';
457 0 $self->warn;
458 }
459  
460 1 my $url = $self->_list_url;
461  
462 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
463 1 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
464 } else {
465 0 local $@;
466 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or do { $self->{"error"} = 23; $self->{"errorString"} = 'init failed. probing the network list endpoint failed... ' . $@; $self->warn } 0 0 0
467 0 $self->{error} = 23;
468 0 $self->{errorString} = 'init failed. probing the network list endpoint failed... ' . $@;
469 0 $self->warn;
470 };
471 }
472  
473 1 $self->{inited} = 1;
474 } ## end sub init
475  
476 =head2 ban
477  
478 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
479 and lowercased, then appended to the network list via a POST to the list's
480 C</append> endpoint. Banning an already banned IP is a noop.
481  
482 $backend->ban(ban => $ip);
483  
484 =cut
485  
486 sub ban {
487 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=37.2
Pod documented
488  
489 2 $self->errorblank;
490  
491 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
492 0 $self->{error} = 1;
493 0 $self->{errorString} = 'backend has not been inited';
494 0 $self->warn;
495 0 return;
496 }
497  
498 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::akamai::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/) { } 0 2 2
499 0 $self->{error} = 9;
500 0 $self->{errorString} = 'Nothing specified for the value ban';
501 0 $self->warn;
502 0 return;
503 } elsif ( ref( $opts{ban} ) ne '' ) {
504 0 $self->{error} = 10;
505 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
506 0 $self->warn;
507 0 return;
508 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/
l!l&&r!l&&!r
110
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/
ABresult
1X1
011
000
509 && $opts{ban} !~ /\A$IPv6_re\z/ )
510 {
511 0 $self->{error} = 10;
512 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
513 0 $self->warn;
514 0 return;
515 }
516  
517 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
518 2 $opts{ban} = lc( $opts{ban} );
519  
520 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
521 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
522 0 $self->{frontend_obj}->{test_data} = 'already banned';
523 }
524 0 return;
525 }
526  
527 2 my $body = $self->_json->encode( { list => [ $opts{ban} ] } );
528  
529 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
530 2 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_append_url, content => $body } ];
531 } else {
532 0 local $@;
533 0 ▶ eval { $self->_request( 'POST', $self->_append_url, $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("POST", $self->_append_url, $body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
534 0 $self->{error} = 13;
535 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
536 0 $self->warn;
537 0 return;
538 };
539 }
540  
541 2 $self->{banned}{ $opts{ban} } = 1;
542 } ## end sub ban
543  
544 =head2 unban
545  
546 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
547 and lowercased, then removed from the network list via a DELETE of its
548 element at the list's C</elements> endpoint. Unbanning an IP that is not
549 banned is a noop.
550  
551 $backend->unban(ban => $ip);
552  
553 =cut
554  
555 sub unban {
556 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=38.2
Pod documented
557  
558 1 $self->errorblank;
559  
560 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
561 0 $self->{error} = 1;
562 0 $self->{errorString} = 'backend has not been inited';
563 0 $self->warn;
564 0 return;
565 }
566  
567 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::akamai::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/) { } 0 1 1
568 0 $self->{error} = 9;
569 0 $self->{errorString} = 'Nothing specified for the value ban';
570 0 $self->warn;
571 0 return;
572 } elsif ( ref( $opts{ban} ) ne '' ) {
573 0 $self->{error} = 10;
574 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
575 0 $self->warn;
576 0 return;
577 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/
ABresult
1X1
011
000
578 && $opts{ban} !~ /\A$IPv6_re\z/ )
579 {
580 0 $self->{error} = 10;
581 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
582 0 $self->warn;
583 0 return;
584 }
585  
586 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
587 1 $opts{ban} = lc( $opts{ban} );
588  
589 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
590 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
591 0 $self->{frontend_obj}->{test_data} = 'not banned';
592 }
593 0 return;
594 }
595  
596 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
597 1 $self->{frontend_obj}->{test_data} = [ { method => 'DELETE', url => $self->_element_url( $opts{ban} ) } ];
598 } else {
599 0 local $@;
600 0 ▶ eval { $self->_request( 'DELETE', $self->_element_url( $opts{ban} ) ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("DELETE", $self->_element_url($opts{"ban"})); 1 } } or do { $self->{"error"} = 14; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
601 0 $self->{error} = 14;
602 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
603 0 $self->warn;
604 0 return;
605 };
606 }
607  
608 1 delete( $self->{banned}{ $opts{ban} } );
609 } ## end sub unban
610  
611 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
612 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
613 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
614 # IPv6). Returns false otherwise.
615 sub _valid_cidr {
616 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
617  
618 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
619  
620 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
621 2 my ( $addr, $prefix ) = ( $1, $2 );
622 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
623 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::akamai::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
624 }
625  
626 0 return 0;
627 } ## end sub _valid_cidr
628  
629 =head2 ban_cidr
630  
631 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
632 CIDR and lowercased, then appended to the network list via a POST to the
633 list's C</append> endpoint; Akamai network lists accept a CIDR range as a
634 list element in the same manner as a single address. Banning an already
635 banned range is a noop.
636  
637 $backend->ban_cidr(ban => '1.2.3.0/24');
638  
639 =cut
640  
641 sub ban_cidr {
642 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=9 SCAR=36.0
Pod documented
643  
644 1 $self->errorblank;
645  
646 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
647 0 $self->{error} = 1;
648 0 $self->{errorString} = 'backend has not been inited';
649 0 $self->warn;
650 0 return;
651 }
652  
653 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
654 0 $self->{error} = 9;
655 0 $self->{errorString} = 'Nothing specified for the value ban';
656 0 $self->warn;
657 0 return;
658 } elsif ( ref( $opts{ban} ) ne '' ) {
659 0 $self->{error} = 37;
660 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
661 0 $self->warn;
662 0 return;
663 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
664 0 $self->{error} = 37;
665 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
666 0 $self->warn;
667 0 return;
668 }
669  
670 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
671 1 $opts{ban} = lc( $opts{ban} );
672  
673 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
674 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
675 0 $self->{frontend_obj}->{test_data} = 'already banned';
676 }
677 0 return;
678 }
679  
680 1 my $body = $self->_json->encode( { list => [ $opts{ban} ] } );
681  
682 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
683 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_append_url, content => $body } ];
684 } else {
685 0 local $@;
686 0 ▶ eval { $self->_request( 'POST', $self->_append_url, $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("POST", $self->_append_url, $body); 1 } } or do { $self->{"error"} = 35; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
687 0 $self->{error} = 35;
688 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
689 0 $self->warn;
690 0 return;
691 };
692 }
693  
694 1 $self->{banned_cidr}{ $opts{ban} } = 1;
695 } ## end sub ban_cidr
696  
697 =head2 unban_cidr
698  
699 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
700 CIDR and lowercased, then removed from the network list via a DELETE of its
701 element at the list's C</elements> endpoint. Unbanning a range that is not
702 banned is a noop.
703  
704 $backend->unban_cidr(ban => '1.2.3.0/24');
705  
706 =cut
707  
708 sub unban_cidr {
709 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=9 SCAR=36.4
Pod documented
710  
711 1 $self->errorblank;
712  
713 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
714 0 $self->{error} = 1;
715 0 $self->{errorString} = 'backend has not been inited';
716 0 $self->warn;
717 0 return;
718 }
719  
720 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
721 0 $self->{error} = 9;
722 0 $self->{errorString} = 'Nothing specified for the value ban';
723 0 $self->warn;
724 0 return;
725 } elsif ( ref( $opts{ban} ) ne '' ) {
726 0 $self->{error} = 37;
727 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
728 0 $self->warn;
729 0 return;
730 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
731 0 $self->{error} = 37;
732 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
733 0 $self->warn;
734 0 return;
735 }
736  
737 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
738 1 $opts{ban} = lc( $opts{ban} );
739  
740 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
741 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
742 0 $self->{frontend_obj}->{test_data} = 'not banned';
743 }
744 0 return;
745 }
746  
747 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
748 1 $self->{frontend_obj}->{test_data} = [ { method => 'DELETE', url => $self->_element_url( $opts{ban} ) } ];
749 } else {
750 0 local $@;
751 0 ▶ eval { $self->_request( 'DELETE', $self->_element_url( $opts{ban} ) ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("DELETE", $self->_element_url($opts{"ban"})); 1 } } or do { $self->{"error"} = 36; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
752 0 $self->{error} = 36;
753 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
754 0 $self->warn;
755 0 return;
756 };
757 }
758  
759 1 delete( $self->{banned_cidr}{ $opts{ban} } );
760 } ## end sub unban_cidr
761  
762 =head2 list_cidr
763  
764 List banned CIDR ranges. Returns an array of the currently banned CIDR
765 ranges from internal state; the network list is not fetched. Single IPs are
766 not included; for those see L</list>.
767  
768 my @banned_cidrs = $backend->list_cidr;
769  
770 =cut
771  
772 sub list_cidr {
773 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
774  
775 2 $self->errorblank;
776  
777 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
778 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
779 }
780  
781 2 return keys( %{ $self->{banned_cidr} } );
782 }
783  
784 =head2 list
785  
786 List banned IPs. Returns an array of the currently banned single IPs from
787 internal state; the network list is not fetched. CIDR ranges are not
788 included; for those see L</list_cidr>.
789  
790 my @banned = $backend->list;
791  
792 =cut
793  
794 sub list {
795 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
796  
797 0 $self->errorblank;
798  
799 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
800 0 $self->{frontend_obj}->{test_data} = 'list';
801 }
802  
803 0 return keys( %{ $self->{banned} } );
804 }
805  
806 =head2 re_init
807  
808 Tears down and re-initiates the backend. teardown is called best effort,
809 then init, after which every previously banned single IP and CIDR range is
810 re-added to the network list via POSTs to the list's C</append> endpoint.
811  
812 $backend->re_init;
813  
814 =cut
815  
816 sub re_init {
817 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=6 SCAR=37.4
Pod documented
818  
819 0 $self->errorblank;
820  
821 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
822 0 $self->{error} = 1;
823 0 $self->{errorString} = 'backend has not been inited';
824 0 $self->warn;
825 0 return;
826 }
827  
828 # teardown is best effort here as a partially or fully wiped setup is
829 # exactly what re_init needs to recover from; init cleans up any remnants
830 {
831 0 local $@;
832 0 eval { $self->teardown; };
833 }
834 0 $self->init;
835  
836 # single IPs and CIDR ranges are both re-added as list elements
837 0 my @to_ban = ( sort( keys( %{ $self->{banned} } ) ), sort( keys( %{ $self->{banned_cidr} } ) ) );
838  
839 0 my @re_init_test_data;
840 0 foreach my $item (@to_ban) {
841 0 my $body = $self->_json->encode( { list => [$item] } );
842  
843 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
844 0 push( @re_init_test_data, { method => 'POST', url => $self->_append_url, content => $body } );
845 } else {
846 0 local $@;
847 0 ▶ eval { $self->_request( 'POST', $self->_append_url, $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("POST", $self->_append_url, $body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $item . '" failed... ' . $@; $self->warn } 0 0 0
848 0 $self->{error} = 13;
849 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
850 0 $self->warn;
851 };
852 }
853 } ## end foreach my $item (@to_ban)
854  
855 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
856 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
857 }
858  
859 0 $self->{inited} = 1;
860 } ## end sub re_init
861  
862 =head2 teardown
863  
864 Tears down the setup for the backend by DELETEing the element for each
865 currently banned single IP from the network list. Banned CIDR ranges are
866 left in place. The internal list of bans is kept, so a following re_init
867 will re-add them.
868  
869 $backend->teardown;
870  
871 =cut
872  
873 sub teardown {
874 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
875  
876 0 $self->errorblank;
877  
878 0 $self->{inited} = 0;
879  
880 0 my @requests;
881 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
882 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
883 0 push( @requests, { method => 'DELETE', url => $self->_element_url($item) } );
884 } else {
885 0 local $@;
886 0 ▶ eval { $self->_request( 'DELETE', $self->_element_url($item) ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("DELETE", $self->_element_url($item)); 1 } } or do { $self->{"error"} = 17; $self->{"errorString"} = 'teardown failed removing the element for "' . $item . '"... ' . $@; $self->warn } 0 0 0
887 0 $self->{error} = 17;
888 0 $self->{errorString} = 'teardown failed removing the element for "' . $item . '"... ' . $@;
889 0 $self->warn;
890 };
891 }
892 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
893  
894 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
895 0 $self->{frontend_obj}->{test_data} = \@requests;
896 }
897 } ## end sub teardown
898  
899 =head2 stop
900  
901 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
902 concept.
903  
904 $backend->stop;
905  
906 =cut
907  
908 sub stop {
909 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
910  
911 0 return $self->teardown(%opts);
912 }
913  
914 =head2 check
915  
916 Verifies the endpoint and credentials are still usable by fetching the
917 network list. Returns a true value if so and a false value otherwise. This is
918 the equivalent of fail2ban's C<actioncheck>.
919  
920 if ( !$backend->check ) {
921 $backend->re_init;
922 }
923  
924 =cut
925  
926 sub check {
927 5 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=14.7
Pod documented
928  
929 5 $self->errorblank;
930  
931 5 my $url = $self->_list_url;
932  
933 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
934 5 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
935 5 return 1;
936 }
937  
938 0 local $@;
939 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or return 0 0 0 0
940  
941 0 return 1;
942 } ## end sub check
943  
944 =head2 flush
945  
946 Removes all currently banned IPs and CIDR ranges at once by DELETEing their
947 elements from the network list and forgetting them. This is the equivalent
948 of fail2ban's C<actionflush>.
949  
950 $backend->flush;
951  
952 =cut
953  
954 sub flush {
955 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=6 SCAR=37.4
Pod documented
956  
957 0 $self->errorblank;
958  
959 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
960 0 $self->{error} = 1;
961 0 $self->{errorString} = 'backend has not been inited';
962 0 $self->warn;
963 0 return;
964 }
965  
966 0 my @requests;
967 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ), sort( keys( %{ $self->{banned_cidr} } ) ) ) {
968 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
969 0 push( @requests, { method => 'DELETE', url => $self->_element_url($item) } );
970 } else {
971 0 local $@;
972 0 ▶ eval { $self->_request( 'DELETE', $self->_element_url($item) ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("DELETE", $self->_element_url($item)); 1 } } or do { $self->{"error"} = 25; $self->{"errorString"} = 'flush failed removing the element for "' . $item . '"... ' . $@; $self->warn } 0 0 0
973 0 $self->{error} = 25;
974 0 $self->{errorString} = 'flush failed removing the element for "' . $item . '"... ' . $@;
975 0 $self->warn;
976 };
977 }
978 } ## end foreach my $item ( sort( keys( %{ $self...}))...)
979  
980 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
981 0 $self->{frontend_obj}->{test_data} = \@requests;
982 }
983  
984 0 $self->{banned} = {};
985 0 $self->{banned_cidr} = {};
986 } ## end sub flush
987  
988 =head1 ERROR CODES / FLAGS
989  
990 Error handling is provided by L<Error::Helper>. All
991 errors are considered fatal.
992  
993 =head2 1, notInited
994  
995 The backend has not been inited yet.
996  
997 =head2 6, invalidPrefixSpecified
998  
999 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1000  
1001 =head2 7, invalidName
1002  
1003 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1004  
1005 =head2 8, optionsNotHash
1006  
1007 The item passed to new for options is not a hash.
1008  
1009 =head2 9, noBanItem
1010  
1011 No IP or CIDR range specified to ban or unban.
1012  
1013 =head2 10, banItemNotIP
1014  
1015 The item to ban is not an IP. Either wrong ref type or regexp
1016 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1017  
1018 =head2 12, backendInitError
1019  
1020 Failed to init the backend.
1021  
1022 =head2 13, banFailed
1023  
1024 Failed to ban the item.
1025  
1026 =head2 14, unbanFailed
1027  
1028 Failed to unban the item.
1029  
1030 =head2 15, listFailed
1031  
1032 Failed to get a list of bans.
1033  
1034 =head2 16, reInitFailed
1035  
1036 Failed to re_init the backend.
1037  
1038 =head2 17, teardownFailed
1039  
1040 Failed to teardown the backend.
1041  
1042 =head2 18, alreadyInited
1043  
1044 init called, but the backend has already been inited.
1045  
1046 =head2 23, initFailed
1047  
1048 init failed. Probing the network list endpoint failed.
1049  
1050 =head2 24, checkFailed
1051  
1052 The backend check raised an error.
1053  
1054 =head2 25, flushFailed
1055  
1056 Failed to flush the bans.
1057  
1058 =head2 26, portsNotSupported
1059  
1060 The akamai backend blocks whole IPs and does not support ports.
1061  
1062 =head2 27, protocolsNotSupported
1063  
1064 The akamai backend blocks whole IPs and does not support protocols.
1065  
1066 =head2 30, hostNotDefined
1067  
1068 The option host is undef or blank.
1069  
1070 =head2 31, clientTokenNotDefined
1071  
1072 The option client_token is undef or blank.
1073  
1074 =head2 32, clientSecretNotDefined
1075  
1076 The option client_secret is undef or blank.
1077  
1078 =head2 33, accessTokenNotDefined
1079  
1080 The option access_token is undef or blank.
1081  
1082 =head2 34, networkListIdNotDefined
1083  
1084 The option network_list_id is undef or blank.
1085  
1086 =head2 35, banCidrFailed
1087  
1088 Failed to ban the CIDR range.
1089  
1090 =head2 36, unbanCidrFailed
1091  
1092 Failed to unban the CIDR range.
1093  
1094 =head2 37, cidrItemNotCidr
1095  
1096 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1097 IPv4 or IPv6 address followed by a prefix length valid for its family.
1098  
1099 =head2 38, cidrNotSupported
1100  
1101 The backend does not support CIDR bans.
1102  
1103 =head2 39, listCidrFailed
1104  
1105 Failed to get a list of CIDR bans.
1106  
1107 =head1 AUTHOR
1108  
1109 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1110  
1111 =head1 BUGS
1112  
1113 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1114 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
1115  
1116 =head1 LICENSE AND COPYRIGHT
1117  
1118 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1119  
1120 This is free software, licensed under:
1121  
1122 The GNU Lesser General Public License, Version 2.1, February 1999
1123  
1124  
1125 =cut
1126  
1127 1 1; # End of Net::Firewall::BlockerHelper::backends::akamai
« BlockerHelper/backends/panos.pm ↑ Summary BlockerHelper/backends/vyos.pm »