mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 21:51:56 +00:00
feat: changed backend-pg to backend
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { Knex } from "knex";
|
||||
|
||||
import { TDbClient } from "@app/db";
|
||||
import { TableName } from "@app/db/schemas";
|
||||
import { DatabaseError } from "@app/lib/errors";
|
||||
import { ormify, selectAllTableCols } from "@app/lib/knex";
|
||||
|
||||
export type TSecretBlindIndexDALFactory = ReturnType<typeof secretBlindIndexDALFactory>;
|
||||
|
||||
export const secretBlindIndexDALFactory = (db: TDbClient) => {
|
||||
const secretBlindIndexOrm = ormify(db, TableName.SecretBlindIndex);
|
||||
|
||||
const countOfSecretsWithNullSecretBlindIndex = async (projectId: string, tx?: Knex) => {
|
||||
try {
|
||||
const doc = await (tx || db)(TableName.Secret)
|
||||
.leftJoin(
|
||||
TableName.SecretFolder,
|
||||
`${TableName.SecretFolder}.id`,
|
||||
`${TableName.Secret}.folderId`
|
||||
)
|
||||
.leftJoin(
|
||||
TableName.Environment,
|
||||
`${TableName.Environment}.id`,
|
||||
`${TableName.SecretFolder}.envId`
|
||||
)
|
||||
.where({ projectId })
|
||||
.whereNull("secretBlindIndex")
|
||||
.count(`${TableName.Secret}.id`);
|
||||
return (doc as any)?.[0]?.count || 0;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "CountOfSecretWillNullSecretBlindIndex" });
|
||||
}
|
||||
};
|
||||
|
||||
const findAllSecretsByProjectId = async (projectId: string, tx?: Knex) => {
|
||||
try {
|
||||
const docs = await (tx || db)(TableName.Secret)
|
||||
.leftJoin(
|
||||
TableName.SecretFolder,
|
||||
`${TableName.SecretFolder}.id`,
|
||||
`${TableName.Secret}.folderId`
|
||||
)
|
||||
.leftJoin(
|
||||
TableName.Environment,
|
||||
`${TableName.Environment}.id`,
|
||||
`${TableName.SecretFolder}.envId`
|
||||
)
|
||||
.where({ projectId })
|
||||
.whereNull("secretBlindIndex")
|
||||
.select(selectAllTableCols(TableName.Secret))
|
||||
.select(
|
||||
db.ref("slug").withSchema(TableName.Environment).as("environment"),
|
||||
db.ref("projectId").withSchema(TableName.Environment).as("workspace")
|
||||
);
|
||||
return docs;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "CountOfSecretWillNullSecretBlindIndex" });
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
...secretBlindIndexOrm,
|
||||
countOfSecretsWithNullSecretBlindIndex,
|
||||
findAllSecretsByProjectId
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,80 @@
|
||||
import { ProjectMembershipRole } from "@app/db/schemas";
|
||||
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service";
|
||||
import { BadRequestError, UnauthorizedError } from "@app/lib/errors";
|
||||
|
||||
import { TSecretDALFactory } from "../secret/secret-dal";
|
||||
import { generateSecretBlindIndexBySalt } from "../secret/secret-fns";
|
||||
import { TSecretBlindIndexDALFactory } from "./secret-blind-index-dal";
|
||||
import {
|
||||
TGetProjectBlindIndexStatusDTO,
|
||||
TGetProjectSecretsDTO,
|
||||
TUpdateProjectSecretNameDTO
|
||||
} from "./secret-blind-index-types";
|
||||
|
||||
type TSecretBlindIndexServiceFactoryDep = {
|
||||
permissionService: Pick<TPermissionServiceFactory, "getProjectPermission">;
|
||||
secretBlindIndexDAL: TSecretBlindIndexDALFactory;
|
||||
secretDAL:Pick<TSecretDALFactory,'bulkUpdate'>;
|
||||
};
|
||||
|
||||
export type TSecretBlindIndexServiceFactory = ReturnType<typeof secretBlindIndexServiceFactory>;
|
||||
|
||||
export const secretBlindIndexServiceFactory = ({
|
||||
secretBlindIndexDAL,
|
||||
permissionService,
|
||||
secretDAL
|
||||
}: TSecretBlindIndexServiceFactoryDep) => {
|
||||
const getSecretBlindIndexStatus = async ({
|
||||
actor,
|
||||
projectId,
|
||||
actorId
|
||||
}: TGetProjectBlindIndexStatusDTO) => {
|
||||
const { membership } = await permissionService.getProjectPermission(actor, actorId, projectId);
|
||||
if (membership?.role !== ProjectMembershipRole.Admin) {
|
||||
throw new UnauthorizedError({ message: "User must be admin" });
|
||||
}
|
||||
|
||||
const secretCount = await secretBlindIndexDAL.countOfSecretsWithNullSecretBlindIndex(projectId);
|
||||
return Number(secretCount);
|
||||
};
|
||||
|
||||
const getProjectSecrets = async ({ projectId, actorId, actor }: TGetProjectSecretsDTO) => {
|
||||
const { membership } = await permissionService.getProjectPermission(actor, actorId, projectId);
|
||||
if (membership?.role !== ProjectMembershipRole.Admin) {
|
||||
throw new UnauthorizedError({ message: "User must be admin" });
|
||||
}
|
||||
|
||||
const secrets = await secretBlindIndexDAL.findAllSecretsByProjectId(projectId);
|
||||
return secrets;
|
||||
};
|
||||
|
||||
const updateProjectSecretName = async ({
|
||||
projectId,
|
||||
actor,
|
||||
actorId,
|
||||
secretsToUpdate
|
||||
}: TUpdateProjectSecretNameDTO) => {
|
||||
const { membership } = await permissionService.getProjectPermission(actor, actorId, projectId);
|
||||
if (membership?.role !== ProjectMembershipRole.Admin) {
|
||||
throw new UnauthorizedError({ message: "User must be admin" });
|
||||
}
|
||||
const blindIndexCfg = await secretBlindIndexDAL.findOne({ projectId });
|
||||
if (!blindIndexCfg)
|
||||
throw new BadRequestError({ message: "Blind index not found", name: "CreateSecret" });
|
||||
|
||||
const operations = await Promise.all(secretsToUpdate.map(async ({secretName,secretId:id})=>{
|
||||
const secretBlindIndex = await generateSecretBlindIndexBySalt(secretName,blindIndexCfg);
|
||||
return { filter:{id},data:{secretBlindIndex} }
|
||||
}))
|
||||
|
||||
await secretBlindIndexDAL.transaction(async(tx)=>{
|
||||
await secretDAL.bulkUpdate(operations,tx)
|
||||
})
|
||||
};
|
||||
|
||||
return {
|
||||
getSecretBlindIndexStatus,
|
||||
getProjectSecrets,
|
||||
updateProjectSecretName
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
import { TProjectPermission } from "@app/lib/types";
|
||||
|
||||
export type TGetProjectBlindIndexStatusDTO = TProjectPermission;
|
||||
|
||||
export type TGetProjectSecretsDTO = TProjectPermission;
|
||||
|
||||
export type TUpdateProjectSecretNameDTO = TProjectPermission & {
|
||||
secretsToUpdate: {
|
||||
secretName: string;
|
||||
secretId: string;
|
||||
}[];
|
||||
};
|
||||
Reference in New Issue
Block a user