Skip to content

Commit

Permalink
Merge branch 'main' into CHARTS-10307-add-check-parent-folder-in-rest…
Browse files Browse the repository at this point in the history
…ore-entity
  • Loading branch information
Sergey-weber committed Sep 30, 2024
2 parents 33d28d9 + 2dc8a79 commit 25d5bee
Show file tree
Hide file tree
Showing 41 changed files with 789 additions and 2,354 deletions.
2 changes: 2 additions & 0 deletions api/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export {
AppEnv,
BiTrackingLogs,
US_MASTER_TOKEN_HEADER,
COOKIE_HEADER,
DL_SERVICE_USER_ACCESS_TOKEN,
DL_COMPONENT_HEADER,
SYSTEM_USER,
ORG_TENANT_PREFIX,
Expand Down
1 change: 1 addition & 0 deletions api/entities.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {ResourceType} from '../src/entities/types';
export {
CollectionPermission,
CollectionRole,
Expand Down
2 changes: 0 additions & 2 deletions api/tests.ts

This file was deleted.

1 change: 1 addition & 0 deletions api/tests/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../src/tests/int/auth';
1 change: 1 addition & 0 deletions api/tests/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../src/tests/int/constants';
1 change: 1 addition & 0 deletions api/tests/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../src/tests/int/db';
1 change: 1 addition & 0 deletions api/tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../src/tests/int/helpers';
1 change: 1 addition & 0 deletions api/tests/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../src/tests/int/models';
1 change: 1 addition & 0 deletions api/tests/roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../src/tests/int/roles';
1 change: 1 addition & 0 deletions api/tests/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../src/tests/int/routes';
2 changes: 1 addition & 1 deletion dev/env/opensource/development.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CONTROL_MASTER_TOKEN=development-control-master-token

US_SURPRESS_DB_STATUS_LOGS=true

ZITADEL=false
ZITADEL=true

ZITADEL_URI=http://localhost:8085

Expand Down
2 changes: 2 additions & 0 deletions dev/env/opensource/int-testing.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
APP_INSTALLATION=opensource
APP_ENV=int-testing

ZITADEL=true

MASTER_TOKEN=int-testing-master-token

APP_LOGGING_LEVEL=silent
Expand Down
2 changes: 1 addition & 1 deletion src/configs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default {

dlsEnabled: false,
accessServiceEnabled: Utils.isTrueArg(Utils.getEnvVariable('ZITADEL')),
accessBindingsServiceEnabled: false,
accessBindingsServiceEnabled: Utils.isTrueArg(Utils.getEnvVariable('ZITADEL')),

masterToken: Utils.getEnvTokenVariable('MASTER_TOKEN'),

Expand Down
5 changes: 4 additions & 1 deletion src/const/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,14 @@ export const AJV_PATTERN_KEYS_NOT_OBJECT = {
},
};

export const COOKIE_HEADER = 'cookie';
export const AUTHORIZATION_HEADER = 'authorization';
export const DL_AUTH_HEADER_KEY = 'bearer';

export const US_MASTER_TOKEN_HEADER = 'x-us-master-token';
export const DL_COMPONENT_HEADER = 'x-dl-component';
export const DL_WORKBOOK_ID_HEADER = 'x-dl-workbookid';
export const DL_SERVICE_USER_ACCESS_TOKEN = 'x-dl-service-user-access-token';
export const DL_AUTH_HEADER_KEY = 'bearer';

export const COMPARISON_OPERATORS: {[key: string]: string} = {
eq: '=',
Expand Down
7 changes: 7 additions & 0 deletions src/entities/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {CollectionRole, CollectionPermission} from './collection/types';
export {WorkbookRole, WorkbookPermission} from './workbook/types';

export enum ResourceType {
Collection = 'datalens.collection',
Workbook = 'datalens.workbook',
}
21 changes: 18 additions & 3 deletions src/registry/common/entities/collection/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {CollectionConstructor, CollectionInstance} from './types';
import {CollectionPermission, Permissions} from '../../../../entities/collection/types';
import {US_ERRORS} from '../../../../const';
import {ZitadelUserRole} from '../../../../types/zitadel';
import {getMockedOperation} from '../utils';
import Utils from '../../../../utils';

export const Collection: CollectionConstructor = class Collection implements CollectionInstance {
ctx: AppContext;
Expand All @@ -16,10 +18,13 @@ export const Collection: CollectionConstructor = class Collection implements Col
this.model = model;
}

private getAllPermissions() {
private isEditorOrAdmin() {
const {zitadelUserRole: role} = this.ctx.get('info');
return role === ZitadelUserRole.Editor || role === ZitadelUserRole.Admin;
}

const isEditorOrAdmin = role === ZitadelUserRole.Editor || role === ZitadelUserRole.Admin;
private getAllPermissions() {
const isEditorOrAdmin = this.isEditorOrAdmin();

const permissions = {
listAccessBindings: true,
Expand All @@ -37,7 +42,17 @@ export const Collection: CollectionConstructor = class Collection implements Col
return permissions;
}

async register() {}
async register() {
const isEditorOrAdmin = this.isEditorOrAdmin();

if (!isEditorOrAdmin) {
throw new AppError(US_ERRORS.ACCESS_SERVICE_PERMISSION_DENIED, {
code: US_ERRORS.ACCESS_SERVICE_PERMISSION_DENIED,
});
}

return Promise.resolve(getMockedOperation(Utils.encodeId(this.model.collectionId)));
}

async checkPermission(args: {
parentIds: string[];
Expand Down
18 changes: 18 additions & 0 deletions src/registry/common/entities/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const getMockedOperation = (id: string) => {
const [seconds, nanoseconds] = process.hrtime();
return {
createdAt: {
nanos: nanoseconds,
seconds: seconds.toString(),
},
createdBy: '',
description: 'Datalens operation',
done: true,
id: id,
metadata: {},
modifiedAt: {
nanos: nanoseconds,
seconds: seconds.toString(),
},
};
};
21 changes: 17 additions & 4 deletions src/registry/common/entities/workbook/workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {WorkbookConstructor, WorkbookInstance} from './types';
import {Permissions, WorkbookPermission} from '../../../../entities/workbook/types';
import {US_ERRORS} from '../../../../const';
import {ZitadelUserRole} from '../../../../types/zitadel';
import {getMockedOperation} from '../utils';
import Utils from '../../../../utils';

export const Workbook: WorkbookConstructor<WorkbookInstance> = class Workbook
implements WorkbookInstance
Expand All @@ -18,10 +20,13 @@ export const Workbook: WorkbookConstructor<WorkbookInstance> = class Workbook
this.model = model;
}

private getAllPermissions() {
private isEditorOrAdmin() {
const {zitadelUserRole: role} = this.ctx.get('info');
return role === ZitadelUserRole.Editor || role === ZitadelUserRole.Admin;
}

const isEditorOrAdmin = role === ZitadelUserRole.Editor || role === ZitadelUserRole.Admin;
private getAllPermissions() {
const isEditorOrAdmin = this.isEditorOrAdmin();

const permissions = {
listAccessBindings: true,
Expand All @@ -39,8 +44,16 @@ export const Workbook: WorkbookConstructor<WorkbookInstance> = class Workbook
return permissions;
}

async register(_args: {parentIds: string[]}): Promise<unknown> {
return Promise.resolve();
async register() {
const isEditorOrAdmin = this.isEditorOrAdmin();

if (!isEditorOrAdmin) {
throw new AppError(US_ERRORS.ACCESS_SERVICE_PERMISSION_DENIED, {
code: US_ERRORS.ACCESS_SERVICE_PERMISSION_DENIED,
});
}

return Promise.resolve(getMockedOperation(Utils.encodeId(this.model.workbookId)));
}

async checkPermission(args: {
Expand Down
91 changes: 91 additions & 0 deletions src/tests/int/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import request from 'supertest';
import {
AUTHORIZATION_HEADER,
DL_AUTH_HEADER_KEY,
US_ERRORS,
US_MASTER_TOKEN_HEADER,
} from '../../const';
import {testUserId, testUserLogin} from './constants';
import {OpensourceRole} from './roles';
import usApp from '../..';
import {ZitadelUserRole} from '../../types/zitadel';
import {CollectionPermission} from '../../entities/collection/types';
import {WorkbookPermission} from '../../entities/workbook/types';
import {ResourceType} from '../../entities/types';

export {US_ERRORS};

export const app = usApp.express;
export const appConfig = usApp.config;

export const testTenantId = 'common';
export const testProjectId = null;

export const getCollectionBinding = (
collectionId: string,
permission: `${CollectionPermission}`,
) => {
return {
id: collectionId,
type: ResourceType.Collection as const,
permission,
};
};

export const getWorkbookBinding = (workbookId: string, permission: `${WorkbookPermission}`) => {
return {
id: workbookId,
type: ResourceType.Workbook as const,
permission,
};
};

export type AccessBinding = ReturnType<typeof getCollectionBinding | typeof getWorkbookBinding>;

export type AuthArgs = {
userId?: string;
login?: string;
role?: OpensourceRole;
accessBindings?: AccessBinding[];
};

export const auth = (req: request.Test, args: AuthArgs = {}) => {
const {
userId = testUserId,
login = testUserLogin,
role = OpensourceRole.Viewer,
accessBindings = [],
} = args;

let zitadelRole: ZitadelUserRole;

switch (role) {
case OpensourceRole.Admin:
zitadelRole = ZitadelUserRole.Admin;
break;
case OpensourceRole.Editor:
zitadelRole = ZitadelUserRole.Editor;
break;
default:
zitadelRole = ZitadelUserRole.Viewer;
break;
}

req.set(
AUTHORIZATION_HEADER,
`${DL_AUTH_HEADER_KEY} ${JSON.stringify({
userId,
login,
role: zitadelRole,
accessBindings,
})}`,
);

return req;
};

export const authMasterToken = (req: request.Test) => {
const token = process.env.MASTER_TOKEN ?? '';
req.set(US_MASTER_TOKEN_HEADER, token);
return req;
};
8 changes: 4 additions & 4 deletions src/tests/int/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export const systemId = 'systemId';
export const systemUserId = 'systemId';
export const systemLogin = 'system';

export const testUserLogin = 'unknown';
export const testUserId = 'test-user-id';
export const testUserLogin = 'test-user-login';

export const testTenantId = 'common';
export const testProjectId = null;
export const testOtherUserId = 'test-other-user-id';

export const ZITADEL_USER_ROLE_HEADER = 'zitadel-user-role';
Loading

0 comments on commit 25d5bee

Please sign in to comment.