mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
feat: get secret import by ID
This commit is contained in:
@@ -122,6 +122,7 @@ export enum EventType {
|
||||
UPDATE_WEBHOOK_STATUS = "update-webhook-status",
|
||||
DELETE_WEBHOOK = "delete-webhook",
|
||||
GET_SECRET_IMPORTS = "get-secret-imports",
|
||||
GET_SECRET_IMPORT = "get-secret-import",
|
||||
CREATE_SECRET_IMPORT = "create-secret-import",
|
||||
UPDATE_SECRET_IMPORT = "update-secret-import",
|
||||
DELETE_SECRET_IMPORT = "delete-secret-import",
|
||||
@@ -1004,6 +1005,14 @@ interface GetSecretImportsEvent {
|
||||
};
|
||||
}
|
||||
|
||||
interface GetSecretImportEvent {
|
||||
type: EventType.GET_SECRET_IMPORT;
|
||||
metadata: {
|
||||
secretImportId: string;
|
||||
folderId: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface CreateSecretImportEvent {
|
||||
type: EventType.CREATE_SECRET_IMPORT;
|
||||
metadata: {
|
||||
@@ -1620,6 +1629,7 @@ export type Event =
|
||||
| UpdateWebhookStatusEvent
|
||||
| DeleteWebhookEvent
|
||||
| GetSecretImportsEvent
|
||||
| GetSecretImportEvent
|
||||
| CreateSecretImportEvent
|
||||
| UpdateSecretImportEvent
|
||||
| DeleteSecretImportEvent
|
||||
|
||||
@@ -312,6 +312,64 @@ export const registerSecretImportRouter = async (server: FastifyZodProvider) =>
|
||||
}
|
||||
});
|
||||
|
||||
server.route({
|
||||
url: "/:secretImportId",
|
||||
method: "GET",
|
||||
config: {
|
||||
rateLimit: readLimit
|
||||
},
|
||||
schema: {
|
||||
description: "Get single secret import",
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
}
|
||||
],
|
||||
params: z.object({
|
||||
secretImportId: z.string().trim().describe(SECRET_IMPORTS.GET.secretImportId)
|
||||
}),
|
||||
response: {
|
||||
200: z.object({
|
||||
secretImport: SecretImportsSchema.omit({ importEnv: true }).extend({
|
||||
environment: z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
slug: z.string()
|
||||
}),
|
||||
projectId: z.string(),
|
||||
importEnv: z.object({ name: z.string(), slug: z.string(), id: z.string() }),
|
||||
secretPath: z.string()
|
||||
})
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
onRequest: verifyAuth([AuthMode.JWT, AuthMode.API_KEY, AuthMode.SERVICE_TOKEN, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
||||
handler: async (req) => {
|
||||
const secretImport = await server.services.secretImport.getImportById({
|
||||
actorId: req.permission.id,
|
||||
actor: req.permission.type,
|
||||
actorAuthMethod: req.permission.authMethod,
|
||||
actorOrgId: req.permission.orgId,
|
||||
id: req.params.secretImportId
|
||||
});
|
||||
|
||||
await server.services.auditLog.createAuditLog({
|
||||
...req.auditLogInfo,
|
||||
projectId: secretImport.projectId,
|
||||
event: {
|
||||
type: EventType.GET_SECRET_IMPORT,
|
||||
metadata: {
|
||||
secretImportId: secretImport.id,
|
||||
folderId: secretImport.folderId
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return { secretImport };
|
||||
}
|
||||
});
|
||||
|
||||
server.route({
|
||||
url: "/secrets",
|
||||
method: "GET",
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user