| 1 |
- name: collect selinux facts |
| 2 |
setup: |
| 3 |
gather_subset: ['!all', '!min', selinux] |
| 4 |
register: fact_output |
| 5 |
|
| 6 |
- debug: |
| 7 |
var: fact_output |
| 8 |
|
| 9 |
- name: create tempdir container in home |
| 10 |
file: |
| 11 |
path: ~/.selinux_tmp |
| 12 |
state: directory |
| 13 |
|
| 14 |
- name: create tempdir |
| 15 |
tempfile: |
| 16 |
path: ~/.selinux_tmp |
| 17 |
prefix: selinux_test |
| 18 |
state: directory |
| 19 |
register: tempdir |
| 20 |
|
| 21 |
- name: ls -1Zd tempdir to capture context from FS |
| 22 |
shell: ls -1Zd '{{ tempdir.path }}' |
| 23 |
register: tempdir_context_output |
| 24 |
|
| 25 |
- name: create a file under the tempdir with no context info specified (it should inherit parent context) |
| 26 |
file: |
| 27 |
path: '{{ tempdir.path }}/file_inherited_context' |
| 28 |
state: touch |
| 29 |
register: file_inherited_context |
| 30 |
|
| 31 |
- name: ls -1Z inherited file to capture context from FS |
| 32 |
shell: ls -1Z '{{ tempdir.path }}/file_inherited_context' |
| 33 |
register: inherited_context_output |
| 34 |
|
| 35 |
- name: copy the file with explicit overrides on all context values |
| 36 |
copy: |
| 37 |
remote_src: yes |
| 38 |
src: '{{ tempdir.path }}/file_inherited_context' |
| 39 |
dest: '{{ tempdir.path }}/file_explicit_context' |
| 40 |
seuser: system_u |
| 41 |
serole: system_r |
| 42 |
setype: user_tmp_t |
| 43 |
# default configs don't have MLS levels defined, so we can't test that yet |
| 44 |
# selevel: s1 |
| 45 |
register: file_explicit_context |
| 46 |
|
| 47 |
- name: ls -1Z explicit file to capture context from FS |
| 48 |
shell: ls -1Z '{{ tempdir.path }}/file_explicit_context' |
| 49 |
register: explicit_context_output |
| 50 |
|
| 51 |
- name: alter the tempdir context |
| 52 |
file: |
| 53 |
path: '{{ tempdir.path }}' |
| 54 |
seuser: system_u |
| 55 |
serole: system_r |
| 56 |
setype: user_tmp_t |
| 57 |
# default configs don't have MLS levels defined, so we can't test that yet |
| 58 |
# selevel: s1 |
| 59 |
register: tempdir_altered |
| 60 |
|
| 61 |
- name: ls -1Z tempdir to capture context from FS |
| 62 |
shell: ls -1Z '{{ tempdir.path }}/file_explicit_context' |
| 63 |
register: tempdir_altered_context_output |
| 64 |
|
| 65 |
- name: copy the explicit context file with default overrides on all context values |
| 66 |
copy: |
| 67 |
remote_src: yes |
| 68 |
src: '{{ tempdir.path }}/file_explicit_context' |
| 69 |
dest: '{{ tempdir.path }}/file_default_context' |
| 70 |
seuser: _default |
| 71 |
serole: _default |
| 72 |
setype: _default |
| 73 |
selevel: _default |
| 74 |
register: file_default_context |
| 75 |
|
| 76 |
- name: see what matchpathcon thinks the context of default_file_context should be |
| 77 |
shell: matchpathcon {{ file_default_context.dest }} | awk '{ print $2 }' |
| 78 |
register: expected_default_context |
| 79 |
|
| 80 |
- assert: |
| 81 |
that: |
| 82 |
- fact_output.ansible_facts.ansible_selinux.config_mode in ['enforcing','permissive'] |
| 83 |
- fact_output.ansible_facts.ansible_selinux.mode in ['enforcing','permissive'] |
| 84 |
- fact_output.ansible_facts.ansible_selinux.status == 'enabled' |
| 85 |
- fact_output.ansible_facts.ansible_selinux_python_present == true |
| 86 |
# assert that secontext is set on the file results (injected by basic.py, for better or worse) |
| 87 |
- tempdir.secontext is match('.+:.+:.+') and tempdir.secontext in tempdir_context_output.stdout |
| 88 |
- file_inherited_context.secontext is match('.+:.+:.+') and file_inherited_context.secontext in inherited_context_output.stdout |
| 89 |
- file_inherited_context.secontext == tempdir.secontext # should've been inherited from the parent dir since not set explicitly |
| 90 |
- file_explicit_context.secontext == 'system_u:system_r:user_tmp_t:s0' and file_explicit_context.secontext in explicit_context_output.stdout |
| 91 |
- tempdir_altered.secontext == 'system_u:system_r:user_tmp_t:s0' and tempdir_altered.secontext in tempdir_altered_context_output.stdout |
| 92 |
# the one with reset defaults should match the original tempdir context, not the altered one (ie, it was set by the original policy context, not inherited from the parent dir) |
| 93 |
- file_default_context.secontext == expected_default_context.stdout_lines[0] |