Skip to content

Commit 72639bd

Browse files
authored
fix: πŸ› Pass in session time out when creating session (#3008)
* fix: πŸ› Pass in session time out when creating session βœ… Closes: https://hashicorp.atlassian.net/browse/ICU-17701
1 parent 31f0044 commit 72639bd

File tree

5 files changed

+63
-42
lines changed

5 files changed

+63
-42
lines changed

β€Žui/desktop/app/controllers/scopes/scope/projects/targets/index.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export default class ScopesScopeProjectsTargetsIndexController extends Controlle
190190
const options = {
191191
target_id: target.id,
192192
token: this.session.data.authenticated.token,
193+
session_max_seconds: target.session_max_seconds,
193194
};
194195

195196
if (host) options.host_id = host.id;

β€Žui/desktop/electron-app/src/helpers/spawn-promise.jsβ€Ž

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ module.exports = {
2727
* data on either stdout or stderr. The process is allowed to continue
2828
* running after the promise resolves. This function is intended to launch
2929
* the local proxy.
30-
* @param {string} command
30+
* @param {string[]} command
31+
* @param {string} token
32+
* @param {number} timeout Duration in seconds
3133
* @return {Promise}
3234
*/
33-
spawnAsyncJSONPromise(command, token) {
35+
spawnAsyncJSONPromise(command, token, timeout) {
3436
return new Promise((resolve, reject) => {
3537
const childProcess = spawn(path, command, {
3638
env: {
3739
...process.env,
3840
BOUNDARY_TOKEN: token,
3941
},
42+
timeout: timeout ? timeout * 1000 : undefined,
4043
});
4144
let outputStream = '';
4245
let errorStream = '';

β€Žui/desktop/electron-app/src/ipc/handlers.jsβ€Ž

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,14 @@ handle('cliExists', () => boundaryCli.exists());
7171
/**
7272
* Establishes a boundary session and returns session details.
7373
*/
74-
handle('connect', ({ target_id, token, host_id }) =>
75-
sessionManager.start(runtimeSettings.clusterUrl, target_id, token, host_id),
74+
handle('connect', ({ target_id, token, host_id, session_max_seconds }) =>
75+
sessionManager.start(
76+
runtimeSettings.clusterUrl,
77+
target_id,
78+
token,
79+
host_id,
80+
session_max_seconds,
81+
),
7682
);
7783

7884
/**

β€Žui/desktop/electron-app/src/models/session-manager.jsβ€Ž

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ class SessionManager {
2323
* @param {string} target_id
2424
* @param {string} token
2525
* @param {string} host_id
26+
* @param {number} session_max_seconds
2627
*/
27-
start(addr, target_id, token, host_id) {
28-
const session = new Session(addr, target_id, token, host_id);
28+
start(addr, target_id, token, host_id, session_max_seconds) {
29+
const session = new Session(
30+
addr,
31+
target_id,
32+
token,
33+
host_id,
34+
session_max_seconds,
35+
);
2936
this.#sessions.push(session);
3037
return session.start();
3138
}

β€Žui/desktop/electron-app/src/models/session.jsβ€Ž

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Session {
1717
#process;
1818
#targetId;
1919
#proxyDetails;
20+
#sessionMaxSeconds;
2021

2122
/**
2223
* Initialize a session to a controller address
@@ -25,12 +26,14 @@ class Session {
2526
* @param {string} targetId
2627
* @param {string} token
2728
* @param {string} hostId
29+
* @param {number} sessionMaxSeconds
2830
*/
29-
constructor(addr, targetId, token, hostId) {
31+
constructor(addr, targetId, token, hostId, sessionMaxSeconds) {
3032
this.#addr = addr;
3133
this.#targetId = targetId;
3234
this.#token = token;
3335
this.#hostId = hostId;
36+
this.#sessionMaxSeconds = sessionMaxSeconds;
3437
}
3538

3639
/**
@@ -48,22 +51,48 @@ class Session {
4851
return this.#process && !this.#process.killed;
4952
}
5053

54+
/**
55+
* Generate cli command for session.
56+
* @returns {string[]}
57+
*/
58+
get connectCommand() {
59+
const sanitized = {
60+
target_id: sanitizer.base62EscapeAndValidate(this.#targetId),
61+
addr: sanitizer.urlValidate(this.#addr),
62+
};
63+
64+
const command = [
65+
'connect',
66+
`-target-id=${sanitized.target_id}`,
67+
`-token=env://BOUNDARY_TOKEN`,
68+
`-addr=${sanitized.addr}`,
69+
'-format=json',
70+
];
71+
72+
if (this.#hostId) {
73+
sanitized.host_id = sanitizer.base62EscapeAndValidate(this.#hostId);
74+
command.push(`-host-id=${sanitized.host_id}`);
75+
}
76+
return command;
77+
}
78+
5179
/**
5280
* Using cli, initialize a session to a target.
5381
* Tracks local proxy details if successful.
5482
*/
5583
start() {
56-
const command = this.cliCommand();
5784
const sanitizedToken = sanitizer.base62EscapeAndValidate(this.#token);
58-
return spawnAsyncJSONPromise(command, sanitizedToken).then(
59-
(spawnedSession) => {
60-
this.#process = spawnedSession.childProcess;
61-
this.#proxyDetails = spawnedSession.response;
62-
this.#process = spawnedSession.childProcess;
63-
this.#id = this.#proxyDetails.session_id;
64-
return this.#proxyDetails;
65-
},
66-
);
85+
return spawnAsyncJSONPromise(
86+
this.connectCommand,
87+
sanitizedToken,
88+
this.#sessionMaxSeconds,
89+
).then((spawnedSession) => {
90+
this.#process = spawnedSession.childProcess;
91+
this.#proxyDetails = spawnedSession.response;
92+
this.#process = spawnedSession.childProcess;
93+
this.#id = this.#proxyDetails.session_id;
94+
return this.#proxyDetails;
95+
});
6796
}
6897

6998
/**
@@ -96,31 +125,6 @@ class Session {
96125
}
97126
});
98127
}
99-
100-
/**
101-
* Generate cli command for session.
102-
* @returns {[]}
103-
*/
104-
cliCommand() {
105-
const sanitized = {
106-
target_id: sanitizer.base62EscapeAndValidate(this.#targetId),
107-
addr: sanitizer.urlValidate(this.#addr),
108-
};
109-
110-
const command = [
111-
'connect',
112-
`-target-id=${sanitized.target_id}`,
113-
`-token=env://BOUNDARY_TOKEN`,
114-
`-addr=${sanitized.addr}`,
115-
'-format=json',
116-
];
117-
118-
if (this.#hostId) {
119-
sanitized.host_id = sanitizer.base62EscapeAndValidate(this.#hostId);
120-
command.push(`-host-id=${sanitized.host_id}`);
121-
}
122-
return command;
123-
}
124128
}
125129

126130
module.exports = Session;

0 commit comments

Comments
Β (0)