Debian Codegraph @

pkg/lib/cockpit-components-firewalld-request.jsx

raw
1 /*
2 * Copyright (C) 2021 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5 import React, { useState } from 'react';
6 import { Alert, AlertActionCloseButton, AlertActionLink } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
7 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
8 import { Toolbar, ToolbarContent, ToolbarGroup, ToolbarItem } from "@patternfly/react-core/dist/esm/components/Toolbar/index.js";
9 import { PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js";
10
11 import { SimpleSelect } from "cockpit-components-simple-select";
12
13 import cockpit from 'cockpit';
14 import './cockpit-components-firewalld-request.scss';
15
16 const _ = cockpit.gettext;
17 const firewalld = cockpit.dbus('org.fedoraproject.FirewallD1', { superuser: "try" });
18
19 function debug() {
20 if (window.debugging == "all" || window.debugging?.includes("firewall"))
21 console.debug.apply(console, arguments);
22 }
23
24 /* React component for an info alert to enable some new service in firewalld.
25 * Use this when enabling some network-facing service. The alert will only be shown
26 * if firewalld is running, has at least one active zone, and the service is not enabled
27 * in any zone yet. It will allow the user to enable the service in any active zone,
28 * or go to the firewall page for more fine-grained configuration.
29 *
30 * Properties:
31 * - service (string, required): firewalld service name
32 * - title (string, required): Human readable/translated alert title
33 * - pageSection (bool, optional, default false): Render the alert inside a <PageSection>
34 */
35 export const FirewalldRequest = ({ service, title, pageSection }) => {
36 const [zones, setZones] = useState(null);
37 const [selectedZone, setSelectedZone] = useState(null);
38 const [enabledAnywhere, setEnabledAnywhere] = useState(null);
39 const [enableError, setEnableError] = useState(null);
40 debug("FirewalldRequest", service, "zones", JSON.stringify(zones), "selected zone", selectedZone, "enabledAnywhere", enabledAnywhere);
41
42 if (!service)
43 return null;
44
45 // query zones on component initialization
46 if (zones === null) {
47 firewalld.call("/org/fedoraproject/FirewallD1", "org.fedoraproject.FirewallD1.zone", "getActiveZones")
48 .then(([info]) => {
49 const names = Object.keys(info);
50 Promise.all(names.map(name => firewalld.call("/org/fedoraproject/FirewallD1", "org.fedoraproject.FirewallD1.zone", "getZoneSettings2", [name])))
51 .then(zoneInfos => {
52 setEnabledAnywhere(!!zoneInfos.find(zoneInfo => ((zoneInfo[0].services || {}).v || []).indexOf(service) >= 0));
53 setZones(names);
54 })
55 .catch(ex => {
56 console.warn("FirewalldRequest: getZoneSettings failed:", JSON.stringify(ex));
57 setZones([]);
58 });
59
60 firewalld.call("/org/fedoraproject/FirewallD1", "org.fedoraproject.FirewallD1", "getDefaultZone")
61 .then(([zone]) => setSelectedZone(zone))
62 .catch(ex => console.warn("FirewalldRequest: getDefaultZone failed:", JSON.stringify(ex)));
63 })
64 .catch(ex => {
65 // firewalld not running
66 debug("FirewalldRequest: getActiveZones failed, considering firewall inactive:", JSON.stringify(ex));
67 setZones([]);
68 });
69 }
70
71 const onAddService = () => {
72 firewalld.call("/org/fedoraproject/FirewallD1", "org.fedoraproject.FirewallD1.zone", "addService",
73 [selectedZone, service, 0])
74 // permanent config
75 .then(() => firewalld.call("/org/fedoraproject/FirewallD1/config",
76 "org.fedoraproject.FirewallD1.config",
77 "getZoneByName", [selectedZone]))
78 .then(([path]) => firewalld.call(path, "org.fedoraproject.FirewallD1.config.zone", "addService", [service]))
79 // all successful, hide alert
80 .then(() => setEnabledAnywhere(true))
81 .catch(ex => {
82 // may already be enabled in permanent config, that's ok
83 if (ex.message && ex.message.indexOf("ALREADY_ENABLED") >= 0) {
84 setEnabledAnywhere(true);
85 return;
86 }
87
88 setEnableError(ex.toString());
89 setEnabledAnywhere(true);
90 console.error("Failed to enable", service, "in firewalld:", JSON.stringify(ex));
91 });
92 };
93
94 let alert;
95
96 if (enableError) {
97 alert = (
98 <Alert isInline variant="warning"
99 title={ cockpit.format(_("Failed to enable $0 in firewalld"), service) }
100 actionClose={ <AlertActionCloseButton onClose={ () => setEnableError(null) } /> }
101 actionLinks={
102 <AlertActionLink onClick={() => cockpit.jump("/network/firewall")}>
103 { _("Visit firewall") }
104 </AlertActionLink>
105 }>
106 {enableError}
107 </Alert>
108 );
109 // don't show anything if firewalld is not active, or service is already enabled somewhere
110 } else if (!zones || zones.length === 0 || !selectedZone || enabledAnywhere) {
111 return null;
112 } else {
113 alert = (
114 <Alert id="firewalld-request-alert" isInline variant="info" title={title} className="pf-v6-u-box-shadow-sm">
115 <Toolbar>
116 <ToolbarContent>
117 <ToolbarGroup>
118 <ToolbarItem variant="label">{ _("Zone") }</ToolbarItem>
119 <ToolbarItem>
120 <SimpleSelect
121 onSelect={setSelectedZone}
122 selected={selectedZone}
123 options={zones.map(zone => ({ value: zone, content: zone }))}
124 toggleProps={{
125 id: "firewalld-request-" + service,
126 "aria-label": _("Zone"),
127 }} />
128 </ToolbarItem>
129
130 <ToolbarItem>
131 <Button variant="primary" onClick={onAddService}>{ cockpit.format(_("Add $0"), service) }</Button>
132 </ToolbarItem>
133 </ToolbarGroup>
134
135 <ToolbarItem variant="separator" />
136
137 <ToolbarItem>
138 <Button variant="link" onClick={() => cockpit.jump("/network/firewall")}>
139 { _("Visit firewall") }
140 </Button>
141 </ToolbarItem>
142 </ToolbarContent>
143 </Toolbar>
144 </Alert>
145 );
146 }
147
148 if (pageSection)
149 return <PageSection hasBodyWrapper={false} className="ct-no-bottom-padding">{alert}</PageSection>;
150 else
151 return alert;
152 };