@alwaysai/device-agent 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/application-control/environment-variables.d.ts.map +1 -1
- package/lib/application-control/environment-variables.js +9 -4
- package/lib/application-control/environment-variables.js.map +1 -1
- package/lib/application-control/environment-variables.test.js +1 -1
- package/lib/application-control/environment-variables.test.js.map +1 -1
- package/lib/application-control/install.d.ts.map +1 -1
- package/lib/application-control/install.js +6 -2
- package/lib/application-control/install.js.map +1 -1
- package/lib/application-control/models.d.ts.map +1 -1
- package/lib/application-control/models.js +4 -2
- package/lib/application-control/models.js.map +1 -1
- package/lib/application-control/status.js +4 -5
- package/lib/application-control/status.js.map +1 -1
- package/lib/cloud-connection/device-agent-cloud-connection.d.ts +3 -3
- package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
- package/lib/cloud-connection/device-agent-cloud-connection.js +114 -99
- package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
- package/lib/cloud-connection/live-updates-handler.d.ts +1 -0
- package/lib/cloud-connection/live-updates-handler.d.ts.map +1 -1
- package/lib/cloud-connection/live-updates-handler.js +22 -4
- package/lib/cloud-connection/live-updates-handler.js.map +1 -1
- package/lib/cloud-connection/messages.d.ts.map +1 -1
- package/lib/cloud-connection/messages.js +3 -4
- package/lib/cloud-connection/messages.js.map +1 -1
- package/lib/cloud-connection/shadow-handler.d.ts +14 -21
- package/lib/cloud-connection/shadow-handler.d.ts.map +1 -1
- package/lib/cloud-connection/shadow-handler.js +162 -108
- package/lib/cloud-connection/shadow-handler.js.map +1 -1
- package/lib/cloud-connection/shadow-handler.test.js +100 -83
- package/lib/cloud-connection/shadow-handler.test.js.map +1 -1
- package/lib/device-control/device-control.d.ts +7 -14
- package/lib/device-control/device-control.d.ts.map +1 -1
- package/lib/device-control/device-control.js +37 -14
- package/lib/device-control/device-control.js.map +1 -1
- package/lib/secure-tunneling/secure-tunneling.d.ts +105 -0
- package/lib/secure-tunneling/secure-tunneling.d.ts.map +1 -0
- package/lib/secure-tunneling/secure-tunneling.js +435 -0
- package/lib/secure-tunneling/secure-tunneling.js.map +1 -0
- package/lib/secure-tunneling/secure-tunneling.test.d.ts +2 -0
- package/lib/secure-tunneling/secure-tunneling.test.d.ts.map +1 -0
- package/lib/secure-tunneling/secure-tunneling.test.js +1070 -0
- package/lib/secure-tunneling/secure-tunneling.test.js.map +1 -0
- package/lib/secure-tunneling/spawner-detached.d.ts +6 -0
- package/lib/secure-tunneling/spawner-detached.d.ts.map +1 -0
- package/lib/secure-tunneling/spawner-detached.js +107 -0
- package/lib/secure-tunneling/spawner-detached.js.map +1 -0
- package/lib/subcommands/app/analytics.d.ts.map +1 -1
- package/lib/subcommands/app/analytics.js +9 -13
- package/lib/subcommands/app/analytics.js.map +1 -1
- package/lib/subcommands/app/env-vars.d.ts.map +1 -1
- package/lib/subcommands/app/env-vars.js +11 -16
- package/lib/subcommands/app/env-vars.js.map +1 -1
- package/lib/subcommands/app/models.d.ts.map +1 -1
- package/lib/subcommands/app/models.js +12 -16
- package/lib/subcommands/app/models.js.map +1 -1
- package/lib/subcommands/device/clean.d.ts.map +1 -1
- package/lib/subcommands/device/clean.js +3 -1
- package/lib/subcommands/device/clean.js.map +1 -1
- package/lib/subcommands/device/device.d.ts.map +1 -1
- package/lib/subcommands/device/device.js +14 -6
- package/lib/subcommands/device/device.js.map +1 -1
- package/lib/util/cloud-mode-ready.d.ts +1 -0
- package/lib/util/cloud-mode-ready.d.ts.map +1 -1
- package/lib/util/cloud-mode-ready.js +36 -1
- package/lib/util/cloud-mode-ready.js.map +1 -1
- package/package.json +2 -2
- package/src/application-control/environment-variables.test.ts +1 -1
- package/src/application-control/environment-variables.ts +9 -6
- package/src/application-control/install.ts +7 -3
- package/src/application-control/models.ts +11 -6
- package/src/application-control/status.ts +8 -8
- package/src/cloud-connection/device-agent-cloud-connection.ts +161 -131
- package/src/cloud-connection/live-updates-handler.ts +34 -6
- package/src/cloud-connection/messages.ts +3 -4
- package/src/cloud-connection/shadow-handler.test.ts +101 -84
- package/src/cloud-connection/shadow-handler.ts +275 -133
- package/src/device-control/device-control.ts +46 -19
- package/src/secure-tunneling/secure-tunneling.test.ts +1239 -0
- package/src/secure-tunneling/secure-tunneling.ts +606 -0
- package/src/secure-tunneling/spawner-detached.ts +123 -0
- package/src/subcommands/app/analytics.ts +16 -13
- package/src/subcommands/app/env-vars.ts +18 -16
- package/src/subcommands/app/models.ts +20 -16
- package/src/subcommands/device/clean.ts +4 -1
- package/src/subcommands/device/device.ts +26 -10
- package/src/util/cloud-mode-ready.ts +36 -0
- package/lib/secure-tunneling/index.d.ts +0 -5
- package/lib/secure-tunneling/index.d.ts.map +0 -1
- package/lib/secure-tunneling/index.js +0 -64
- package/lib/secure-tunneling/index.js.map +0 -1
- package/src/secure-tunneling/index.ts +0 -74
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import { AAI_DIR } from 'alwaysai/lib/paths';
|
|
2
|
+
import { JsSpawner } from 'alwaysai/lib/util';
|
|
3
|
+
import { ChildProcess } from 'child_process';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import { aaiArtifactsBucketUrl } from '../urls';
|
|
6
|
+
import { isValidAwsRegion } from '../util/cloud-mode-ready';
|
|
7
|
+
import {
|
|
8
|
+
AWS_ROOT_CERTIFICATE_FILE_PATH,
|
|
9
|
+
SECURE_TUNNEL_BIN_DIR,
|
|
10
|
+
SECURE_TUNNEL_BIN_NAME,
|
|
11
|
+
SECURE_TUNNEL_BIN_PATH
|
|
12
|
+
} from '../util/directories';
|
|
13
|
+
import { downloadFile } from '../util/download-file';
|
|
14
|
+
import { logger } from '../util/logger';
|
|
15
|
+
import { getArch, getDistribution, getOsVersion } from '../util/system-info';
|
|
16
|
+
import { killDetachedProcess, runDetachedProcess } from './spawner-detached';
|
|
17
|
+
|
|
18
|
+
enum SecureTunnelServiceType {
|
|
19
|
+
SSH = 'SSH',
|
|
20
|
+
HTTP = 'HTTP'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type SecureTunnelPortInfo = {
|
|
24
|
+
enabled: boolean;
|
|
25
|
+
type: string;
|
|
26
|
+
ip: string;
|
|
27
|
+
port: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type SecureTunnelShadowDesRep = {
|
|
31
|
+
st_ports: SecureTunnelPortInfo[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type SecureTunnelShadowState = {
|
|
35
|
+
reported?: SecureTunnelShadowDesRep;
|
|
36
|
+
desired?: SecureTunnelShadowDesRep;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type SecureTunnelShadowUpdateDelta = {
|
|
40
|
+
version: number;
|
|
41
|
+
timestamp: number;
|
|
42
|
+
state: SecureTunnelShadowDesRep;
|
|
43
|
+
metadata?: any;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type SecureTunnelShadowUpdate = {
|
|
47
|
+
version: number;
|
|
48
|
+
state: SecureTunnelShadowState;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type SecureTunnelProxyMap = {
|
|
52
|
+
st_port: SecureTunnelPortInfo;
|
|
53
|
+
localhostPort: number;
|
|
54
|
+
mapProcess: ChildProcess;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type SecureTunnelLocalproxyInfo = {
|
|
58
|
+
lpDstAccessKey: string;
|
|
59
|
+
lpRegion: string;
|
|
60
|
+
lpServices: string;
|
|
61
|
+
lpProcess: ChildProcess | null;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const defaultSecureTunnelPortInfo: SecureTunnelPortInfo = {
|
|
65
|
+
enabled: false,
|
|
66
|
+
type: SecureTunnelServiceType.SSH,
|
|
67
|
+
ip: '0.0.0.0',
|
|
68
|
+
port: 22
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
type SecureTunnelNotificationType = {
|
|
72
|
+
clientAccessToken: string;
|
|
73
|
+
region: string;
|
|
74
|
+
services: string[];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// socat tcp4-listen:5001,fork tcp4:100.70.31.118:80
|
|
78
|
+
const ST_MAPPING_TOOL = 'socat';
|
|
79
|
+
const ST_START_PORT_NUMBER = 5010;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Handles Secure Tunnel Shadow State
|
|
83
|
+
*/
|
|
84
|
+
export class SecureTunnelHandlerSingleton {
|
|
85
|
+
private static instance: SecureTunnelHandlerSingleton;
|
|
86
|
+
private reported: SecureTunnelShadowDesRep;
|
|
87
|
+
private httpProxyMap: SecureTunnelProxyMap[];
|
|
88
|
+
private localproxyInfo: SecureTunnelLocalproxyInfo;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Initializes private variables of SecureTunnel handler.
|
|
92
|
+
* private constructor to prevent instantiation from outside
|
|
93
|
+
*/
|
|
94
|
+
private constructor() {
|
|
95
|
+
logger.debug('-> SecureTunnelHandlerSingleton.constructor');
|
|
96
|
+
this.reported = {
|
|
97
|
+
st_ports: [JSON.parse(JSON.stringify(defaultSecureTunnelPortInfo))]
|
|
98
|
+
};
|
|
99
|
+
this.httpProxyMap = [];
|
|
100
|
+
this.localproxyInfo = {
|
|
101
|
+
lpDstAccessKey: '',
|
|
102
|
+
lpRegion: '',
|
|
103
|
+
lpServices: '',
|
|
104
|
+
lpProcess: null
|
|
105
|
+
};
|
|
106
|
+
// TODO: create a recovery process if we just restarted
|
|
107
|
+
// for that to work we need to store somewhere the dst access key,
|
|
108
|
+
// recover it to restart local proxy to reconnect to secure tunnel
|
|
109
|
+
logger.debug('<- SecureTunnelHandlerSingleton.constructor');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
//---------------------------------------------------------------------------
|
|
113
|
+
// public functions
|
|
114
|
+
//---------------------------------------------------------------------------
|
|
115
|
+
/**
|
|
116
|
+
* Implements the Singleton of the SecureTunnel
|
|
117
|
+
*/
|
|
118
|
+
public static getInstance(): SecureTunnelHandlerSingleton {
|
|
119
|
+
logger.debug('-> SecureTunnelHandlerSingleton.getInstance');
|
|
120
|
+
if (!SecureTunnelHandlerSingleton.instance) {
|
|
121
|
+
SecureTunnelHandlerSingleton.instance =
|
|
122
|
+
new SecureTunnelHandlerSingleton();
|
|
123
|
+
}
|
|
124
|
+
logger.debug('<- SecureTunnelHandlerSingleton.getInstance');
|
|
125
|
+
return SecureTunnelHandlerSingleton.instance;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Stops all proxies started before
|
|
130
|
+
*/
|
|
131
|
+
public async destroy(): Promise<void> {
|
|
132
|
+
logger.debug('-> SecureTunnelHandlerSingleton.destroy');
|
|
133
|
+
logger.debug(`httpProxyMap before : ${JSON.stringify(this.httpProxyMap)}`);
|
|
134
|
+
logger.debug(`reported before : ${JSON.stringify(this.reported)}`);
|
|
135
|
+
|
|
136
|
+
// We need operate on a copy instead of the original this.httpProxyMap
|
|
137
|
+
// because stopProxyMapping removes entries and there for manipulates this loop
|
|
138
|
+
const httpProxyMapCopy: SecureTunnelProxyMap[] = JSON.parse(
|
|
139
|
+
JSON.stringify(this.httpProxyMap)
|
|
140
|
+
);
|
|
141
|
+
for (const item of httpProxyMapCopy) {
|
|
142
|
+
await this.stopProxyMapping(item.st_port);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
this.reported = {
|
|
146
|
+
st_ports: [JSON.parse(JSON.stringify(defaultSecureTunnelPortInfo))]
|
|
147
|
+
};
|
|
148
|
+
logger.debug(`httpProxyMap after : ${JSON.stringify(this.httpProxyMap)}`);
|
|
149
|
+
logger.debug(`reported after : ${JSON.stringify(this.reported)}`);
|
|
150
|
+
await this.stopLocalproxy();
|
|
151
|
+
logger.debug('<- SecureTunnelHandlerSingleton.destroy');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Returns current state of SecureTunnel shadow
|
|
156
|
+
* @returns {SecureTunnelShadowDesRep} - reported state of the SecureTunnel shadow
|
|
157
|
+
*/
|
|
158
|
+
public getSecureTunnelShadow(): SecureTunnelShadowDesRep {
|
|
159
|
+
logger.debug('-> SecureTunnelHandlerSingleton.getSecureTunnelShadow');
|
|
160
|
+
logger.debug(`reported: ${JSON.stringify(this.reported)}`);
|
|
161
|
+
logger.debug('<- SecureTunnelHandlerSingleton.getSecureTunnelShadow');
|
|
162
|
+
return this.reported;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Updates current state of SecureTunnel shadow
|
|
167
|
+
* @param {SecureTunnelShadowUpdateDelta} deltaMsg - delta message, which includes desired state of the SecureTunnel shadow
|
|
168
|
+
* @return {SecureTunnelShadowDesRep} update reported message to send back to AWS IoT device shadow
|
|
169
|
+
*/
|
|
170
|
+
public async syncShadowToDeviceState(
|
|
171
|
+
deltaMsg: SecureTunnelShadowUpdateDelta
|
|
172
|
+
): Promise<SecureTunnelShadowDesRep> {
|
|
173
|
+
logger.debug('-> SecureTunnelHandlerSingleton.syncShadowToDeviceState');
|
|
174
|
+
const { version, state } = deltaMsg;
|
|
175
|
+
if (!state || typeof state.st_ports === 'undefined') {
|
|
176
|
+
return this.reported;
|
|
177
|
+
}
|
|
178
|
+
logger.debug(`version: ${version}`);
|
|
179
|
+
logger.debug(`state.st_ports: ${JSON.stringify(state.st_ports)}`);
|
|
180
|
+
await this.CleanupReportedPorts(state.st_ports);
|
|
181
|
+
|
|
182
|
+
for (const item of state.st_ports) {
|
|
183
|
+
logger.debug(`desiredItem: ${JSON.stringify(item)}`);
|
|
184
|
+
const existingPort = this.reported.st_ports.find(
|
|
185
|
+
(portInfo) =>
|
|
186
|
+
portInfo.ip === item.ip &&
|
|
187
|
+
portInfo.port === item.port &&
|
|
188
|
+
portInfo.type === item.type
|
|
189
|
+
);
|
|
190
|
+
logger.debug(`existingPort: ${JSON.stringify(existingPort)}`);
|
|
191
|
+
|
|
192
|
+
const numberOfEnabledPorts: number = this.reported.st_ports.reduce(
|
|
193
|
+
(acc, port) => {
|
|
194
|
+
return port.enabled ? acc + 1 : acc;
|
|
195
|
+
},
|
|
196
|
+
0
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
// check that port already exist in the reported shadow
|
|
200
|
+
if (!existingPort) {
|
|
201
|
+
if (item.enabled && item.type === SecureTunnelServiceType.HTTP) {
|
|
202
|
+
await this.startProxyMapping(item);
|
|
203
|
+
}
|
|
204
|
+
this.reported.st_ports.push(item);
|
|
205
|
+
} else if (existingPort.enabled !== item.enabled) {
|
|
206
|
+
if (item.type === SecureTunnelServiceType.HTTP) {
|
|
207
|
+
if (item.enabled) {
|
|
208
|
+
await this.startProxyMapping(item);
|
|
209
|
+
} else {
|
|
210
|
+
await this.stopProxyMapping(item);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
existingPort.enabled = item.enabled;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// if all entries are disabled, we need also to kill local proxy
|
|
218
|
+
if (this.reported.st_ports.every((portInfo) => !portInfo.enabled)) {
|
|
219
|
+
await this.stopLocalproxy();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// need to order list in the same order as desired before sending back
|
|
223
|
+
const sortedPorts = this.sortPorts(state);
|
|
224
|
+
|
|
225
|
+
logger.debug(`sortedPorts: ${JSON.stringify(sortedPorts)}`);
|
|
226
|
+
logger.debug('<- SecureTunnelHandlerSingleton.syncShadowToDeviceState');
|
|
227
|
+
return sortedPorts;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Starts SecureTunnel
|
|
232
|
+
* @param {SecureTunnelNotificationType} message - AWS notification received
|
|
233
|
+
*/
|
|
234
|
+
public async secureTunnelNotifyHandler(
|
|
235
|
+
message: SecureTunnelNotificationType
|
|
236
|
+
): Promise<void> {
|
|
237
|
+
logger.debug('-> SecureTunnelHandlerSingleton.secureTunnelNotifyHandler');
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
await this.stopLocalproxy();
|
|
241
|
+
this.processNotifyMessage(message);
|
|
242
|
+
await this.downloadSecureTunnel();
|
|
243
|
+
await this.startLocalproxy();
|
|
244
|
+
} catch (error) {
|
|
245
|
+
logger.error(error);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
logger.info(`Local Proxy Started: ${JSON.stringify(this.localproxyInfo)}`);
|
|
249
|
+
logger.debug('<- SecureTunnelHandlerSingleton.secureTunnelNotifyHandler');
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
//---------------------------------------------------------------------------
|
|
253
|
+
// private functions
|
|
254
|
+
//---------------------------------------------------------------------------
|
|
255
|
+
/**
|
|
256
|
+
* Removes reported ports which are do not exist in desired
|
|
257
|
+
* @param {SecureTunnelPortInfo[]} desiredPorts - desired port config
|
|
258
|
+
*/
|
|
259
|
+
private async CleanupReportedPorts(desiredPorts: SecureTunnelPortInfo[]) {
|
|
260
|
+
logger.debug('-> SecureTunnelHandlerSingleton.CleanupReportedPorts');
|
|
261
|
+
const itemsToRemove: SecureTunnelPortInfo[] = [];
|
|
262
|
+
|
|
263
|
+
for (const item of this.reported.st_ports) {
|
|
264
|
+
logger.debug(`Checking item: ${JSON.stringify(item)}`);
|
|
265
|
+
if (
|
|
266
|
+
!desiredPorts.some(
|
|
267
|
+
(port) =>
|
|
268
|
+
port.ip === item.ip &&
|
|
269
|
+
port.port === item.port &&
|
|
270
|
+
port.type === item.type
|
|
271
|
+
)
|
|
272
|
+
) {
|
|
273
|
+
logger.debug(`Marking item for removal: ${JSON.stringify(item)}`);
|
|
274
|
+
itemsToRemove.push(item);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
for (const item of itemsToRemove) {
|
|
279
|
+
const index = this.reported.st_ports.indexOf(item);
|
|
280
|
+
if (index !== -1) {
|
|
281
|
+
const removedItem = this.reported.st_ports.splice(index, 1)[0];
|
|
282
|
+
logger.debug(`Removing item: ${JSON.stringify(removedItem)}`);
|
|
283
|
+
if (removedItem.type === SecureTunnelServiceType.HTTP) {
|
|
284
|
+
await this.stopProxyMapping(removedItem);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
logger.debug(`reportedPorts: ${JSON.stringify(this.reported.st_ports)}`);
|
|
290
|
+
logger.debug('<- SecureTunnelHandlerSingleton.CleanupReportedPorts');
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Starts port proxy mapping process
|
|
295
|
+
* @param {SecureTunnelPortInfo} portInfo - port info to start the port mapping process for
|
|
296
|
+
*/
|
|
297
|
+
private async startProxyMapping(
|
|
298
|
+
portInfo: SecureTunnelPortInfo
|
|
299
|
+
): Promise<void> {
|
|
300
|
+
logger.debug('-> SecureTunnelHandlerSingleton.startProxyMapping');
|
|
301
|
+
logger.debug(`portInfo: ${JSON.stringify(portInfo)}`);
|
|
302
|
+
|
|
303
|
+
// if there is already a process running for the ip:port, don't start another one
|
|
304
|
+
const itemIndex = this.httpProxyMap.findIndex(
|
|
305
|
+
(item) =>
|
|
306
|
+
item.st_port.ip === portInfo.ip && item.st_port.port === portInfo.port
|
|
307
|
+
);
|
|
308
|
+
if (itemIndex !== -1) {
|
|
309
|
+
logger.debug(`socat already active for: ${portInfo.ip}:${portInfo.port}`);
|
|
310
|
+
logger.debug('<- SecureTunnelHandlerSingleton.startProxyMapping');
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const localPort = this.getNextAvailablePort();
|
|
315
|
+
try {
|
|
316
|
+
logger.info(`Starting Port Proxy Mapping on ${localPort}`);
|
|
317
|
+
const args = [
|
|
318
|
+
`tcp4-listen:${localPort},fork`,
|
|
319
|
+
`tcp4:${portInfo.ip}:${portInfo.port}`
|
|
320
|
+
];
|
|
321
|
+
const childProcess = await runDetachedProcess(ST_MAPPING_TOOL, args);
|
|
322
|
+
// TODO: if there is a device restart, to restore port mapping,
|
|
323
|
+
// this info needs to be saved on drive to be able to recover
|
|
324
|
+
this.httpProxyMap.push({
|
|
325
|
+
st_port: portInfo,
|
|
326
|
+
localhostPort: localPort,
|
|
327
|
+
mapProcess: childProcess
|
|
328
|
+
});
|
|
329
|
+
logger.info(
|
|
330
|
+
`Started Port Proxy Mapping for: ${JSON.stringify(
|
|
331
|
+
this.httpProxyMap[this.httpProxyMap.length - 1]
|
|
332
|
+
)}`
|
|
333
|
+
);
|
|
334
|
+
} catch (error) {
|
|
335
|
+
logger.error(
|
|
336
|
+
`ERROR: starting socat for: ${portInfo.ip}:${portInfo.port} on localhost port: ${localPort}, error: ${error}`
|
|
337
|
+
);
|
|
338
|
+
const lastHttpProxyMap = this.httpProxyMap.pop();
|
|
339
|
+
logger.info(`removed last proxyMap: ${JSON.stringify(lastHttpProxyMap)}`);
|
|
340
|
+
}
|
|
341
|
+
logger.debug('<- SecureTunnelHandlerSingleton.startProxyMapping');
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Stops port proxy mapping process
|
|
346
|
+
* @param {SecureTunnelPortInfo} portInfo - port info to stop the port mapping process for
|
|
347
|
+
*/
|
|
348
|
+
private async stopProxyMapping(
|
|
349
|
+
portInfo: SecureTunnelPortInfo
|
|
350
|
+
): Promise<void> {
|
|
351
|
+
logger.debug('-> SecureTunnelHandlerSingleton.stopProxyMapping');
|
|
352
|
+
logger.debug(`portInfo: ${JSON.stringify(portInfo)}`);
|
|
353
|
+
|
|
354
|
+
const itemIndex = this.httpProxyMap.findIndex(
|
|
355
|
+
(item) =>
|
|
356
|
+
item.st_port.ip === portInfo.ip && item.st_port.port === portInfo.port
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
if (itemIndex !== -1) {
|
|
360
|
+
logger.info(
|
|
361
|
+
`Stopping Port Proxy Mapping for: ${JSON.stringify(
|
|
362
|
+
this.httpProxyMap[itemIndex]
|
|
363
|
+
)}`
|
|
364
|
+
);
|
|
365
|
+
try {
|
|
366
|
+
const processName = [
|
|
367
|
+
ST_MAPPING_TOOL,
|
|
368
|
+
`tcp4-listen:${this.httpProxyMap[itemIndex].localhostPort},fork`,
|
|
369
|
+
`tcp4:${portInfo.ip}:${portInfo.port}`
|
|
370
|
+
];
|
|
371
|
+
await killDetachedProcess(this.httpProxyMap[itemIndex].mapProcess, [
|
|
372
|
+
processName.join(' ')
|
|
373
|
+
]);
|
|
374
|
+
logger.debug(
|
|
375
|
+
`SUCCESS: killing socat process: ${JSON.stringify(
|
|
376
|
+
this.httpProxyMap[itemIndex]
|
|
377
|
+
)}`
|
|
378
|
+
);
|
|
379
|
+
logger.debug(`Remaining map: ${JSON.stringify(this.httpProxyMap)}`);
|
|
380
|
+
} catch (e) {
|
|
381
|
+
logger.error('ERROR: killing socat process:', e);
|
|
382
|
+
}
|
|
383
|
+
logger.debug(
|
|
384
|
+
`Removing map: ${JSON.stringify(this.httpProxyMap[itemIndex])}`
|
|
385
|
+
);
|
|
386
|
+
this.httpProxyMap.splice(itemIndex, 1);
|
|
387
|
+
}
|
|
388
|
+
logger.debug('<- SecureTunnelHandlerSingleton.stopProxyMapping');
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Starts SecureTunnel localproxy process
|
|
393
|
+
*/
|
|
394
|
+
private async startLocalproxy(): Promise<void> {
|
|
395
|
+
logger.debug('-> SecureTunnelHandlerSingleton.startLocalproxy');
|
|
396
|
+
|
|
397
|
+
const args = [
|
|
398
|
+
'--destination-app',
|
|
399
|
+
this.localproxyInfo.lpServices,
|
|
400
|
+
'--region',
|
|
401
|
+
this.localproxyInfo.lpRegion,
|
|
402
|
+
'--capath',
|
|
403
|
+
AWS_ROOT_CERTIFICATE_FILE_PATH,
|
|
404
|
+
'--local-bind-address',
|
|
405
|
+
'0.0.0.0',
|
|
406
|
+
'-t',
|
|
407
|
+
this.localproxyInfo.lpDstAccessKey
|
|
408
|
+
];
|
|
409
|
+
|
|
410
|
+
this.localproxyInfo.lpProcess = await runDetachedProcess(
|
|
411
|
+
SECURE_TUNNEL_BIN_PATH,
|
|
412
|
+
args
|
|
413
|
+
);
|
|
414
|
+
logger.debug('<- SecureTunnelHandlerSingleton.startLocalproxy');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Stops SecureTunnel localproxy process
|
|
419
|
+
*/
|
|
420
|
+
private async stopLocalproxy(): Promise<void> {
|
|
421
|
+
logger.debug('-> SecureTunnelHandlerSingleton.stopLocalproxy');
|
|
422
|
+
if (!this.localproxyInfo.lpProcess) {
|
|
423
|
+
logger.debug('No localproxy process running');
|
|
424
|
+
logger.debug('<- SecureTunnelHandlerSingleton.stopLocalproxy');
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
try {
|
|
429
|
+
logger.debug(
|
|
430
|
+
`About to kill localproxy: ${this.localproxyInfo.lpDstAccessKey}`
|
|
431
|
+
);
|
|
432
|
+
await killDetachedProcess(this.localproxyInfo.lpProcess, [
|
|
433
|
+
SECURE_TUNNEL_BIN_PATH
|
|
434
|
+
]);
|
|
435
|
+
logger.debug('SUCCESS: killing localproxy process');
|
|
436
|
+
} catch (e) {
|
|
437
|
+
logger.error('ERROR: killing localproxy process:', e);
|
|
438
|
+
}
|
|
439
|
+
this.localproxyInfo = {
|
|
440
|
+
lpDstAccessKey: '',
|
|
441
|
+
lpRegion: '',
|
|
442
|
+
lpServices: '',
|
|
443
|
+
lpProcess: null
|
|
444
|
+
};
|
|
445
|
+
logger.debug('<- SecureTunnelHandlerSingleton.stopLocalproxy');
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* processes and validate notify message
|
|
450
|
+
* @param {string[]} message - message, which contains: clientAccessToken, region, services
|
|
451
|
+
*/
|
|
452
|
+
private processNotifyMessage(message: SecureTunnelNotificationType): void {
|
|
453
|
+
logger.debug('-> SecureTunnelHandlerSingleton.processNotifyMessage');
|
|
454
|
+
const { clientAccessToken, region, services } = message;
|
|
455
|
+
const portMappingList: string[] = [];
|
|
456
|
+
|
|
457
|
+
if (clientAccessToken === '') {
|
|
458
|
+
throw new Error('ERROR: invalid destination access token');
|
|
459
|
+
}
|
|
460
|
+
if (!isValidAwsRegion(region)) {
|
|
461
|
+
throw new Error(`ERROR: invalid/unsupported region: ${region}`);
|
|
462
|
+
}
|
|
463
|
+
if (services.length === 0) {
|
|
464
|
+
throw new Error(`ERROR: services field is empty: ${region}`);
|
|
465
|
+
}
|
|
466
|
+
if (services.some((field) => field === '')) {
|
|
467
|
+
throw new Error(`ERROR: one service fields is empty: ${region}`);
|
|
468
|
+
}
|
|
469
|
+
if (
|
|
470
|
+
services.some(
|
|
471
|
+
(service) =>
|
|
472
|
+
!service.startsWith(SecureTunnelServiceType.SSH) &&
|
|
473
|
+
!service.startsWith(SecureTunnelServiceType.HTTP)
|
|
474
|
+
)
|
|
475
|
+
) {
|
|
476
|
+
throw new Error(
|
|
477
|
+
`ERROR: one service fields is invalid: ${JSON.stringify(services)}`
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
const sshEnabledPorts = this.reported.st_ports.filter(
|
|
481
|
+
(port) => port.type === SecureTunnelServiceType.SSH && port.enabled
|
|
482
|
+
);
|
|
483
|
+
const sshServicePorts = services.filter((service) =>
|
|
484
|
+
service.startsWith(SecureTunnelServiceType.SSH)
|
|
485
|
+
);
|
|
486
|
+
const httpServicePorts = services.filter((service) =>
|
|
487
|
+
service.startsWith(SecureTunnelServiceType.HTTP)
|
|
488
|
+
);
|
|
489
|
+
if (sshServicePorts.length > 1) {
|
|
490
|
+
throw new Error(
|
|
491
|
+
`ERROR: None or only 1 SSH port is allowed! sshEnabledPortsCount: ${sshEnabledPorts.length}`
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
// the new SSH mismatch, but NOT the SSH, because that device without shadow
|
|
495
|
+
if (
|
|
496
|
+
sshEnabledPorts.length !== sshServicePorts.length &&
|
|
497
|
+
sshServicePorts[0] !== SecureTunnelServiceType.SSH
|
|
498
|
+
) {
|
|
499
|
+
throw new Error(
|
|
500
|
+
`ERROR: SSH ports mismatch! sshEnabledPortsCount: ${sshEnabledPorts.length}, sshServicePortsCount: ${sshServicePorts.length}`
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
if (this.httpProxyMap.length !== httpServicePorts.length) {
|
|
504
|
+
throw new Error(
|
|
505
|
+
`ERROR: HTTP ports mismatch! httpMappedPortsCount: ${this.httpProxyMap.length}, httpServicePortsCount: ${httpServicePorts.length}`
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// this is the default case: just 1 SSH without any HTTP service
|
|
510
|
+
if (
|
|
511
|
+
services.length === 1 &&
|
|
512
|
+
services[0] === defaultSecureTunnelPortInfo.type
|
|
513
|
+
) {
|
|
514
|
+
portMappingList.push(defaultSecureTunnelPortInfo.port.toString());
|
|
515
|
+
} else {
|
|
516
|
+
// this is the multi port case, need port mapping to running services
|
|
517
|
+
sshServicePorts.forEach((port, index) => {
|
|
518
|
+
portMappingList.push(`${port}=${sshEnabledPorts[index].port}`);
|
|
519
|
+
});
|
|
520
|
+
httpServicePorts.forEach((port, index) => {
|
|
521
|
+
portMappingList.push(
|
|
522
|
+
`${port}=${this.httpProxyMap[index].localhostPort}`
|
|
523
|
+
);
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
this.localproxyInfo.lpDstAccessKey = clientAccessToken;
|
|
528
|
+
this.localproxyInfo.lpRegion = region;
|
|
529
|
+
this.localproxyInfo.lpServices = portMappingList.join(',');
|
|
530
|
+
logger.debug(`reported = ${JSON.stringify(this.reported.st_ports)}`);
|
|
531
|
+
logger.debug(`localproxyInfo = ${JSON.stringify(this.localproxyInfo)}`);
|
|
532
|
+
logger.debug('<- SecureTunnelHandlerSingleton.processNotifyMessage');
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Downloads SecureTunnel localproxy, if it was not downloaded before
|
|
537
|
+
*/
|
|
538
|
+
private async downloadSecureTunnel(): Promise<void> {
|
|
539
|
+
logger.debug('-> SecureTunnelHandlerSingleton.downloadSecureTunnel');
|
|
540
|
+
|
|
541
|
+
if (await JsSpawner().exists(SECURE_TUNNEL_BIN_PATH)) {
|
|
542
|
+
logger.debug('<- SecureTunnelHandlerSingleton.downloadSecureTunnel');
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const [arch, linuxDistro, osVersion] = await Promise.all([
|
|
547
|
+
getArch(),
|
|
548
|
+
getDistribution(),
|
|
549
|
+
getOsVersion()
|
|
550
|
+
]);
|
|
551
|
+
|
|
552
|
+
logger.info('Downloading SecureTunnel localproxy ...');
|
|
553
|
+
const url = `${aaiArtifactsBucketUrl}/securetunnel/${linuxDistro}/${osVersion}/${arch}/${SECURE_TUNNEL_BIN_NAME}`;
|
|
554
|
+
await JsSpawner().mkdirp(join(AAI_DIR, SECURE_TUNNEL_BIN_DIR));
|
|
555
|
+
await downloadFile({
|
|
556
|
+
url,
|
|
557
|
+
path: SECURE_TUNNEL_BIN_PATH,
|
|
558
|
+
errorMessage: `Secure Tunnel bin for ${linuxDistro} ${osVersion} ${arch} not found}`
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
await JsSpawner().run({
|
|
562
|
+
exe: 'chmod',
|
|
563
|
+
args: ['+x', SECURE_TUNNEL_BIN_PATH]
|
|
564
|
+
});
|
|
565
|
+
logger.debug('<- SecureTunnelHandlerSingleton.downloadSecureTunnel');
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Gets next available localhost port
|
|
570
|
+
*/
|
|
571
|
+
private getNextAvailablePort(): number {
|
|
572
|
+
logger.debug('-> SecureTunnelHandlerSingleton.getNextAvailablePort');
|
|
573
|
+
let lastLocalhostPort: number = ST_START_PORT_NUMBER;
|
|
574
|
+
if (this.httpProxyMap.length > 0) {
|
|
575
|
+
lastLocalhostPort = this.httpProxyMap.reduce((maxPort, proxy) => {
|
|
576
|
+
return proxy.localhostPort > maxPort ? proxy.localhostPort : maxPort;
|
|
577
|
+
}, ST_START_PORT_NUMBER);
|
|
578
|
+
}
|
|
579
|
+
// for now just check whether we are using specific port number
|
|
580
|
+
// TODO: in the future we need to check whether some other services are using a specific port
|
|
581
|
+
// possible way to check taken ports: "sudo netstat -tuln | grep <port_number>""
|
|
582
|
+
logger.debug(`lastLocalhostPort: ${(lastLocalhostPort + 1).toString()}`);
|
|
583
|
+
logger.debug('<- SecureTunnelHandlerSingleton.getNextAvailablePort');
|
|
584
|
+
return lastLocalhostPort + 1;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
private sortPorts(
|
|
588
|
+
desired: SecureTunnelShadowDesRep
|
|
589
|
+
): SecureTunnelShadowDesRep {
|
|
590
|
+
logger.debug('-> SecureTunnelHandlerSingleton.sortPorts');
|
|
591
|
+
const sortedPorts = JSON.parse(JSON.stringify(this.reported));
|
|
592
|
+
sortedPorts.st_ports.sort((a, b) => {
|
|
593
|
+
const aOriginalIndex = desired.st_ports.findIndex(
|
|
594
|
+
(item) =>
|
|
595
|
+
item.type === a.type && item.ip === a.ip && item.port === a.port
|
|
596
|
+
);
|
|
597
|
+
const bOriginalIndex = desired.st_ports.findIndex(
|
|
598
|
+
(item) =>
|
|
599
|
+
item.type === b.type && item.ip === b.ip && item.port === b.port
|
|
600
|
+
);
|
|
601
|
+
return aOriginalIndex - bOriginalIndex;
|
|
602
|
+
});
|
|
603
|
+
logger.debug('<- SecureTunnelHandlerSingleton.sortPorts');
|
|
604
|
+
return sortedPorts;
|
|
605
|
+
}
|
|
606
|
+
}
|