blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSecAgent.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 105 CC 14 SCAR 36.2
CC
14
Cov
0%
controlUrl
72.0
init
20.0
sendIpAlerts
20.0
CRAP 37.2
1 package Lemonldap::NG::Portal::Plugins::CrowdSecAgent;
2  
3 use strict;
4 use Mouse;
5 use Lemonldap::NG::Portal::Main::Constants qw(
6 PE_BADCERTIFICATE
7 PE_BADCREDENTIALS
8 PE_BADOTP
9 PE_CAPTCHAERROR
10 PE_MALFORMEDUSER
11 PE_OK
12 PE_OPENID_BADID
13 PE_SAML_SIGNATURE_ERROR
14 PE_SENDRESPONSE
15 PE_USERNOTFOUND
16 );
17  
18 our $VERSION = '2.22.0';
19  
20 our @ALERTS = (
21 PE_USERNOTFOUND, PE_BADCREDENTIALS,
22 PE_BADCERTIFICATE, PE_MALFORMEDUSER,
23 PE_SAML_SIGNATURE_ERROR, PE_OPENID_BADID,
24 PE_CAPTCHAERROR, PE_BADOTP,
25 );
26  
27 extends 'Lemonldap::NG::Portal::Main::Plugin';
28 with 'Lemonldap::NG::Portal::Lib::CrowdSec';
29  
30 has rule => ( is => 'rw', default => sub { 0 } );
31  
32 has filters => ( is => 'rw', default => sub { {} } );
33  
34 # Entrypoint
35 use constant aroundSub => {
36  
37 # Filter function (if crowdsecFilters is set)
38 controlUrl => 'controlUrl',
39  
40 # Generate alerts for bad credentials
41 getUser => 'sendIpAlerts',
42 authenticate => 'sendIpAlerts'
43 };
44  
45 sub init {
46 my ($self) = @_;
47 $self->_init or return 0;
48 unless ($self->conf->{crowdsecMachineId}
49 and $self->conf->{crowdsecPassword} )
50 {
51 $self->logger->error(
52 'Crowdsec report enabled without machine_id and password');
53 return 0;
54 }
55 $self->rule(
56 $self->p->buildRule( $self->conf->{crowdsecAgent}, 'crowdsecAgent' ) );
57  
58 if ( $self->conf->{crowdsecFilters} ) {
59 if ( -d $self->conf->{crowdsecFilters} ) {
60 with 'Lemonldap::NG::Portal::Lib::CrowdSecFilter';
61 $self->initializeFilters;
62 }
63 else {
64 $self->logger->error(
65 'Crowdsec filter directory not found, ignoring');
66 }
67 }
68 $self->conf->{crowdSecAgentResponseCode} ||= 404;
69  
70 return 1;
71 }
72  
73 sub sendIpAlerts {
74 my ( $self, $sub, $req ) = @_;
75 my $ret = $sub->($req);
76  
77 if ( !$self->rule->( $req, $req->sessionInfo ) ) {
78 $self->logger->debug('Crowdsec-agent disabled for this env');
79 return $ret;
80 }
81  
82 # Nothing to do if config doesn't allow alerts or if auth is OK
83 unless ( grep { $_ == $ret } @ALERTS ) {
84 $self->logger->debug(
85 "Crowdsec-Agent: not an authentication error: code $ret");
86 return $ret;
87 }
88 my $msg = 'Authentication failed: '
89 . &Lemonldap::NG::Portal::Main::Constants::portalConsts->{$ret};
90 $self->alert( $req->address, $msg,
91 { scenario => 'llng/badcredentials', reason => $msg, } )
92 ? $self->auditLog(
93 $req,
94 code => 200,
95 message => "Alert sent to Crowdsec: $msg",
96 ip => $req->address
97 )
98 : $self->logger->error(
99 "Unable to send alert to Crowdsec (was '$msg' for " . $req->address );
100 return $ret;
101 }
102  
103 sub controlUrl {
104 my ( $self, $sub, $req ) = @_;
105 my $ret = $sub->($req);
106 return $ret if $ret;
107  
108 if ( !$self->rule->( $req, $req->sessionInfo ) ) {
109 $self->logger->debug('Crowdsec-agent disabled for this env');
110 return $ret;
111 }
112  
113 return $ret unless $self->filters;
114 if ( $self->filters->{url}
115 and $req->env->{REQUEST_URI} =~ $self->filters->{url} )
116 {
117 my $msg = 'Bad URI detected: ' . $req->env->{REQUEST_URI};
118 $self->alert( $req->address, $msg,
119 { scenario => 'llng/urlscan', reason => $msg, } )
120 ? $self->auditLog(
121 $req,
122 code => 200,
123 message => "Alert sent to Crowdsec: $msg",
124 ip => $req->address,
125 uri => $req->env->{REQUEST_URI},
126 matchingPart => $&,
127 )
128 : $self->logger->error(
129 "Unable to send alert to Crowdsec (was '$msg' for "
130 . $req->address );
131  
132 $req->response(
133  
134 # Case "redirection", crowdSecAgentResponseValue is the "location" value
135 $self->conf->{crowdSecAgentResponseCode} =~ /^3/
136 ? $self->p->sendRawHtml(
137 $req,
138 $self->conf->{crowdSecAgentResponseValue},
139 {
140 code => $self->conf->{crowdSecAgentResponseCode},
141 headers => [
142 Location => (
143 $self->conf->{crowdSecAgentResponseValue}
144 || 'https://somewhere.else'
145 )
146 ],
147 }
148 )
149  
150 # Case "custom response
151 : $self->conf->{crowdSecAgentResponseValue}
152 ? $self->p->sendRawHtml(
153 $req,
154 $self->conf->{crowdSecAgentResponseValue},
155 { code => $self->conf->{crowdSecAgentResponseCode} }
156 )
157  
158 # Case "default Lemon response
159 : $self->sendError(
160 $req, '', $self->conf->{crowdSecAgentResponseCode}
161 )
162 );
163 return PE_SENDRESPONSE;
164 }
165 return $ret;
166 }
167  
168 1;