| 1 |
/* |
| 2 |
* Copyright (C) 2016 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
|
| 8 |
const _ = cockpit.gettext; |
| 9 |
|
| 10 |
export const client = {}; |
| 11 |
|
| 12 |
const busName = "org.fedoraproject.Setroubleshootd"; |
| 13 |
const dbusInterface = "org.fedoraproject.SetroubleshootdIface"; |
| 14 |
const dbusPath = "/org/fedoraproject/Setroubleshootd"; |
| 15 |
|
| 16 |
const busNameFixit = "org.fedoraproject.SetroubleshootFixit"; |
| 17 |
const dbusInterfaceFixit = busNameFixit; |
| 18 |
const dbusPathFixit = "/org/fedoraproject/SetroubleshootFixit/object"; |
| 19 |
|
| 20 |
client.init = function() { |
| 21 |
client.connected = false; |
| 22 |
const dbusClientSeTroubleshoot = cockpit.dbus(busName, { superuser: "try" }); |
| 23 |
client.proxy = dbusClientSeTroubleshoot.proxy(dbusInterface, dbusPath); |
| 24 |
|
| 25 |
client.proxyFixit = cockpit.dbus(busNameFixit, { superuser: "try" }).proxy(dbusInterfaceFixit, dbusPathFixit); |
| 26 |
|
| 27 |
const connectPromise = new Promise((resolve, reject) => { |
| 28 |
client.proxy.wait(() => { |
| 29 |
// HACK setroubleshootd seems to drop connections if we don't start explicitly |
| 30 |
client.proxy.call("start", []) |
| 31 |
.then(() => { |
| 32 |
client.connected = true; |
| 33 |
resolve(); |
| 34 |
}) |
| 35 |
.catch(ex => reject(new Error(_("Unable to start setroubleshootd")))); |
| 36 |
}); |
| 37 |
}); |
| 38 |
|
| 39 |
client.alertCallback = null; |
| 40 |
|
| 41 |
function handleSignal(event, name, args) { |
| 42 |
if (client.alertCallback && name == "alert") { |
| 43 |
const level = args[0]; |
| 44 |
const localId = args[1]; |
| 45 |
client.alertCallback(level, localId); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// register to receive calls whenever a new alert becomes available |
| 50 |
// signature for the alert callback: (level, localId) |
| 51 |
client.handleAlert = function(callback) { |
| 52 |
// if we didn't listen to events before, do so now |
| 53 |
if (!client.alertCallback) { |
| 54 |
client.proxy.addEventListener("signal", handleSignal); |
| 55 |
} |
| 56 |
client.alertCallback = callback; |
| 57 |
}; |
| 58 |
|
| 59 |
// returns a promise |
| 60 |
client.getAlerts = function(since) { |
| 61 |
return new Promise((resolve, reject) => { |
| 62 |
let call; |
| 63 |
if (since !== undefined) |
| 64 |
call = client.proxy.call("get_all_alerts_since", [since]); |
| 65 |
else |
| 66 |
call = client.proxy.call("get_all_alerts", []); |
| 67 |
call |
| 68 |
.then(result => { |
| 69 |
resolve(result[0].map(entry => ({ |
| 70 |
localId: entry[0], |
| 71 |
summary: entry[1], |
| 72 |
reportCount: entry[2], |
| 73 |
}))); |
| 74 |
}) |
| 75 |
.catch(ex => reject(ex)); |
| 76 |
}); |
| 77 |
}; |
| 78 |
|
| 79 |
/* Return an alert with summary, audit events, fix suggestions (by id) |
| 80 |
localId: an alert id |
| 81 |
summary: a brief description of an alert. E.g. |
| 82 |
"SELinux is preventing /usr/bin/bash from ioctl access on the unix_stream_socket unix_stream_socket." |
| 83 |
reportCount: count of reports of this alert |
| 84 |
auditEvent: an array of audit events (AVC, SYSCALL) connected to the alert |
| 85 |
pluginAnalysis: an array of plugin analysis structure |
| 86 |
ifText |
| 87 |
thenText |
| 88 |
doText |
| 89 |
analysisId: plugin id. It can be used in org.fedoraproject.SetroubleshootFixit.run_fix() |
| 90 |
fixable: True when an alert is fixable by a plugin |
| 91 |
reportBug: True when an alert should be reported |
| 92 |
firstSeen: when the alert was seen for the first time, timestamp in ms |
| 93 |
lastSeen: when the alert was seen for the last time, timestamp in ms |
| 94 |
level: "green", "yellow" or "red" |
| 95 |
*/ |
| 96 |
client.getAlert = localId => client.proxy.call("get_alert", [localId]) |
| 97 |
.then(result => { |
| 98 |
const details = { |
| 99 |
localId: result[0], |
| 100 |
summary: result[1], |
| 101 |
reportCount: result[2], |
| 102 |
auditEvent: result[3], |
| 103 |
pluginAnalysis: result[4], |
| 104 |
firstSeen: result[5] / 1000, |
| 105 |
lastSeen: result[6] / 1000, |
| 106 |
level: result[7], |
| 107 |
}; |
| 108 |
// cleanup analysis |
| 109 |
details.pluginAnalysis = details.pluginAnalysis.map(itm => ({ |
| 110 |
ifText: itm[0], |
| 111 |
thenText: itm[1], |
| 112 |
doText: itm[2], |
| 113 |
analysisId: itm[3], |
| 114 |
fixable: itm[4], |
| 115 |
reportBug: itm[5], |
| 116 |
})); |
| 117 |
return details; |
| 118 |
}) |
| 119 |
.catch(ex => { |
| 120 |
console.warn("Unable to get alert for id", localId, ":", JSON.stringify(ex)); |
| 121 |
return Promise.reject(new Error(`Unable to get alert for id ${localId}: ${ex.toString()}`)); |
| 122 |
}); |
| 123 |
|
| 124 |
/* Run a fix via SetroubleshootFixit |
| 125 |
The analysisId is given as part of pluginAnalysis entries in alert details |
| 126 |
*/ |
| 127 |
client.runFix = (alertId, analysisId) => client.proxyFixit.call("run_fix", [alertId, analysisId]) |
| 128 |
.then(result => result[0]) |
| 129 |
.catch(ex => { |
| 130 |
console.warn("Unable to run fix:", JSON.stringify(ex)); |
| 131 |
return Promise.reject(new Error(cockpit.format(_("Unable to run fix: $0"), ex.toString()))); |
| 132 |
}); |
| 133 |
|
| 134 |
/* Delete an alert from the database (will be removed for all users), returns true on success |
| 135 |
* Only assign this to the client variable if the dbus interface actually supports the operation |
| 136 |
*/ |
| 137 |
client.deleteAlert = localId => client.proxy.call("delete_alert", [localId]) |
| 138 |
.then(success => { |
| 139 |
if (!success) |
| 140 |
return Promise.reject(new Error(cockpit.format(_("Failed to delete alert: $0"), localId))); |
| 141 |
}) |
| 142 |
.catch(ex => { |
| 143 |
console.warn("Unable to delete alert with id", localId, ":", JSON.stringify(ex)); |
| 144 |
return Promise.reject(new Error(cockpit.format(_("Failed to delete alert: $0"), ex.toString()))); |
| 145 |
}); |
| 146 |
|
| 147 |
// connect to dbus and start setroubleshootd |
| 148 |
return connectPromise; |
| 149 |
}; |