From 0814245ce66e0cf85607467accf443490c15f1e0 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Fri, 4 Oct 2024 18:43:29 +0400 Subject: [PATCH] cleanup --- .../server/routes/v1/project-env-router.ts | 51 ++++++++++++++++++- .../project-env/project-env-service.ts | 25 ++++----- .../services/project-env/project-env-types.ts | 2 +- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/backend/src/server/routes/v1/project-env-router.ts b/backend/src/server/routes/v1/project-env-router.ts index fed609196..1b0e074c2 100644 --- a/backend/src/server/routes/v1/project-env-router.ts +++ b/backend/src/server/routes/v1/project-env-router.ts @@ -4,7 +4,7 @@ import { z } from "zod"; import { ProjectEnvironmentsSchema } from "@app/db/schemas"; import { EventType } from "@app/ee/services/audit-log/audit-log-types"; import { ENVIRONMENTS } from "@app/lib/api-docs"; -import { writeLimit } from "@app/server/config/rateLimiter"; +import { readLimit, writeLimit } from "@app/server/config/rateLimiter"; import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; import { AuthMode } from "@app/services/auth/auth-type"; @@ -23,6 +23,7 @@ export const registerProjectEnvRouter = async (server: FastifyZodProvider) => { } ], params: z.object({ + // NOTE(daniel): workspaceId isn't used, but we need to keep it for backwards compatibility. The endpoint defined below, uses no project ID, and is takes a pure environment ID. workspaceId: z.string().trim().describe(ENVIRONMENTS.GET.workspaceId), envId: z.string().trim().describe(ENVIRONMENTS.GET.id) }), @@ -39,7 +40,53 @@ export const registerProjectEnvRouter = async (server: FastifyZodProvider) => { actor: req.permission.type, actorOrgId: req.permission.orgId, actorAuthMethod: req.permission.authMethod, - projectId: req.params.workspaceId, + id: req.params.envId + }); + + await server.services.auditLog.createAuditLog({ + ...req.auditLogInfo, + projectId: environment.projectId, + event: { + type: EventType.GET_ENVIRONMENT, + metadata: { + id: environment.id + } + } + }); + + return { environment }; + } + }); + + server.route({ + method: "GET", + url: "/environments/:envId", + config: { + rateLimit: readLimit + }, + schema: { + description: "Get Environment by ID", + security: [ + { + bearerAuth: [] + } + ], + params: z.object({ + envId: z.string().trim().describe(ENVIRONMENTS.GET.id) + }), + response: { + 200: z.object({ + environment: ProjectEnvironmentsSchema + }) + } + }, + onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]), + handler: async (req) => { + const environment = await server.services.projectEnv.getEnvironmentById({ + actorId: req.permission.id, + actor: req.permission.type, + actorOrgId: req.permission.orgId, + actorAuthMethod: req.permission.authMethod, id: req.params.envId }); diff --git a/backend/src/services/project-env/project-env-service.ts b/backend/src/services/project-env/project-env-service.ts index 645f34ade..ea826b3a6 100644 --- a/backend/src/services/project-env/project-env-service.ts +++ b/backend/src/services/project-env/project-env-service.ts @@ -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 { diff --git a/backend/src/services/project-env/project-env-types.ts b/backend/src/services/project-env/project-env-types.ts index 27d808a47..229e8b195 100644 --- a/backend/src/services/project-env/project-env-types.ts +++ b/backend/src/services/project-env/project-env-types.ts @@ -23,4 +23,4 @@ export type TReorderEnvDTO = { export type TGetEnvDTO = { id: string; -} & TProjectPermission; +} & Omit;