blib/lib/Ereshkigal/App/Command/ban.pm

filter: Statementstmt 96.8% 31 / 32 Branchbran 87.5% 7 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 91.8% 56 / 61 CC 6 SCAR 8.1
CC
6
Cov
93%
validate_args
4.2
execute
3.0
BEGIN
1.0
CRAP 2.2
« blib/lib/Ereshkigal/App/Command/unban.pm ↑ Summary blib/lib/Ereshkigal/App/Command/status.pm »
1 package Ereshkigal::App::Command::ban;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Ereshkigal::App -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Ereshkigal::App::Command::ban - Ban one or more IPs.
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 2 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 # ban on all kur instances
23 ereshkigal ban 1.2.3.4 5.6.7.8
24  
25 # ban on just the kur instance sshd
26 ereshkigal ban --kur sshd 1.2.3.4
27  
28 # an hour long ban
29 ereshkigal ban --ban-time 3600 1.2.3.4
30  
31 # a permanent ban
32 ereshkigal ban --ban-time 0 1.2.3.4
33  
34 =head1 DESCRIPTION
35  
36 Bans one or more IPs. Without C<--kur> every kur gets them, each blocking per
37 its own ports and protocols; with C<--kur> only that instance does, and
38 naming a fan_out gate sends them to every kur behind it.
39  
40 Each IP is answered for separately, so one bad address does not spoil the
41 rest of the request, and re-banning something already banned just resets its
42 sentence rather than erroring.
43  
44 C<--ban-time> is in seconds and overrides the kur's default for these bans
45 only, with 0 meaning never time out... nothing will release it but an
46 C<unban>. Both IPv4 and IPv6 are taken in any spelling, v6 being
47 canonicalized so long and short forms of one address cannot become two
48 bans. For whole ranges see L<cidr_ban|Ereshkigal::App::Command::cidr_ban>.
49  
50 =head1 METHODS
51  
52 The App::Cmd hooks this subcommand supplies... abstract, description,
53 usage_desc, opt_spec, validate_args, and execute.
54  
55 =cut
56  
57 1 ▶sub abstract { return 'ban one or more IPs' }
Subroutine abstract: called CC=1 SCAR=0.0
Pod documented
58  
59 4 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
60 4 return <<'DESCRIPTION';
61 Banish one or more IPs to the underworld.
62  
63 Without --kur every kur gets them, which is usually what you want... each
64 one blocks per its own ports and protocols. With --kur only that instance
65 does, and naming a fan_out gate sends them to every kur behind it.
66  
67 Each IP is answered for separately, so one bad address does not spoil the
68 rest of the request. Re-banning something already held just resets its
69 sentence rather than erroring.
70  
71 --ban-time is in seconds and overrides the kur's default for these bans
72 only. A 0 means never time out, so nothing will release it but an unban.
73  
74 ereshkigal ban 1.2.3.4 5.6.7.8
75 ereshkigal ban --kur sshd 1.2.3.4
76 ereshkigal ban --ban-time 0 1.2.3.4 # until told otherwise
77  
78 Both IPv4 and IPv6 are taken, in any spelling... a v6 address is
79 canonicalized, so long and short forms of one address cannot become two
80 separate bans. For a whole range use cidr-ban.
81 DESCRIPTION
82 } ## end sub description
83  
84 10 ▶sub usage_desc { return '%c ban %o <IP> [<IP> ...]'; }
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
85  
86 10 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
87 return (
88 10 [ 'kur=s', 'kur instance to send the ban to, defaulting to all of them' ],
89 [ 'ban-time=i', 'seconds the bans should last, 0 meaning never time out, defaulting to the kur default' ],
90 );
91 }
92  
93 6 ▶sub validate_args {
Subroutine validate_args: called CC=4 SCAR=14.2
Pod documented
94 6 my ( $self, $opt, $args ) = @_;
95  
96 6 ▶ if ( !@{$args} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$args;}) 1 5 6
97 1 $self->usage_error('at least one IP must be specified');
98 }
99 5 ▶ if ( defined( $opt->ban_time ) && $opt->ban_time !~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->ban_time and not $opt->ban_time =~ /^[0-9]+$/) 0 5 5
Condition66%2/3
defined $opt->ban_time and not $opt->ban_time =~ /^[0-9]+$/
!ll&&!rl&&r
410
MC/DC0%0/2
defined $opt->ban_time and not $opt->ban_time =~ /^[0-9]+$/
defined $opt->ban_time not $opt->ban_time =~ /^[0-9]+$/
Truth table66%2/3
A and not(B)
A= defined $opt->ban_timeB= not $opt->ban_time =~ /^[0-9]+$/
ABresult
0X0
100
111
100 0 $self->usage_error('--ban-time must be a non-negative integer of seconds');
101 }
102  
103 5 return;
104 } ## end sub validate_args
105  
106 5 ▶sub execute {
Subroutine execute: called CC=3 SCAR=11.0
Pod documented
107 5 my ( $self, $opt, $args ) = @_;
108  
109 5 my $ban_args = { 'ips' => $args };
110 5 ▶ if ( defined( $opt->kur ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->kur) 1 4 5
111 1 $ban_args->{kur} = $opt->kur;
112 }
113 5 ▶ if ( defined( $opt->ban_time ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->ban_time) 1 4 5
114 1 $ban_args->{ban_time} = $opt->ban_time;
115 }
116  
117 5 $self->run_command( 'ban', $ban_args );
118  
119 4 return;
120 } ## end sub execute
121  
122 2 1;
« blib/lib/Ereshkigal/App/Command/unban.pm ↑ Summary blib/lib/Ereshkigal/App/Command/status.pm »