mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 18:27:36 +00:00
fix: resolved lints, addressed feedback from rabbit, reptile and maidul
This commit is contained in:
@@ -68,18 +68,15 @@ const awsRegionFromHeader = (authorizationHeader: string): string | null => {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function isValidAwsRegion(region: string | null): boolean {
|
||||||
|
const validRegionPattern = new RE2("^[a-z0-9-]+$");
|
||||||
function isValidAwsRegion(region: (string | null)): boolean {
|
if (typeof region !== "string" || region.length === 0 || region.length > 20) {
|
||||||
const validRegionPattern = new RE2('^[a-z0-9-]+$');
|
|
||||||
if (typeof region !== 'string' || region.length === 0 || region.length > 20) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return validRegionPattern.test(region);
|
return validRegionPattern.test(region);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const identityAwsAuthServiceFactory = ({
|
export const identityAwsAuthServiceFactory = ({
|
||||||
identityAccessTokenDAL,
|
identityAccessTokenDAL,
|
||||||
identityAwsAuthDAL,
|
identityAwsAuthDAL,
|
||||||
@@ -100,7 +97,7 @@ export const identityAwsAuthServiceFactory = ({
|
|||||||
const region = headers.Authorization ? awsRegionFromHeader(headers.Authorization) : null;
|
const region = headers.Authorization ? awsRegionFromHeader(headers.Authorization) : null;
|
||||||
|
|
||||||
if (!isValidAwsRegion(region)) {
|
if (!isValidAwsRegion(region)) {
|
||||||
throw new BadRequestError({message: "Invalid AWS region"});
|
throw new BadRequestError({ message: "Invalid AWS region" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = region ? `https://sts.${region}.amazonaws.com` : identityAwsAuth.stsEndpoint;
|
const url = region ? `https://sts.${region}.amazonaws.com` : identityAwsAuth.stsEndpoint;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { MongoAbility } from "@casl/ability";
|
||||||
import { Knex } from "knex";
|
import { Knex } from "knex";
|
||||||
import { validate as uuidValidate } from "uuid";
|
import { validate as uuidValidate } from "uuid";
|
||||||
|
|
||||||
@@ -21,22 +22,21 @@ 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 { MongoAbility } from "@casl/ability";
|
|
||||||
|
|
||||||
export const SecretDalCacheKeys = {
|
export const SecretServiceCacheKeys = {
|
||||||
get productKey() {
|
get productKey() {
|
||||||
const { INFISICAL_PLATFORM_VERSION } = getConfig();
|
const { INFISICAL_PLATFORM_VERSION } = getConfig();
|
||||||
return `${ProjectType.SecretManager}:${INFISICAL_PLATFORM_VERSION || 0}`;
|
return `${ProjectType.SecretManager}:${INFISICAL_PLATFORM_VERSION || 0}`;
|
||||||
},
|
},
|
||||||
getSecretDalVersion: (projectId: string) => {
|
getSecretDalVersion: (projectId: string) => {
|
||||||
return `${SecretDalCacheKeys.productKey}:${projectId}:${TableName.SecretV2}-dal-version`;
|
return `${SecretServiceCacheKeys.productKey}:${projectId}:${TableName.SecretV2}-dal-version`;
|
||||||
},
|
},
|
||||||
getSecretsOfServiceLayer: (
|
getSecretsOfServiceLayer: (
|
||||||
projectId: string,
|
projectId: string,
|
||||||
version: number,
|
version: number,
|
||||||
dto: TGetSecretsDTO & { permissionRules: MongoAbility["rules"] }
|
dto: TGetSecretsDTO & { permissionRules: MongoAbility["rules"] }
|
||||||
) => {
|
) => {
|
||||||
return `${SecretDalCacheKeys.productKey}:${projectId}:${
|
return `${SecretServiceCacheKeys.productKey}:${projectId}:${
|
||||||
TableName.SecretV2
|
TableName.SecretV2
|
||||||
}-dal:v${version}:get-secrets-service-layer:${dto.actorId}-${generateCacheKeyFromData(dto)}`;
|
}-dal:v${version}:get-secrets-service-layer:${dto.actorId}-${generateCacheKeyFromData(dto)}`;
|
||||||
}
|
}
|
||||||
@@ -55,7 +55,7 @@ export const secretV2BridgeDALFactory = ({ db, keyStore }: TSecretV2DalArg) => {
|
|||||||
const secretOrm = ormify(db, TableName.SecretV2);
|
const secretOrm = ormify(db, TableName.SecretV2);
|
||||||
|
|
||||||
const invalidateSecretCacheByProjectId = async (projectId: string) => {
|
const invalidateSecretCacheByProjectId = async (projectId: string) => {
|
||||||
const secretDalVersionKey = SecretDalCacheKeys.getSecretDalVersion(projectId);
|
const secretDalVersionKey = SecretServiceCacheKeys.getSecretDalVersion(projectId);
|
||||||
await keyStore.incrementBy(secretDalVersionKey, 1);
|
await keyStore.incrementBy(secretDalVersionKey, 1);
|
||||||
await keyStore.setExpiry(secretDalVersionKey, SECRET_DAL_VERSION_TTL);
|
await keyStore.setExpiry(secretDalVersionKey, SECRET_DAL_VERSION_TTL);
|
||||||
};
|
};
|
||||||
@@ -287,7 +287,7 @@ export const secretV2BridgeDALFactory = ({ db, keyStore }: TSecretV2DalArg) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const findByFolderId = async (dto: { folderId: string; userId?: string; tx?: Knex; useCache?: boolean }) => {
|
const findByFolderId = async (dto: { folderId: string; userId?: string; tx?: Knex }) => {
|
||||||
try {
|
try {
|
||||||
const { folderId, tx } = dto;
|
const { folderId, tx } = dto;
|
||||||
let { userId } = dto;
|
let { userId } = dto;
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import { TSecretApprovalPolicyServiceFactory } from "@app/ee/services/secret-app
|
|||||||
import { TSecretApprovalRequestDALFactory } from "@app/ee/services/secret-approval-request/secret-approval-request-dal";
|
import { TSecretApprovalRequestDALFactory } from "@app/ee/services/secret-approval-request/secret-approval-request-dal";
|
||||||
import { TSecretApprovalRequestSecretDALFactory } from "@app/ee/services/secret-approval-request/secret-approval-request-secret-dal";
|
import { TSecretApprovalRequestSecretDALFactory } from "@app/ee/services/secret-approval-request/secret-approval-request-secret-dal";
|
||||||
import { TSecretSnapshotServiceFactory } from "@app/ee/services/secret-snapshot/secret-snapshot-service";
|
import { TSecretSnapshotServiceFactory } from "@app/ee/services/secret-snapshot/secret-snapshot-service";
|
||||||
|
import { TKeyStoreFactory } from "@app/keystore/keystore";
|
||||||
import { DatabaseErrorCode } from "@app/lib/error-codes";
|
import { DatabaseErrorCode } from "@app/lib/error-codes";
|
||||||
import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
|
import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
|
||||||
import { diff, groupBy } from "@app/lib/fn";
|
import { diff, groupBy } from "@app/lib/fn";
|
||||||
@@ -46,7 +47,7 @@ import { TSecretTagDALFactory } from "../secret-tag/secret-tag-dal";
|
|||||||
import {
|
import {
|
||||||
MAX_SECRET_CACHE_BYTES,
|
MAX_SECRET_CACHE_BYTES,
|
||||||
SECRET_DAL_TTL,
|
SECRET_DAL_TTL,
|
||||||
SecretDalCacheKeys,
|
SecretServiceCacheKeys,
|
||||||
TSecretV2BridgeDALFactory
|
TSecretV2BridgeDALFactory
|
||||||
} from "./secret-v2-bridge-dal";
|
} from "./secret-v2-bridge-dal";
|
||||||
import {
|
import {
|
||||||
@@ -81,7 +82,6 @@ import {
|
|||||||
} from "./secret-v2-bridge-types";
|
} from "./secret-v2-bridge-types";
|
||||||
import { TSecretVersionV2DALFactory } from "./secret-version-dal";
|
import { TSecretVersionV2DALFactory } from "./secret-version-dal";
|
||||||
import { TSecretVersionV2TagDALFactory } from "./secret-version-tag-dal";
|
import { TSecretVersionV2TagDALFactory } from "./secret-version-tag-dal";
|
||||||
import { TKeyStoreFactory } from "@app/keystore/keystore";
|
|
||||||
|
|
||||||
type TSecretV2BridgeServiceFactoryDep = {
|
type TSecretV2BridgeServiceFactoryDep = {
|
||||||
secretDAL: TSecretV2BridgeDALFactory;
|
secretDAL: TSecretV2BridgeDALFactory;
|
||||||
@@ -111,7 +111,7 @@ type TSecretV2BridgeServiceFactoryDep = {
|
|||||||
>;
|
>;
|
||||||
snapshotService: Pick<TSecretSnapshotServiceFactory, "performSnapshot">;
|
snapshotService: Pick<TSecretSnapshotServiceFactory, "performSnapshot">;
|
||||||
resourceMetadataDAL: Pick<TResourceMetadataDALFactory, "insertMany" | "delete">;
|
resourceMetadataDAL: Pick<TResourceMetadataDALFactory, "insertMany" | "delete">;
|
||||||
keyStore: Pick<TKeyStoreFactory, "getItem" | "setExpiry" | "setItemWithExpiry">;
|
keyStore: Pick<TKeyStoreFactory, "getItem" | "setExpiry" | "setItemWithExpiry" | "deleteItem">;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TSecretV2BridgeServiceFactory = ReturnType<typeof secretV2BridgeServiceFactory>;
|
export type TSecretV2BridgeServiceFactory = ReturnType<typeof secretV2BridgeServiceFactory>;
|
||||||
@@ -941,9 +941,9 @@ export const secretV2BridgeServiceFactory = ({
|
|||||||
});
|
});
|
||||||
throwIfMissingSecretReadValueOrDescribePermission(permission, ProjectPermissionSecretActions.DescribeSecret);
|
throwIfMissingSecretReadValueOrDescribePermission(permission, ProjectPermissionSecretActions.DescribeSecret);
|
||||||
|
|
||||||
const cachedSecretDalVersion = await keyStore.getItem(SecretDalCacheKeys.getSecretDalVersion(projectId));
|
const cachedSecretDalVersion = await keyStore.getItem(SecretServiceCacheKeys.getSecretDalVersion(projectId));
|
||||||
const secretDalVersion = Number(cachedSecretDalVersion || 0);
|
const secretDalVersion = Number(cachedSecretDalVersion || 0);
|
||||||
const cacheKey = SecretDalCacheKeys.getSecretsOfServiceLayer(projectId, secretDalVersion, {
|
const cacheKey = SecretServiceCacheKeys.getSecretsOfServiceLayer(projectId, secretDalVersion, {
|
||||||
...dto,
|
...dto,
|
||||||
permissionRules: permission.rules
|
permissionRules: permission.rules
|
||||||
});
|
});
|
||||||
@@ -953,18 +953,28 @@ export const secretV2BridgeServiceFactory = ({
|
|||||||
type: KmsDataKey.SecretManager,
|
type: KmsDataKey.SecretManager,
|
||||||
projectId
|
projectId
|
||||||
});
|
});
|
||||||
|
|
||||||
const encryptedCachedSecrets = await keyStore.getItem(cacheKey);
|
const encryptedCachedSecrets = await keyStore.getItem(cacheKey);
|
||||||
if (encryptedCachedSecrets) {
|
if (encryptedCachedSecrets) {
|
||||||
await keyStore.setExpiry(cacheKey, SECRET_DAL_TTL);
|
try {
|
||||||
const cachedSecrets = secretManagerDecryptor({ cipherTextBlob: Buffer.from(encryptedCachedSecrets, "base64") });
|
await keyStore.setExpiry(cacheKey, SECRET_DAL_TTL);
|
||||||
const { secrets, imports = [] } = JSON.parse(cachedSecrets.toString("utf8")) as {
|
const cachedSecrets = secretManagerDecryptor({ cipherTextBlob: Buffer.from(encryptedCachedSecrets, "base64") });
|
||||||
secrets: typeof decryptedSecrets;
|
const { secrets, imports = [] } = JSON.parse(cachedSecrets.toString("utf8")) as {
|
||||||
imports: typeof importedSecrets;
|
secrets: typeof decryptedSecrets;
|
||||||
};
|
imports: typeof importedSecrets;
|
||||||
return {
|
};
|
||||||
secrets: secrets.map((el) => ({ ...el, createdAt: new Date(el.createdAt), updatedAt: new Date(el.updatedAt) })),
|
return {
|
||||||
imports
|
secrets: secrets.map((el) => ({
|
||||||
};
|
...el,
|
||||||
|
createdAt: new Date(el.createdAt),
|
||||||
|
updatedAt: new Date(el.updatedAt)
|
||||||
|
})),
|
||||||
|
imports
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(err, "Secret service layer cache miss");
|
||||||
|
await keyStore.deleteItem(cacheKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let paths: { folderId: string; path: string }[] = [];
|
let paths: { folderId: string; path: string }[] = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user