mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 18:27:36 +00:00
Merge pull request #3505 from akhilmhdh/feat/cache-jitter
feat: increased secret caching to 10mins with jitter of 2min
This commit is contained in:
@@ -2,7 +2,7 @@ export const daysToMillisecond = (days: number) => days * 24 * 60 * 60 * 1000;
|
|||||||
|
|
||||||
export const secondsToMillis = (seconds: number) => seconds * 1000;
|
export const secondsToMillis = (seconds: number) => seconds * 1000;
|
||||||
|
|
||||||
export const applyJitter = (delayMs: number, jitterMs: number) => {
|
export const applyJitter = (delay: number, jitter: number) => {
|
||||||
const jitter = Math.floor(Math.random() * (2 * jitterMs)) - jitterMs;
|
const jitterTime = Math.floor(Math.random() * (2 * jitter)) - jitter;
|
||||||
return delayMs + jitter;
|
return delay + jitterTime;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import type {
|
|||||||
TFindSecretsByFolderIdsFilter,
|
TFindSecretsByFolderIdsFilter,
|
||||||
TGetSecretsDTO
|
TGetSecretsDTO
|
||||||
} from "@app/services/secret-v2-bridge/secret-v2-bridge-types";
|
} from "@app/services/secret-v2-bridge/secret-v2-bridge-types";
|
||||||
|
import { applyJitter } from "@app/lib/dates";
|
||||||
|
|
||||||
export const SecretServiceCacheKeys = {
|
export const SecretServiceCacheKeys = {
|
||||||
get productKey() {
|
get productKey() {
|
||||||
@@ -48,7 +49,7 @@ interface TSecretV2DalArg {
|
|||||||
keyStore: TKeyStoreFactory;
|
keyStore: TKeyStoreFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SECRET_DAL_TTL = 5 * 60;
|
export const SECRET_DAL_TTL = () => applyJitter(10 * 60, 2 * 60);
|
||||||
export const SECRET_DAL_VERSION_TTL = 15 * 60;
|
export const SECRET_DAL_VERSION_TTL = 15 * 60;
|
||||||
export const MAX_SECRET_CACHE_BYTES = 25 * 1024 * 1024;
|
export const MAX_SECRET_CACHE_BYTES = 25 * 1024 * 1024;
|
||||||
export const secretV2BridgeDALFactory = ({ db, keyStore }: TSecretV2DalArg) => {
|
export const secretV2BridgeDALFactory = ({ db, keyStore }: TSecretV2DalArg) => {
|
||||||
|
|||||||
@@ -962,7 +962,7 @@ export const secretV2BridgeServiceFactory = ({
|
|||||||
const encryptedCachedSecrets = await keyStore.getItem(cacheKey);
|
const encryptedCachedSecrets = await keyStore.getItem(cacheKey);
|
||||||
if (encryptedCachedSecrets) {
|
if (encryptedCachedSecrets) {
|
||||||
try {
|
try {
|
||||||
await keyStore.setExpiry(cacheKey, SECRET_DAL_TTL);
|
await keyStore.setExpiry(cacheKey, SECRET_DAL_TTL());
|
||||||
const cachedSecrets = secretManagerDecryptor({ cipherTextBlob: Buffer.from(encryptedCachedSecrets, "base64") });
|
const cachedSecrets = secretManagerDecryptor({ cipherTextBlob: Buffer.from(encryptedCachedSecrets, "base64") });
|
||||||
const { secrets, imports = [] } = JSON.parse(cachedSecrets.toString("utf8")) as {
|
const { secrets, imports = [] } = JSON.parse(cachedSecrets.toString("utf8")) as {
|
||||||
secrets: typeof decryptedSecrets;
|
secrets: typeof decryptedSecrets;
|
||||||
@@ -1132,7 +1132,7 @@ export const secretV2BridgeServiceFactory = ({
|
|||||||
plainText: Buffer.from(JSON.stringify(payload))
|
plainText: Buffer.from(JSON.stringify(payload))
|
||||||
}).cipherTextBlob;
|
}).cipherTextBlob;
|
||||||
if (encryptedUpdatedCachedSecrets.byteLength < MAX_SECRET_CACHE_BYTES) {
|
if (encryptedUpdatedCachedSecrets.byteLength < MAX_SECRET_CACHE_BYTES) {
|
||||||
await keyStore.setItemWithExpiry(cacheKey, SECRET_DAL_TTL, encryptedUpdatedCachedSecrets.toString("base64"));
|
await keyStore.setItemWithExpiry(cacheKey, SECRET_DAL_TTL(), encryptedUpdatedCachedSecrets.toString("base64"));
|
||||||
}
|
}
|
||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
@@ -1179,7 +1179,7 @@ export const secretV2BridgeServiceFactory = ({
|
|||||||
plainText: Buffer.from(JSON.stringify(payload))
|
plainText: Buffer.from(JSON.stringify(payload))
|
||||||
}).cipherTextBlob;
|
}).cipherTextBlob;
|
||||||
if (encryptedUpdatedCachedSecrets.byteLength < MAX_SECRET_CACHE_BYTES) {
|
if (encryptedUpdatedCachedSecrets.byteLength < MAX_SECRET_CACHE_BYTES) {
|
||||||
await keyStore.setItemWithExpiry(cacheKey, SECRET_DAL_TTL, encryptedUpdatedCachedSecrets.toString("base64"));
|
await keyStore.setItemWithExpiry(cacheKey, SECRET_DAL_TTL(), encryptedUpdatedCachedSecrets.toString("base64"));
|
||||||
}
|
}
|
||||||
return payload;
|
return payload;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user