diff --git a/launcher/src/local-storage-key-provider.ts b/launcher/src/local-storage-key-provider.ts index f0533a8c2bf..bc6f2db2443 100644 --- a/launcher/src/local-storage-key-provider.ts +++ b/launcher/src/local-storage-key-provider.ts @@ -57,11 +57,11 @@ export class LocalStorageKeyProvider { async getPartOfPublicKey(file: string): Promise { let content = await fs.readFile(file); - content = content.substring(content.indexOf(' ') + 1); + content = content.split(' ')[1]; let secret = ''; for (let i = 0; i < 32; i++) { - secret += content.charAt(i * 4); + secret += content.charAt(i * 2); } return secret; diff --git a/launcher/tests/local-storage-key-provider.spec.ts b/launcher/tests/local-storage-key-provider.spec.ts index f191f5301a8..a376af85d85 100644 --- a/launcher/tests/local-storage-key-provider.spec.ts +++ b/launcher/tests/local-storage-key-provider.spec.ts @@ -16,7 +16,11 @@ some code, some code, a mask to be replaced {{LOCAL-STORAGE}}/{{SECURE-KEY}}, so `; const NEW_WORKBENCH_FILE = ` -some code, some code, a mask to be replaced 1234567890ABCDEFGHIJKLMNOPQRSTUV, some code +some code, some code, a mask to be replaced 11223344556677889900AABBCCDDEEFF, some code +`; + +const NEW_WORKBENCH_FILE_NEW_LINE = ` +some code, some code, a mask to be replaced AACNa1ZINEAAIblpQoqFHrJPOyAt2gX1, some code `; describe('Test setting of Local Storage public key to VS Code', () => { @@ -70,4 +74,47 @@ describe('Test setting of Local Storage public key to VS Code', () => { expect(writeFileMock).toBeCalledWith('out/vs/code/browser/workbench/workbench.js', NEW_WORKBENCH_FILE); }); + + test('should work with a public key including new lines', async () => { + const pathExistsMock = jest.fn(); + const readdirMock = jest.fn(); + const isFileMock = jest.fn(); + const readFileMock = jest.fn(); + const writeFileMock = jest.fn(); + Object.assign(fs, { + pathExists: pathExistsMock, + readdir: readdirMock, + isFile: isFileMock, + readFile: readFileMock, + writeFile: writeFileMock, + }); + + pathExistsMock.mockImplementation(async (path: string) => { + return '/etc/ssh' === path || '/etc/ssh/first-key.pub' === path; + }); + + readdirMock.mockImplementation(async (path: string) => { + return ['some-file', 'first-key', 'second-key', 'first-key.pub', 'second-key.pub']; + }); + + isFileMock.mockImplementation(async (path: string) => { + return '/etc/ssh/first-key' === path || '/etc/ssh/first-key.pub' === path; + }); + + readFileMock.mockImplementation(async (file: string) => { + switch (file) { + case '/etc/ssh/first-key.pub': + // This key has new lines, and was randomly generated for this test + return `ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIObHl2pRQsoCqvF7HrrUJwPcOByIAOtS2ggoX51xMWAQ eclipse@che.com + `; + case 'out/vs/code/browser/workbench/workbench.js': + return ORIGIN_WORKBENCH_FILE; + } + }); + + const localStorageKeyProvider = new LocalStorageKeyProvider(); + await localStorageKeyProvider.configure(); + + expect(writeFileMock).toBeCalledWith('out/vs/code/browser/workbench/workbench.js', NEW_WORKBENCH_FILE_NEW_LINE); + }); });