Skip to content

Commit a044008

Browse files
authored
Add some basic SELinux facts & additional fact documentation (#648)
* Add some basic SELinux facts * Add documentation for requires_command and default functions for facts * Improve example docstring for SEBoolean fact * Add required command for SEboolean * Remove lint * Remove lint
1 parent f5ad1f7 commit a044008

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

docs/api/facts.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ Writing Facts
55
and a ``process`` function. The command is executed on the target host and the output
66
passed (as a ``list`` of lines) to the ``process`` handler to generate fact data. Facts can output anything, normally a ``list`` or ``dict``.
77

8+
Fact classes may provide a ``default`` function that takes no arguments (except ``self``). The return value of this function is used if an error
9+
occurs during fact collection. Additionally, a ``requires_command`` variable can be set on the fact that specifies a command that must be available
10+
on the host to collect the fact. If this command is not present on the hos the fact will be set to the default, or empty if no ``default`` function
11+
is available.
12+
813
Importing & Using Facts
914
~~~~~~~~~~~~~~~~~~~~~~~
1015

pyinfra/facts/selinux.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from pyinfra.api import FactBase
2+
3+
4+
class FileContext(FactBase):
5+
'''
6+
Returns structured SELinux file context data for a specified file.
7+
8+
.. code:: python
9+
{
10+
'user': 'system_u',
11+
'role': 'object_r',
12+
'type': 'deafult_t',
13+
'level': 's0',
14+
}
15+
'''
16+
17+
def command(self, path):
18+
return 'stat -c %C {0} || exit 0'.format(path)
19+
20+
def process(self, output):
21+
context = {}
22+
components = output[0].split(':')
23+
context['user'] = components[0]
24+
context['role'] = components[1]
25+
context['type'] = components[2]
26+
context['level'] = components[3]
27+
return context
28+
29+
30+
class SEBoolean(FactBase):
31+
'''
32+
Returns the on/off status of a SELinux Boolean.
33+
34+
.. code:: python
35+
host.get_fact(SEBoolean, "httpd_can_network_connect") -> "off"
36+
'''
37+
requires_command = 'getsebool'
38+
39+
def command(self, boolean):
40+
return 'getsebool {0}'.format(boolean)
41+
42+
def process(self, output):
43+
components = output[0].split(' --> ')
44+
return components[1]

0 commit comments

Comments
 (0)