| 1 |
package Lemonldap::NG::Manager::Conf::Parser; |
| 2 |
|
| 3 |
# This module is called either to parse a new configuration in JSON format (as |
| 4 |
# posted by the web interface) and test a new configuration object. |
| 5 |
# |
| 6 |
# The new object must be built with the following properties: |
| 7 |
# - refConf: the actual configuration |
| 8 |
# - req : the Lemonldap::NG::Common::PSGI::Request |
| 9 |
# - tree : the new configuration in JSON format |
| 10 |
# or |
| 11 |
# - newConf: the configuration to test |
| 12 |
# |
| 13 |
# The main method is check() which calls: |
| 14 |
# - scanTree() if configuration is not parsed (JSON string) |
| 15 |
# - testNewConf() |
| 16 |
# |
| 17 |
# It returns a boolean. Errors, warnings and changes are stored as array |
| 18 |
# containing `{ message => 'Explanation' }. A main message is stored in |
| 19 |
# `message` property. |
| 20 |
|
| 21 |
use strict; |
| 22 |
use utf8; |
| 23 |
use Crypt::URandom; |
| 24 |
use Mouse; |
| 25 |
use JSON 'to_json'; |
| 26 |
use Lemonldap::NG::Common::Conf::ReConstants; |
| 27 |
use Lemonldap::NG::Manager::Attributes; |
| 28 |
|
| 29 |
our $VERSION = '2.23.0'; |
| 30 |
|
| 31 |
extends 'Lemonldap::NG::Common::Conf::Compact'; |
| 32 |
|
| 33 |
# High debugging for developers, set this to 1 |
| 34 |
use constant HIGHDEBUG => 0; |
| 35 |
|
| 36 |
# Messages storage |
| 37 |
has errors => ( |
| 38 |
is => 'rw', |
| 39 |
isa => 'ArrayRef', |
| 40 |
default => sub { return [] } |
| 41 |
); |
| 42 |
has warnings => ( |
| 43 |
is => 'rw', |
| 44 |
isa => 'ArrayRef', |
| 45 |
default => sub { return [] }, |
| 46 |
trigger => sub { |
| 47 |
hdebug( 'warnings contains', $_[0]->{warnings} ); |
| 48 |
} |
| 49 |
); |
| 50 |
has changes => ( is => 'rw', isa => 'ArrayRef', default => sub { return [] } ); |
| 51 |
has message => ( |
| 52 |
is => 'rw', |
| 53 |
isa => 'Str', |
| 54 |
lazy => 1, |
| 55 |
default => sub { |
| 56 |
return join( ', ', map { $_->{message} } @{ $_[0]->errors } ); |
| 57 |
}, |
| 58 |
trigger => sub { |
| 59 |
hdebug( "Message becomes " . $_[0]->{message} ); |
| 60 |
} |
| 61 |
); |
| 62 |
has needConfirmation => |
| 63 |
( is => 'rw', isa => 'ArrayRef', default => sub { return [] } ); |
| 64 |
|
| 65 |
# Booleans |
| 66 |
has confChanged => ( |
| 67 |
is => 'rw', |
| 68 |
isa => 'Bool', |
| 69 |
default => 0, |
| 70 |
trigger => sub { |
| 71 |
hdebug( "condChanged: " . $_[0]->{confChanged} ); |
| 72 |
} |
| 73 |
); |
| 74 |
|
| 75 |
# Properties required during build |
| 76 |
has refConf => ( is => 'ro', isa => 'HashRef', required => 1 ); |
| 77 |
has req => ( is => 'ro', required => 1 ); |
| 78 |
has newConf => ( is => 'rw', isa => 'HashRef' ); |
| 79 |
has tree => ( is => 'rw', isa => 'ArrayRef' ); |
| 80 |
|
| 81 |
# High debug method |
| 82 |
sub hdebug { |
| 83 |
if (HIGHDEBUG) { |
| 84 |
foreach my $d (@_) { |
| 85 |
if ( ref $d ) { |
| 86 |
require Data::Dumper; |
| 87 |
$Data::Dumper::Useperl = 1; |
| 88 |
print STDERR Data::Dumper::Dumper($d); |
| 89 |
} |
| 90 |
else { print STDERR "$d\n" } |
| 91 |
} |
| 92 |
} |
| 93 |
undef; |
| 94 |
} |
| 95 |
|
| 96 |
##@method boolean check() |
| 97 |
# Main method |
| 98 |
#@return result |
| 99 |
sub check { |
| 100 |
my ( $self, $localConf ) = @_; |
| 101 |
|
| 102 |
hdebug("# check()"); |
| 103 |
unless ( $self->newConf ) { |
| 104 |
return 0 unless ( $self->scanTree ); |
| 105 |
} |
| 106 |
unless ( $self->testNewConf($localConf) ) { |
| 107 |
hdebug(" testNewConf() failed"); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
my $separator = $self->newConf->{multiValuesSeparator} || '; '; |
| 111 |
hdebug(" tests succeed"); |
| 112 |
my %conf = %{ $self->newConf }; |
| 113 |
my %compactedConf = %{ $self->compactConf( $self->newConf ) }; |
| 114 |
my @removedKeys = (); |
| 115 |
unless ( $self->confChanged ) { |
| 116 |
hdebug(" no change detected"); |
| 117 |
$self->message('__confNotChanged__'); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
# Return removed keys if conf compacted |
| 122 |
@removedKeys = map { exists $compactedConf{$_} ? () : $_ } sort keys %conf |
| 123 |
if ( $self->newConf->{compactConf} ); |
| 124 |
push @{ $self->changes }, |
| 125 |
( |
| 126 |
$self->{newConf}->{compactConf} |
| 127 |
? { |
| 128 |
confCompacted => '1', |
| 129 |
removedKeys => join( $separator, @removedKeys ) |
| 130 |
} |
| 131 |
: { confCompacted => '0' } |
| 132 |
); |
| 133 |
|
| 134 |
return 1; |
| 135 |
} |
| 136 |
|
| 137 |
##@method boolean scanTree() |
| 138 |
# Methods to build new conf from JSON string |
| 139 |
#@result true if succeed |
| 140 |
sub scanTree { |
| 141 |
my $self = shift; |
| 142 |
hdebug("# scanTree()"); |
| 143 |
$self->newConf( {} ); |
| 144 |
$self->_scanNodes( $self->tree ) or return 0; |
| 145 |
|
| 146 |
# Set cfgNum to ref cfgNum (will be changed when saving), set other |
| 147 |
# metadata and set a value to the key if empty |
| 148 |
$self->newConf->{cfgNum} = $self->req->params('cfgNum'); |
| 149 |
my $envUser; |
| 150 |
foreach (qw(REMOTE_USER SUDO_USER USER)) { |
| 151 |
( $envUser = $self->req->env->{$_} || $ENV{$_} ) and last; |
| 152 |
} |
| 153 |
$envUser = '' unless $envUser =~ /\w/; |
| 154 |
$self->newConf->{cfgAuthor} = |
| 155 |
$self->req->userData->{ Lemonldap::NG::Handler::Main->tsv->{whatToTrace} |
| 156 |
|| '_whatToTrace' } |
| 157 |
|| $envUser |
| 158 |
|| "anonymous"; |
| 159 |
$self->newConf->{cfgAuthorIP} = $self->req->address; |
| 160 |
$self->newConf->{cfgAuthorIP} .= |
| 161 |
' (maybe ' |
| 162 |
. ( $self->req->env->{'X-Real-IP'} |
| 163 |
|| $self->req->env->{HTTP_X_FORWARDED_FOR} ) |
| 164 |
. ')' |
| 165 |
if $self->req->env->{'X-Real-IP'} |
| 166 |
or $self->req->env->{HTTP_X_FORWARDED_FOR}; |
| 167 |
$self->newConf->{cfgDate} = time; |
| 168 |
$self->newConf->{cfgVersion} = $Lemonldap::NG::Manager::VERSION; |
| 169 |
$self->newConf->{key} ||= join( '', |
| 170 |
map { chr( int( ord( Crypt::URandom::urandom(1) ) * 94 / 256 ) + 33 ) } |
| 171 |
( 1 .. 16 ) ); |
| 172 |
|
| 173 |
return 1; |
| 174 |
} |
| 175 |
|
| 176 |
use feature 'state'; |
| 177 |
|
| 178 |
##@method private boolean _scanNodes() |
| 179 |
# Recursive JSON parser |
| 180 |
#@result true if succeed |
| 181 |
sub _scanNodes { |
| 182 |
my ( $self, $tree, ) = @_; |
| 183 |
hdebug("# _scanNodes()"); |
| 184 |
state ( $knownCat, %newNames ); |
| 185 |
unless ( ref($tree) eq 'ARRAY' ) { |
| 186 |
print STDERR 'Fatal: node is not an array'; |
| 187 |
push @{ $self->errors }, { message => 'Fatal: node is not an array' }; |
| 188 |
return 0; |
| 189 |
} |
| 190 |
unless (@$tree) { |
| 191 |
hdebug(' empty tree !?'); |
| 192 |
} |
| 193 |
foreach my $leaf (@$tree) { |
| 194 |
my $name = $leaf->{title}; |
| 195 |
hdebug("Looking to $name"); |
| 196 |
|
| 197 |
# subnode |
| 198 |
my $subNodes = $leaf->{nodes} // $leaf->{_nodes}; |
| 199 |
my $subNodesCond = $leaf->{nodes_cond} // $leaf->{_nodes_cond}; |
| 200 |
|
| 201 |
################################## |
| 202 |
# VirtualHosts and SAML partners # |
| 203 |
################################## |
| 204 |
|
| 205 |
# Root nodes |
| 206 |
if ( $leaf->{id} =~ /^($specialNodeKeys)$/i ) { |
| 207 |
hdebug("Root special node detected $leaf->{id}"); |
| 208 |
|
| 209 |
# If node has not been opened |
| 210 |
if ( $leaf->{cnodes} ) { |
| 211 |
hdebug(" not opened"); |
| 212 |
foreach my $k ( @{ $specialNodeHash->{ $leaf->{id} } } ) { |
| 213 |
hdebug(" copying $k"); |
| 214 |
$self->newConf->{$k} = $self->refConf->{$k}; |
| 215 |
} |
| 216 |
next; |
| 217 |
} |
| 218 |
$self->_scanNodes($subNodes); |
| 219 |
|
| 220 |
# Check deleted keys |
| 221 |
my $field = $specialNodeHash->{ $leaf->{id} }->[0]; |
| 222 |
my @old = keys %{ $self->refConf->{$field} }; |
| 223 |
foreach my $k ( keys %{ $self->newConf->{$field} } ) { |
| 224 |
@old = grep { $_ ne $k } @old; |
| 225 |
} |
| 226 |
if (@old) { |
| 227 |
hdebug( "Keys detected as removed:", \@old ); |
| 228 |
$self->confChanged(1); |
| 229 |
foreach my $deletedHost (@old) { |
| 230 |
push @{ $self->changes }, |
| 231 |
{ key => $leaf->{id}, old => $deletedHost }; |
| 232 |
} |
| 233 |
} |
| 234 |
next; |
| 235 |
} |
| 236 |
|
| 237 |
# 1st sublevel |
| 238 |
elsif ( $leaf->{id} =~ /^($specialNodeKeys)\/([^\/]+)$/i ) { |
| 239 |
hdebug("Special node chield detected $leaf->{id}"); |
| 240 |
my ( $base, $host ) = ( $1, $2 ); |
| 241 |
|
| 242 |
# Check hostname/partner name changes (id points to the old name) |
| 243 |
$newNames{$host} = $leaf->{title}; |
| 244 |
if ( $newNames{$host} ne $host and $host !~ /^new__/ ) { |
| 245 |
hdebug(" $host becomes $newNames{$host}"); |
| 246 |
$self->confChanged(1); |
| 247 |
push @{ $self->changes }, |
| 248 |
{ key => $base, old => $host, new => $newNames{$host} }; |
| 249 |
} |
| 250 |
|
| 251 |
$self->_scanNodes($subNodes); |
| 252 |
next; |
| 253 |
} |
| 254 |
|
| 255 |
# Other sub levels |
| 256 |
elsif ( $leaf->{id} =~ |
| 257 |
/^($specialNodeKeys)\/([^\/]+)\/([^\/]+)(?:\/(.*))?$/i ) |
| 258 |
{ |
| 259 |
my ( $base, $key, $oldName, $target, $h ) = |
| 260 |
( $1, $newNames{$2}, $2, $3, $4 ); |
| 261 |
hdebug( |
| 262 |
"Special node chield subnode detected $leaf->{id}", |
| 263 |
" base $base, key $key, target $target, h " |
| 264 |
. ( $h ? $h : 'undef' ) |
| 265 |
); |
| 266 |
|
| 267 |
# VirtualHosts |
| 268 |
if ( $base eq 'virtualHosts' ) { |
| 269 |
hdebug(" virtualhost"); |
| 270 |
if ( $target =~ /^(?:locationRules|exportedHeaders|post)$/ ) { |
| 271 |
if ( $leaf->{cnodes} ) { |
| 272 |
hdebug(' unopened subnode'); |
| 273 |
$self->newConf->{$target}->{$key} = |
| 274 |
$self->refConf->{$target}->{$oldName} // {}; |
| 275 |
} |
| 276 |
|
| 277 |
elsif ($h) { |
| 278 |
hdebug(' 4 levels'); |
| 279 |
if ( $target eq 'locationRules' ) { |
| 280 |
hdebug(' locationRules'); |
| 281 |
my $k = |
| 282 |
$leaf->{comment} |
| 283 |
? "(?#$leaf->{comment})$leaf->{re}" |
| 284 |
: $leaf->{re}; |
| 285 |
$k .= "(?#AuthnLevel=$leaf->{level})" |
| 286 |
if $leaf->{level}; |
| 287 |
$self->set( $target, $key, $k, $leaf->{data} ); |
| 288 |
} |
| 289 |
else { |
| 290 |
hdebug(' other than locationrules'); |
| 291 |
$self->set( $target, $key, $leaf->{title}, |
| 292 |
$leaf->{data} ); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
# Unless $h is set, scan subnodes and check changes |
| 297 |
else { |
| 298 |
hdebug(' 3 levels only (missing $h)'); |
| 299 |
if ( ref $subNodes ) { |
| 300 |
hdebug(' has subnodes'); |
| 301 |
$self->_scanNodes($subNodes) |
| 302 |
or return 0; |
| 303 |
} |
| 304 |
if ( exists $self->refConf->{$target}->{$key} |
| 305 |
and %{ $self->refConf->{$target}->{$key} } ) |
| 306 |
{ |
| 307 |
hdebug(' old conf subnode has values'); |
| 308 |
my $c = $self->newConf->{$target}; |
| 309 |
foreach my $k ( |
| 310 |
keys %{ $self->refConf->{$target}->{$key} } ) |
| 311 |
{ |
| 312 |
unless ( defined $c->{$key}->{$k} ) { |
| 313 |
hdebug(' missing value in old conf'); |
| 314 |
$self->confChanged(1); |
| 315 |
push @{ $self->changes }, |
| 316 |
{ |
| 317 |
key => "$target, $key", |
| 318 |
old => $k, |
| 319 |
}; |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
elsif ( exists $self->newConf->{$target}->{$key} |
| 324 |
and %{ $self->newConf->{$target}->{$key} } ) |
| 325 |
{ |
| 326 |
hdebug(" '$key' has values"); |
| 327 |
$self->confChanged(1); |
| 328 |
push @{ $self->changes }, |
| 329 |
{ key => "$target", new => $key }; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
elsif ( $target =~ /^$virtualHostKeys$/ ) { |
| 334 |
$self->set( 'vhostOptions', [ $oldName, $key ], |
| 335 |
$target, $leaf->{data} ); |
| 336 |
} |
| 337 |
else { |
| 338 |
push @{ $self->errors }, |
| 339 |
{ message => "Unknown vhost key $target" }; |
| 340 |
return 0; |
| 341 |
} |
| 342 |
next; |
| 343 |
} |
| 344 |
|
| 345 |
# SAML |
| 346 |
elsif ( $base =~ /^saml(?:S|ID)PMetaDataNodes$/ ) { |
| 347 |
hdebug('SAML'); |
| 348 |
if ( defined $leaf->{data} |
| 349 |
and ref( $leaf->{data} ) eq 'ARRAY' ) |
| 350 |
{ |
| 351 |
hdebug(" SAML data is an array, serializing"); |
| 352 |
$leaf->{data} = join ';', @{ $leaf->{data} }; |
| 353 |
} |
| 354 |
if ( $target =~ |
| 355 |
/^saml(?:S|ID)PMetaData(?:ExportedAttributes|Macros)$/ ) |
| 356 |
{ |
| 357 |
if ( $leaf->{cnodes} ) { |
| 358 |
hdebug(" $target: unopened node"); |
| 359 |
$self->newConf->{$target}->{$key} = |
| 360 |
$self->refConf->{$target}->{$oldName} // {}; |
| 361 |
} |
| 362 |
elsif ($h) { |
| 363 |
hdebug(" $target: opened node"); |
| 364 |
$self->confChanged(1); |
| 365 |
$self->set( $target, $key, $leaf->{title}, |
| 366 |
$leaf->{data} ); |
| 367 |
} |
| 368 |
elsif ( !@$subNodes ) { |
| 369 |
hdebug(" $target: no subnodes"); |
| 370 |
$self->confChanged(1); |
| 371 |
} |
| 372 |
else { |
| 373 |
hdebug(" $target: scanning subnodes"); |
| 374 |
$self->_scanNodes($subNodes); |
| 375 |
} |
| 376 |
} |
| 377 |
elsif ( $target =~ /^saml(?:S|ID)PMetaDataXML$/ ) { |
| 378 |
hdebug(" $target"); |
| 379 |
$self->set( $target, [ $oldName, $key ], |
| 380 |
$target, $leaf->{data} ); |
| 381 |
} |
| 382 |
elsif ( $target =~ /^saml(?:S|ID)PMetaDataURL$/ ) { |
| 383 |
hdebug(" $target"); |
| 384 |
$self->set( $target, [ $oldName, $key ], |
| 385 |
$target, $leaf->{data} ); |
| 386 |
} |
| 387 |
elsif ( $target =~ /^saml(?:ID|S)PMetaDataOptions/ ) { |
| 388 |
my $optKey = $&; |
| 389 |
hdebug(" $base sub key: $target"); |
| 390 |
if ( $target =~ |
| 391 |
/^(?:$samlIDPMetaDataNodeKeys|$samlSPMetaDataNodeKeys)/ |
| 392 |
) |
| 393 |
{ |
| 394 |
$self->set( |
| 395 |
$optKey, [ $oldName, $key ], |
| 396 |
$target, $leaf->{data} |
| 397 |
); |
| 398 |
} |
| 399 |
else { |
| 400 |
push @{ $self->errors }, |
| 401 |
{ message => "Unknown SAML metadata option $target" }; |
| 402 |
return 0; |
| 403 |
} |
| 404 |
} |
| 405 |
else { |
| 406 |
push @{ $self->errors }, |
| 407 |
{ message => "Unknown SAML key $target" }; |
| 408 |
return 0; |
| 409 |
} |
| 410 |
next; |
| 411 |
} |
| 412 |
|
| 413 |
# OIDC |
| 414 |
elsif ( $base =~ /^oidc(?:O|R)PMetaDataNodes$/ ) { |
| 415 |
hdebug('OIDC'); |
| 416 |
if ( $target =~ /^oidc(?:O|R)PMetaDataOptions$/ ) { |
| 417 |
hdebug(" $target: looking for subnodes"); |
| 418 |
$self->_scanNodes($subNodes); |
| 419 |
$self->set( $target, $key, $leaf->{title}, $leaf->{data} ); |
| 420 |
} |
| 421 |
elsif ( $target =~ /^oidcOPMetaData(?:JSON|JWKS)$/ ) { |
| 422 |
hdebug(" $target"); |
| 423 |
$self->set( $target, $key, $leaf->{data} ); |
| 424 |
} |
| 425 |
elsif ( $target =~ /^oidcRPMetaDataExportedVars$/ ) { |
| 426 |
hdebug(" $target"); |
| 427 |
if ( $leaf->{cnodes} ) { |
| 428 |
hdebug(' unopened'); |
| 429 |
$self->newConf->{$target}->{$key} = |
| 430 |
$self->refConf->{$target}->{$oldName} // {}; |
| 431 |
} |
| 432 |
elsif ($h) { |
| 433 |
hdebug(' opened'); |
| 434 |
$self->confChanged(1); |
| 435 |
my $tmp = $leaf->{data}; |
| 436 |
if ( ref( $leaf->{data} ) eq 'ARRAY' ) { |
| 437 |
|
| 438 |
# Forward compatibility. If Type and Array have |
| 439 |
# default values, store in old format |
| 440 |
if ( $leaf->{data}->[1] eq "string" |
| 441 |
and $leaf->{data}->[2] eq "auto" ) |
| 442 |
{ |
| 443 |
$tmp = $leaf->{data}->[0]; |
| 444 |
} |
| 445 |
else { |
| 446 |
$tmp = join ';', @{ $leaf->{data} }; |
| 447 |
} |
| 448 |
} |
| 449 |
$self->set( $target, $key, $leaf->{title}, $tmp ); |
| 450 |
} |
| 451 |
elsif ( !@$subNodes ) { |
| 452 |
hdebug(" $target: no subnodes"); |
| 453 |
$self->confChanged(1); |
| 454 |
} |
| 455 |
else { |
| 456 |
hdebug(" $target: scanning subnodes"); |
| 457 |
$self->_scanNodes($subNodes); |
| 458 |
} |
| 459 |
} |
| 460 |
elsif ( $target =~ |
| 461 |
/^oidc(?:O|R)PMetaData(?:ExportedVars|Macros|ScopeRules)$/ ) |
| 462 |
{ |
| 463 |
hdebug(" $target"); |
| 464 |
if ( $leaf->{cnodes} ) { |
| 465 |
hdebug(' unopened'); |
| 466 |
$self->newConf->{$target}->{$key} = |
| 467 |
$self->refConf->{$target}->{$oldName} // {}; |
| 468 |
} |
| 469 |
elsif ($h) { |
| 470 |
hdebug(' opened'); |
| 471 |
$self->confChanged(1); |
| 472 |
$self->set( $target, $key, $leaf->{title}, |
| 473 |
$leaf->{data} ); |
| 474 |
} |
| 475 |
elsif ( !@$subNodes ) { |
| 476 |
hdebug(" $target: no subnodes"); |
| 477 |
$self->confChanged(1); |
| 478 |
} |
| 479 |
else { |
| 480 |
hdebug(" $target: scanning subnodes"); |
| 481 |
$self->_scanNodes($subNodes); |
| 482 |
} |
| 483 |
} |
| 484 |
elsif ( $target =~ /^oidc(?:O|R)PMetaDataOptions/ ) { |
| 485 |
my $optKey = $&; |
| 486 |
hdebug " $base sub key: $target"; |
| 487 |
if ( $target eq 'oidcRPMetaDataOptionsExtraClaims' ) { |
| 488 |
if ( $leaf->{cnodes} ) { |
| 489 |
hdebug(' unopened'); |
| 490 |
$self->newConf->{$target}->{$key} = |
| 491 |
$self->refConf->{$target}->{$oldName} // {}; |
| 492 |
} |
| 493 |
elsif ($h) { |
| 494 |
hdebug(' opened'); |
| 495 |
$self->set( $target, $key, $leaf->{title}, |
| 496 |
$leaf->{data} ); |
| 497 |
} |
| 498 |
elsif ( !@$subNodes ) { |
| 499 |
hdebug(" $target: no subnodes"); |
| 500 |
$self->confChanged(1); |
| 501 |
} |
| 502 |
else { |
| 503 |
hdebug(" $target: scanning subnodes"); |
| 504 |
$self->_scanNodes($subNodes); |
| 505 |
} |
| 506 |
} |
| 507 |
elsif ( $target =~ |
| 508 |
/^(?:$oidcOPMetaDataNodeKeys|$oidcRPMetaDataNodeKeys)/ ) |
| 509 |
{ |
| 510 |
$self->set( |
| 511 |
$optKey, [ $oldName, $key ], |
| 512 |
$target, $leaf->{data} |
| 513 |
); |
| 514 |
} |
| 515 |
else { |
| 516 |
push @{ $self->errors }, |
| 517 |
{ message => "Unknown OIDC metadata option $target" }; |
| 518 |
return 0; |
| 519 |
} |
| 520 |
} |
| 521 |
else { |
| 522 |
push @{ $self->errors }, |
| 523 |
{ message => "Unknown OIDC key $target" }; |
| 524 |
return 0; |
| 525 |
} |
| 526 |
next; |
| 527 |
} |
| 528 |
|
| 529 |
# CAS |
| 530 |
elsif ( $base =~ /^cas(?:App|Srv)MetaDataNodes$/ ) { |
| 531 |
my $optKey = $&; |
| 532 |
hdebug('CAS'); |
| 533 |
if ( $target =~ /^cas(?:App|Srv)MetaDataOptions$/ ) { |
| 534 |
hdebug(" $target: looking for subnodes"); |
| 535 |
$self->_scanNodes($subNodes); |
| 536 |
$self->set( $target, $key, $leaf->{title}, $leaf->{data} ); |
| 537 |
} |
| 538 |
elsif ( $target =~ |
| 539 |
/^cas(?:App|Srv)MetaData(?:ExportedVars|Macros)$/ ) |
| 540 |
{ |
| 541 |
hdebug(" $target"); |
| 542 |
if ( $leaf->{cnodes} ) { |
| 543 |
hdebug(' unopened'); |
| 544 |
$self->newConf->{$target}->{$key} = |
| 545 |
$self->refConf->{$target}->{$oldName} // {}; |
| 546 |
} |
| 547 |
elsif ($h) { |
| 548 |
hdebug(' opened'); |
| 549 |
$self->confChanged(1); |
| 550 |
$self->set( $target, $key, $leaf->{title}, |
| 551 |
$leaf->{data} ); |
| 552 |
} |
| 553 |
elsif ( !@$subNodes ) { |
| 554 |
hdebug(" $target: no subnodes"); |
| 555 |
$self->confChanged(1); |
| 556 |
} |
| 557 |
else { |
| 558 |
hdebug(" $target: scanning subnodes"); |
| 559 |
$self->_scanNodes($subNodes); |
| 560 |
} |
| 561 |
} |
| 562 |
elsif ( $target =~ /^cas(?:Srv|App)MetaDataOptions/ ) { |
| 563 |
my $optKey = $&; |
| 564 |
hdebug " $base sub key: $target"; |
| 565 |
if ( $target eq 'casSrvMetaDataOptionsProxiedServices' ) { |
| 566 |
if ( $leaf->{cnodes} ) { |
| 567 |
hdebug(' unopened'); |
| 568 |
$self->newConf->{$target}->{$key} = |
| 569 |
$self->refConf->{$target}->{$oldName} // {}; |
| 570 |
} |
| 571 |
elsif ($h) { |
| 572 |
hdebug(' opened'); |
| 573 |
$self->set( $target, $key, $leaf->{title}, |
| 574 |
$leaf->{data} ); |
| 575 |
} |
| 576 |
elsif ( !@$subNodes ) { |
| 577 |
hdebug(" $target: no subnodes"); |
| 578 |
$self->confChanged(1); |
| 579 |
} |
| 580 |
else { |
| 581 |
hdebug(" $target: scanning subnodes"); |
| 582 |
$self->_scanNodes($subNodes); |
| 583 |
} |
| 584 |
} |
| 585 |
elsif ( $target =~ |
| 586 |
/^(?:$casSrvMetaDataNodeKeys|$casAppMetaDataNodeKeys)/ ) |
| 587 |
{ |
| 588 |
$self->set( |
| 589 |
$optKey, [ $oldName, $key ], |
| 590 |
$target, $leaf->{data} |
| 591 |
); |
| 592 |
} |
| 593 |
else { |
| 594 |
push @{ $self->errors }, |
| 595 |
{ message => "Unknown CAS metadata option $target" }; |
| 596 |
return 0; |
| 597 |
} |
| 598 |
} |
| 599 |
else { |
| 600 |
push @{ $self->errors }, |
| 601 |
{ message => "Unknown CAS option $target" }; |
| 602 |
return 0; |
| 603 |
} |
| 604 |
next; |
| 605 |
} |
| 606 |
elsif ( $base eq 'keyNodes' ) { |
| 607 |
hdebug('Keys'); |
| 608 |
hdebug("target $target"); |
| 609 |
hdebug("key $key"); |
| 610 |
if ( $target eq "KeyMaterial" ) { |
| 611 |
if ( $leaf->{data} ) { |
| 612 |
hdebug(' opened'); |
| 613 |
for my $data ( @{ $leaf->{data} } ) { |
| 614 |
$self->set( "keys", $key, $data->{title}, |
| 615 |
$data->{data} ); |
| 616 |
} |
| 617 |
} |
| 618 |
else { |
| 619 |
hdebug(' unopened'); |
| 620 |
for my $opt (qw/keyPrivate keyPrivatePwd keyPublic/) { |
| 621 |
$self->set( "keys", $key, $opt, undef ); |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
else { |
| 626 |
$self->set( "keys", $key, $leaf->{title}, $leaf->{data} ); |
| 627 |
} |
| 628 |
next; |
| 629 |
} |
| 630 |
else { |
| 631 |
push @{ $self->errors }, |
| 632 |
{ message => "Fatal: unknown special sub node $base" }; |
| 633 |
return 0; |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
#################### |
| 638 |
# Application list # |
| 639 |
#################### |
| 640 |
|
| 641 |
# Application list root node |
| 642 |
elsif ( $leaf->{title} eq 'applicationList' ) { |
| 643 |
hdebug( $leaf->{title} ); |
| 644 |
if ( $leaf->{cnodes} ) { |
| 645 |
hdebug(' unopened'); |
| 646 |
$self->newConf->{applicationList} = |
| 647 |
$self->refConf->{applicationList} // {}; |
| 648 |
} |
| 649 |
else { |
| 650 |
$self->_scanNodes($subNodes) or return 0; |
| 651 |
|
| 652 |
# Check for deleted |
| 653 |
my @listCatRef = |
| 654 |
map { $self->refConf->{applicationList}->{$_}->{catname} } |
| 655 |
keys %{ $self->refConf->{applicationList} }; |
| 656 |
my @listCatNew = |
| 657 |
map { $self->newConf->{applicationList}->{$_}->{catname} } |
| 658 |
keys( |
| 659 |
%{ |
| 660 |
ref $self->newConf->{applicationList} |
| 661 |
? $self->newConf->{applicationList} |
| 662 |
: {} |
| 663 |
} |
| 664 |
); |
| 665 |
|
| 666 |
@listCatRef = map { $_ ? $_ : () } @listCatRef; |
| 667 |
@listCatNew = map { $_ ? $_ : () } @listCatNew; |
| 668 |
@listCatRef = sort @listCatRef; |
| 669 |
@listCatNew = sort @listCatNew; |
| 670 |
hdebug( '# @listCatRef : ', \@listCatRef ); |
| 671 |
hdebug( '# @listCatNew : ', \@listCatNew ); |
| 672 |
|
| 673 |
# Check for deleted |
| 674 |
my @diff = |
| 675 |
grep !${ { map { $_, 1 } @listCatNew } }{$_}, @listCatRef; |
| 676 |
if ( scalar @diff ) { |
| 677 |
$self->confChanged(1); |
| 678 |
push @{ $self->changes }, |
| 679 |
{ |
| 680 |
new => join( ', ', 'categoryList', @listCatNew ), |
| 681 |
key => join( ', ', 'Deletes in cat(s)', @diff ), |
| 682 |
old => join( ', ', 'categoryList', @listCatRef ), |
| 683 |
}; |
| 684 |
} |
| 685 |
} |
| 686 |
next; |
| 687 |
} |
| 688 |
|
| 689 |
# Application list sub nodes |
| 690 |
elsif ( $leaf->{id} =~ /^applicationList\/(.+)$/ ) { |
| 691 |
hdebug('Application list subnode'); |
| 692 |
use feature 'state'; |
| 693 |
my @cats = split /\//, $1; |
| 694 |
my $app = pop @cats; |
| 695 |
$self->newConf->{applicationList} //= {}; |
| 696 |
|
| 697 |
# $cn is a pointer to the parent |
| 698 |
my $cn = $self->newConf->{applicationList}; |
| 699 |
my $cmp = $self->refConf->{applicationList}; |
| 700 |
my @path; |
| 701 |
|
| 702 |
# Makes $cn point to the parent |
| 703 |
foreach my $cat (@cats) { |
| 704 |
hdebug(" looking to cat $cat"); |
| 705 |
unless ( defined $knownCat->{$cat} ) { |
| 706 |
push @{ $self->{errors} }, |
| 707 |
{ message => |
| 708 |
"Fatal: sub cat/app before parent ($leaf->{id})" }; |
| 709 |
return 0; |
| 710 |
} |
| 711 |
$cn = $cn->{ $knownCat->{$cat} }; |
| 712 |
push @path, $cn->{catname}; |
| 713 |
$cmp->{$cat} //= {}; |
| 714 |
$cmp = $cmp->{$cat}; |
| 715 |
} |
| 716 |
|
| 717 |
my $newapp = $app; |
| 718 |
|
| 719 |
# Compute a nice name for new nodes, taking care of potential conflicts |
| 720 |
# For some reason, the manager sends /nNaN sometimes |
| 721 |
if ( $newapp =~ /^n(\d+|NaN)$/ ) { |
| 722 |
|
| 723 |
# Remove all special characters |
| 724 |
my $baseName = $leaf->{title} =~ s/\W//gr; |
| 725 |
$baseName = lc $baseName; |
| 726 |
$newapp = $baseName; |
| 727 |
my $cnt = 1; |
| 728 |
while ( exists $cn->{$newapp} ) { |
| 729 |
$newapp = "${baseName}_" . $cnt++; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
# Create new category |
| 734 |
# |
| 735 |
# Note that this works because nodes are ordered so "cat/cat2/app" |
| 736 |
# is looked after "cat" and "cat/cat2" |
| 737 |
if ( $leaf->{type} eq 'menuCat' ) { |
| 738 |
hdebug(' menu cat'); |
| 739 |
$knownCat->{__id}++; |
| 740 |
$knownCat->{$app} = $newapp; |
| 741 |
$cn->{$newapp} = { |
| 742 |
catname => $leaf->{title}, |
| 743 |
type => 'category', |
| 744 |
order => $knownCat->{__id} |
| 745 |
}; |
| 746 |
unless ($cmp->{$app} |
| 747 |
and $cmp->{$app}->{catname} eq $cn->{$newapp}->{catname} ) |
| 748 |
{ |
| 749 |
$self->confChanged(1); |
| 750 |
push @{ $self->changes }, |
| 751 |
{ |
| 752 |
key => join( |
| 753 |
', ', 'applicationList', @path, $leaf->{title} |
| 754 |
), |
| 755 |
new => $cn->{$newapp}->{catname}, |
| 756 |
old => ( |
| 757 |
$cn->{$newapp} ? $cn->{$newapp}->{catname} : undef |
| 758 |
) |
| 759 |
}; |
| 760 |
} |
| 761 |
if ( ref $subNodes ) { |
| 762 |
$self->_scanNodes($subNodes) or return 0; |
| 763 |
} |
| 764 |
my @listCatRef = keys %{ $cmp->{$app} }; |
| 765 |
my @listCatNew = keys %{ $cn->{$newapp} }; |
| 766 |
|
| 767 |
# Check for deleted |
| 768 |
unless ( @listCatRef == @listCatNew ) { |
| 769 |
$self->confChanged(1); |
| 770 |
push @{ $self->changes }, |
| 771 |
{ |
| 772 |
key => join( ', ', 'applicationList', @path ), |
| 773 |
new => 'Changes in cat(s)/app(s)', |
| 774 |
}; |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
# Create new apps |
| 779 |
if ( $leaf->{type} eq 'menuApp' ) { |
| 780 |
hdebug(' new app'); |
| 781 |
$knownCat->{__id}++; |
| 782 |
$cn->{$newapp} = { |
| 783 |
type => 'application', |
| 784 |
options => $leaf->{data}, |
| 785 |
order => $knownCat->{__id} |
| 786 |
}; |
| 787 |
$cn->{$newapp}->{options}->{name} = $leaf->{title}; |
| 788 |
unless ( $cmp->{$app} ) { |
| 789 |
$self->confChanged(1); |
| 790 |
push @{ $self->changes }, |
| 791 |
{ |
| 792 |
key => join( ', ', 'applicationList', @path ), |
| 793 |
new => $leaf->{title}, |
| 794 |
}; |
| 795 |
} |
| 796 |
else { |
| 797 |
# Check for change in ordering |
| 798 |
if ( ( $cn->{$newapp}->{order} || 0 ) != |
| 799 |
( $cmp->{$newapp}->{order} || 0 ) ) |
| 800 |
{ |
| 801 |
$self->confChanged(1); |
| 802 |
} |
| 803 |
|
| 804 |
# Check for change in options |
| 805 |
foreach my $k ( keys %{ $cn->{$newapp}->{options} } ) { |
| 806 |
unless ( $cmp->{$app}->{options}->{$k} eq |
| 807 |
$cn->{$newapp}->{options}->{$k} ) |
| 808 |
{ |
| 809 |
$self->confChanged(1); |
| 810 |
push @{ $self->changes }, |
| 811 |
{ |
| 812 |
key => join( ', ', |
| 813 |
'applicationList', @path, |
| 814 |
$leaf->{title}, $k ), |
| 815 |
new => $cn->{$newapp}->{options}->{$k}, |
| 816 |
old => $cmp->{$app}->{options}->{$k} |
| 817 |
}; |
| 818 |
} |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
next; |
| 823 |
} |
| 824 |
elsif ( $leaf->{id} eq 'grantSessionRules' ) { |
| 825 |
hdebug('grantSessionRules'); |
| 826 |
if ( $leaf->{cnodes} ) { |
| 827 |
hdebug(' unopened'); |
| 828 |
$self->newConf->{$name} = $self->refConf->{$name} // {}; |
| 829 |
} |
| 830 |
else { |
| 831 |
hdebug(' opened'); |
| 832 |
$subNodes //= []; |
| 833 |
my $count = 0; |
| 834 |
my $ref = $self->refConf->{grantSessionRules}; |
| 835 |
my $new = $self->newConf->{grantSessionRules}; |
| 836 |
my @old = ref $ref ? keys %$ref : (); |
| 837 |
$self->newConf->{grantSessionRules} = {}; |
| 838 |
foreach my $n (@$subNodes) { |
| 839 |
hdebug(" looking at $n subnode"); |
| 840 |
my $k = |
| 841 |
$n->{re} . ( $n->{comment} ? "##$n->{comment}" : '' ); |
| 842 |
$self->newConf->{grantSessionRules}->{$k} = $n->{data}; |
| 843 |
$count++; |
| 844 |
unless ( defined $ref->{$k} ) { |
| 845 |
$self->confChanged(1); |
| 846 |
push @{ $self->changes }, |
| 847 |
{ keys => 'grantSessionRules', new => $k }; |
| 848 |
} |
| 849 |
elsif ( $ref->{$k} ne $n->{data} ) { |
| 850 |
$self->confChanged(1); |
| 851 |
push @{ $self->changes }, |
| 852 |
{ |
| 853 |
key => "grantSessionRules, $k", |
| 854 |
old => $self->refConf->{grantSessionRules}->{$k}, |
| 855 |
new => $n->{data} |
| 856 |
}; |
| 857 |
} |
| 858 |
@old = grep { $_ ne $k } @old; |
| 859 |
} |
| 860 |
if (@old) { |
| 861 |
$self->confChanged(1); |
| 862 |
push @{ $self->changes }, |
| 863 |
{ key => 'grantSessionRules', old => $_, } |
| 864 |
foreach (@old); |
| 865 |
} |
| 866 |
} |
| 867 |
next; |
| 868 |
} |
| 869 |
|
| 870 |
# openIdIDPList: data is splitted by Conf.pm into a boolean and a |
| 871 |
# string |
| 872 |
elsif ( $name eq 'openIdIDPList' ) { |
| 873 |
hdebug('openIdIDPList'); |
| 874 |
if ( $leaf->{data} ) { |
| 875 |
unless ( ref $leaf->{data} eq 'ARRAY' ) { |
| 876 |
push @{ $self->{errors} }, |
| 877 |
{ message => 'Malformed openIdIDPList ' . $leaf->{data} }; |
| 878 |
return 0; |
| 879 |
} |
| 880 |
$self->set( $name, join( ';', @{ $leaf->{data} } ) ); |
| 881 |
} |
| 882 |
else { |
| 883 |
$self->set( $name, undef ); |
| 884 |
} |
| 885 |
next; |
| 886 |
} |
| 887 |
|
| 888 |
#################### |
| 889 |
# Other hash nodes # |
| 890 |
#################### |
| 891 |
elsif ( $leaf->{title} =~ /^$simpleHashKeys$/ |
| 892 |
and not $leaf->{title} eq 'applicationList' ) |
| 893 |
{ |
| 894 |
hdebug( $leaf->{title} ); |
| 895 |
|
| 896 |
# If a `cnodes` key is found, keep old key unchanges |
| 897 |
if ( $leaf->{cnodes} ) { |
| 898 |
hdebug(' unopened'); |
| 899 |
$self->newConf->{$name} = $self->refConf->{$name} // {}; |
| 900 |
} |
| 901 |
else { |
| 902 |
hdebug(' opened'); |
| 903 |
|
| 904 |
# combModules: just to replace "over" key |
| 905 |
if ( $name eq 'combModules' ) { |
| 906 |
hdebug(' combModules'); |
| 907 |
$self->newConf->{$name} = {}; |
| 908 |
foreach my $node ( @{ $leaf->{nodes} } ) { |
| 909 |
my $tmp; |
| 910 |
$tmp->{$_} = $node->{data}->{$_} foreach (qw(type for)); |
| 911 |
$tmp->{over} = {}; |
| 912 |
foreach ( @{ $node->{data}->{over} } ) { |
| 913 |
$tmp->{over}->{ $_->[0] } = $_->[1]; |
| 914 |
} |
| 915 |
$self->newConf->{$name}->{ $node->{title} } = $tmp; |
| 916 |
} |
| 917 |
|
| 918 |
# TODO: check changes |
| 919 |
$self->confChanged(1); |
| 920 |
next; |
| 921 |
} |
| 922 |
|
| 923 |
# sfExtra: just to replace "over" key |
| 924 |
if ( $name eq 'sfExtra' ) { |
| 925 |
hdebug(' sfExtra'); |
| 926 |
$self->newConf->{$name} = {}; |
| 927 |
foreach my $node ( @{ $leaf->{nodes} } ) { |
| 928 |
my $tmp; |
| 929 |
$tmp->{$_} = $node->{data}->{$_} |
| 930 |
foreach (qw(type rule logo level label regrule)); |
| 931 |
$tmp->{register} = $node->{data}->{register} ? 1 : 0; |
| 932 |
$tmp->{over} = {}; |
| 933 |
foreach ( @{ $node->{data}->{over} } ) { |
| 934 |
$tmp->{over}->{ $_->[0] } = $_->[1]; |
| 935 |
} |
| 936 |
$self->newConf->{$name}->{ $node->{title} } = $tmp; |
| 937 |
} |
| 938 |
|
| 939 |
# TODO: check changes |
| 940 |
$self->confChanged(1); |
| 941 |
next; |
| 942 |
} |
| 943 |
|
| 944 |
$subNodes //= []; |
| 945 |
my $count = 0; |
| 946 |
my @old = ( |
| 947 |
ref( $self->refConf->{$name} ) |
| 948 |
? ( keys %{ $self->refConf->{$name} } ) |
| 949 |
: () |
| 950 |
); |
| 951 |
$self->newConf->{$name} = {}; |
| 952 |
foreach my $n (@$subNodes) { |
| 953 |
hdebug(" looking at $n subnode"); |
| 954 |
if ( ref $n->{data} and ref $n->{data} eq 'ARRAY' ) { |
| 955 |
|
| 956 |
# authChoiceModules |
| 957 |
if ( $name eq 'authChoiceModules' ) { |
| 958 |
hdebug(' authChoiceModules'); |
| 959 |
if ( ref( $n->{data}->[5] ) eq 'ARRAY' ) { |
| 960 |
$n->{data}->[5] = |
| 961 |
to_json( { map { @$_ } @{ $n->{data}->[5] } }, |
| 962 |
{ canonical => 1 } ); |
| 963 |
} |
| 964 |
else { |
| 965 |
$n->{data}->[5] = '{}'; |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
$n->{data} = join ';', @{ $n->{data} }; |
| 970 |
} |
| 971 |
$self->newConf->{$name}->{ $n->{title} } = $n->{data}; |
| 972 |
$count++; |
| 973 |
unless ( defined $self->refConf->{$name}->{ $n->{title} } ) |
| 974 |
{ |
| 975 |
$self->confChanged(1); |
| 976 |
push @{ $self->changes }, |
| 977 |
{ key => $name, new => $n->{title}, }; |
| 978 |
} |
| 979 |
elsif ( |
| 980 |
$self->refConf->{$name}->{ $n->{title} } ne $n->{data} ) |
| 981 |
{ |
| 982 |
$self->confChanged(1); |
| 983 |
push @{ $self->changes }, |
| 984 |
{ |
| 985 |
key => "$name, $n->{title}", |
| 986 |
old => $self->refConf->{$name}->{ $n->{title} }, |
| 987 |
new => $n->{data} |
| 988 |
}; |
| 989 |
} |
| 990 |
@old = grep { $_ ne $n->{title} } @old; |
| 991 |
} |
| 992 |
if (@old) { |
| 993 |
$self->confChanged(1); |
| 994 |
push @{ $self->changes }, { key => $name, old => $_, } |
| 995 |
foreach (@old); |
| 996 |
} |
| 997 |
} |
| 998 |
next; |
| 999 |
} |
| 1000 |
|
| 1001 |
# Double hash nodes |
| 1002 |
elsif ( $leaf->{title} =~ /^$doubleHashKeys$/ ) { |
| 1003 |
hdebug( $leaf->{title} ); |
| 1004 |
my @oldHosts = ( |
| 1005 |
ref( $self->refConf->{$name} ) |
| 1006 |
? ( keys %{ $self->refConf->{$name} } ) |
| 1007 |
: () |
| 1008 |
); |
| 1009 |
$self->newConf->{$name} = {}; |
| 1010 |
unless ( defined $leaf->{data} ) { |
| 1011 |
hdebug(' unopened'); |
| 1012 |
$self->newConf->{$name} = $self->refConf->{$name} || {}; |
| 1013 |
next; |
| 1014 |
} |
| 1015 |
foreach my $getHost ( @{ $leaf->{data} } ) { |
| 1016 |
my $change = 0; |
| 1017 |
my @oldKeys; |
| 1018 |
my $host = $getHost->{k}; |
| 1019 |
hdebug(" looking at host: $host"); |
| 1020 |
$self->newConf->{$name}->{$host} = {}; |
| 1021 |
unless ( defined $self->refConf->{$name}->{$host} ) { |
| 1022 |
$self->confChanged(1); |
| 1023 |
$change++; |
| 1024 |
push @{ $self->changes }, { key => $name, new => $host }; |
| 1025 |
hdebug(" $host is new"); |
| 1026 |
} |
| 1027 |
else { |
| 1028 |
@oldHosts = grep { $_ ne $host } @oldHosts; |
| 1029 |
@oldKeys = keys %{ $self->refConf->{$name}->{$host} }; |
| 1030 |
} |
| 1031 |
foreach my $prm ( @{ $getHost->{h} } ) { |
| 1032 |
$self->newConf->{$name}->{$host}->{ $prm->{k} } = |
| 1033 |
$prm->{v}; |
| 1034 |
if ( |
| 1035 |
!$change |
| 1036 |
and ( |
| 1037 |
not defined( |
| 1038 |
$self->refConf->{$name}->{$host}->{ $prm->{k} } |
| 1039 |
) |
| 1040 |
or $self->newConf->{$name}->{$host}->{ $prm->{k} } |
| 1041 |
ne $self->refConf->{$name}->{$host}->{ $prm->{k} } |
| 1042 |
) |
| 1043 |
) |
| 1044 |
{ |
| 1045 |
$self->confChanged(1); |
| 1046 |
hdebug(" key $prm->{k} has been changed"); |
| 1047 |
push @{ $self->changes }, |
| 1048 |
{ key => "$name/$host", new => $prm->{k} }; |
| 1049 |
} |
| 1050 |
elsif ( !$change ) { |
| 1051 |
@oldKeys = grep { $_ ne $prm->{k} } @oldKeys; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
if (@oldKeys) { |
| 1055 |
$self->confChanged(1); |
| 1056 |
hdebug( " old keys: " . join( ' ', @oldKeys ) ); |
| 1057 |
push @{ $self->changes }, |
| 1058 |
{ key => "$name/$host", old => $_ } |
| 1059 |
foreach (@oldKeys); |
| 1060 |
} |
| 1061 |
} |
| 1062 |
if (@oldHosts) { |
| 1063 |
$self->confChanged(1); |
| 1064 |
hdebug( " old hosts " . join( ' ', @oldHosts ) ); |
| 1065 |
push @{ $self->changes }, { key => "$name", old => $_ } |
| 1066 |
foreach (@oldHosts); |
| 1067 |
} |
| 1068 |
next; |
| 1069 |
} |
| 1070 |
|
| 1071 |
############### |
| 1072 |
# Other nodes # |
| 1073 |
############### |
| 1074 |
|
| 1075 |
# Check if subnodes |
| 1076 |
my $n = 0; |
| 1077 |
if ( ref $subNodesCond ) { |
| 1078 |
hdebug(' conditional subnodes detected'); |
| 1079 |
|
| 1080 |
# Bad idea,subnode unopened are not read |
| 1081 |
#$subNodesCond = [ grep { $_->{show} } @$subNodesCond ]; |
| 1082 |
$self->_scanNodes($subNodesCond) or return 0; |
| 1083 |
$n++; |
| 1084 |
} |
| 1085 |
if ( ref $subNodes ) { |
| 1086 |
hdebug(' subnodes detected'); |
| 1087 |
$self->_scanNodes($subNodes) or return 0; |
| 1088 |
$n++; |
| 1089 |
} |
| 1090 |
if ($n) { |
| 1091 |
next; |
| 1092 |
} |
| 1093 |
if ( defined $leaf->{data} and ref( $leaf->{data} ) eq 'ARRAY' ) { |
| 1094 |
if ( ref( $leaf->{data}->[0] ) eq 'HASH' ) { |
| 1095 |
hdebug(" array found"); |
| 1096 |
$self->_scanNodes( $leaf->{data} ) or return 0; |
| 1097 |
} |
| 1098 |
else { |
| 1099 |
$self->set( $name, join( ';', @{ $leaf->{data} } ) ); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
# Grouped nodes not opened |
| 1104 |
elsif ( $leaf->{get} and ref $leaf->{get} eq 'ARRAY' ) { |
| 1105 |
hdebug(" unopened grouped node"); |
| 1106 |
foreach my $subkey ( @{ $leaf->{get} } ) { |
| 1107 |
$self->set( $subkey, undef ); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
# Normal leaf |
| 1112 |
else { |
| 1113 |
$self->set( $name, $leaf->{data} ); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
return 1; |
| 1117 |
} |
| 1118 |
|
| 1119 |
##@method private void set($target, @path, $data) |
| 1120 |
# Store a value in the $target key (following subkeys if @path is set) |
| 1121 |
sub set { |
| 1122 |
my $self = shift; |
| 1123 |
my $data = pop; |
| 1124 |
my @confs = ( $self->refConf, $self->newConf ); |
| 1125 |
my @path; |
| 1126 |
while ( @_ > 1 ) { |
| 1127 |
my $tmp = shift; |
| 1128 |
push @path, $tmp; |
| 1129 |
foreach my $i ( 0, 1 ) { |
| 1130 |
my $v = ref($tmp) ? $tmp->[$i] : $tmp; |
| 1131 |
$confs[$i]->{$v} //= {}; |
| 1132 |
$confs[$i] = $confs[$i]->{$v}; |
| 1133 |
} |
| 1134 |
} |
| 1135 |
my $target = shift; |
| 1136 |
hdebug( "# set() called:", |
| 1137 |
{ data => $data, path => \@path, target => $target } ); |
| 1138 |
die @path unless ($target); |
| 1139 |
|
| 1140 |
# Check new value |
| 1141 |
if ( defined $data ) { |
| 1142 |
hdebug(" data defined"); |
| 1143 |
|
| 1144 |
# TODO: remove if $data == default value |
| 1145 |
$confs[1]->{$target} = $data; |
| 1146 |
eval { |
| 1147 |
unless ( |
| 1148 |
$target eq 'cfgLog' |
| 1149 |
or ( defined $confs[0]->{$target} |
| 1150 |
and $confs[0]->{$target} eq $data ) |
| 1151 |
or ( !defined $confs[0]->{$target} |
| 1152 |
and defined $self->defaultValue($target) |
| 1153 |
and $data eq $self->defaultValue($target) ) |
| 1154 |
) |
| 1155 |
{ |
| 1156 |
$self->confChanged(1); |
| 1157 |
push @{ $self->changes }, |
| 1158 |
{ |
| 1159 |
key => join( ', ', @path, $target ), |
| 1160 |
old => $confs[0]->{$target} // $self->defaultValue($target), |
| 1161 |
new => $confs[1]->{$target} |
| 1162 |
}; |
| 1163 |
} |
| 1164 |
}; |
| 1165 |
} |
| 1166 |
|
| 1167 |
# Set old value if exists |
| 1168 |
else { |
| 1169 |
hdebug(" data undefined"); |
| 1170 |
if ( exists $confs[0]->{$target} ) { |
| 1171 |
hdebug(" old value exists"); |
| 1172 |
$confs[1]->{$target} = $confs[0]->{$target}; |
| 1173 |
} |
| 1174 |
else { |
| 1175 |
hdebug(" no old value, skipping"); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
sub defaultValue { |
| 1181 |
my ( $self, $target ) = @_; |
| 1182 |
hdebug("# defaultValue($target)"); |
| 1183 |
die unless ($target); |
| 1184 |
my $res = eval { |
| 1185 |
&Lemonldap::NG::Manager::Attributes::attributes()->{$target} |
| 1186 |
->{'default'}; |
| 1187 |
}; |
| 1188 |
return $res; |
| 1189 |
} |
| 1190 |
|
| 1191 |
##@method boolean testNewConf() |
| 1192 |
# Launch _unitTest() and _globaTest() |
| 1193 |
# |
| 1194 |
#@return true if tests succeed |
| 1195 |
sub testNewConf { |
| 1196 |
my ( $self, $localConf ) = @_; |
| 1197 |
|
| 1198 |
hdebug('# testNewConf()'); |
| 1199 |
return $self->_unitTest( $self->newConf(), $localConf ) |
| 1200 |
&& $self->_globalTest($localConf); |
| 1201 |
} |
| 1202 |
|
| 1203 |
##@method private boolean _unitTest() |
| 1204 |
# Launch unit tests declared in Lemonldap::NG::Manager::Build::Attributes file |
| 1205 |
# |
| 1206 |
#@return true if tests succeed |
| 1207 |
sub _unitTest { |
| 1208 |
my ( $self, $conf, $localConf ) = @_; |
| 1209 |
hdebug('# _unitTest()'); |
| 1210 |
my $types = &Lemonldap::NG::Manager::Attributes::types(); |
| 1211 |
my $attrs = &Lemonldap::NG::Manager::Attributes::attributes(); |
| 1212 |
my $res = 1; |
| 1213 |
|
| 1214 |
foreach my $key ( keys %$conf ) { |
| 1215 |
if ( $localConf |
| 1216 |
and $localConf->{skippedUnitTests} |
| 1217 |
and $localConf->{skippedUnitTests} =~ /\b$key\b/ ) |
| 1218 |
{ |
| 1219 |
$localConf->logger->debug("-> Ignore test for $key\n"); |
| 1220 |
next; |
| 1221 |
} |
| 1222 |
hdebug("Testing $key"); |
| 1223 |
my $attr = $attrs->{$key}; |
| 1224 |
my $type = $types->{ $attr->{type} } if $attr; |
| 1225 |
unless ( $type or $attr->{test} ) { |
| 1226 |
$localConf->logger->debug("Unknown attribute $key, deleting it\n") |
| 1227 |
if $localConf; |
| 1228 |
delete $conf->{$key}; |
| 1229 |
next; |
| 1230 |
} |
| 1231 |
|
| 1232 |
# Vhost, CAS, SAML, OIDC options |
| 1233 |
if ( $key =~ |
| 1234 |
/^(?^:(?:(?:(?:saml(?:ID|S)|oidc[OR])P|cas(?:App|Srv))MetaData|vhost)Options)$/ |
| 1235 |
) |
| 1236 |
{ |
| 1237 |
|
| 1238 |
# Iterate on vhost names, or saml/cas/oidc configuration keys |
| 1239 |
for my $vhost ( keys %{ $conf->{$key} } ) { |
| 1240 |
my $options = $conf->{$key}->{$vhost}; |
| 1241 |
if ( ref($options) eq "HASH" ) { |
| 1242 |
|
| 1243 |
# Recurse on option list, |
| 1244 |
# FIXME this does check for oidcRPMetaDataOptionsXXX |
| 1245 |
# appearing under samlSPMetadataOptions |
| 1246 |
$res = 0 |
| 1247 |
unless $self->_unitTest( $options, $localConf, |
| 1248 |
"$key/$vhost/" ); |
| 1249 |
} |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
else { |
| 1254 |
|
| 1255 |
# Check if key exists |
| 1256 |
unless ($attr) { |
| 1257 |
push @{ $self->errors }, { message => "__unknownKey__: $key" }; |
| 1258 |
$res = 0; |
| 1259 |
next; |
| 1260 |
} |
| 1261 |
|
| 1262 |
# Hash parameters |
| 1263 |
if ( $key =~ /^$simpleHashKeys$/ ) { |
| 1264 |
$conf->{$key} //= {}; |
| 1265 |
unless ( ref $conf->{$key} eq 'HASH' ) { |
| 1266 |
push @{ $self->errors }, |
| 1267 |
{ message => "$key is not a hash ref" }; |
| 1268 |
$res = 0; |
| 1269 |
next; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
elsif ( $attr->{type} =~ /Container$/ ) { |
| 1273 |
|
| 1274 |
#TODO |
| 1275 |
} |
| 1276 |
if ( $key =~ /^(?:$simpleHashKeys|$doubleHashKeys)$/ |
| 1277 |
or $attr->{type} =~ /Container$/ ) |
| 1278 |
{ |
| 1279 |
$res = 0 |
| 1280 |
unless ( |
| 1281 |
$self->_execTest( { |
| 1282 |
keyTest => $attr->{keyTest} // $type->{keyTest}, |
| 1283 |
keyMsgFail => $attr->{keyMsgFail} |
| 1284 |
// $type->{keyMsgFail}, |
| 1285 |
test => $attr->{test} // $type->{test}, |
| 1286 |
msgFail => $attr->{msgFail} // $type->{msgFail}, |
| 1287 |
}, |
| 1288 |
$conf->{$key}, |
| 1289 |
$key, $attr, undef, $conf |
| 1290 |
) |
| 1291 |
); |
| 1292 |
} |
| 1293 |
elsif ( defined $attr->{keyTest} ) { |
| 1294 |
|
| 1295 |
#TODO |
| 1296 |
} |
| 1297 |
else { |
| 1298 |
my $msg = $attr->{msgFail} // $type->{msgFail}; |
| 1299 |
$res = 0 |
| 1300 |
unless ( |
| 1301 |
$self->_execTest( |
| 1302 |
$attr->{test} // $type->{test}, |
| 1303 |
$conf->{$key}, $key, $attr, $msg, $conf |
| 1304 |
) |
| 1305 |
); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
} |
| 1309 |
return $res; |
| 1310 |
} |
| 1311 |
|
| 1312 |
##@method private boolean _execTest($test, $value) |
| 1313 |
# Execute the given test with value |
| 1314 |
#@param test that can be a code-ref, or a regexp |
| 1315 |
#@return result of test |
| 1316 |
sub _execTest { |
| 1317 |
my ( $self, $test, $value, $key, $attr, $msg, $conf ) = @_; |
| 1318 |
my $ref; |
| 1319 |
die |
| 1320 |
"Malformed test for $key: only regexp ref or sub are accepted (type \"$ref\")" |
| 1321 |
unless ( $ref = ref($test) and $ref =~ /^(CODE|Regexp|HASH)$/ ); |
| 1322 |
if ( $ref eq 'CODE' ) { |
| 1323 |
my ( $r, $m ) = ( $test->( $value, $conf, $attr ) ); |
| 1324 |
if ($m) { |
| 1325 |
push @{ |
| 1326 |
$self->{ ( |
| 1327 |
$r > 0 |
| 1328 |
? 'warnings' |
| 1329 |
: ( $r < 0 ? 'needConfirmation' : 'errors' ) |
| 1330 |
) |
| 1331 |
} |
| 1332 |
}, |
| 1333 |
{ message => "$key: $m" }; |
| 1334 |
} |
| 1335 |
elsif ( !$r ) { |
| 1336 |
push @{ $self->{errors} }, { message => "$key: $msg" }; |
| 1337 |
} |
| 1338 |
return $r; |
| 1339 |
} |
| 1340 |
elsif ( $ref eq 'Regexp' ) { |
| 1341 |
my $r = $value =~ $test; |
| 1342 |
push @{ $self->errors }, { message => "$key: $msg" } unless ($r); |
| 1343 |
return $r; |
| 1344 |
} |
| 1345 |
|
| 1346 |
# Recursive test (for locationRules,...) |
| 1347 |
else { |
| 1348 |
my $res = 1; |
| 1349 |
return $res unless ( ref($value) eq 'HASH' ); |
| 1350 |
foreach my $k ( keys %$value ) { |
| 1351 |
$res = 0 |
| 1352 |
unless ( |
| 1353 |
$self->_execTest( |
| 1354 |
$test->{keyTest}, $k, "$key/$k", |
| 1355 |
$attr, $test->{keyMsgFail}, $conf |
| 1356 |
) |
| 1357 |
and $self->_execTest( |
| 1358 |
$test->{test}, $value->{$k}, "$key/$k", |
| 1359 |
$attr, $test->{msgFail}, $conf |
| 1360 |
) |
| 1361 |
); |
| 1362 |
} |
| 1363 |
return $res; |
| 1364 |
} |
| 1365 |
} |
| 1366 |
|
| 1367 |
##@method private boolean _globalTest() |
| 1368 |
# Launch all tests declared in Lemonldap::NG::Manager::Conf::Tests::tests() |
| 1369 |
# |
| 1370 |
#@return true if tests succeed |
| 1371 |
sub _globalTest { |
| 1372 |
my ( $self, $localConf ) = @_; |
| 1373 |
|
| 1374 |
require Lemonldap::NG::Manager::Conf::Tests; |
| 1375 |
hdebug('# _globalTest()'); |
| 1376 |
my $result = 1; |
| 1377 |
my $tests = &Lemonldap::NG::Manager::Conf::Tests::tests( $self->newConf ); |
| 1378 |
|
| 1379 |
foreach my $name ( keys %$tests ) { |
| 1380 |
if ( $localConf |
| 1381 |
and $localConf->{skippedGlobalTests} |
| 1382 |
and $localConf->{skippedGlobalTests} =~ /\b$name\b/ ) |
| 1383 |
{ |
| 1384 |
$localConf->logger->debug("-> Ignore test for $name\n") |
| 1385 |
if $localConf; |
| 1386 |
next; |
| 1387 |
} |
| 1388 |
my $sub = $tests->{$name}; |
| 1389 |
my ( $res, $msg ); |
| 1390 |
eval { |
| 1391 |
( $res, $msg ) = $sub->(); |
| 1392 |
if ( $res == -1 ) { |
| 1393 |
push @{ $self->needConfirmation }, { message => $msg }; |
| 1394 |
} |
| 1395 |
elsif ($res) { |
| 1396 |
if ($msg) { |
| 1397 |
push @{ $self->warnings }, { message => $msg }; |
| 1398 |
} |
| 1399 |
} |
| 1400 |
else { |
| 1401 |
$result = 0; |
| 1402 |
push @{ $self->errors }, { message => $msg }; |
| 1403 |
} |
| 1404 |
}; |
| 1405 |
if ($@) { |
| 1406 |
push @{ $self->warnings }, "Test $name failed: $@"; |
| 1407 |
$localConf->logger->debug("Test $name failed: $@\n") if $localConf; |
| 1408 |
} |
| 1409 |
} |
| 1410 |
return $result; |
| 1411 |
} |
| 1412 |
|
| 1413 |
1; |
| 1414 |
__END__ |
| 1415 |
|
| 1416 |
=head1 NAME |
| 1417 |
|
| 1418 |
=encoding utf8 |
| 1419 |
|
| 1420 |
Lemonldap::NG::Manager::Conf::Parser - Perl extension for parsing new uploaded |
| 1421 |
configurations. |
| 1422 |
|
| 1423 |
=head1 SYNOPSIS |
| 1424 |
|
| 1425 |
require Lemonldap::NG::Manager::Conf::Parser; |
| 1426 |
my $parser = Lemonldap::NG::Manager::Conf::Parser->new( |
| 1427 |
{ tree => $new, refConf => $self->currentConf } |
| 1428 |
); |
| 1429 |
my $res = { result => $parser->check }; |
| 1430 |
$res->{message} = $parser->{message}; |
| 1431 |
foreach my $t (qw(errors warnings changes)) { |
| 1432 |
push @{ $res->{details} }, { message => $t, items => $parser->$t } |
| 1433 |
if ( @{$parser->$t} ); |
| 1434 |
} |
| 1435 |
|
| 1436 |
=head1 DESCRIPTION |
| 1437 |
|
| 1438 |
Lemonldap::NG::Manager::Conf::Parser checks new configuration |
| 1439 |
|
| 1440 |
This package is used by Manager to examine uploaded configuration. It is |
| 1441 |
currently called using check() which return a boolean. |
| 1442 |
check() looks if a newConf is available. If not, it builds it from uploaded |
| 1443 |
JSON (using scanTree() subroutine) |
| 1444 |
|
| 1445 |
Messages are stored in errors(), warnings() and changes() as arrays. |
| 1446 |
|
| 1447 |
This interface uses L<Plack> to be compatible with CGI, FastCGI,... |
| 1448 |
|
| 1449 |
=head1 SEE ALSO |
| 1450 |
|
| 1451 |
L<Lemonldap::NG::Manager>, L<http://lemonldap-ng.org/> |
| 1452 |
|
| 1453 |
=head1 AUTHORS |
| 1454 |
|
| 1455 |
=over |
| 1456 |
|
| 1457 |
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team> |
| 1458 |
|
| 1459 |
=back |
| 1460 |
|
| 1461 |
=head1 BUG REPORT |
| 1462 |
|
| 1463 |
Use OW2 system to report bug or ask for features: |
| 1464 |
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues> |
| 1465 |
|
| 1466 |
If you want to report a bug concerning configuration upload, please change |
| 1467 |
HIGHDEBUG constant value to produce more logs. Then post them in the Gitlab |
| 1468 |
ticket. |
| 1469 |
|
| 1470 |
=head1 DOWNLOAD |
| 1471 |
|
| 1472 |
Lemonldap::NG is available at |
| 1473 |
L<https://lemonldap-ng.org/download> |
| 1474 |
|
| 1475 |
=head1 COPYRIGHT AND LICENSE |
| 1476 |
|
| 1477 |
See COPYING file for details. |
| 1478 |
|
| 1479 |
This library is free software; you can redistribute it and/or modify |
| 1480 |
it under the terms of the GNU General Public License as published by |
| 1481 |
the Free Software Foundation; either version 2, or (at your option) |
| 1482 |
any later version. |
| 1483 |
|
| 1484 |
This program is distributed in the hope that it will be useful, |
| 1485 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1486 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 1487 |
GNU General Public License for more details. |
| 1488 |
|
| 1489 |
You should have received a copy of the GNU General Public License |
| 1490 |
along with this program. If not, see L<http://www.gnu.org/licenses/>. |
| 1491 |
|
| 1492 |
=cut |