| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from 'cockpit'; |
| 7 |
import React from 'react'; |
| 8 |
import { useObject, useEvent } from 'hooks.js'; |
| 9 |
|
| 10 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 11 |
import { Card, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js'; |
| 12 |
import { OverflowMenu, OverflowMenuContent, OverflowMenuControl, OverflowMenuDropdownItem, OverflowMenuGroup, OverflowMenuItem } from "@patternfly/react-core/dist/esm/components/OverflowMenu/index.js"; |
| 13 |
import { TextArea } from "@patternfly/react-core/dist/esm/components/TextArea/index.js"; |
| 14 |
import { show_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 15 |
import { ListingTable } from 'cockpit-components-table.jsx'; |
| 16 |
import { KebabDropdown } from 'cockpit-components-dropdown.jsx'; |
| 17 |
import { show_unexpected_error } from "./dialog-utils.js"; |
| 18 |
import * as authorized_keys from './authorized-keys.js'; |
| 19 |
|
| 20 |
const _ = cockpit.gettext; |
| 21 |
|
| 22 |
function AddAuthorizedKeyDialogBody({ state, change }) { |
| 23 |
const { text } = state; |
| 24 |
|
| 25 |
return ( |
| 26 |
<TextArea id="authorized-keys-text" |
| 27 |
placeholder={_("Paste the contents of your public SSH key file here")} |
| 28 |
className="form-control" |
| 29 |
value={text} onChange={(_event, value) => change("text", value)} /> |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
function add_authorized_key_dialog(keys) { |
| 34 |
let dlg = null; |
| 35 |
const state = { |
| 36 |
text: "" |
| 37 |
}; |
| 38 |
|
| 39 |
function change(field, value) { |
| 40 |
state[field] = value; |
| 41 |
update(); |
| 42 |
} |
| 43 |
|
| 44 |
function update() { |
| 45 |
const props = { |
| 46 |
id: "add-authorized-key-dialog", |
| 47 |
title: _("Add public key"), |
| 48 |
body: <AddAuthorizedKeyDialogBody state={state} change={change} /> |
| 49 |
}; |
| 50 |
|
| 51 |
const footer = { |
| 52 |
actions: [ |
| 53 |
{ |
| 54 |
caption: _("Add"), |
| 55 |
style: "primary", |
| 56 |
clicked: () => { |
| 57 |
return keys.add_key(state.text); |
| 58 |
} |
| 59 |
} |
| 60 |
] |
| 61 |
}; |
| 62 |
|
| 63 |
if (!dlg) |
| 64 |
dlg = show_modal_dialog(props, footer); |
| 65 |
else { |
| 66 |
dlg.setProps(props); |
| 67 |
dlg.setFooterProps(footer); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
update(); |
| 72 |
} |
| 73 |
|
| 74 |
export function AuthorizedKeys({ name, home, allow_mods }) { |
| 75 |
const manager = useObject(() => authorized_keys.instance(name, home), |
| 76 |
manager => manager.close(), |
| 77 |
[name, home]); |
| 78 |
useEvent(manager, "changed"); |
| 79 |
|
| 80 |
function remove_key(raw) { |
| 81 |
manager.remove_key(raw).catch(show_unexpected_error); |
| 82 |
} |
| 83 |
|
| 84 |
const { state, keys } = manager; |
| 85 |
let error = ""; |
| 86 |
|
| 87 |
if (state == "access-denied") |
| 88 |
error = _("You do not have permission to view the authorized public keys for this account."); |
| 89 |
else if (state == "failed") |
| 90 |
error = _("Failed to load authorized keys."); |
| 91 |
else if (state == "ready") |
| 92 |
error = _("There are no authorized public keys for this account."); |
| 93 |
else |
| 94 |
return null; |
| 95 |
|
| 96 |
const actions = allow_mods && ( |
| 97 |
<Button variant="secondary" id="authorized-key-add" onClick={() => add_authorized_key_dialog(manager)}> |
| 98 |
{_("Add key")} |
| 99 |
</Button> |
| 100 |
); |
| 101 |
|
| 102 |
return ( |
| 103 |
<Card isPlain id="account-authorized-keys"> |
| 104 |
<CardHeader actions={{ actions }}> |
| 105 |
<CardTitle component="h2">{_("Authorized public SSH keys")}</CardTitle> |
| 106 |
</CardHeader> |
| 107 |
<ListingTable |
| 108 |
aria-label={ _("Authorized public SSH keys") } |
| 109 |
id="account-authorized-keys-list" |
| 110 |
showHeader={false} |
| 111 |
emptyCaption={error} |
| 112 |
columns={ [ |
| 113 |
{ title: _("Name"), header: true }, |
| 114 |
{ title: _("Fingerprint") }, |
| 115 |
{ title: "" }, |
| 116 |
] } |
| 117 |
rows={keys.map(k => { |
| 118 |
return { |
| 119 |
columns: [ |
| 120 |
{ title: k.comment || _("Unnamed"), props: { width: 20 } }, |
| 121 |
{ title: k.fp || _("Invalid key"), props: { width: 80 } }, |
| 122 |
{ |
| 123 |
title: <OverflowMenu breakpoint="lg"> |
| 124 |
<OverflowMenuContent> |
| 125 |
<OverflowMenuGroup groupType="button"> |
| 126 |
<OverflowMenuItem> |
| 127 |
<Button key={k.fp} variant="secondary" onClick={() => remove_key(k.raw)}> |
| 128 |
{_("Remove")} |
| 129 |
</Button> |
| 130 |
</OverflowMenuItem> |
| 131 |
</OverflowMenuGroup> |
| 132 |
</OverflowMenuContent> |
| 133 |
<OverflowMenuControl> |
| 134 |
<KebabDropdown position="right" dropdownItems={[ |
| 135 |
<OverflowMenuDropdownItem key="delete" isShared onClick={() => remove_key(k.raw)}> |
| 136 |
{_("Remove")} |
| 137 |
</OverflowMenuDropdownItem>]} |
| 138 |
/> |
| 139 |
</OverflowMenuControl> |
| 140 |
</OverflowMenu>, |
| 141 |
props: { className: "pf-v6-c-table__action" } |
| 142 |
} |
| 143 |
], |
| 144 |
props: { key: k.fp } |
| 145 |
}; |
| 146 |
})} /> |
| 147 |
</Card> |
| 148 |
); |
| 149 |
} |