|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { useContext, useEffect, useState } from "react"; |
| 8 | +import { PageWithSubMenu } from "../components/PageWithSubMenu"; |
| 9 | +import getSettingsMenu from "./settings-menu"; |
| 10 | +import { PaymentContext } from "../payment-context"; |
| 11 | +import Modal from "../components/Modal"; |
| 12 | +import Alert from "../components/Alert"; |
| 13 | +import { Item, ItemField, ItemFieldContextMenu } from "../components/ItemsList"; |
| 14 | +import ConfirmationModal from "../components/ConfirmationModal"; |
| 15 | +import { SSHPublicKeyValue, UserSSHPublicKeyValue } from "@gitpod/gitpod-protocol"; |
| 16 | +import { getGitpodService } from "../service/service"; |
| 17 | + |
| 18 | +interface AddModalProps { |
| 19 | + value: SSHPublicKeyValue; |
| 20 | + onClose: () => void; |
| 21 | + onSave: () => void; |
| 22 | +} |
| 23 | + |
| 24 | +interface DeleteModalProps { |
| 25 | + value: KeyData; |
| 26 | + onConfirm: () => void; |
| 27 | + onClose: () => void; |
| 28 | +} |
| 29 | + |
| 30 | +type KeyData = SSHPublicKeyValue & { id?: string }; |
| 31 | + |
| 32 | +export function AddSSHKeyModal(props: AddModalProps) { |
| 33 | + const [errorMsg, setErrorMsg] = useState(""); |
| 34 | + |
| 35 | + const [value, setValue] = useState({ ...props.value }); |
| 36 | + const update = (pev: Partial<SSHPublicKeyValue>) => { |
| 37 | + setValue({ ...value, ...pev }); |
| 38 | + setErrorMsg(""); |
| 39 | + }; |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + setValue({ ...props.value }); |
| 43 | + setErrorMsg(""); |
| 44 | + }, [props.value]); |
| 45 | + |
| 46 | + const save = async () => { |
| 47 | + const tmp = SSHPublicKeyValue.validate(value); |
| 48 | + if (tmp) { |
| 49 | + setErrorMsg(tmp); |
| 50 | + return false; |
| 51 | + } |
| 52 | + try { |
| 53 | + await getGitpodService().server.addSSHPublicKey(value); |
| 54 | + } catch (e) { |
| 55 | + setErrorMsg(e.message); |
| 56 | + return false; |
| 57 | + } |
| 58 | + props.onClose(); |
| 59 | + props.onSave(); |
| 60 | + return true; |
| 61 | + }; |
| 62 | + |
| 63 | + return ( |
| 64 | + <Modal |
| 65 | + title="New SSH Key" |
| 66 | + buttons={ |
| 67 | + <button className="ml-2" onClick={save}> |
| 68 | + Add Key |
| 69 | + </button> |
| 70 | + } |
| 71 | + visible={true} |
| 72 | + onClose={props.onClose} |
| 73 | + onEnter={save} |
| 74 | + > |
| 75 | + <> |
| 76 | + {errorMsg.length > 0 && ( |
| 77 | + <Alert type="error" className="mb-2"> |
| 78 | + {errorMsg} |
| 79 | + </Alert> |
| 80 | + )} |
| 81 | + </> |
| 82 | + <div className="text-gray-500 dark:text-gray-400 text-md"> |
| 83 | + Add an SSH key for secure access workspaces via SSH. Learn more |
| 84 | + </div> |
| 85 | + <Alert type="info" className="mt-2"> |
| 86 | + SSH key are used to connect securely to workspaces.{" "} |
| 87 | + <a |
| 88 | + href="https://www.gitpod.io/docs/configure/ssh#create-an-ssh-key" |
| 89 | + target="gitpod-create-ssh-key-doc" |
| 90 | + className="gp-link" |
| 91 | + > |
| 92 | + Learn how to create an SSH Key |
| 93 | + </a> |
| 94 | + </Alert> |
| 95 | + <div className="mt-2"> |
| 96 | + <h4>Key</h4> |
| 97 | + <textarea |
| 98 | + autoFocus |
| 99 | + style={{ height: "160px" }} |
| 100 | + className="w-full resize-none" |
| 101 | + value={value.key} |
| 102 | + placeholder="Begins with 'ssh-rsa', 'ecdsa-sha2-nistp256', |
| 103 | + 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521', |
| 104 | + 'ssh-ed25519', |
| 105 | + |
| 106 | + |
| 107 | + onChange={(v) => update({ key: v.target.value })} |
| 108 | + /> |
| 109 | + </div> |
| 110 | + <div className="mt-2"> |
| 111 | + <h4>Title</h4> |
| 112 | + <input |
| 113 | + className="w-full" |
| 114 | + type="text" |
| 115 | + placeholder="e.g. laptop" |
| 116 | + value={value.name} |
| 117 | + onChange={(v) => { |
| 118 | + update({ name: v.target.value }); |
| 119 | + }} |
| 120 | + /> |
| 121 | + </div> |
| 122 | + </Modal> |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +export function DeleteSSHKeyModal(props: DeleteModalProps) { |
| 127 | + const confirmDelete = async () => { |
| 128 | + await getGitpodService().server.deleteSSHPublicKey(props.value.id!); |
| 129 | + props.onConfirm(); |
| 130 | + props.onClose(); |
| 131 | + }; |
| 132 | + return ( |
| 133 | + <ConfirmationModal |
| 134 | + title="Delete SSH Key" |
| 135 | + areYouSureText="This action CANNOT be undone. This will permanently delete the SSH key and if you'd like to use it in the future, you will need to upload it again." |
| 136 | + buttonText="Delete SSH Key" |
| 137 | + onClose={props.onClose} |
| 138 | + onConfirm={confirmDelete} |
| 139 | + /> |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +export default function SSHKeys() { |
| 144 | + const { showPaymentUI, showUsageBasedUI } = useContext(PaymentContext); |
| 145 | + |
| 146 | + const [dataList, setDataList] = useState<UserSSHPublicKeyValue[]>([]); |
| 147 | + const [currentData, setCurrentData] = useState<KeyData>({ name: "", key: "" }); |
| 148 | + const [showAddModal, setShowAddModal] = useState(false); |
| 149 | + const [showDelModal, setShowDelModal] = useState(false); |
| 150 | + |
| 151 | + const loadData = () => { |
| 152 | + getGitpodService() |
| 153 | + .server.getSSHPublicKeys() |
| 154 | + .then((r) => setDataList(r)); |
| 155 | + }; |
| 156 | + |
| 157 | + useEffect(() => { |
| 158 | + loadData(); |
| 159 | + }, []); |
| 160 | + |
| 161 | + const addOne = () => { |
| 162 | + setCurrentData({ name: "", key: "" }); |
| 163 | + setShowAddModal(true); |
| 164 | + setShowDelModal(false); |
| 165 | + }; |
| 166 | + |
| 167 | + const deleteOne = (value: UserSSHPublicKeyValue) => { |
| 168 | + setCurrentData({ id: value.id, name: value.name, key: "" }); |
| 169 | + setShowAddModal(false); |
| 170 | + setShowDelModal(true); |
| 171 | + }; |
| 172 | + |
| 173 | + return ( |
| 174 | + <PageWithSubMenu |
| 175 | + subMenu={getSettingsMenu({ showPaymentUI, showUsageBasedUI })} |
| 176 | + title="SSH Keys" |
| 177 | + subtitle="Connect securely to workspaces." |
| 178 | + > |
| 179 | + {showAddModal && ( |
| 180 | + <AddSSHKeyModal value={currentData} onSave={loadData} onClose={() => setShowAddModal(false)} /> |
| 181 | + )} |
| 182 | + {showDelModal && ( |
| 183 | + <DeleteSSHKeyModal value={currentData} onConfirm={loadData} onClose={() => setShowDelModal(false)} /> |
| 184 | + )} |
| 185 | + <div className="flex items-start sm:justify-between mb-2"> |
| 186 | + <div> |
| 187 | + <h3>SSH Keys</h3> |
| 188 | + <h2 className="text-gray-500"> |
| 189 | + Create and manage SSH keys.{" "} |
| 190 | + <a href="https://www.gitpod.io/docs/configure/ssh" target="gitpod-ssh-doc" className="gp-link"> |
| 191 | + More details |
| 192 | + </a> |
| 193 | + </h2> |
| 194 | + </div> |
| 195 | + {dataList.length !== 0 ? ( |
| 196 | + <div className="mt-3 flex"> |
| 197 | + <button onClick={addOne} className="ml-2"> |
| 198 | + New SSH Key |
| 199 | + </button> |
| 200 | + </div> |
| 201 | + ) : null} |
| 202 | + </div> |
| 203 | + {dataList.length === 0 ? ( |
| 204 | + <div className="bg-gray-100 dark:bg-gray-800 rounded-xl w-full h-96"> |
| 205 | + <div className="pt-28 flex flex-col items-center w-120 m-auto"> |
| 206 | + <h3 className="text-center pb-3 text-gray-500 dark:text-gray-400">No SSH Keys</h3> |
| 207 | + <div className="text-center pb-6 text-gray-500"> |
| 208 | + SSH keys allow you to establish a <b>secure connection</b> between your <b>computer</b> and{" "} |
| 209 | + <b>workspaces</b>. |
| 210 | + </div> |
| 211 | + <button onClick={addOne}>New SSH Key</button> |
| 212 | + </div> |
| 213 | + </div> |
| 214 | + ) : ( |
| 215 | + <div className="grid grid-cols-1 lg:grid-cols-2 gap-4 mt-4"> |
| 216 | + {dataList.map((key) => { |
| 217 | + return ( |
| 218 | + <Item solid className="items-start"> |
| 219 | + <ItemField className="flex flex-col gap-y box-border overflow-hidden"> |
| 220 | + <p className="truncate text-gray-400 dark:text-gray-600"> |
| 221 | + {key.fingerprint |
| 222 | + .toUpperCase() |
| 223 | + .match(/.{1,2}/g) |
| 224 | + ?.join(":") ?? key.fingerprint} |
| 225 | + </p> |
| 226 | + <div className="truncate my-1 text-xl text-gray-800 dark:text-gray-100 font-semibold"> |
| 227 | + {key.name} |
| 228 | + </div> |
| 229 | + <p className="truncate mt-4">Added on {key.creationTime}</p> |
| 230 | + {!!key.lastUsedTime && <p className="truncate">Last used on {key.lastUsedTime}</p>} |
| 231 | + </ItemField> |
| 232 | + <ItemFieldContextMenu |
| 233 | + position="start" |
| 234 | + menuEntries={[ |
| 235 | + { |
| 236 | + title: "Delete", |
| 237 | + customFontStyle: |
| 238 | + "text-red-600 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300", |
| 239 | + onClick: () => deleteOne(key), |
| 240 | + }, |
| 241 | + ]} |
| 242 | + /> |
| 243 | + </Item> |
| 244 | + ); |
| 245 | + })} |
| 246 | + </div> |
| 247 | + )} |
| 248 | + </PageWithSubMenu> |
| 249 | + ); |
| 250 | +} |
0 commit comments