From 5bacfc408aed995944697609b1e1300772e7570a Mon Sep 17 00:00:00 2001
From: Guillaume Weber <gweber@takima.fr>
Date: Wed, 15 Jun 2022 14:23:00 +0200
Subject: [PATCH] fix: you can change user

---
 .../deadlock-extension/src/core/config.ts     |  3 +-
 .../deadlock-extension/src/core/controller.ts | 15 ++++--
 .../src/core/extensionStore.ts                | 13 +++++
 .../src/core/mission/missionDevContainer.ts   |  4 +-
 .../src/core/sshKeyManager.ts                 | 49 ++++++++++---------
 5 files changed, 53 insertions(+), 31 deletions(-)

diff --git a/deadlock-plugins/deadlock-extension/src/core/config.ts b/deadlock-plugins/deadlock-extension/src/core/config.ts
index 9dbf60ba..9a002fc3 100644
--- a/deadlock-plugins/deadlock-extension/src/core/config.ts
+++ b/deadlock-plugins/deadlock-extension/src/core/config.ts
@@ -8,8 +8,7 @@ const onContainer = isDocker();
 
 const deadlockExtensionPath = path.join(homeDir, 'deadlock-extension');
 
-const deadlockConfigPath = path.join(homeDir, '.deadlock');
-export const userSshKeyFolderPath = path.join(deadlockConfigPath, '.ssh');
+export const deadlockConfigPath = path.join(homeDir, '.deadlock');
 
 export const PROJECT_SRC_PATH = onContainer ? '/project' : path.join(homeDir, 'deadlock-extension', '/project');
 
diff --git a/deadlock-plugins/deadlock-extension/src/core/controller.ts b/deadlock-plugins/deadlock-extension/src/core/controller.ts
index 14597460..13c2961d 100644
--- a/deadlock-plugins/deadlock-extension/src/core/controller.ts
+++ b/deadlock-plugins/deadlock-extension/src/core/controller.ts
@@ -92,10 +92,10 @@ export default class Controller {
     this.quickSetupView.isAlreadyConnected = false;
   }
 
-  public async createSshKeyPairIfNotExist() {
+  public async createSshKeyPairIfNotExist(userId: string) {
     if (isSshKeyPairExist()) return;
     const { publicKey, privateKey } = await this.apiService.getUserSshKey();
-    await createSshKeyFiles(publicKey, privateKey);
+    await createSshKeyFiles(publicKey, privateKey, userId);
   }
 
   public async authenticate() {
@@ -103,8 +103,10 @@ export default class Controller {
     const tokens = await this.connection.getToken({ openLink: Controller.openBrowserWithUrl });
     await this.extensionStore.setAccessToken(tokens.accessToken);
     await this.extensionStore.setRefreshToken(tokens.refreshToken);
-    await this.createSshKeyPairIfNotExist();
+    const user = await this.apiService.getUser();
+    await this.createSshKeyPairIfNotExist(user.id);
     this.quickSetupView.isAlreadyConnected = true;
+    return user;
   }
 
   public static openBrowserWithUrl(url: string) {
@@ -120,15 +122,18 @@ export default class Controller {
 
     const hadBeenConnected = (await this.extensionStore.getAccessToken()) !== undefined;
 
+    let user: User;
+
     if (!hadBeenConnected) {
-      await this.authenticate();
+      user = await this.authenticate();
       window.showInformationMessage('Connexion validée');
+    } else {
+      user = await this.apiService.getUser();
     }
 
     const mission = new Mission(missionId, missionVersion);
 
     const missionsWorkdir = this.extensionStore.getMissionWorkdir() ?? '';
-    const user: User = await this.apiService.getUser();
     const giteaPublicProperties: GiteaPublicProperties = await this.apiService.getGiteaPublicProperties();
 
     const missionDevcontainer = new MissionDevContainer(missionsWorkdir, user, mission, giteaPublicProperties);
diff --git a/deadlock-plugins/deadlock-extension/src/core/extensionStore.ts b/deadlock-plugins/deadlock-extension/src/core/extensionStore.ts
index 9759b5e1..269dbf95 100644
--- a/deadlock-plugins/deadlock-extension/src/core/extensionStore.ts
+++ b/deadlock-plugins/deadlock-extension/src/core/extensionStore.ts
@@ -50,6 +50,10 @@ export default class ExtensionStore {
     return this.readSecret(StoreKey.RefreshTokenKey);
   }
 
+  public getSshFolderPath(): Thenable<string | undefined> {
+    return this.readSecret(StoreKey.SshFolderKey);
+  }
+
   public setAccessToken(accessToken: string): Thenable<void> {
     if (!accessToken) {
       log('Attempt to store undefined access token');
@@ -66,6 +70,14 @@ export default class ExtensionStore {
     return this.storeSecret(StoreKey.RefreshTokenKey, refreshToken);
   }
 
+  public setSshFolderPath(path: string): Thenable<void> {
+    if (!path) {
+      log('Attempt to store undefined path');
+      return Promise.resolve();
+    }
+    return this.storeSecret(StoreKey.SshFolderKey, path);
+  }
+
   private storeSecret(key: StoreKey, value: string): Thenable<void> {
     return this.secretStorage.store(key, value);
   }
@@ -77,6 +89,7 @@ export default class ExtensionStore {
 
 enum StoreKey {
   MissionWorkdirKey = 'mission-workdir-key',
+  SshFolderKey = 'ssh-folder-key',
   AccessTokenKey = 'access-token-key',
   RefreshTokenKey = 'refresh-token-key',
 }
diff --git a/deadlock-plugins/deadlock-extension/src/core/mission/missionDevContainer.ts b/deadlock-plugins/deadlock-extension/src/core/mission/missionDevContainer.ts
index e40df49c..2045c4ad 100644
--- a/deadlock-plugins/deadlock-extension/src/core/mission/missionDevContainer.ts
+++ b/deadlock-plugins/deadlock-extension/src/core/mission/missionDevContainer.ts
@@ -1,4 +1,4 @@
-import { userSshKeyFolderPath } from '../config';
+import { deadlockConfigPath } from './config';
 import { Base, DockerfileSpecific, LifecycleScripts, VSCodespecific } from './devContainer';
 import { mkdir, writeFile } from 'fs/promises';
 import { Mission } from './mission';
@@ -90,7 +90,7 @@ export class MissionDevContainer {
 
     const image = `${this.dockerImageUrl}/${this.mission.id}:${this.mission.version}`;
     this.mounts.push(
-      `source=${userSshKeyFolderPath},target=/tmp/.ssh,type=bind,consistency=cached,readonly`,
+      `source=${join(deadlockConfigPath, this.user.id, '.ssh')},target=/tmp/.ssh,type=bind,consistency=cached,readonly`,
       `source=${this.dirs.config},target=/home/config/,type=bind,consistency=cached,readonly`,
       'source=/etc/hosts,target=/etc/hosts,type=bind,consistency=cached,readonly',
     );
diff --git a/deadlock-plugins/deadlock-extension/src/core/sshKeyManager.ts b/deadlock-plugins/deadlock-extension/src/core/sshKeyManager.ts
index c3fdbb76..f263067d 100644
--- a/deadlock-plugins/deadlock-extension/src/core/sshKeyManager.ts
+++ b/deadlock-plugins/deadlock-extension/src/core/sshKeyManager.ts
@@ -1,30 +1,35 @@
 import { existsSync, promises } from 'fs';
-import { userSshKeyFolderPath } from './config';
+import { deadlockConfigPath } from './config';
+import { join } from 'path';
 
-  export function isSshKeyPairExist(): boolean {
-    return isPrivateKeyExist() && isPublicKeyExist();
-  }
+let currentUserSshKeyFolderPath: string;
 
-  function isPublicKeyExist(): boolean {
-    return existsSync(`${userSshKeyFolderPath}/id_rsa.pub`);
-  }
+export function isSshKeyPairExist(): boolean {
+  return isPrivateKeyExist() && isPublicKeyExist();
+}
 
-  function isPrivateKeyExist(): boolean {
-    return existsSync(`${userSshKeyFolderPath}/id_rsa`);
-  }
+function isPublicKeyExist(): boolean {
+  return existsSync(`${currentUserSshKeyFolderPath}/id_rsa.pub`);
+}
 
-  export async function createSshKeyFiles(publicKey: string, privateKey: string) {
-    await createSshKeyFolderIfNotExist(userSshKeyFolderPath);
-    await promises.writeFile(`${userSshKeyFolderPath}/id_rsa.pub`, publicKey);
+function isPrivateKeyExist(): boolean {
+  return existsSync(`${currentUserSshKeyFolderPath}/id_rsa`);
+}
 
-    await promises.writeFile(`${userSshKeyFolderPath}/id_rsa`, privateKey, { mode: 0o600 });
-  }
-  async function createSshKeyFolderIfNotExist(sshKeyFolderPath) {
-    if (!isSshKeyFolderExist(sshKeyFolderPath)) {
-      await promises.mkdir(sshKeyFolderPath, { recursive: true });
-    }
-  }
+export async function createSshKeyFiles(publicKey: string, privateKey: string, userId: string) {
+  currentUserSshKeyFolderPath = join(deadlockConfigPath, userId, '.ssh');
+  await createSshKeyFolderIfNotExist(currentUserSshKeyFolderPath);
+  await promises.writeFile(`${currentUserSshKeyFolderPath}/id_rsa.pub`, publicKey);
+
+  await promises.writeFile(`${currentUserSshKeyFolderPath}/id_rsa`, privateKey, { mode: 0o600 });
+}
 
-  export function isSshKeyFolderExist(sshKeyFolderPath: string) {
-    return existsSync(sshKeyFolderPath);
+async function createSshKeyFolderIfNotExist(sshKeyFolderPath) {
+  if (!isSshKeyFolderExist(sshKeyFolderPath)) {
+    await promises.mkdir(sshKeyFolderPath, { recursive: true });
   }
+}
+
+function isSshKeyFolderExist(sshKeyFolderPath: string) {
+  return existsSync(sshKeyFolderPath);
+}
-- 
GitLab