| 1 | #!/usr/bin/env bash |
| 2 | . $builddir/tests/test_common.sh |
| 3 | |
| 4 | set -e |
| 5 | set -o pipefail |
| 6 | |
| 7 | 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=$(basename $0 .sh) |
| 23 | results_arf=$(mktemp -t ${name}.arf.XXXXXX) |
| 24 | stdout=$(mktemp -t ${name}.out.XXXXXX) |
| 25 | stderr=$(mktemp -t ${name}.err.XXXXXX) |
| 26 | script=$(mktemp -t ${name}.sh.XXXXXX) |
| 27 | playbook=$(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 || [ $? == 2 ] |
| 31 | [ -f $stdout ]; [ -s $stdout ]; rm $stdout |
| 32 | [ -f $stderr ]; [ ! -s $stderr ]; 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 | [ -f $stdout ]; [ ! -s $stdout ]; rm $stdout |
| 37 | [ -f $stderr ]; [ ! -s $stderr ]; rm $stderr |
| 38 | grep -q "$bash_line1" $script |
| 39 | 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 | grep -Ev $regex >$playbook 2>$stderr |
| 43 | diff -B $playbook $srcdir/$name.playbook1.yml |
| 44 | [ -f $stderr ]; [ ! -s $stderr ]; rm $stderr |
| 45 | grep -q "$ansible_task1a" $playbook |
| 46 | grep -q "$ansible_task1b" $playbook |
| 47 | grep -q "$ansible_task2a" $playbook |
| 48 | 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 | [ -f $stdout ]; [ ! -s $stdout ]; rm $stdout |
| 53 | [ -f $stderr ]; [ ! -s $stderr ]; rm $stderr |
| 54 | grep -q -v "$bash_line1" $script |
| 55 | 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 | grep -Ev $regex >$playbook 2>$stderr |
| 59 | diff -B $playbook $srcdir/$name.playbook2.yml |
| 60 | [ -f $stderr ]; [ ! -s $stderr ]; rm $stderr |
| 61 | grep -q -v "$ansible_task1a" $playbook |
| 62 | grep -q -v "$ansible_task1b" $playbook |
| 63 | grep -q "$ansible_task2a" $playbook |
| 64 | grep -q "$ansible_task2b" $playbook |
| 65 | |
| 66 | rm $results_arf |
| 67 | rm $script |
| 68 | rm $playbook |
| 69 | |
| 70 | rm not_executable |