Debian Codegraph @

lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Build.pm

raw
1 package Lemonldap::NG::Manager::Build;
2
3 use strict;
4 use utf8;
5 use Mouse;
6 use Lemonldap::NG::Manager::Build::Attributes;
7 use Lemonldap::NG::Manager::Build::Tree;
8 use Lemonldap::NG::Manager::Build::CTrees;
9 use Lemonldap::NG::Manager::Build::PortalConstants;
10 use Lemonldap::NG::Manager::Conf::Zero;
11 use Data::Dumper;
12 use Regexp::Common 'URI';
13 use Regexp::Assemble;
14 use JSON;
15 use Getopt::Std;
16 use IO::String;
17
18 my %peOkValues = (
19 PE_2FWAIT => 'Wait for second factor',
20 PE_IDPCHOICE => 'Prompt to choose identity provider',
21 PE_SENDRESPONSE => 'Send response given in $req->response',
22 PE_INFO => 'Prompt to display $req->info',
23 PE_REDIRECT => 'Redirect user to $req->urldc',
24 PE_DONE => "Don't launch next steps",
25 );
26
27 has structFile => ( isa => 'Str', is => 'ro', required => 1 );
28 has confTreeFile => ( isa => 'Str', is => 'ro', required => 1 );
29 has managerConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
30 has managerAttributesFile => ( isa => 'Str', is => 'ro', required => 1 );
31 has defaultValuesFile => ( isa => 'Str', is => 'ro', required => 1 );
32 has confConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
33 has firstLmConfFile => ( isa => 'Str', is => 'ro', required => 1 );
34 has reverseTreeFile => ( isa => 'Str', is => 'ro', required => 1 );
35 has portalConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
36 has handlerStatusConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
37 has docConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
38
39 my @managerAttrKeys = qw(keyTest keyMsgFail select type test msgFail default);
40 my $format = 'Creating %-69s: ';
41 my $reIgnoreKeys = qr/^$/;
42 my $module = __PACKAGE__;
43
44 my @angularScopeVars;
45 my @bool;
46 my @arrayParam;
47 my @cnodesKeys;
48 my %cnodesRe;
49 my @ignoreKeys;
50 my $ignoreKeys;
51 my $mainTree;
52 my @sessionTypes;
53 my @simpleHashKeys;
54 my @doubleHashKeys;
55 my $authParameters;
56 my $issuerParameters;
57 my $samlServiceParameters;
58 my $oidcServiceParameters;
59 my $casServiceParameters = [];
60 my $defaultValues;
61
62 my $attributes = Lemonldap::NG::Manager::Build::Attributes::attributes();
63 my $jsonEnc = JSON->new()->allow_nonref;
64 $jsonEnc->canonical(1);
65
66 $Data::Dumper::Sortkeys = sub {
67 my ($hash) = @_;
68 return [
69 ( defined $hash->{id} ? ('id') : () ),
70 ( defined $hash->{title} ? ( 'title', ) : () ),
71 (
72 grep { /^(?:id|title)$/ ? 0 : 1 }
73 sort {
74 return 1
75 if ( $a =~ /node/ and $b !~ /node/ );
76 return -1 if ( $b =~ /node/ );
77 lc($a) cmp lc($b);
78 } keys %$hash
79 )
80 ];
81 };
82
83 $Data::Dumper::Deparse = 1;
84 $Data::Dumper::Deepcopy = 1;
85
86 sub run {
87 my $self = shift;
88 $self = $module->new(@_) unless ref $self;
89
90 # 1. confTree.js
91 printf STDERR $format, $self->confTreeFile;
92 $mainTree = Lemonldap::NG::Manager::Build::CTrees::cTrees();
93
94 my $script = 'function templates(tpl,key) {
95 var ind;
96 var scalarTemplate = function(r) {
97 return {
98 "id": tpl+"s/"+(ind++),
99 "title": r,
100 "get": tpl+"s/"+key+"/"+r
101 };
102 };
103 switch(tpl){
104 ';
105
106 my $reverseScanResult =
107 $self->reverseScan( Lemonldap::NG::Manager::Build::Tree::tree(),
108 '', {}, $mainTree );
109
110 # To build confTree.js, each special node is scanned from
111 # Lemonldap::NG::Manager::Build::CTrees
112 foreach my $node ( sort keys %$mainTree ) {
113 @cnodesKeys = ();
114 my $jsonTree = [];
115 $self->scanTree( $mainTree->{$node}, $jsonTree, '__KEY__', '' );
116 $jsonEnc->pretty(1);
117 my $tmp = $jsonEnc->encode($jsonTree);
118 $tmp =~ s!"__KEY__!tpl+"s/"+key+"/"+"!mg;
119 $tmp =~ s/"(true|false)"/$1/sg;
120 $tmp =~ s/:\s*"(\d+)"\s*(["\}])/:$1$2/sg;
121 $script .= " case '$node':
122 return $tmp;
123 ";
124
125 # Second step, Manager/Constants.pm file will contain data issued from
126 # this scan
127 my $ra = Regexp::Assemble->new;
128
129 # Build $oidcOPMetaDataNodeKeys, $samlSPMetaDataNodeKeys,...
130 foreach my $r (@cnodesKeys) {
131 $ra->add($r);
132 }
133 $cnodesRe{$node} = $ra->as_string;
134
135 push @ignoreKeys, $node;
136 }
137 $script .= " default:\n return [];\n }\n}";
138 open F, ">", $self->confTreeFile or die $!;
139 print F $script;
140 close F;
141 print STDERR "done\n";
142 my $ra = Regexp::Assemble->new;
143 foreach my $re (@ignoreKeys) {
144 $ra->add($re);
145 }
146 $ignoreKeys = $ra->as_string;
147 $reIgnoreKeys = $ra->re;
148
149 # Reinitialize $defaultValues
150 $defaultValues = {};
151
152 # 2. struct.json
153 printf STDERR $format, $self->structFile;
154 $mainTree = Lemonldap::NG::Manager::Build::Tree::tree();
155 my $jsonTree = [];
156 $self->scanTree( $mainTree, $jsonTree, '', '' );
157 $script = "\n\nfunction setScopeVars(scope) {\n";
158 foreach my $v (@angularScopeVars) {
159 $script .=
160 " scope.$v->[0] = scope$v->[1];\n scope.getKey(scope.$v->[0]);\n";
161 }
162 $script .= "}";
163 open F, ">>", $self->confTreeFile || die $!;
164 print F $script;
165 close F;
166 open F, ">", $self->structFile || die $!;
167 $jsonEnc->pretty(0);
168 my $tmp = $jsonEnc->encode($jsonTree);
169 $tmp =~ s/"(true|false)"/$1/sg;
170 $tmp =~ s/:\s*"(\d+)"\s*(["\}])/:$1$2/sg;
171 print F $tmp;
172 close F;
173 print STDERR "done\n";
174 $tmp = undef;
175
176 printf STDERR $format, $self->managerConstantsFile;
177
178 open F, ">", $self->managerConstantsFile or die($!);
179 my $exportedVars = '$'
180 . join( 'Keys $',
181 'simpleHash', 'doubleHash', 'specialNode', sort keys %cnodesRe )
182 . 'Keys $specialNodeHash $authParameters $issuerParameters $samlServiceParameters $oidcServiceParameters $casServiceParameters';
183 print F <<EOF;
184 # This file is generated by $module. Don't modify it by hand
185 package Lemonldap::NG::Common::Conf::ReConstants;
186
187 use strict;
188 use Exporter 'import';
189 use base qw(Exporter);
190
191 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
192
193 our %EXPORT_TAGS = ( 'all' => [qw($exportedVars)] );
194 our \@EXPORT_OK = ( \@{ \$EXPORT_TAGS{'all'} } );
195 our \@EXPORT = ( \@{ \$EXPORT_TAGS{'all'} } );
196
197 our \$specialNodeHash = {
198 virtualHosts => [qw(exportedHeaders locationRules post vhostOptions)],
199 samlIDPMetaDataNodes => [qw(samlIDPMetaDataXML samlIDPMetaDataExportedAttributes samlIDPMetaDataOptions)],
200 samlSPMetaDataNodes => [qw(samlSPMetaDataXML samlSPMetaDataExportedAttributes samlSPMetaDataOptions samlSPMetaDataMacros)],
201 oidcOPMetaDataNodes => [qw(oidcOPMetaDataJSON oidcOPMetaDataJWKS oidcOPMetaDataOptions oidcOPMetaDataExportedVars)],
202 oidcRPMetaDataNodes => [qw(oidcRPMetaDataOptions oidcRPMetaDataExportedVars oidcRPMetaDataOptionsExtraClaims oidcRPMetaDataMacros oidcRPMetaDataScopeRules)],
203 casSrvMetaDataNodes => [qw(casSrvMetaDataOptions casSrvMetaDataExportedVars)],
204 casAppMetaDataNodes => [qw(casAppMetaDataOptions casAppMetaDataExportedVars casAppMetaDataMacros)],
205 keyNodes => [qw(keys)],
206 };
207
208 EOF
209
210 # Reinitialize $attributes
211 $attributes = Lemonldap::NG::Manager::Build::Attributes::attributes();
212
213 $ra = Regexp::Assemble->new;
214 foreach (@doubleHashKeys) {
215 $ra->add($_);
216 }
217 print F "our \$doubleHashKeys = '" . $ra->as_string . "';\n";
218 $ra = Regexp::Assemble->new;
219 foreach (@simpleHashKeys) {
220 $ra->add($_);
221 }
222 print F "our \$simpleHashKeys = '"
223 . $ra->as_string . "';\n"
224 . "our \$specialNodeKeys = '${ignoreKeys}s';\n";
225 foreach ( sort keys %cnodesRe ) {
226 print F "our \$${_}Keys = '$cnodesRe{$_}';\n";
227 }
228 print F "\n";
229
230 foreach (qw(authParameters issuerParameters)) {
231 $tmp = "our \$$_ = {\n";
232 no strict 'refs';
233 foreach my $k ( sort keys %$$_ ) {
234 my $v = $$_->{$k};
235 $tmp .= " $k => [qw(" . join( ' ', @$v ) . ")],\n";
236 }
237 print F "$tmp};\n";
238 }
239 foreach (qw(samlServiceParameters oidcServiceParameters)) {
240 no strict 'refs';
241 $tmp = "our \$$_ = [qw(" . join( ' ', @$$_ ) . ")];\n";
242 print F "$tmp";
243 }
244
245 print F "\n1;\n";
246 close F;
247 print STDERR "done\n";
248
249 printf STDERR $format, $self->defaultValuesFile;
250 $defaultValues->{locationRules} = $attributes->{locationRules}->{default};
251 foreach ( keys %$attributes ) {
252 if ( not /(?:MetaData|vhost)/
253 and $attributes->{$_}->{default} )
254 {
255 $defaultValues->{$_} = $attributes->{$_}->{default};
256 }
257 }
258 my $defaultAttr = mydump( $defaultValues, 'defaultValues' );
259 $defaultAttr = "# This file is generated by $module. Don't modify it by hand
260 package Lemonldap::NG::Common::Conf::DefaultValues;
261
262 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
263
264 $defaultAttr}
265
266 1;
267 ";
268
269 my $dst;
270
271 eval {
272 require Perl::Tidy;
273 Perl::Tidy::perltidy(
274 source => IO::String->new($defaultAttr),
275 destination => \$dst
276 );
277 };
278 $dst = $defaultAttr if ($@);
279
280 open( F, ">", $self->defaultValuesFile ) or die($!);
281 print F $dst;
282 close F;
283 print STDERR "done\n";
284
285 printf STDERR $format, $self->confConstantsFile;
286 $ra = Regexp::Assemble->new;
287 foreach ( @simpleHashKeys, @doubleHashKeys, sort keys %cnodesRe ) {
288 $ra->add($_);
289 }
290 foreach ( qw(
291 exportedHeaders locationRules post vhostOptions keys
292 samlIDPMetaDataXML samlIDPMetaDataExportedAttributes
293 samlIDPMetaDataURL samlSPMetaDataURL
294 samlIDPMetaDataOptions samlSPMetaDataXML
295 samlSPMetaDataExportedAttributes samlSPMetaDataMacros
296 samlSPMetaDataOptions oidcOPMetaDataJSON
297 oidcOPMetaDataJWKS oidcOPMetaDataOptions
298 oidcOPMetaDataExportedVars oidcRPMetaDataOptions
299 oidcRPMetaDataExportedVars oidcRPMetaDataOptionsExtraClaims
300 oidcRPMetaDataMacros oidcRPMetaDataScopeRules
301 casAppMetaDataExportedVars casAppMetaDataOptions casAppMetaDataMacros
302 casSrvMetaDataExportedVars casSrvMetaDataOptions
303 )
304 )
305 {
306 $ra->add($_);
307 }
308
309 my $sessionTypes = join( "', '", @sessionTypes );
310 my $confConstants =
311 "our \$hashParameters = qr/^" . $ra->as_string . "\$/;\n";
312 $ra = Regexp::Assemble->new;
313 foreach (@arrayParam) {
314 $ra->add($_);
315 }
316
317 # Not in Tree.pm
318 foreach (qw(mySessionAuthorizedRWKeys)) {
319 $ra->add($_);
320 }
321 $confConstants .=
322 "our \$arrayParameters = qr/^" . $ra->as_string . "\$/;\n";
323 $ra = Regexp::Assemble->new;
324 foreach (@bool) {
325 $ra->add($_);
326 }
327 $confConstants .= "our \$boolKeys = qr/^" . $ra->as_string . "\$/;\n";
328 open( F, ">", $self->confConstantsFile ) or die($!);
329 print F <<EOF;
330 # This file is generated by $module. Don't modify it by hand
331 package Lemonldap::NG::Common::Conf::Constants;
332
333 use strict;
334 use Exporter 'import';
335 use base qw(Exporter);
336
337 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
338
339 # CONSTANTS
340
341 use constant CONFIG_WAS_CHANGED => -1;
342 use constant UNKNOWN_ERROR => -2;
343 use constant DATABASE_LOCKED => -3;
344 use constant UPLOAD_DENIED => -4;
345 use constant SYNTAX_ERROR => -5;
346 use constant DEPRECATED => -6;
347 use constant DEFAULTCONFFILE => "/usr/local/lemonldap-ng/etc/lemonldap-ng.ini";
348 use constant DEFAULTSECTION => "all";
349 use constant CONFSECTION => "configuration";
350 use constant PORTALSECTION => "portal";
351 use constant HANDLERSECTION => "handler";
352 use constant MANAGERSECTION => "manager";
353 use constant SESSIONSEXPLORERSECTION => "sessionsExplorer";
354 use constant APPLYSECTION => "apply";
355
356 # Default configuration backend
357 use constant DEFAULTCONFBACKEND => "File";
358 use constant DEFAULTCONFBACKENDOPTIONS => (
359 dirName => '/usr/local/lemonldap-ng/data/conf',
360 );
361 $confConstants
362 our \@sessionTypes = ( '$sessionTypes' );
363
364 sub NO {qr/^(?:off|no|0)?\$/i}
365
366 our %EXPORT_TAGS = (
367 'all' => [
368 qw(
369 CONFIG_WAS_CHANGED
370 UNKNOWN_ERROR
371 DATABASE_LOCKED
372 UPLOAD_DENIED
373 SYNTAX_ERROR
374 DEPRECATED
375 DEFAULTCONFFILE
376 DEFAULTSECTION
377 CONFSECTION
378 PORTALSECTION
379 HANDLERSECTION
380 MANAGERSECTION
381 SESSIONSEXPLORERSECTION
382 APPLYSECTION
383 DEFAULTCONFBACKEND
384 DEFAULTCONFBACKENDOPTIONS
385 NO
386 \$hashParameters
387 \$arrayParameters
388 \@sessionTypes
389 \$boolKeys
390 )
391 ]
392 );
393 our \@EXPORT_OK = ( \@{ \$EXPORT_TAGS{'all'} } );
394 our \@EXPORT = ( \@{ \$EXPORT_TAGS{'all'} } );
395
396 1;
397 EOF
398 close F;
399 print STDERR "done\n";
400
401 printf STDERR $format, $self->managerAttributesFile;
402 my $managerAttr = {
403 map {
404 my @r;
405 foreach my $f (@managerAttrKeys) {
406 push @r, $f, $attributes->{$_}->{$f}
407 if ( defined $attributes->{$_}->{$f} );
408 }
409 ( $_ => {@r} );
410 } keys(%$attributes)
411 };
412 $managerAttr = mydump( $managerAttr, 'attributes' );
413 my $managerSub =
414 Dumper( \&Lemonldap::NG::Manager::Build::Attributes::perlExpr );
415 $managerSub =~ s/\$VAR1 = sub/sub perlExpr/s;
416 $managerSub =~ s/^\s*(?:use strict;|package .*?;|)\n//gm;
417 my $managerTypes =
418 mydump( Lemonldap::NG::Manager::Build::Attributes::types(), 'types' );
419 $managerAttr = "# This file is generated by $module. Don't modify it by hand
420 package Lemonldap::NG::Manager::Attributes;
421
422 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
423
424 $managerSub
425
426 $managerTypes}
427
428 $managerAttr}
429
430 ";
431 eval {
432 Perl::Tidy::perltidy(
433 source => IO::String->new($managerAttr),
434 destination => \$dst
435 );
436 };
437 $dst = $managerAttr if ($@);
438
439 open( F, ">", $self->managerAttributesFile ) or die($!);
440 print F $dst;
441 close F;
442 print STDERR "done\n";
443
444 $self->buildZeroConf();
445
446 printf STDERR $format, $self->reverseTreeFile;
447 open( F, ">", $self->reverseTreeFile ) or die($!);
448 $jsonEnc->pretty(0);
449 print F $jsonEnc->encode($reverseScanResult);
450 close F;
451
452 print STDERR "done\n";
453 $self->buildPortalConstants();
454 }
455
456 sub buildZeroConf {
457 my $self = shift;
458 $jsonEnc->pretty(1);
459 printf STDERR $format, $self->firstLmConfFile;
460 open( F, '>', $self->firstLmConfFile ) or die($!);
461 my $tmp = Lemonldap::NG::Manager::Conf::Zero::zeroConf(
462 '__DNSDOMAIN__', '__SESSIONDIR__',
463 '__PSESSIONDIR__', '__NOTIFICATIONDIR__',
464 '__CACHEDIR__'
465 );
466 $tmp->{cfgNum} = 1;
467 print F $jsonEnc->encode($tmp);
468 close F;
469 print STDERR "done\n";
470 }
471
472 sub buildPortalConstants() {
473 my $self = shift;
474
475 my %portalConstants =
476 %{ Lemonldap::NG::Manager::Build::PortalConstants::portalConstants() };
477 my %reverseConstants = reverse %portalConstants;
478 die "Duplicate value in portal constants"
479 unless %reverseConstants == %portalConstants;
480
481 printf STDERR $format, $self->portalConstantsFile;
482 open( F, '>', $self->portalConstantsFile ) or die($!);
483 my $urire = $RE{URI}{HTTP}{ -scheme => qr/https?/ }{-keep};
484 $urire =~ s/([\$\@])/\\$1/g;
485 my $content = <<EOF;
486 # This file is generated by $module. Don't modify it by hand
487 package Lemonldap::NG::Portal::Main::Constants;
488
489 use strict;
490 use Exporter 'import';
491
492 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
493
494 use constant HANDLER => 'Lemonldap::NG::Handler::PSGI::Main';
495 use constant URIRE => qr{$urire};
496 use constant {
497 EOF
498 for my $pe (
499 sort { $portalConstants{$a} <=> $portalConstants{$b} }
500 keys %portalConstants
501 )
502 {
503 my $str = $portalConstants{$pe};
504 $content .= " $pe => $str,\n";
505 }
506
507 my $exports = join ", ", map { "'$_'" }
508 sort { $portalConstants{$a} <=> $portalConstants{$b} }
509 keys %portalConstants;
510
511 my $portalConstsStr .= mydump( \%reverseConstants, 'portalConsts' );
512 $content .= <<EOF;
513 };
514
515 $portalConstsStr
516 }
517
518 # EXPORTER PARAMETERS
519 our \@EXPORT_OK = ( 'portalConsts', 'HANDLER', 'URIRE', $exports );
520 our %EXPORT_TAGS = ( 'all' => [ \@EXPORT_OK, 'import' ], );
521
522 our \@EXPORT = qw(import PE_OK);
523
524 1;
525 EOF
526
527 my $dst;
528 eval {
529 Perl::Tidy::perltidy(
530 source => IO::String->new($content),
531 destination => \$dst
532 );
533 };
534 $dst = $content if ($@);
535 open( F, '>', $self->portalConstantsFile ) or die($!);
536 print F $dst;
537 close F;
538 print STDERR "done\n";
539
540 printf STDERR $format, $self->handlerStatusConstantsFile;
541
542 # Handler Status file
543 $content = <<EOF;
544 # This file is generated by $module. Don't modify it by hand
545 package Lemonldap::NG::Handler::Lib::StatusConstants;
546
547 use strict;
548 use Exporter 'import';
549
550 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
551
552 $portalConstsStr
553 }
554
555 # EXPORTER PARAMETERS
556 our \@EXPORT_OK = ( 'portalConsts' );
557 our %EXPORT_TAGS = ( 'all' => [ \@EXPORT_OK, 'import' ], );
558
559 1;
560 EOF
561
562 eval {
563 Perl::Tidy::perltidy(
564 source => IO::String->new($content),
565 destination => \$dst
566 );
567 };
568 $dst = $content if ($@);
569 open( F, '>', $self->handlerStatusConstantsFile ) or die($!);
570 print F $dst;
571 close F;
572 print STDERR "done\n";
573
574 printf STDERR $format, $self->docConstantsFile;
575
576 # Doc error code list
577 my %details;
578 if ( open F, 'lemonldap-ng-portal/site/htdocs/static/languages/en.json' ) {
579 local $/ = undef, my $tmp = JSON::from_json(<F>);
580 close F;
581 %details = map { s/^\s+//; $_ } %$tmp;
582 }
583 $content = <<EOF;
584 ..
585 This file is generated by $module. Don't modify it by hand
586
587 Error codes list
588 ================
589
590 .. note::
591
592 This page references all Portal error codes.
593
594 .. csv-table::
595 :header: "Error label", "Error number", "Detail"
596 :delim: ,
597 :widths: auto
598
599
600 EOF
601
602 for my $key (
603 sort { $portalConstants{$a} <=> $portalConstants{$b} }
604 keys %portalConstants
605 )
606 {
607
608 $content .=
609 " ``"
610 . $key . "``,"
611 . $portalConstants{$key} . ','
612 . (
613 %details
614 ? '"'
615 . (
616 $details{"PE$portalConstants{$key}"}
617 || $peOkValues{$key}
618 || [ $key =~ /PE_(.*)$/ ]->[0]
619 )
620 . '"'
621 : ''
622 ) . "\n";
623 }
624
625 open( F, '>', $self->docConstantsFile ) or die($!);
626 print F $content;
627 close F;
628
629 print STDERR "done\n";
630 }
631
632 sub mydump {
633 my ( $obj, $subname ) = @_;
634 my $t = Dumper($obj);
635 $t =~ s/^\s*(?:use strict;|package .*?;|)\n//gm;
636 $t =~ s/\n\s*BEGIN.*?\}\n/\n/sg;
637 $t =~ s/^\$VAR1\s*=/sub $subname {\n return/;
638 return $t;
639 }
640
641 sub scanTree {
642 my ( $self, $tree, $json, $prefix, $path ) = @_;
643 unless ( ref($tree) eq 'ARRAY' ) {
644 die 'Not an array';
645 }
646 $prefix //= '';
647 my $ord = -1;
648 my $nodeName = $path ? '_nodes' : 'data';
649 foreach my $leaf (@$tree) {
650 $ord++;
651 my $jleaf = {};
652
653 # Grouped leaf
654 if ( ref($leaf) and $leaf->{group} ) {
655 die "'form' is required when using 'group'"
656 unless ( $leaf->{form} );
657 push @$json,
658 {
659 id => "$prefix$leaf->{title}",
660 title => $leaf->{title},
661 type => $leaf->{form},
662 get => [ map { "${prefix}$_" } @{ $leaf->{group} } ]
663 };
664 }
665
666 # Subnode
667 elsif ( ref($leaf) ) {
668 $jleaf->{title} = $jleaf->{id} = $leaf->{title};
669 $jleaf->{type} = $leaf->{form} if ( $leaf->{form} );
670 if ( $leaf->{title} =~ /^((?:oidc|saml|cas)Service)MetaData$/ ) {
671 no strict 'refs';
672 my @tmp = $self->scanLeaf( $leaf->{nodes} );
673 ${ $1 . 'Parameters' } = \@tmp;
674 }
675 foreach my $n (qw(nodes nodes_cond)) {
676 if ( $leaf->{$n} ) {
677 $jleaf->{"_$n"} = [];
678 $self->scanTree( $leaf->{$n}, $jleaf->{"_$n"}, $prefix,
679 "$path.$nodeName\[$ord\]" );
680 if ( (
681 $leaf->{title} eq 'authParams'
682 and $n eq 'nodes_cond'
683 )
684 or $leaf->{title} eq 'issuerParams'
685 )
686 {
687 my $vn = $leaf->{title};
688 $vn =~ s/Params$/Parameters/;
689 foreach my $sn ( @{ $leaf->{$n} } ) {
690 no strict 'refs';
691 my @cn = $self->scanLeaf( $sn->{nodes} );
692 ${$vn}->{ $sn->{title} } = \@cn;
693 }
694 }
695 elsif ( $leaf->{title} eq 'issuerParams' ) {
696 }
697 if ( $n eq 'nodes_cond' ) {
698 foreach my $sn ( @{ $jleaf->{"_$n"} } ) {
699 $sn->{show} = 'false';
700 }
701 }
702 }
703 }
704 $jleaf->{help} = $leaf->{help} if ( $leaf->{help} );
705 $jleaf->{_nodes_filter} = $leaf->{nodes_filter}
706 if ( $leaf->{nodes_filter} );
707 push @$json, $jleaf;
708 }
709
710 # Leaf
711 else {
712 # Get data type and build tree
713 #
714 # Types : PerlModule array bool boolOrExpr catAndAppList file
715 # hostname int keyTextContainer lmAttrOrMacro longtext
716 # openidServerList oidcAttributeContainer pcre rulesContainer
717 # samlAssertion samlAttributeContainer samlService select text
718 # trool url virtualHostContainer word password
719
720 if ( $leaf =~ s/^\*// ) {
721 push @angularScopeVars, [ $leaf, "$path._nodes[$ord]" ];
722 }
723 if ( $leaf =~ /^(.*)(?<!notification)StorageOptions$/ ) {
724 push @sessionTypes, $1;
725 }
726 my $attr = $attributes->{$leaf} or die("Missing attribute $leaf");
727
728 #print STDERR "| $attr->{documentation} | $leaf |\n";
729 $jleaf = { id => "$prefix$leaf", title => $leaf };
730 unless ( $attr->{type} ) {
731 print STDERR "Fatal: no type: $leaf\n";
732 exit;
733 }
734
735 # TODO: change this
736 $attr->{type} =~
737 s/^(?:url|word|pcre|lmAttrOrMacro|hostname|PerlModule)$/text/;
738 $jleaf->{type} = $attr->{type} if ( $attr->{type} ne 'text' );
739 foreach my $w (qw(default help select get template)) {
740 $jleaf->{$w} = $attr->{$w} if ( defined $attr->{$w} );
741 }
742 if ( defined $jleaf->{default} ) {
743 unless ( $attr->{type} eq 'bool' and $jleaf->{default} == 0 ) {
744 $defaultValues->{$leaf} = $jleaf->{default};
745 }
746 if ( ref( $jleaf->{default} ) ) {
747 $jleaf->{default} = [];
748 my $type = $attr->{type};
749 $type =~ s/Container//;
750 foreach my $k ( sort keys( %{ $attr->{default} } ) ) {
751
752 # Special handling for oidcAttribute
753 my $default = $attr->{default}->{$k};
754 if ( $attr->{type} eq 'oidcAttributeContainer' ) {
755 $default = [ $default, "string", "auto" ];
756 }
757
758 push @{ $jleaf->{default} },
759 {
760 id => "$prefix$leaf/$k",
761 title => $k,
762 type => $type,
763 data => $default,
764 (
765 $type eq 'rule'
766 ? ( re => $k )
767 : ()
768 ),
769 };
770 }
771 }
772 }
773
774 if ($prefix) {
775 push @cnodesKeys, $leaf;
776 }
777
778 # issue 2439
779 # FIXME: in future versions, oidcOPMetaDataJSON and samlIDPMetaDataXML should
780 # behave the same
781 if ( $leaf =~ /^oidcOPMetaData(?:JSON|JWKS)$/ ) {
782 push @simpleHashKeys, $leaf;
783 }
784
785 if ( $attr->{type} =~ /^(?:catAndAppList|\w+Container)$/ ) {
786 $jleaf->{cnodes} = $prefix . $leaf;
787 unless ( $prefix or $leaf =~ $reIgnoreKeys ) {
788 push @simpleHashKeys, $leaf;
789 }
790 }
791 elsif ( $attr->{type} eq 'doubleHash' and $leaf !~ $reIgnoreKeys ) {
792 push @doubleHashKeys, $leaf;
793 }
794 else {
795 if ( $prefix and !$jleaf->{get} ) {
796 $jleaf->{get} = $prefix . $jleaf->{title};
797 }
798 if ( $attr->{type} eq 'bool' ) {
799 push @bool, $leaf;
800 }
801 if ( $attr->{type} eq 'array' ) {
802 push @arrayParam, $leaf;
803 }
804 }
805 push @$json, $jleaf;
806 }
807 }
808 }
809
810 sub scanLeaf {
811 my ( $self, $tree ) = @_;
812 my @res;
813 foreach my $k (@$tree) {
814 if ( ref $k ) {
815 push @res, $self->scanLeaf1( $k->{nodes} || $k->{group} );
816 }
817 else {
818 push @res, $k;
819 }
820 }
821 return @res;
822 }
823
824 sub scanLeaf1 {
825 my ( $self, $tree ) = @_;
826 my @res;
827 foreach my $k (@$tree) {
828 if ( ref $k ) {
829 push @res, $self->scanLeaf( $k->{nodes} || $k->{group} );
830 }
831 else {
832 push @res, $k;
833 }
834 }
835 return @res;
836 }
837
838 sub reverseScan {
839 my ( $self, $tree, $path, $res, $cTrees ) = @_;
840 foreach my $elem (@$tree) {
841 $elem =~ s/^\*//;
842 if ( ref($elem) eq 'HASH' ) {
843 foreach (qw(nodes nodes_cond group)) {
844 $self->reverseScan( $elem->{$_}, "$path$elem->{title}/", $res,
845 $cTrees )
846 if ( $elem->{$_} );
847 }
848 }
849 else {
850 my ($singular) = $elem =~ /^(.*)s$/;
851 if ( $singular and $cTrees->{$singular} ) {
852 $self->reverseScan( $cTrees->{$singular},
853 "${path}${elem}/__confKey__/", $res, $cTrees );
854 }
855 else {
856 my $tmp = $path;
857 $tmp =~ s#/$##;
858 $res->{$elem} = $tmp;
859 }
860 }
861 }
862 return $res;
863 }
864
865 1;
866 __END__
867
868 =head1 NAME
869
870 =encoding utf8
871
872 Lemonldap::NG::Manager::Build - Static files generator of Lemonldap::NG Web-SSO
873 system.
874
875 =head1 SYNOPSIS
876
877 use Lemonldap::NG::Manager::Build;
878
879 Lemonldap::NG::Manager::Build->run(
880 structFile => "site/htdocs/static/struct.json",
881 confTreeFile => "site/htdocs/static/js/conftree.js",
882 managerConstantsFile => "lib/Lemonldap/NG/Common/Conf/ReConstants.pm",
883 managerAttributesFile => 'lib/Lemonldap/NG/Manager/Attributes.pm',
884 defaultValuesFile => "lib/Lemonldap/NG/Common/Conf/DefaultValues.pm",
885 firstLmConfFile => "_example/conf/lmConf-1.json",
886 reverseTreeFile => "site/htdocs/static/reverseTree.json",
887 );
888
889 =head1 DESCRIPTION
890
891 Lemonldap::NG::Manager::Build is only used to build javascript files and
892 Lemonldap::NG constants Perl files. It must be launched after each change.
893
894 =head2 DEVELOPER CORNER
895
896 To add a new parameter, you have to:
897
898 =over
899
900 =item declare it in Manager/Build/Attributes.pm;
901
902 =item declare its position in the tree in Manager/Build/Tree.pm (or
903 Manager/Build/CTrees.pm for complex nodes);
904
905 =item refresh files by using this (or launch any build makefile target at the
906 root of the Lemonldap::NG project sources).
907
908 =back
909
910 See below for details.
911
912 =head3 Files generated
913
914 `scripts/jsongenerator.pl` file uses Lemonldap::NG::Manager::Build::Attributes,
915 Lemonldap::NG::Manager::Build::Tree and Lemonldap::NG::Manager::Build::CTrees to generate
916
917 =over
918
919 =item `site/htdocs/static/struct.json`:
920
921 Main file containing the tree view;
922
923 =item `site/htdocs/static/js/conftree.js`:
924
925 generates Virtualhosts, SAML and OpenID-Connect partners sub-trees;
926
927 =item `site/htdocs/static/reverseTree.json`:
928
929 map used by manager diff to find attribute position in the tree;
930
931 =item `Lemonldap::NG::Manager::Constants`:
932
933 constants used by all Perl manager components;
934
935 =item `Lemonldap::NG::Common::Conf::DefaultValues`:
936
937 constants used to read configuration;
938
939 =item `Lemonldap::NG::Manager::Attributes`:
940
941 parameters attributes used by the manager during configuration upload;
942
943 =item lmConf-1.json:
944
945 first configuration in file format;
946
947 =back
948
949 =head3 Attribute declaration
950
951 set your new attribute as a key of attributes() function that points to a hash
952 ref containing:
953
954 =over
955
956 =item type (required):
957
958 content type must be declared in sub types() in the same file
959 (except if attribute embeds its own tests) and must match
960 to a form stored in static/forms/ directory;
961
962 =item help (optional):
963
964 Relative HTML path to help page (relative to
965 /doc/pages/documentation/<version>/);
966
967 =item default (recommended):
968
969 default value to set if not defined;
970
971 =item select (optional):
972
973 required only if type is `select`. In this case, it
974 must contain an array of { k => <keyName>, v => <display name> } hashref
975
976 =item documentation (recommended):
977
978 some words for other developers
979
980 =item test (optional):
981
982 if test is not defined for this type or if test must
983 be more restrictive, set here a regular expression or a subroutine. Arguments
984 passed to subroutine are (keyValue, newConf, currentKey). It returns 2
985 arguments: a boolean result and a message (if non empty message will be
986 displayed as warning or error depending of result);
987
988 =item msgFail (optional):
989
990 for regexp based tests, message to display in case of
991 error. Words to translate have to be written as so: __toTranslate__;
992
993 =item keyTest (optional):
994
995 for keys/values attributes, test to be applied on key;
996
997 =item keyMsgFail (optional):
998
999 for regexp based key tests, same as msgFail for keys test;
1000
1001 =back
1002
1003 If you want to declare a new type, you have to declare following
1004 properties:
1005
1006 =over
1007
1008 =item test, msgFail, keyTest, keyMsgFail as shown above,
1009
1010 =item form: form to use if it doesn't have the same name.
1011
1012 =back
1013
1014 =head3 Tree location
1015
1016 The tree is now very simple: it contains nodes and leaves. Leaves are designed only
1017 by their attribute name. All description must be done in the file described
1018 above. Nodes are array member designed as this:
1019
1020 {
1021 title => 'titleToTranslate',
1022 help => 'helpUrl',
1023 form => 'relativeUrl',
1024 nodes => [
1025 ... nodes or leaf ...
1026 ]
1027 }
1028
1029 Explanations:
1030
1031 =over
1032
1033 =item title (required):
1034
1035 it must contain an entry of static/languages/lang.json
1036
1037 =item help (recommended):
1038
1039 as above, the relative HTML path to the help page
1040 (relative to /doc/pages/documentation/<version>/);
1041
1042 =item form (optional):
1043
1044 the name of a static/forms/<name>.html file
1045
1046 =item nodes:
1047
1048 array of sub nodes and leaf attached to this node
1049
1050 =item group:
1051
1052 must never be used in conjunction with nodes. Array of leaves only
1053 to be displayed in the same form
1054
1055 =item nodes_cond:
1056
1057 array of sub nodes that will be displayed with a filter. Not yet
1058 documented here, see the source code of site/htdocs/static/js/filterFunctions.js.
1059
1060 =item nodes_filter:
1061
1062 filter entry in site/htdocs/static/js/filterFunctions.js for the same feature.
1063
1064 =back
1065
1066 =head1 SEE ALSO
1067
1068 L<http://lemonldap-ng.org/>
1069
1070 =head1 AUTHORS
1071
1072 =over
1073
1074 =item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
1075
1076 =back
1077
1078 =head1 BUG REPORT
1079
1080 Use OW2 system to report bug or ask for features:
1081 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
1082
1083 Note that if you want to post a ticket for a conf upload problem, please
1084 see L<Lemonldap::NG::Manager::Conf::Parser> before.
1085
1086 =head1 DOWNLOAD
1087
1088 Lemonldap::NG is available at
1089 L<https://lemonldap-ng.org/download>
1090
1091 =head1 COPYRIGHT AND LICENSE
1092
1093 See COPYING file for details.
1094
1095 This library is free software; you can redistribute it and/or modify
1096 it under the terms of the GNU General Public License as published by
1097 the Free Software Foundation; either version 2, or (at your option)
1098 any later version.
1099
1100 This program is distributed in the hope that it will be useful,
1101 but WITHOUT ANY WARRANTY; without even the implied warranty of
1102 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1103 GNU General Public License for more details.
1104
1105 You should have received a copy of the GNU General Public License
1106 along with this program. If not, see L<http://www.gnu.org/licenses/>.
1107
1108 =cut