Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Validate SSH keys list before view/delete #448

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions plugins/ssh-plugin/src/ssh-plugin-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export async function start() {
});
}

const RESTART_WARNING_MESSAGE = 'Che Git plugin can leverage the generated keys now. To make them available in every workspace containers please restart your workspace.';

const getHostName = async () => {
const hostName = await theia.window.showInputBox({ placeHolder: 'Please provide a Host name e.g. github.com' });
return hostName ? hostName : '';
Expand Down Expand Up @@ -82,8 +84,8 @@ const writeKey = async (name: string, key: string) => {
await chmod(keyFile, '600');
};

const showWarning = async () => {
theia.window.showWarningMessage('Che Git plugin can leverage the generated keys now. To make them available in every workspace containers please restart your workspace.');
const showWarning = async (message: string) => {
theia.window.showWarningMessage(message);
};

const generateKeyPair = async (sshkeyManager: SshKeyManager) => {
Expand All @@ -97,7 +99,7 @@ const generateKeyPair = async (sshkeyManager: SshKeyManager) => {
const document = await theia.workspace.openTextDocument({ content: key.publicKey });
await theia.window.showTextDocument(document);
}
showWarning();
showWarning(RESTART_WARNING_MESSAGE);
};

const generateKeyPairForHost = async (sshkeyManager: SshKeyManager) => {
Expand All @@ -111,7 +113,7 @@ const generateKeyPairForHost = async (sshkeyManager: SshKeyManager) => {
const document = await theia.workspace.openTextDocument({ content: key.publicKey });
await theia.window.showTextDocument(document);
}
showWarning();
showWarning(RESTART_WARNING_MESSAGE);
};

const createKeyPair = async (sshkeyManager: SshKeyManager) => {
Expand All @@ -124,14 +126,28 @@ const createKeyPair = async (sshkeyManager: SshKeyManager) => {
await updateConfig(hostName);
await writeKey(hostName, privateKey);
await theia.window.showInformationMessage(`Key pair for ${hostName} successfully created`);
showWarning();
showWarning(RESTART_WARNING_MESSAGE);
} catch (error) {
theia.window.showErrorMessage(error);
}
};

const getKeys = async (sshKeyManager: SshKeyManager): Promise<cheApi.ssh.SshPair[]> => {
const keys: cheApi.ssh.SshPair[] = await sshKeyManager.getAll('vcs');
if (!keys || keys.length < 1) {
throw new Error('No SSH key pair has been defined.');
}
return keys;
};

const deleteKeyPair = async (sshkeyManager: SshKeyManager) => {
const keys: cheApi.ssh.SshPair[] = await sshkeyManager.getAll('vcs');
let keys: cheApi.ssh.SshPair[];
try {
keys = await getKeys(sshkeyManager);
} catch (error) {
showWarning('Delete SSH key operation is interrupted: ' + error.message);
return;
}
const keyResp = await theia.window.showQuickPick<theia.QuickPickItem>(keys.map(key =>
({ label: key.name ? key.name : '' })), {});
const keyName = keyResp ? keyResp.label : '';
Expand All @@ -150,7 +166,13 @@ const deleteKeyPair = async (sshkeyManager: SshKeyManager) => {
};

const viewPublicKey = async (sshkeyManager: SshKeyManager) => {
const keys: cheApi.ssh.SshPair[] = await sshkeyManager.getAll('vcs');
let keys: cheApi.ssh.SshPair[];
try {
keys = await getKeys(sshkeyManager);
} catch (error) {
showWarning('View public SSH key operation is interrupted: ' + error.message);
return;
}
const keyResp = await theia.window.showQuickPick<theia.QuickPickItem>(keys.map(key =>
({ label: key.name ? key.name : '' })), {});
const keyName = keyResp ? keyResp.label : '';
Expand Down