Commit deb28bc9 authored by Xavier Guimard's avatar Xavier Guimard

Replace lmLog by logger-> (#857)

parent b7d4dac6
......@@ -35,6 +35,7 @@ lib/Lemonldap/NG/Common/Crypto.pm
lib/Lemonldap/NG/Common/FormEncode.pm
lib/Lemonldap/NG/Common/Logger/Apache2.pm
lib/Lemonldap/NG/Common/Logger/Std.pm
lib/Lemonldap/NG/Common/Logger/Syslog.pm
lib/Lemonldap/NG/Common/Module.pm
lib/Lemonldap/NG/Common/Notifications.pm
lib/Lemonldap/NG/Common/Notifications/DBI.pm
......
......@@ -173,7 +173,7 @@ sub getConf {
unless ( ref( $self->{refLocalStorage} ) ) {
$msg .= "Get remote configuration (localStorage unavailable).\n";
$r = $self->getDBConf($args);
return undef unless ($r->{cfgNum});
return undef unless ( $r->{cfgNum} );
$self->setDefault( $r, $args->{localPrm} );
$self->compactConf($r);
}
......
......@@ -96,14 +96,14 @@ sub store {
sub load {
my ( $self, $cfgNum, $fields ) = @_;
my ($f,$filename);
my ( $f, $filename );
if ( -e $self->_file($cfgNum) ) {
$filename = $self->_file($cfgNum);
}
elsif ( -e "$self->{dirName}/lmConf-$cfgNum.js" ) {
$filename = "$self->{dirName}/lmConf-$cfgNum.js";
}
if($filename) {
if ($filename) {
local $/ = '';
my $ret;
unless ( open FILE, $filename ) {
......
......@@ -18,7 +18,7 @@ sub compactConf {
$keep{$mod} = 1;
}
if ( $keep{combination} ) {
foreach my $md (keys %{$conf->{combModules}}) {
foreach my $md ( keys %{ $conf->{combModules} } ) {
$_ = $conf->{combModules}->{$md}->{type};
s/^(\w+).*$/lc($1)/e;
s/OpenIDConnect/oidc/i;
......@@ -35,7 +35,7 @@ sub compactConf {
}
}
}
foreach my $key (keys %$authParameters) {
foreach my $key ( keys %$authParameters ) {
my $mod = $key;
$mod =~ s/Params$//;
unless ( $keep{$mod} ) {
......
......@@ -28,14 +28,14 @@ extends 'Lemonldap::NG::Common::Conf::AccessLib';
#@return keyvalue (string, int or hashref)
sub getConfKey {
my ( $self, $req, $key, @args ) = @_;
$self->lmLog( "Search for $key in conf", 'debug' );
$self->logger->debug("Search for $key in conf");
# Verify that cfgNum has been asked
unless ( defined $req->params('cfgNum') ) {
$req->error("Missing configuration number");
return undef;
}
$self->lmLog( "Cfgnum set to " . $req->params('cfgNum'), 'debug' );
$self->logger->debug( "Cfgnum set to " . $req->params('cfgNum') );
# when 'latest' => replace by last cfgNum
if ( $req->params('cfgNum') eq 'latest' ) {
......@@ -117,7 +117,7 @@ sub getConfByNum {
#@return PSGI JSON response
sub complexNodesRoot {
my ( $self, $req, $query, $tpl ) = @_;
$self->lmLog( "Query for $query template keys", 'debug' );
$self->logger->debug("Query for $query template keys");
my $tmp = $self->getConfKey( $req, $query );
return $self->sendError( $req, undef, 400 ) if ( $req->error );
......@@ -170,7 +170,7 @@ sub virtualHosts {
my ( $id, $resp ) = ( 1, [] );
my $vhk = eval { $self->getConfKey( $req, $query )->{$vh} } // {};
return $self->sendError( $req, undef, 400 ) if ( $req->error );
$self->lmLog( "Query for $vh/$query keys", 'debug' );
$self->logger->debug("Query for $vh/$query keys");
# Keys are ordered except 'default' which must be at the end
foreach my $r (
......@@ -210,7 +210,7 @@ sub virtualHosts {
return $self->sendJSONresponse( $req, $resp );
}
elsif ( $query =~ /^vhost(?:(?:Aliase|Http)s|Maintenance|Port|Type)$/ ) {
$self->lmLog( "Query for $vh/$query key", 'debug' );
$self->logger->debug("Query for $vh/$query key");
# TODO: verify how this is done actually
my $k1 = $self->getConfKey( $req, 'vhostOptions' );
......@@ -593,14 +593,14 @@ sub combModules {
foreach my $mod ( keys %$val ) {
my $tmp;
$tmp->{title} = $mod;
$tmp->{id} = "combModules/$mod";
$tmp->{type} = 'cmbModule';
$tmp->{id} = "combModules/$mod";
$tmp->{type} = 'cmbModule';
$tmp->{data}->{$_} = $val->{$mod}->{$_} foreach (qw(type for));
my $over = $val->{$mod}->{over} // {};
$tmp->{data}->{over} = [ map { [ $_, $over->{$_} ] } keys %$over ];
push @$res, $tmp;
}
return $self->sendJSONresponse($req,$res);
return $self->sendJSONresponse( $req, $res );
}
# 33 - Root queries
......
......@@ -2,7 +2,7 @@ package Lemonldap::NG::Common::Logger::Apache2;
use Apache2::ServerRec;
sub new{
sub new {
return bless {}, shift;
}
......@@ -10,6 +10,6 @@ sub AUTOLOAD {
shift;
$AUTOLOAD =~ s/.*:://;
return Apache2::ServerRec->log->$AUTOLOAD(@_);
};
}
1;
......@@ -22,7 +22,7 @@ sub new {
$name = 'warning' if ( $_ eq 'warn' );
$name = 'err' if ( $_ eq 'error' );
eval qq'sub $_ {syslog("$name|".\$_[0]->{facility},\$_[1])}';
die $@ if($@);
die $@ if ($@);
}
else {
eval qq'sub $_ {1}';
......
......@@ -5,16 +5,14 @@ use Mouse;
our $VERSION = '2.0.0';
# Object that provides lmLog and error methods (typically PSGI object)
# Object that provides loggers and error methods (typically PSGI object)
has p => ( is => 'rw', weak_ref => 1 );
# Lemonldap::NG configuration hash ref
has conf => ( is => 'rw', weak_ref => 1 );
sub lmLog {
my $self = shift;
return $self->p->lmLog(@_);
}
has logger => ( is => 'ro', default => sub { $_[0]->{p}->logger } );
has userLogger => ( is => 'ro', default => sub { $_[0]->{p}->userLogger } );
sub error {
my $self = shift;
......
......@@ -14,9 +14,10 @@ use Encode;
our $VERSION = '2.0.0';
extends 'Lemonldap::NG::Common::Notifications';
sub import {
shift;
return Lemonldap::NG::Common::Notifications->import(@_)
return Lemonldap::NG::Common::Notifications->import(@_);
}
has dbiTable => (
......@@ -32,7 +33,7 @@ has dbiChain => (
has dbiUser => (
is => 'ro',
default => sub {
$_[0]->{p}->lmLog( 'Warning: "dbiUser" parameter is not set', 'warn' );
$_[0]->{p}->logger->warn('Warning: "dbiUser" parameter is not set');
return '';
}
);
......@@ -49,7 +50,7 @@ has _dbh => (
$self->{dbiChain}, $self->{dbiUser},
$self->{dbiPassword}, { RaiseError => 0 }
);
$self->lmLog( $DBI::errstr, 'error' ) unless ($r);
$self->logger->error($DBI::errstr) unless ($r);
return $r;
}
);
......@@ -83,7 +84,7 @@ sub get {
&getIdentifier( $self, $h->{uid}, $h->{ref}, $h->{date} );
$result->{$identifier} = $xml;
}
$self->lmLog( $self->sth->err(), 'warn' ) if ( $self->sth->err() );
$self->logger->warn( $self->sth->err() ) if ( $self->sth->err() );
return $result;
}
......@@ -104,7 +105,7 @@ sub getAll {
condition => $h->{condition}
};
}
$self->lmLog( $self->sth->err(), 'warn' ) if ( $self->sth->err() );
$self->logger->warn( $self->sth->err() ) if ( $self->sth->err() );
return $result;
}
......@@ -115,7 +116,7 @@ sub delete {
my ( $self, $myref ) = @_;
my ( $d, $u, $r );
unless ( ( $d, $u, $r ) = ( $myref =~ /^([^#]+)#(.+?)#(.+)$/ ) ) {
$self->lmLog( "Bad reference $myref", 'warn' );
$self->logger->warn("Bad reference $myref");
return 0;
}
my @ts = localtime();
......@@ -136,7 +137,7 @@ sub purge {
my ( $self, $myref, $force ) = @_;
my ( $d, $u, $r );
unless ( ( $d, $u, $r ) = ( $myref =~ /^([^#]+)#(.+?)#(.+)$/ ) ) {
$self->lmLog( "Bad reference $myref", 'warn' );
$self->logger->warn("Bad reference $myref");
return 0;
}
......@@ -186,7 +187,7 @@ sub getDone {
$result->{"$h->{date}#$h->{uid}#$h->{ref}"} =
{ notified => $done, uid => $h->{uid}, ref => $h->{ref}, };
}
$self->lmLog( $self->sth->err(), 'warn' ) if ( $self->sth->err() );
$self->logger->warn( $self->sth->err() ) if ( $self->sth->err() );
return $result;
}
......@@ -197,12 +198,12 @@ sub _execute {
my ( $self, $query, @args ) = @_;
my $dbh = $self->_dbh or return 0;
unless ( $self->sth( $dbh->prepare($query) ) ) {
$self->lmLog( $dbh->errstr(), 'warn' );
$self->logger->warn( $dbh->errstr() );
return 0;
}
my $tmp;
unless ( $tmp = $self->sth->execute(@args) ) {
$self->lmLog( $self->sth->errstr(), 'warn' );
$self->logger->warn( $self->sth->errstr() );
return 0;
}
return $tmp;
......
......@@ -48,8 +48,8 @@ sub get {
my $files;
foreach my $file (@notif) {
unless ( open F, $self->{dirName} . "/$file" ) {
$self->lmLog( "Unable to read notification $self->{dirName}/$file",
'error' );
$self->logger->error(
"Unable to read notification $self->{dirName}/$file");
next;
}
$files->{$file} = join( '', <F> );
......
......@@ -9,7 +9,7 @@ sub newNotification {
my $json;
eval { $json = from_json($jsonString) };
if ( my $err = $@ ) {
eval { $self->lmLog( "Unable to decode JSON file: $err", 'error' ) };
eval { $self->logger->error("Unable to decode JSON file: $err") };
return 0;
}
my @notifs;
......@@ -21,7 +21,7 @@ sub newNotification {
foreach (qw(date uid reference)) {
my $tmp;
unless ( $tmp = $notif->{$_} ) {
$self->lmLog( "Attribute $_ is missing", 'error' );
$self->logger->error("Attribute $_ is missing");
return 0;
}
push @datas, $tmp;
......@@ -44,16 +44,14 @@ sub deleteNotification {
# Check input parameters
unless ( $uid and $myref ) {
$self->lmLog(
'REST service "delete notification" called without all parameters',
'error'
$self->userLogger->error(
'REST service "delete notification" called without all parameters'
);
return 0;
}
$self->lmLog(
"REST service deleteNotification called for uid $uid and reference $myref",
'debug'
$self->logger->debug(
"REST service deleteNotification called for uid $uid and reference $myref"
);
# Get notifications
......@@ -79,7 +77,7 @@ sub deleteNotification {
# Delete the notification (really)
foreach (@data) {
if ( $self->purge( $_, 1 ) ) {
$self->lmLog( "Notification $_ was removed.", 'debug' );
$self->logger->debug("Notification $_ was removed.");
$count++;
}
}
......
......@@ -16,9 +16,10 @@ use utf8;
our $VERSION = '2.0.0';
extends 'Lemonldap::NG::Common::Notifications';
sub import {
shift;
return Lemonldap::NG::Common::Notifications->import(@_)
return Lemonldap::NG::Common::Notifications->import(@_);
}
has ldapServer => (
......@@ -38,7 +39,7 @@ has ldapConfBase => (
has ldapBindDN => (
is => 'ro',
default => sub {
$_[0]->p->lmLog( 'Warning: "ldapBindDN" parameter is not set', 'warn' );
$_[0]->p->logger->warn('Warning: "ldapBindDN" parameter is not set');
return '';
}
);
......@@ -69,7 +70,7 @@ sub get {
my $identifier =
&getIdentifier( $self, $f->{uid}, $f->{ref}, $f->{date} );
$result->{$identifier} = "$xml";
$self->lmLog( "notification $identifier found", 'info' );
$self->logger->info("notification $identifier found");
}
return $result;
......@@ -113,7 +114,7 @@ sub delete {
my ( $self, $myref ) = @_;
my ( $d, $u, $r );
unless ( ( $d, $u, $r ) = ( $myref =~ /^([^#]+)#(.+?)#(.+)$/ ) ) {
$self->lmLog( "Bad reference $myref", 'warn' );
$self->logger->warn("Bad reference $myref");
return 0;
}
my @ts = localtime();
......@@ -140,7 +141,7 @@ sub purge {
my ( $self, $myref, $force ) = @_;
my ( $d, $u, $r );
unless ( ( $d, $u, $r ) = ( $myref =~ /^([^#]+)#(.+?)#(.+)$/ ) ) {
$self->lmLog( "Bad reference $myref", 'warn' );
$self->logger->warn("Bad reference $myref");
return 0;
}
......@@ -235,7 +236,7 @@ sub _search {
);
if ( $search->code ) {
$self->lmLog( "search error: " . $search->error(), 'error' );
$self->logger->error( "search error: " . $search->error() );
return ();
}
......@@ -385,7 +386,7 @@ sub _ldap {
);
unless ($ldap) {
$self->lmLog( 'connexion failed: ' . $@, 'error' );
$self->logger->error( 'connexion failed: ' . $@ );
return;
}
......@@ -396,7 +397,7 @@ sub _ldap {
$h{capath} = $self->{caPath} if ( $self->{caPath} );
my $start_tls = $ldap->start_tls(%h);
if ( $start_tls->code ) {
$self->lmLog( 'tls failed: ' . $start_tls->error, 'error' );
$self->logger->error( 'tls failed: ' . $start_tls->error );
return;
}
}
......@@ -405,7 +406,7 @@ sub _ldap {
my $bind =
$ldap->bind( $self->{ldapBindDN}, password => $self->{ldapBindPassword} );
if ( $bind->code ) {
$self->lmLog( 'bind failed: ' . $bind->error, 'error' );
$self->logger->error( 'bind failed: ' . $bind->error );
return;
}
......
......@@ -19,7 +19,7 @@ sub newNotification {
my ( $self, $xml ) = @_;
eval { $xml = $self->parser->parse_string($xml) };
if ( my $err = $@ ) {
eval { $self->lmLog( "Unable to read XML file : $err", 'error' ) };
eval { $self->logger->error("Unable to read XML file : $err") };
return 0;
}
my @notifs;
......@@ -33,7 +33,7 @@ sub newNotification {
foreach (qw(date uid reference)) {
my $tmp;
unless ( $tmp = $notif->getAttribute($_) ) {
$self->lmLog( "Attribute $_ is missing", 'error' );
$self->logger->error("Attribute $_ is missing");
return 0;
}
push @datas, $tmp;
......@@ -75,15 +75,13 @@ sub deleteNotification {
# Check input parameters
unless ( $uid and $myref ) {
$self->lmLog(
"SOAP service deleteNotification called without all parameters",
'error' );
$self->userLogger->error(
"SOAP service deleteNotification called without all parameters");
return 0;
}
$self->lmLog(
"SOAP service deleteNotification called for uid $uid and reference $myref",
'debug'
$self->logger->debug(
"SOAP service deleteNotification called for uid $uid and reference $myref"
);
# Get notifications
......@@ -111,7 +109,7 @@ sub deleteNotification {
# Delete the notification (really)
foreach (@data) {
if ( $self->purge( $_, 1 ) ) {
$self->lmLog( "Notification $_ was removed.", 'debug' );
$self->logger->debug("Notification $_ was removed.");
$count++;
}
}
......
......@@ -186,7 +186,7 @@ body{background:#000;color:#fff;padding:10px 50px;font-family:sans-serif;}a{text
sub abort {
my ( $self, $err ) = @_;
$self->lmLog( $err, 'error' );
$self->logger->error($err);
return sub {
$self->sendError( Lemonldap::NG::Common::PSGI::Request->new( $_[0] ),
$err, 500 );
......@@ -233,7 +233,7 @@ sub sendHtml {
return $self->sendError( $req, "Unable to read $template", 500 )
unless ( -r $template and -f $template );
eval {
$self->lmLog( "Starting HTML generation using $template", 'debug' );
$self->logger->debug("Starting HTML generation using $template");
require HTML::Template;
$htpl = HTML::Template->new(
filehandle => IO::File->new($template),
......@@ -263,7 +263,7 @@ sub sendHtml {
# Set headers
my $hdrs = [ 'Content-Type' => 'text/html', @{ $args{headers} } ];
$self->lmLog( "Sending $template", 'debug' );
$self->logger->debug("Sending $template");
return [ $args{code}, $hdrs, [ $htpl->output() ] ];
}
......
......@@ -37,8 +37,8 @@ sub new {
sub uri { $_[0]->{uri} }
sub userData {
my($self,$v)=@_;
return $_[0]->{userData} = $v if($v);
my ( $self, $v ) = @_;
return $_[0]->{userData} = $v if ($v);
return $_[0]->{userData} || { _whatToTrace => $_[0]->user, };
}
......
......@@ -22,7 +22,7 @@ sub addRoute {
my ( $self, $word, $dest, $methods, $transform ) = (@_);
$methods ||= [qw(GET POST PUT DELETE)];
foreach my $method (@$methods) {
$self->lmLog( "Add $method route:", 'debug' );
$self->logger->debug("Add $method route:");
$self->genRoute( $self->routes->{$method}, $word, $dest, $transform );
}
return $self;
......@@ -51,7 +51,7 @@ sub genRoute {
}
if ( $dest =~ /^(.+)\.html$/ ) {
my $tpl = $1 or die;
$self->lmLog( "route $dest will use $tpl", 'debug' );
$self->logger->debug("route $dest will use $tpl");
$routes->{$word} = sub { $self->sendHtml( $_[1], $tpl ) };
return;
}
......@@ -85,7 +85,7 @@ sub genRoute {
else {
die "$dest() isn't a method";
}
$self->lmLog( "route $word added", 'debug' );
$self->logger->debug("route $word added");
}
}
......@@ -121,7 +121,7 @@ sub handler {
$last = 1 if ( $_ =~ /[^\.\w]/ );
( $last or /^$/ ? 0 : 1 );
} split /\//, $req->path();
$self->lmLog( "Start routing " . ( $path[0] // 'default route' ), 'debug' );
$self->logger->debug( "Start routing " . ( $path[0] // 'default route' ) );
if ( !@path and $self->defaultRoute ) {
@path = ( $self->defaultRoute );
......
......@@ -125,8 +125,7 @@ sub _tie_session {
eval {
# SOAP session module must be directly tied
if ( $self->storageModule =~
/^Lemonldap::NG::Common::Apache::Session/ )
if ( $self->storageModule =~ /^Lemonldap::NG::Common::Apache::Session/ )
{
tie %h, $self->storageModule, $self->id,
{ %{ $self->options }, %$options };
......
......@@ -59,7 +59,7 @@ ok(
'if(0) then [A,B] else [A,B] and [B,C]' );
while ( my $expr = shift @tests ) {
ok( [getok($expr)]->[0] == 0, qq{"$expr" returns PE_OK as auth result} )
ok( [ getok($expr) ]->[0] == 0, qq{"$expr" returns PE_OK as auth result} )
or print STDERR "Expect 0, get " . getok($expr) . "\n";
}
......
......@@ -36,7 +36,6 @@ eval { require threads::shared; };
our $request; # Apache2::RequestRec object for current request
#*run = \&Lemonldap::NG::Handler::Main::run;
## @rmethod protected int redirectFilter(string url, Apache2::Filter f)
......
......@@ -74,7 +74,7 @@ sub createSession {
my $creds = $class->header_in('Authorization');
$creds =~ s/^Basic\s+//;
my ( $user, $pwd ) = ( decode_base64($creds) =~ /^(.*?):(.*)$/ );
$class->lmLog( "AuthBasic authentication for user: $user", 'debug' );
$class->logger->debug("AuthBasic authentication for user: $user");
my $soapRequest = $soapClient->getCookies( $user, $pwd, $id );
# Catch SOAP errors
......@@ -87,11 +87,8 @@ sub createSession {
# If authentication failed, display error
if ( $res->{errorCode} ) {
$class->lmLog(
"Authentication failed for $user: "
. $soapClient->error( $res->{errorCode}, 'en' )->result(),
'notice'
);
$class->userLogger->notice( "Authentication failed for $user: "
. $soapClient->error( $res->{errorCode}, 'en' )->result() );
return 0;
}
else {
......@@ -104,7 +101,7 @@ sub createSession {
# Hide user credentials to the protected application
sub hideCookie {
my $class = shift;
$class->lmLog( "removing Authorization header", 'debug' );
$class->logger->debug("removing Authorization header");
$class->unset_header_in('Authorization');
}
......
......@@ -45,7 +45,7 @@ sub _run {
# Override _run() only if protection != 'none'
if ( !$self->rule or $self->rule ne 'none' ) {
$self->lmLog( 'PSGI app is protected', 'debug' );
$self->logger->debug('PSGI app is protected');
# Handle requests
# Developers, be careful: Only this part is executed at each request
......@@ -56,13 +56,13 @@ sub _run {
}
else {
$self->lmLog( 'PSGI app is not protected', 'debug' );
$self->logger->debug('PSGI app is not protected');
# Check if main handler initialization has been done
unless ( $self->api->tsv ) {
$self->lmLog( 'Checking conf', 'debug' );
$self->logger->debug('Checking conf');
eval { $self->api->checkConf() };
$self->lmLog( $@, 'error' ) if ($@);
$self->logger->error($@) if ($@);
}
# Handle unprotected requests
......@@ -83,7 +83,7 @@ sub status {
# Check if main handler initialization has been done
unless ( %$self->api->tsv ) {
eval { $self->api->checkConf() };
$self->lmLog( $@, 'error' ) if ($@);
$self->logger->error($@) if ($@);
}
return sub {
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
......@@ -100,7 +100,7 @@ sub reload {
# Check if main handler initialization has been done
unless ( %$self->api->tsv ) {
eval { $self->api->checkConf() };
$self->lmLog( $@, 'error' ) if ($@);
$self->logger->error($@) if ($@);
}
return sub {
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
......@@ -134,7 +134,7 @@ sub _authAndTrace {
return [ $res, $req->{respHeaders}, [] ];
}
else {
$self->lmLog( 'User authenticated, calling handler()', 'debug' );
$self->logger->debug('User authenticated, calling handler()');
$res = $self->handler($req);