Merge pull request #2530 from Infisical/daniel/terraform-imports-prerequsuite

feat: terraform imports prerequisite / api improvements
This commit is contained in:
Daniel Hougaard
2024-10-05 02:18:46 +04:00
committed by GitHub
12 changed files with 246 additions and 20 deletions
@@ -215,29 +215,26 @@ export const projectEnvServiceFactory = ({
}
};
const getEnvironmentById = async ({ projectId, actor, actorId, actorOrgId, actorAuthMethod, id }: TGetEnvDTO) => {
const getEnvironmentById = async ({ actor, actorId, actorOrgId, actorAuthMethod, id }: TGetEnvDTO) => {
const environment = await projectEnvDAL.findById(id);
if (!environment) {
throw new NotFoundError({
message: "Environment does not exist"
});
}
const { permission } = await permissionService.getProjectPermission(
actor,
actorId,
projectId,
environment.projectId,
actorAuthMethod,
actorOrgId
);
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Read, ProjectPermissionSub.Environments);
const [env] = await projectEnvDAL.find({
id,
projectId
});
if (!env) {
throw new NotFoundError({
message: "Environment does not exist"
});
}
return env;
return environment;
};
return {
@@ -23,4 +23,4 @@ export type TReorderEnvDTO = {
export type TGetEnvDTO = {
id: string;
} & TProjectPermission;
} & Omit<TProjectPermission, "projectId">;
@@ -502,12 +502,21 @@ export const secretFolderServiceFactory = ({
const getFolderById = async ({ actor, actorId, actorOrgId, actorAuthMethod, id }: TGetFolderByIdDTO) => {
const folder = await folderDAL.findById(id);
if (!folder) throw new NotFoundError({ message: "folder not found" });
if (!folder) throw new NotFoundError({ message: "Folder not found" });
// folder list is allowed to be read by anyone
// permission to check does user has access
await permissionService.getProjectPermission(actor, actorId, folder.projectId, actorAuthMethod, actorOrgId);
return folder;
const [folderWithPath] = await folderDAL.findSecretPathByFolderIds(folder.projectId, [folder.id]);
if (!folderWithPath) {
throw new NotFoundError({ message: "Folder path not found" });
}
return {
...folder,
path: folderWithPath.path
};
};
return {
@@ -97,6 +97,34 @@ export const secretImportDALFactory = (db: TDbClient) => {
}
};
const findById = async (id: string, tx?: Knex) => {
try {
const doc = await (tx || db.replicaNode())(TableName.SecretImport)
.where({ [`${TableName.SecretImport}.id` as "id"]: id })
.join(TableName.Environment, `${TableName.SecretImport}.importEnv`, `${TableName.Environment}.id`)
.select(
db.ref("*").withSchema(TableName.SecretImport) as unknown as keyof TSecretImports,
db.ref("slug").withSchema(TableName.Environment),
db.ref("name").withSchema(TableName.Environment),
db.ref("id").withSchema(TableName.Environment).as("envId")
)
.first();
if (!doc) {
return null;
}
const { envId, slug, name, ...el } = doc;
return {
...el,
importEnv: { id: envId, slug, name }
};
} catch (error) {
throw new DatabaseError({ error, name: "Find secret imports" });
}
};
const getProjectImportCount = async (
{ search, ...filter }: Partial<TSecretImports & { projectId: string; search?: string }>,
tx?: Knex
@@ -144,6 +172,7 @@ export const secretImportDALFactory = (db: TDbClient) => {
return {
...secretImportOrm,
find,
findById,
findByFolderIds,
findLastImportPosition,
updateAllPosition,
@@ -24,6 +24,7 @@ import { fnSecretsFromImports, fnSecretsV2FromImports } from "./secret-import-fn
import {
TCreateSecretImportDTO,
TDeleteSecretImportDTO,
TGetSecretImportByIdDTO,
TGetSecretImportsDTO,
TGetSecretsFromImportDTO,
TResyncSecretImportReplicationDTO,
@@ -455,6 +456,64 @@ export const secretImportServiceFactory = ({
return secImports;
};
const getImportById = async ({
actor,
actorId,
actorAuthMethod,
actorOrgId,
id: importId
}: TGetSecretImportByIdDTO) => {
const importDoc = await secretImportDAL.findById(importId);
if (!importDoc) {
throw new NotFoundError({ message: "Secret import not found" });
}
// the folder to import into
const folder = await folderDAL.findById(importDoc.folderId);
if (!folder) throw new NotFoundError({ message: "Secret import folder not found" });
// the folder to import into, with path
const [folderWithPath] = await folderDAL.findSecretPathByFolderIds(folder.projectId, [folder.id]);
if (!folderWithPath) throw new NotFoundError({ message: "Folder path not found" });
const { permission } = await permissionService.getProjectPermission(
actor,
actorId,
folder.projectId,
actorAuthMethod,
actorOrgId
);
ForbiddenError.from(permission).throwUnlessCan(
ProjectPermissionActions.Read,
subject(ProjectPermissionSub.Secrets, {
environment: folder.environment.envSlug,
secretPath: folderWithPath.path
})
);
const importIntoEnv = await projectEnvDAL.findOne({
projectId: folder.projectId,
slug: folder.environment.envSlug
});
if (!importIntoEnv) throw new NotFoundError({ message: "Environment to import into not found" });
return {
...importDoc,
projectId: folder.projectId,
secretPath: folderWithPath.path,
environment: {
id: importIntoEnv.id,
slug: importIntoEnv.slug,
name: importIntoEnv.name
}
};
};
const getSecretsFromImports = async ({
path: secretPath,
environment,
@@ -565,6 +624,7 @@ export const secretImportServiceFactory = ({
updateImport,
deleteImport,
getImports,
getImportById,
getSecretsFromImports,
getRawSecretsFromImports,
resyncSecretImportReplication,
@@ -37,6 +37,10 @@ export type TGetSecretImportsDTO = {
offset?: number;
} & TProjectPermission;
export type TGetSecretImportByIdDTO = {
id: string;
} & Omit<TProjectPermission, "projectId">;
export type TGetSecretsFromImportDTO = {
environment: string;
path: string;