/* * Copyright (C) 2016 Red Hat, Inc. * SPDX-License-Identifier: LGPL-2.1-or-later */ import cockpit from "cockpit"; import React from "react"; import { Alert, AlertActionCloseButton, AlertGroup } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; import { Badge } from "@patternfly/react-core/dist/esm/components/Badge/index.js"; import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; import { Divider } from "@patternfly/react-core/dist/esm/components/Divider/index.js"; import { Card, CardBody, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js'; import { ExpandableSection } from "@patternfly/react-core/dist/esm/components/ExpandableSection/index.js"; import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; import { Page, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js"; import { Switch } from "@patternfly/react-core/dist/esm/components/Switch/index.js"; import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; import { Stack, StackItem } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; import { ExclamationCircleIcon, ExclamationTriangleIcon, InfoCircleIcon } from "@patternfly/react-icons"; import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js"; import { Content, ContentVariants } from "@patternfly/react-core/dist/esm/components/Content/index.js"; import { CodeBlock, CodeBlockCode } from "@patternfly/react-core/dist/esm/components/CodeBlock/index.js"; import { Modifications } from "cockpit-components-modifications"; import { EmptyStatePanel } from "cockpit-components-empty-state.jsx"; import { ListingTable } from "cockpit-components-table.jsx"; import { ListingPanel } from 'cockpit-components-listing-panel.jsx'; import * as timeformat from 'timeformat'; import { Title, TitleSizes } from "@patternfly/react-core/dist/esm/components/Title"; const _ = cockpit.gettext; /* Show details for an alert, including possible solutions * Props correspond to an item in the setroubleshoot dataStore */ class SELinuxEventDetails extends React.Component { runFix(itmIdx, runCommand) { const localId = this.props.details.localId; const analysisId = this.props.details.pluginAnalysis[itmIdx].analysisId; this.props.runFix(localId, analysisId, itmIdx, runCommand); } render() { if (!this.props.details) { // details should be requested by default, so we just need to wait for them if (this.props.details === undefined) return ; else return ; } const self = this; const fixEntries = this.props.details.pluginAnalysis.map(function(itm, itmIdx) { let fixit = null; let fixit_command = null; let msg = null; /* some plugins like catchall_sebool don't report fixable as they offer multiple solutions; * we can offer to run a single setsebool command for convenience */ let fixable = itm.fixable; if (!fixable && itm.doText && itm.doText.startsWith("setsebool") && itm.doText.indexOf("\n") < 0) { fixable = true; fixit_command = itm.doText; } if (fixable) { if ((itm.fix) && (itm.fix.plugin == itm.analysisId)) { if (itm.fix.running) { msg = (
{ _("Applying solution...") }
); } else { if (itm.fix.success) { msg = ( {itm.fix.result} ); } else { msg = ( {itm.fix.result} ); } } } if (!itm.fix) { fixit = (
); } } else { fixit = (
{ _("Unable to apply this solution automatically") }
); } function codeBlock(text, key) { return ( {text} ); } let doElement = ""; // One line usually means one command if (itm.doText && itm.doText.indexOf("\n") < 0) doElement = codeBlock(itm.doText); // There can be text with commands. Command always starts on a new line with '#' // Group subsequent commands into one `` element. if (itm.doText && itm.doText.indexOf("\n") >= 0) { const parts = []; const lines = itm.doText.split("\n"); let lastCommand = false; lines.forEach(l => { if (l[0] == "#") { // command if (lastCommand) // When appending command remove "# ". Only the first command keeps it and it is removed later on parts[parts.length - 1] += ("\n" + l.substring(2)); else parts.push(l); lastCommand = true; } else { parts.push(l); lastCommand = false; } }); doElement = parts.map((p, index) => p[0] == "#" ? codeBlock(p.substring(2), index) : {p}); } return (
{itm.ifText}
{itm.thenText}
{doElement} {msg}
{fixit}
{itmIdx != self.props.details.pluginAnalysis.length - 1 && }
); }); return {fixEntries}; } } /* Show the audit log events for an alert */ const SELinuxEventLog = ({ details }) => { if (!details) { // details should be requested by default, so we just need to wait for them if (details === undefined) return ; else return ; } const logEntries = details.auditEvent.map((itm, idx) => { // use the alert id and index in the event log array as the data key for react // if the log becomes dynamic, the entire log line might need to be considered as the key return
{itm}
; }); return
{logEntries}
; }; /* Component to show a dismissable error, message as child text * dismissError callback function triggered when the close button is pressed */ class DismissableError extends React.Component { constructor(props) { super(props); this.handleDismissError = this.handleDismissError.bind(this); } handleDismissError(e) { // only consider primary mouse button if (!e || e.button !== 0) return; if (this.props.dismissError) this.props.dismissError(); e.stopPropagation(); } render() { return ( } /> ); } } /* Component to show selinux status and offer an option to change it * selinuxStatus status of selinux on the system, properties as defined in selinux-client.js * selinuxStatusError error message from reading or setting selinux status/mode * changeSelinuxMode function to use for changing the selinux enforcing mode * dismissError function to dismiss the error message */ const SELinuxStatus = ({ selinuxStatus, selinuxStatusError, changeSelinuxMode, dismissError }) => { const errorMessage = selinuxStatusError ? {selinuxStatusError} : null; if (selinuxStatus.enabled === undefined) { return (
{errorMessage}

{_("SELinux system status is unknown.")}

); } else if (!selinuxStatus.enabled) { return (
{errorMessage}

{_("SELinux is disabled on the system.")}

); } const configUnknown = (selinuxStatus.configEnforcing === undefined); let note = null; if (configUnknown) note = _("The configured state is unknown, it might change on the next boot."); else if (!configUnknown && selinuxStatus.enforcing !== selinuxStatus.configEnforcing) note = _("Setting deviates from the configured state and will revert on the next boot."); return ( {_("SELinux policy")} { note !== null && { "\n" } { note } } {errorMessage} ); }; /* The listing only shows if we have a connection to the dbus API * Otherwise we have blank slate: trying to connect, error * Expected properties: * connected true if the client is connected to setroubleshoot-server via dbus * error error message to show (in EmptyState if not connected, as a dismissable alert otherwise * dismissError callback, triggered for the dismissable error in connected state * deleteAlert callback, triggered with an alert id as parameter to trigger deletion * entries setroubleshoot entries * - runFix function to run fix * - details fix details as provided by the setroubleshoot client * - description brief description of the error * - count how many times (>= 1) this alert occurred * selinuxStatus status of selinux on the system, properties as defined in selinux-client.js * selinuxStatusError error message from reading or setting selinux status/mode * changeSelinuxMode function to use for changing the selinux enforcing mode * dismissStatusError function that is triggered to dismiss the selinux status error */ export class SETroubleshootPage extends React.Component { constructor(props) { super(props); this.state = { selected: {} }; this.handleDismissError = this.handleDismissError.bind(this); this.onSelect = this.onSelect.bind(this); } handleDismissError(e) { // only consider primary mouse button if (!e || e.button !== 0) return; if (this.props.dismissError) this.props.dismissError(); e.stopPropagation(); } onSelect(_, isSelected, rowId) { this.setState(prevState => ({ selected: { ...prevState.selected, [this.props.entries[rowId].key]: isSelected } })); } render() { // if selinux is disabled, we only show EmptyState if (this.props.selinuxStatus.enabled === false) { return ; } const self = this; const title = _("SELinux access control errors"); const emptyCaption = _("No SELinux alerts."); let emptyState; let entries; if (!this.props.connected) { if (this.props.connecting) { emptyState = ; } else { // if we don't have setroubleshoot-server, be more subtle about saying that emptyState = ; } } else { entries = this.props.entries.map(function(itm, index) { itm.runFix = self.props.runFix; let listingDetail; if (itm.details && 'firstSeen' in itm.details) { if (itm.details.reportCount >= 2) { listingDetail = cockpit.format(_("Occurred between $0 and $1"), timeformat.dateTime(itm.details.firstSeen), timeformat.dateTime(itm.details.lastSeen) ); } else { listingDetail = cockpit.format(_("Occurred $0"), timeformat.dateTime(itm.details.firstSeen)); } } const tabRenderers = [ { name: _("Solutions"), renderer: SELinuxEventDetails, data: itm, }, { name: _("Audit log"), renderer: SELinuxEventLog, data: itm, }, ]; // if the alert has level "red", it's critical const criticalAlert = (itm.details && 'level' in itm.details && itm.details.level == "red") ? : null; const columns = [ { title: criticalAlert }, { title: itm.description } ]; if (itm.count > 1) { columns.push({ title: {itm.count}, props: { className: "pf-v6-c-table__action" } }); } else { columns.push({ title: , props: { className: "pf-v6-c-table__action" } }); } const rowId = itm.details ? itm.details.localId : index; return ({ props: { key: rowId, "data-row-id": rowId }, selected: self.state.selected[itm.details ? itm.details.localId : index], disableSelection: !itm.details, columns, expandedContent: }); }); } let selectedCnt = 0; for (const k in this.state.selected) if (this.state.selected[k]) selectedCnt++; const onDeleteClick = () => { for (const k in this.state.selected) if (this.state.selected[k]) this.props.deleteAlert(k).then(() => this.setState(prevState => ({ selected: { ...prevState.selected, [k]: false } }))); }; const actions = ( !emptyState ? : null ); const troubleshooting = ( {title} {!emptyState ? : emptyState} ); const modifications = ( ); let errorMessage; if (this.props.error) { errorMessage = ( } /> ); } return ( <> {errorMessage} {modifications} {troubleshooting} ); } }