From 092998237ea74abd5816b603d6a936049c9548fe Mon Sep 17 00:00:00 2001 From: Petr Lautrbach Date: Mar 28 2020 15:22:55 +0000 Subject: [PATCH 1/2] framework: Rewrite SetroubleshootFixit.py to use pydbus pydbus is considered to be a modern Python DBUS library and it's already used in other tools/services in framework. This change will allow us to drop unnecessary dependency on Python slip package. --- diff --git a/framework/src/SetroubleshootFixit.py b/framework/src/SetroubleshootFixit.py index 15c6cab..e2d3413 100644 --- a/framework/src/SetroubleshootFixit.py +++ b/framework/src/SetroubleshootFixit.py @@ -1,31 +1,64 @@ #!/usr/bin/python3 -import dbus -import dbus.service -import dbus.mainloop.glib +# Authors: Petr Lautrbach +# +# Copyright (C) 2020 Red Hat, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + from gi.repository import GLib -import slip.dbus.service -from slip.dbus import polkit +from pydbus import SystemBus + import os +import signal +import subprocess +class RunFix(object): + """ + + + + + + + + + + """ -class RunFix(slip.dbus.service.Object): - default_polkit_auth_required = "org.fedoraproject.setroubleshootfixit.write" + def __init__(self, timeout=10): + self.timeout = timeout + self.alarm(self.timeout) - def __init__(self, *p, **k): - super(RunFix, self).__init__(*p, **k) + def alarm(self, timeout=10): + signal.alarm(timeout) - @dbus.service.method("org.fedoraproject.SetroubleshootFixit", in_signature='ss', out_signature='s') - def run_fix(self, local_id, analysis_id): - import subprocess - command = ["sealert", "-f", local_id, "-P", analysis_id] - return subprocess.check_output(command, universal_newlines=True) + def run_fix(self, local_id, analysis_id, dbus_context): + self.alarm(0) + result = "" + + if dbus_context.is_authorized('org.fedoraproject.setroubleshootfixit.write', None, interactive=True): + command = ["sealert", "-f", local_id, "-P", analysis_id] + result = subprocess.check_output(command, universal_newlines=True) + + self.alarm(self.timeout) + return result if __name__ == "__main__": - mainloop = GLib.MainLoop() - dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) - system_bus = dbus.SystemBus() - name = dbus.service.BusName("org.fedoraproject.SetroubleshootFixit", system_bus) - object = RunFix(system_bus, "/org/fedoraproject/SetroubleshootFixit/object") - slip.dbus.service.set_mainloop(mainloop) - mainloop.run() + bus = SystemBus() + bus.publish("org.fedoraproject.SetroubleshootFixit", ("object", RunFix())) + + loop = GLib.MainLoop() + loop.run() From 532a26ce98b2dfdf901652264730e03eafc5d8f6 Mon Sep 17 00:00:00 2001 From: Petr Lautrbach Date: Mar 28 2020 15:56:40 +0000 Subject: [PATCH 2/2] framework: Improve status reporting in run_fix() run_fix() DBUS API returns the following string now: $ sealert -f -P or "Authorization failed" is polkit authorization fails --- diff --git a/framework/src/SetroubleshootFixit.py b/framework/src/SetroubleshootFixit.py index e2d3413..a36fc79 100644 --- a/framework/src/SetroubleshootFixit.py +++ b/framework/src/SetroubleshootFixit.py @@ -47,11 +47,14 @@ class RunFix(object): def run_fix(self, local_id, analysis_id, dbus_context): self.alarm(0) - result = "" + + command = ["sealert", "-f", local_id, "-P", analysis_id] + result = "$ {}\n\n".format(" ".join(command)) if dbus_context.is_authorized('org.fedoraproject.setroubleshootfixit.write', None, interactive=True): - command = ["sealert", "-f", local_id, "-P", analysis_id] - result = subprocess.check_output(command, universal_newlines=True) + result += subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True) + else: + result += "Authorization failed" self.alarm(self.timeout) return result