Debian Codegraph @

lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Api/Misc.pm

raw
1 # Miscenalleous endpoints
2 package Lemonldap::NG::Manager::Api::Misc;
3
4 our $VERSION = '2.0.10';
5
6 package Lemonldap::NG::Manager::Api;
7
8 use strict;
9 use Mouse;
10 extends 'Lemonldap::NG::Manager::Api::Common';
11
12 # Health-check endpoint
13 sub status {
14 my ( $self, $req ) = @_;
15 my $code = 200;
16 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 my $conf =
27 $self->_confAcc->getDBConf( { cfgNum => $self->_confAcc->lastCfg } );
28 unless ( $conf->{cfgNum} ) {
29 $code = 503;
30 $response->{status} = "ko";
31 $response->{status_config} = "ko";
32
33 }
34
35 # Test session backend
36 my $status = $self->_getSessionDBState( $self->_getSSOMod );
37 if ( $status == 0 ) {
38 $code = 503;
39 $response->{status} = "ko";
40 $response->{status_sessions} = "ko";
41 }
42 elsif ( $status == 2 ) {
43 $response->{status_sessions} = "unknown";
44 }
45
46 # Test psession backend
47 $status = $self->_getSessionDBState( $self->_getPersistentMod );
48 if ( $status == 0 ) {
49 $code = 503;
50 $response->{status} = "ko";
51 $response->{status_psessions} = "ko";
52 }
53 elsif ( $status == 2 ) {
54 $response->{status_psessions} = "unknown";
55 }
56
57 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 sub _getSessionDBState {
73 my ( $self, $mod ) = @_;
74
75 my $fakeobj;
76
77 eval { $fakeobj = $self->_getObjectSessionModule($mod); };
78
79 # If we could not instanciate the session module directly, bail
80 return 2 unless $fakeobj;
81
82 my $fakeobj_store = $fakeobj->{object_store};
83
84 # If the Apache::Session object does not have an object store, bail
85 return 2 unless $fakeobj_store;
86
87 # Handle DBI-type session stores
88 if ( $fakeobj->{object_store}->isa("Apache::Session::Store::DBI") ) {
89
90 # The 'connection' method will fail if the DB is unreachable
91 # this is good enough a test for now
92 eval { $fakeobj->{object_store}->connection($fakeobj) };
93 return $@ ? 0 : 1;
94 }
95
96 # Handle MongoDB
97 if ( $fakeobj->{object_store}->isa("Apache::Session::Store::MongoDB") ) {
98
99 # Try to find collection
100 eval {
101 $fakeobj->{object_store}->connection($fakeobj);
102 $fakeobj->{object_store}->{collection}->estimated_document_count;
103 };
104 return $@ ? 0 : 1;
105 }
106
107 # We don't know
108 return 2;
109 }
110
111 sub _getObjectSessionModule {
112 my ( $self, $mod ) = @_;
113 my $class = $mod->{module};
114 eval "require $class;";
115
116 my $fakeSession = { args => $mod->{options}, };
117 bless $fakeSession, $class;
118 $fakeSession->populate;
119 return $fakeSession;
120 }
121
122 1;