summaryrefslogtreecommitdiffstats
path: root/tests/dogtag/shared/rhcs-shared.sh
blob: c82b8d3a24e3fad769edf6b491b9e743387d433d (plain)
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
#!/bin/sh

########################################################################
#  RHCS SERVER SHARED LIBRARY
#######################################################################
# Includes:
#       verifyErrorMsg
#       submit_log
#       submit_instance_logs
#	submit_log <file>
#	submit_instance_logs <instance_name>
#	rhcs_start_instance <instance_name>
#	rhcs_stop_instance <instance_name>
#	runJava <java class> <input>
#	set_javapath
#	install_and_trust_CA_cert <ca_server_root> <nss_db_dir>
#       install_and_trust_user_cert <certificate pem file> <nickname> <nss-db-directory>
#	disable_ca_nonce <ca_server_root>
#	enable_ca_nonce <ca_server_root>
#	importP12File <P12FileLocation> <P12FilePassword> <nssdbDirectory> <nssdbPassword> <cert_nickname>
#       forward_system_clock <number_of_days>
#       reverse_system_clock <number_of_days>
#       replace_string_in_a_file <file_name> <original_string> <replace_string>
######################################################################
#######################################################################

#########################################################################
# verifyErrorMsg Usage:
#	verifyErrorMsg <command> <expected_msg>
#######################################################################

verifyErrorMsg()
{
   local command="$1"
   local expmsg=$2
   local expErrorCode=$3
   local rc=0

   rm -rf /tmp/errormsg.out /tmp/errormsg_clean.out
   rlLog "Executing: $command"
   $command
   rc=$?
   if [ $rc -eq 0 ] ; then
        rlLog "ERROR: Expected \"$command\" to fail."
        rc=1
   else
        rlLog "\"$command\" failed as expected."
        rlRun "$command 2> /tmp/errormsg.out" 255 "\"$command\" failed"
        sed 's/"//g' /tmp/errormsg.out > /tmp/errormsg_clean.out
        actual=`cat /tmp/errormsg_clean.out`
        if [[ "$actual" = "$expmsg" ]] ; then
                rlPass "Error message as expected: $actual"
                if [ $expErrorCode ] ; then
                        if [[ "$rc" = "$expErrorCode" ]] ; then
                                rlPass "Error code as expected: $rc"
                                return 0
                        else
                                rlLog "ERROR: Error code is not expected. GOT: $rc EXP: $expErrorCode"
                                return 1
                        fi
                else
                        rlLog "Error code validation is not selected."
                        return 0
                fi
        else
                rlFail "ERROR: Message not as expected. GOT: $actual  EXP: $expmsg"
                return 1
        fi
  fi
  return $rc
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#   rhcs_quick_uninstall
#   Usage: rhcs_quick_uninstall
#
# This will uninstall RHCS and related components.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rhcs_quick_uninstall(){
	echo "rhcs_quick_uninstall"
	# Uninstall/unconfigure RHCS

} #rhcs_quick_uninstall


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# submit_log
#   Usage: submit_log <logfilename>
#
# This will backup and submit a log file to beaker.  The backup file
# submitted is named $LOGFILE.$DATE
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
submit_log(){
	if [ $# -ne 1 ]; then
		echo "Usage: $FUNCNAME <log filename>"
		return 1
	fi

	if [ ! -d /tmp/logbackups ]; then
		mkdir /tmp/logbackups
	fi
	local DATE=$(date +%Y%m%d-%H%M%S)
	local LOGFILE=$1
	local LOGBACK=$LOGFILE.$DATE
	if [ -f $LOGFILE ]; then
		rlLog "Backing up and submitting $LOGFILE"
		cp $LOGFILE $LOGBACK
		rhts-submit-log -l $LOGBACK
	else
		rlLog "Cannot file $LOGFILE"
	fi
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# submit_instance_logs
#   Usage: submit_instance_logs <instance_name>
#
# This will rhts-submit various/all RHCS subsystem related log files to beaker for
# debugging, troubleshooting, and/or record keeping
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
submit_instance_logs(){
	INSTANCE_ID=$1
	submit_log /var/log/$INSTANCE_ID-install.log
	submit_log /var/lib/$INSTANCE_ID/logs/selftests.log
	submit_log /var/lib/$INSTANCE_ID/logs/catalina.out
	submit_log /var/lib/$INSTANCE_ID/logs/debug
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# rhcs_start_instance
#   Usage: rhcs_start_instance <instance_name>
#
# This will
# start RHCS instance
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rhcs_start_instance(){
        TOMCAT_ID=$1
	rlLog "Executing: systemctl start pki-tomcatd@$TOMCAT_ID.service"
	systemctl start pki-tomcatd@$TOMCAT_ID.service
	sleep 60
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# rhcs_stop_instance
#   Usage: rhcs_stop_instance <instance_name>
#
# This will
# stop RHCS instance
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rhcs_stop_instance(){
        TOMCAT_ID=$1
	rlLog "Executing: systemctl stop pki-tomcatd@$TOMCAT_ID.service"
	systemctl stop pki-tomcatd@$TOMCAT_ID.service
        sleep 60
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# runJava
#   Usage: runJava <java class> <input>
# This will execute the java classes
# returns the output of the java program
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
runJava(){
	javaclass="$1"
        input_file="$2"
	data=$(< $input_file)
	input=`echo $data|tr '\n' ' '`
	rlLog "input=$input"
	command="$javaclass $input"
	echo $CLASSPATH | grep "."
	if [ $? -eq 0 ] ; then
		rlRun "/usr/bin/java $command > /tmp/java_output.out"
	else
		rlRun "set_javapath"
		rlRun "source /opt/rhqa_pki/env.sh"
		rlRun "/usr/bin/java -cp \"$CLASSPATH\" $command > /tmp/java_output.out"
	fi
        cat /tmp/java_output.out
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# set_javapath
#   Usage: set_javapath
# function to set java path
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set_javapath(){
        arch=`uname -p`
        os_flavor=`uname -s`
        classpath=""
        echo $os_flavor | grep "Linux"
        if [ $? -eq 0 ] ; then
		echo $arch | grep "x86_64"
                if [ $? -eq 0 ] ; then
                        classpath="./:/usr/lib/java/jss4.jar:/usr/share/java/ldapjdk.jar:/usr/share/java/pki/pki-certsrv.jar:/usr/share/java/pki/pki-cmscore.jar:/usr/share/java/pki/pki-nsutil.jar:/usr/share/java/pki/pki-cmsutil.jar:/usr/share/java/pki/pki-tools.jar:/usr/share/java/xml-commons-resolver.jar:/usr/share/java/xerces-j2.jar:/usr/share/java/apache-commons-codec.jar:/usr/share/java/pki/pki-silent.jar:/opt/rhqa_pki/java/generateCRMFRequest.jar:"
                else
                        classpath="./:/usr/lib/java/jss4.jar:/usr/share/java/ldapjdk.jar:/usr/share/java/pki/pki-certsrv.jar:/usr/share/java/pki/pki-cmscore.jar:/usr/share/java/pki/pki-nsutil.jar:/usr/share/java/pki/pki-cmsutil.jar:/usr/share/java/pki/pki-tools.jar:/usr/share/java/xml-commons-resolver.jar:/usr/share/java/xerces-j2.jar:/usr/share/java/apache-commons-codec.jar:/usr/share/java/pki/pki-silent.jar:/opt/rhqa_pki/java/generateCRMFRequest.jar:"
                fi
		echo "export CLASSPATH=$classpath" >> /opt/rhqa_pki/env.sh
                return 0
        else
                rlLog "OS flavor is not Linux"
                return 1
        fi
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#set_newjavapath()
#	Usage: set_newjavapath <classpath>
#	Function to add new jar files to the classpath
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set_newjavapath()
{
	classpath=$1
	arch=$(uname -p)
	os_flavour=$(uname -s)
	echo $os_flavour | grep "Linux"
	if [ $? -eq 0 ]; then
	#we check CLASSPATH variable is already available
	echo $CLASSPATH | grep  "."
		if [ $? -eq 0 ]; then
		echo $CLASSPATH | grep $classpath
			if [ $? -eq 0 ]; then
				rlRun "echo \"export CLASSPATH=$classpath\" >> /opt/rhqa_pki/env.sh"
				return 0
			else
				#CLASSPATH is set but the jar file that we are newly adding is missing
				#so we add the missing jar files to the classpath 
				CLASSPATH=$CLASSPATH:$classpath:
				rlRun "echo \"export CLASSPATH=$classpath\" >> /opt/rhqa_pki/env.sh"
				return 0
			fi
		else
	#No ClASSPATH environment variable available so set set CLASSPATH
			rlLog "Set CLASSPATH we are here"
			rlRun "echo \"export CLASSPATH=./:$classpath\" >> /opt/rhqa_pki/env.sh"
		return 0
		fi
	else
		rlLog "OS flavour is not Linux"
		return 1
	fi
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# install_and_trust_CA_cert
#   Usage: install_and_trust_CA_cert <ca_server_root> <nss-db-directory>
#
# This will check and install CA certificate in a given nss-db
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
install_and_trust_CA_cert(){
	local ca_server_root="$1"
	local nss_db_dir="$2"
	local rc=0
	ca_cert_nick=`cat $ca_server_root/conf/CS.cfg | grep "ca.cert.signing.nickname="|  cut -d "=" -f 2`
	ca_nss_dir="$ca_server_root/alias"
	rlLog "CA cert nickname = $ca_cert_nick"
	rlRun "certutil -d $ca_nss_dir -L -n \"$ca_cert_nick\" -a > $nss_db_dir/ca_cert.pem"
	rlRun "certutil -d $nss_db_dir -A -n \"$ca_cert_nick\" -i $nss_db_dir/ca_cert.pem -t \"CT,CT,CT\" "
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# install_and_trust_KRA_cert
#   Usage: install_and_trust_KRA_cert <kra_server_root> <nss-db-directory>
#
# This will check and install CA certificate in a given nss-db
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
install_and_trust_KRA_cert(){
        local kra_server_root="$1"
        local nss_db_dir="$2"
        local rc=0
        kra_cert_nick=`cat $kra_server_root/conf/CS.cfg | grep "kra.cert.subsystem.nickname="|  cut -d "=" -f 2`
        kra_nss_dir="$kra_server_root/alias"
        rlLog "KRA cert nickname = $kra_cert_nick"
        rlRun "certutil -d $kra_nss_dir -L -n \"$kra_cert_nick\" -a > $nss_db_dir/kra_cert.pem"
        rlRun "certutil -d $nss_db_dir -A -n \"$kra_cert_nick\" -i $nss_db_dir/kra_cert.pem -t \"CT,CT,CT\" "
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# install_and_trust_user_cert
#   Usage: install_and_trust_user_cert <certificate pem file> <nickname> <nss-db-directory>
#
# This will check and install user certificate in a given nss-db
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
install_and_trust_user_cert(){
        local cert_pem_file="$1"
        local user_cert_nick="$2"
        local nss_db_dir="$3"
        rlRun "certutil -d $nss_db_dir -A -n \"$user_cert_nick\" -i $cert_pem_file -t \"u,u,u\" "
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# forward_system_clock
#   Usage: forward_system_clock <number_of_days>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
forward_system_clock(){
        local number_of_days=$1
        rlLog "Current Date/Time: $(date)"
        rlRun "chronyc -a 'manual on' 1> $TmpDir/chrony.out" 0 "Set chrony to manual mode"
        rlAssertGrep "200 OK" "$TmpDir/chrony.out"
        local cur_date=$(date)
        rlLog "Move system to $cur_date + $number_of_days days ahead"
        rlRun "chronyc -a -m 'offline' 'settime $cur_date + $number_of_days days' 'makestep' 'manual reset' 1> $TmpDir/chrony.out"
        rlLog "Date after modifying using chrony: $(date)"
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# reverse_system_clock
#   Usage: reverse_system_clock <number_of_days>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reverse_system_clock(){
        local numdays=$1
        rlLog "number_of_days=$numdays"
        rlLog "Current Date/Time: $(date)"
        local new_string="$numdays days ago"
        local new_date=$(date -d "$new_string")
        rlRun "chronyc -a -m 'settime $new_date' 'makestep' 'manual reset' 'online' 1> $TmpDir/chrony.out"
        rlAssertGrep "200 OK" "$TmpDir/chrony.out"
        rlLog "Date after modifying using chrony: $(date)"
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#       replace_string_in_a_file <file_name> <original_string> <replace_string>
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
replace_string_in_a_file()
{
        local file_name=$1
        local original_string=$2
        local replace_string=$3
        local rc=0
        temp_file="$file_name.temp"
        rlRun "sed 's/$original_string/$replace_string/g' $file_name > $temp_file"
        cp $temp_file $file_name
        cat $file_name | grep $replace_string
        if [ $? -ne 0 ] ; then
                rlLog "$file_name did not get replaced with $replace_string"
                rc=1
        fi
        return $rc
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# disable_ca_nonce
#   Usage: disable_ca_nonce <ca_server_root>
#
# Disable Nonce -- no session id required for command line requests
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
disable_ca_nonce(){
        local ca_tomcat_name=$1
        local rc=0
        rlLog "Configuring ca.enableNonces=false ..."
        ca_config_file="/var/lib/pki/$ca_tomcat_name/ca/conf/CS.cfg"
        temp_file="$ca_config_file.temp"
        search_string="ca.enableNonces=true"
        replace_string="ca.enableNonces=false"
        rlRun "sed 's/$search_string/$replace_string/g' $ca_config_file > $temp_file"
        cp $temp_file $ca_config_file
        chown pkiuser:pkiuser $ca_config_file
        cat $ca_config_file | grep $replace_string
        if [ $? -eq 0 ] ; then
                rhcs_stop_instance $ca_tomcat_name
                rhcs_start_instance $ca_tomcat_name
        else
                lLog "$ca_config_file did not get configured with $replace_string"
                rc=1
        fi
        return $rc
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# enable_ca_nonce
#   Usage: enable_ca_nonce <ca_server_root>
#
# Enable Nonce -- session id is required for command line requests
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
enable_ca_nonce(){
        local ca_tomcat_name=$1
        local rc=0
        rlLog "Configuring ca.enableNonces=true ..."
        ca_config_file="/var/lib/pki/$ca_tomcat_name/ca/conf/CS.cfg"
        temp_file="$ca_config_file.temp"
        search_string="ca.enableNonces=false"
        replace_string="ca.enableNonces=true"
        rlRun "sed 's/$search_string/$replace_string/g' $ca_config_file > $temp_file"
        cp $temp_file $ca_config_file
        chown pkiuser:pkiuser $ca_config_file
        cat $ca_config_file | grep $replace_string
        if [ $? -eq 0 ] ; then
                rhcs_stop_instance $ca_tomcat_name
                rhcs_start_instance $ca_tomcat_name
        else
                rlLog "$ca_config_file did not get configured with $replace_string"
                rc=1
        fi
        return $rc
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# importP12File Usage:
#       importP12File <P12FileLocation> <P12FilePassword> <nssdbDirectory> <nssdbPassword> <cert_nickname>
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
importP12File()
{
        local cert_p12file=$1
        local p12file_password=$2
        local nssdb_dir=$3
        local nss_db_password=$4
        local cert_nickname=$5
        local rc=0
        exp=$nssdb_dir/expfile.out
        tmpout=$nssdb_dir/tmpout.out

        rlLog "cert_p12file = $cert_p12file"
        rlLog "nss_db_dir = $nssdb_dir"
        rlRun "echo \"$nss_db_password\" > $nssdb_dir/passwd_certdb"
	create_certdb $nssdb_dir $nss_db_password
        rlRun "echo \"$p12file_password\" > $nssdb_dir/cert_p12_password"
        local cmd="pk12util -i $cert_p12file -d $nssdb_dir -w $nssdb_dir/cert_p12_password"
        echo "set timeout 5" > $exp
        echo "set force_conservative 0" >> $exp
        echo "set send_slow {1 .1}" >> $exp
        echo "spawn $cmd" >> $exp
        echo 'expect "*Password*: "' >> $exp
        echo "send -s -- \"$nss_db_password\r\"" >> $exp
        echo 'expect eof ' >> $exp
        rlLog "cat $exp"
        /usr/bin/expect $exp > $tmpout 2>&1
        if [ $? = 0 ]; then
                cat $tmpout | grep "pk12util: PKCS12 IMPORT SUCCESSFUL"
                if [ $? = 0 ]; then
                        rlPass "pk12util command executed successfully"
                        rlRun "certutil -L -d $nssdb_dir | grep $cert_nickname" 0 "Verify certificate is installed"
                else
                        rlFail "ERROR: Certificate is not installed in $nssdb_dir"
                        rc=1
                fi

        else
                rlFail "ERROR: pk12util execution failed."
        fi
        return $rc
}



# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# check_coredump
#   Usage: check_coredump
#
# This will check for any coredump messages in abrt output and try to
# generate backtrace.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check_coredump(){

        /usr/bin/abrt-cli list | grep Directory |  awk '{print $2}'
                crashes=`/usr/bin/abrt-cli list | grep Directory |  awk '{print $2}' | wc -l`
                if [ $crashes -ne 0 ]; then
                        echo "Crash detected."
                        for dir in `/usr/bin/abrt-cli list | grep Directory |  awk '{print $2}'`; do
                                cd $dir
                                /usr/bin/abrt-action-install-debuginfo -v;
                                /usr/bin/abrt-action-generate-backtrace -v;
                                /usr/bin/rhts-submit-log -l backtrace
                                /usr/bin/reporter-mailx -v
                        done
                else
                        echo "No crash detected."
                fi


} #check_coredump

#############################################################################
# makereport Usage: (generates summary report)
#       makereport <full_path_and_name_for_report_location>
#############################################################################

makereport()
{
    #check_coredump
    local report=$1
    if [ -n "$report" ];then
        touch $report
    else
        if [ ! -w "$report" ];then
            report=/tmp/rhts.report.$RANDOM.txt
            touch $report
        else
            touch $report
        fi
    fi
    # capture the result and make a simple report
    local total=`rlJournalPrintText | grep "RESULT" | wc -l`
    local unfinished=`rlJournalPrintText | grep "RESULT" | grep "\[unfinished\]" | wc -l`
    local pass=`rlJournalPrintText | grep "RESULT" | grep "\[   PASS   \]" | wc -l`
    local fail=`rlJournalPrintText | grep "RESULT" | grep "\[   FAIL   \]" | wc -l`
    local abort=`rlJournalPrintText | grep "RESULT" | grep "\[  ABORT   \]" | wc -l`
    if rlJournalPrintText | grep "^:: \[   FAIL   \] :: RESULT: $"
    then
        total=$((total-1))
        fail=$((fail-1))
    fi
    echo "========================== Final Pass/Fail Report ===========================" > $report
    echo "  Test Date: `date` " >> $report
    echo "     Total : [$total] "  >> $report
    echo "     Passed: [$pass] "   >> $report
    echo "     Failed: [$fail] "   >> $report
    echo " Unfinished: [$unfinished] "   >> $report
    echo "     Abort : [$abort]"   >> $report
    echo "     Crash : [$crashes]" >> $report
    echo " ---------------------------------------------------------" >> $report
    rlJournalPrintText | grep "RESULT" | grep "\[   PASS   \]"| sed -e 's/:/ /g' -e 's/RESULT//g' >> $report
    echo "" >> $report
    rlJournalPrintText | grep "RESULT" | grep "\[   FAIL   \]"| grep -v "^:: \[   FAIL   \] :: RESULT: $" | sed -e 's/:/ /g' -e 's/RESULT//g'  >> $report
    echo "" >> $report
    rlJournalPrintText | grep "RESULT" | grep "\[unfinished\]"| sed -e 's/:/ /g' -e 's/RESULT//g' >> $report
    echo "" >> $report
    rlJournalPrintText | grep "RESULT" | grep "\[  ABORT   \]"| sed -e 's/:/ /g' -e 's/RESULT//g' >> $report
    echo "===========================[$report]===============================" >> $report
    cat $report
    echo "[`date`] test summary report saved as: $report"
    echo ""
} #makereport


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
#################################################################################
#This functions is alternate of importP12File function,
#importP12FileNew <P12FileLocation> <P12FilePassword> <nssdbDirectory> <nssdbPassword> <cert_nickname>
#################################################################################
importP12FileNew()                                                                                                                                                                            
{                                                                                                                                                                                             
        local cert_p12file=$1                                                                                                                                                                 
        local p12file_password=$2                                                                                                                                                             
        local nssdb_dir=$3                                                                                                                                                                    
        local nss_db_password=$4                                                                                                                                                              
        local cert_nickname=$5                                                                                                                                                                
        rlLog "cert_p12file = $cert_p12file"                                                                                                                                                  
        rlLog "nss_db_dir = $nssdb_dir"
	rlLog "Verify $nssdb_dir is already NSS Database"
	if [ -d "$nssdb_dir" ]; then
		rlLog "$nssdb_dir is already a existing database we don't create a new one"
	else
		rlLog "$nssdb_dir doesn't exist"
		rlRun "pki -d $nssdb_dir -c $nss_db_password client-init" 0 "Initialiaize $nssdb_dir with password $nss_db_password"
	fi
        rlRun "pk12util -i $cert_p12file -d $nssdb_dir -K $nss_db_password -W $p12file_password"
        if [ $? = 0 ]; then                                                                                                                                                                   
                        rlPass "pk12util command executed successfully"                                                                                                                       
                        rlRun "certutil -L -d $nssdb_dir | grep $cert_nickname" 0 "Verify certificate is installed"                                                                           
                else                                                                                                                                                                          
                        rlFail "ERROR: pk12util execution failed."                                                                                                                            
                        rlFail "ERROR: Certificate is not installed in $nssdb_dir"                                                                                                            
                        rc=1                                                                                                                                                                  
                fi                                                                                                                                                                            
        return $rc                                                                                                                                                                            
}
###############################################
#set_time_source setups up chrony with a clocksource 
#set_time_source $timeserver
###############################
set_time_source()
{
	# first stop the existing chrony daemon
	local clocksource=$1
	local chrony_config_file=/etc/chrony.conf
	local chrony_config_backupfile=/etc/chrony.conf.backup
	rlLog "Stopping chrony daemon"
	rlRun "systemctl stop chronyd"
	rlLog "Backup existing chrony config file"
	rlRun "/usr/bin/cp -f $chrony_config_file $chrony_config_backupfile"
	# we setup initial chrony configuration
	echo -e "server $clocksource" > $chrony_config_file
	echo -e "stratumweight 0" >> $chrony_config_file
	echo -e "driftfile /var/lib/chrony/drift" >> $chrony_config_file
	echo -e "rtcsync" >> $chrony_config_file
	echo -e "makestep 10 3" >> $chrony_config_file
	echo -e "bindcmdaddress 127.0.0.1" >> $chrony_config_file
	echo -e "bindcmdaddress ::1" >> $chrony_config_file
	echo -e "keyfile /etc/chrony.keys" >> $chrony_config_file
	echo -e "commandkey 1" >> $chrony_config_file
	echo -e "generatecommandkey" >> $chrony_config_file
	echo -e "noclientlog" >> $chrony_config_file
	echo -e "logchange 0.5" >> $chrony_config_file
	echo -e "logdir /var/log/chrony" >> $chrony_config_file
	echo -e "log measurements statistics tracking" >> $chrony_config_file
	#restore the selinux context just in case
	rlRun "restorecon -v $chrony_config_file"
	rlRun "systemctl start chronyd"
	#Run makestep to sync the time 
	rlLog "Sync time"
	rlRun "chronyc -a makestep 1> /tmp/chrony.out"
	rlAssertGrep "200 OK" "/tmp/chrony.out"
	
}
#################################################
#cert-hold_expect_data <file where expect data will be written> <file_containing_certificate_details> 
#<command line to be spawned>
##################################################
cert-hold_expect_data()
{
        local expfile=$1
        local cert_info=$2
        local cmdline=$3
        local cert_serialNumber=$(cat $cert_info| grep cert_serialNumber | cut -d- -f2)
        local start_date=$(cat $cert_info | grep cert_start_date | cut -d- -f2)
        local end_date=$(cat $cert_info | grep cert_end_date | cut -d- -f2)
        local requestdn=$(cat $cert_info | grep cert_requestdn | cut -d- -f2)
        echo "set timeout 5" > $expfile
        echo "spawn $cmdline" >> $expfile
        echo "expect -exact \"Placing certificate on-hold:\\r" >> $expfile
        echo "   Serial Number: $cert_serialNumber\\r" >> $expfile
        echo "   Issuer: CN=CA Signing Certificate,O=$CA_DOMAIN Security Domain\\r" >> $expfile
        echo "   Subject: $requestdn\\r" >> $expfile
        echo "   Status: VALID\\r" >> $expfile
        echo "   Not Before: $start_date\\r" >> $expfile
        echo "   Not After: $end_date\\r" >> $expfile
        echo "Are you sure \(Y/N\)? \"" >> $expfile
        echo "send -- \"y\\r\"" >> $expfile
        echo "expect eof" >> $expfile
}
#################################################
#cert-revoke_expect_data <file where expect data will be written> <file_containing_certificate_details> 
#<command line to be spawned>
##################################################
cert-revoke_expect_data()
{
        local expfile=$1
        local cert_info=$2
        local cmdline=$3
        local cert_serialNumber=$(cat $cert_info| grep cert_serialNumber | cut -d- -f2)
        local start_date=$(cat $cert_info | grep cert_start_date | cut -d- -f2)
        local end_date=$(cat $cert_info | grep cert_end_date | cut -d- -f2)
        local requestdn=$(cat $cert_info | grep cert_requestdn | cut -d- -f2)
        local cmd=$cmdline
        echo "set timeout 5" > $expfile
        echo "spawn $cmdline" >> $expfile
        echo "expect -exact \"Revoking certificate:\\r" >> $expfile
        echo "   Serial Number: $cert_serialNumber\\r" >> $expfile
        echo "   Issuer: CN=CA Signing Certificate,O=$CA_DOMAIN Security Domain\\r" >> $expfile
        echo "   Subject: $requestdn\\r" >> $expfile
        echo "   Status: VALID\\r" >> $expfile
        echo "   Not Before: $start_date\\r" >> $expfile
        echo "   Not After: $end_date\\r" >> $expfile
        echo "Are you sure \(Y/N\)? \"" >> $expfile
        echo "send -- \"y\\r\"" >> $expfile
        echo "expect eof" >> $expfile
}
#################################################
#cert-release-hold_expect_data <file where expect data will be written> <file_containing_certificate_details> 
#<command line to be spawned>
##################################################
cert-release-hold_expect_data()
{
        local expfile=$1
        local cert_info=$2
        local cmdline=$3
        local cert_serialNumber=$(cat $cert_info| grep cert_serialNumber | cut -d- -f2)
        local start_date=$(cat $cert_info | grep cert_start_date | cut -d- -f2)
        local end_date=$(cat $cert_info | grep cert_end_date | cut -d- -f2)
        local requestdn=$(cat $cert_info | grep cert_requestdn | cut -d- -f2)
        echo "set timeout 5" > $expfile
        echo "spawn $cmdline" >> $expfile
        echo "expect -exact \"Placing certificate off-hold:\\r" >> $expfile
        echo "   Serial Number: $cert_serialNumber\\r" >> $expfile
        echo "   Issuer: CN=CA Signing Certificate,O=$CA_DOMAIN Security Domain\\r" >> $expfile
        echo "   Subject: $requestdn\\r" >> $expfile
        echo "   Status: REVOKED\\r" >> $expfile
        echo "   Not Before: $start_date\\r" >> $expfile
        echo "   Not After: $end_date\\r" >> $expfile
        echo "Are you sure \(Y/N\)? \"" >> $expfile
        echo "send -- \"y\\r\"" >> $expfile
        echo "expect eof" >> $expfile
}
##################################################
# Returns Subsystem used by particular Topology
#get_topo_stack takes 2 arguments, 
#1. Role 
#2. Ouptut file to save the topology details
#Examples:
#get_topo_stack MASTER $TmpDir/topo_file
#local KRA_INST=$(cat $TmpDir/topo_file | grep MY_KRA | cut -d= -f2)
#local CA_INST=$(cat $TmpDir/topo_file | grep MY_CA | cut -d= -f2)
################################################
get_topo_stack()
{
	MY_ROLE=$1
	TOPO_FILE=$2
	if [ $MY_ROLE == "MASTER" ]; then
        	echo "MY_CA=ROOTCA" > $TOPO_FILE
	        echo "MY_KRA=KRA3" >> $TOPO_FILE
        	echo "MY_OCSP=OCSP3" >> $TOPO_FILE
	        echo "MY_TKS=TKS1" >> $TOPO_FILE
		echo "MY_TPS=TPS1" >> $TOPO_FILE
		echo "MY_SUBCA=SUBCA1" >> $TOPO_FILE
                echo "MY_CLONE_CA=CLONE_CA1" >> $TOPO_FILE
                echo "MY_CLONE_KRA=CLONE_KRA1" >> $TOPO_FILE
                echo "MY_CLONE_OCSP=CLONE_OCSP1" >> $TOPO_FILE
                echo "MY_CLONE_TKS=CLONE_TKS1" >> $TOPO_FILE
		echo "MY_CLONE_TPS=CLONE_TPS1" >> $TOPO_FILE
	elif [ $MY_ROLE == "SUBCA1" ]; then
        	echo "MY_CA=SUBCA1" > $TOPO_FILE
	        echo "MY_KRA=KRA1" >> $TOPO_FILE
        	echo "MY_OCSP=OCSP1" >> $TOPO_FILE
	elif [ $MY_ROLE = "SUBCA2" ]; then
        	echo "MY_CA=SUBCA2" >> $TOPO_FILE
	        echo "MY_KRA=KRA2" >> $TOPO_FILE
        	echo "MY_OCSP=OCSP2" >> $TOPO_FILE
	elif [ $MY_ROLE = "CLONECA1" ]; then
        	echo "MY_CA=CLONE_CA1" > $TOPO_FILE
	        echo "MY_KRA=CLONE_KRA1" >> $TOPO_FILE
        	echo "MY_OCSP=CLONE_OCSP1" >> $TOPO_FILE
	elif [ $MY_ROLE = "CLONECA2" ]; then
        	echo "MY_CA=CLONE_CA2" > $TOPO_FILE
	        echo "MY_KRA=CLONE_KRA2" >> $TOPO_FILE
        	echo "MY_OCSP=CLONE_OCSP2" >> $TOPO_FILE
	fi
}
#################################################################
#
#When using curl to query/update a resource in console,
#use this function to process the output receieved
#
##################################################################
process_curl_output()
{
        output_file=$1
        sed -i "s/\&/\n&/g" $output_file
        sed -i "s/+//g"  $output_file
        sed -i "s/^&//g" $output_file
        sed -i "s/%3A/":"/g" $output_file
        sed -i "s/%3B/":"/g" $output_file
        sed -i "s/%3D/"="/g" $output_file
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
### This script Creates Posix Users with inetOrgPerson , PosixAccount and ShadowAccount ObjectClass
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
create_dir_user()
{
local SUFFIX=$1
local MAX=`expr $2`
local COUNT=`expr 1`
local temp=`expr 0`
local UIDVALUE=14583100
local GIDVALUE=14564100
local DOMAIN=$(hostname)

                while [ "$COUNT" -le "$MAX" ]
                do

                                echo -e "dn: uid=idmuser$(expr $COUNT),ou=People,$SUFFIX"
                                echo -e "uid: idmuser$(expr $COUNT)"
                                echo -e "cn: idmuser$(expr $COUNT)"
                                echo -e "sn: $(expr $COUNT)"
                                echo -e "objectClass: top"
                                #echo -e "objectClass: account"
                                echo -e "objectClass: posixAccount"
                                echo -e "objectClass: inetOrgPerson"
                                echo -e "loginShell: /bin/bash"
                                echo -e "homeDirectory: /home/student$(expr $COUNT)"
                                uidNumber=$(expr $UIDVALUE + $temp)
                                echo -e "uidNumber: $uidNumber"
                                echo -e "gidNumber: $GIDVALUE"
                                echo -e "userPassword: {SSHA}j3lBh1Seqe4rqF1+NuWmjhvtAni1JC5A"
                                echo -e "mail: idmuser$(expr $COUNT)@$DOMAIN"
                                echo -e "gecos: idmuser$(expr $COUNT) User"
                                echo -e "l: India\n"
                                COUNT=`expr $COUNT + 1`
                                temp=`expr $temp + 1`

                done
                COUNT=`expr 1`
                echo -e "dn: cn=idmusers,ou=Groups,$SUFFIX"
                echo -e "objectClass: posixGroup"
                echo -e "objectClass: top"
                echo -e "objectClass: groupOfUniqueNames"
                echo -e "cn: idmusers"
                echo -e "userPassword: {crypt}x"
                echo -e "gidNumber: $GIDVALUE"
                while [ "$COUNT" -le "$MAX" ]
                do
                        echo -e "memberUid: idmuser$COUNT"
                        COUNT=`expr $COUNT + 1`
                done

}

#################################################################
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
### This script creates a tpsclient enrollment file
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
gen_enroll_data_file()
{
        tps_host=$1
        tps_port=$2
        cuid=$3
        ldap_user=$4
        ldap_userpwd=$5
        data_file=$6
        new_pin="redhat"
        rlLog "$data_file"
        echo "op=var_set name=ra_host value=$tps_host" > $data_file
        echo "op=var_set name=ra_port value=$tps_port" >> $data_file
        echo "op=var_set name=ra_uri value=/tps/tps" >> $data_file
        echo "op=token_set cuid=$cuid msn=01020304 app_ver=6FBBC105 key_info=0101 major_ver=0 minor_ver=0" >> $data_file
        echo "op=token_set auth_key=404142434445464748494a4b4c4d4e4f" >> $data_file
        echo "op=token_set mac_key=404142434445464748494a4b4c4d4e4f" >> $data_file
        echo "op=token_set kek_key=404142434445464748494a4b4c4d4e4f" >> $data_file
        echo "op=ra_enroll uid=$ldap_user pwd=$ldap_userpwd new_pin=$new_pin num_threads=1" >> $data_file
        echo "op=exit" >> $data_file
}
############################################################################################################
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
### This script creates a tpsclient format file
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
gen_format_data_file()
{
        tps_host=$1
        tps_port=$2
        cuid=$3
        ldap_user=$4
        ldap_userpwd=$5
        data_file=$6
        new_pin="redhat"
        rlLog "$data_file"
        echo "op=var_set name=ra_host value=$tps_host" > $data_file
        echo "op=var_set name=ra_port value=$tps_port" >> $data_file
        echo "op=var_set name=ra_uri value=/tps/tps" >> $data_file
        echo "op=token_set cuid=$cuid msn=01020304 app_ver=6FBBC105 key_info=0101 major_ver=0 minor_ver=0" >> $data_file
        echo "op=token_set auth_key=404142434445464748494a4b4c4d4e4f" >> $data_file
        echo "op=token_set mac_key=404142434445464748494a4b4c4d4e4f" >> $data_file
        echo "op=token_set kek_key=404142434445464748494a4b4c4d4e4f" >> $data_file
        echo "op=ra_format uid=$ldap_user pwd=$ldap_userpwd new_pin=$new_pin num_threads=1" >> $data_file
        echo "op=exit" >> $data_file
}
############################################################################################################
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
### This script creates a tpsclient pin reset file
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
gen_pin_reset_data_file()
{
        tps_host=$1
        tps_port=$2
        cuid=$3
        ldap_user=$4
        ldap_userpwd=$5
        data_file=$6
        new_pin="redhat"
        rlLog "$data_file"
        echo "op=var_set name=ra_host value=$tps_host" > $data_file
        echo "op=var_set name=ra_port value=$tps_port" >> $data_file
        echo "op=var_set name=ra_uri value=/tps/tps" >> $data_file
        echo "op=token_set cuid=$cuid msn=01020304 app_ver=6FBBC105 key_info=0101 major_ver=0 minor_ver=0" >> $data_file
        echo "op=token_set auth_key=404142434445464748494a4b4c4d4e4f" >> $data_file
        echo "op=token_set mac_key=404142434445464748494a4b4c4d4e4f" >> $data_file
        echo "op=token_set kek_key=404142434445464748494a4b4c4d4e4f" >> $data_file
        echo "op=ra_reset_pin uid=$ldap_user pwd=$ldap_userpwd new_pin=$new_pin num_threads=1" >> $data_file
        echo "op=exit" >> $data_file
}
#################################################################