Skip to content

Commit 08e6be1

Browse files
authored
chore(connection-storage): make sure correct keychain service name is used by connection storage (#4719)
* fix(connection-storage): always get keychain service name in runtime * chore(compass): make sure that all electron app variables are set as the first thing in the app entrypoint
1 parent 057b3a1 commit 08e6be1

File tree

5 files changed

+48
-40
lines changed

5 files changed

+48
-40
lines changed

packages/compass/src/app/index.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// THIS IMPORT SHOULD ALWAYS BE THE FIRST ONE FOR THE APPLICATION ENTRY POINT
2+
import '../setup-hadron-distribution';
3+
14
import dns from 'dns';
25
import ipc from 'hadron-ipc';
36
import * as remote from '@electron/remote';
@@ -25,7 +28,6 @@ window.addEventListener('error', (event) => {
2528
});
2629

2730
import './index.less';
28-
import '../setup-hadron-distribution';
2931
import 'source-code-pro/source-code-pro.css';
3032

3133
import * as marky from 'marky';

packages/compass/src/main/application.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ class CompassApplication {
6969
}
7070
this.mode = mode;
7171

72-
this.setupUserDirectory();
73-
// need to happen after setupUserDirectory
7472
await setupPreferencesAndUser(globalPreferences);
7573
await this.setupLogging();
7674
// need to happen after setupPreferencesAndUser
@@ -190,22 +188,6 @@ class CompassApplication {
190188
});
191189
}
192190

193-
private static setupUserDirectory(): void {
194-
if (process.env.NODE_ENV === 'development') {
195-
const appName = app.getName();
196-
// When NODE_ENV is dev, we are probably running the app unpackaged
197-
// directly with Electron binary which causes user dirs to be just
198-
// `Electron` instead of app name that we want here
199-
app.setPath('userData', path.join(app.getPath('appData'), appName));
200-
201-
// @ts-expect-error this seems to work but not exposed as public path and
202-
// so is not available in d.ts files. As this is a dev-only path and
203-
// seemingly nothing is using this path anyway, we probably can ignore an
204-
// error here
205-
app.setPath('userCache', path.join(app.getPath('cache'), appName));
206-
}
207-
}
208-
209191
private static async setupLogging(): Promise<void> {
210192
const home = app.getPath('home');
211193
const appData = process.env.LOCALAPPDATA || process.env.APPDATA;

packages/compass/src/main/index.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// THIS IMPORT SHOULD ALWAYS BE THE FIRST ONE FOR THE APPLICATION ENTRY POINT
12
import '../setup-hadron-distribution';
3+
24
import { app, dialog } from 'electron';
35
import { handleUncaughtException } from './handle-uncaught-exception';
4-
import { initialize } from '@electron/remote/main';
6+
import { initialize as initializeElectronRemote } from '@electron/remote/main';
57
import {
68
doImportConnections,
79
doExportConnections,
@@ -15,25 +17,10 @@ import chalk from 'chalk';
1517
import { installEarlyLoggingListener } from './logging';
1618
import { installEarlyOpenUrlListener } from './window-manager';
1719

18-
initialize();
20+
initializeElectronRemote();
1921
installEarlyLoggingListener();
2022
installEarlyOpenUrlListener();
2123

22-
// Name and version are setup outside of Application and before anything else so
23-
// that if uncaught exception happens we already show correct name and version
24-
app.setName(process.env.HADRON_PRODUCT_NAME);
25-
// For webdriverio env we are changing appName so that keychain records do not
26-
// overlap with anything else. Only appName should be changed for the webdriverio
27-
// environment that is running tests, all relevant paths are configured from the
28-
// test runner.
29-
if (process.env.APP_ENV === 'webdriverio') {
30-
app.setName(`${app.getName()} Webdriverio`);
31-
}
32-
33-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
34-
// @ts-expect-error setVersion is not a public method
35-
app.setVersion(process.env.HADRON_APP_VERSION);
36-
3724
process.title = `${app.getName()} ${app.getVersion()}`;
3825

3926
void main();

packages/compass/src/setup-hadron-distribution.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import path from 'path';
2+
import { app } from 'electron';
3+
14
/**
25
* All these variables below are used by Compass and its plugins in one way or
36
* another. These process.env vars are inlined in the code durng the build
@@ -27,3 +30,38 @@ const env = Object.fromEntries(
2730
);
2831

2932
Object.assign(process.env, env);
33+
34+
if (
35+
// type `browser` indicates that we are in the main electron process
36+
process.type === 'browser'
37+
) {
38+
// Name and version are setup outside of Application and before anything else
39+
// so that if uncaught exception happens we already show correct name and
40+
// version
41+
app.setName(process.env.HADRON_PRODUCT_NAME);
42+
43+
// For webdriverio env we are changing appName so that keychain records do not
44+
// overlap with anything else. Only appName should be changed for the
45+
// webdriverio environment that is running tests, all relevant paths are
46+
// configured from the test runner.
47+
if (process.env.APP_ENV === 'webdriverio') {
48+
app.setName(`${app.getName()} Webdriverio`);
49+
}
50+
51+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
52+
// @ts-expect-error setVersion is not a public method
53+
app.setVersion(process.env.HADRON_APP_VERSION);
54+
55+
// When NODE_ENV is dev, we are probably running the app unpackaged directly
56+
// with Electron binary which causes user dirs to be just `Electron` instead
57+
// of app name that we want here
58+
if (process.env.NODE_ENV === 'development') {
59+
app.setPath('userData', path.join(app.getPath('appData'), app.getName()));
60+
61+
// @ts-expect-error this seems to work but not exposed as public path and so
62+
// is not available in d.ts files. As this is a dev-only path change and
63+
// seemingly nothing is using this path anyway, we probably can ignore an
64+
// error here
65+
app.setPath('userCache', path.join(app.getPath('cache'), app.getName()));
66+
}
67+
}

packages/connection-storage/src/connection-storage.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export class ConnectionStorage {
3737

3838
private static readonly folder = 'Connections';
3939
private static readonly maxAllowedRecentConnections = 10;
40-
private static readonly keytarServiceName = getKeytarServiceName();
4140

4241
private constructor() {
4342
// singleton
@@ -85,7 +84,7 @@ export class ConnectionStorage {
8584
return {};
8685
}
8786
try {
88-
const credentials = await keytar.findCredentials(this.keytarServiceName);
87+
const credentials = await keytar.findCredentials(getKeytarServiceName());
8988
return Object.fromEntries(
9089
credentials.map(({ account, password }) => [
9190
account,
@@ -233,7 +232,7 @@ export class ConnectionStorage {
233232
);
234233
try {
235234
await keytar.setPassword(
236-
this.keytarServiceName,
235+
getKeytarServiceName(),
237236
connectionInfo.id,
238237
JSON.stringify({ secrets }, null, 2)
239238
);
@@ -277,7 +276,7 @@ export class ConnectionStorage {
277276
return;
278277
}
279278
try {
280-
await keytar.deletePassword(this.keytarServiceName, id);
279+
await keytar.deletePassword(getKeytarServiceName(), id);
281280
} catch (e) {
282281
log.error(
283282
mongoLogId(1_001_000_203),

0 commit comments

Comments
 (0)