Debian Codegraph @

lib/ansible/module_utils/facts/system/caps.py

raw
1 # Collect facts related to systems 'capabilities' via capsh
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import typing as t
21
22 from ansible.module_utils.facts.collector import BaseFactCollector
23
24
25 class SystemCapabilitiesFactCollector(BaseFactCollector):
26 name = 'caps'
27 _fact_ids = set(['system_capabilities',
28 'system_capabilities_enforced']) # type: t.Set[str]
29
30 def collect(self, module=None, collected_facts=None):
31
32 rc = -1
33 facts_dict = {'system_capabilities_enforced': 'N/A',
34 'system_capabilities': 'N/A'}
35 if module:
36 capsh_path = module.get_bin_path('capsh')
37 if capsh_path:
38 # NOTE: -> get_caps_data()/parse_caps_data() for easier mocking -akl
39 try:
40 rc, out, err = module.run_command([capsh_path, "--print"], errors='surrogate_then_replace', handle_exceptions=False)
41 except OSError as ex:
42 module.error_as_warning('Could not query system capabilities.', exception=ex)
43
44 if rc == 0:
45 enforced_caps = []
46 enforced = 'NA'
47 for line in out.splitlines():
48 if len(line) < 1:
49 continue
50 if line.startswith('Current:'):
51 if line.split(':')[1].strip() == '=ep':
52 enforced = 'False'
53 else:
54 enforced = 'True'
55 enforced_caps = [i.strip() for i in line.split('=')[1].split(',')]
56
57 facts_dict['system_capabilities_enforced'] = enforced
58 facts_dict['system_capabilities'] = enforced_caps
59
60 return facts_dict