| 1 |
import React, { useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Flex, |
| 5 |
Form, |
| 6 |
FormGroup, |
| 7 |
JumpLinks, |
| 8 |
JumpLinksItem, |
| 9 |
Label, |
| 10 |
Sidebar, |
| 11 |
SidebarPanel, |
| 12 |
SidebarContent, |
| 13 |
Tab, |
| 14 |
Tabs, |
| 15 |
TabTitleText, |
| 16 |
Content, |
| 17 |
ContentVariants, |
| 18 |
Title, |
| 19 |
} from "@patternfly/react-core"; |
| 20 |
// Forms |
| 21 |
import IpaTextArea from "src/components/Form/IpaTextArea"; |
| 22 |
import IpaCheckbox from "src/components/Form/IpaCheckbox"; |
| 23 |
// Layouts |
| 24 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 25 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 26 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 27 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 28 |
// Utils |
| 29 |
import { asRecord } from "../../utils/hostUtils"; |
| 30 |
// Redux |
| 31 |
import { useAppDispatch } from "src/store/hooks"; |
| 32 |
// Hooks |
| 33 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 34 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 35 |
// Data types |
| 36 |
import { HBACRule, Metadata } from "../../utils/datatypes/globalDataTypes"; |
| 37 |
import HBACRulesMemberTable from "./HBACRulesMemberTable"; |
| 38 |
// Icons |
| 39 |
import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons"; |
| 40 |
// RPC |
| 41 |
import { ErrorResult } from "src/services/rpc"; |
| 42 |
import { |
| 43 |
useSaveAndCleanHbacRuleMutation, |
| 44 |
AllowAllPayload, |
| 45 |
} from "src/services/rpcHBACRules"; |
| 46 |
|
| 47 |
interface PropsToSettings { |
| 48 |
rule: Partial<HBACRule>; |
| 49 |
originalRule: Partial<HBACRule>; |
| 50 |
metadata: Metadata; |
| 51 |
onRuleChange: (rule: Partial<HBACRule>) => void; |
| 52 |
onRefresh: () => void; |
| 53 |
isModified: boolean; |
| 54 |
isDataLoading?: boolean; |
| 55 |
modifiedValues: () => Partial<HBACRule>; |
| 56 |
onResetValues: () => void; |
| 57 |
} |
| 58 |
|
| 59 |
const HBACRulesSettings = (props: PropsToSettings) => { |
| 60 |
const dispatch = useAppDispatch(); |
| 61 |
|
| 62 |
// API |
| 63 |
const [saveRule] = useSaveAndCleanHbacRuleMutation(); |
| 64 |
|
| 65 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 66 |
useUpdateRoute({ pathname: "hbac-rules", noBreadcrumb: true }); |
| 67 |
|
| 68 |
const [cn, setRuleName] = useState<string>(""); |
| 69 |
const [memberUsers, setMemberUsers] = useState<string[]>([]); |
| 70 |
const [memberGroups, setMemberGroups] = useState<string[]>([]); |
| 71 |
const [memberHosts, setMemberHosts] = useState<string[]>([]); |
| 72 |
const [memberHostGroups, setMemberHostGroups] = useState<string[]>([]); |
| 73 |
const [memberServices, setMemberServices] = useState<string[]>([]); |
| 74 |
const [memberServiceGroups, setMemberServiceGroups] = useState<string[]>([]); |
| 75 |
|
| 76 |
React.useEffect(() => { |
| 77 |
// Process the member users and hosts |
| 78 |
setRuleName(props.rule.cn ? props.rule.cn : ""); |
| 79 |
if (props.rule.memberuser_user) { |
| 80 |
setMemberUsers(props.rule.memberuser_user); |
| 81 |
} |
| 82 |
if (props.rule.memberuser_group) { |
| 83 |
setMemberGroups(props.rule.memberuser_group); |
| 84 |
} |
| 85 |
if (props.rule.memberhost_host) { |
| 86 |
setMemberHosts(props.rule.memberhost_host); |
| 87 |
} |
| 88 |
if (props.rule.memberhost_hostgroup) { |
| 89 |
setMemberHostGroups(props.rule.memberhost_hostgroup); |
| 90 |
} |
| 91 |
if (props.rule.memberservice_hbacsvc) { |
| 92 |
setMemberServices(props.rule.memberservice_hbacsvc); |
| 93 |
} |
| 94 |
if (props.rule.memberservice_hbacsvcgroup) { |
| 95 |
setMemberServiceGroups(props.rule.memberservice_hbacsvcgroup); |
| 96 |
} |
| 97 |
}, [props.rule]); |
| 98 |
|
| 99 |
// Tabs |
| 100 |
const [userTabKey, setUserTabKey] = useState(0); |
| 101 |
const [hostTabKey, setHostTabKey] = useState(0); |
| 102 |
const [srvTabKey, setSrvTabKey] = useState(0); |
| 103 |
|
| 104 |
const handleUserTabClick = ( |
| 105 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 106 |
tabIndex: number | string |
| 107 |
) => { |
| 108 |
setUserTabKey(tabIndex as number); |
| 109 |
}; |
| 110 |
const handleHostTabClick = ( |
| 111 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 112 |
tabIndex: number | string |
| 113 |
) => { |
| 114 |
setHostTabKey(tabIndex as number); |
| 115 |
}; |
| 116 |
const handleSrvTabClick = ( |
| 117 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 118 |
tabIndex: number | string |
| 119 |
) => { |
| 120 |
setSrvTabKey(tabIndex as number); |
| 121 |
}; |
| 122 |
|
| 123 |
// Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' |
| 124 |
const { ipaObject, recordOnChange } = asRecord( |
| 125 |
props.rule, |
| 126 |
props.onRuleChange |
| 127 |
); |
| 128 |
|
| 129 |
const [isSaving, setSaving] = useState(false); |
| 130 |
|
| 131 |
// 'Save' handler method |
| 132 |
const onSave = () => { |
| 133 |
const modifiedValues = props.modifiedValues(); |
| 134 |
const payload = { |
| 135 |
groupName: cn, |
| 136 |
users: memberUsers, |
| 137 |
groups: memberGroups, |
| 138 |
hosts: memberHosts, |
| 139 |
hostgroups: memberHostGroups, |
| 140 |
services: memberServices, |
| 141 |
servicegroups: memberServiceGroups, |
| 142 |
modifiedValues, |
| 143 |
} as AllowAllPayload; |
| 144 |
setSaving(true); |
| 145 |
|
| 146 |
saveRule(payload).then((response) => { |
| 147 |
if ("data" in response) { |
| 148 |
if (response.data?.result) { |
| 149 |
// Show toast notification: success |
| 150 |
dispatch( |
| 151 |
addAlert({ |
| 152 |
name: "save-success", |
| 153 |
title: "HBAC rule modified", |
| 154 |
variant: "success", |
| 155 |
}) |
| 156 |
); |
| 157 |
setHostTabKey(0); |
| 158 |
setUserTabKey(0); |
| 159 |
setSrvTabKey(0); |
| 160 |
props.onRefresh(); |
| 161 |
} else if (response.data?.error) { |
| 162 |
// Show toast notification: error |
| 163 |
const errorMessage = response.data.error as ErrorResult; |
| 164 |
dispatch( |
| 165 |
addAlert({ |
| 166 |
name: "save-error", |
| 167 |
title: errorMessage.message, |
| 168 |
variant: "danger", |
| 169 |
}) |
| 170 |
); |
| 171 |
// Reset values. Disable 'revert' and 'save' buttons |
| 172 |
props.onResetValues(); |
| 173 |
} |
| 174 |
setSaving(false); |
| 175 |
} |
| 176 |
}); |
| 177 |
}; |
| 178 |
|
| 179 |
// 'Revert' handler method |
| 180 |
const onRevert = () => { |
| 181 |
props.onRuleChange(props.originalRule); |
| 182 |
dispatch( |
| 183 |
addAlert({ |
| 184 |
name: "revert-success", |
| 185 |
title: "HBAC rule data reverted", |
| 186 |
variant: "success", |
| 187 |
}) |
| 188 |
); |
| 189 |
}; |
| 190 |
|
| 191 |
// Toolbar |
| 192 |
const toolbarFields = [ |
| 193 |
{ |
| 194 |
key: 0, |
| 195 |
element: ( |
| 196 |
<SecondaryButton |
| 197 |
dataCy="hbac-rules-tab-settings-button-refresh" |
| 198 |
onClickHandler={props.onRefresh} |
| 199 |
> |
| 200 |
Refresh |
| 201 |
</SecondaryButton> |
| 202 |
), |
| 203 |
}, |
| 204 |
{ |
| 205 |
key: 1, |
| 206 |
element: ( |
| 207 |
<SecondaryButton |
| 208 |
dataCy="hbac-rules-tab-settings-button-revert" |
| 209 |
isDisabled={!props.isModified} |
| 210 |
onClickHandler={onRevert} |
| 211 |
> |
| 212 |
Revert |
| 213 |
</SecondaryButton> |
| 214 |
), |
| 215 |
}, |
| 216 |
{ |
| 217 |
key: 2, |
| 218 |
element: ( |
| 219 |
<SecondaryButton |
| 220 |
dataCy="hbac-rules-tab-settings-button-save" |
| 221 |
isDisabled={!props.isModified || isSaving} |
| 222 |
onClickHandler={onSave} |
| 223 |
isLoading={isSaving} |
| 224 |
spinnerAriaValueText="Saving" |
| 225 |
spinnerAriaLabelledBy="Saving" |
| 226 |
spinnerAriaLabel="Saving" |
| 227 |
> |
| 228 |
{isSaving ? "Saving" : "Save"} |
| 229 |
</SecondaryButton> |
| 230 |
), |
| 231 |
}, |
| 232 |
]; |
| 233 |
|
| 234 |
const whoLabel = ( |
| 235 |
<Content component={ContentVariants.small}>Allow anyone</Content> |
| 236 |
); |
| 237 |
const whatLabel = ( |
| 238 |
<Content component={ContentVariants.small}>Any host</Content> |
| 239 |
); |
| 240 |
const howLabel = ( |
| 241 |
<Content component={ContentVariants.small}>Any service</Content> |
| 242 |
); |
| 243 |
|
| 244 |
// Render component |
| 245 |
return ( |
| 246 |
<TabLayout id="settings-page" toolbarItems={toolbarFields}> |
| 247 |
<Sidebar isPanelRight> |
| 248 |
<SidebarPanel variant="sticky"> |
| 249 |
<HelpTextWithIconLayout |
| 250 |
textContent="Help" |
| 251 |
icon={ |
| 252 |
<OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100 pf-v6-u-mr-sm" /> |
| 253 |
} |
| 254 |
/> |
| 255 |
<JumpLinks |
| 256 |
isVertical |
| 257 |
label="Jump to section" |
| 258 |
scrollableSelector="#settings-page" |
| 259 |
offset={220} // for masthead |
| 260 |
expandable={{ default: "expandable", md: "nonExpandable" }} |
| 261 |
> |
| 262 |
<JumpLinksItem key={0} href="#hbacrule-settings"> |
| 263 |
Settings |
| 264 |
</JumpLinksItem> |
| 265 |
<JumpLinksItem key={1} href="#usercategory"> |
| 266 |
Who the rule applies to |
| 267 |
</JumpLinksItem> |
| 268 |
<JumpLinksItem key={2} href="#hostcategory"> |
| 269 |
Gives access to |
| 270 |
</JumpLinksItem> |
| 271 |
<JumpLinksItem key={3} href="#servicecategory"> |
| 272 |
Via the following services |
| 273 |
</JumpLinksItem> |
| 274 |
</JumpLinks> |
| 275 |
</SidebarPanel> |
| 276 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 277 |
<Flex direction={{ default: "column" }} flex={{ default: "flex_1" }}> |
| 278 |
<TitleLayout |
| 279 |
key={0} |
| 280 |
headingLevel="h1" |
| 281 |
id="hbacrule-settings" |
| 282 |
text="HBAC rule settings" |
| 283 |
/> |
| 284 |
<Form className="pf-v6-u-mt-sm pf-v6-u-mb-lg pf-v6-u-mr-md"> |
| 285 |
<FormGroup label="Description" fieldId="description"> |
| 286 |
<IpaTextArea |
| 287 |
dataCy="hbac-rules-tab-settings-textbox-description" |
| 288 |
name="description" |
| 289 |
ipaObject={ipaObject} |
| 290 |
onChange={recordOnChange} |
| 291 |
objectName="hbacrule" |
| 292 |
metadata={props.metadata} |
| 293 |
/> |
| 294 |
</FormGroup> |
| 295 |
</Form> |
| 296 |
<Content key="usercategory"> |
| 297 |
<Title |
| 298 |
headingLevel="h2" |
| 299 |
id="usercategory" |
| 300 |
className="pf-v6-u-mt-lg pf-v6-u-display-flex" |
| 301 |
> |
| 302 |
Who the rule applies to |
| 303 |
<IpaCheckbox |
| 304 |
dataCy="hbac-rules-tab-settings-checkbox-usercategory" |
| 305 |
name="usercategory" |
| 306 |
value="usercategory" |
| 307 |
text="Allow anyone" |
| 308 |
textNode={whoLabel} |
| 309 |
className="pf-v6-u-ml-lg pf-v6-u-mt-xs" |
| 310 |
ipaObject={ipaObject} |
| 311 |
onChange={recordOnChange} |
| 312 |
objectName="hbacrule" |
| 313 |
metadata={props.metadata} |
| 314 |
altTrue="all" |
| 315 |
altFalse={""} |
| 316 |
/> |
| 317 |
</Title> |
| 318 |
</Content> |
| 319 |
{ipaObject.usercategory === "" && ( |
| 320 |
<Tabs |
| 321 |
activeKey={userTabKey} |
| 322 |
onSelect={handleUserTabClick} |
| 323 |
className="pf-v6-u-ml-md pf-v6-u-mr-md" |
| 324 |
> |
| 325 |
<Tab |
| 326 |
eventKey={0} |
| 327 |
name="users" |
| 328 |
data-cy="hbac-rules-tab-settings-tab-users" |
| 329 |
title={ |
| 330 |
<TabTitleText> |
| 331 |
Users <Label isCompact>{memberUsers.length}</Label> |
| 332 |
</TabTitleText> |
| 333 |
} |
| 334 |
> |
| 335 |
<HBACRulesMemberTable |
| 336 |
from="user" |
| 337 |
id={cn} |
| 338 |
members={memberUsers} |
| 339 |
onRefresh={props.onRefresh} |
| 340 |
unsetCategory={ |
| 341 |
ipaObject.usercategory !== |
| 342 |
props.originalRule.usercategory && |
| 343 |
ipaObject.usercategory === "" |
| 344 |
} |
| 345 |
/> |
| 346 |
</Tab> |
| 347 |
<Tab |
| 348 |
eventKey={1} |
| 349 |
name="groups" |
| 350 |
data-cy="hbac-rules-tab-settings-tab-groups" |
| 351 |
title={ |
| 352 |
<TabTitleText> |
| 353 |
Groups <Label isCompact>{memberGroups.length}</Label> |
| 354 |
</TabTitleText> |
| 355 |
} |
| 356 |
> |
| 357 |
<HBACRulesMemberTable |
| 358 |
from="group" |
| 359 |
id={cn} |
| 360 |
members={memberGroups} |
| 361 |
onRefresh={props.onRefresh} |
| 362 |
unsetCategory={ |
| 363 |
ipaObject.usercategory !== |
| 364 |
props.originalRule.usercategory && |
| 365 |
ipaObject.usercategory === "" |
| 366 |
} |
| 367 |
/> |
| 368 |
</Tab> |
| 369 |
</Tabs> |
| 370 |
)} |
| 371 |
<Content key="hostcategory"> |
| 372 |
<Title |
| 373 |
headingLevel="h2" |
| 374 |
id="hostcategory" |
| 375 |
className="pf-v6-u-mt-xl pf-v6-u-display-flex" |
| 376 |
> |
| 377 |
Gives access to |
| 378 |
<IpaCheckbox |
| 379 |
dataCy="hbac-rules-tab-settings-checkbox-hostcategory" |
| 380 |
name="hostcategory" |
| 381 |
value="hostcategory" |
| 382 |
text="Any host" |
| 383 |
textNode={whatLabel} |
| 384 |
className="pf-v6-u-ml-lg pf-v6-u-mt-xs" |
| 385 |
ipaObject={ipaObject} |
| 386 |
onChange={recordOnChange} |
| 387 |
objectName="hbacrule" |
| 388 |
metadata={props.metadata} |
| 389 |
altTrue="all" |
| 390 |
altFalse={""} |
| 391 |
/> |
| 392 |
</Title> |
| 393 |
</Content> |
| 394 |
{ipaObject.hostcategory === "" && ( |
| 395 |
<Tabs |
| 396 |
activeKey={hostTabKey} |
| 397 |
onSelect={handleHostTabClick} |
| 398 |
className="pf-v6-u-ml-md pf-v6-u-mr-md" |
| 399 |
> |
| 400 |
<Tab |
| 401 |
eventKey={0} |
| 402 |
name="memberHosts" |
| 403 |
data-cy="hbac-rules-tab-settings-tab-hosts" |
| 404 |
title={ |
| 405 |
<TabTitleText> |
| 406 |
Hosts <Label isCompact>{memberHosts.length}</Label> |
| 407 |
</TabTitleText> |
| 408 |
} |
| 409 |
> |
| 410 |
<HBACRulesMemberTable |
| 411 |
from="host" |
| 412 |
id={cn} |
| 413 |
members={memberHosts} |
| 414 |
onRefresh={props.onRefresh} |
| 415 |
unsetCategory={ |
| 416 |
ipaObject.hostcategory !== |
| 417 |
props.originalRule.hostcategory && |
| 418 |
ipaObject.hostcategory === "" |
| 419 |
} |
| 420 |
/> |
| 421 |
</Tab> |
| 422 |
<Tab |
| 423 |
eventKey={1} |
| 424 |
name="memberHostGroups" |
| 425 |
data-cy="hbac-rules-tab-settings-tab-hostgroups" |
| 426 |
title={ |
| 427 |
<TabTitleText> |
| 428 |
Host groups{" "} |
| 429 |
<Label isCompact>{memberHostGroups.length}</Label> |
| 430 |
</TabTitleText> |
| 431 |
} |
| 432 |
> |
| 433 |
<HBACRulesMemberTable |
| 434 |
from="hostgroup" |
| 435 |
id={cn} |
| 436 |
members={memberHostGroups} |
| 437 |
onRefresh={props.onRefresh} |
| 438 |
fromLabel={"Host group"} |
| 439 |
unsetCategory={ |
| 440 |
ipaObject.hostcategory !== |
| 441 |
props.originalRule.hostcategory && |
| 442 |
ipaObject.hostcategory === "" |
| 443 |
} |
| 444 |
/> |
| 445 |
</Tab> |
| 446 |
</Tabs> |
| 447 |
)} |
| 448 |
<Content key="servicecategory"> |
| 449 |
<Title |
| 450 |
headingLevel="h2" |
| 451 |
id="servicecategory" |
| 452 |
className="pf-v6-u-mt-xl pf-v6-u-display-flex" |
| 453 |
> |
| 454 |
Via the following services |
| 455 |
<IpaCheckbox |
| 456 |
dataCy="hbac-rules-tab-settings-checkbox-servicecategory" |
| 457 |
name="servicecategory" |
| 458 |
value="servicecategory" |
| 459 |
text="Any service" |
| 460 |
textNode={howLabel} |
| 461 |
className="pf-v6-u-ml-lg pf-v6-u-mt-xs" |
| 462 |
ipaObject={ipaObject} |
| 463 |
onChange={recordOnChange} |
| 464 |
objectName="hbacrule" |
| 465 |
metadata={props.metadata} |
| 466 |
altTrue="all" |
| 467 |
altFalse={""} |
| 468 |
/> |
| 469 |
</Title> |
| 470 |
</Content> |
| 471 |
{ipaObject.servicecategory === "" && ( |
| 472 |
<Tabs |
| 473 |
activeKey={srvTabKey} |
| 474 |
onSelect={handleSrvTabClick} |
| 475 |
className="pf-v6-u-ml-md pf-v6-u-mr-md" |
| 476 |
> |
| 477 |
<Tab |
| 478 |
eventKey={0} |
| 479 |
name="services" |
| 480 |
data-cy="hbac-rules-tab-settings-tab-services" |
| 481 |
title={ |
| 482 |
<TabTitleText> |
| 483 |
Services <Label isCompact>{memberServices.length}</Label> |
| 484 |
</TabTitleText> |
| 485 |
} |
| 486 |
> |
| 487 |
<HBACRulesMemberTable |
| 488 |
from="hbacsvc" |
| 489 |
fromLabel="Service" |
| 490 |
id={cn} |
| 491 |
members={memberServices} |
| 492 |
onRefresh={props.onRefresh} |
| 493 |
unsetCategory={ |
| 494 |
ipaObject.servicecategory !== |
| 495 |
props.originalRule.servicecategory && |
| 496 |
ipaObject.servicecategory === "" |
| 497 |
} |
| 498 |
/> |
| 499 |
</Tab> |
| 500 |
<Tab |
| 501 |
eventKey={1} |
| 502 |
name="servicegroups" |
| 503 |
data-cy="hbac-rules-tab-settings-tab-servicegroups" |
| 504 |
title={ |
| 505 |
<TabTitleText> |
| 506 |
Service groups{" "} |
| 507 |
<Label isCompact>{memberServiceGroups.length}</Label> |
| 508 |
</TabTitleText> |
| 509 |
} |
| 510 |
> |
| 511 |
<HBACRulesMemberTable |
| 512 |
from="hbacsvcgroup" |
| 513 |
fromLabel="Service group" |
| 514 |
id={cn} |
| 515 |
members={memberServiceGroups} |
| 516 |
onRefresh={props.onRefresh} |
| 517 |
unsetCategory={ |
| 518 |
ipaObject.servicecategory !== |
| 519 |
props.originalRule.servicecategory && |
| 520 |
ipaObject.servicecategory === "" |
| 521 |
} |
| 522 |
/> |
| 523 |
</Tab> |
| 524 |
</Tabs> |
| 525 |
)} |
| 526 |
</Flex> |
| 527 |
</SidebarContent> |
| 528 |
</Sidebar> |
| 529 |
</TabLayout> |
| 530 |
); |
| 531 |
}; |
| 532 |
|
| 533 |
export default HBACRulesSettings; |