[Desktop][Bug fix]: Stop target sessions before unauthenticating user session (#2891)
* add stopAll invocation to ipc handler * trigger modal on both signout btns * refactor invalidateSession fn * fix application controller tests * fix failing tests settings-card/user * add test to Acceptance | authentication * migrate electron dialog.showMessageBoxSync to Hds * rename channel and tracked prop, consolidate cancel fns * combine modals, add willDestroy, refactor * test signout and close sessions modal * address feedback * refactor window.electron in authentication-test * address feedback * refactor: 💡 Fix failing tests --------- Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>
This commit is contained in:
1 parent
f776ba0617
commit
990e2d6b9d
20 files changed
+342
-84
No files matched your search
@@ -13,7 +13,6 @@ const {
|
||||
const {
|
||||
session,
|
||||
app,
|
||||
dialog,
|
||||
protocol,
|
||||
BrowserWindow,
|
||||
Menu,
|
||||
@@ -22,7 +21,6 @@ const {
|
||||
} = require('electron');
|
||||
require('./ipc/handlers.js');
|
||||
const log = require('electron-log/main');
|
||||
const fs = require('fs');
|
||||
|
||||
const { generateCSPHeader } = require('./config/content-security-policy.js');
|
||||
const runtimeSettings = require('./services/runtime-settings.js');
|
||||
@@ -40,6 +38,7 @@ const { version } = require('./cli/index.js');
|
||||
const isDev = require('electron-is-dev');
|
||||
|
||||
// Register the custom file protocol
|
||||
// This will render as the default cluster URL: serve://boundary
|
||||
const emberAppProtocol = 'serve';
|
||||
const emberAppName = 'boundary';
|
||||
const emberAppURL = `${emberAppProtocol}://${emberAppName}`;
|
||||
@@ -129,7 +128,7 @@ const createWindow = async (partition, closeWindowCB) => {
|
||||
const browserWindow = new BrowserWindow(browserWindowOptions);
|
||||
|
||||
// If the user-specified cluster URL changes, reload the page so that
|
||||
// the CSP can be refreshed with the this source allowed
|
||||
// the content security policy (CSP) can be refreshed with the source allowed
|
||||
runtimeSettings.onClusterUrlChange(
|
||||
async () => await browserWindow.loadURL(emberAppURL),
|
||||
);
|
||||
@@ -143,6 +142,8 @@ const createWindow = async (partition, closeWindowCB) => {
|
||||
await browserWindow.loadURL(emberAppURL);
|
||||
});
|
||||
|
||||
// Emitted when the renderer process unexpectedly disappears.
|
||||
// This is normally because it was crashed or killed.
|
||||
browserWindow.webContents.on('render-process-gone', () => {
|
||||
console.log(
|
||||
'Your Ember app (or other code) in the main window has crashed.',
|
||||
@@ -160,7 +161,7 @@ const createWindow = async (partition, closeWindowCB) => {
|
||||
});
|
||||
|
||||
// Opens external links in the host default browser.
|
||||
// We allow developer.hashicorp.com domain to open on external window
|
||||
// We allow developer.hashicorp.com domain to open on an external window
|
||||
// and releases.hashicorp.com domain to download the desktop app or
|
||||
// link to the release page for the desktop app.
|
||||
browserWindow.webContents.setWindowOpenHandler(({ url }) => {
|
||||
@@ -204,15 +205,15 @@ app.on('window-all-closed', () => {
|
||||
|
||||
app.on('ready', async () => {
|
||||
// Setup a partition
|
||||
// Must be prefixed with persist: in order to save things like localStorage
|
||||
// Must be prefixed with "persist:" in order to save things like localStorage
|
||||
// data. Without this, the session is in-memory only.
|
||||
// https://www.electronjs.org/docs/api/session#sessionfrompartitionpartition-options
|
||||
const partition = `persist:${emberAppName}`;
|
||||
const ses = session.fromPartition(partition);
|
||||
|
||||
// Register custom protocol
|
||||
// This file protocol is the exclusive protocol for this application. All
|
||||
// other protocols are disabled.
|
||||
// This file protocol is the exclusive protocol for this application.
|
||||
// All other protocols are disabled.
|
||||
ses.protocol.handle(emberAppProtocol, (request) => {
|
||||
/* eng-disable PROTOCOL_HANDLER_JS_CHECK */
|
||||
const isDir = request.url.endsWith('/');
|
||||
@@ -270,8 +271,8 @@ app.on('ready', async () => {
|
||||
|
||||
/**
|
||||
* Need for Mac OS behavior of closing the window but not the app.
|
||||
* This allows to reopen the window if the user clicks Boundary icon in the dock
|
||||
* while the app is not close.
|
||||
* This allows a user to reopen the window when clicking the Boundary icon in the dock,
|
||||
* only when the app has not been closed.
|
||||
* More info: https://www.electronjs.org/docs/latest/tutorial/quick-start#open-a-window-if-none-are-open-macos
|
||||
*/
|
||||
app.on('activate', async () => {
|
||||
@@ -284,18 +285,13 @@ app.on('ready', async () => {
|
||||
});
|
||||
|
||||
/**
|
||||
* Prompt for closing spawned processes
|
||||
* Prompt user to confirm action before closing spawned processes
|
||||
* This interaction lives in the ember app using Hds::Modal
|
||||
*/
|
||||
app.on('before-quit', (event) => {
|
||||
app.on('before-quit', async (event) => {
|
||||
if (sessionManager.hasRunningSessions) {
|
||||
const dialogOpts = {
|
||||
type: 'question',
|
||||
buttons: ['Close', 'Cancel'],
|
||||
detail: 'Close sessions before quitting?',
|
||||
};
|
||||
|
||||
const buttonId = dialog.showMessageBoxSync(null, dialogOpts);
|
||||
buttonId === 0 ? sessionManager.stopAll() : event.preventDefault();
|
||||
mainWindow.webContents.send('onAppQuit');
|
||||
event.preventDefault();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -312,7 +308,7 @@ app.on('quit', () => {
|
||||
// from the exception can cause additional unforeseen and unpredictable issues.
|
||||
//
|
||||
// Attempting to resume normally after an uncaught exception can be similar to pulling out
|
||||
// of the power cord when upgrading a computer -- nine out of ten times nothing happens -
|
||||
// the power cord when upgrading a computer -- nine out of ten times nothing happens -
|
||||
// but the 10th time, the system becomes corrupted.
|
||||
//
|
||||
// The correct use of 'uncaughtException' is to perform synchronous cleanup of allocated
|
||||
|
||||
@@ -76,10 +76,15 @@ handle('connect', ({ target_id, token, host_id }) =>
|
||||
);
|
||||
|
||||
/**
|
||||
* Cancel an established boundary session spawned process.
|
||||
* Stop an established boundary session spawned process.
|
||||
*/
|
||||
handle('stop', ({ session_id }) => sessionManager.stopById(session_id));
|
||||
|
||||
/**
|
||||
* Stop all active and pending target sessions.
|
||||
*/
|
||||
handle('stopAll', async () => sessionManager.stopAll());
|
||||
|
||||
/**
|
||||
* Check for OS window chrome. Enabled on MacOS only.
|
||||
* Window chrome is disabled regardless of platform for
|
||||
@@ -116,6 +121,12 @@ handle('toggleFullscreenWindow', () => {
|
||||
*/
|
||||
handle('closeWindow', () => app.quit());
|
||||
|
||||
/**
|
||||
* Check if session manager has running sessions
|
||||
* Return boolean
|
||||
*/
|
||||
handle('hasRunningSessions', () => sessionManager.hasRunningSessions);
|
||||
|
||||
/**
|
||||
* Focus the window
|
||||
*/
|
||||
|
||||
@@ -39,8 +39,13 @@ class SessionManager {
|
||||
return session?.stop?.();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop all active and pending target sessions
|
||||
* Returning Promise.all() ensures all sessions in the list have been
|
||||
* stopped before calling the next fn
|
||||
*/
|
||||
stopAll() {
|
||||
this.#sessions.forEach((session) => session.stop());
|
||||
return Promise.all(this.#sessions.map((session) => session.stop()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,12 @@ contextBridge.exposeInMainWorld('terminal', {
|
||||
},
|
||||
receive: (callback, id) => {
|
||||
const incomingDataChannel = `terminalIncomingData-${id}`;
|
||||
ipcRenderer.on(incomingDataChannel, callback);
|
||||
const listenerCallback = (_event, value) => callback(value);
|
||||
ipcRenderer.on(incomingDataChannel, listenerCallback);
|
||||
|
||||
// Return a function for the caller to handle cleaning up the listener
|
||||
return () => {
|
||||
return ipcRenderer.removeListener(incomingDataChannel, callback);
|
||||
return ipcRenderer.removeListener(incomingDataChannel, listenerCallback);
|
||||
};
|
||||
},
|
||||
create: (vars) => {
|
||||
@@ -61,3 +62,18 @@ process.once('loaded', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Listener on electron app when user triggers before-quit event
|
||||
*/
|
||||
contextBridge.exposeInMainWorld('electron', {
|
||||
onAppQuit: (callback) => {
|
||||
// Don't pass in callback directly so users can't access passed in event for security
|
||||
const listenerCallback = () => callback();
|
||||
ipcRenderer.on('onAppQuit', listenerCallback);
|
||||
|
||||
return () => {
|
||||
return ipcRenderer.removeListener('onAppQuit', listenerCallback);
|
||||
};
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user