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

Commit

Permalink
Validate SSH keys list before view/delete
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Vinokur <ivinokur@redhat.com>
  • Loading branch information
vinokurig committed Oct 2, 2019
1 parent 510b458 commit 5009b5b
Showing 1 changed file with 29 additions and 7 deletions.
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) {
theia.window.showWarningMessage('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) {
theia.window.showWarningMessage('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

0 comments on commit 5009b5b

Please sign in to comment.