Improve configuration backend errors management (#315)

This commit is contained in:
Clément Oudot 2011-07-07 15:08:45 +00:00
parent aa5b65e697
commit 97522f6a8c
8 changed files with 103 additions and 54 deletions

View File

@ -218,7 +218,14 @@ sub abort {
my $self = shift; my $self = shift;
my $cgi = CGI->new(); my $cgi = CGI->new();
my ( $t1, $t2 ) = @_; my ( $t1, $t2 ) = @_;
# Default message
$t2 ||= "See Apache's logs"; $t2 ||= "See Apache's logs";
# Change \n into <br /> for HTML
my $t2html = $t2;
$t2html =~ s#\n#<br />#g;
print $cgi->header( -type => 'text/html; charset=utf8', ); print $cgi->header( -type => 'text/html; charset=utf8', );
print $cgi->start_html( print $cgi->start_html(
-title => $t1, -title => $t1,
@ -230,13 +237,17 @@ body{
color:#fff; color:#fff;
padding:10px 50px; padding:10px 50px;
font-family:sans-serif; font-family:sans-serif;
}
a {
text-decoration:none;
color:#fff;
} }
' '
}, },
); );
print "<h1>$t1</h1><p>$t2</p>"; print "<h1>$t1</h1><p>$t2html</p>";
print print
'<center><img alt="Lemonldap::NG" src="http://lemonldap.ow2.org/logo_lemonldap-ng.png" /></center>'; '<center><a href="http://lemonldap-ng.org">LemonLDAP::NG</a></center>';
print STDERR ( ref($self) || $self ) . " error: $t1, $t2\n"; print STDERR ( ref($self) || $self ) . " error: $t1, $t2\n";
exit; exit;
} }

View File

@ -20,7 +20,7 @@ use Config::IniFiles;
#inherits Lemonldap::NG::Common::Conf::SOAP #inherits Lemonldap::NG::Common::Conf::SOAP
#inherits Lemonldap::NG::Common::Conf::LDAP #inherits Lemonldap::NG::Common::Conf::LDAP
our $VERSION = '1.0.5'; our $VERSION = '1.1.0';
our $msg; our $msg;
our $iniObj; our $iniObj;
@ -70,27 +70,27 @@ sub new {
} }
} }
unless ( $self->{type} ) { unless ( $self->{type} ) {
$msg .= ' Error: configStorage: type is not defined.'; $msg .= "Error: configStorage: type is not defined.\n";
return 0; return 0;
} }
unless ( $self->{type} =~ /^[\w:]+$/ ) { unless ( $self->{type} =~ /^[\w:]+$/ ) {
$msg .= ' Error: configStorage: type is not well formed.'; $msg .= "Error: configStorage: type is not well formed.\n";
} }
$self->{type} = "Lemonldap::NG::Common::Conf::$self->{type}" $self->{type} = "Lemonldap::NG::Common::Conf::$self->{type}"
unless $self->{type} =~ /^Lemonldap::/; unless $self->{type} =~ /^Lemonldap::/;
eval "require $self->{type}"; eval "require $self->{type}";
if ($@) { if ($@) {
$msg .= " Error: Unknown package $self->{type}."; $msg .= "Error: Unknown package $self->{type}.\n";
return 0; return 0;
} }
return 0 unless $self->prereq; return 0 unless $self->prereq;
$self->{mdone}++; $self->{mdone}++;
$msg = "$self->{type} loaded."; $msg .= "$self->{type} loaded.\n";
} }
if ( $self->{localStorage} and not defined( $self->{refLocalStorage} ) ) { if ( $self->{localStorage} and not defined( $self->{refLocalStorage} ) ) {
eval "use $self->{localStorage};"; eval "use $self->{localStorage};";
if ($@) { if ($@) {
$msg .= " Unable to load $self->{localStorage}: $@."; $msg .= "Unable to load $self->{localStorage}: $@.\n";
} }
else { else {
$self->{refLocalStorage} = $self->{refLocalStorage} =
@ -118,7 +118,7 @@ sub saveConf {
foreach my $k (qw(reVHosts cipher)) { foreach my $k (qw(reVHosts cipher)) {
delete( $conf->{$k} ); delete( $conf->{$k} );
} }
$msg = "Configuration $conf->{cfgNum} stored."; $msg .= "Configuration $conf->{cfgNum} stored.\n";
my $tmp = $self->store($conf); my $tmp = $self->store($conf);
return ( $self->unlock() ? $tmp : UNKNOWN_ERROR ); return ( $self->unlock() ? $tmp : UNKNOWN_ERROR );
} }
@ -137,25 +137,26 @@ sub getConf {
and ref( $self->{refLocalStorage} ) and ref( $self->{refLocalStorage} )
and my $res = $self->{refLocalStorage}->get('conf') ) and my $res = $self->{refLocalStorage}->get('conf') )
{ {
$msg = "get configuration from cache without verification."; $msg .= "Get configuration from cache without verification.\n";
return $res; return $res;
} }
else { else {
$args->{cfgNum} ||= $self->lastCfg; $args->{cfgNum} ||= $self->lastCfg;
unless ( $args->{cfgNum} ) { unless ( $args->{cfgNum} ) {
$msg = "No configuration available."; $msg .= "No configuration available.\n";
return 0; return 0;
} }
my $r; my $r;
unless ( ref( $self->{refLocalStorage} ) ) { unless ( ref( $self->{refLocalStorage} ) ) {
$msg = "get remote configuration (localStorage unavailable)."; $msg .= "Get remote configuration (localStorage unavailable).\n";
$r = $self->getDBConf($args); $r = $self->getDBConf($args);
} }
else { else {
eval { $r = $self->{refLocalStorage}->get('conf') }; eval { $r = $self->{refLocalStorage}->get('conf') };
$msg = "Warn: $@" if ($@); $msg = "Warn: $@" if ($@);
if ( ref($r) and $r->{cfgNum} == $args->{cfgNum} ) { if ( ref($r) and $r->{cfgNum} == $args->{cfgNum} ) {
$msg = "configuration unchanged, get configuration from cache."; $msg .=
"Configuration unchanged, get configuration from cache.\n";
} }
else { else {
$r = $self->getDBConf($args); $r = $self->getDBConf($args);
@ -175,8 +176,8 @@ sub getConf {
); );
}; };
if ($@) { if ($@) {
$msg = "Bad key : $@."; $msg .= "Bad key: $@. \n";
return 0; return $r;
} }
} }
return $r; return $r;
@ -207,8 +208,8 @@ sub getLocalConf {
# - Silent exit for other section requests # - Silent exit for other section requests
unless ( -r $file ) { unless ( -r $file ) {
if ( $section eq CONFSECTION ) { if ( $section eq CONFSECTION ) {
$msg = $msg .=
"Cannot read $file to get configuration access parameters."; "Cannot read $file to get configuration access parameters.\n";
return $r; return $r;
} }
return $r; return $r;
@ -218,19 +219,19 @@ sub getLocalConf {
$cfg = Config::IniFiles->new( -file => $file, -allowcontinue => 1 ); $cfg = Config::IniFiles->new( -file => $file, -allowcontinue => 1 );
unless ( defined $cfg ) { unless ( defined $cfg ) {
$msg = "Local config error: " . @Config::IniFiles::errors; $msg .= "Local config error: " . @Config::IniFiles::errors . "\n";
return $r; return $r;
} }
# Check if default section exists # Check if default section exists
unless ( $cfg->SectionExists(DEFAULTSECTION) ) { unless ( $cfg->SectionExists(DEFAULTSECTION) ) {
$msg = "Default section (" . DEFAULTSECTION . ") is missing."; $msg .= "Default section (" . DEFAULTSECTION . ") is missing. \n";
return $r; return $r;
} }
# Check if configuration section exists # Check if configuration section exists
if ( $section eq CONFSECTION and !$cfg->SectionExists(CONFSECTION) ) { if ( $section eq CONFSECTION and !$cfg->SectionExists(CONFSECTION) ) {
$msg = "Configuration section (" . CONFSECTION . ") is missing."; $msg .= "Configuration section (" . CONFSECTION . ") is missing.\n";
return $r; return $r;
} }
} }
@ -243,7 +244,7 @@ sub getLocalConf {
if ( $r->{$_} =~ /^[{\[].*[}\]]$/ || $r->{$_} =~ /^sub\s*{.*}$/ ) { if ( $r->{$_} =~ /^[{\[].*[}\]]$/ || $r->{$_} =~ /^sub\s*{.*}$/ ) {
eval "\$r->{$_} = $r->{$_}"; eval "\$r->{$_} = $r->{$_}";
if ($@) { if ($@) {
$msg = "Warning: error in file $file: $@."; $msg .= "Warning: error in file $file: $@.\n";
return $r; return $r;
} }
} }
@ -262,7 +263,7 @@ sub getLocalConf {
if ( $r->{$_} =~ /^[{\[].*[}\]]$/ || $r->{$_} =~ /^sub\s*{.*}$/ ) { if ( $r->{$_} =~ /^[{\[].*[}\]]$/ || $r->{$_} =~ /^sub\s*{.*}$/ ) {
eval "\$r->{$_} = $r->{$_}"; eval "\$r->{$_} = $r->{$_}";
if ($@) { if ($@) {
$msg = "Warning: error in file $file: $@."; $msg .= "Warning: error in file $file: $@.\n";
return $r; return $r;
} }
} }
@ -277,7 +278,7 @@ sub getLocalConf {
sub setLocalConf { sub setLocalConf {
my ( $self, $conf ) = @_; my ( $self, $conf ) = @_;
eval { $self->{refLocalStorage}->set( "conf", $conf ) }; eval { $self->{refLocalStorage}->set( "conf", $conf ) };
$msg .= "Warn: $@" if ($@); $msg .= "Warn: $@\n" if ($@);
} }
## @method hashRef getDBConf(hashRef args) ## @method hashRef getDBConf(hashRef args)
@ -297,7 +298,7 @@ sub getDBConf {
: $a[0]; : $a[0];
} }
my $conf = $self->load( $args->{cfgNum} ); my $conf = $self->load( $args->{cfgNum} );
$msg = "Get configuration $conf->{cfgNum}."; $msg .= "Get configuration $conf->{cfgNum}.\n";
my $re = Regexp::Assemble->new(); my $re = Regexp::Assemble->new();
foreach ( keys %{ $conf->{locationRules} } ) { foreach ( keys %{ $conf->{locationRules} } ) {
$_ = quotemeta($_); $_ = quotemeta($_);

View File

@ -4,7 +4,7 @@ use strict;
require Storable; require Storable;
use Lemonldap::NG::Common::Conf::_DBI; use Lemonldap::NG::Common::Conf::_DBI;
our $VERSION = '1.0.0'; our $VERSION = '1.1.0';
our @ISA = qw(Lemonldap::NG::Common::Conf::_DBI); our @ISA = qw(Lemonldap::NG::Common::Conf::_DBI);
sub store { sub store {
@ -37,8 +37,8 @@ sub load {
my $r; my $r;
eval { $r = Storable::thaw( $row->[0] ); }; eval { $r = Storable::thaw( $row->[0] ); };
if ($@) { if ($@) {
$Lemonldap::NG::Common::Conf::msg = $Lemonldap::NG::Common::Conf::msg .=
"Bad stored data in conf database: $@"; "Bad stored data in conf database: $@ \n";
return 0; return 0;
} }
return $r; return $r;

View File

@ -4,18 +4,18 @@ use strict;
use Lemonldap::NG::Common::Conf::Constants; #inherits use Lemonldap::NG::Common::Conf::Constants; #inherits
use Lemonldap::NG::Common::Conf::Serializer; use Lemonldap::NG::Common::Conf::Serializer;
our $VERSION = '1.0.0'; our $VERSION = '1.1.0';
sub prereq { sub prereq {
my $self = shift; my $self = shift;
unless ( $self->{dirName} ) { unless ( $self->{dirName} ) {
$Lemonldap::NG::Common::Conf::msg = $Lemonldap::NG::Common::Conf::msg .=
'"dirName" is required in "File" configuration type !'; '"dirName" is required in "File" configuration type ! \n';
return 0; return 0;
} }
unless ( -d $self->{dirName} ) { unless ( -d $self->{dirName} ) {
$Lemonldap::NG::Common::Conf::msg = $Lemonldap::NG::Common::Conf::msg .=
"Directory \"$self->{dirName}\" does not exist !"; "Directory \"$self->{dirName}\" does not exist ! \n";
return 0; return 0;
} }
1; 1;
@ -43,8 +43,8 @@ sub lock {
return 0 if ( $self->isLocked ); return 0 if ( $self->isLocked );
} }
unless ( open F, ">" . $self->{dirName} . "/lmConf.lock" ) { unless ( open F, ">" . $self->{dirName} . "/lmConf.lock" ) {
$Lemonldap::NG::Common::Conf::msg = $Lemonldap::NG::Common::Conf::msg .=
"Unable to lock (" . $self->{dirName} . "/lmConf.lock)\n"; "Unable to lock (" . $self->{dirName} . "/lmConf.lock) \n";
return 0; return 0;
} }
print F $$; print F $$;
@ -71,7 +71,7 @@ sub store {
unless ( open FILE, unless ( open FILE,
'>' . $self->{dirName} . "/lmConf-" . $fields->{cfgNum} ) '>' . $self->{dirName} . "/lmConf-" . $fields->{cfgNum} )
{ {
$Lemonldap::NG::Common::Conf::msg = "Open file failed: $!"; $Lemonldap::NG::Common::Conf::msg .= "Open file failed: $! \n";
$self->unlock; $self->unlock;
return UNKNOWN_ERROR; return UNKNOWN_ERROR;
} }

View File

@ -10,7 +10,7 @@ use Net::LDAP;
use Lemonldap::NG::Common::Conf::Constants; #inherits use Lemonldap::NG::Common::Conf::Constants; #inherits
use Lemonldap::NG::Common::Conf::Serializer; use Lemonldap::NG::Common::Conf::Serializer;
our $VERSION = '1.0.0'; our $VERSION = '1.1.0';
BEGIN { BEGIN {
*Lemonldap::NG::Common::Conf::ldap = \&ldap; *Lemonldap::NG::Common::Conf::ldap = \&ldap;
@ -20,8 +20,8 @@ sub prereq {
my $self = shift; my $self = shift;
foreach ( 'ldapServer', 'ldapConfBase', 'ldapBindDN', 'ldapBindPassword' ) { foreach ( 'ldapServer', 'ldapConfBase', 'ldapBindDN', 'ldapBindPassword' ) {
unless ( $self->{$_} ) { unless ( $self->{$_} ) {
$Lemonldap::NG::Common::Conf::msg = $Lemonldap::NG::Common::Conf::msg .=
"$_ is required in LDAP configuration type"; "$_ is required in LDAP configuration type \n";
return 0; return 0;
} }
} }
@ -29,14 +29,24 @@ sub prereq {
} }
sub available { sub available {
my $self = shift; my $self = shift;
unless ( $self->ldap ) {
return 0;
}
my $search = $self->ldap->search( my $search = $self->ldap->search(
base => $self->{ldapConfBase}, base => $self->{ldapConfBase},
filter => '(objectClass=applicationProcess)', filter => '(objectClass=applicationProcess)',
scope => 'one', scope => 'one',
attrs => ['cn'], attrs => ['cn'],
); );
$self->logError($search) if ( $search->code );
if ( $search->code ) {
$self->logError($search);
return 0;
}
my @entries = $search->entries(); my @entries = $search->entries();
my @conf; my @conf;
foreach (@entries) { foreach (@entries) {
@ -125,6 +135,11 @@ sub unlock {
sub store { sub store {
my ( $self, $fields ) = @_; my ( $self, $fields ) = @_;
unless ( $self->ldap ) {
return 0;
}
$fields = $self->serialize($fields); $fields = $self->serialize($fields);
my $confName = "lmConf-" . $fields->{cfgNum}; my $confName = "lmConf-" . $fields->{cfgNum};
@ -145,7 +160,11 @@ sub store {
] ]
); );
$self->logError($add) if ( $add->code ); if ( $add->code ) {
$self->logError($add);
return 0;
}
$self->ldap->unbind() && delete $self->{ldap}; $self->ldap->unbind() && delete $self->{ldap};
$self->unlock; $self->unlock;
return $fields->{cfgNum}; return $fields->{cfgNum};
@ -153,6 +172,11 @@ sub store {
sub load { sub load {
my ( $self, $cfgNum, $fields ) = @_; my ( $self, $cfgNum, $fields ) = @_;
unless ( $self->ldap ) {
return;
}
my $f; my $f;
my $confName = "lmConf-" . $cfgNum; my $confName = "lmConf-" . $cfgNum;
my $confDN = "cn=$confName," . $self->{ldapConfBase}; my $confDN = "cn=$confName," . $self->{ldapConfBase};
@ -163,7 +187,12 @@ sub load {
scope => 'base', scope => 'base',
attrs => ['description'], attrs => ['description'],
); );
$self->logError($search) if ( $search->code );
if ( $search->code ) {
$self->logError($search);
return;
}
my $entry = $search->shift_entry(); my $entry = $search->shift_entry();
my @confValues = $entry->get_value('description'); my @confValues = $entry->get_value('description');
foreach (@confValues) { foreach (@confValues) {
@ -182,6 +211,10 @@ sub load {
sub delete { sub delete {
my ( $self, $cfgNum ) = @_; my ( $self, $cfgNum ) = @_;
unless ( $self->ldap ) {
return 0;
}
my $confDN = "cn=lmConf-" . $cfgNum . "," . $self->{ldapConfBase}; my $confDN = "cn=lmConf-" . $cfgNum . "," . $self->{ldapConfBase};
my $delete = $self->ldap->delete($confDN); my $delete = $self->ldap->delete($confDN);
$self->ldap->unbind() && delete $self->{ldap}; $self->ldap->unbind() && delete $self->{ldap};
@ -191,10 +224,10 @@ sub delete {
sub logError { sub logError {
my $self = shift; my $self = shift;
my $ldap_operation = shift; my $ldap_operation = shift;
$Lemonldap::NG::Common::Conf::msg = $Lemonldap::NG::Common::Conf::msg .=
"LDAP error " "LDAP error "
. $ldap_operation->code . ": " . $ldap_operation->code . ": "
. $ldap_operation->error . "\n"; . $ldap_operation->error . " \n";
} }
1; 1;

View File

@ -4,7 +4,7 @@ use strict;
use Lemonldap::NG::Common::Conf::Serializer; use Lemonldap::NG::Common::Conf::Serializer;
use Lemonldap::NG::Common::Conf::_DBI; use Lemonldap::NG::Common::Conf::_DBI;
our $VERSION = '1.0.0'; our $VERSION = '1.1.0';
our @ISA = qw(Lemonldap::NG::Common::Conf::_DBI); our @ISA = qw(Lemonldap::NG::Common::Conf::_DBI);
sub store { sub store {
@ -43,7 +43,8 @@ sub load {
$res->{ $row[1] } = $row[2]; $res->{ $row[1] } = $row[2];
} }
unless ($res) { unless ($res) {
$Lemonldap::NG::Common::Conf::msg .= "No configuration $cfgNum found"; $Lemonldap::NG::Common::Conf::msg .=
"No configuration $cfgNum found \n";
return 0; return 0;
} }
$res->{cfgNum} = $cfgNum; $res->{cfgNum} = $cfgNum;

View File

@ -3,7 +3,7 @@ package Lemonldap::NG::Common::Conf::SOAP;
use strict; use strict;
use SOAP::Lite; use SOAP::Lite;
our $VERSION = '1.0.0'; our $VERSION = '1.1.0';
#parameter proxy Url of SOAP service #parameter proxy Url of SOAP service
#parameter proxyOptions SOAP::Lite parameters #parameter proxyOptions SOAP::Lite parameters
@ -23,8 +23,8 @@ our ( $username, $password ) = ( '', '' );
sub prereq { sub prereq {
my $self = shift; my $self = shift;
unless ( $self->{proxy} ) { unless ( $self->{proxy} ) {
$Lemonldap::NG::Common::Conf::msg = $Lemonldap::NG::Common::Conf::msg .=
'"proxy" parameter is required in "SOAP" configuration type'; "proxy parameter is required in SOAP configuration type \n";
return 0; return 0;
} }
1; 1;

View File

@ -46,10 +46,13 @@ sub getConf {
my $num = $self->__lmConf->lastCfg; my $num = $self->__lmConf->lastCfg;
unless ( $confCached and $confCached->{cfgNum} == $num ) { unless ( $confCached and $confCached->{cfgNum} == $num ) {
%$confCached = ( my $gConf = $self->__lmConf->getConf( { cfgNum => $num } );
%{ $self->__lmConf->getConf( { cfgNum => $num } ) }, my $lConf = $self->__lmConf->getLocalConf(PORTALSECTION);
%{ $self->__lmConf->getLocalConf(PORTALSECTION) }, unless ( ref($gConf) and ref($lConf) ) {
); $self->abort( "Cannot get configuration",
$Lemonldap::NG::Common::Conf::msg );
}
%$confCached = ( %$gConf, %$lConf );
} }
%$self = ( %$self, %$confCached, %args, ); %$self = ( %$self, %$confCached, %args, );