-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathoscap_tests.pm
1382 lines (1249 loc) · 58.9 KB
/
oscap_tests.pm
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SUSE's openQA tests
#
# Copyright 2022 SUSE LLC
# SPDX-License-Identifier: FSFAP
#
# Summary: Base module for STIG test cases
# Maintainer: QE Security <[email protected]>
package oscap_tests;
use testapi;
use strict;
use warnings;
use utils;
use base 'opensusebasetest';
use version_utils qw(is_sle is_opensuse);
use bootloader_setup qw(add_grub_cmdline_settings);
use power_action_utils 'power_action';
use Utils::Backends 'is_pvm';
use Utils::Architectures;
use registration qw(add_suseconnect_product get_addon_fullname is_phub_ready);
use List::MoreUtils qw(uniq);
use List::Compare;
use Config::Tiny;
use YAML::PP;
our @EXPORT = qw(
$profile_ID
$ansible_profile_ID
$f_ssg_sle_ds
$f_ssg_sle_xccdf
$f_ssg_ds
$ssg_sle_ds
$ssg_sle_xccdf
$ssg_tw_ds
$ssg_tw_xccdf
$f_stdout
$f_stderr
$f_report
$remediated
$ansible_remediation
$sle_version
$compliance_as_code_path
$evaluate_count
set_ds_file
set_ds_file_name
upload_logs_reports
pattern_count_in_file
oscap_security_guide_setup
oscap_remediate
oscap_evaluate
oscap_evaluate_remote
oscap_upload_debug_logs
);
# The file names of scap logs and reports
our $f_stdout = 'stdout.txt';
our $f_stderr = 'stderr.txt';
our $f_vlevel = 'ERROR';
our $f_report = 'report.html';
our $f_pregex = '\\bpass\\b';
our $f_fregex = '\\bfail\\b';
our $ansible_exclusions;
our $ansible_playbook_modified = 0;
our $compliance_as_code_path;
our $oscap_upload_debug_logs = 0;
# Set default value for 'scap-security-guide' ds file
our $f_ssg_sle_ds = '/usr/share/xml/scap/ssg/content/ssg-sle15-ds.xml';
our $f_ssg_tw_ds = '/usr/share/xml/scap/ssg/content/ssg-opensuse-ds.xml';
our $ssg_sle_ds = 'ssg-sle15-ds.xml';
our $ssg_tw_ds = 'ssg-opensuse-ds.xml';
our $f_ssg_sle_xccdf = '/usr/share/xml/scap/ssg/content/ssg-sle15-xccdf.xml';
our $f_ssg_tw_xccdf = '/usr/share/xml/scap/ssg/content/ssg-opensuse-xccdf.xml';
our $ssg_sle_xccdf = 'ssg-sle15-xccdf.xml';
our $ssg_tw_xccdf = 'ssg-opensuse-xccdf.xml';
our $f_ssg_ds;
# Profile IDs
our $profile_ID = "";
our $ansible_profile_ID = "";
# Profile names:
our $profile_ID_sle_stig = 'xccdf_org.ssgproject.content_profile_stig';
our $profile_ID_sle_cis = 'xccdf_org.ssgproject.content_profile_cis';
our $profile_ID_sle_pci_dss_4 = 'xccdf_org.ssgproject.content_profile_pci-dss-4';
our $profile_ID_sle_hipaa = 'xccdf_org.ssgproject.content_profile_hipaa';
our $profile_ID_sle_anssi_bp28_high = 'xccdf_org.ssgproject.content_profile_anssi_bp28_high';
our $profile_ID_tw = 'xccdf_org.ssgproject.content_profile_standard';
# Ansible playbooks
our $ansible_playbook_sle_stig = "playbook-stig.yml";
our $ansible_playbook_sle_cis = "playbook-cis.yml";
our $ansible_playbook_sle_pci_dss_4 = "playbook-pci-dss-4.yml";
# Only sle-15
our $ansible_playbook_sle_hipaa = "playbook-hipaa.yml";
our $ansible_playbook_sle_anssi_bp28_high = "playbook-anssi_bp28_high.yml";
our $ansible_playbook_standart = "opensuse-playbook-standard.yml";
# The OS status of remediation: '0', not remediated; '>=1', remediated
our $remediated = 0;
# Is it ansible remediation: '0', bash remediation; '1' ansible remediation
our $ansible_remediation = 0;
# Directory where ansible playbooks resides on local system
our $ansible_file_path = "/usr/share/scap-security-guide/ansible/";
our $full_ansible_file_path = "";
# Variables $use_content_type and $remove_rules_missing_fixes are fetched from configuration file located in:
#https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/blob/main/content/openqa_config.conf
# Configuration for the contenyt type (source of ds, ansible and xccdf files):
# If set to 1 - tests will use files from scap-security-guide package
# If set to 2 - tests will use files from compliance-as-code-compiled repository - https://gitlab.suse.de/seccert-public/compliance-as-code-compiled
# If set to 3 - tests will use files cloned and built from ComplianceAsCode repository master branch - https://github.com/ComplianceAsCode/content.git
our $use_content_type = 1;
# Option configures to use or not functionality to remove from DS and ansible file rules for which do not have remediations
# If set to 1 - rules for which do not have remediations will be removed from DS and ansible file rules for which do not have remediations
# If set to 0 - no changes done.
our $remove_rules_missing_fixes = 1;
# Option configures to use or not functionality to exclude rules for profiles defined in file:
# https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/blob/main/content/openqa_tests_exclusions.yaml
# If set to 1 - rules defined in exclusions files are excluded for remediation and evaluation
# If set to 0 - rules defined in exclusions files are not used
our $use_exclusions = 1;
# Keeps count of reboots to control it
our $reboot_count = 0;
# evaluate execution count done by test execution. Set in oscap_security_guide_setup.pm
# and "security/oscap_stig/oscap_xccdf_eval" need to be set in the schedule yaml file accordingly
our $evaluate_count = 2;
# Stores CCE IDs of failed ansible remediation tasks.
# Used in second ansible remediation as exclusions.
our $failed_cce_ids_ref;
# List to collect needed run results
our @test_run_report = ();
# Get sle version "sle12" or "sle15"
our $sle_version = '';
# Stores current SCAP benchmark version
our $benchmark_version = '';
# Upload HTML report by default
set_var('UPLOAD_REPORT_HTML', 1);
sub set_ds_file {
# Set the ds file for separate product, e.g.,
# for SLE15 the ds file is "ssg-sle15-ds.xml";
# for SLE12 the ds file is "ssg-sle12-ds.xml";
# for Tumbleweed the ds file is "ssg-opensuse-ds.xml"
my $version = get_required_var('VERSION') =~ s/([0-9]+).*/$1/r;
$f_ssg_sle_ds =
'/usr/share/xml/scap/ssg/content/ssg-sle' . "$version" . '-ds.xml';
$f_ssg_sle_xccdf =
'/usr/share/xml/scap/ssg/content/ssg-sle' . "$version" . '-xccdf.xml';
}
sub set_ds_file_name {
# Set the ds file name for separate product, e.g.,
# for SLE15 the ds file is "ssg-sle15-ds.xml";
# for SLE12 the ds file is "ssg-sle12-ds.xml";
# for Tumbleweed the ds file is "ssg-opensuse-ds.xml"
my $version = get_required_var('VERSION') =~ s/([0-9]+).*/$1/r;
$ssg_sle_ds =
'ssg-sle' . "$version" . '-ds.xml';
$ssg_sle_xccdf =
'ssg-sle' . "$version" . '-xccdf.xml';
}
sub replace_ds_file {
# Replace original ds file whith built or downloaded from repository
my ($self) = $_[0];
my $ds_file_name = $_[1];
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/content/";
# ComplianceAsCode repository master branch
if ($use_content_type == 3) {
assert_script_run("rm $f_ssg_sle_ds") if script_run "! [[ -e $f_ssg_sle_ds ]]";
# Copy built file to correct location
my $ds_local_full_file_path = "$compliance_as_code_path/build/$ds_file_name";
assert_script_run("cp $ds_local_full_file_path $f_ssg_sle_ds");
record_info("Copied ds file", "Copied file $ds_local_full_file_path to $f_ssg_sle_ds");
}
# compliance-as-code-compiled
elsif ($use_content_type == 2) {
download_file_from_https_repo($url, $ds_file_name);
# Remove original ds file
assert_script_run("rm $f_ssg_sle_ds") if script_run "! [[ -e $f_ssg_sle_ds ]]";
# Copy downloaded file to correct location
assert_script_run("cp $ds_file_name $f_ssg_sle_ds");
record_info("Copied ds file", "Copied file $ds_file_name to $f_ssg_sle_ds");
}
}
sub replace_xccdf_file {
# Replace original xccdf file whith built or downloaded from repository
my ($self) = $_[0];
my $xccdf_file_name = $_[1];
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/content/";
# ComplianceAsCode repository master branch
if ($use_content_type == 3) {
assert_script_run("rm $f_ssg_sle_xccdf") if script_run "! [[ -e $f_ssg_sle_xccdf ]]";
# Copy built file to correct location
my $xccdf_local_full_file_path = "$compliance_as_code_path/build/$xccdf_file_name";
assert_script_run("cp $xccdf_local_full_file_path $f_ssg_sle_xccdf");
record_info("Copied xccdf file", "Copied file $xccdf_local_full_file_path to $f_ssg_sle_xccdf");
}
# compliance-as-code-compiled
elsif ($use_content_type == 2) {
download_file_from_https_repo($url, $xccdf_file_name);
# Remove original xccdf file
assert_script_run("rm $f_ssg_sle_xccdf") if script_run "! [[ -e $f_ssg_sle_xccdf ]]";
# Copy downloaded file to correct location
assert_script_run("cp $xccdf_file_name $f_ssg_sle_xccdf");
record_info("Copied xccdf file", "Copied file $xccdf_file_name to $f_ssg_sle_xccdf");
}
}
sub replace_ansible_file {
# Replace original ansible file whith built or downloaded from repository
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/ansible/";
# ComplianceAsCode repository master branch
if ($use_content_type == 3) {
# Remove original ansible file
assert_script_run("rm $full_ansible_file_path");
my $ansible_local_full_file_path = "$compliance_as_code_path/build/ansible/$ansible_profile_ID";
# Copy built file to correct location
assert_script_run("cp $ansible_local_full_file_path $full_ansible_file_path");
record_info("Copied ansible file", "Copied file $ansible_local_full_file_path to $full_ansible_file_path");
uload_log_file($full_ansible_file_path);
}
# compliance-as-code-compiled
elsif ($use_content_type == 2) {
download_file_from_https_repo($url, $ansible_profile_ID);
# Remove original ansible file
assert_script_run("rm $full_ansible_file_path");
# Copy downloaded file to correct location
assert_script_run("cp $ansible_profile_ID $full_ansible_file_path");
record_info("Copied ansible file", "Copied file $ansible_profile_ID to $full_ansible_file_path");
}
# scap-security-guide
elsif ($use_content_type == 1) {
# Remove original ansible file
assert_script_run("rm $full_ansible_file_path");
# Copy file to correct location
my $ansible_local_full_file_path = "/root/$ansible_profile_ID";
assert_script_run("cp $ansible_local_full_file_path $full_ansible_file_path");
record_info("Copied ansible file", "Copied file $ansible_local_full_file_path to $full_ansible_file_path");
}
}
sub modify_ansible_playbook {
# Modify and backup ansible playbok for later reuse in remediation
if ($ansible_playbook_modified == 0) {
my $ansible_local_full_file_path = "/root/$ansible_profile_ID";
# Copy downloaded file to correct location
assert_script_run("cp $full_ansible_file_path $ansible_local_full_file_path");
record_info("Backuped ansible file", "Backuped file $full_ansible_file_path to $ansible_local_full_file_path");
my $insert_cmd = "sed -i \'s/ tags:/ ignore_errors: true\\n tags:/g\' $full_ansible_file_path";
assert_script_run("$insert_cmd");
record_info("Inserted ignore_errors", "Inserted \"ignore_errors: true\" for every tag in playbook. CMD:\n$insert_cmd");
$ansible_playbook_modified = 1;
}
}
sub backup_ds_file {
# Backup ds file for later reuse
assert_script_run("cp $f_ssg_ds /root/$ssg_sle_ds");
record_info("Backed up ds file", "Backuped file $f_ssg_ds to /root/$ssg_sle_ds");
}
sub restore_ds_file {
# Restore ds file
assert_script_run("rm $f_ssg_ds") if script_run "! [[ -e $f_ssg_ds ]]";
assert_script_run("cp /root/$ssg_sle_ds $f_ssg_ds");
record_info("Restored ds file", "Restored file /root/$ssg_sle_ds to $f_ssg_ds");
}
sub ansible_result_analysis {
#Find count of failed or ignored ansible remediations
my $data = $_[0];
my @report = ();
my $found = 0;
my $full_report = "";
my $failed_number = -1;
my $error_number = -1;
my $ignored_number = -1;
my $i;
my @lines = split /\n|\r/, $data;
for ($i = $#lines; $i >= 0;) {
if ($lines[$i] =~ /PLAY RECAP/) {
$found = 1;
$full_report = $lines[$i + 1];
my @report = split /\s+/, $full_report;
for my $j (0 .. $#report) {
if ($report[$j] =~ /failed/) {
my @failed = split /\=/, $report[$j];
$failed_number = $failed[1];
}
if ($report[$j] =~ /ignored/) {
my @failed = split /\=/, $report[$j];
$ignored_number = $failed[1];
}
}
last;
}
$i--;
}
#Returning results
$_[1] = $full_report;
$_[2] = $failed_number;
$_[3] = $ignored_number;
return $found;
}
sub ansible_failed_tasks_search_vv {
#Find count and rules names of matched pattern
my $data = $_[0];
my @report = ();
my $full_report = "";
my $i;
my $j = 1;
my @failed_tasks = ();
my @tasks_line_numbers = ();
my $found_task = 0;
my @lines = split /\n|\r/, $data;
for ($i = 0; $i <= $#lines;) {
if (($lines[$i] =~ /^fatal:/) or ($lines[$i] =~ /^failed:/)) {
# looking for TASK name in upper lines
unless (($found_task == 1) or ($i - $j == 0)) {
if ($lines[$i - $j] =~ /task path:/) {
# recording task line number in palybook
@report = (split /:/, $lines[$i - $j]);
$report[2] =~ s/\r|\n//g;
push(@tasks_line_numbers, $report[2]);
}
if ($lines[$i - $j] =~ /TASK/) {
$found_task = 1;
}
else { $j++; }
}
$full_report = $lines[$i - $j];
@report = (split /\[/, $full_report, 2);
$report[1] =~ s/\]\s\*+|\n//g;
push(@failed_tasks, $report[1]);
$j = 1;
$found_task = 0;
}
$i++;
}
@failed_tasks = uniq @failed_tasks;
$_[1] = \@failed_tasks;
$_[2] = \@tasks_line_numbers;
my $failed_tasks_size = @failed_tasks;
return $failed_tasks_size;
}
sub find_ansible_cce_by_task_name_vv {
# Finding CCE IDs for failed or ignored rules in ansible playbook
my $data = $_[0];
my $failed_tasks = $_[1];
my $tasks_line_numbers = $_[2];
my $j = 1;
my $i;
my @cce_ids;
my @cce_id_and_name;
my $found_cce = 0;
my $index;
my $line;
my @report = ();
# Join long task name to one line
my @lines = split /\n/, $data;
for ($i = 0; $i <= $#lines;) {
if ($lines[$i] =~ /- name:/) {
$index = index($lines[$i], "me: ");
if ($lines[$i + 1] =~ /^\s{$index}/) {
$lines[$i] =~ s/\r|\n//g;
$lines[$i + 1] =~ s/^\s{$index}//g;
$lines[$i] .= " " . $lines[$i + 1];
}
}
$i++;
}
$i = 0;
for my $task_line_number (@$tasks_line_numbers) {
if ($lines[$task_line_number - 1] =~ /- name:/) {
# looking for task CCE ID
while (($found_cce == 0) or ($task_line_number + $j == $#lines)) {
if ($lines[$task_line_number + $j] =~ /CCE-/) {
$found_cce = 1;
}
else { $j++; }
}
@report = split /\-\s+/, $lines[$task_line_number + $j];
$report[1] =~ s/\r|\n//g;
push(@cce_ids, $report[1]);
$line = "$report[1], @$failed_tasks[$i], Line number: $task_line_number";
push(@cce_id_and_name, $line);
$j = 1;
$found_cce = 0;
}
$i++;
}
@cce_ids = uniq @cce_ids;
@cce_id_and_name = uniq @cce_id_and_name;
my $cce_ids_size = @cce_ids;
$_[3] = \@cce_ids;
$_[4] = \@cce_id_and_name;
return $cce_ids_size;
}
sub uload_log_file {
# Compress and upload single file for reference
my $file_name = $_[0];
if (script_run "! [[ -e $file_name ]]") {
$file_name =~ s/\s//g; # remove whitespaces
script_run "p7zip -k $file_name";
if (script_run "! [[ -e $file_name.7z ]]") {
upload_logs($file_name . ".7z", timeout => 600);
script_run "rm $file_name.7z";
}
}
}
sub upload_logs_reports {
# Upload logs & ouputs for reference
if ($oscap_upload_debug_logs) { # Upload xml logs only if debug needed. Can be set in test command line OSCAP_UPLOAD_DEBUG_LOGS = 1
my $files;
if (is_sle) {
$files = script_output('ls | grep "^ssg-sle.*.xml"');
}
else {
$files = script_output('ls | grep "^ssg-opensuse.*.xml"');
}
foreach my $file (split("\n", $files)) {
uload_log_file($file);
}
}
uload_log_file($f_stdout);
uload_log_file($f_stderr);
if (get_var('UPLOAD_REPORT_HTML')) {
uload_log_file($f_report);
}
}
sub download_file_from_https_repo {
# Downloads file from provided url
my $url = $_[0];
my $file_name = $_[1];
my $full_url = "$url" . "$file_name";
my $result = -1;
my $FULL_URL = get_var("FILE", "$full_url");
if (script_run("wget --no-check-certificate $FULL_URL") != 0) {
record_info("FAILED to Downloaded file", "FAILED to downloaded file $file_name from $FULL_URL");
$result = 0;
}
else {
assert_script_run("chmod 774 $file_name");
record_info("Downloaded file", "Downloaded file $file_name from $FULL_URL");
$result = 1;
}
return $result;
}
sub display_oscap_information {
#Displays OSCAP packages information
# Record the pkgs' version for reference
my $out = script_output("zypper se -s openscap-utils scap-security-guide");
record_info("Pkg_ver", "openscap security guide packages' version:\n $out");
# Check the ds file information for reference
$out = script_output("oscap info $f_ssg_ds", quiet => 1);
record_info("oscap info", "\"# oscap info $f_ssg_ds\" returns:\n $out");
# Check the oscap version information for reference
$out = script_output("oscap -V");
record_info("oscap version", "\"# oscap -V\" returns:\n $out");
}
sub pattern_count_in_file {
#Find count and rules names of matched pattern
my $data = $_[0];
my $pattern = $_[1];
my @rules = ();
my @rules_cce = ();
my @rules_ids = ();
my $count = 0;
my @nlines;
my $j;
my $rule_name = "";
my $cce_id = "";
my @lines = split /\n|\r/, $data;
for my $i (0 .. $#lines) {
if ($lines[$i] =~ /$pattern/) {
$count++;
for ($j = 1; $j <= 5;) { # Looking in upper lines
if ($lines[$i - $j] =~ /Rule/) { # Found rule
$lines[$i - $j + 1] =~ s/\s+//g; # Remove whitespace from rule id
$rule_name = $lines[$i - $j + 1];
push(@rules_ids, $rule_name); # Push rule id to list
}
if ($lines[$i - $j] =~ /Ident/) { # Found CCE ID
$lines[$i - $j + 1] =~ s/\s+//g; # Remove whitespace from CCE id
$cce_id = $lines[$i - $j + 1];
push(@rules_cce, $cce_id); # Push rule id to list
}
$j++;
}
$rule_name .= ", " . $cce_id; # Add CCE ID to rule name
push(@rules, $rule_name); # Push rule id and rule cce id to list
$cce_id = "";
}
}
#Returning by reference array of matched rules
$_[2] = \@rules; # rule id and rule cce id
$_[3] = \@rules_cce; # cce IDs
$_[4] = \@rules_ids; # rule IDs
return $count;
}
sub modify_ds_ansible_files {
# Removes bash and ansible excluded and not having fixes rules from DS and playbook files
my $in_file_path = $_[0];
my $bash_pattern = "missing a bash fix";
my $ansible_pattern = "missing a ansible fix";
my $data;
my @bash_rules = ();
my @ansible_rules = ();
my $i = 0;
my $bash_fix_missing = "bash_fix_missing.txt";
my $ansible_fix_missing = "ansible_fix_missing.txt";
my $ds_unselect_rules_script = "ds_unselect_rules.sh";
$data = script_output("cat $in_file_path", quiet => 1);
my @lines = split /\n|\r/, $data;
# Find ansible and bash rules and write them to the list
for ($i = 0; $i <= $#lines;) { #(0 .. $#lines)
if ($lines[$i] =~ /$bash_pattern/) {
$i++;
until ($lines[$i] =~ /\*\*\*/) {
$lines[$i] =~ s/\s+|\r|\n//g; #remowe unneded symbols
push(@bash_rules, $lines[$i]);
$i++;
}
}
if ($lines[$i] =~ /$ansible_pattern/) {
$i++;
until ($lines[$i] =~ /\*\*\*/) {
$lines[$i] =~ s/\s+|\r|\n//g; #remowe unneded symbols
push(@ansible_rules, $lines[$i]);
$i++;
}
}
$i++;
}
record_info("Got rules from lists", "Got rules from lists from $in_file_path\nBash pattern:\n$bash_pattern\nAnsible pattern:\n $ansible_pattern");
if ($#bash_rules > 0 and $ansible_remediation == 0) {
record_info("Bash rules missing fix", "Bash rules missing fix:\n" . join "\n",
@bash_rules
);
}
if ($#ansible_rules > 0 and $ansible_remediation == 1) {
record_info("Ansible rules missing fix", "Ansible rules missing fix:\n" . join "\n",
@ansible_rules
);
}
if ($ansible_remediation == 1) {
my $ansible_f = join "\n", @ansible_rules;
# Write rules to file
assert_script_run("printf \"$ansible_f\" > \"$ansible_fix_missing\"");
my ($ansible_exclusions, $ansible_exclusions_diff);
# Get rule exclusions for ansible playbook
get_test_exclusions("openqa_tests_exclusions_base", $ansible_exclusions);
get_test_exclusions("openqa_tests_exclusions_diff", $ansible_exclusions_diff);
if (@$ansible_exclusions_diff > 0) { # if found SP specific exclusion
push(@$ansible_exclusions, @$ansible_exclusions_diff);
}
# Write exclusions to the file
if (@$ansible_exclusions > 0) {
my $exclusions = (join "\n", @$ansible_exclusions);
assert_script_run("printf \"\n$exclusions\" >> \"$ansible_fix_missing\"");
record_info("Writing ansible exceptions to file", "Writing ansible exclusions:\n$exclusions\n\nto file: $ansible_fix_missing");
}
# Diasble excluded and fix missing rules in ds file
my $unselect_cmd = "sh $compliance_as_code_path/tests/$ds_unselect_rules_script $f_ssg_sle_ds $ansible_fix_missing";
assert_script_run("$unselect_cmd", timeout => 600);
assert_script_run("rm $f_ssg_sle_ds");
assert_script_run("cp /tmp/$ssg_sle_ds $f_ssg_sle_ds");
record_info("Diasble excluded and fix missing rules in ds file", "Command $unselect_cmd");
uload_log_file($ansible_fix_missing);
# Generate new playbook without exclusions and fix_missing rules
my $playbook_gen_cmd = "oscap xccdf generate fix --profile $profile_ID --fix-type ansible $f_ssg_sle_ds > playbook.yml";
assert_script_run("$playbook_gen_cmd", timeout => 600);
record_info("Generated playbook", "Command $playbook_gen_cmd");
# Replace original paybook to generated one
assert_script_run("rm $full_ansible_file_path");
assert_script_run("cp playbook.yml $full_ansible_file_path");
record_info("Replaced playbook", "Replaced playbook $full_ansible_file_path with generated playbook.yml");
# Modify and backup ansible playbook
modify_ansible_playbook();
# Upload generated playbook for evidence
uload_log_file($full_ansible_file_path);
}
else {
my $bash_f = join "\n", @bash_rules;
# Write rules to file
assert_script_run("printf \"$bash_f\" > \"$bash_fix_missing\"");
my ($bash_exclusions, $bash_exclusions_diff);
# Get rule exclusions for bash playbook
get_test_exclusions("openqa_tests_exclusions_base", $bash_exclusions);
get_test_exclusions("openqa_tests_exclusions_diff", $bash_exclusions_diff);
if (@$bash_exclusions_diff > 0) { # if found SP specific exclusion
push(@$bash_exclusions, @$bash_exclusions_diff);
}
# Write exclusions to the file
if (@$bash_exclusions > 0) {
my $exclusions = (join "\n", @$bash_exclusions);
assert_script_run("printf \"\n$exclusions\" >> \"$bash_fix_missing\"");
record_info("Writing bash exceptions to file", "Writing bash exclusions:\n$exclusions\n\nto file: $bash_fix_missing");
}
# Diasble excluded and fix missing rules in ds file
my $unselect_cmd = "sh $compliance_as_code_path/tests/$ds_unselect_rules_script $f_ssg_sle_ds $bash_fix_missing";
assert_script_run("$unselect_cmd", timeout => 600);
assert_script_run("rm $f_ssg_sle_ds");
assert_script_run("cp /tmp/$ssg_sle_ds $f_ssg_sle_ds");
record_info("Diasble excluded and fix missing rules in ds file", "Command $unselect_cmd");
uload_log_file($bash_fix_missing);
}
uload_log_file($f_ssg_sle_ds);
my $output_full_path = script_output("pwd", quiet => 1);
$output_full_path =~ s/\r|\n//g;
my $bash_file_full_path = "$output_full_path/$bash_fix_missing";
my $ansible_file_full_path = "$output_full_path/$ansible_fix_missing";
record_info("Files paths for missing rules ", "Bash file path:\n$bash_file_full_path\nAnsible file path:\n $ansible_file_full_path");
# Return bash and ansible rules missing fix
$_[1] = \@bash_rules;
$_[2] = \@ansible_rules;
}
sub install_python311 {
# Install python 3.11 needed for script execution
# Ansible playbook still executed by python 3.6 because 3.11 breaks many rules
zypper_call("in python311 python311-rpm");
# Set sl for scap scripts
assert_script_run("ln -s python3.11 /usr/bin/python");
}
sub generate_missing_rules {
# Generate text file that contains rules that missing implimentation for profile
my $output_file = "missing_rules.txt";
# Installing python libs to be able to run profile_tool.py
zypper_call('in python311-Jinja2 python311-PyYAML python311-pytest python311-pytest-cov python311-setuptools', timeout => 180);
my $py_libs = "jinja2 ninja";
assert_script_run("pip3.11 install $py_libs", timeout => 600);
assert_script_run("cd $compliance_as_code_path");
assert_script_run("source .pyenv.sh");
# Running script that generates file containing rules missing fixes
my $cmd = "python build-scripts/profile_tool.py stats --missing --skip-stats --profile $profile_ID --benchmark $f_ssg_sle_xccdf --format plain > $output_file";
assert_script_run("$cmd");
record_info("Generated file $output_file", "generate_missing_rules Input file $f_ssg_sle_xccdf\n Command:\n$cmd");
assert_script_run("cp $output_file /root");
# Getting and showing profile statistics
my $data = script_output("cat $output_file", quiet => 1);
record_info("Profile missing stat", "Profile missing stat:\n $data");
#Uplaod file to logs
uload_log_file($output_file);
assert_script_run("cd /root");
my $output_full_path = script_output("pwd", quiet => 1);
$output_full_path =~ s/\r|\n//g;
$output_full_path .= "/$output_file";
return $output_full_path;
}
sub get_cac_code {
# Get the code for the ComplianceAsCode by cloning its repository
my $cac_dir = "src/content";
my $git_repo = "https://github.com/ComplianceAsCode/content.git";
my $git_clone_cmd = "git clone " . $git_repo . " $cac_dir";
zypper_call("in git-core");
assert_script_run("mkdir src");
assert_script_run("rm -r $cac_dir", quiet => 1) if (-e "$cac_dir");
assert_script_run('git config --global http.sslVerify false', quiet => 1);
assert_script_run("set -o pipefail ; $git_clone_cmd", timeout => 600, quiet => 1);
$compliance_as_code_path = script_output("pwd", quiet => 1);
$compliance_as_code_path =~ s/\r|\n//g;
$compliance_as_code_path .= "/$cac_dir";
record_info("Cloned ComplianceAsCode", "Cloned repo $git_repo to folder: $compliance_as_code_path");
# In case of use CaC master as source - building content
if ($use_content_type == 3) {
zypper_call('in cmake libxslt-tools python311-lxml python311-pytest python311-sphinx_rtd_theme python311-prometheus_client python311-Jinja2 python311-pytest-cov', timeout => 180);
my $py_libs = "json2html sphinxcontrib-jinjadomain autojinja myst_parser mypy openpyxl pcre2 cmakelint sphinx";
# On s390x pip requires packages to build modules
if (is_s390x) {
zypper_call('in ninja clang15 libxslt-devel libxml2-devel python311-devel', timeout => 180);
assert_script_run("pip3.11 install $py_libs", timeout => 600);
}
else {
assert_script_run("pip3.11 install $py_libs pandas", timeout => 600);
}
# Building CaC content
assert_script_run("cd $compliance_as_code_path");
# Set python to version 3.11
my $python_ver_fix_cmd = "sed -i \'s/Python_ADDITIONAL_VERSIONS 3/Python_ADDITIONAL_VERSIONS 3.11/g\' CMakeLists.txt";
assert_script_run("$python_ver_fix_cmd");
assert_script_run("sh build_product $sle_version", timeout => 9000);
record_info("build_product", "sh build_product $sle_version");
assert_script_run("cd /root");
}
return $compliance_as_code_path;
}
=comment
OSCAP exit codes from https://github.com/OpenSCAP/openscap/blob/maint-1.3/utils/oscap-tool.h
// standard oscap CLI exit statuses
enum oscap_exitcode {
OSCAP_OK = 0, // successful exit
OSCAP_ERROR = 1, // an error occurred
OSCAP_FAIL = 2, // a process (e.g. scan or validation) failed
OSCAP_ERR_FETCH = 1, // cold not fetch input file (same as error for now)
OSCAP_BADARGS = 100, // bad commandline arguments
OSCAP_BADMODULE = 101, // unrecognized module
OSCAP_UNIMPL_MOD = 110, // module functionality not implemented
OSCAP_UNIMPL = 111, // functionality not implemented
// end of list
OSCAP_EXITCODES_END_ = 120 // any code returned shall not be higher than this
};
=cut
sub get_tests_config {
# Get the tests configuration file from repository
my $config_file_name = "openqa_config.conf";
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/content/";
download_file_from_https_repo($url, $config_file_name);
my $config_file_path = script_output("pwd", quiet => 1);
$config_file_path =~ s/\r|\n//g;
$config_file_path .= "/$config_file_name";
my $data = script_output("cat $config_file_path", quiet => 1);
my $config = Config::Tiny->new;
$config = Config::Tiny->read_string("$data");
my $err = Config::Tiny::errstr;
if ($err eq "") {
# Configuration can be overridden by OpenQA variables
$use_content_type = (get_var('OSCAP_USE_CONTENT_TYPE', '') eq '' ? $config->{tests_config}->{use_content_type} : get_required_var('OSCAP_USE_CONTENT_TYPE'));
$remove_rules_missing_fixes = (get_var('OSCAP_REMOVE_RULES_MISSING_FIXES', '') eq '' ? $config->{tests_config}->{remove_rules_missing_fixes} : get_required_var('OSCAP_REMOVE_RULES_MISSING_FIXES'));
$use_exclusions = (get_var('OSCAP_USE_EXCLUSIONS', '') eq '' ? $config->{tests_config}->{use_content_type} : get_required_var('OSCAP_USE_EXCLUSIONS'));
record_info("Set test configuration", "Set test configuration from file $config_file_path\n use_content_type = $use_content_type\n remove_rules_missing_fixes = $remove_rules_missing_fixes\n use_exclusions = $use_exclusions");
}
else {
record_info("Tiny->read error", "Config::Tiny->read( $config_file_path )returned error:\n$err");
}
return $config_file_path;
}
sub get_test_expected_results {
# Get expected results from remote file
my $file_name = $_[0];
my $eval_match = ();
my $type = "";
my $arch = "";
if ($ansible_remediation == 1) {
$type = 'ansible';
}
else {
$type = 'bash';
}
if (is_s390x) { $arch = "s390x"; }
if (is_aarch64 or is_arm) { $arch = "aarch64"; }
if (is_ppc64le) { $arch = "ppc"; }
if (is_x86_64) { $arch = "x86_64"; }
my $version = get_var('VERSION');
my $sles_sp = (split('-', $version))[1];
my $exp_fail_list_name = $sle_version . "-exp_fail_list";
my $expected_results_file_name = $file_name . "_" . $benchmark_version . ".yaml";
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/content/";
my @eval_match = ();
my $return = download_file_from_https_repo($url, $expected_results_file_name);
if ($return == 1) {
record_info("Downloded results", "Downloded expected results for benchmark version $benchmark_version");
}
# In case if expected_results are not defined for specific benchmark_version
else {
$expected_results_file_name = "$file_name.yaml";
$return = download_file_from_https_repo($url, $expected_results_file_name);
}
if ($return == 1) {
uload_log_file($expected_results_file_name);
my $data = script_output("cat $expected_results_file_name", quiet => 1);
# Phrase the expected results
my $expected_results = YAML::PP::Load($data);
record_info("Looking expected results", "Looking expected results for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exp_fail_list_name\nService Pack: $sles_sp");
if ($expected_results_file_name =~ /base/) {
$eval_match = $expected_results->{$profile_ID}->{$type}->{$arch}->{$exp_fail_list_name};
}
else {
$eval_match = $expected_results->{$profile_ID}->{$type}->{$arch}->{$exp_fail_list_name}->{$sles_sp};
}
if (defined $eval_match) {
@eval_match = @$eval_match;
record_info("Got expected results", "Got expected results for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exp_fail_list_name\nService Pack: $sles_sp\nBenchmark: $benchmark_version\nList of expected to fail rules:\n" . (join "\n", @eval_match));
}
else {
record_info("No expected results", "Expected results are not defined.");
}
}
else {
record_info("No file for expected results", "Not able to download file with expected results.\nExpected results are not defined.");
}
$_[1] = \@eval_match;
return 1;
}
sub get_test_exclusions {
# Get exclusions from remote file
my $file_name = $_[0];
my $exclusions = ();
my $found = -1;
my $type = "";
my $arch = "";
my $return = -1;
# If set in configuration to not use excusions
if ($use_exclusions == 0) {
return $found;
}
else {
if ($ansible_remediation == 1) {
$type = 'ansible';
}
else {
$type = 'bash';
}
if (is_s390x) { $arch = "s390x"; }
if (is_aarch64 or is_arm) { $arch = "aarch64"; }
if (is_ppc64le) { $arch = "ppc"; }
if (is_x86_64) { $arch = "x86_64"; }
my $version = get_var('VERSION');
my $sles_sp = (split('-', $version))[1];
my $exclusions_list_name = $sle_version . "-exclusions_list";
my $exclusions_file_name = $file_name . "_" . $benchmark_version . ".yaml";
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/content/";
my @exclusions = ();
$return = download_file_from_https_repo($url, $exclusions_file_name);
if ($return == 1) {
record_info("Downloded exclusions", "Downloded exclusions for benchmark version $benchmark_version");
}
# In case if exclusions are not defined for specific benchmark_version
else {
$exclusions_file_name = "$file_name.yaml";
$return = download_file_from_https_repo($url, $exclusions_file_name);
}
if ($return == 1) {
uload_log_file($exclusions_file_name);
my $data = script_output("cat $exclusions_file_name", quiet => 1);
# Phrase the expected results
my $exclusions_data = YAML::PP::Load($data);
record_info("Looking exclusions", "Looking exclusions for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exclusions_list_name\nService Pack: $sles_sp");
if ($exclusions_file_name =~ /base/) {
$exclusions = $exclusions_data->{$profile_ID}->{$type}->{$arch}->{$exclusions_list_name};
}
else {
$exclusions = $exclusions_data->{$profile_ID}->{$type}->{$arch}->{$exclusions_list_name}->{$sles_sp};
}
# If results defined
if (defined $exclusions) {
@exclusions = @$exclusions;
$found = 1;
record_info("Got exclusions", "Got exclusions for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exclusions_list_name\nService Pack: $sles_sp\nBenchmark: $benchmark_version\nList of excluded rules:\n" . (join "\n", @exclusions));
}
else {
record_info("No exclusions", "Exclusions are not defined.");
}
}
else {
record_info("No file for exclusions", "Not able to download file with exclusions.\nExclusions are not defined.");
}
$_[1] = \@exclusions;
return $found;
}
}
sub oscap_security_guide_setup {
# Main test setup function
$full_ansible_file_path = $ansible_file_path . $ansible_profile_ID;
record_info("$profile_ID", "Profile $profile_ID");
if ($ansible_remediation == 0) {
record_info("BASH", "BASH remediation used");
}
else {
record_info("Ansible", "Ansible remediation used");
}
zypper_call('ref -s', timeout => 180);
zypper_call('in openscap-utils scap-security-guide p7zip', timeout => 180);
set_ds_file();
$f_ssg_ds = is_sle ? $f_ssg_sle_ds : $f_ssg_tw_ds;
display_oscap_information();
# Get the tests configuration file from repository and set global configuration variables
get_tests_config();
push(@test_run_report, "[configuration]");
my $out = script_output("date", quiet => 1);
push(@test_run_report, "date = $out");
$out = "";
push(@test_run_report, "profile_ID = $profile_ID");
push(@test_run_report, "ansible_profile_file_name = $ansible_profile_ID");
push(@test_run_report, "use_content_type = $use_content_type");
push(@test_run_report, "remove_rules_missing_fixes = $remove_rules_missing_fixes");
push(@test_run_report, "use_exclusions = $use_exclusions");
push(@test_run_report, "evaluate_count = $evaluate_count");
# Replace original ds and xccdf files whith downloaded from local repository
set_ds_file_name();
push(@test_run_report, "sle_version = $sle_version");
push(@test_run_report, "ssg_sle_ds = $ssg_sle_ds");
push(@test_run_report, "ssg_sle_xccdf = $ssg_sle_xccdf");
my $arch = get_var 'ARCH';
push(@test_run_report, "os_arch = $arch");
my $type;
if ($ansible_remediation == 1) {
$type = 'ansible';
}
else {
$type = 'bash';
}
push(@test_run_report, "remediation_type = $type");
my $build_url = get_var 'CASEDIR';
push(@test_run_report, "build_url = $build_url");
my $iso_name = get_var 'ISO';
push(@test_run_report, "iso_name = $iso_name");
my $full_name = get_var 'NAME';
push(@test_run_report, "full_name = $full_name");
my $schedule = get_var 'YAML_SCHEDULE';
push(@test_run_report, "schedule = $schedule");
unless (is_opensuse) {
# Some packages require PackageHub repo is available
return unless is_phub_ready();
add_suseconnect_product(get_addon_fullname('phub'));
# Need to use pyython3.1x
add_suseconnect_product(get_addon_fullname('python3'));
# On SLES 12 ansible packages require dependencies located in sle-module-public-cloud
add_suseconnect_product(get_addon_fullname('pcm'), (is_sle('<15') ? '12' : undef)) if is_sle;
install_python311();
}
# If required ansible remediation