blib/lib/Lemonldap/NG/Manager/Api/Misc.pm

filter: Statementstmt 75.9% 41 / 54 Branchbran 45.4% 10 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 68.2% 56 / 82 CC 12 SCAR 21.9
CC
12
Cov
67%
_getSessionDBState
13.8
status
7.2
BEGIN
1.0
CRAP 8.9
« blib/lib/Lemonldap/NG/Manager/Build/Attributes.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Api/Providers/SamlSp.pm »
1 # Miscenalleous endpoints
2 package Lemonldap::NG::Manager::Api::Misc;
3  
4 28 our $VERSION = '2.0.10';
5  
6 package Lemonldap::NG::Manager::Api;
7  
8 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 extends 'Lemonldap::NG::Manager::Api::Common';
11  
12 # Health-check endpoint
13 2 ▶sub status {
Subroutine status: called CC=6 SCAR=19.8
Pod undocumented
14 2 my ( $self, $req ) = @_;
15 2 my $code = 200;
16 2 my $response = {
17 name => "LemonLDAP::NG Manager API",
18 version => $Lemonldap::NG::Manager::VERSION,
19 status => "ok",
20 status_config => "ok",
21 status_sessions => "ok",
22 status_psessions => "ok",
23 };
24  
25 # Test configuration backend
26 2 my $conf =
27 $self->_confAcc->getDBConf( { cfgNum => $self->_confAcc->lastCfg } );
28 2 ▶ unless ( $conf->{cfgNum} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->{"cfgNum"}) 1 1 2
29 1 $code = 503;
30 1 $response->{status} = "ko";
31 1 $response->{status_config} = "ko";
32  
33 }
34  
35 # Test session backend
36 2 my $status = $self->_getSessionDBState( $self->_getSSOMod );
37 2 ▶ if ( $status == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status == 0) { } 0 2 2
elsif ($status == 2) { } 2 0 2
38 0 $code = 503;
39 0 $response->{status} = "ko";
40 0 $response->{status_sessions} = "ko";
41 }
42 elsif ( $status == 2 ) {
43 2 $response->{status_sessions} = "unknown";
44 }
45  
46 # Test psession backend
47 2 $status = $self->_getSessionDBState( $self->_getPersistentMod );
48 2 ▶ if ( $status == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status == 0) { } 0 2 2
elsif ($status == 2) { } 2 0 2
49 0 $code = 503;
50 0 $response->{status} = "ko";
51 0 $response->{status_psessions} = "ko";
52 }
53 elsif ( $status == 2 ) {
54 2 $response->{status_psessions} = "unknown";
55 }
56  
57 2 return $self->sendJSONresponse(
58 $req, $response,
59 code => $code,
60 pretty => 1
61 );
62 }
63  
64 # Apache::Session has no API for healthchecking yet. Until it does, we have to
65 # break the encapsulation model to get the info. This needs to be reworked.
66 # So far, we only implement the check for:
67 # * Apache::Session::DBI
68 # * Apache::Session::Browseable::DBI (Postgresq, MySQL)
69 # * Apache::Session::MongoDB
70 #
71 # Returns 0 for ko, 1 for ok and 2 for unknown
72 4 ▶sub _getSessionDBState {
Subroutine _getSessionDBState: called CC=7 SCAR=26.2
73 4 my ( $self, $mod ) = @_;
74  
75 4 my $fakeobj;
76  
77 4 eval { $fakeobj = $self->_getObjectSessionModule($mod); };
78  
79 # If we could not instanciate the session module directly, bail
80 4 ▶ return 2 unless $fakeobj;
Branch: 1 branch
BranchTrueFalseTotal
unless $fakeobj 0 4 4
81  
82 4 my $fakeobj_store = $fakeobj->{object_store};
83  
84 # If the Apache::Session object does not have an object store, bail
85 4 ▶ return 2 unless $fakeobj_store;
Branch: 1 branch
BranchTrueFalseTotal
unless $fakeobj_store 0 4 4
86  
87 # Handle DBI-type session stores
88 4 ▶ if ( $fakeobj->{object_store}->isa("Apache::Session::Store::DBI") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fakeobj->{"object_store"}->isa("Apache::Session::Store::DBI")) 0 4 4
89  
90 # The 'connection' method will fail if the DB is unreachable
91 # this is good enough a test for now
92 0 eval { $fakeobj->{object_store}->connection($fakeobj) };
93 0 ▶ return $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
94 }
95  
96 # Handle MongoDB
97 4 ▶ if ( $fakeobj->{object_store}->isa("Apache::Session::Store::MongoDB") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fakeobj->{"object_store"}->isa("Apache::Session::Store::MongoDB")) 0 4 4
98  
99 # Try to find collection
100 0 eval {
101 0 $fakeobj->{object_store}->connection($fakeobj);
102 0 $fakeobj->{object_store}->{collection}->estimated_document_count;
103 };
104 0 ▶ return $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
105 }
106  
107 # We don't know
108 4 return 2;
109 }
110  
111 4 ▶sub _getObjectSessionModule {
Subroutine _getObjectSessionModule: called CC=1 SCAR=0.0
112 4 my ( $self, $mod ) = @_;
113 4 my $class = $mod->{module};
114 4 eval "require $class;";
115  
116 4 my $fakeSession = { args => $mod->{options}, };
117 4 bless $fakeSession, $class;
118 4 $fakeSession->populate;
119 4 return $fakeSession;
120 }
121  
122 28 1;
« blib/lib/Lemonldap/NG/Manager/Build/Attributes.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Api/Providers/SamlSp.pm »