Skip to content

Commit 6c25f23

Browse files
committed
fix(socket): fix build permissions on new triggered build appearing on UI
1 parent aa1136f commit 6c25f23

File tree

5 files changed

+71
-22
lines changed

5 files changed

+71
-22
lines changed

package-lock.json

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"@types/temp": "^0.8.31",
102102
"@types/uuid": "^3.4.3",
103103
"@types/webpack-dev-server": "^2.9.1",
104+
"@types/ws": "^3.2.1",
104105
"@types/yamljs": "^0.2.30",
105106
"add-asset-html-webpack-plugin": "^2.1.2",
106107
"angular2-jwt": "^0.2.3",
@@ -179,6 +180,7 @@
179180
"webpack-dev-server": "^2.9.4",
180181
"webpack-dll-bundles-plugin": "^1.0.0-beta.5",
181182
"webpack-merge": "^4.1.1",
183+
"ws": "^3.3.2",
182184
"xterm": "2.9.2",
183185
"yamljs": "^0.3.0",
184186
"zone.js": "0.8.18"

src/api/process-manager.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -495,14 +495,13 @@ export function startBuild(data: any, buildConfig?: any): Promise<any> {
495495
}).then(() => queueJob(dataJob.id));
496496
}));
497497
})
498-
.then(() => getLastBuild(null))
499498
.then(lastBuild => {
500499
jobEvents.next({
501500
type: 'process',
502501
build_id: data.build_id,
503502
repository_id: repoId,
504503
data: 'build added',
505-
additionalData: lastBuild
504+
additionalData: null
506505
});
507506
})
508507
.then(() => getDepracatedBuilds(buildData))

src/api/socket.ts

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { sessionParser } from './server';
2525
import { IMemoryData, memory } from './stats/memory';
2626
import { ICpuData, cpu } from './stats/cpu';
2727
import { decodeJwt } from './security';
28+
import { getLastBuild } from './db/build';
2829

2930
export interface ISocketServerOptions {
3031
app: express.Application;
@@ -112,6 +113,19 @@ export class SocketServer {
112113
};
113114
this.addClient(client);
114115

116+
client.send({ type: 'time', data: new Date().getTime() });
117+
jobEvents.subscribe(event => {
118+
if (event.data === 'build added') {
119+
getLastBuild(client.session.userId)
120+
.then(lastBuild => {
121+
event.additionalData = lastBuild;
122+
client.send(event);
123+
});
124+
} else {
125+
client.send(event);
126+
}
127+
});
128+
115129
socket.on('message', event => this.handleEvent(JSON.parse(event), client));
116130
socket.on('close', (code, message) => this.removeClient(socket));
117131
});

tests/unit/040_socket.spec.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Socket Security', () => {
2626
.then(() => process.chdir(join(__dirname, '../')))
2727
.then(() => {
2828
tempRoot = temp.mkdirSync('abstruse-socket-tests');
29-
console.log(`Using "${tempRoot}" as temporary directory for e2e protractor tests.`);
29+
console.log(`Using "${tempRoot}" as temporary directory for socket unit tests.`);
3030
})
3131
.then(() => abstruse(tempRoot, false))
3232
.then(() => sendRequest(registerData, 'api/user/create'));
@@ -35,7 +35,7 @@ describe('Socket Security', () => {
3535
after(() => killAllProcesses());
3636

3737
it(`should receive initial 'time' event after connected to socket server`, (done) => {
38-
socket = new ws('ws://localhost:6500', null);
38+
socket = new ws('ws://localhost:6500');
3939

4040
socket.on('message', (data: any) => {
4141
data = JSON.parse(data);
@@ -47,7 +47,7 @@ describe('Socket Security', () => {
4747
});
4848

4949
it(`should not have permissions to trigger build image on 'buildImage' event`, (done) => {
50-
socket = new ws('ws://localhost:6500', null);
50+
socket = new ws('ws://localhost:6500');
5151

5252
socket.on('message', (data: any) => {
5353
data = JSON.parse(data);
@@ -66,7 +66,7 @@ describe('Socket Security', () => {
6666

6767
sendRequest(loginData, 'api/user/login')
6868
.then((jwt: any) => {
69-
socket = new ws('ws://localhost:6500', jwt.data);
69+
socket = new ws('ws://localhost:6500/?token=' + jwt.data);
7070

7171
socket.on('message', (data: any) => {
7272
data = JSON.parse(data);
@@ -83,7 +83,7 @@ describe('Socket Security', () => {
8383
});
8484

8585
it(`should not have permissions to trigger 'subscribeToImageBuilder' event`, (done) => {
86-
socket = new ws('ws://localhost:6500', null);
86+
socket = new ws('ws://localhost:6500');
8787

8888
socket.on('message', (data: any) => {
8989
data = JSON.parse(data);
@@ -104,7 +104,7 @@ describe('Socket Security', () => {
104104

105105
sendRequest(loginData, 'api/user/login')
106106
.then((jwt: any) => {
107-
socket = new ws('ws://localhost:6500', jwt.data);
107+
socket = new ws('ws://localhost:6500/?token=' + jwt.data);
108108

109109
socket.on('message', (data: any) => {
110110
data = JSON.parse(data);
@@ -121,7 +121,7 @@ describe('Socket Security', () => {
121121
});
122122

123123
it(`should not have permissions to trigger 'stopBuild' event`, (done) => {
124-
socket = new ws('ws://localhost:6500', null);
124+
socket = new ws('ws://localhost:6500');
125125

126126
socket.on('message', (data: any) => {
127127
data = JSON.parse(data);
@@ -140,7 +140,7 @@ describe('Socket Security', () => {
140140

141141
sendRequest(loginData, 'api/user/login')
142142
.then((jwt: any) => {
143-
socket = new ws('ws://localhost:6500', jwt.data);
143+
socket = new ws('ws://localhost:6500/?token=' + jwt.data);
144144

145145
socket.on('message', (data: any) => {
146146
data = JSON.parse(data);
@@ -157,7 +157,7 @@ describe('Socket Security', () => {
157157
});
158158

159159
it(`should not have permissions to trigger 'restartBuild' event`, (done) => {
160-
socket = new ws('ws://localhost:6500', null);
160+
socket = new ws('ws://localhost:6500');
161161

162162
socket.on('message', (data: any) => {
163163
data = JSON.parse(data);
@@ -176,7 +176,7 @@ describe('Socket Security', () => {
176176

177177
sendRequest(loginData, 'api/user/login')
178178
.then((jwt: any) => {
179-
socket = new ws('ws://localhost:6500', jwt.data);
179+
socket = new ws('ws://localhost:6500/?token=' + jwt.data);
180180

181181
socket.on('message', (data: any) => {
182182
data = JSON.parse(data);
@@ -193,7 +193,7 @@ describe('Socket Security', () => {
193193
});
194194

195195
it(`should not have permissions to trigger 'restartJob' event`, (done) => {
196-
socket = new ws('ws://localhost:6500', null);
196+
socket = new ws('ws://localhost:6500');
197197

198198
socket.on('message', (data: any) => {
199199
data = JSON.parse(data);
@@ -212,7 +212,7 @@ describe('Socket Security', () => {
212212

213213
sendRequest(loginData, 'api/user/login')
214214
.then((jwt: any) => {
215-
socket = new ws('ws://localhost:6500', jwt.data);
215+
socket = new ws('ws://localhost:6500/?token=' + jwt.data);
216216

217217
socket.on('message', (data: any) => {
218218
data = JSON.parse(data);
@@ -229,7 +229,7 @@ describe('Socket Security', () => {
229229
});
230230

231231
it(`should not have permissions to trigger 'stopJob' event`, (done) => {
232-
socket = new ws('ws://localhost:6500', null);
232+
socket = new ws('ws://localhost:6500');
233233

234234
socket.on('message', (data: any) => {
235235
data = JSON.parse(data);
@@ -248,7 +248,7 @@ describe('Socket Security', () => {
248248

249249
sendRequest(loginData, 'api/user/login')
250250
.then((jwt: any) => {
251-
socket = new ws('ws://localhost:6500', jwt.data);
251+
socket = new ws('ws://localhost:6500/?token=' + jwt.data);
252252

253253
socket.on('message', (data: any) => {
254254
data = JSON.parse(data);
@@ -265,7 +265,7 @@ describe('Socket Security', () => {
265265
});
266266

267267
it(`should not have permissions to trigger 'debugJob' event`, (done) => {
268-
socket = new ws('ws://localhost:6500', null);
268+
socket = new ws('ws://localhost:6500');
269269

270270
socket.on('message', (data: any) => {
271271
data = JSON.parse(data);
@@ -285,7 +285,7 @@ describe('Socket Security', () => {
285285

286286
sendRequest(loginData, 'api/user/login')
287287
.then((jwt: any) => {
288-
socket = new ws('ws://localhost:6500', jwt.data);
288+
socket = new ws('ws://localhost:6500/?token=' + jwt.data);
289289

290290
socket.on('message', (data: any) => {
291291
data = JSON.parse(data);
@@ -302,7 +302,7 @@ describe('Socket Security', () => {
302302
});
303303

304304
it(`should not have permissions to trigger 'subscribeToLogs' event`, (done) => {
305-
socket = new ws('ws://localhost:6500', null);
305+
socket = new ws('ws://localhost:6500');
306306

307307
socket.on('message', (data: any) => {
308308
data = JSON.parse(data);
@@ -321,7 +321,7 @@ describe('Socket Security', () => {
321321

322322
sendRequest(loginData, 'api/user/login')
323323
.then((jwt: any) => {
324-
socket = new ws('ws://localhost:6500', jwt.data);
324+
socket = new ws('ws://localhost:6500/?token=' + jwt.data);
325325

326326
socket.on('message', (data: any) => {
327327
data = JSON.parse(data);
@@ -338,7 +338,7 @@ describe('Socket Security', () => {
338338
});
339339

340340
it(`should not have permissions to trigger 'subscribeToNotifications' event`, (done) => {
341-
socket = new ws('ws://localhost:6500', null);
341+
socket = new ws('ws://localhost:6500');
342342

343343
socket.on('message', (data: any) => {
344344
data = JSON.parse(data);
@@ -358,7 +358,7 @@ describe('Socket Security', () => {
358358

359359
sendRequest(loginData, 'api/user/login')
360360
.then((jwt: any) => {
361-
socket = new ws('ws://localhost:6500', jwt.data);
361+
socket = new ws('ws://localhost:6500/?token=' + jwt.data);
362362

363363
socket.on('message', (data: any) => {
364364
data = JSON.parse(data);

0 commit comments

Comments
 (0)