11import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox' ;
22import { logger } from '@openops/server-shared' ;
33import {
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' ;
1216import { sendStepFailureEvent } from '../telemetry/event-models/step' ;
1317import {
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+ }
0 commit comments