| 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 |
import 'cockpit-dark-theme'; // once per page |
| 8 |
|
| 9 |
import React from "react"; |
| 10 |
import { createRoot } from "react-dom/client"; |
| 11 |
|
| 12 |
import '../lib/patternfly/patternfly-6-cockpit.scss'; |
| 13 |
|
| 14 |
import * as troubleshootClient from "./setroubleshoot-client.js"; |
| 15 |
import * as selinuxClient from "./selinux-client.js"; |
| 16 |
import { SETroubleshootPage } from "./setroubleshoot-view.jsx"; |
| 17 |
import { superuser } from 'superuser'; |
| 18 |
|
| 19 |
import "./setroubleshoot.scss"; |
| 20 |
|
| 21 |
const _ = cockpit.gettext; |
| 22 |
|
| 23 |
superuser.reload_page_on_change(); |
| 24 |
|
| 25 |
const initStore = function(rootElement) { |
| 26 |
const dataStore = { }; |
| 27 |
dataStore.domRootElement = rootElement; |
| 28 |
|
| 29 |
dataStore.entries = []; |
| 30 |
|
| 31 |
// connected to the dbus api of setroubleshootd |
| 32 |
dataStore.connected = false; |
| 33 |
|
| 34 |
// currently trying to connect / used for timer |
| 35 |
dataStore.connecting = null; |
| 36 |
|
| 37 |
// did we have a connection error? |
| 38 |
dataStore.error = null; |
| 39 |
|
| 40 |
dataStore.client = troubleshootClient.client; |
| 41 |
|
| 42 |
dataStore.selinuxStatusError = undefined; |
| 43 |
|
| 44 |
const selinuxStatusChanged = function(status, errorMessage) { |
| 45 |
dataStore.selinuxStatus = status; |
| 46 |
if (errorMessage !== undefined) |
| 47 |
dataStore.selinuxStatusError = errorMessage; |
| 48 |
dataStore.render(); |
| 49 |
}; |
| 50 |
const selinuxStatusDismissError = function() { |
| 51 |
dataStore.selinuxStatusError = undefined; |
| 52 |
dataStore.render(); |
| 53 |
}; |
| 54 |
const selinuxChangeMode = function(_event, newMode) { |
| 55 |
selinuxClient.setEnforcing(newMode).then( |
| 56 |
function() { |
| 57 |
dataStore.selinuxStatus.enforcing = newMode; |
| 58 |
dataStore.render(); |
| 59 |
}, |
| 60 |
function(error) { |
| 61 |
dataStore.selinuxStatusError = cockpit.format(_("Error while setting SELinux mode: '$0'"), error.message); |
| 62 |
dataStore.render(); |
| 63 |
} |
| 64 |
); |
| 65 |
}; |
| 66 |
dataStore.selinuxStatus = selinuxClient.init(selinuxStatusChanged); |
| 67 |
|
| 68 |
// run a fix and update the entries accordingly |
| 69 |
const runFix = function(alertId, analysisId, fixId, runCommand) { |
| 70 |
let idx; |
| 71 |
for (idx = dataStore.entries.length - 1; idx >= 0; --idx) { |
| 72 |
if (dataStore.entries[idx].key == alertId) |
| 73 |
break; |
| 74 |
} |
| 75 |
if (idx < 0) { |
| 76 |
console.log("Unable to find alert entry for element requesting fix: " + alertId + " (" + analysisId + ")."); |
| 77 |
return; |
| 78 |
} |
| 79 |
dataStore.entries[idx].details.pluginAnalysis[fixId].fix = { |
| 80 |
plugin: analysisId, |
| 81 |
running: true, |
| 82 |
result: null, |
| 83 |
success: false, |
| 84 |
}; |
| 85 |
dataStore.render(); |
| 86 |
const promise = runCommand |
| 87 |
? cockpit.script(runCommand, { err: "message", superuser: "require" }) |
| 88 |
: dataStore.client.runFix(alertId, analysisId); |
| 89 |
|
| 90 |
promise |
| 91 |
.then(output => { |
| 92 |
dataStore.entries[idx].details.pluginAnalysis[fixId].fix = { |
| 93 |
plugin: analysisId, |
| 94 |
running: false, |
| 95 |
result: output, |
| 96 |
success: true, |
| 97 |
}; |
| 98 |
selinuxClient.getModifications(selinuxStatusChanged); |
| 99 |
dataStore.render(); |
| 100 |
}) |
| 101 |
.catch(error => { |
| 102 |
dataStore.entries[idx].details.pluginAnalysis[fixId].fix = { |
| 103 |
plugin: analysisId, |
| 104 |
running: false, |
| 105 |
result: error.toString(), |
| 106 |
success: false, |
| 107 |
}; |
| 108 |
dataStore.render(); |
| 109 |
}); |
| 110 |
}; |
| 111 |
|
| 112 |
/* Delete an alert via the client |
| 113 |
* if it goes wrong, show an error |
| 114 |
* remove the entry if successful |
| 115 |
* This function will only be called if the backend functionality is actually present |
| 116 |
*/ |
| 117 |
const deleteAlert = function(alertId) { |
| 118 |
return dataStore.client.deleteAlert(alertId) |
| 119 |
.then(() => { |
| 120 |
let idx; |
| 121 |
for (idx = dataStore.entries.length - 1; idx >= 0; --idx) { |
| 122 |
if (dataStore.entries[idx].key == alertId) |
| 123 |
break; |
| 124 |
} |
| 125 |
if (idx < 0) |
| 126 |
return; |
| 127 |
dataStore.entries.splice(idx, 1); |
| 128 |
dataStore.render(); |
| 129 |
}) |
| 130 |
.catch(error => { |
| 131 |
dataStore.error = error.toString(); |
| 132 |
dataStore.render(); |
| 133 |
}); |
| 134 |
}; |
| 135 |
|
| 136 |
const dismissError = function() { |
| 137 |
dataStore.error = null; |
| 138 |
dataStore.render(); |
| 139 |
}; |
| 140 |
|
| 141 |
const render = function() { |
| 142 |
if (!dataStore.reactRoot) |
| 143 |
dataStore.reactRoot = createRoot(rootElement); |
| 144 |
|
| 145 |
dataStore.reactRoot.render(React.createElement(SETroubleshootPage, { |
| 146 |
connected: dataStore.connected, |
| 147 |
connecting: dataStore.connecting, |
| 148 |
error: dataStore.error, |
| 149 |
dismissError, |
| 150 |
entries: dataStore.entries, |
| 151 |
runFix, |
| 152 |
deleteAlert, |
| 153 |
selinuxStatus: dataStore.selinuxStatus, |
| 154 |
selinuxStatusError: dataStore.selinuxStatusError, |
| 155 |
changeSelinuxMode: selinuxChangeMode, |
| 156 |
dismissStatusError: selinuxStatusDismissError, |
| 157 |
})); |
| 158 |
}; |
| 159 |
dataStore.render = render; |
| 160 |
|
| 161 |
/* Update an alert entry if it exists, otherwise create one |
| 162 |
Details: if undefined, we don't have info on them yet, |
| 163 |
while null means an error occurred while retrieving them |
| 164 |
The function doesn't trigger a render |
| 165 |
*/ |
| 166 |
const maybeUpdateAlert = function(localId, description, count, details) { |
| 167 |
// if we already know about this alert, ignore unless the repetition count changed |
| 168 |
// we start at the back because that's where we push new entries |
| 169 |
// if we receive an alert multiple times, this is where it will be |
| 170 |
for (let idx = dataStore.entries.length - 1; idx >= 0; --idx) { |
| 171 |
if (dataStore.entries[idx].key == localId) { |
| 172 |
if (description === undefined || count === undefined) { |
| 173 |
dataStore.entries[idx].details = details; |
| 174 |
return; |
| 175 |
} |
| 176 |
// don't update newer information |
| 177 |
// this can happen in cases of highly frequent updates |
| 178 |
if (dataStore.entries[idx].count <= count) { |
| 179 |
// don't tamper with the status of a fix being run |
| 180 |
// new alerts might be coming in while a fix is running and we don't want |
| 181 |
// to lose the progress or result |
| 182 |
|
| 183 |
// only allow details to be null if the count has increased |
| 184 |
if ((details !== undefined) || (dataStore.entries[idx].count < count)) { |
| 185 |
dataStore.entries[idx].details = details; |
| 186 |
} |
| 187 |
dataStore.entries[idx].description = description; |
| 188 |
dataStore.entries[idx].count = count; |
| 189 |
} |
| 190 |
return; |
| 191 |
} |
| 192 |
} |
| 193 |
// nothing found, so we create a new entry |
| 194 |
dataStore.entries.push({ key: localId, description, count, details, fix: null }); |
| 195 |
}; |
| 196 |
|
| 197 |
/* Add a list of messages and triggers getting details for each of them |
| 198 |
The list is added without details at first (if it's a new entry) to preserve the order |
| 199 |
*/ |
| 200 |
const handleMultipleMessages = function(entries) { |
| 201 |
const detailPromises = []; |
| 202 |
for (let idxEntry = 0; idxEntry != entries.length; ++idxEntry) { |
| 203 |
const entry = entries[idxEntry]; |
| 204 |
maybeUpdateAlert(entry.localId, entry.summary, entry.reportCount, undefined); |
| 205 |
detailPromises.push(dataStore.getAlertDetails(entry.localId)); |
| 206 |
} |
| 207 |
|
| 208 |
Promise.all(detailPromises).then(render); |
| 209 |
}; |
| 210 |
|
| 211 |
dataStore.handleAlert = function(level, localId) { |
| 212 |
// right now the level is unused, since we can't access it for existing alerts |
| 213 |
|
| 214 |
// we receive the item details in added delayed fashion, render only once we have the full info |
| 215 |
dataStore.getAlertDetails(localId); |
| 216 |
render(); |
| 217 |
}; |
| 218 |
|
| 219 |
const getAlertDetails = function(id) { |
| 220 |
return dataStore.client.getAlert(id) |
| 221 |
.then(details => { |
| 222 |
maybeUpdateAlert(id, details.summary, details.reportCount, details); |
| 223 |
}) |
| 224 |
.catch(() => { |
| 225 |
maybeUpdateAlert(id, undefined, undefined, null); |
| 226 |
}); |
| 227 |
}; |
| 228 |
dataStore.getAlertDetails = getAlertDetails; |
| 229 |
|
| 230 |
const setDisconnected = function() { |
| 231 |
dataStore.connected = false; |
| 232 |
render(); |
| 233 |
}; |
| 234 |
|
| 235 |
const setErrorIfNotConnected = function() { |
| 236 |
if (dataStore.connecting === null) |
| 237 |
return; |
| 238 |
dataStore.error = _("Not connected"); |
| 239 |
render(); |
| 240 |
}; |
| 241 |
|
| 242 |
dataStore.connectionTimeout = 5000; |
| 243 |
|
| 244 |
// try to connect |
| 245 |
dataStore.tryConnect = function() { |
| 246 |
if (dataStore.connecting === null) { |
| 247 |
dataStore.connecting = window.setTimeout(setErrorIfNotConnected, dataStore.connectionTimeout); |
| 248 |
render(); |
| 249 |
// initialize our setroubleshootd client |
| 250 |
dataStore.client.init() |
| 251 |
.then(() => { |
| 252 |
dataStore.connected = true; |
| 253 |
window.clearTimeout(dataStore.connecting); |
| 254 |
dataStore.connecting = null; |
| 255 |
dataStore.error = null; // reset "not connected" |
| 256 |
render(); |
| 257 |
// now register a callback to get new entries and get all existing ones |
| 258 |
// the order is important, since we don't want to miss an entry |
| 259 |
dataStore.client.handleAlert(dataStore.handleAlert); |
| 260 |
dataStore.client.getAlerts() |
| 261 |
.then(handleMultipleMessages) |
| 262 |
.catch(() => { |
| 263 |
console.error("Unable to get setroubleshootd messages"); |
| 264 |
setDisconnected(); |
| 265 |
}); |
| 266 |
}) |
| 267 |
.catch(() => { |
| 268 |
dataStore.connected = false; |
| 269 |
window.clearTimeout(dataStore.connecting); |
| 270 |
dataStore.connecting = null; |
| 271 |
render(); |
| 272 |
}); |
| 273 |
} |
| 274 |
}; |
| 275 |
|
| 276 |
// render once initially |
| 277 |
render(); |
| 278 |
|
| 279 |
// try to connect immediately |
| 280 |
dataStore.tryConnect(); |
| 281 |
|
| 282 |
return dataStore; |
| 283 |
}; |
| 284 |
|
| 285 |
document.addEventListener("DOMContentLoaded", function() { |
| 286 |
initStore(document.getElementById('app')); |
| 287 |
}); |