Handle gracefully settings that are not functional
Background
As reported in https://bugs.kali.org/view.php?id=8024, kali-tweaks crashed because of a regression in samba, and more precisely the testparms command.
#################################################################################
$ kali-tweaks # followed by selecting Hardening
Traceback (most recent call last):
File "/usr/bin/kali-tweaks", line 33, in <module>
sys.exit(load_entry_point('kali-tweaks==2022.4.0', 'console_scripts', 'kali-tweaks')())
File "/usr/lib/python3/dist-packages/kali_tweaks/__main__.py", line 1108, in main
do_main_screen()
File "/usr/lib/python3/dist-packages/kali_tweaks/__main__.py", line 1099, in do_main_screen
ret = func()
File "/usr/lib/python3/dist-packages/kali_tweaks/__main__.py", line 870, in do_hardening_screen
config = SambaSetting().load()
File "/usr/lib/python3/dist-packages/kali_tweaks/settings/samba.py", line 24, in load
value = self.get_client_min_protocol()
File "/usr/lib/python3/dist-packages/kali_tweaks/settings/samba.py", line 45, in get_client_min_protocol
res = run(cmd)
File "/usr/lib/python3/dist-packages/kali_tweaks/utils.py", line 121, in run
return _run(cmd, **kwargs)
File "/usr/lib/python3/dist-packages/kali_tweaks/utils.py", line 112, in _run
res = subprocess.run(cmd, capture_output=(not interactive), shell=True, text=True)
File "/usr/lib/python3.10/subprocess.py", line 503, in run
stdout, stderr = process.communicate(input, timeout=timeout)
File "/usr/lib/python3.10/subprocess.py", line 1152, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.10/subprocess.py", line 2045, in _communicate
stderr = self._translate_newlines(stderr,
File "/usr/lib/python3.10/subprocess.py", line 1029, in _translate_newlines
data = data.decode(encoding, errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 28: invalid continuation byte
#################################################################################
$ testparm /etc/samba/smb.conf --suppress-prompt
Load smb config files from �R/�U
Error loading services.
#################################################################################
The problem is that testparms got broken by an update of one of its dependencies, libpopt. In turns, it breaks kali-tweaks in a surprising way: testparms outputs garbage on the terminal, and it's invalid UTF-8.
What should kali-tweaks do about that? I don't think that handling this particular error will get us anywhere. But I think that handling unexpected errors in general, and per setting, and disabling a setting that fails, would be a useful improvement. Kali-tweaks could just grey out the setting, output some error message on the terminal (to help users report the issue), and keep going. I prefer kali-tweaks to be functional and missing a setting, rather than crashing miserably.
Way forward
I don't think the code, in its current state, is ready for this improvement. It supposes that every setting is well isolated, and that we can add code that handles unexpected errors in one place. In the current state of thing, I'm afraid I would have to copy/paste this code for every setting.
So, let's keep that in mind, and as kali-tweaks internals mature and improve, finally we'll get there.