-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathexecute-http-request-step.usecase.ts
More file actions
406 lines (352 loc) · 13.8 KB
/
execute-http-request-step.usecase.ts
File metadata and controls
406 lines (352 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { Injectable } from '@nestjs/common';
import {
buildNovuSignatureHeader,
CreateExecutionDetails,
CreateExecutionDetailsCommand,
DetailEnum,
dashboardSanitizeControlValues,
evaluateRules,
GetDecryptedSecretKey,
GetDecryptedSecretKeyCommand,
HttpClientService,
ICompileContext,
InstrumentUsecase,
PinoLogger,
shouldIncludeBody,
toBodyRecord,
toHeadersRecord,
validateUrlSsrf,
} from '@novu/application-generic';
import { ControlValuesRepository, JobRepository, MessageRepository, NotificationTemplateRepository } from '@novu/dal';
import { createLiquidEngine } from '@novu/framework/internal';
import {
ControlValuesLevelEnum,
DeliveryLifecycleDetail,
DeliveryLifecycleStatusEnum,
ExecutionDetailsSourceEnum,
ExecutionDetailsStatusEnum,
ResourceOriginEnum,
} from '@novu/shared';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import { AdditionalOperation, RulesLogic } from 'json-logic-js';
import { SendMessageChannelCommand } from './send-message-channel.command';
import { SendMessageResult, SendMessageStatus, SendMessageType } from './send-message-type.usecase';
const MAX_RAW_SIZE = 10_240;
@Injectable()
export class ExecuteHttpRequestStep extends SendMessageType {
private readonly liquidEngine: ReturnType<typeof createLiquidEngine>;
constructor(
private jobRepository: JobRepository,
private httpClientService: HttpClientService,
private controlValuesRepository: ControlValuesRepository,
private notificationTemplateRepository: NotificationTemplateRepository,
private logger: PinoLogger,
private getDecryptedSecretKey: GetDecryptedSecretKey,
protected messageRepository: MessageRepository,
protected createExecutionDetails: CreateExecutionDetails
) {
super(messageRepository, createExecutionDetails);
this.liquidEngine = createLiquidEngine();
}
@InstrumentUsecase()
public async execute(command: SendMessageChannelCommand): Promise<SendMessageResult> {
const controlValues = await this.fetchControlValues(command);
const compileContext = this.buildCompileContect(command.compileContext);
const shouldSkip = this.evaluateSkipCondition(controlValues, compileContext);
if (shouldSkip) {
await this.createExecutionDetails.execute(
CreateExecutionDetailsCommand.create({
...CreateExecutionDetailsCommand.getDetailsFromJob(command.job),
detail: DetailEnum.SKIPPED_BRIDGE_EXECUTION,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: JSON.stringify({ skip: true }),
})
);
return {
status: SendMessageStatus.SKIPPED,
deliveryLifecycleState: {
status: DeliveryLifecycleStatusEnum.SKIPPED,
detail: DeliveryLifecycleDetail.USER_STEP_CONDITION,
},
};
}
const { skip: _skip, ...controlValuesWithoutSkip } = controlValues;
const secretKey = await this.getDecryptedSecretKey.execute(
GetDecryptedSecretKeyCommand.create({ environmentId: command.environmentId })
);
let compiled: typeof controlValuesWithoutSkip;
try {
compiled = (await this.compileControlValues(
controlValuesWithoutSkip,
compileContext
)) as typeof controlValuesWithoutSkip;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
await this.createExecutionDetails.execute(
CreateExecutionDetailsCommand.create({
...CreateExecutionDetailsCommand.getDetailsFromJob(command.job),
detail: DetailEnum.ACTION_STEP_EXECUTION_FAILED,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: JSON.stringify({
error: `HTTP request step template compilation failed: ${errorMessage}`,
}),
})
);
return {
status: SendMessageStatus.FAILED,
errorMessage: DetailEnum.ACTION_STEP_EXECUTION_FAILED,
shouldHalt: !controlValuesWithoutSkip.continueOnFailure,
};
}
const url = compiled.url as string | undefined;
const method = (compiled.method as string) ?? 'POST';
const rawHeaders = (compiled.headers as Array<{ key: string; value: string }> | undefined) ?? [];
const rawBody = (compiled.body as Array<{ key: string; value: string }> | undefined) ?? [];
const timeout = (compiled.timeout as number | undefined) ?? 5000;
if (!url) {
await this.createExecutionDetails.execute(
CreateExecutionDetailsCommand.create({
...CreateExecutionDetailsCommand.getDetailsFromJob(command.job),
detail: DetailEnum.ACTION_STEP_EXECUTION_FAILED,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: JSON.stringify({
error: 'HTTP request step is missing a URL. Please configure a URL in the step settings.',
}),
})
);
return {
status: SendMessageStatus.FAILED,
errorMessage: DetailEnum.ACTION_STEP_EXECUTION_FAILED,
shouldHalt: !controlValuesWithoutSkip.continueOnFailure,
};
}
const ssrfValidationError = await validateUrlSsrf(url);
if (ssrfValidationError) {
await this.createExecutionDetails.execute(
CreateExecutionDetailsCommand.create({
...CreateExecutionDetailsCommand.getDetailsFromJob(command.job),
detail: DetailEnum.ACTION_STEP_EXECUTION_FAILED,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: JSON.stringify({ error: ssrfValidationError }),
})
);
return {
status: SendMessageStatus.FAILED,
errorMessage: DetailEnum.ACTION_STEP_EXECUTION_FAILED,
shouldHalt: !controlValuesWithoutSkip.continueOnFailure,
};
}
const headersRecord = toHeadersRecord(rawHeaders);
const bodyObject = toBodyRecord(rawBody);
const hasBody = shouldIncludeBody(bodyObject, method);
const signatureHeaders = {
'novu-signature': buildNovuSignatureHeader(secretKey, hasBody ? bodyObject : {}),
};
const mergedHeaders = { ...headersRecord, ...signatureHeaders };
let result: { statusCode?: number; body: unknown; headers: Record<string, string> };
try {
const response = await this.httpClientService.request<string>({
url,
method: method as 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH',
headers: mergedHeaders,
timeout,
responseType: 'text',
...(hasBody ? { body: bodyObject } : {}),
});
const parsedBody = tryParseJson(response.body);
const isObjectBody = parsedBody !== null && typeof parsedBody === 'object' && !Array.isArray(parsedBody);
if (!isObjectBody) {
await this.createExecutionDetails.execute(
CreateExecutionDetailsCommand.create({
...CreateExecutionDetailsCommand.getDetailsFromJob(command.job),
detail: DetailEnum.ACTION_STEP_NON_OBJECT_RESPONSE,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.WARNING,
isTest: false,
isRetry: false,
raw: JSON.stringify({
message: `The endpoint at "${url}" returned a non-object response (type: ${Array.isArray(parsedBody) ? 'array' : typeof parsedBody}). Subsequent steps that reference this step's output may fail because the framework expects a JSON object. Configure the endpoint to return a JSON object to avoid this issue.`,
url,
receivedType: Array.isArray(parsedBody) ? 'array' : typeof parsedBody,
}),
})
);
}
result = { statusCode: response.statusCode, body: parsedBody, headers: response.headers };
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
await this.createExecutionDetails.execute(
CreateExecutionDetailsCommand.create({
...CreateExecutionDetailsCommand.getDetailsFromJob(command.job),
detail: DetailEnum.ACTION_STEP_EXECUTION_FAILED,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: JSON.stringify({ error: errorMessage }),
})
);
return {
status: SendMessageStatus.FAILED,
errorMessage: DetailEnum.ACTION_STEP_EXECUTION_FAILED,
shouldHalt: !controlValuesWithoutSkip.continueOnFailure,
};
}
if (controlValuesWithoutSkip.enforceSchemaValidation && controlValuesWithoutSkip.responseBodySchema) {
const validationResult = this.validateResponseSchema(
result.body,
controlValuesWithoutSkip.responseBodySchema as Record<string, unknown>
);
if (!validationResult.isValid) {
const { errors } = validationResult;
await this.createExecutionDetails.execute(
CreateExecutionDetailsCommand.create({
...CreateExecutionDetailsCommand.getDetailsFromJob(command.job),
detail: DetailEnum.RESPONSE_SCHEMA_VALIDATION_FAILED,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: truncateRaw({ errors, responseBody: result.body }),
})
);
return {
status: SendMessageStatus.FAILED,
errorMessage: DetailEnum.RESPONSE_SCHEMA_VALIDATION_FAILED,
shouldHalt: !controlValuesWithoutSkip.continueOnFailure,
};
}
}
await this.jobRepository.updateOne(
{ _id: command.job._id, _environmentId: command.environmentId },
{ $set: { stepOutput: result.body } }
);
await this.createExecutionDetails.execute(
CreateExecutionDetailsCommand.create({
...CreateExecutionDetailsCommand.getDetailsFromJob(command.job),
detail: DetailEnum.STEP_PROCESSED,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.SUCCESS,
isTest: false,
isRetry: false,
raw: truncateRaw(result),
})
);
return { status: SendMessageStatus.SUCCESS };
}
private validateResponseSchema(
responseBody: unknown,
schema: Record<string, unknown>
): { isValid: true; errors?: undefined } | { isValid: false; errors: { path: string; message: string }[] } {
try {
const ajv = new Ajv({ strict: false });
addFormats(ajv);
const validate = ajv.compile(schema);
const valid = validate(responseBody);
if (valid) {
return { isValid: true };
}
return {
isValid: false,
errors: (validate.errors ?? []).map((err) => ({
path: err.instancePath,
message: err.message ?? 'Validation error',
})),
};
} catch (error) {
return {
isValid: false,
errors: [{ path: '', message: error instanceof Error ? error.message : 'Schema compilation error' }],
};
}
}
private async compileControlValues(
values: Record<string, unknown>,
context: Record<string, unknown>
): Promise<unknown> {
const compiled = await this.liquidEngine.parseAndRender(JSON.stringify(values), context);
try {
return JSON.parse(compiled);
} catch {
throw new Error('Rendered template output is not valid JSON');
}
}
private buildCompileContect(compileContext: ICompileContext): Record<string, unknown> {
return {
subscriber: compileContext.subscriber ?? {},
payload: compileContext.payload ?? {},
actor: compileContext.actor ?? {},
tenant: compileContext.tenant ?? {},
context: compileContext.context ?? {},
step: compileContext.step,
webhook: compileContext.webhook ?? {},
env: compileContext.env ?? {},
};
}
private evaluateSkipCondition(
controlValues: Record<string, unknown>,
compileContext: Record<string, unknown>
): boolean {
const skipRules = controlValues.skip as RulesLogic<AdditionalOperation> | undefined;
if (!skipRules || (typeof skipRules === 'object' && Object.keys(skipRules).length === 0)) {
return false;
}
const { result, error } = evaluateRules(skipRules, compileContext);
if (error) {
this.logger.error({ err: error }, 'Failed to evaluate skip rule for HTTP request step');
}
return !result;
}
private async fetchControlValues(command: SendMessageChannelCommand): Promise<Record<string, unknown>> {
const workflow =
command.workflow ??
(command._templateId
? await this.notificationTemplateRepository.findById(command._templateId, command.environmentId)
: null);
if (!workflow) {
return {};
}
const controlsEntity = await this.controlValuesRepository.findOne({
_organizationId: command.organizationId,
_workflowId: workflow._id,
_stepId: command.step._id,
level: ControlValuesLevelEnum.STEP_CONTROLS,
});
const rawControls = controlsEntity?.controls;
if (!rawControls) {
return {};
}
if (workflow.origin === ResourceOriginEnum.NOVU_CLOUD) {
return dashboardSanitizeControlValues(this.logger, rawControls, command.step?.template?.type) ?? {};
}
return rawControls;
}
}
function tryParseJson(text: string): unknown {
try {
return JSON.parse(text);
} catch {
return text;
}
}
function truncateRaw(obj: unknown, maxSize: number = MAX_RAW_SIZE): string {
const serialized = JSON.stringify(obj);
if (serialized.length <= maxSize) {
return serialized;
}
const suffix = '... [truncated]';
return serialized.slice(0, maxSize - suffix.length) + suffix;
}