Skip to content

Commit 800a96b

Browse files
authored
feat(api-gateway, server-core): Renamed Permissions to ApiScopes (#6397)
1 parent cc13167 commit 800a96b

File tree

12 files changed

+172
-98
lines changed

12 files changed

+172
-98
lines changed

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

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
} from 'express';
2020
import {
2121
QueryType,
22-
Permission,
22+
ApiScopes,
2323
} from './types/strings';
2424
import {
2525
QueryType as QueryTypeEnum, ResultType
@@ -44,7 +44,7 @@ import {
4444
CheckAuthInternalOptions,
4545
JWTOptions,
4646
CheckAuthFn,
47-
ContextToPermissionsFn,
47+
ContextToApiScopesFn,
4848
} from './types/auth';
4949
import {
5050
Query,
@@ -117,9 +117,9 @@ class ApiGateway {
117117

118118
public readonly checkAuthSystemFn: CheckAuthFn;
119119

120-
protected readonly contextToPermFn: ContextToPermissionsFn;
120+
protected readonly contextToApiScopesFn: ContextToApiScopesFn;
121121

122-
protected readonly contextToPermDefFn: ContextToPermissionsFn =
122+
protected readonly contextToApiScopesDefFn: ContextToApiScopesFn =
123123
async () => ['liveliness', 'graphql', 'meta', 'data'];
124124

125125
protected readonly checkAuthMiddleware: CheckAuthMiddlewareFn;
@@ -158,7 +158,7 @@ class ApiGateway {
158158

159159
this.checkAuthFn = this.createCheckAuthFn(options);
160160
this.checkAuthSystemFn = this.createCheckAuthSystemFn();
161-
this.contextToPermFn = this.createContextToPermissionsFn(options);
161+
this.contextToApiScopesFn = this.createContextToApiScopesFn(options);
162162
this.checkAuthMiddleware = options.checkAuthMiddleware
163163
? this.wrapCheckAuthMiddleware(options.checkAuthMiddleware)
164164
: this.checkAuth;
@@ -178,19 +178,19 @@ class ApiGateway {
178178
];
179179

180180
/** **************************************************************
181-
* Liveliness permission *
181+
* Liveliness scope *
182182
*************************************************************** */
183183

184184
// @todo Should we pass requestLoggerMiddleware?
185-
// @todo Should we add permission assert here?
185+
// @todo Should we add scope assert here?
186186

187187
const guestMiddlewares = [];
188188

189189
app.get('/readyz', guestMiddlewares, cachedHandler(this.readiness));
190190
app.get('/livez', guestMiddlewares, cachedHandler(this.liveness));
191191

192192
/** **************************************************************
193-
* Graphql permission *
193+
* Graphql scope *
194194
*************************************************************** */
195195

196196
app.use(
@@ -199,7 +199,7 @@ class ApiGateway {
199199
...userMiddlewares,
200200
async (req, res, next) => {
201201
try {
202-
await this.assertPermission(
202+
await this.assertApiScope(
203203
'graphql',
204204
req?.context?.securityContext
205205
);
@@ -236,7 +236,7 @@ class ApiGateway {
236236
);
237237

238238
/** **************************************************************
239-
* Data permission *
239+
* Data scope *
240240
*************************************************************** */
241241

242242
app.get(`${this.basePath}/v1/load`, userMiddlewares, (async (req, res) => {
@@ -300,7 +300,7 @@ class ApiGateway {
300300
}));
301301

302302
/** **************************************************************
303-
* Meta permission *
303+
* Meta scope *
304304
*************************************************************** */
305305

306306
app.get(
@@ -327,7 +327,7 @@ class ApiGateway {
327327
...userMiddlewares,
328328
async (req, res, next) => {
329329
try {
330-
await this.assertPermission(
330+
await this.assertApiScope(
331331
'meta',
332332
req?.context?.securityContext
333333
);
@@ -353,7 +353,7 @@ class ApiGateway {
353353
);
354354

355355
/** **************************************************************
356-
* Jobs permission *
356+
* Jobs scope *
357357
*************************************************************** */
358358

359359
app.get(
@@ -375,7 +375,7 @@ class ApiGateway {
375375
);
376376

377377
/** **************************************************************
378-
* Private API (no permissions) *
378+
* Private API (no scopes) *
379379
*************************************************************** */
380380

381381
if (this.playgroundAuthSecret) {
@@ -473,7 +473,7 @@ class ApiGateway {
473473
}) {
474474
const requestStarted = new Date();
475475
try {
476-
await this.assertPermission('jobs', context.securityContext);
476+
await this.assertApiScope('jobs', context.securityContext);
477477
const refreshScheduler = this.refreshScheduler();
478478
res(await refreshScheduler.runScheduledRefresh(context, {
479479
...this.parseQueryParam(queryingOptions || {}),
@@ -507,7 +507,7 @@ class ApiGateway {
507507
const requestStarted = new Date();
508508

509509
try {
510-
await this.assertPermission('meta', context.securityContext);
510+
await this.assertApiScope('meta', context.securityContext);
511511
const metaConfig = await this.getCompilerApi(context).metaConfig({
512512
requestId: context.requestId,
513513
});
@@ -527,7 +527,7 @@ class ApiGateway {
527527
const requestStarted = new Date();
528528

529529
try {
530-
await this.assertPermission('meta', context.securityContext);
530+
await this.assertApiScope('meta', context.securityContext);
531531
const metaConfigExtended = await this.getCompilerApi(context).metaConfigExtended({
532532
requestId: context.requestId,
533533
});
@@ -737,7 +737,7 @@ class ApiGateway {
737737
const query = <PreAggsJobsRequest>req.body;
738738
let result;
739739
try {
740-
await this.assertPermission('jobs', req?.context?.securityContext);
740+
await this.assertApiScope('jobs', req?.context?.securityContext);
741741
switch (query.action) {
742742
case 'post':
743743
if (
@@ -1158,7 +1158,7 @@ class ApiGateway {
11581158
const requestStarted = new Date();
11591159

11601160
try {
1161-
await this.assertPermission('data', context.securityContext);
1161+
await this.assertApiScope('data', context.securityContext);
11621162

11631163
query = this.parseQueryParam(query);
11641164
const [queryType, normalizedQueries] = await this.getNormalizedQueries(query, context);
@@ -1246,7 +1246,7 @@ class ApiGateway {
12461246
const requestStarted = new Date();
12471247

12481248
try {
1249-
await this.assertPermission('data', context.securityContext);
1249+
await this.assertApiScope('data', context.securityContext);
12501250

12511251
const [queryType, normalizedQueries] = await this.getNormalizedQueries(query, context);
12521252

@@ -1484,7 +1484,7 @@ class ApiGateway {
14841484
const requestStarted = new Date();
14851485

14861486
try {
1487-
await this.assertPermission('data', context.securityContext);
1487+
await this.assertApiScope('data', context.securityContext);
14881488

14891489
query = this.parseQueryParam(request.query);
14901490
let resType: ResultType = ResultType.DEFAULT;
@@ -1960,49 +1960,56 @@ class ApiGateway {
19601960
};
19611961
}
19621962

1963-
protected createContextToPermissionsFn(
1963+
protected createContextToApiScopesFn(
19641964
options: ApiGatewayOptions,
1965-
): ContextToPermissionsFn {
1966-
return options.contextToPermissions
1967-
? async (securityContext?: any, defaultPermissions?: Permission[]) => {
1968-
const permissions = options.contextToPermissions &&
1969-
await options.contextToPermissions(
1965+
): ContextToApiScopesFn {
1966+
return options.contextToApiScopes
1967+
? async (securityContext?: any, defaultApiScopes?: ApiScopes[]) => {
1968+
const scopes = options.contextToApiScopes &&
1969+
await options.contextToApiScopes(
19701970
securityContext,
1971-
defaultPermissions,
1971+
defaultApiScopes,
19721972
);
1973-
if (!permissions || !Array.isArray(permissions)) {
1973+
if (!scopes || !Array.isArray(scopes)) {
19741974
throw new Error(
1975-
'A user-defined contextToPermissions function returns an inconsistent type.'
1975+
'A user-defined contextToApiScopes function returns an inconsistent type.'
19761976
);
19771977
} else {
1978-
permissions.forEach((p) => {
1978+
scopes.forEach((p) => {
19791979
if (['liveliness', 'graphql', 'meta', 'data', 'jobs'].indexOf(p) === -1) {
19801980
throw new Error(
1981-
`A user-defined contextToPermissions function returns a wrong permission: ${p}`
1981+
`A user-defined contextToApiScopes function returns a wrong scope: ${p}`
19821982
);
19831983
}
19841984
});
19851985
}
1986-
return permissions;
1986+
return scopes;
19871987
}
1988-
: this.contextToPermDefFn;
1988+
: async () => {
1989+
const defaultApiScope = getEnv('defaultApiScope');
1990+
if (defaultApiScope) {
1991+
return defaultApiScope;
1992+
} else {
1993+
return this.contextToApiScopesDefFn();
1994+
}
1995+
};
19891996
}
19901997

1991-
protected async assertPermission(
1992-
permission: Permission,
1998+
protected async assertApiScope(
1999+
scope: ApiScopes,
19932000
securityContext?: any,
19942001
): Promise<void> {
1995-
const permissions =
1996-
await this.contextToPermFn(
2002+
const scopes =
2003+
await this.contextToApiScopesFn(
19972004
securityContext || {},
1998-
await this.contextToPermDefFn(),
2005+
await this.contextToApiScopesDefFn(),
19992006
);
2000-
const permited = permissions.indexOf(permission) >= 0;
2007+
const permited = scopes.indexOf(scope) >= 0;
20012008
if (!permited) {
20022009
throw new CubejsHandlerError(
20032010
403,
20042011
'Forbidden',
2005-
`Permission is missed: ${permission}`
2012+
`Api scope is missed: ${scope}`
20062013
);
20072014
}
20082015
}

packages/cubejs-api-gateway/src/interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
CheckSQLAuthSuccessResponse,
2929
CheckSQLAuthFn,
3030
CanSwitchSQLUserFn,
31-
ContextToPermissionsFn,
31+
ContextToApiScopesFn,
3232
} from './types/auth';
3333

3434
import {
@@ -67,7 +67,7 @@ export {
6767
ExtendContextFn,
6868
ResponseResultFn,
6969
QueryRequest,
70-
ContextToPermissionsFn,
70+
ContextToApiScopesFn,
7171
};
7272

7373
/**

packages/cubejs-api-gateway/src/types/auth.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* Cube.js auth related data types definition.
66
*/
77

8-
import { Permission } from './strings';
8+
import { ApiScopes } from './strings';
99

1010
/**
11-
* Permissions tuple.
11+
* ApiScopes tuple.
1212
*/
13-
type PermissionsTuple = Permission[];
13+
type ApiScopesTuple = ApiScopes[];
1414

1515
/**
1616
* Internal auth logic options object data type.
@@ -78,11 +78,11 @@ type CanSwitchSQLUserFn =
7878
boolean;
7979

8080
/**
81-
* Returns permissions tuple from a security context.
81+
* Returns scopes tuple from a security context.
8282
*/
83-
type ContextToPermissionsFn =
84-
(securityContext?: any, permissions?: PermissionsTuple) =>
85-
Promise<PermissionsTuple>;
83+
type ContextToApiScopesFn =
84+
(securityContext?: any, scopes?: ApiScopesTuple) =>
85+
Promise<ApiScopesTuple>;
8686

8787
export {
8888
CheckAuthInternalOptions,
@@ -91,6 +91,6 @@ export {
9191
CheckSQLAuthSuccessResponse,
9292
CheckSQLAuthFn,
9393
CanSwitchSQLUserFn,
94-
PermissionsTuple,
95-
ContextToPermissionsFn,
94+
ApiScopesTuple,
95+
ContextToApiScopesFn,
9696
};

packages/cubejs-api-gateway/src/types/gateway.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
RequestLoggerMiddlewareFn,
1919
ContextRejectionMiddlewareFn,
2020
ContextAcceptorFn,
21-
ContextToPermissionsFn,
21+
ContextToApiScopesFn,
2222
} from '../interfaces';
2323

2424
type UserBackgroundContext = {
@@ -69,7 +69,7 @@ interface ApiGatewayOptions {
6969
* @deprecated Use checkAuth property instead.
7070
*/
7171
checkAuthMiddleware?: CheckAuthMiddlewareFn;
72-
contextToPermissions?: ContextToPermissionsFn;
72+
contextToApiScopes?: ContextToApiScopesFn;
7373
}
7474

7575
export {

packages/cubejs-api-gateway/src/types/strings.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ type QueryOrderType =
9999
'desc';
100100

101101
/**
102-
* Permission data type.
102+
* ApiScopes data type.
103103
*/
104-
type Permission =
104+
type ApiScopes =
105105
'liveliness' |
106106
'graphql' |
107107
'meta' |
@@ -119,5 +119,5 @@ export {
119119
FilterOperator,
120120
QueryTimeDimensionGranularity,
121121
QueryOrderType,
122-
Permission,
122+
ApiScopes,
123123
};

0 commit comments

Comments
 (0)