mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
misc: added license check in backend for custom rate limits
This commit is contained in:
@@ -575,6 +575,9 @@ export const licenseServiceFactory = ({
|
||||
getInstanceType() {
|
||||
return instanceType;
|
||||
},
|
||||
get onPremFeatures() {
|
||||
return onPremFeatures;
|
||||
},
|
||||
getPlan,
|
||||
updateSubscriptionOrgMemberCount,
|
||||
refreshPlan,
|
||||
|
||||
@@ -2,6 +2,7 @@ import { CronJob } from "cron";
|
||||
|
||||
import { logger } from "@app/lib/logger";
|
||||
|
||||
import { TLicenseServiceFactory } from "../license/license-service";
|
||||
import { TRateLimitDALFactory } from "./rate-limit-dal";
|
||||
import { TRateLimit, TRateLimitUpdateDTO } from "./rate-limit-types";
|
||||
|
||||
@@ -24,11 +25,12 @@ export const getRateLimiterConfig = () => {
|
||||
|
||||
type TRateLimitServiceFactoryDep = {
|
||||
rateLimitDAL: TRateLimitDALFactory;
|
||||
licenseService: Pick<TLicenseServiceFactory, "onPremFeatures">;
|
||||
};
|
||||
|
||||
export type TRateLimitServiceFactory = ReturnType<typeof rateLimitServiceFactory>;
|
||||
|
||||
export const rateLimitServiceFactory = ({ rateLimitDAL }: TRateLimitServiceFactoryDep) => {
|
||||
export const rateLimitServiceFactory = ({ rateLimitDAL, licenseService }: TRateLimitServiceFactoryDep) => {
|
||||
const DEFAULT_RATE_LIMIT_CONFIG_ID = "00000000-0000-0000-0000-000000000000";
|
||||
|
||||
const getRateLimits = async (): Promise<TRateLimit | undefined> => {
|
||||
@@ -78,7 +80,16 @@ export const rateLimitServiceFactory = ({ rateLimitDAL }: TRateLimitServiceFacto
|
||||
}
|
||||
};
|
||||
|
||||
const initializeBackgroundSync = () => {
|
||||
const initializeBackgroundSync = async () => {
|
||||
if (!licenseService.onPremFeatures.customRateLimits) {
|
||||
logger.info("Current license does not support custom rate limit configuration");
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info("Setting up backround sync process for rate limits");
|
||||
// initial sync upon startup
|
||||
await syncRateLimitConfiguration();
|
||||
|
||||
// sync rate limits configuration every 10 minutes
|
||||
const job = new CronJob("*/10 * * * *", syncRateLimitConfiguration);
|
||||
job.start();
|
||||
|
||||
@@ -14,8 +14,6 @@ import fasitfy from "fastify";
|
||||
import { Knex } from "knex";
|
||||
import { Logger } from "pino";
|
||||
|
||||
import { rateLimitDALFactory } from "@app/ee/services/rate-limit/rate-limit-dal";
|
||||
import { rateLimitServiceFactory } from "@app/ee/services/rate-limit/rate-limit-service";
|
||||
import { TKeyStoreFactory } from "@app/keystore/keystore";
|
||||
import { getConfig } from "@app/lib/config/env";
|
||||
import { TQueueServiceFactory } from "@app/queue";
|
||||
@@ -71,9 +69,6 @@ export const main = async ({ db, smtp, logger, queue, keyStore }: TMain) => {
|
||||
|
||||
// Rate limiters and security headers
|
||||
if (appCfg.isProductionMode) {
|
||||
const rateLimitDAL = rateLimitDALFactory(db);
|
||||
const rateLimitService = rateLimitServiceFactory({ rateLimitDAL });
|
||||
await rateLimitService.syncRateLimitConfiguration();
|
||||
await server.register<FastifyRateLimitOptions>(ratelimiter, globalRateLimiterCfg());
|
||||
}
|
||||
|
||||
|
||||
@@ -449,7 +449,8 @@ export const registerRoutes = async (
|
||||
keyStore
|
||||
});
|
||||
const rateLimitService = rateLimitServiceFactory({
|
||||
rateLimitDAL
|
||||
rateLimitDAL,
|
||||
licenseService
|
||||
});
|
||||
const apiKeyService = apiKeyServiceFactory({ apiKeyDAL, userDAL });
|
||||
|
||||
@@ -910,7 +911,10 @@ export const registerRoutes = async (
|
||||
|
||||
const cronJobs: CronJob[] = [];
|
||||
if (appCfg.isProductionMode) {
|
||||
cronJobs.push(rateLimitService.initializeBackgroundSync());
|
||||
const rateLimitJob = await rateLimitService.initializeBackgroundSync();
|
||||
if (rateLimitJob) {
|
||||
cronJobs.push(rateLimitJob);
|
||||
}
|
||||
}
|
||||
|
||||
server.decorate<FastifyZodProvider["store"]>("store", {
|
||||
|
||||
Reference in New Issue
Block a user