Skip to content

Commit

Permalink
selinux: Fix error reporting
Browse files Browse the repository at this point in the history
Exception objects are not directly printable, so trying to render it in
an Alert or with `console.warn()` just results in an empty string. That
makes debugging really hard, as there is no corresponding entry in the
journal either.

Use `.toString()` on the exception to render them properly. Also show
the actual exception text in the Alert in the UI. Still keep the
console.warn()s, so that we can do naughty matching on them.

While at it, eliminate the unnecessary Promise wrappers, and use chaining.
  • Loading branch information
martinpitt authored and mvollmer committed Apr 20, 2023
1 parent 86fb923 commit db99329
Showing 1 changed file with 42 additions and 55 deletions.
97 changes: 42 additions & 55 deletions pkg/selinux/setroubleshoot-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,69 +107,56 @@ client.init = function(capabilitiesChangedCallback) {
lastSeen: when the alert was seen for the last time, timestamp in ms
level: "green", "yellow" or "red"
*/
client.getAlert = function(localId) {
return new Promise((resolve, reject) => {
client.proxy.call("get_alert", [localId])
.then(result => {
const details = {
localId: result[0],
summary: result[1],
reportCount: result[2],
auditEvent: result[3],
pluginAnalysis: result[4],
firstSeen: result[5] / 1000,
lastSeen: result[6] / 1000,
level: result[7],
};
// cleanup analysis
details.pluginAnalysis = details.pluginAnalysis.map(itm => ({
ifText: itm[0],
thenText: itm[1],
doText: itm[2],
analysisId: itm[3],
fixable: itm[4],
reportBug: itm[5],
}));
resolve(details);
})
.catch(ex => {
console.warn("Unable to get alert for id " + localId);
console.warn(ex);
reject(new Error(cockpit.format(_("Unable to get alert: $0"), localId)));
});
});
};
client.getAlert = localId => client.proxy.call("get_alert", [localId])
.then(result => {
const details = {
localId: result[0],
summary: result[1],
reportCount: result[2],
auditEvent: result[3],
pluginAnalysis: result[4],
firstSeen: result[5] / 1000,
lastSeen: result[6] / 1000,
level: result[7],
};
// cleanup analysis
details.pluginAnalysis = details.pluginAnalysis.map(itm => ({
ifText: itm[0],
thenText: itm[1],
doText: itm[2],
analysisId: itm[3],
fixable: itm[4],
reportBug: itm[5],
}));
return details;
})
.catch(ex => {
console.warn("Unable to get alert for id", localId, ":", JSON.stringify(ex));
return Promise.reject(new Error(`Unable to get alert for id ${localId}: ${ex.toString()}`));
});

/* Run a fix via SetroubleshootFixit
The analysisId is given as part of pluginAnalysis entries in alert details
*/
client.runFix = function(alertId, analysisId) {
return new Promise((resolve, reject) => {
client.proxyFixit.call("run_fix", [alertId, analysisId])
.then(result => resolve(result[0]))
.catch(ex => reject(new Error(cockpit.format(_("Unable to run fix: $0"), ex))));
});
};
client.runFix = (alertId, analysisId) => client.proxyFixit.call("run_fix", [alertId, analysisId])
.then(result => result[0])
.catch(ex => {
console.warn("Unable to run fix:", JSON.stringify(ex));
return Promise.reject(new Error(cockpit.format(_("Unable to run fix: $0"), ex.toString())));
});

/* Delete an alert from the database (will be removed for all users), returns true on success
* Only assign this to the client variable if the dbus interface actually supports the operation
*/
const deleteAlert = function(localId) {
return new Promise((resolve, reject) => {
client.proxy.call("delete_alert", [localId])
.then(success => {
if (success)
resolve();
else
reject(new Error(cockpit.format(_("Failed to delete alert: $0"), localId)));
})
.catch(ex => {
console.warn("Unable to delete alert with id " + localId);
console.warn(ex);
reject(new Error(cockpit.format(_("Error while deleting alert: $0"), localId)));
});
});
};
const deleteAlert = localId => client.proxy.call("delete_alert", [localId])
.then(success => {
if (!success)
return Promise.reject(new Error(cockpit.format(_("Failed to delete alert: $0"), localId)));
})
.catch(ex => {
console.warn("Unable to delete alert with id", localId, ":", JSON.stringify(ex));
return Promise.reject(new Error(cockpit.format(_("Failed to delete alert: $0"), ex.toString())));
});

// earlier versions of the dbus interface don't support alert deletion/dismissal
// HACK https://bugzilla.redhat.com/show_bug.cgi?id=1306700
Expand Down

0 comments on commit db99329

Please sign in to comment.