Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Adjust credential endpoints permissions #4656

Merged
merged 4 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/cli/src/UserManagement/PermissionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ export class PermissionChecker {
// allow if all creds used in this workflow are a subset of
// all creds accessible to users who have access to this workflow

const workflowSharings = await Db.collections.SharedWorkflow.find({
relations: ['workflow'],
where: { workflow: { id: Number(workflow.id) } },
});

const workflowUserIds = workflowSharings.map((s) => s.userId);
let workflowUserIds: string[] = [];

if (workflow.id) {
const workflowSharings = await Db.collections.SharedWorkflow.find({
relations: ['workflow'],
where: { workflow: { id: Number(workflow.id) } },
});

workflowUserIds = workflowSharings.map((s) => s.userId);
} else {
// unsaved workflows have no id, so only get credentials for current user
workflowUserIds = [userId];
}

const credentialSharings = await Db.collections.SharedCredentials.find({
where: { user: In(workflowUserIds) },
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/credentials/credentials.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ credentialsController.use('/', EECredentialsController);
credentialsController.get(
'/',
ResponseHelper.send(async (req: CredentialRequest.GetAll): Promise<ICredentialsResponse[]> => {
const credentials = await CredentialsService.getAll(req.user);
const credentials = await CredentialsService.getAll(req.user, { roles: ['owner'] });

return credentials.map((credential) => {
// eslint-disable-next-line no-param-reassign
Expand Down
31 changes: 30 additions & 1 deletion packages/cli/src/credentials/credentials.service.ee.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-param-reassign */
import { DeleteResult, EntityManager, In, Not } from 'typeorm';
import { DeleteResult, EntityManager, FindOneOptions, In, Not, ObjectLiteral } from 'typeorm';
import * as Db from '@/Db';
import { RoleService } from '@/role/role.service';
import { CredentialsEntity } from '@db/entities/CredentialsEntity';
Expand All @@ -25,6 +25,35 @@ export class EECredentialsService extends CredentialsService {
return { ownsCredential: true, credential };
}

/**
* Retrieve the sharing that matches a user and a credential.
*/
static async getSharing(
user: User,
credentialId: number | string,
relations: string[] = ['credentials'],
krynble marked this conversation as resolved.
Show resolved Hide resolved
{ allowGlobalOwner } = { allowGlobalOwner: true },
): Promise<SharedCredentials | undefined> {
const options: FindOneOptions<SharedCredentials> & { where: ObjectLiteral } = {
where: {
credentials: { id: credentialId },
},
};

// Omit user from where if the requesting user is the global
// owner. This allows the global owner to view and delete
// credentials they don't own.
if (!allowGlobalOwner || user.globalRole.name !== 'owner') {
options.where.user = { id: user.id };
}

if (relations?.length) {
options.relations = relations;
}

return Db.collections.SharedCredentials.findOne(options);
}

static async getSharings(
transaction: EntityManager,
credentialId: string,
Expand Down
31 changes: 25 additions & 6 deletions packages/cli/src/credentials/credentials.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export class CredentialsService {
});
}

static async getAll(user: User, options?: { relations: string[] }): Promise<ICredentialsDb[]> {
static async getAll(
user: User,
options?: { relations?: string[]; roles?: string[] },
): Promise<ICredentialsDb[]> {
const SELECT_FIELDS: Array<keyof ICredentialsDb> = [
'id',
'name',
Expand All @@ -52,11 +55,21 @@ export class CredentialsService {

// if member, return credentials owned by or shared with member

const userSharings = await Db.collections.SharedCredentials.find({
const whereConditions: FindManyOptions = {
krynble marked this conversation as resolved.
Show resolved Hide resolved
where: {
user,
},
});
};

if (options?.roles?.length) {
whereConditions.where = {
...whereConditions.where,
role: { name: In(options.roles) },
} as FindManyOptions;
whereConditions.relations = ['role'];
}

const userSharings = await Db.collections.SharedCredentials.find(whereConditions);

return Db.collections.Credentials.find({
select: SELECT_FIELDS,
Expand All @@ -77,7 +90,7 @@ export class CredentialsService {
static async getSharing(
user: User,
credentialId: number | string,
relations: string[] | undefined = ['credentials'],
relations: string[] = ['credentials'],
{ allowGlobalOwner } = { allowGlobalOwner: true },
): Promise<SharedCredentials | undefined> {
const options: FindOneOptions = {
Expand All @@ -90,8 +103,14 @@ export class CredentialsService {
// owner. This allows the global owner to view and delete
// credentials they don't own.
if (!allowGlobalOwner || user.globalRole.name !== 'owner') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
options.where.user = { id: user.id };
options.where = {
...options.where,
user: { id: user.id },
role: { name: 'owner' },
} as FindOneOptions;
if (!relations.includes('role')) {
relations.push('role');
}
}

if (relations?.length) {
Expand Down
15 changes: 8 additions & 7 deletions packages/cli/src/workflows/workflows.controller.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,14 @@ EEWorkflowController.post(
const workflow = new WorkflowEntity();
Object.assign(workflow, req.body.workflowData);

const safeWorkflow = await EEWorkflows.preventTampering(
workflow,
workflow.id.toString(),
req.user,
);

req.body.workflowData.nodes = safeWorkflow.nodes;
if (workflow.id !== undefined) {
const safeWorkflow = await EEWorkflows.preventTampering(
workflow,
workflow.id.toString(),
req.user,
);
req.body.workflowData.nodes = safeWorkflow.nodes;
}

return EEWorkflows.runManually(req.body, req.user, GenericHelpers.getSessionId(req));
}),
Expand Down