| 1 |
import React, { useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Button, |
| 5 |
Content, |
| 6 |
DropdownItem, |
| 7 |
Flex, |
| 8 |
JumpLinks, |
| 9 |
JumpLinksItem, |
| 10 |
Sidebar, |
| 11 |
SidebarContent, |
| 12 |
SidebarPanel, |
| 13 |
} from "@patternfly/react-core"; |
| 14 |
// Data types |
| 15 |
import { Host, Metadata } from "src/utils/datatypes/globalDataTypes"; |
| 16 |
// Redux |
| 17 |
import { useAppDispatch } from "src/store/hooks"; |
| 18 |
// Hooks |
| 19 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 20 |
// Errors |
| 21 |
import { FetchBaseQueryError } from "@reduxjs/toolkit/query"; |
| 22 |
import { SerializedError } from "@reduxjs/toolkit"; |
| 23 |
import useApiError from "src/hooks/useApiError"; |
| 24 |
import ModalErrors from "src/components/errors/ModalErrors"; |
| 25 |
// Modals |
| 26 |
import HostSetPassword from "src/components/modals/HostModals/HostSetPassword"; |
| 27 |
import IssueNewCertificate from "src/components/modals/CertificateModals/IssueNewCertificate"; |
| 28 |
import ConfirmationModal from "src/components/modals/ConfirmationModal"; |
| 29 |
// Layouts |
| 30 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 31 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 32 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 33 |
import KebabLayout from "src/components/layouts/KebabLayout"; |
| 34 |
import ModalWithFormLayout from "src/components/layouts/ModalWithFormLayout"; |
| 35 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 36 |
// Field sections |
| 37 |
import HostSettings from "src/components/HostsSections/HostSettings"; |
| 38 |
import Enrollment from "src/components/HostsSections/Enrollment"; |
| 39 |
import HostCertificate from "src/components/HostsSections/HostCertificate"; |
| 40 |
import AllowedRetrieveKeytab from "src/components/HostsSections/AllowedRetrieveKeytab"; |
| 41 |
import AllowedCreateKeytab from "src/components/HostsSections/AllowedCreateKeytab"; |
| 42 |
// RPC |
| 43 |
import { ErrorResult } from "src/services/rpc"; |
| 44 |
import { |
| 45 |
useSaveHostMutation, |
| 46 |
useAutoMemberRebuildHostsMutation, |
| 47 |
useUnprovisionHostMutation, |
| 48 |
} from "src/services/rpcHosts"; |
| 49 |
|
| 50 |
interface PropsToHostsSettings { |
| 51 |
host: Partial<Host>; |
| 52 |
originalHost: Partial<Host>; |
| 53 |
metadata: Metadata; |
| 54 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 55 |
certData: any; |
| 56 |
onHostChange: (host: Partial<Host>) => void; |
| 57 |
onRefresh: () => void; |
| 58 |
isModified: boolean; |
| 59 |
isDataLoading?: boolean; |
| 60 |
modifiedValues: () => Partial<Host>; |
| 61 |
onResetValues: () => void; |
| 62 |
changeFromPage: (from: string) => void; |
| 63 |
onOpenContextualPanel: () => void; |
| 64 |
} |
| 65 |
|
| 66 |
const HostsSettings = (props: PropsToHostsSettings) => { |
| 67 |
const dispatch = useAppDispatch(); |
| 68 |
const modalErrors = useApiError([]); |
| 69 |
|
| 70 |
// API save the host |
| 71 |
const [saveHost] = useSaveHostMutation(); |
| 72 |
|
| 73 |
// Automember rebuild command |
| 74 |
const [executeAutoMemberRebuild] = useAutoMemberRebuildHostsMutation(); |
| 75 |
|
| 76 |
const [executeUnprovisionHost] = useUnprovisionHostMutation(); |
| 77 |
|
| 78 |
// Update page to show correct links info in Contextual panel |
| 79 |
React.useEffect(() => { |
| 80 |
props.changeFromPage("active-users-settings"); |
| 81 |
}, [props.changeFromPage]); |
| 82 |
|
| 83 |
// Kebab |
| 84 |
const [isKebabOpen, setIsKebabOpen] = useState(false); |
| 85 |
const [isSaving, setSaving] = useState(false); |
| 86 |
|
| 87 |
// 'Set password' option |
| 88 |
const [isPasswordModalOpen, setIsPasswordModalOpen] = useState(false); |
| 89 |
|
| 90 |
// 'Rebuild auto membership' modal |
| 91 |
const [isMembershipModalOpen, setIsMembershipModalOpen] = useState(false); |
| 92 |
|
| 93 |
// 'Add certificate' modal |
| 94 |
const [isCertModalOpen, setIsCertModalOpen] = useState(false); |
| 95 |
const onCloseCertModal = () => { |
| 96 |
setIsCertModalOpen(false); |
| 97 |
}; |
| 98 |
|
| 99 |
// Unprovision host modal |
| 100 |
const [modalSpinning, setModalSpinning] = React.useState(false); |
| 101 |
const [isUnprovisionHostModalOpen, setIsUnprovisionHostModalOpen] = |
| 102 |
React.useState(false); |
| 103 |
|
| 104 |
const onCloseUnprovisionHostModal = () => { |
| 105 |
setIsUnprovisionHostModalOpen(false); |
| 106 |
}; |
| 107 |
|
| 108 |
const unprovisionHostModalActions = [ |
| 109 |
<Button |
| 110 |
data-cy="modal-button-unprovision" |
| 111 |
key="unprov-host" |
| 112 |
variant="danger" |
| 113 |
onClick={() => onUnprovisionHost(props.host.fqdn ? props.host.fqdn : "")} |
| 114 |
isDisabled={modalSpinning} |
| 115 |
isLoading={modalSpinning} |
| 116 |
spinnerAriaValueText="Unprovisioning" |
| 117 |
spinnerAriaLabelledBy="Unprovisioning" |
| 118 |
spinnerAriaLabel="Unprovisioning" |
| 119 |
> |
| 120 |
{modalSpinning ? "Unprovisioning" : "Unprovision"} |
| 121 |
</Button>, |
| 122 |
<Button |
| 123 |
data-cy="modal-button-cancel" |
| 124 |
key="cancel" |
| 125 |
variant="link" |
| 126 |
onClick={onCloseUnprovisionHostModal} |
| 127 |
> |
| 128 |
Cancel |
| 129 |
</Button>, |
| 130 |
]; |
| 131 |
|
| 132 |
const onUnprovisionHost = (host: string) => { |
| 133 |
if (host === "") { |
| 134 |
return; |
| 135 |
} |
| 136 |
setModalSpinning(true); |
| 137 |
|
| 138 |
executeUnprovisionHost(host).then((result) => { |
| 139 |
if ("data" in result) { |
| 140 |
const disableError = result.data?.error as |
| 141 |
| FetchBaseQueryError |
| 142 |
| SerializedError; |
| 143 |
|
| 144 |
if (disableError) { |
| 145 |
// alert: error |
| 146 |
let error: string | undefined = ""; |
| 147 |
if ("error" in disableError) { |
| 148 |
error = disableError.error; |
| 149 |
} else if ("message" in disableError) { |
| 150 |
error = disableError.message; |
| 151 |
} |
| 152 |
|
| 153 |
dispatch( |
| 154 |
addAlert({ |
| 155 |
name: "unprovision-error", |
| 156 |
title: error || "Error when unprovisioning host", |
| 157 |
variant: "danger", |
| 158 |
}) |
| 159 |
); |
| 160 |
} else { |
| 161 |
// alert: success |
| 162 |
dispatch( |
| 163 |
addAlert({ |
| 164 |
name: "unprovision--success", |
| 165 |
title: "Host successfully unprovisioned", |
| 166 |
variant: "success", |
| 167 |
}) |
| 168 |
); |
| 169 |
} |
| 170 |
setModalSpinning(false); |
| 171 |
setIsUnprovisionHostModalOpen(false); |
| 172 |
} |
| 173 |
}); |
| 174 |
}; |
| 175 |
|
| 176 |
// [API call] 'Rebuild auto membership' |
| 177 |
const onRebuildAutoMembership = () => { |
| 178 |
// Task can potentially run for a very long time, give feed back that we |
| 179 |
// at least started the task |
| 180 |
dispatch( |
| 181 |
addAlert({ |
| 182 |
name: "rebuild-automember-start", |
| 183 |
title: |
| 184 |
"Starting automember rebuild membership task (this may take a long " + |
| 185 |
"time to complete) ...", |
| 186 |
variant: "info", |
| 187 |
}) |
| 188 |
); |
| 189 |
|
| 190 |
executeAutoMemberRebuild([props.host]).then((result) => { |
| 191 |
if ("data" in result) { |
| 192 |
const automemberError = result.data?.error as |
| 193 |
| FetchBaseQueryError |
| 194 |
| SerializedError; |
| 195 |
|
| 196 |
if (automemberError) { |
| 197 |
// alert: error |
| 198 |
let error: string | undefined = ""; |
| 199 |
if ("error" in automemberError) { |
| 200 |
error = automemberError.error; |
| 201 |
} else if ("message" in automemberError) { |
| 202 |
error = automemberError.message; |
| 203 |
} |
| 204 |
|
| 205 |
dispatch( |
| 206 |
addAlert({ |
| 207 |
name: "rebuild-automember-error", |
| 208 |
title: error || "Error when rebuilding membership", |
| 209 |
variant: "danger", |
| 210 |
}) |
| 211 |
); |
| 212 |
} else { |
| 213 |
// alert: success |
| 214 |
dispatch( |
| 215 |
addAlert({ |
| 216 |
name: "rebuild-automember-success", |
| 217 |
title: "Automember rebuild membership task completed", |
| 218 |
variant: "success", |
| 219 |
}) |
| 220 |
); |
| 221 |
} |
| 222 |
// Hide modal |
| 223 |
setIsMembershipModalOpen(!isMembershipModalOpen); |
| 224 |
} |
| 225 |
}); |
| 226 |
}; |
| 227 |
|
| 228 |
const membershipModalActions: JSX.Element[] = [ |
| 229 |
<Button |
| 230 |
data-cy="modal-button-ok" |
| 231 |
key="rebuild-auto-membership" |
| 232 |
variant="primary" |
| 233 |
onClick={onRebuildAutoMembership} |
| 234 |
form="rebuild-auto-membership-modal" |
| 235 |
> |
| 236 |
OK |
| 237 |
</Button>, |
| 238 |
<Button |
| 239 |
data-cy="modal-button-cancel" |
| 240 |
key="cancel-rebuild-auto-membership" |
| 241 |
variant="link" |
| 242 |
onClick={() => setIsMembershipModalOpen(!isMembershipModalOpen)} |
| 243 |
> |
| 244 |
Cancel |
| 245 |
</Button>, |
| 246 |
]; |
| 247 |
|
| 248 |
const dropdownItems = [ |
| 249 |
<DropdownItem |
| 250 |
key="rebuild auto membership" |
| 251 |
data-cy="hosts-tab-settings-kebab-rebuild-auto-membership" |
| 252 |
component="button" |
| 253 |
onClick={() => setIsMembershipModalOpen(!isMembershipModalOpen)} |
| 254 |
> |
| 255 |
Rebuild auto membership |
| 256 |
</DropdownItem>, |
| 257 |
<DropdownItem |
| 258 |
key="unprovision" |
| 259 |
data-cy="hosts-tab-settings-kebab-unprovision" |
| 260 |
onClick={() => setIsUnprovisionHostModalOpen(true)} |
| 261 |
component="button" |
| 262 |
isDisabled={!props.host.has_keytab} |
| 263 |
> |
| 264 |
Unprovision |
| 265 |
</DropdownItem>, |
| 266 |
<DropdownItem |
| 267 |
key="set-one-time-password" |
| 268 |
data-cy="hosts-tab-settings-kebab-set-one-time-password" |
| 269 |
onClick={() => setIsPasswordModalOpen(true)} |
| 270 |
component="button" |
| 271 |
> |
| 272 |
Set one-time password |
| 273 |
</DropdownItem>, |
| 274 |
<DropdownItem |
| 275 |
key="new certificate" |
| 276 |
component="button" |
| 277 |
data-cy="hosts-tab-settings-kebab-new-certificate" |
| 278 |
onClick={() => setIsCertModalOpen(true)} |
| 279 |
> |
| 280 |
New certificate |
| 281 |
</DropdownItem>, |
| 282 |
]; |
| 283 |
|
| 284 |
const onKebabToggle = () => { |
| 285 |
setIsKebabOpen(!isKebabOpen); |
| 286 |
}; |
| 287 |
|
| 288 |
const onKebabSelect = ( |
| 289 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 290 |
_event: React.MouseEvent<Element, MouseEvent> | undefined |
| 291 |
) => { |
| 292 |
setIsKebabOpen(!isKebabOpen); |
| 293 |
}; |
| 294 |
|
| 295 |
// 'Save' handler method |
| 296 |
const onSave = () => { |
| 297 |
const modifiedValues = props.modifiedValues(); |
| 298 |
modifiedValues.fqdn = props.host.fqdn; |
| 299 |
setSaving(true); |
| 300 |
|
| 301 |
saveHost(modifiedValues).then((response) => { |
| 302 |
if ("data" in response) { |
| 303 |
if (response.data?.result) { |
| 304 |
// Show toast notification: success |
| 305 |
dispatch( |
| 306 |
addAlert({ |
| 307 |
name: "save-success", |
| 308 |
title: "Host modified", |
| 309 |
variant: "success", |
| 310 |
}) |
| 311 |
); |
| 312 |
} else if (response.data?.error) { |
| 313 |
// Show toast notification: error |
| 314 |
const errorMessage = response.data.error as ErrorResult; |
| 315 |
dispatch( |
| 316 |
addAlert({ |
| 317 |
name: "save-error", |
| 318 |
title: errorMessage.message, |
| 319 |
variant: "danger", |
| 320 |
}) |
| 321 |
); |
| 322 |
} |
| 323 |
// Reset values. Disable 'revert' and 'save' buttons |
| 324 |
props.onResetValues(); |
| 325 |
setSaving(false); |
| 326 |
} |
| 327 |
}); |
| 328 |
}; |
| 329 |
|
| 330 |
// 'Revert' handler method |
| 331 |
const onRevert = () => { |
| 332 |
props.onHostChange(props.originalHost); |
| 333 |
dispatch( |
| 334 |
addAlert({ |
| 335 |
name: "revert-success", |
| 336 |
title: "Host data reverted", |
| 337 |
variant: "success", |
| 338 |
}) |
| 339 |
); |
| 340 |
}; |
| 341 |
|
| 342 |
// 'Rebuild auto membership' modal fields: Confirmation question |
| 343 |
const confirmationQuestion = [ |
| 344 |
{ |
| 345 |
id: "question-text", |
| 346 |
pfComponent: ( |
| 347 |
<Content component="p"> |
| 348 |
<b>Warning</b> In case of a high number of users, hosts or groups, the |
| 349 |
rebuild task may require high CPU usage. This can severely impact |
| 350 |
server performance. Typically this only needs to be done once after |
| 351 |
importing raw data into the server. Are you sure you want to rebuild |
| 352 |
the auto memberships? |
| 353 |
</Content> |
| 354 |
), |
| 355 |
}, |
| 356 |
]; |
| 357 |
|
| 358 |
// Toolbar |
| 359 |
const toolbarFields = [ |
| 360 |
{ |
| 361 |
key: 0, |
| 362 |
element: ( |
| 363 |
<SecondaryButton |
| 364 |
dataCy="hosts-tab-settings-button-refresh" |
| 365 |
onClickHandler={props.onRefresh} |
| 366 |
> |
| 367 |
Refresh |
| 368 |
</SecondaryButton> |
| 369 |
), |
| 370 |
}, |
| 371 |
{ |
| 372 |
key: 1, |
| 373 |
element: ( |
| 374 |
<SecondaryButton |
| 375 |
dataCy="hosts-tab-settings-button-revert" |
| 376 |
isDisabled={!props.isModified} |
| 377 |
onClickHandler={onRevert} |
| 378 |
> |
| 379 |
Revert |
| 380 |
</SecondaryButton> |
| 381 |
), |
| 382 |
}, |
| 383 |
{ |
| 384 |
key: 2, |
| 385 |
element: ( |
| 386 |
<SecondaryButton |
| 387 |
dataCy="hosts-tab-settings-button-save" |
| 388 |
isDisabled={!props.isModified || isSaving} |
| 389 |
onClickHandler={onSave} |
| 390 |
isLoading={isSaving} |
| 391 |
spinnerAriaValueText="Saving" |
| 392 |
spinnerAriaLabelledBy="Saving" |
| 393 |
spinnerAriaLabel="Saving" |
| 394 |
> |
| 395 |
{isSaving ? "Saving" : "Save"} |
| 396 |
</SecondaryButton> |
| 397 |
), |
| 398 |
}, |
| 399 |
{ |
| 400 |
key: 3, |
| 401 |
element: ( |
| 402 |
<KebabLayout |
| 403 |
dataCy="hosts-tab-settings-kebab" |
| 404 |
direction={"up"} |
| 405 |
onDropdownSelect={onKebabSelect} |
| 406 |
onKebabToggle={onKebabToggle} |
| 407 |
idKebab="toggle-action-buttons" |
| 408 |
isKebabOpen={isKebabOpen} |
| 409 |
dropdownItems={dropdownItems} |
| 410 |
isDisabled={isSaving} |
| 411 |
/> |
| 412 |
), |
| 413 |
}, |
| 414 |
]; |
| 415 |
|
| 416 |
return ( |
| 417 |
<TabLayout id="settings-page" toolbarItems={toolbarFields}> |
| 418 |
<Sidebar isPanelRight> |
| 419 |
<SidebarPanel variant="sticky"> |
| 420 |
<HelpTextWithIconLayout |
| 421 |
textContent="Help" |
| 422 |
onClick={props.onOpenContextualPanel} |
| 423 |
/> |
| 424 |
<JumpLinks |
| 425 |
isVertical |
| 426 |
label="Jump to section" |
| 427 |
scrollableSelector="#settings-page" |
| 428 |
offset={220} // for masthead |
| 429 |
expandable={{ default: "expandable", md: "nonExpandable" }} |
| 430 |
> |
| 431 |
<JumpLinksItem key={0} href="#host-settings"> |
| 432 |
Host settings |
| 433 |
</JumpLinksItem> |
| 434 |
<JumpLinksItem key={1} href="#enrollment"> |
| 435 |
Enrollment |
| 436 |
</JumpLinksItem> |
| 437 |
<JumpLinksItem key={2} href="#host-certificate"> |
| 438 |
Host certificate |
| 439 |
</JumpLinksItem> |
| 440 |
<JumpLinksItem key={3} href="#allow-retrieve-keytab"> |
| 441 |
Allow to retrieve keytab |
| 442 |
</JumpLinksItem> |
| 443 |
<JumpLinksItem key={4} href="#allow-create-keytab"> |
| 444 |
Allow to create keytab |
| 445 |
</JumpLinksItem> |
| 446 |
</JumpLinks> |
| 447 |
</SidebarPanel> |
| 448 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 449 |
<Flex direction={{ default: "column" }} flex={{ default: "flex_1" }}> |
| 450 |
<TitleLayout |
| 451 |
key={0} |
| 452 |
headingLevel="h2" |
| 453 |
id="host-settings" |
| 454 |
text="Host settings" |
| 455 |
/> |
| 456 |
<HostSettings |
| 457 |
host={props.host} |
| 458 |
metadata={props.metadata} |
| 459 |
onHostChange={props.onHostChange} |
| 460 |
onRefresh={props.onRefresh} |
| 461 |
/> |
| 462 |
<TitleLayout |
| 463 |
key={1} |
| 464 |
headingLevel="h2" |
| 465 |
id="enrollment" |
| 466 |
text="Enrollment" |
| 467 |
/> |
| 468 |
<Enrollment host={props.host} /> |
| 469 |
<TitleLayout |
| 470 |
key={2} |
| 471 |
headingLevel="h2" |
| 472 |
id="host-certificate" |
| 473 |
text="Host certificate" |
| 474 |
/> |
| 475 |
<HostCertificate |
| 476 |
host={props.host} |
| 477 |
metadata={props.metadata} |
| 478 |
onHostChange={props.onHostChange} |
| 479 |
onRefresh={props.onRefresh} |
| 480 |
certData={props.certData} |
| 481 |
/> |
| 482 |
<TitleLayout |
| 483 |
key={3} |
| 484 |
headingLevel="h2" |
| 485 |
id="allow-retrieve-keytab" |
| 486 |
text="Allow to retrieve keytab" |
| 487 |
/> |
| 488 |
<AllowedRetrieveKeytab |
| 489 |
host={props.host} |
| 490 |
onRefresh={props.onRefresh} |
| 491 |
/> |
| 492 |
<TitleLayout |
| 493 |
key={4} |
| 494 |
headingLevel="h2" |
| 495 |
id="allow-create-keytab" |
| 496 |
text="Allow to create keytab" |
| 497 |
/> |
| 498 |
<AllowedCreateKeytab |
| 499 |
host={props.host} |
| 500 |
onRefresh={props.onRefresh} |
| 501 |
/> |
| 502 |
</Flex> |
| 503 |
</SidebarContent> |
| 504 |
</Sidebar> |
| 505 |
<ModalErrors errors={modalErrors.getAll()} dataCy="hosts-modal-error" /> |
| 506 |
{isMembershipModalOpen && ( |
| 507 |
<ModalWithFormLayout |
| 508 |
dataCy="rebuild-auto-membership-modal" |
| 509 |
variantType="medium" |
| 510 |
modalPosition="top" |
| 511 |
offPosition="76px" |
| 512 |
title="Confirmation" |
| 513 |
formId="rebuild-auto-membership-modal" |
| 514 |
fields={confirmationQuestion} |
| 515 |
show={isMembershipModalOpen} |
| 516 |
onClose={() => setIsMembershipModalOpen(!isMembershipModalOpen)} |
| 517 |
actions={membershipModalActions} |
| 518 |
/> |
| 519 |
)} |
| 520 |
<HostSetPassword |
| 521 |
host={props.host.fqdn || ""} |
| 522 |
isOpen={isPasswordModalOpen} |
| 523 |
onClose={() => setIsPasswordModalOpen(false)} |
| 524 |
onRefresh={props.onRefresh} |
| 525 |
/> |
| 526 |
<IssueNewCertificate |
| 527 |
isOpen={isCertModalOpen} |
| 528 |
onClose={onCloseCertModal} |
| 529 |
id={props.host.fqdn} |
| 530 |
showPrincipalFields={false} |
| 531 |
onRefresh={props.onRefresh} |
| 532 |
principal={ |
| 533 |
props.host.krbprincipalname ? props.host.krbprincipalname[0] : "" |
| 534 |
} |
| 535 |
/> |
| 536 |
<ConfirmationModal |
| 537 |
dataCy="unprovision-host-modal" |
| 538 |
title={"Unprovision host"} |
| 539 |
isOpen={isUnprovisionHostModalOpen} |
| 540 |
onClose={onCloseUnprovisionHostModal} |
| 541 |
actions={unprovisionHostModalActions} |
| 542 |
messageText={"Unprovision/disable this host?"} |
| 543 |
messageObj={props.host.fqdn ? props.host.fqdn : ""} |
| 544 |
/> |
| 545 |
</TabLayout> |
| 546 |
); |
| 547 |
}; |
| 548 |
|
| 549 |
export default HostsSettings; |