| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import React, { useState } from "react"; |
| 8 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 9 |
import { ClipboardCopy, ClipboardCopyVariant } from "@patternfly/react-core/dist/esm/components/ClipboardCopy/index.js"; |
| 10 |
import { DescriptionList, DescriptionListDescription, DescriptionListGroup, DescriptionListTerm } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js"; |
| 11 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 12 |
import { ActionGroup, Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 13 |
import { HelperText, HelperTextItem } from "@patternfly/react-core/dist/esm/components/HelperText/index.js"; |
| 14 |
import { |
| 15 |
Modal, ModalBody, ModalFooter, ModalHeader |
| 16 |
} from '@patternfly/react-core/dist/esm/components/Modal/index.js'; |
| 17 |
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js"; |
| 18 |
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 19 |
import { Switch } from "@patternfly/react-core/dist/esm/components/Switch/index.js"; |
| 20 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 21 |
import { InfoCircleIcon } from '@patternfly/react-icons'; |
| 22 |
|
| 23 |
import * as credentials from "credentials"; |
| 24 |
import { ListingPanel } from 'cockpit-components-listing-panel.jsx'; |
| 25 |
import { ListingTable } from 'cockpit-components-table.jsx'; |
| 26 |
import { ModalError } from 'cockpit-components-inline-notification.jsx'; |
| 27 |
import { useEvent, useObject } from 'hooks'; |
| 28 |
import { WithDialogs, useDialogs, DialogResult } from "dialogs"; |
| 29 |
import { FileChooser } from "cockpit/react/FileChooser"; |
| 30 |
|
| 31 |
import "./credentials.scss"; |
| 32 |
|
| 33 |
const _ = cockpit.gettext; |
| 34 |
|
| 35 |
const KeyDetails = ({ currentKey } : { currentKey: credentials.Key }) => { |
| 36 |
return ( |
| 37 |
<DescriptionList className="pf-m-horizontal-on-sm"> |
| 38 |
<DescriptionListGroup> |
| 39 |
<DescriptionListTerm>{_("Comment")}</DescriptionListTerm> |
| 40 |
<DescriptionListDescription data-name="comment">{currentKey.comment}</DescriptionListDescription> |
| 41 |
</DescriptionListGroup> |
| 42 |
<DescriptionListGroup> |
| 43 |
<DescriptionListTerm>{_("Type")}</DescriptionListTerm> |
| 44 |
<DescriptionListDescription data-name="type">{currentKey.type}</DescriptionListDescription> |
| 45 |
</DescriptionListGroup> |
| 46 |
<DescriptionListGroup> |
| 47 |
<DescriptionListTerm>{_("Fingerprint")}</DescriptionListTerm> |
| 48 |
<DescriptionListDescription data-name="fingerprint">{currentKey.fingerprint}</DescriptionListDescription> |
| 49 |
</DescriptionListGroup> |
| 50 |
</DescriptionList> |
| 51 |
); |
| 52 |
}; |
| 53 |
|
| 54 |
const PublicKey = ({ currentKey } : { currentKey: credentials.Key }) => { |
| 55 |
return ( |
| 56 |
<ClipboardCopy isReadOnly hoverTip={_("Copy")} clickTip={_("Copied")} variant={ClipboardCopyVariant.expansion}> |
| 57 |
{currentKey.data.trim()} |
| 58 |
</ClipboardCopy> |
| 59 |
); |
| 60 |
}; |
| 61 |
|
| 62 |
const KeyPassword = ({ |
| 63 |
currentKey, |
| 64 |
keys, |
| 65 |
setDialogError |
| 66 |
} : { |
| 67 |
currentKey: credentials.Key, |
| 68 |
keys: credentials.Keys, |
| 69 |
setDialogError: (msg: string | null) => void |
| 70 |
}) => { |
| 71 |
const [confirmPassword, setConfirmPassword] = useState(''); |
| 72 |
const [inProgress, setInProgress] = useState<boolean | undefined>(undefined); |
| 73 |
const [newPassword, setNewPassword] = useState(''); |
| 74 |
const [oldPassword, setOldPassword] = useState(''); |
| 75 |
|
| 76 |
function changePassword() { |
| 77 |
if (!currentKey || !currentKey.name) |
| 78 |
return; |
| 79 |
|
| 80 |
setInProgress(true); |
| 81 |
setDialogError(null); |
| 82 |
|
| 83 |
if (oldPassword === undefined || newPassword === undefined || confirmPassword === undefined) |
| 84 |
setDialogError("Invalid password fields"); |
| 85 |
|
| 86 |
keys.change(currentKey.name, oldPassword, newPassword) |
| 87 |
.then(() => { |
| 88 |
setOldPassword(''); |
| 89 |
setNewPassword(''); |
| 90 |
setConfirmPassword(''); |
| 91 |
setInProgress(false); |
| 92 |
}) |
| 93 |
.catch(ex => { |
| 94 |
setDialogError(ex.message); |
| 95 |
setInProgress(undefined); |
| 96 |
}); |
| 97 |
} |
| 98 |
|
| 99 |
const changePasswordBtn = ( |
| 100 |
<Button variant="primary" |
| 101 |
id={(currentKey.name || currentKey.comment) + "-change-password"} |
| 102 |
{... inProgress !== undefined ? { isDisabled: inProgress, isLoading: inProgress } : {} } |
| 103 |
onClick={() => changePassword()}>{_("Change password")}</Button> |
| 104 |
); |
| 105 |
|
| 106 |
return ( |
| 107 |
<Form onSubmit={e => { e.preventDefault(); return false }} isHorizontal> |
| 108 |
{inProgress === false && <HelperText> |
| 109 |
<HelperTextItem variant="success"> |
| 110 |
{_("Password changed successfully")} |
| 111 |
</HelperTextItem> |
| 112 |
</HelperText>} |
| 113 |
<FormGroup label={_("Password")} fieldId={(currentKey.name || currentKey.comment) + "-old-password"} type="password"> |
| 114 |
<TextInput type="password" id={(currentKey.name || currentKey.comment) + "-old-password"} value={oldPassword} onChange={(_event, value) => setOldPassword(value)} /> |
| 115 |
</FormGroup> |
| 116 |
<FormGroup label={_("New password")} fieldId={(currentKey.name || currentKey.comment) + "-new-password"} type="password"> |
| 117 |
<TextInput type="password" id={(currentKey.name || currentKey.comment) + "-new-password"} value={newPassword} onChange={(_event, value) => setNewPassword(value)} /> |
| 118 |
</FormGroup> |
| 119 |
<FormGroup label={_("Confirm password")} fieldId={(currentKey.name || currentKey.comment) + "-confirm-password"} type="password"> |
| 120 |
<TextInput type="password" id={(currentKey.name || currentKey.comment) + "-confirm-password"} value={confirmPassword} onChange={(_event, value) => setConfirmPassword(value)} /> |
| 121 |
</FormGroup> |
| 122 |
<ActionGroup> |
| 123 |
<Flex spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}> |
| 124 |
<Popover |
| 125 |
aria-label={_("Password tip")} |
| 126 |
bodyContent={_("Tip: Make your key password match your login password to automatically authenticate against other systems.")} |
| 127 |
> |
| 128 |
<button className="pf-v6-c-form__group-label-help ct-icon-info-circle" |
| 129 |
onClick={e => e.preventDefault()} |
| 130 |
type="button"> |
| 131 |
<InfoCircleIcon /> |
| 132 |
</button> |
| 133 |
</Popover> |
| 134 |
{changePasswordBtn} |
| 135 |
</Flex> |
| 136 |
</ActionGroup> |
| 137 |
</Form> |
| 138 |
); |
| 139 |
}; |
| 140 |
|
| 141 |
const UnlockKey = ({ |
| 142 |
keyName, |
| 143 |
keys, |
| 144 |
onClose |
| 145 |
} : { |
| 146 |
keyName: string, |
| 147 |
keys: credentials.Keys, |
| 148 |
onClose: () => void |
| 149 |
}) => { |
| 150 |
const [password, setPassword] = useState(""); |
| 151 |
const [dialogError, setDialogError] = useState(""); |
| 152 |
|
| 153 |
function load_key() { |
| 154 |
if (!keyName) |
| 155 |
return; |
| 156 |
|
| 157 |
keys.load(keyName, password) |
| 158 |
.then(onClose) |
| 159 |
.catch(ex => { |
| 160 |
setDialogError(ex.message); |
| 161 |
console.warn("loading key failed: ", ex.message); |
| 162 |
}); |
| 163 |
} |
| 164 |
|
| 165 |
return ( |
| 166 |
<Modal isOpen position="top" variant="small" |
| 167 |
onClose={onClose}> |
| 168 |
<ModalHeader title={cockpit.format(_("Unlock key $0"), keyName)} /> |
| 169 |
<ModalBody> |
| 170 |
<Form onSubmit={e => { e.preventDefault(); return false }} isHorizontal> |
| 171 |
{dialogError && <ModalError dialogError={dialogError} />} |
| 172 |
<FormGroup label={_("Password")} fieldId={keyName + "-password"} type="password"> |
| 173 |
<TextInput type="password" id={keyName + "-password"} value={password} onChange={(_event, value) => setPassword(value)} /> |
| 174 |
</FormGroup> |
| 175 |
</Form> |
| 176 |
</ModalBody> |
| 177 |
<ModalFooter> |
| 178 |
<Button variant="primary" id={keyName + "-unlock"} isDisabled={!keyName} onClick={load_key}>{_("Unlock")}</Button> |
| 179 |
<Button variant='link' onClick={onClose}>{_("Cancel")}</Button> |
| 180 |
</ModalFooter> |
| 181 |
</Modal> |
| 182 |
); |
| 183 |
}; |
| 184 |
|
| 185 |
const AddKeyButton = ({ |
| 186 |
callback |
| 187 |
} : { |
| 188 |
callback: (path: string) => Promise<void>, |
| 189 |
}) => { |
| 190 |
const Dialogs = useDialogs(); |
| 191 |
|
| 192 |
return ( |
| 193 |
<Button |
| 194 |
variant='secondary' |
| 195 |
id="ssh-file-add-custom" |
| 196 |
onClick={ |
| 197 |
async () => { |
| 198 |
const user = await cockpit.user(); |
| 199 |
Dialogs.show( |
| 200 |
<FileChooser |
| 201 |
title={_("Add private SSH key")} |
| 202 |
action={callback} |
| 203 |
actionLabel={_("Add key")} |
| 204 |
path={user.home + "/.ssh"} |
| 205 |
/> |
| 206 |
); |
| 207 |
} |
| 208 |
} |
| 209 |
> |
| 210 |
{_("Add key")} |
| 211 |
</Button> |
| 212 |
); |
| 213 |
}; |
| 214 |
|
| 215 |
export const CredentialsModal = ({ |
| 216 |
dialogResult |
| 217 |
} : { |
| 218 |
dialogResult: DialogResult<void> |
| 219 |
}) => { |
| 220 |
const keys = useObject(() => credentials.keys_instance(), null, []); |
| 221 |
const [dialogError, setDialogError] = useState(); |
| 222 |
const [unlockKey, setUnlockKey] = useState<string | undefined>(); |
| 223 |
|
| 224 |
useEvent(keys as unknown as cockpit.EventSource<cockpit.EventMap>, "changed"); |
| 225 |
|
| 226 |
if (!keys) |
| 227 |
return null; |
| 228 |
|
| 229 |
function onToggleKey(id: string, enable: boolean) { |
| 230 |
const key = keys.items[id]; |
| 231 |
|
| 232 |
if (!key || !key.name) |
| 233 |
return; |
| 234 |
|
| 235 |
/* Key needs to be loaded, show load UI */ |
| 236 |
if (enable && !key.loaded) { |
| 237 |
setUnlockKey(key.name); |
| 238 |
/* Key needs to be unloaded, do that directly */ |
| 239 |
} else if (!enable && key.loaded) { |
| 240 |
keys.unload(key).catch(ex => setDialogError(ex.message)); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
const addCustomKey = async (newKeyPath: string): Promise<void> => { |
| 245 |
try { |
| 246 |
await keys.load(newKeyPath, ""); |
| 247 |
} catch (ex) { |
| 248 |
if (!(ex instanceof credentials.KeyLoadError) || !ex.sent_password) |
| 249 |
throw ex; |
| 250 |
else |
| 251 |
setUnlockKey(newKeyPath); |
| 252 |
} |
| 253 |
}; |
| 254 |
|
| 255 |
return ( |
| 256 |
<> |
| 257 |
<Modal isOpen position="top" variant="medium" |
| 258 |
onClose={() => dialogResult.resolve()} |
| 259 |
id="credentials-modal" |
| 260 |
> |
| 261 |
<ModalHeader title={_("SSH keys")} /> |
| 262 |
<ModalBody> |
| 263 |
<Stack hasGutter> |
| 264 |
{dialogError && <ModalError dialogError={dialogError} />} |
| 265 |
<Flex justifyContent={{ default: 'justifyContentSpaceBetween' }}> |
| 266 |
<FlexItem>{_("Use the following keys to authenticate against other systems")}</FlexItem> |
| 267 |
<WithDialogs> |
| 268 |
<AddKeyButton callback={addCustomKey} /> |
| 269 |
</WithDialogs> |
| 270 |
</Flex> |
| 271 |
<ListingTable |
| 272 |
aria-label={ _("SSH keys") } |
| 273 |
gridBreakPoint='' |
| 274 |
id="credential-keys" |
| 275 |
showHeader={false} |
| 276 |
variant="compact" |
| 277 |
columns={ [ |
| 278 |
{ title: _("Name"), header: true }, |
| 279 |
{ title: _("Toggle") }, |
| 280 |
] } |
| 281 |
rows={ Object.keys(keys.items).map((currentKeyId, index) => { |
| 282 |
const currentKey = keys.items[currentKeyId] || { name: 'test' }; |
| 283 |
const tabRenderers = [ |
| 284 |
{ |
| 285 |
data: { currentKey }, |
| 286 |
name: _("Details"), |
| 287 |
renderer: KeyDetails, |
| 288 |
}, |
| 289 |
{ |
| 290 |
data: { currentKey }, |
| 291 |
name: _("Public key"), |
| 292 |
renderer: PublicKey, |
| 293 |
}, |
| 294 |
{ |
| 295 |
data: { currentKey, keys, setDialogError }, |
| 296 |
name: _("Password"), |
| 297 |
renderer: KeyPassword, |
| 298 |
}, |
| 299 |
]; |
| 300 |
const expandedContent = ( |
| 301 |
<ListingPanel tabRenderers={tabRenderers} /> |
| 302 |
); |
| 303 |
|
| 304 |
return ({ |
| 305 |
columns: [ |
| 306 |
{ |
| 307 |
title: currentKey.name || currentKey.comment, |
| 308 |
}, |
| 309 |
{ |
| 310 |
title: <Switch aria-label={_("Use key")} |
| 311 |
isChecked={!!currentKey.loaded} |
| 312 |
key={"switch-" + index} |
| 313 |
onChange={(_event, value) => onToggleKey(currentKeyId, value)} />, |
| 314 |
props: { |
| 315 |
isActionCell: true |
| 316 |
} |
| 317 |
} |
| 318 |
], |
| 319 |
expandedContent, |
| 320 |
props: { key: currentKey.fingerprint, 'data-name': currentKey.name || currentKey.comment, 'data-loaded': !!currentKey.loaded }, |
| 321 |
}); |
| 322 |
})} /> |
| 323 |
</Stack> |
| 324 |
</ModalBody> |
| 325 |
<ModalFooter> |
| 326 |
<Button variant='secondary' onClick={() => dialogResult.resolve()}>{_("Close")}</Button> |
| 327 |
</ModalFooter> |
| 328 |
</Modal> |
| 329 |
{unlockKey && <UnlockKey keyName={unlockKey} keys={keys} onClose={() => setUnlockKey(undefined)} />} |
| 330 |
</> |
| 331 |
); |
| 332 |
}; |