Merge pull request #549 from hashicorp/ICU-1335-close-session-on-cancel

feat: Close local proxy on session cancellation in Desktop
This commit is contained in:
Susmitha Girumala authored and GitHub committed 2021-05-17 08:29:51 -04:00
commit 685ddeac7e
25 files changed
+338 -151

No files matched your search

+6
View File
@@ -2,6 +2,12 @@
Canonical reference for changes, improvements, and bugfixes for Boundary Desktop.
## Next
### Bug Fixes
- Close spawned boundary process on session cancel. ([PR](https://github.com/hashicorp/boundary-ui/pull/549))
## v1.0.0 [CLI 0.2.0] (2021.04.14)
### New
@@ -11,6 +11,7 @@ export default class ScopesScopeProjectsSessionsRoute extends Route {
// =services
@service ipc;
@service session;
// =attributes
@@ -83,5 +84,6 @@ export default class ScopesScopeProjectsSessionsRoute extends Route {
@notifySuccess('notifications.canceled-success')
async cancelSession(session) {
await session.cancelSession();
await this.ipc.invoke('stop', { session_id: session.id });
}
}
@@ -1,8 +1,13 @@
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeProjectsTargetsTargetSessionsRoute extends Route {
// =services
@service ipc;
// =actions
/**
@@ -14,5 +19,6 @@ export default class ScopesScopeProjectsTargetsTargetSessionsRoute extends Route
@notifySuccess('notifications.canceled-success')
async cancelSession(session) {
await session.cancelSession();
await this.ipc.invoke('stop', { session_id: session.id });
}
}
+1 -1
View File
@@ -93,7 +93,7 @@ ember-dist/
ember-test/
# CLI
cli/
/cli/
# Autoupdate
nextVersion/
@@ -1,5 +1,5 @@
const process = require('process');
const { version } = require('../src/boundary-cli.js');
const { version } = require('../src/cli/index.js');
const formattedCLIVersion = version().formatted;
@@ -1,58 +0,0 @@
const path = require('path');
const { spawnAsyncJSONPromise, spawnSync } = require('./spawn-promise');
const origin = require('./origin.js');
const cliPath = async () => path.resolve(__dirname, '..', 'cli', 'boundary');
/**
* Super paranoid shell quote/escape and validation. Input must be base62.
* @param {string} str
*/
const escapeAndValidateBase62 = (str) => {
const candidate = str.toString();
if (candidate.match(/^[A-Za-z0-9_]*$/)) return candidate;
throw new Error(`
Could not invoke command:
input contained unsafe characters.
`);
};
module.exports = {
// Check boundary cli existence
exists: () => Boolean(cliPath()),
// Initiate connection and return output
connect: (target_id, token, host_id) => {
const addr = origin.origin;
const sanitized = {
target_id: escapeAndValidateBase62(target_id),
token: escapeAndValidateBase62(token),
};
if (host_id) sanitized.host_id = escapeAndValidateBase62(host_id);
const command = [
'connect',
`-target-id=${sanitized.target_id}`,
`-token=${sanitized.token}`,
`-addr=${addr}`,
'-format=json',
];
if (host_id) command.push(`-host-id=${sanitized.host_id}`);
return spawnAsyncJSONPromise(command);
},
// Returns JSON-formatted version information from the CLI
version: () => {
const command = ['-v'];
const rawOutput = spawnSync(command);
let gitRevision = /Git Revision:\s*(?<rev>.*)\n/.exec(rawOutput);
let versionNumber = /Version Number:\s*(?<ver>.*)\n/.exec(rawOutput);
if (gitRevision) gitRevision = gitRevision.groups.rev;
if (versionNumber) versionNumber = versionNumber.groups.ver;
const formatted = versionNumber
? `CLI Version: ${versionNumber}\nCLI Commit: ${gitRevision}`
: `CLI Commit: ${gitRevision}`;
return { gitRevision, versionNumber, formatted };
return rawOutput;
},
};
+23
View File
@@ -0,0 +1,23 @@
const path = require('path');
const { spawnSync } = require('../helpers/spawn-promise.js');
const cliPath = async () => path.resolve(__dirname, '..', '..', 'cli', 'boundary');
module.exports = {
// Check boundary cli existence
exists: () => Boolean(cliPath()),
// Returns JSON-formatted version information from the CLI
version: () => {
const command = ['-v'];
const rawOutput = spawnSync(command);
let gitRevision = /Git Revision:\s*(?<rev>.*)\n/.exec(rawOutput);
let versionNumber = /Version Number:\s*(?<ver>.*)\n/.exec(rawOutput);
if (gitRevision) gitRevision = gitRevision.groups.rev;
if (versionNumber) versionNumber = versionNumber.groups.ver;
const formatted = versionNumber
? `CLI Version: ${versionNumber}\nCLI Commit: ${gitRevision}`
: `CLI Commit: ${gitRevision}`;
return { gitRevision, versionNumber, formatted };
},
};
@@ -1,4 +1,4 @@
const origin = require('./origin.js');
const runtimeSettings = require('../services/runtime-settings.js');
const isDev = require('electron-is-dev');
const csp = {
@@ -28,9 +28,9 @@ const generateCSPHeader = () => {
const policy = Object.assign({}, csp);
// If an origin is specified, add it to the connect-src directive
if (origin.origin) {
if (runtimeSettings.origin) {
policy['connect-src'] = policy['connect-src'].slice();
policy['connect-src'].push(origin.origin);
policy['connect-src'].push(runtimeSettings.origin);
}
return Object.keys(policy)
@@ -1,5 +1,5 @@
const { shell } = require('electron');
const appUpdater = require('./app-updater.js');
const appUpdater = require('../helpers/app-updater.js');
const generateMenuTemplate = () => {
return [
@@ -0,0 +1,26 @@
const { net } = require('electron');
const requestTimeoutSeconds = 10;
// Simple promise wrapper around Electron's net.request feature.
// https://www.electronjs.org/docs/api/client-request
const netRequestPromise = (url) =>
new Promise((resolve, reject) => {
try {
const request = net.request(url);
// We don't worry about canceling the request timeout timer once a
// request ends because: request.abort() has no effect after a request
// ends and the promise will already have been completed, so a rejection
// has no effect either.
const requestTimeout = setTimeout(() => {
request.abort();
reject(new Error('Request timeout'));
}, requestTimeoutSeconds * 1000);
request.on('error', reject);
request.on('response', resolve);
request.end();
} catch (e) {
reject(e);
}
});
module.exports = netRequestPromise;
@@ -1,17 +1,8 @@
const path = require('path');
const { spawn, spawnSync } = require('child_process');
const jsonify = require('../utils/jsonify.js');
const boundaryPath = path.resolve(__dirname, '..', 'cli', 'boundary');
// Convert to json
const jsonify = (data) => {
if (typeof data !== 'string') data = JSON.stringify(data);
try {
return JSON.parse(data);
} catch (e) {
// Ignore parse errors
}
};
const boundaryPath = path.resolve(__dirname, '..', '..', 'cli', 'boundary');
// You can throw exceptions, or allow them to occur, and this is supported.
// Exceptions thrown in this way will be returned to the UI as an
@@ -42,25 +33,37 @@ module.exports = {
let outputStream = '';
let errorStream = '';
childProcess.stdout.on('data', (data) => {
const processData = (data) => {
outputStream += data.toString();
const jsonData = jsonify(outputStream);
if (jsonData) resolve(jsonData);
});
if (jsonData) {
childProcess.removeAllListeners();
resolve({ childProcess, response: jsonData });
}
};
childProcess.stderr.on('data', (data) => {
errorStream += data.toString();
childProcess.stdout.on('data', processData);
// Errors are cli returned json objects
const processError = (error) => {
errorStream += error.toString();
const jsonData = jsonify(errorStream);
if (jsonData) {
const error = jsonData.api_error || jsonData.error;
childProcess.removeAllListeners();
reject(new Error(error.message));
}
});
};
childProcess.stderr.on('error', processError);
});
},
/**
*
* Spawn child process and return output immediately.
* This function is intended for non-connection related tasks.
* @param {string} command
* @return {string}
*/
spawnSync(command) {
const childProcess = spawnSync(boundaryPath, command);
+6 -6
View File
@@ -13,13 +13,13 @@ const {
Menu,
MenuItem,
} = require('electron');
require('./handlers.js');
require('./ipc/handlers.js');
const origin = require('./origin.js');
const { generateCSPHeader } = require('./content-security-policy.js');
const runtimeSettings = require('./services/runtime-settings.js');
const { generateCSPHeader } = require('./config/content-security-policy.js');
const menu = require('./menu.js');
const appUpdater = require('./app-updater.js');
const menu = require('./config/menu.js');
const appUpdater = require('./helpers/app-updater.js');
const isDev = require('electron-is-dev');
// Register the custom file protocol
@@ -131,7 +131,7 @@ app.on('ready', async () => {
// If the user-specified origin changes, reload the page so that
// the CSP can be refreshed with the this source allowed
origin.onOriginChange(() => mainWindow.loadURL(emberAppURL));
runtimeSettings.onOriginChange(() => mainWindow.loadURL(emberAppURL));
// If you want to open up dev tools programmatically, call
// mainWindow.openDevTools();
@@ -1,30 +1,31 @@
const { shell } = require('electron');
const isDev = require('electron-is-dev');
const handle = require('./ipc-handler');
const boundaryCli = require('./boundary-cli');
const origin = require('./origin.js');
const handle = require('./ipc-handler.js');
const boundaryCli = require('../cli/index.js');
const sessionManager = require('../services/session-manager.js');
const runtimeSettings = require('../services/runtime-settings.js');
const sanitizer = require('../utils/sanitizer.js');
/**
* Returns the current runtime origin, which is used by the main thread to
* rewrite the CSP to allow requests.
*/
handle('getOrigin', () => origin.origin);
handle('getOrigin', () => runtimeSettings.origin);
/**
* Sets the origin to be used in the content security policy and triggers
* a main window reload.
*/
handle('setOrigin', async (requestOrigin) => {
await origin.validateOrigin(requestOrigin);
origin.origin = requestOrigin;
const origin = sanitizer.urlValidate(requestOrigin);
await runtimeSettings.validateOrigin(origin);
runtimeSettings.origin = origin;
});
/**
* Resets the origin.
*/
handle('resetOrigin', async (requestOrigin) => {
origin.resetOrigin();
});
handle('resetOrigin', async () => runtimeSettings.resetOrigin());
/**
* Opens the specified URL in an external browser. Only secure HTTPs URLs are
@@ -56,5 +57,10 @@ handle('cliExists', () => boundaryCli.exists());
* Establishes a boundary session and returns session details.
*/
handle('connect', ({ target_id, token, host_id }) =>
boundaryCli.connect(target_id, token, host_id)
sessionManager.start(runtimeSettings.origin, target_id, token, host_id)
);
/**
* Cancel an established boundary session spawned process.
*/
handle('stop', ({ session_id }) => sessionManager.stopById(session_id));
@@ -1,34 +1,7 @@
const { net } = require('electron');
const requestTimeoutSeconds = 10;
// Simple promise wrapper around Electron's net.request feature.
// https://www.electronjs.org/docs/api/client-request
const netRequestPromise = (url) =>
new Promise((resolve, reject) => {
try {
const request = net.request(url);
// We don't worry about canceling the request timeout timer once a
// request ends because: request.abort() has no effect after a request
// ends and the promise will already have been completed, so a rejection
// has no effect either.
const requestTimeout = setTimeout(() => {
request.abort();
reject(new Error('Request timeout'));
}, requestTimeoutSeconds * 1000);
request.on('error', reject);
request.on('response', resolve);
request.end();
} catch (e) {
reject(e);
}
});
// Provides a singleton class instance to enable a consistent view of
// runtime settings across the application.
const netRequestPromise = require('../helpers/net-request-promise.js');
// Runtime settings are any configuration, especially user-specified,
// set up at runtime. For example, the Boundary origin is a runtime setting.
class RuntimeSettings {
// Internal private origin is exposed via getter/setter below.
#origin = undefined;
@@ -84,6 +57,4 @@ class RuntimeSettings {
}
}
const runtimeSettings = new RuntimeSettings();
module.exports = runtimeSettings;
module.exports = RuntimeSettings;
@@ -0,0 +1,33 @@
const Session = require('./session.js');
class SessionManager {
#sessions = [];
/**
* Start a session and track it.
* Returns session proxy details if successfully started.
* @param {string} addr
* @param {string} target_id
* @param {string} token
* @param {string} host_id
*/
start(addr, target_id, token, host_id) {
const session = new Session(addr, target_id, token, host_id);
this.#sessions.push(session);
return session.start();
}
/**
* Stop a session using identifier.
* @param {string} session_id
*/
stopById(session_id) {
const session = this.#sessions.find((session) => session.id === session_id);
return session.stop();
}
stopAll() {
}
}
module.exports = SessionManager;
@@ -0,0 +1,101 @@
const { spawnAsyncJSONPromise } = require('../helpers/spawn-promise.js');
const sanitizer = require('../utils/sanitizer.js');
class Session {
#id;
#addr;
#token;
#hostId;
#process;
#targetId;
#proxyDetails;
/**
* Initialize a session to a controller address
* using target details.
* @param {string} addr
* @param {string} targetId
* @param {string} token
* @param {string} hostId
*/
constructor(addr, targetId, token, hostId) {
this.#addr = addr;
this.#targetId = targetId;
this.#token = token;
this.#hostId = hostId;
}
/**
* Session id
* @return {string}
*/
get id() {
return this.#id;
}
/**
* @return {boolean}
*/
get isActive() {
return !this.#process.killed;
}
/**
* Using cli, initialize a session to a target.
* Tracks local proxy details if successful.
*/
start() {
const command = this.cliCommand();
return spawnAsyncJSONPromise(command).then((spawnedSession) => {
this.#process = spawnedSession.childProcess;
this.#proxyDetails = spawnedSession.response;
this.#process = spawnedSession.childProcess;
this.#id = this.#proxyDetails.session_id;
return this.#proxyDetails;
});
}
/**
* Stop proxy process used by session.
*/
stop() {
return new Promise((resolve, reject) => {
if (this.isActive) {
this.#process.on('close', () => resolve());
this.#process.on('error', (e) => reject(e));
this.#process.kill();
} else {
// Do nothing when process isn't active
resolve();
}
});
}
/**
* Generate cli command for session.
* @returns {[]}
*/
cliCommand() {
const sanitized = {
target_id: sanitizer.base62EscapeAndValidate(this.#targetId),
token: sanitizer.base62EscapeAndValidate(this.#token),
addr: sanitizer.urlValidate(this.#addr),
};
const command = [
'connect',
`-target-id=${sanitized.target_id}`,
`-token=${sanitized.token}`,
`-addr=${sanitized.addr}`,
'-format=json',
];
if (this.#hostId) {
sanitized.host_id = sanitizer.base62EscapeAndValidate(this.#hostId);
command.push(`-host-id=${sanitized.host_id}`);
}
return command;
}
}
module.exports = Session;
@@ -0,0 +1,8 @@
const RuntimeSettings = require('../models/runtime-settings.js');
// Provides a singleton class instance to enable a consistent view of
// runtime settings across the application.
const runtimeSettings = new RuntimeSettings();
module.exports = runtimeSettings;
@@ -0,0 +1,7 @@
const SessionManager = require('../models/session-manager.js');
/**
* Singleton for SessionManager
*/
const sessionManager = new SessionManager();
module.exports = sessionManager;
@@ -0,0 +1,11 @@
// Convert to json
const jsonify = (data) => {
if (typeof data !== 'string') data = JSON.stringify(data);
try {
return JSON.parse(data);
} catch (e) {
// Ignore parse errors
}
};
module.exports = jsonify;
@@ -0,0 +1,30 @@
const { URL } = require('url');
module.exports = {
/**
* Super paranoid shell quote/escape and validation. Input must be base62.
* @param {string} str
*/
base62EscapeAndValidate: (str) => {
const candidate = str.toString();
if (candidate.match(/^[A-Za-z0-9_]*$/)) return candidate;
throw new Error(`
Could not invoke command:
input contained unsafe characters.
`);
},
/**
* Return a quoted string suitable for using in shell commands.
* @param {string} str
* @returns {string}
*/
urlValidate: (str) => {
try {
const url = new URL(str);
return `${url.protocol}://${url.host}`;
} catch (e) {
// Parse error
throw new Error(`URL ${str} could not be validated.`);
}
},
};
@@ -19,6 +19,7 @@ import {
authenticateSession,
invalidateSession,
} from 'ember-simple-auth/test-support';
import sinon from 'sinon';
module('Acceptance | projects | sessions', function (hooks) {
setupApplicationTest(hooks);
@@ -43,6 +44,7 @@ module('Acceptance | projects | sessions', function (hooks) {
const stubs = {
global: null,
org: null,
ipcService: null,
};
const urls = {
@@ -144,6 +146,9 @@ module('Acceptance | projects | sessions', function (hooks) {
window.addEventListener('message', messageHandler);
setDefaultOrigin(this);
const ipcService = this.owner.lookup('service:ipc');
stubs.ipcService = sinon.stub(ipcService, 'invoke');
});
hooks.afterEach(function () {
@@ -240,6 +245,7 @@ module('Acceptance | projects | sessions', function (hooks) {
test('cancelling a session', async function (assert) {
assert.expect(1);
stubs.ipcService.withArgs('stop');
later(async () => {
run.cancelTimers();
await click('tbody tr:first-child td:last-child button');
@@ -258,4 +264,15 @@ module('Acceptance | projects | sessions', function (hooks) {
}, 750);
await visit(urls.sessions);
});
test('cancelling a session with ipc error shows notification', async function (assert) {
assert.expect(1);
stubs.ipcService.withArgs('stop').throws();
later(async () => {
run.cancelTimers();
await click('tbody tr:first-child td:last-child button');
assert.ok(find('[role="alert"].is-error'));
}, 750);
await visit(urls.sessions);
});
});
@@ -248,6 +248,7 @@ module('Acceptance | projects | targets | sessions', function (hooks) {
test('cancelling a session', async function (assert) {
assert.expect(2);
const sessionsCount = this.server.schema.sessions.all().models.length;
stubs.ipcService.withArgs('cancel');
later(async () => {
run.cancelTimers();
await click('tbody tr:first-child td:last-child button');
@@ -268,6 +269,17 @@ module('Acceptance | projects | targets | sessions', function (hooks) {
await visit(urls.sessions);
});
test('cancelling a session with ipc error shows notification', async function (assert) {
assert.expect(1);
stubs.ipcService.withArgs('stop').throws();
later(async () => {
run.cancelTimers();
await click('tbody tr:first-child td:last-child button');
assert.ok(find('[role="alert"].is-error'));
}, 750);
await visit(urls.sessions);
});
test('connecting to a target', async function (assert) {
assert.expect(3);
stubs.ipcService.withArgs('cliExists').returns(true);
+5 -22
View File
@@ -7578,7 +7578,7 @@ debug@~4.1.0:
dependencies:
ms "^2.1.1"
debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -12380,7 +12380,7 @@ import-lazy@^4.0.0:
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-4.0.0.tgz#e8eb627483a0a43da3c03f3e35548be5cb0cc153"
integrity sha512-rKtvo6a868b5Hu3heneU+L4yEQ4jYKLtjpnPeUdK7h0yzXGmyBTypknlkCvHFBqfX9YlorEiMM6Dnq/5atfHkw==
imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -14078,11 +14078,6 @@ lodash._baseflatten@^3.0.0:
lodash.isarguments "^3.0.0"
lodash.isarray "^3.0.0"
lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -14091,16 +14086,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"
lodash._bindcallback@*, lodash._bindcallback@^3.0.0:
lodash._bindcallback@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
lodash._createassigner@^3.0.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/lodash._createassigner/-/lodash._createassigner-3.1.1.tgz#838a5bae2fdaca63ac22dee8e19fa4e6d6970b11"
@@ -14110,19 +14100,12 @@ lodash._createassigner@^3.0.0:
lodash._isiterateecall "^3.0.0"
lodash.restparam "^3.0.0"
lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"
lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
lodash._getnative@*, lodash._getnative@^3.0.0:
lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
@@ -14340,7 +14323,7 @@ lodash.pick@^4.4.0:
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=
lodash.restparam@*, lodash.restparam@^3.0.0:
lodash.restparam@^3.0.0:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=