blib/lib/CrowdSec/Client.pm

filter: Statementstmt 30.6% 34 / 111 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 58.3% 7 / 12 Podpod 50.0% 2 / 4 totaltotal 22.5% 43 / 191 CC 30 SCAR 45.2
CC
30
Cov
20%
testIp
182.0
login
90.0
banIp
72.0
CRAP 92.3
↑ Summary
1 package CrowdSec::Client;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use POSIX "strftime";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.04';
12  
13 # Default alert template
14 1 our %DEFAULTS = (
15 duration => '4h',
16 labels => undef,
17 origin => 'CrowdSec::Client',
18 reason => 'Banned by CrowdSec::Client',
19 scenario => 'Banned by CrowdSec::Client',
20 simulated => JSON::false,
21 type => 'ban',
22 );
23  
24 # Watcher accessors
25 1 has machineId => ( is => 'ro' );
26 1 has password => ( is => 'ro' );
27 1 has token => ( is => 'rw' );
28 1 has tokenVal => ( is => 'rw' );
29 1 has strictSsl => ( is => 'ro', default => 1, );
30 1 has baseUrl => ( is => 'ro' );
31  
32 # Bouncer accessors
33 1 has apiKey => ( is => 'ro' );
34  
35 # Common accessors
36 has userAgent => (
37 is => 'ro',
38 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
39 0 ▶ return LWP::UserAgent->new(
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->strictSsl ? : 0 0 0
40 $_[0]->strictSsl
41 ? ()
42 : (
43 ssl_opts => {
44 verify_hostname => 0,
45 SSL_verify_mode => 0
46 }
47 )
48 );
49 }
50 1 );
51  
52 1 has autoLogin => ( is => 'ro' );
53  
54 1 has error => ( is => 'rw' );
55  
56 0 ▶sub login {
Subroutine login: not called CC=9 SCAR=45.0
Pod documented
57 0 my ($self) = @_;
58 0 my $error = 0;
59 0 ▶ return 1 if $self->tokenIsvalid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tokenIsvalid 0 0 0
60 0 foreach my $k (qw(machineId password baseUrl)) {
61 0 ▶ unless ( $self->{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$k}) 0 0 0
62 0 $self->error("Missing parameter: $k");
63 0 $error++;
64 }
65 }
66 0 ▶ return if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
67 0 my $request = POST(
68 $self->baseUrl . '/v1/watchers/login',
69 Accept => 'application/json',
70 Content_Type => 'application/json',
71 Content => JSON::to_json(
72 {
73 machine_id => $self->machineId,
74 password => $self->password,
75 scenarios => [],
76 }
77 )
78 );
79 0 my $response = $self->userAgent->request($request);
80 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
81 0 eval {
82 0 my $tmp = JSON::from_json( $response->content );
83 0 $self->token( $tmp->{token} );
84 0 $self->tokenVal( str2time( $tmp->{expire} ) );
85 };
86 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
87 0 $self->error("Bad response content from CrowdSec server: $@");
88 0 return;
89 }
90 0 ▶ if ( !$self->token and !$self->tokenVal ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->token or $self->tokenVal) 0 0 0
Condition0%0/3
$self->token or $self->tokenVal
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->token or $self->tokenVal
$self->token $self->tokenVal
Truth table0%0/3
A or B
A= $self->tokenB= $self->tokenVal
ABresult
1X1
011
000
91 0 $self->error(
92 "Missing token and expire fields in CrowdSec response: "
93 . $response->content );
94 }
95 0 return 1;
96 }
97 else {
98 0 $self->error(
99 "Bad response from CrowdSec server: " . $response->status_line );
100 0 return;
101 }
102 }
103  
104 0 ▶sub banIp {
Subroutine banIp: not called CC=8 SCAR=42.8
Pod documented
105 0 my ( $self, $ip, %params ) = @_;
106 0 ▶ unless ( $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 0 0 0
107 0 $self->error('Usage: banIp($ip, \%options)');
108 0 return;
109 }
110 0 ▶ if ( $self->autoLogin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autoLogin) 0 0 0
111 0 ▶ unless ( $self->login ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->login) 0 0 0
112 0 return;
113 }
114 }
115 0 ▶ unless ( $self->token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->token) 0 0 0
116 0 $self->error("No valid token");
117 0 return;
118 }
119 0 my %prm = ( %DEFAULTS, %params );
120 0 ▶ $prm{simulated} = $prm{simulated} ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$prm{'simulated'} ? : 0 0 0
121 0 my $stamp = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime(time);
122 my $decision = [
123 {
124 capacity => 0,
125 created_at => $stamp,
126 decisions => [
127 {
128 duration => $prm{duration},
129 origin => $prm{origin},
130 scenario => $prm{scenario},
131 scope => 'Ip',
132 type => $prm{type},
133 value => $ip,
134 }
135 ],
136 events => [],
137 events_count => 1,
138 labels => $prm{labels},
139 leakspeed => '0',
140 message => $prm{reason},
141 scenario => $prm{reason},
142 scenario_hash => '',
143 scenario_version => '',
144 simulated => $prm{simulated},
145 0 source => {
146 ip => $ip,
147 scope => 'Ip',
148 value => $ip,
149 },
150 start_at => $stamp,
151 stop_at => $stamp,
152 }
153 ];
154 0 my $request = POST(
155 $self->baseUrl . '/v1/alerts',
156 Authorization => 'Bearer ' . $self->token,
157 Content_Type => 'application/json',
158 Content => JSON::to_json($decision),
159 );
160 0 my $response = $self->userAgent->request($request);
161 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
162 0 my $response_content = $response->content;
163 0 my $res = eval { JSON::from_json($response_content)->[0] };
164 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
165 0 $self->error(
166 "CrowdSec didn't return an array: " . $response->content );
167 0 return;
168 }
169 0 return $res;
170 }
171 else {
172 0 print "Échec de la requête : " . $response->status_line . "\n";
173 0 return;
174 }
175 }
176  
177 0 ▶sub testIp {
Subroutine testIp: not called CC=13 SCAR=52.0
Pod undocumented
178 0 my ( $self, $ip ) = @_;
179 0 my $error = 0;;
180 0 foreach my $k (qw(apiKey baseUrl)) {
181 0 ▶ unless ( $self->{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$k}) 0 0 0
182 0 $self->error("Missing parameter: $k");
183 0 $error++;
184 }
185 }
186 0 ▶ unless ($ip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 0 0 0
187 0 $self->error('Missing IP');
188 0 $error++;
189 }
190 0 ▶ return (0, $self->error) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
191 0 my $response = $self->userAgent->get(
192 $self->baseUrl . "/v1/decisions?ip=$ip",
193 Accept => 'application/json',
194 'X-Api-Key' => $self->apiKey,
195 );
196 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
197 0 my @decisions;
198 0 ▶ return 0 if !$response->content or $response->content eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if not $response->content or $response->content eq "null" 0 0 0
Condition0%0/3
not $response->content or $response->content eq "null"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response->content or $response->content eq "null"
not $response->content $response->content eq "null"
Truth table0%0/3
not(A) or B
A= not $response->contentB= $response->content eq "null"
ABresult
1X1
011
000
199 0 eval {
200 0 my $tmp = JSON::from_json( $response->content );
201 0 @decisions = @$tmp;
202 };
203 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
204 0 $self->error("Bad response content from CrowdSec server: $@");
205 0 return (0, $self->error);
206 }
207 0 ▶ return 0 unless @decisions;
Branch: 1 branch
BranchTrueFalseTotal
unless @decisions 0 0 0
208 0 foreach my $decision (@decisions) {
209 0 ▶ if ( $decision->{type} and $decision->{type} eq 'ban' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decision->{"type"} and $decision->{"type"} eq "ban") 0 0 0
Condition0%0/3
$decision->{"type"} and $decision->{"type"} eq "ban"
!ll&&!rl&&r
000
MC/DC0%0/2
$decision->{"type"} and $decision->{"type"} eq "ban"
$decision->{"type"} $decision->{"type"} eq "ban"
Truth table0%0/3
A and B
A= $decision->{"type"}B= $decision->{"type"} eq "ban"
ABresult
0X0
100
111
210 0 return 1;
211 }
212 }
213 0 return 0;
214 };
215 }
216  
217 0 ▶sub tokenIsvalid {
Subroutine tokenIsvalid: not called CC=2 SCAR=17.9
Pod undocumented
218 0 my ($self) = @_;
219 0 ▶ return ( $self->tokenVal and ( $self->tokenVal > time ) );
Condition0%0/3
$self->tokenVal && $self->tokenVal > time
!ll&&!rl&&r
000
MC/DC0%0/2
$self->tokenVal && $self->tokenVal > time
$self->tokenVal $self->tokenVal > time
Truth table0%0/3
A && B
A= $self->tokenValB= $self->tokenVal > time
ABresult
0X0
100
111
220 }
221  
222 1 1;
223 __END__
↑ Summary