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 $cgi = CGI->new();
my ( $t1, $t2 ) = @_;
# Default message
$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->start_html(
-title => $t1,
@ -230,13 +237,17 @@ body{
color:#fff;
padding:10px 50px;
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
'<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";
exit;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -46,10 +46,13 @@ sub getConf {
my $num = $self->__lmConf->lastCfg;
unless ( $confCached and $confCached->{cfgNum} == $num ) {
%$confCached = (
%{ $self->__lmConf->getConf( { cfgNum => $num } ) },
%{ $self->__lmConf->getLocalConf(PORTALSECTION) },
);
my $gConf = $self->__lmConf->getConf( { cfgNum => $num } );
my $lConf = $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, );