| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | . $builddir/tests/test_common.sh |
| 3 | |
| 4 | set -e |
| 5 | set -o pipefail |
| 6 | |
| 7 | touchscip-shell system . . touch. not_executable |
| 8 | |
| 9 | profile="xccdf_moc.elpmaxe.www_profile_standard" |
| 10 | result_id="xccdf_org.open-scap_testresult_xccdf_moc.elpmaxe.www_profile_standard" |
| 11 | bash_line1="echo this_is_ok" |
| 12 | bash_line2="echo fix_me_please" |
| 13 | ansible_task1a="\- name: ensure everything passes" |
| 14 | ansible_task1b="shell: /bin/true" |
| 15 | ansible_task2a="\- name: correct the failing case" |
| 16 | ansible_task2b="shell: /bin/false" |
| 17 | |
| 18 | # Regex matches for script header comments, including: line of #, #\n, # Example Comment |
| 19 | # Avoids matching # - hosts: and required ansible header elements |
| 20 | regex="^$|[\#]$|^([\#][[:blank:]][^\-])" |
| 21 | |
| 22 | name=$(basenamescip-shell system . . basename. $0 .sh) |
| 23 | results_arf=$(mktempscip-shell system . . mktemp. -t ${name}.arf.XXXXXX) |
| 24 | stdout=$(mktempscip-shell system . . mktemp. -t ${name}.out.XXXXXX) |
| 25 | stderr=$(mktempscip-shell system . . mktemp. -t ${name}.err.XXXXXX) |
| 26 | script=$(mktempscip-shell system . . mktemp. -t ${name}.sh.XXXXXX) |
| 27 | playbook=$(mktempscip-shell system . . mktemp. -t ${name}.yml.XXXXXX) |
| 28 | |
| 29 | # Create an ARF |
| 30 | $OSCAP xccdf eval --profile $profile --results-arf $results_arf $srcdir/$name.xccdf.xml >$stdout 2>$stderr || [scip-shell system . . `[`. $? == 2 ] |
| 31 | [scip-shell system . . `[`. -f $stdout ]; [scip-shell system . . `[`. -s $stdout ]; rmscip-shell system . . rm. $stdout |
| 32 | [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; rmscip-shell system . . rm. $stderr |
| 33 | |
| 34 | # Generate a bash script from a profile in ARF file |
| 35 | $OSCAP xccdf generate fix --profile $profile --output $script $results_arf >$stdout 2>$stderr |
| 36 | [scip-shell system . . `[`. -f $stdout ]; [scip-shell system . . `[`. ! -s $stdout ]; rmscip-shell system . . rm. $stdout |
| 37 | [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; rmscip-shell system . . rm. $stderr |
| 38 | grepscip-shell system . . grep. -q "$bash_line1" $script |
| 39 | grepscip-shell system . . grep. -q "$bash_line2" $script |
| 40 | |
| 41 | # Generate an Ansible playbook from a profile in ARF file |
| 42 | $OSCAP xccdf generate fix --profile $profile --fix-type ansible $results_arf | grepscip-shell system . . grep. -Ev $regex >$playbook 2>$stderr |
| 43 | diffscip-shell system . . diff. -B $playbook $srcdir/$name.playbook1.yml |
| 44 | [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; rmscip-shell system . . rm. $stderr |
| 45 | grepscip-shell system . . grep. -q "$ansible_task1a" $playbook |
| 46 | grepscip-shell system . . grep. -q "$ansible_task1b" $playbook |
| 47 | grepscip-shell system . . grep. -q "$ansible_task2a" $playbook |
| 48 | grepscip-shell system . . grep. -q "$ansible_task2b" $playbook |
| 49 | |
| 50 | # Generate a bash script based on scan results stored in ARF file |
| 51 | $OSCAP xccdf generate fix --result-id $result_id --output $script $results_arf >$stdout 2>$stderr |
| 52 | [scip-shell system . . `[`. -f $stdout ]; [scip-shell system . . `[`. ! -s $stdout ]; rmscip-shell system . . rm. $stdout |
| 53 | [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; rmscip-shell system . . rm. $stderr |
| 54 | grepscip-shell system . . grep. -q -v "$bash_line1" $script |
| 55 | grepscip-shell system . . grep. -q "$bash_line2" $script |
| 56 | |
| 57 | # Generate an Ansible playbook based on scan results stored in ARF file |
| 58 | $OSCAP xccdf generate fix --result-id $result_id --fix-type ansible $results_arf | grepscip-shell system . . grep. -Ev $regex >$playbook 2>$stderr |
| 59 | diffscip-shell system . . diff. -B $playbook $srcdir/$name.playbook2.yml |
| 60 | [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; rmscip-shell system . . rm. $stderr |
| 61 | grepscip-shell system . . grep. -q -v "$ansible_task1a" $playbook |
| 62 | grepscip-shell system . . grep. -q -v "$ansible_task1b" $playbook |
| 63 | grepscip-shell system . . grep. -q "$ansible_task2a" $playbook |
| 64 | grepscip-shell system . . grep. -q "$ansible_task2b" $playbook |
| 65 | |
| 66 | rmscip-shell system . . rm. $results_arf |
| 67 | rmscip-shell system . . rm. $script |
| 68 | rmscip-shell system . . rm. $playbook |
| 69 | |
| 70 | rmscip-shell system . . rm. not_executable |