-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwso2ciphertoolconfig.pl
executable file
·837 lines (695 loc) · 24.4 KB
/
wso2ciphertoolconfig.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#!/usr/bin/perl -w
#############################################################################
# Program wso2ciphertool.pl #
# #
# Author Axel Zuber #
# Created 11.09.2016 #
# #
# Description scan the wso2 config files for password tags. #
# collect the #
# - file name #
# - the password location #
# - the password text #
# in the secure vault configuration files #
# cipher-tools.properties #
# cipher-text.properties #
# for use with the ciphertool.sh utility. #
# #
# ------------------------------------------------------------------------- #
# #
# Input <carbon_home>/repository/conf #
# #
# ------------------------------------------------------------------------- #
# #
# Output <carbon_home>/repository/conf/security/cipher-tool.properties #
# <carbon_home>/repository/conf/security/cipher-text.properties #
# #
# ------------------------------------------------------------------------- #
# #
# Exit code #
# 0 : Successful #
# 4 : Warnings #
# 8 : Errors #
# #
# ------------------------------------------------------------------------- #
# #
# History #
# Ver Date Name Description #
# 1.0 11.09.2016 A. Zuber Initial version #
# #
#############################################################################
# ------------------------------------------------------------------------- #
# SAX callback functions #
# ------------------------------------------------------------------------- #
package ConfigScanner;
use base qw(XML::SAX::Base);
my $verbose = 0;
my $debug = 0;
my $headerdone = 0;
my $elementPath = [];
my $inPassword = 0;
my $inName = 0;
my $currfile;
my $resultset;
my $numPasswords;
# scope $adminpassword explicitly so that it can be assigned in MAIN
our $adminpassword;
sub new()
{
my $invocant = shift;
my $class = ref($invocant) || $invocant; # Object or class name
my $self = {}; # initiate our handy hashref
bless( $self, $class ); # make it usable
$currfile = shift;
$resultset = shift;
$elementPath = [];
$headerdone = 0;
$numPasswords = 0;
return $self;
}
# testForPassword : return true if element/attribute name
# match one of the password patterns.
# return false for non-matches or for
# matches with a reject-pattern.
sub testForPassword
{
my ( $string ) = @_;
return ( ( $string =~ /.*password$/i
|| $string =~ /.*KeyPassword$/i
|| $string =~ /.*keystorePass$/i
|| $string =~ /ConnectionPassword/
)
&& ( $string !~ /StoreSaltedPassword/
&& $string !~ /mail\.smtp\.password/
)
);
}
sub attribList2String
{
my ( $attriblist ) = @_;
my $s = "";
map { $s = "$s $_->{attrname}=$_->{attrval}"; } @$attriblist;
$s =~ s/^\ //;
return $s;
}
sub getResultSet() { return $resultset; }
sub getPasswordsFound() { return $numPasswords; }
##
# deep copy of element path
##
sub cloneXPath
{
my @clone = ();
map { push @clone, $_ } @$elementPath;
return \@clone;
}
sub addPasswordReference
{
my ( $pwdText ) = @_;
# replace reference to password ${admin.password} password text from user-mgt.xml
#
if ($pwdText =~ /^\$\{.+\}$/)
{
SWITCH: {
($pwdText =~ /admin.password/) && do {
$pwdText = $adminpassword;
last SWITCH;
};
do {
die "password reference $pwdText can't be resolved";
}
}
}
printf "\nFILE : %s\n", $currfile if $verbose && !$debug && !$headerdone;
$headerdone = 1;
printf "====>: %s=[%s]%s\n", MAIN::generateXPath($elementPath), $pwdText, $skipPwd?" skipped: yes":"" if $verbose || $debug;
if (!$skipPwd)
{
$numPasswords += 1;
push @$resultset,
{
'file' => $currfile,
'pwdpath' => cloneXPath($elementPath),
'pwdtext' => $pwdText
}
}
}
##
# method names as specified by SAX
##
sub start_element
{
my ( $self, $data ) = @_;
my $newElement = $data->{Name};
my $attributes = $data->{Attributes};
my $attriblist = ();
if ( keys %$attributes )
{
# 1. scan for identifying attributes
map {
my $attrib = $attributes->{$_};
my $attribname = $attrib->{LocalName};
my $attribvalue = $attrib->{Value};
push @$attriblist,
{ 'attrname' => $attribname, 'attrval' => $attribvalue };
# add 'name', 'username' and 'type' attribute values to current element
$newElement .= "[\@$attribname=\'$attribvalue\']"
if exists { "name" => 1, "username" => 1, "type" => 1 }->{$attribname};
} keys %$attributes;
# 2. scan for passwords
map {
my $attrib = $attributes->{$_};
my $attribname = $attrib->{LocalName};
my $attribvalue = $attrib->{Value};
# process passwords configured as element attributes
if ( testForPassword($attribname) )
{
push @$elementPath, $newElement . "[\@$attribname]";
addPasswordReference($attribvalue);
pop @$elementPath;
}
if ( testForPassword($attribvalue) )
{
$inPassword = 1;
}
} keys %$attributes;
}
push @$elementPath, $newElement;
if ($debug)
{
printf "BEG : %s\n", $data->{Name};
printf "ATTR : %s\n", attribList2String($attriblist) if scalar $attriblist;
printf "PATH : %s\n", MAIN::generateXPath($elementPath);
}
# pattern-driven state transition
$inPassword = 1 if testForPassword( $data->{Name} );
$inName = 1 if $data->{Name} =~ /^name$/;
}
sub characters
{
# method names are specified by SAX
my ( $self, $data ) = @_;
my $char = $data->{Data};
$char =~ tr/\x{d}\x{a}//d;
$char =~ s/^\s+|\s+$//g;
if ($debug)
{
printf "CHAR : %s\n", $char if $char ne "";
}
if ($inName)
{
# a 'name' element is converted into an attribute
# and added to the topmost element on the stack.
pop @$elementPath;
my $tos = pop @$elementPath;
$tos .= "[name=\'$char\']";
push @$elementPath, $tos;
}
addPasswordReference( $data->{Data} ) if $inPassword;
}
sub end_element
{
# method names are specified by SAX
my ( $self, $data ) = @_;
# discard elements without a name
pop @$elementPath if !$inName;
# reset state at the end of an element
$inPassword = 0;
$inName = 0;
if ($debug)
{
printf "END : %s\n", $data->{Name};
printf "PATH : %s\n", MAIN::generateXPath($elementPath);
}
}
# ------------------------------------------------------------------------- #
# MAIN program #
# ------------------------------------------------------------------------- #
package MAIN;
require 5.008;
use strict 'vars';
use warnings;
use utf8;
use Carp qw(carp cluck croak confess);
use File::Basename;
use File::Find;
use File::Spec;
use Getopt::Long;
use Getopt::Std;
use IO::File;
use Time::HiRes qw(clock);
use XML::Parser;
use XML::SAX::ParserFactory;
use XML::Simple;
use POSIX qw(strftime);
# ------------------------------------------------------------------------- #
# global variables #
# ------------------------------------------------------------------------- #
my @todolist;
my $totalFiles;
my $impactedFiles;
my $totalPasswords;
# ------------------------------------------------------------------------- #
# process the files from the configuration directory #
# ------------------------------------------------------------------------- #
# retrieve the the password to replace
# ${admin.password} references by.
#
sub retrieveAdminPassword
{
my ( $configpath ) = @_;
my $usermgtxml = "$configpath/user-mgt.xml";
die "user-mgt.xml not found in $configpath" if ! -f $usermgtxml;
my $admpwd;
my $filehandle = IO::File->new("< $usermgtxml") or die "Could not open $usermgtxml";
# create object
my $xmlin = new XML::Simple(
forcearray => 1,
keeproot => 1
);
# read XML file into an XML hash
my $xmldata = $xmlin->XMLin($filehandle);
$filehandle->close();
$admpwd = $xmldata->{UserManager}[0]->{Realm}[0]->{Configuration}[0]->{AdminUser}[0]->{Password}[0];
if ( ref $admpwd eq "HASH")
{
die "admin password already encrypted in user-mgt.xml";
}
# printf "retrieved admin.password $admpwd from $usermgtxml";
die "admin password not found in user-mgt.xml" if !defined $admpwd;
return $admpwd;
}
# wanted : - collect all .xml file names
# from the /repository/conf tree.
# - reject out-of-scope files.
sub wanted
{
my $currfile = $File::Find::name; # pick the absolute filename
my $notwanted = 1 if
$currfile eq "."
|| $currfile eq ".."
|| $currfile !~ /.*\.xml$/
|| $currfile =~ qw[/repository/conf/multitenancy/stratos.xml] # no internet connection to stratos
|| $currfile =~ qw[/repository/conf/tomcat/tomcat-users.xml] # tomcat not exposed to user
;
push @todolist, $currfile if !$notwanted;
return;
}
##
# parse the input file
##
sub processSingleFile
{
my ( $infile, $resulthashtable ) = @_;
printf "\nFILE : %s\n", $infile if $debug;
$totalFiles += 1;
my $handler = ConfigScanner->new( $infile, $resulthashtable );
my $parser = XML::SAX::ParserFactory->parser( Handler => $handler );
$parser->parse_uri($infile);
my $np = $handler->getPasswordsFound();
if ( $np > 0 )
{
$impactedFiles += 1;
$totalPasswords += $np;
}
return $handler->getResultSet();
}
##
# parse all files from the todo list
##
sub scanInputFiles
{
my ( $configpath ) = @_;
find( \&wanted, $configpath );
my $result = [];
$totalFiles = 0;
$impactedFiles = 0;
$totalPasswords = 0;
map { processSingleFile( $_, $result ); } sort @todolist;
return $result;
}
# ------------------------------------------------------------------------- #
# generate the output files #
# ------------------------------------------------------------------------- #
# generatePasswordAlias: convert the element path into a password alias.
# skip the first element, since it only brackets
# the file contents.
sub generatePasswordAlias
{
my ( $scannedXPath, $scannedFile ) = @_;
my $alias = "";
my $delim = "";
my $state = 0;
my $allxmluc = 1;
map {
my $newElement = $_;
if ($state)
{
my $pathelem;
my $pathattr;
my $isCompound = 0;
# translate path elements with attributes into
# the attribute value (required for datasources)
$allxmluc = 0 if $newElement =~ /^[a-z]/;
if ( $newElement =~ /(.+)\[[^=]+=\'([^']+)'\]/ )
{
$pathelem = $1;
$pathattr = $2;
$isCompound = 1;
}
elsif ( $newElement =~ /(.+)\[\@([^\]]+)\]/ )
{
$pathelem = $1;
$pathattr = $2;
$isCompound = 1;
}
if ($isCompound)
{
SWITCH:
{
( $alias =~ /Server.Service/ && $pathelem =~ /Connector/ ) && do
{
$newElement = "$pathelem.$pathattr";
last SWITCH;
};
( $alias =~ /Tomcat/ && $pathelem =~ /user/ ) && do
{
$newElement = "$pathelem.$pathattr";
last SWITCH;
};
( $pathelem =~ /axisconfig/ ) && do
{
$newElement = "Axis2";
last SWITCH;
};
( $pathelem =~ /transport(Receiver|Sender)/ ) && do
{
$newElement = "$pathelem.$pathattr";
last SWITCH;
};
( $pathelem =~ /Service/ && $pathattr =~ /Catalina/ ) && do
{
$newElement = "Server.Service";
last SWITCH;
};
( $pathelem =~ /Environment/ && $pathattr =~ /hybrid/ ) && do
{
$alias = "APIGateway";
$newElement = "";
$delim = "";
last SWITCH;
};
do
{
$newElement = $pathattr;
last SWITCH;
};
}
}
SWITCH:
{
( $alias =~ /^Axis2.transportSender/ && $pathelem =~ /parameter/ ) && do
{
$alias = "Axis2.Https.Listener";
$newElement = "";
$delim = "";
last SWITCH;
};
( $alias =~ /^Axis2.transportReceiver/ && $pathelem =~ /parameter/ ) && do
{
$alias = "Axis2.Https.Sender";
$newElement = "";
$delim = "";
last SWITCH;
};
( $alias =~ /^Axis2/ && $scannedFile =~ /.*axis2_(.+)\.xml$/ ) && $pathelem =~ /parameter/ && do
{
my $axis2Qualifier = $1 if $scannedFile =~ /.*axis2_(.+)\.xml$/;
$axis2Qualifier =~ s/_//g;
$alias = "Axis2.$axis2Qualifier";
$newElement = "";
$delim = "";
last SWITCH;
};
( $alias =~ /^Security/ && $newElement =~ /(KeyStore)|(TrustStore)/ ) && do
{
$alias = "Carbon.Security";
last SWITCH;
};
( $alias =~ /^Realm.Configuration/ && $newElement =~ /AdminUser/ ) && do
{
$alias = "UserManager";
last SWITCH;
};
}
$alias .= "$delim$newElement";
$delim = ".";
}
else
{
SWITCH:
{
( $newElement =~ /axisconfig/ ) && do
{
$alias = "Axis2";
$delim = ".";
last SWITCH;
};
( $newElement =~ /tomcat-users/ ) && do
{
$alias = "Tomcat";
$delim = ".";
last SWITCH;
};
}
$state = 1;
}
} @$scannedXPath;
return { 'ALIAS' => $alias, 'ALLXMLELEMENTSUPPERCASE' => $allxmluc };
}
##
# generateXPath: create an XPath expression from the element path.
##
sub generateXPath
{
my ( $elementPath ) = @_;
my $path = "";
map {
my $newElement = $_;
my $pathelem;
my $pathattr;
my $isCompound = 0;
if ( $newElement =~ /(.+)\[[^=]+=\'([^']+)'\]/ )
{
$pathelem = $1;
$pathattr = $2;
$isCompound = 1;
}
elsif ( $newElement =~ /(.+)\[\@([^\]]+)\]/ )
{
$pathelem = $1;
$pathattr = $2;
$isCompound = 1;
}
if ($isCompound)
{
SWITCH:
{
( $pathelem =~ /axisconfig/ ) && do
{
$newElement = "$pathelem";
last SWITCH;
};
( $pathelem =~ /Service/ && $pathattr =~ /Catalina/ ) && do
{
$newElement = "Service";
last SWITCH;
};
( $pathelem =~ /Environment/ && $pathattr =~ /hybrid/ ) && do
{
$newElement = "Environment";
last SWITCH;
};
do
{
last SWITCH;
};
}
}
$path = "$path/$newElement";
} @$elementPath;
return $path;
}
sub getLoggingTime
{
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
my $nice_timestamp = sprintf( "%04d.%02d.%02d %02d:%02d:%02d",
$year + 1900,
$mon + 1, $mday, $hour, $min, $sec );
return $nice_timestamp;
}
##
# generateOutputFiles : create cipher-text.properties and cipher-tool.properties
##
sub generateOutputFiles
{
my ( $result, $carbonhome, $cipherpath ) = @_;
my $date = getLoggingTime();
my $toolname = "cipher-tool.properties";
my $textname = "cipher-text.properties";
my $toolhandle;
my $texthandle;
if ( $debug )
{
$toolname= "-";
$textname= "-";
}
else
{
$toolname= "$cipherpath/$toolname";
$textname= "$cipherpath/$textname";
}
$toolhandle = new IO::File(">$toolname");
$texthandle = new IO::File(">$textname");
print $toolhandle <<"FOO_BAR";
# cipher-tool.properties. Generated on $date.
# This properties file contains all the aliases to be used in carbon components.
# If any property need to be secured, you need to add alias name and the value.
# This value is described as follows.
# The value goes as, the file name//xpath to the property value to be secured,
# true if xml elements start with capital letter.
# Please check existing property values below.
FOO_BAR
my $checkDuplicateAlias = {};
map {
my $relativeConfigPath = File::Spec->abs2rel( $_->{file}, $carbonhome );
my $alias = generatePasswordAlias( $_->{pwdpath}, $_->{file} );
my $aliastext = $alias->{ALIAS};
##
# die a horrible death if the alias is not unique
##
if ( exists $checkDuplicateAlias->{$aliastext} )
{
my $prevFile = $checkDuplicateAlias->{$aliastext};
die "alias $aliastext for file $relativeConfigPath already defined for file $prevFile";
}
else
{
$checkDuplicateAlias->{$aliastext} = $relativeConfigPath;
}
printf $toolhandle "%s=%s/%s,%s\n",
$aliastext,
$relativeConfigPath,
generateXPath( $_->{pwdpath} ),
##
# the true/false label of an XPath expression
# triggers special processing of named elements.
# the DOM parser regularly aborts, so set it
# always false and verify if this works out.
##
"false";
# $alias->{ALLXMLELEMENTSUPPERCASE} ? "true" : "false";
} @$result;
printf "\n\n";
print $texthandle <<"FOO_BAR";
# cipher-text.properties. Generated on $date.
# This is the file based secret repository, used by Secret Manager of synapse secure vault
# By default, This file contains the secret alias names Vs the plain text passwords enclosed with '[]' brackets
# In Production environments, It is recommend to replace those plain text password by the encrypted values.
# CipherTool can be used for it.
FOO_BAR
map {
my $alias = generatePasswordAlias( $_->{pwdpath}, $_->{file} );
printf $texthandle "%s=[%s]\n", $alias->{ALIAS}, $_->{pwdtext};
} @$result;
printf "Found %d passwords in %d out of %d files.\n",
$totalPasswords,
$impactedFiles,
$totalFiles;
if ( !$debug )
{
printf "Created output files :\n%s\n%s\n",
$toolname,
$textname;
}
}
########################################################################
##
## Help screen
##
########################################################################
sub printHelpScreen
{
my ( $usage ) = @_;
my @helptext = (
'',
"wso2ciphertool.pl - create ciphertool properties files from wso2 configuration",
'',
"$usage",
'',
"create cipher-text.properties and cipher-tool.properties files",
"by scanning wso2 configuration files for plaintext passwords.",
'',
"Parameters:",
'',
"location of carbon home directory",
'',
"Options:",
'',
"-d Debug",
"-h print help screen",
"-v increase verbosity",
"-x print list of xml parsers"
);
map { print $_ . "\n"; } @helptext;
}
sub listXMLParsers
{
my @parsers = @{ XML::SAX->parsers() };
map
{
my $p = $_;
print "\n", $p->{Name}, "\n";
map
{
my $f = $_;
print "$f => ", $p->{Features}->{$f}, "\n";
} sort keys %{ $p->{Features} };
} @parsers;
}
##
#-----------------------------------------------------------------------
# main
#-----------------------------------------------------------------------
#
{
my $scriptName = basename($0);
my $usage = "Usage: $scriptName [-dhvx] <carbonhome>";
# Check Options
my $optStr = 'dDhHvVxX';
my %options = ();
die 'Invalid option(s) given' if ( !getopts( "$optStr", \%options ) );
$verbose = 1 if exists $options{v};
$debug = 1 if exists $options{d} || exists $options{D};
# print help screen
if ( (!@ARGV || exists $options{h}) && !exists $options{x} )
{
printHelpScreen($usage);
exit 0;
}
# print list of xml parsers available for perl
if ( exists $options{x} )
{
listXMLParsers();
exit 0;
}
my $carbonhome = $ARGV[0];
my $configpath = File::Spec->canonpath("$carbonhome/repository/conf");
my $cipherpath = File::Spec->canonpath("$carbonhome/repository/conf/security");
die "CARBON_HOME path $carbonhome does not exist" if !-d "$carbonhome";
die "directory $configpath does not exist. check CARBON_HOME" if !-d "$configpath";
die "directory $cipherpath does not exist. check CARBON_HOME" if !-d "$cipherpath";
$adminpassword = retrieveAdminPassword( $configpath );
my $result = scanInputFiles( $configpath );
generateOutputFiles( $result, $carbonhome, $cipherpath );
}