Skip to content

Commit 400667d

Browse files
author
Roman Snapko
committed
Merge branch 'refs/heads/main' into fix/trigger-errors
2 parents 1b8797a + d821090 commit 400667d

9 files changed

Lines changed: 132 additions & 1 deletion

File tree

packages/server/api/src/app/app-connection/app-connection-service/app-connection-service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,14 @@ export const appConnectionService = {
232232
entity: AppConnectionEntity,
233233
query: {
234234
limit,
235-
order: 'ASC',
235+
order: 'DESC',
236236
afterCursor: decodedCursor.nextCursor,
237237
beforeCursor: decodedCursor.previousCursor,
238238
},
239+
customPaginationColumn: {
240+
columnPath: 'updated',
241+
columnName: 'app_connection.updated',
242+
},
239243
});
240244

241245
const querySelector: Record<string, string | FindOperator<string>> = {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { AuthorizationGuards } from './authorization-guards';
2+
import { noopAuthorizationGuards } from './noop-authorization-guards';
3+
4+
export function getAuthorizationGuards(): AuthorizationGuards {
5+
return noopAuthorizationGuards;
6+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
CreateStepRunRequestBody,
3+
Principal,
4+
TestFlowRunRequestBody,
5+
} from '@openops/shared';
6+
import { FastifyRequest } from 'fastify';
7+
8+
export type AuthorizationGuards = {
9+
enforceTestStepAuthorizationFromRequest(
10+
request: FastifyRequest,
11+
): Promise<void>;
12+
13+
enforceTestStepAuthorization(
14+
data: CreateStepRunRequestBody,
15+
principal: Principal,
16+
): Promise<void>;
17+
18+
enforceTestRunAuthorization(
19+
data: TestFlowRunRequestBody,
20+
principal: Principal,
21+
): Promise<void>;
22+
23+
enforceWorkflowStatusAuthorization(request: FastifyRequest): Promise<void>;
24+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
CreateStepRunRequestBody,
3+
Principal,
4+
TestFlowRunRequestBody,
5+
} from '@openops/shared';
6+
import { FastifyRequest } from 'fastify';
7+
import { AuthorizationGuards } from './authorization-guards';
8+
9+
export const noopAuthorizationGuards: AuthorizationGuards = {
10+
enforceTestRunAuthorization(
11+
_data: TestFlowRunRequestBody,
12+
_principal: Principal,
13+
): Promise<void> {
14+
return Promise.resolve();
15+
},
16+
17+
enforceTestStepAuthorization(
18+
_data: CreateStepRunRequestBody,
19+
_principal: Principal,
20+
): Promise<void> {
21+
return Promise.resolve();
22+
},
23+
24+
enforceTestStepAuthorizationFromRequest(
25+
_request: FastifyRequest,
26+
): Promise<void> {
27+
return Promise.resolve();
28+
},
29+
30+
enforceWorkflowStatusAuthorization(_request: FastifyRequest): Promise<void> {
31+
return Promise.resolve();
32+
},
33+
};

packages/server/api/src/app/flows/flow.module.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
22
import { logger } from '@openops/server-shared';
33
import {
4+
ApplicationError,
45
CreateStepRunRequestBody,
6+
ErrorCode,
57
FlowRunStatus,
68
StepRunResponse,
79
TestFlowRunRequestBody,
810
WebsocketClientEvent,
911
WebsocketServerEvent,
1012
flowHelper,
1113
} from '@openops/shared';
14+
import { Socket } from 'socket.io';
15+
import { getAuthorizationGuards } from '../core/security/authorization-guards/authorization-guards-factory';
1216
import { sendStepFailureEvent } from '../telemetry/event-models/step';
1317
import {
1418
sendWorkflowTestFailureEvent,
@@ -39,6 +43,11 @@ export const flowModule: FastifyPluginAsyncTypebox = async (app) => {
3943
try {
4044
principal = await getPrincipalFromWebsocket(socket);
4145

46+
await getAuthorizationGuards().enforceTestRunAuthorization(
47+
data,
48+
principal,
49+
);
50+
4251
flowRun = await flowRunService.test({
4352
projectId: principal.projectId,
4453
flowVersionId: data.flowVersionId,
@@ -63,6 +72,11 @@ export const flowModule: FastifyPluginAsyncTypebox = async (app) => {
6372
);
6473
socket.emit(WebsocketClientEvent.TEST_FLOW_RUN_STARTED, flowRun);
6574
} catch (err) {
75+
if (isAuthorizationError(err)) {
76+
sendAuthorizationError(socket, err);
77+
return;
78+
}
79+
6680
sendWorkflowTestFailureEvent({
6781
userId: principal?.id ?? '',
6882
projectId: principal?.projectId ?? '',
@@ -90,6 +104,11 @@ export const flowModule: FastifyPluginAsyncTypebox = async (app) => {
90104
try {
91105
principal = await getPrincipalFromWebsocket(socket);
92106

107+
await getAuthorizationGuards().enforceTestStepAuthorization(
108+
data,
109+
principal,
110+
);
111+
93112
logger.debug({ data }, '[Socket#testStepRun]');
94113
const stepRun = await stepRunService.create({
95114
userId: principal.id,
@@ -105,6 +124,11 @@ export const flowModule: FastifyPluginAsyncTypebox = async (app) => {
105124
};
106125
socket.emit(WebsocketClientEvent.TEST_STEP_FINISHED, response);
107126
} catch (err) {
127+
if (isAuthorizationError(err)) {
128+
sendAuthorizationError(socket, err);
129+
return;
130+
}
131+
108132
let step;
109133
try {
110134
const flowVersion = await flowVersionService.getOneOrThrow(
@@ -139,3 +163,20 @@ export const flowModule: FastifyPluginAsyncTypebox = async (app) => {
139163
};
140164
});
141165
};
166+
167+
function isAuthorizationError(error: unknown): boolean {
168+
logger.debug('isAuthorizationError', error);
169+
return (
170+
error instanceof ApplicationError &&
171+
error.error.code === ErrorCode.AUTHORIZATION
172+
);
173+
}
174+
175+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
176+
function sendAuthorizationError(socket: Socket, error: any): void {
177+
socket.emit('error', {
178+
success: false,
179+
code: error.error.code,
180+
output: error.error.params.message,
181+
});
182+
}

packages/server/api/src/app/flows/flow/flow.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
} from '@openops/shared';
3333
import { StatusCodes } from 'http-status-codes';
3434
import { entitiesMustBeOwnedByCurrentProject } from '../../authentication/authorization';
35+
import { getAuthorizationGuards } from '../../core/security/authorization-guards/authorization-guards-factory';
3536
import { getProjectScopedRoutePolicy } from '../../core/security/route-policies/route-security-policy-factory';
3637
import { projectService } from '../../project/project-service';
3738
import { sendWorkflowCreatedFromTemplateEvent } from '../../telemetry/event-models';
@@ -291,6 +292,7 @@ const UpdateFlowRequestOptions = {
291292
permission: Permission.WRITE_FLOW,
292293
}),
293294
},
295+
preHandler: getAuthorizationGuards().enforceWorkflowStatusAuthorization,
294296
schema: {
295297
tags: ['flows'],
296298
description:

packages/server/api/src/app/flows/trigger-events/trigger-event.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
PrincipalType,
66
TestPollingTriggerRequest,
77
} from '@openops/shared';
8+
import { getAuthorizationGuards } from '../../core/security/authorization-guards/authorization-guards-factory';
89
import { getProjectScopedRoutePolicy } from '../../core/security/route-policies/route-security-policy-factory';
910
import { systemJobsSchedule } from '../../helper/system-jobs';
1011
import { SystemJobName } from '../../helper/system-jobs/common';
@@ -44,6 +45,8 @@ const triggerEventController: FastifyPluginAsyncTypebox = async (fastify) => {
4445
permission: Permission.READ_FLOW,
4546
}),
4647
},
48+
preHandler:
49+
getAuthorizationGuards().enforceTestStepAuthorizationFromRequest,
4750
schema: {
4851
querystring: TestPollingTriggerRequest,
4952
},

packages/server/api/src/app/webhooks/webhook-simulation/webhook-simulation-controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Type,
44
} from '@fastify/type-provider-typebox';
55
import { Permission, PrincipalType } from '@openops/shared';
6+
import { getAuthorizationGuards } from '../../core/security/authorization-guards/authorization-guards-factory';
67
import { getProjectScopedRoutePolicy } from '../../core/security/route-policies/route-security-policy-factory';
78
import { webhookSimulationService } from './webhook-simulation-service';
89

@@ -52,6 +53,7 @@ const CreateWebhookSimulationRequest = {
5253
permission: Permission.TEST_STEP_FLOW,
5354
}),
5455
},
56+
preHandler: getAuthorizationGuards().enforceTestStepAuthorizationFromRequest,
5557
schema: {
5658
body: Type.Object({
5759
flowId: Type.String(),

packages/server/api/test/unit/app-connection/app-connection-service.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ import {
6666
} from '@openops/shared';
6767
import { appConnectionService } from '../../../src/app/app-connection/app-connection-service/app-connection-service';
6868
import { restoreRedactedSecrets } from '../../../src/app/app-connection/app-connection-utils';
69+
import { AppConnectionEntity } from '../../../src/app/app-connection/app-connection.entity';
70+
import { buildPaginator } from '../../../src/app/helper/pagination/build-paginator';
6971

7072
describe('appConnectionService.update', () => {
7173
const projectId = 'project-123';
@@ -201,6 +203,20 @@ describe('appConnectionService.list', () => {
201203
authProviders,
202204
});
203205

206+
expect(buildPaginator).toHaveBeenCalledWith({
207+
entity: AppConnectionEntity,
208+
query: {
209+
limit: 10,
210+
order: 'DESC',
211+
afterCursor: null,
212+
beforeCursor: null,
213+
},
214+
customPaginationColumn: {
215+
columnPath: 'updated',
216+
columnName: 'app_connection.updated',
217+
},
218+
});
219+
204220
expect(andWhereMock).toHaveBeenCalledWith(
205221
'LOWER(app_connection.authProviderKey) IN (:...authProviders)',
206222
{ authProviders: ['github', 'slack'] },

0 commit comments

Comments
 (0)