Skip to content

Commit f22523b

Browse files
committed
chore: fixes
1 parent ed9a7f8 commit f22523b

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed

packages/cubejs-api-gateway/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"typings": "dist/src/index.d.ts",
1616
"scripts": {
1717
"test": "npm run unit",
18-
"unit": "CUBE_JS_NATIVE_API_GATEWAY_INTERNAL=true jest --coverage dist/test",
18+
"unit": "CUBE_JS_NATIVE_API_GATEWAY_INTERNAL=true jest --coverage --forceExit dist/test",
1919
"build": "rm -rf dist && npm run tsc",
2020
"tsc": "tsc",
2121
"watch": "tsc -w",

packages/cubejs-api-gateway/src/sql-server.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
setupLogger,
3-
resetLogger,
43
registerInterface,
54
shutdownInterface,
65
execSql,
@@ -15,7 +14,7 @@ import { displayCLIWarning, getEnv } from '@cubejs-backend/shared';
1514

1615
import * as crypto from 'crypto';
1716
import type { ApiGateway } from './gateway';
18-
import type { CheckAuthFn, CheckSQLAuthFn, ExtendedRequestContext, CanSwitchSQLUserFn } from './interfaces';
17+
import type { CheckSQLAuthFn, ExtendedRequestContext, CanSwitchSQLUserFn } from './interfaces';
1918

2019
export type SQLServerOptions = {
2120
checkSqlAuth?: CheckSQLAuthFn,
@@ -381,9 +380,5 @@ export class SQLServer {
381380

382381
public async shutdown(mode: ShutdownMode): Promise<void> {
383382
await shutdownInterface(this.sqlInterfaceInstance!, mode);
384-
385-
resetLogger(
386-
process.env.CUBEJS_LOG_LEVEL === 'trace' ? 'trace' : 'warn'
387-
);
388383
}
389384
}

packages/cubejs-api-gateway/test/auth.test.ts

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import express, { Application as ExpressApplication, RequestHandler } from 'expr
33
// eslint-disable-next-line import/no-extraneous-dependencies
44
import request from 'supertest';
55
import jwt from 'jsonwebtoken';
6-
import { getEnv, pausePromise } from '@cubejs-backend/shared';
6+
import { pausePromise } from '@cubejs-backend/shared';
7+
import { resetLogger } from '@cubejs-backend/native';
78

8-
import { ApiGateway, ApiGatewayOptions, CubejsHandlerError, Request } from '../src';
9+
import { ApiGateway, ApiGatewayOptions, CubejsHandlerError, Request, RequestContext } from '../src';
910
import { AdapterApiMock, DataSourceStorageMock } from './mocks';
10-
import { RequestContext } from '../src/interfaces';
1111
import { generateAuthToken } from './utils';
1212

1313
class ApiGatewayOpenAPI extends ApiGateway {
@@ -33,6 +33,12 @@ class ApiGatewayOpenAPI extends ApiGateway {
3333
} finally {
3434
this.isRunning = null;
3535
}
36+
37+
// SQLServer changes logger for rust side with setupLogger in the constructor, but it leads
38+
// to a memory leak, that's why jest doesn't allow to shut down tests
39+
resetLogger(
40+
process.env.CUBEJS_LOG_LEVEL === 'trace' ? 'trace' : 'warn'
41+
);
3642
}
3743
}
3844

@@ -64,6 +70,7 @@ function createApiGateway(handler: RequestHandler, logger: () => any, options: P
6470
});
6571

6672
process.env.NODE_ENV = 'unknown';
73+
6774
const app = express();
6875
apiGateway.initApp(app);
6976

@@ -74,27 +81,28 @@ function createApiGateway(handler: RequestHandler, logger: () => any, options: P
7481
}
7582

7683
describe('test authorization with native gateway', () => {
77-
const expectSecurityContext = (securityContext) => {
78-
expect(securityContext.uid).toEqual(5);
79-
expect(securityContext.iat).toBeDefined();
80-
expect(securityContext.exp).toBeDefined();
81-
};
82-
8384
let app: ExpressApplication;
8485
let apiGateway: ApiGatewayOpenAPI;
8586

86-
const handlerMock = jest.fn((req, res) => {
87-
expectSecurityContext(req.context.authInfo);
88-
expectSecurityContext(req.context.securityContext);
89-
90-
res.status(200).end();
87+
const handlerMock = jest.fn(() => {
88+
// nothing, we are using it to verify that we don't got to express code
9189
});
9290
const loggerMock = jest.fn(() => {
9391
//
9492
});
93+
const checkAuthMock = jest.fn((req, token) => {
94+
jwt.verify(token, 'secret');
95+
96+
return {
97+
security_context: {}
98+
};
99+
});
95100

96101
beforeAll(async () => {
97-
const result = createApiGateway(handlerMock, loggerMock, {});
102+
const result = createApiGateway(handlerMock, loggerMock, {
103+
checkAuth: checkAuthMock,
104+
gatewayPort: 8585,
105+
});
98106

99107
app = result.app;
100108
apiGateway = result.apiGateway;
@@ -105,15 +113,11 @@ describe('test authorization with native gateway', () => {
105113
beforeEach(() => {
106114
handlerMock.mockClear();
107115
loggerMock.mockClear();
116+
checkAuthMock.mockClear();
108117
});
109118

110119
afterAll(async () => {
111-
try {
112-
await apiGateway.shutdownSQLServer();
113-
} catch (error) {
114-
// TODO: Figure out, why ApiGatewayServer cannot shutdown!?
115-
console.log(`Error while shutting down server: ${error}`);
116-
}
120+
await apiGateway.shutdownSQLServer();
117121
});
118122

119123
it('default authorization - success', async () => {
@@ -122,43 +126,70 @@ describe('test authorization with native gateway', () => {
122126
await request(app)
123127
.get('/cubejs-api/v2/stream')
124128
.set('Authorization', `${token}`)
129+
.send()
125130
.expect(501);
126131

127132
// No bad logs
128133
expect(loggerMock.mock.calls.length).toEqual(0);
129134
// We should not call js handler, request should go into rust code
130135
expect(handlerMock.mock.calls.length).toEqual(0);
131136

132-
await apiGateway.shutdownSQLServer();
137+
// Verify that we passed token to JS side
138+
expect(checkAuthMock.mock.calls.length).toEqual(1);
139+
expect(checkAuthMock.mock.calls[0][0].protocol).toEqual('http');
140+
expect(checkAuthMock.mock.calls[0][1]).toEqual(token);
141+
});
142+
143+
it('default authorization - success (bearer prefix)', async () => {
144+
const token = generateAuthToken({ uid: 5, });
145+
146+
await request(app)
147+
.get('/cubejs-api/v2/stream')
148+
.set('Authorization', `Bearer ${token}`)
149+
.send()
150+
.expect(501);
151+
152+
// No bad logs
153+
expect(loggerMock.mock.calls.length).toEqual(0);
154+
// We should not call js handler, request should go into rust code
155+
expect(handlerMock.mock.calls.length).toEqual(0);
156+
157+
// Verify that we passed token to JS side
158+
expect(checkAuthMock.mock.calls.length).toEqual(1);
159+
expect(checkAuthMock.mock.calls[0][0].protocol).toEqual('http');
160+
expect(checkAuthMock.mock.calls[0][1]).toEqual(token);
133161
});
134162

135163
it('default authorization - wrong secret', async () => {
136-
const badToken = generateAuthToken({ uid: 5, }, {}, 'bad');
164+
const badToken = 'SUPER_LARGE_BAD_TOKEN_WHICH_IS_NOT_A_TOKEN';
137165

138166
await request(app)
139167
.get('/cubejs-api/v2/stream')
140168
.set('Authorization', `${badToken}`)
141-
.expect(403);
169+
.send()
170+
.expect(401);
142171

143172
// No bad logs
144173
expect(loggerMock.mock.calls.length).toEqual(0);
145174
// We should not call js handler, request should go into rust code
146175
expect(handlerMock.mock.calls.length).toEqual(0);
147176

148-
await apiGateway.shutdownSQLServer();
177+
// Verify that we passed token to JS side
178+
expect(checkAuthMock.mock.calls.length).toEqual(1);
179+
expect(checkAuthMock.mock.calls[0][0].protocol).toEqual('http');
180+
expect(checkAuthMock.mock.calls[0][1]).toEqual(badToken);
149181
});
150182

151183
it('default authorization - missing auth header', async () => {
152184
await request(app)
153185
.get('/cubejs-api/v2/stream')
154-
.expect(403);
186+
.send()
187+
.expect(401);
155188

156189
// No bad logs
157190
expect(loggerMock.mock.calls.length).toEqual(0);
158191
// We should not call js handler, request should go into rust code
159192
expect(handlerMock.mock.calls.length).toEqual(0);
160-
161-
await apiGateway.shutdownSQLServer();
162193
});
163194
});
164195

0 commit comments

Comments
 (0)