Commit c1212147 authored by Christophe Maudoux's avatar Christophe Maudoux 🐛

Backport all v2.1 improvements

parent 33a7a1b1
......@@ -193,6 +193,7 @@ t/05-rest-api.t
t/06-rest-api.t
t/07-utf8.t
t/10-save-unchanged-conf.t
t/11-save-changed-conf-with-confirmation.t
t/12-save-changed-conf.t
t/14-bad-changes-in-conf.t
t/15-combination.t
......@@ -206,6 +207,7 @@ t/99-pod.t
t/conf/lmConf-1.json
t/jsonfiles/01-base-tree.json
t/jsonfiles/02-base-tree-all-nodes-opened.json
t/jsonfiles/11-modified-with-confirmation.json
t/jsonfiles/12-modified.json
t/jsonfiles/14-bad.json
t/jsonfiles/15-combination.json
......
......@@ -36,37 +36,40 @@ sub addRoutes {
# HTML template
$self->addRoute( 'manager.html', undef, ['GET'] )
# READ
# Special keys
->addRoute(
# READ
# Special keys
->addRoute(
confs => {
':cfgNum' => [
qw(virtualHosts samlIDPMetaDataNodes samlSPMetaDataNodes
applicationList oidcOPMetaDataNodes oidcRPMetaDataNodes
casSrvMetaDataNodes casAppMetaDataNodes
authChoiceModules grantSessionRules combModules
openIdIDPList)
applicationList oidcOPMetaDataNodes oidcRPMetaDataNodes
casSrvMetaDataNodes casAppMetaDataNodes
authChoiceModules grantSessionRules combModules
openIdIDPList)
]
},
['GET']
)
)
# Other keys
->addRoute( confs => { ':cfgNum' => { '*' => 'getKey' } }, ['GET'] )
# Other keys
->addRoute( confs => { ':cfgNum' => { '*' => 'getKey' } }, ['GET'] )
# New key and conf save
->addRoute(
confs =>
{ newRSAKey => 'newRSAKey', raw => 'newRawConf', '*' => 'newConf' },
# New key and conf save
->addRoute(
confs => {
newRSAKey => 'newRSAKey',
raw => 'newRawConf',
'*' => 'newConf'
},
['POST']
)
)
# Difference between confs
->addRoute( diff => { ':conf1' => { ':conf2' => 'diff' } } )
->addRoute( 'diff.html', undef, ['GET'] )
# Difference between confs
->addRoute( diff => { ':conf1' => { ':conf2' => 'diff' } } )
->addRoute( 'diff.html', undef, ['GET'] )
# Url loader
->addRoute( 'prx', undef, ['POST'] );
# Url loader
->addRoute( 'prx', undef, ['POST'] );
}
# 35 - New RSA key pair on demand
......@@ -82,7 +85,7 @@ sub addRoutes {
sub newRSAKey {
my ( $self, $req, @others ) = @_;
return $self->sendError( $req, 'There is no subkey for "newRSAKey"', 400 )
if (@others);
if (@others);
my $query = $req->jsonBodyToObj;
my $rsa = Crypt::OpenSSL::RSA->generate_key(2048);
my $keys = {
......@@ -124,12 +127,12 @@ sub newRSAKey {
sub prx {
my ( $self, $req, @others ) = @_;
return $self->sendError( $req, 'There is no subkey for "prx"', 400 )
if (@others);
if (@others);
my $query = $req->jsonBodyToObj;
return $self->sendError( $req, 'Missing parameter', 400 )
unless ( $query->{url} );
unless ( $query->{url} );
return $self->sendError( $req, 'Bad parameter', 400 )
unless ( $query->{url} =~ m#^(?:f|ht)tps?://\w# );
unless ( $query->{url} =~ m#^(?:f|ht)tps?://\w# );
$self->ua->timeout(10);
my $response = $self->ua->get( $query->{url} );
......@@ -137,11 +140,12 @@ sub prx {
return $self->sendError( $req,
$response->code . " (" . $response->message . ")", 400 );
}
unless ( $response->header('Content-Type') =~
m#^(?:application/json|(?:application|text)/.*xml).*$# )
unless ( $response->header('Content-Type')
=~ m#^(?:application/json|(?:application|text)/.*xml).*$# )
{
return $self->sendError( $req,
'Content refused for security reason (neither XML or JSON)', 400 );
'Content refused for security reason (neither XML or JSON)',
400 );
}
return $self->sendJSONresponse( $req, { content => $response->content } );
}
......@@ -185,7 +189,7 @@ sub getConfByNum {
sub newConf {
my ( $self, $req, @other ) = @_;
return $self->sendError( $req, 'There is no subkey for "newConf"', 400 )
if (@other);
if (@other);
# Body must be json
my $new = $req->jsonBodyToObj;
......@@ -203,9 +207,9 @@ sub newConf {
return $self->sendError(
$req,
"Configuration "
. $req->params('cfgNum')
. " not available "
. $Lemonldap::NG::Common::Conf::msg,
. $req->params('cfgNum')
. " not available "
. $Lemonldap::NG::Common::Conf::msg,
400
);
}
......@@ -228,10 +232,17 @@ sub newConf {
# "message" fields: note that words enclosed by "__" (__word__) will be
# translated
$res->{message} = $parser->{message};
foreach my $t (qw(errors warnings changes)) {
$res->{details}->{ '__' . $t . '__' } = $parser->$t
if ( @{ $parser->$t } );
$res->{details}->{'__errors__'} = $parser->{errors}
if ( @{ $parser->{errors} } );
unless ( @{ $parser->{errors} } ) {
$res->{details}->{'__needConfirmation__'}
= $parser->{needConfirmation}
if ( @{ $parser->{needConfirmation} } && !$req->params('force') );
$res->{message} = $parser->{message};
foreach my $t (qw(warnings changes)) {
$res->{details}->{ '__' . $t . '__' } = $parser->$t
if ( @{ $parser->$t } );
}
}
if ( $res->{result} ) {
if ( $self->{demoMode} ) {
......@@ -240,7 +251,9 @@ sub newConf {
else {
my %args;
$args{force} = 1 if ( $req->params('force') );
my $s = $self->confAcc->saveConf( $parser->newConf, %args );
my $s = CONFIG_WAS_CHANGED;
$s = $self->confAcc->saveConf( $parser->newConf, %args )
unless ( @{ $parser->{needConfirmation} } && !$args{force} );
if ( $s > 0 ) {
$self->userLogger->notice(
'User ' . $self->userId($req) . " has stored conf $s" );
......@@ -248,18 +261,19 @@ sub newConf {
$res->{cfgNum} = $s;
if ( my $status = $self->applyConf( $parser->newConf ) ) {
push @{ $res->{details}->{__applyResult__} },
{ message => "$_: $status->{$_}" }
foreach ( keys %$status );
{ message => "$_: $status->{$_}" }
foreach ( keys %$status );
}
}
else {
$self->userLogger->notice(
'Saving attempt rejected, asking for confirmation to '
. $self->userId($req) );
. $self->userId($req) );
$res->{result} = 0;
if ( $s == CONFIG_WAS_CHANGED ) {
$res->{needConfirm} = 1;
$res->{message} .= '__needConfirmation__';
$res->{message} .= '__needConfirmation__'
unless @{ $parser->{needConfirmation} };
}
else {
$res->{message} = $Lemonldap::NG::Common::Conf::msg;
......@@ -278,7 +292,7 @@ sub newConf {
sub newRawConf {
my ( $self, $req, @other ) = @_;
return $self->sendError( $req, 'There is no subkey for "newConf"', 400 )
if (@other);
if (@other);
# Body must be json
my $new = $req->jsonBodyToObj;
......@@ -303,7 +317,7 @@ sub newRawConf {
else {
$self->userLogger->notice(
'Raw saving attempt rejected, asking for confirmation to '
. $self->userId($req) );
. $self->userId($req) );
$res->{result} = 0;
$res->{needConfirm} = 1 if ( $s == CONFIG_WAS_CHANGED );
$res->{message} .= '__needConfirmation__';
......@@ -325,8 +339,8 @@ sub applyConf {
$self->api->checkConf();
# Get apply section values
my %reloadUrls =
%{ $self->confAcc->getLocalConf( APPLYSECTION, undef, 0 ) };
my %reloadUrls
= %{ $self->confAcc->getLocalConf( APPLYSECTION, undef, 0 ) };
if ( !%reloadUrls && $newConf->{reloadUrls} ) {
%reloadUrls = %{ $newConf->{reloadUrls} };
}
......@@ -342,10 +356,10 @@ sub applyConf {
my $targetUrl = $url->scheme . "://" . $host;
$targetUrl .= ":" . $url->port if defined( $url->port );
$targetUrl .= $url->full_path;
$r =
HTTP::Request->new( 'GET', $targetUrl,
$r = HTTP::Request->new( 'GET', $targetUrl,
HTTP::Headers->new( Host => $url->host ) );
if ( defined $url->userinfo && $url->userinfo =~ /^([^:]+):(.*)$/ )
if ( defined $url->userinfo
&& $url->userinfo =~ /^([^:]+):(.*)$/ )
{
$r->authorization_basic( $1, $2 );
}
......@@ -353,12 +367,14 @@ sub applyConf {
my $response = $self->ua->request($r);
if ( $response->code != 200 ) {
$status->{$host} =
"Error " . $response->code . " (" . $response->message . ")";
$status->{$host}
= "Error "
. $response->code . " ("
. $response->message . ")";
$self->logger->error( "Apply configuration for $host: error "
. $response->code . " ("
. $response->message
. ")" );
. $response->code . " ("
. $response->message
. ")" );
}
else {
$status->{$host} = "OK";
......@@ -372,14 +388,14 @@ sub applyConf {
sub diff {
my ( $self, $req, @path ) = @_;
return $self->sendError( $req, 'to many arguments in path info', 400 )
if (@path);
my @cfgNum =
( scalar( $req->param('conf1') ), scalar( $req->param('conf2') ) );
if (@path);
my @cfgNum
= ( scalar( $req->param('conf1') ), scalar( $req->param('conf2') ) );
my @conf;
$self->logger->debug(" Loading confs");
# Load the 2 configurations
for ( my $i = 0 ; $i < 2 ; $i++ ) {
for ( my $i = 0; $i < 2; $i++ ) {
if ( %{ $self->currentConf }
and $cfgNum[$i] == $self->currentConf->{cfgNum} )
{
......@@ -390,7 +406,7 @@ sub diff {
{ cfgNum => $cfgNum[$i], raw => 1, noCache => 1 } );
return $self->sendError(
$req,
"Configuration $cfgNum[$i] not available $Lemonldap::NG::Common::Conf::msg",
"Configuration $cfgNum[$i] not available $Lemonldap::NG::Common::Conf::msg",
400
) unless ( $conf[$i] );
}
......@@ -398,8 +414,7 @@ sub diff {
require Lemonldap::NG::Manager::Conf::Diff;
return $self->sendJSONresponse(
$req,
[
$self->Lemonldap::NG::Manager::Conf::Diff::diff(
[ $self->Lemonldap::NG::Manager::Conf::Diff::diff(
$conf[0], $conf[1]
)
]
......
......@@ -260,9 +260,9 @@ sub tests {
},
# Warn if session timeout is lower than 10 minutes
timeoutActivity => sub {
sessionTimeout => sub {
return 1 unless ( defined $conf->{timeout} );
return ( 1, "Session timeout should be higher than ten minutes" )
return ( -1, "Session timeout should be higher than ten minutes" )
unless ( $conf->{timeout} > 600
|| $conf->{timeout} == 0 );
......@@ -271,7 +271,7 @@ sub tests {
},
# Error if session Activity Timeout is equal or lower than one minute
timeoutActivity => sub {
sessionTimeoutActivity => sub {
return 1 unless ( defined $conf->{timeoutActivity} );
return ( 0,
"Session activity timeout must be higher or equal than one minute"
......@@ -609,13 +609,16 @@ sub tests {
# Warn if Mailrest plugin is enabled without Token or Captcha
checkMailResetSecurity => sub {
return 1 unless ( $conf->{portalDisplayResetPassword} );
return ( 1,
'"passwordMailReset" plugin is enabled without CSRF Token or Captcha required !!!'
) unless ( $conf->{requireToken} or $conf->{captcha_mail_enabled} );
return ( -1,
'"passwordMailReset" plugin is enabled without CSRF Token neither Captcha required !!!'
)
unless ( $conf->{requireToken}
or $conf->{captcha_mail_enabled} );
# Return
return 1;
},
};
}
......
......@@ -144,13 +144,18 @@ llapp.controller 'TreeCtrl', [
title: ''
message: ''
items: []
$scope.confirmNeeded = true if data.message == '__needConfirmation__'
$scope.confirmNeeded = true if data.needConfirm
$scope.message.message = data.message if data.message
if data.details
for m of data.details when m != '__changes__'
$scope.message.items.push
message: m
items: data.details[m]
if m == '__needConfirmation__'
$scope.message.items.unshift
message: m
items: data.details[m]
else
$scope.message.items.push
message: m
items: data.details[m]
$scope.waiting = false
if data.result == 1
# Force reloading page
......
......@@ -170,7 +170,7 @@ This file contains:
message: '',
items: []
};
if (data.message === '__needConfirmation__') {
if (data.needConfirm) {
$scope.confirmNeeded = true;
}
if (data.message) {
......@@ -179,10 +179,17 @@ This file contains:
if (data.details) {
for (m in data.details) {
if (m !== '__changes__') {
$scope.message.items.push({
message: m,
items: data.details[m]
});
if (m === '__needConfirmation__') {
$scope.message.items.unshift({
message: m,
items: data.details[m]
});
} else {
$scope.message.items.push({
message: m,
items: data.details[m]
});
}
}
}
}
......
......@@ -236,6 +236,7 @@
"enabled":"مفعلة",
"enterPassword":"أدخل كلمة المرور (اختياري)",
"error":"خطأ",
"errors":"ERRORS",
"exportedAttr":"SOAP/REST السمات المصدرة",
"exportedHeaders":"الهيدر المصدرة",
"exportedVars":"المتغيرات المصدرة",
......
......@@ -236,6 +236,7 @@
"enabled":"Aktiviert",
"enterPassword":"Enter password (optional)",
"error":"Error",
"errors":"ERRORS",
"exportedAttr":"SOAP/REST exported attributes",
"exportedHeaders":"Exported headers",
"exportedVars":"Exported Variables",
......
......@@ -236,6 +236,7 @@
"enabled":"Enabled",
"enterPassword":"Enter password (optional)",
"error":"Error",
"errors":"ERRORS",
"exportedAttr":"SOAP/REST exported attributes",
"exportedHeaders":"Exported headers",
"exportedVars":"Exported Variables",
......
......@@ -236,6 +236,7 @@
"enabled":"Activé",
"enterPassword":"Entrer le mot de passe (optionnel)",
"error":"Erreur",
"errors":"ERREURS",
"exportedAttr":"Attributs exportés par le portail (SOAP/REST)",
"exportedHeaders":"En-têtes exportés",
"exportedVars":"Attributs à exporter",
......
......@@ -236,6 +236,7 @@
"enabled":"Abilitato",
"enterPassword":"Inserisci password (opzionale)",
"error":"Errore",
"errors":"ERRORS",
"exportedAttr":"Attributi di SOAP/REST esportati",
"exportedHeaders":"Intestazioni esportate",
"exportedVars":"Variabili esportate",
......
......@@ -236,6 +236,7 @@
"enabled":"Đã bật",
"enterPassword":"Nhập mật khẩu (tùy chọn)",
"error":"Lỗi",
"errors":"ERRORS",
"exportedAttr":"Biến SOAP/REST đã được xuất",
"exportedHeaders":"Tiêu đề đã được xuất",
"exportedVars":"Biến đã được xuất",
......
......@@ -236,6 +236,7 @@
"enabled":"开启",
"enterPassword":"输入密码(可选)",
"error":"错误",
"errors":"ERRORS",
"exportedAttr":"SOAP/REST exported attributes",
"exportedHeaders":"Exported headers",
"exportedVars":"Exported Variables",
......
# Verify that a modified configuration can be saved and that all changes are
# detected
use Test::More;
use strict;
use JSON;
require 't/test-lib.pm';
my $struct = 't/jsonfiles/11-modified-with-confirmation.json';
my $confFiles = [ 't/conf/lmConf-1.json', 't/conf/lmConf-2.json' ];
sub body {
return IO::File->new( $struct, 'r' );
}
# Delete lmConf-2.json if exists
eval { unlink $confFiles->[1]; };
mkdir 't/sessions';
my ( $res, $resBody );
ok( $res = &client->_post( '/confs/', 'cfgNum=1', &body, 'application/json' ),
"Request succeed"
);
ok( $res->[0] == 200, "Result code is 200" );
ok( $resBody = from_json( $res->[2]->[0] ),
"Result body contains JSON text" );
ok( $resBody->{result} == 0, "JSON response contains \"result:0\"" )
or print STDERR Dumper($resBody);
ok( $resBody->{needConfirm} == 1, "JSON response contains \"needConfirm:1\"" )
or print STDERR Dumper($resBody);
ok( @{ $resBody->{details}->{__warnings__} } == 3,
'JSON response contains 3 warnings'
) or print STDERR Dumper($resBody);
foreach my $i (0 .. 2) {
ok( $resBody->{details}->{__warnings__}->[$i]->{message}
=~ /\b(unprotected|cross-domain-authentication|retries)\b/,
"Warning with 'unprotect', 'CDA' or 'retries' found"
) or print STDERR Dumper($resBody);
}
count(4);
ok( @{ $resBody->{details}->{__needConfirmation__} } == 1,
'JSON response contains 1 needConfirmation'
) or print STDERR Dumper($resBody);
ok( $resBody->{details}->{__needConfirmation__}->[0]->{message}
=~ /\bplugin is enabled without CSRF Token neither Captcha required\b/,
"Warning with confirmation needed found"
) or print STDERR Dumper($resBody);
ok( @{ $resBody->{details}->{__changes__} } == 22,
'JSON response contains 24 changes'
) or print STDERR Dumper($resBody);
#print STDERR Dumper($resBody);
ok( $res = &client->_post( '/confs/', 'cfgNum=1&force=1', &body, 'application/json' ),
"Request succeed"
);
ok( -f $confFiles->[1], 'File is created' );
count(5);
my @changes = @{&changes};
my @cmsg = @{ $resBody->{details}->{__changes__} };
my $bug;
while ( my $c = shift @{ $resBody->{details}->{__changes__} } ) {
my $cmp1 = @changes;
my $cmp2 = @cmsg;
my @d1 = grep { $_->{key} eq $c->{key} } @changes;
my @d2 = grep { $_->{key} eq $c->{key} } @cmsg;
@changes = grep { $_->{key} ne $c->{key} } @changes;
@cmsg = grep { $_->{key} ne $c->{key} } @cmsg;
if ( $c->{key} eq 'applicationList' ) {
pass qq("$c->{key}" found);
}
else {
ok( ( $cmp1 - @changes ) == ( $cmp2 - @cmsg ), qq("$c->{key}" found) )
or print STDERR 'Expect: '
. ( $cmp1 - @changes )
. ', got: '
. ( $cmp2 - @cmsg )
. "\nExpect: "
. Dumper( \@d1 ) . "Got: "
. Dumper( \@d2 );
}
count(1);
}
ok( !@changes, 'All changes detected' ) or $bug = 1;
if ($bug) {
print STDERR 'Expected not found: '
. Dumper( \@changes )
. 'Changes announced and not found: '
. Dumper( \@cmsg );
}
#print STDERR Dumper(\@changes,\@cmsg);
count(6);
# TODO: check result of this
ok( $res = &client->jsonResponse('/diff/1/2'), 'Diff called' );
my ( @c1, @c2 );
ok( ( @c1 = sort keys %{ $res->[0] } ), 'diff() detects changes in conf 1' );
ok( ( @c2 = sort keys %{ $res->[1] } ), 'diff() detects changes in conf 2' );
ok( @c1 == 12, '11 keys changed in conf 1' )
or print STDERR "Expect: 12 keys, get: " . join( ', ', @c1 ) . "\n";
ok( @c2 == 16, '14 keys changed or created in conf 2' )
or print STDERR "Expect: 16 keys, get: " . join( ',', @c2 ) . "\n";
count(5);
unlink $confFiles->[1];
#eval { rmdir 't/sessions'; };
done_testing( count() );
# Remove sessions directory
`rm -rf t/sessions`;
sub changes {
return [
{ 'key' => 'portal',