mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-17 16:27:39 +00:00
Address greptile comments
This commit is contained in:
@@ -20,6 +20,10 @@ export const sanitizeLikeInput = (input: string): string => {
|
||||
};
|
||||
|
||||
export const parseTimeToPostgresInterval = (duration: string): string => {
|
||||
if (duration.length > 32) {
|
||||
throw new Error(`Invalid duration format: ${duration}. Use format like '30d', '1w', '3m', '1y'`);
|
||||
}
|
||||
|
||||
const durationRegex = new RE2("^(\\d+)([dwmy])$");
|
||||
const match = durationRegex.exec(duration);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
|
||||
import { getConfig } from "@app/lib/config/env";
|
||||
import { logger } from "@app/lib/logger";
|
||||
import { QueueJobs, QueueName, TQueueServiceFactory } from "@app/queue";
|
||||
|
||||
@@ -24,6 +25,7 @@ export const pkiAlertV2QueueServiceFactory = ({
|
||||
pkiAlertV2DAL,
|
||||
pkiAlertHistoryDAL
|
||||
}: TPkiAlertV2QueueServiceFactoryDep) => {
|
||||
const appCfg = getConfig();
|
||||
const calculateDeduplicationWindow = (alertBefore: string): number => {
|
||||
const alertDays = parseTimeToDays(alertBefore);
|
||||
|
||||
@@ -185,60 +187,34 @@ export const pkiAlertV2QueueServiceFactory = ({
|
||||
);
|
||||
};
|
||||
|
||||
queueService.start(QueueName.DailyPkiAlertV2Processing, async () => {
|
||||
logger.info(`${QueueName.DailyPkiAlertV2Processing}: queue task started`);
|
||||
|
||||
try {
|
||||
await processDailyAlerts();
|
||||
logger.info(`${QueueName.DailyPkiAlertV2Processing}: queue task completed successfully`);
|
||||
} catch (error) {
|
||||
logger.error(error, `${QueueName.DailyPkiAlertV2Processing}: queue task failed`);
|
||||
throw error;
|
||||
const init = async () => {
|
||||
if (appCfg.isSecondaryInstance) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
const startDailyAlertProcessing = async () => {
|
||||
await queueService.stopRepeatableJob(
|
||||
QueueName.DailyPkiAlertV2Processing,
|
||||
await queueService.startPg<QueueName.DailyPkiAlertV2Processing>(
|
||||
QueueJobs.DailyPkiAlertV2Processing,
|
||||
{ pattern: "* * * * *", utc: true },
|
||||
QueueName.DailyPkiAlertV2Processing
|
||||
async () => {
|
||||
try {
|
||||
logger.info(`${QueueJobs.DailyPkiAlertV2Processing}: queue task started`);
|
||||
await processDailyAlerts();
|
||||
logger.info(`${QueueJobs.DailyPkiAlertV2Processing}: queue task completed successfully`);
|
||||
} catch (error) {
|
||||
logger.error(error, `${QueueJobs.DailyPkiAlertV2Processing}: queue task failed`);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
{
|
||||
batchSize: 1,
|
||||
workerCount: 1,
|
||||
pollingIntervalSeconds: 60
|
||||
}
|
||||
);
|
||||
|
||||
await queueService.queue(QueueName.DailyPkiAlertV2Processing, QueueJobs.DailyPkiAlertV2Processing, undefined, {
|
||||
delay: 5000,
|
||||
jobId: QueueName.DailyPkiAlertV2Processing,
|
||||
repeat: { pattern: "* * * * *", utc: true }
|
||||
});
|
||||
|
||||
logger.info("Daily PKI alert processing job scheduled");
|
||||
await queueService.schedulePg(QueueJobs.DailyPkiAlertV2Processing, "* * * * *", undefined, { tz: "UTC" });
|
||||
};
|
||||
|
||||
const stopDailyAlertProcessing = async () => {
|
||||
await queueService.stopRepeatableJob(
|
||||
QueueName.DailyPkiAlertV2Processing,
|
||||
QueueJobs.DailyPkiAlertV2Processing,
|
||||
{ pattern: "* * * * *", utc: true },
|
||||
QueueName.DailyPkiAlertV2Processing
|
||||
);
|
||||
|
||||
logger.info("Daily PKI alert processing job stopped");
|
||||
};
|
||||
|
||||
const triggerAlertProcessing = async () => {
|
||||
await queueService.queue(QueueName.DailyPkiAlertV2Processing, QueueJobs.DailyPkiAlertV2Processing, undefined, {
|
||||
delay: 1000
|
||||
});
|
||||
};
|
||||
|
||||
queueService.listen(QueueName.DailyPkiAlertV2Processing, "failed", (_, err) => {
|
||||
logger.error(err, `${QueueName.DailyPkiAlertV2Processing}: Daily PKI alert processing failed`);
|
||||
});
|
||||
|
||||
return {
|
||||
startDailyAlertProcessing,
|
||||
stopDailyAlertProcessing,
|
||||
triggerAlertProcessing,
|
||||
processDailyAlerts
|
||||
init
|
||||
};
|
||||
};
|
||||
|
||||
@@ -374,6 +374,7 @@ export const pkiAlertV2ServiceFactory = ({
|
||||
const listCurrentMatchingCertificates = async ({
|
||||
projectId,
|
||||
filters,
|
||||
alertBefore,
|
||||
limit = 20,
|
||||
offset = 0,
|
||||
actorId,
|
||||
@@ -392,14 +393,22 @@ export const pkiAlertV2ServiceFactory = ({
|
||||
|
||||
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Read, ProjectPermissionSub.PkiAlerts);
|
||||
|
||||
try {
|
||||
parseTimeToPostgresInterval(alertBefore);
|
||||
} catch (error) {
|
||||
throw new BadRequestError({ message: "Invalid alertBefore format. Use format like '30d', '1w', '3m', '1y'" });
|
||||
}
|
||||
|
||||
const options: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
showPreview?: boolean;
|
||||
alertBefore?: string;
|
||||
} = {
|
||||
limit,
|
||||
offset,
|
||||
showPreview: true
|
||||
showPreview: true,
|
||||
alertBefore: parseTimeToPostgresInterval(alertBefore)
|
||||
};
|
||||
|
||||
const result = await pkiAlertV2DAL.findMatchingCertificates(projectId, filters, options);
|
||||
|
||||
@@ -8,9 +8,12 @@ const createSecureSlugValidator = () => {
|
||||
return (value: string) => slugRegex.test(value);
|
||||
};
|
||||
|
||||
const createSecureAlertBeforeValidator = () => {
|
||||
export const createSecureAlertBeforeValidator = () => {
|
||||
const alertBeforeRegex = new RE2("^\\d+[dwmy]$");
|
||||
return (value: string) => alertBeforeRegex.test(value);
|
||||
return (value: string) => {
|
||||
if (value.length > 32) return false;
|
||||
return alertBeforeRegex.test(value);
|
||||
};
|
||||
};
|
||||
|
||||
export enum PkiAlertEventType {
|
||||
|
||||
Reference in New Issue
Block a user