diff --git a/.github/workflows/apps-event-worker.yaml b/.github/workflows/apps-event-worker.yaml index 8a16070c..9d6df9b2 100644 --- a/.github/workflows/apps-event-worker.yaml +++ b/.github/workflows/apps-event-worker.yaml @@ -6,20 +6,20 @@ on: paths: - apps/event-worker/** - packages/db/** + - packages/job-dispatch/** - packages/validators/** - .github/workflows/apps-event-worker.yaml - pnpm-lock.yaml - - packages/job-dispatch/** push: branches: ["main"] paths: - apps/event-worker/** - packages/db/** + - packages/job-dispatch/** - packages/validators/** - .github/workflows/apps-event-worker.yaml - pnpm-lock.yaml - - packages/job-dispatch/** - + jobs: build: runs-on: ubuntu-latest diff --git a/apps/event-worker/src/target-scan/gke.ts b/apps/event-worker/src/target-scan/gke.ts index 7ab14c6f..9b9065ae 100644 --- a/apps/event-worker/src/target-scan/gke.ts +++ b/apps/event-worker/src/target-scan/gke.ts @@ -120,15 +120,13 @@ export const getGkeTargets = async ( cluster, ); return namespaces - .filter((n) => n.metadata != null) + .filter((n) => n.metadata?.name != null) .map((n) => _.merge(clusterTarget, { name: `${cluster.name ?? cluster.id ?? ""}/${n.metadata!.name}`, kind: "Namespace", identifier: `${project}/${cluster.name}/${n.metadata!.name}`, - config: { - namespace: n.metadata!.name, - }, + config: { namespace: n.metadata!.name }, metadata: { [ReservedMetadataKey.ParentTargetIdentifier]: clusterTarget.identifier, diff --git a/packages/api/package.json b/packages/api/package.json index 864b922d..bd1be0d6 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -22,6 +22,7 @@ "@ctrlplane/auth": "workspace:*", "@ctrlplane/db": "workspace:*", "@ctrlplane/job-dispatch": "workspace:*", + "@ctrlplane/logger": "workspace:*", "@ctrlplane/validators": "workspace:*", "@octokit/auth-app": "^7.1.0", "@octokit/rest": "catalog:", diff --git a/packages/api/src/trpc.ts b/packages/api/src/trpc.ts index ddbf513d..18c648cd 100644 --- a/packages/api/src/trpc.ts +++ b/packages/api/src/trpc.ts @@ -6,17 +6,15 @@ import { ZodError } from "zod"; import { can } from "@ctrlplane/auth/utils"; import { db } from "@ctrlplane/db/client"; +import { logger } from "@ctrlplane/logger"; export const createTRPCContext = (opts: { headers: Headers; session: Session | null; }) => { const session = opts.session; - const source = opts.headers.get("x-trpc-source") ?? "unknown"; - - console.log(">>> tRPC Request from", source, "by", session?.user.email); - - return { session, db }; + const trpcSource = opts.headers.get("x-trpc-source") ?? "unknown"; + return { trpcSource, session, db }; }; export type Context = ReturnType; @@ -54,9 +52,37 @@ export const createCallerFactory = t.createCallerFactory; */ export const createTRPCRouter = t.router; -export const publicProcedure = t.procedure; +export const loggedProcedure = t.procedure.use(async (opts) => { + const start = Date.now(); + + const result = await opts.next(); + + const durationMs = Date.now() - start; + + const session = opts.ctx.session; + const email = session?.user.email ?? "unknown"; + const source = opts.ctx.trpcSource; + + const meta = { + label: "trpc", + path: opts.path, + type: opts.type, + durationMs, + ok: result.ok, + }; -const authnProcedure = t.procedure.use(({ ctx, next }) => { + const log = durationMs > 100 || !result.ok ? logger.warning : logger.info; + log( + `${result.ok ? "OK" : "NOT OK"} - request from ${source} by ${email}`, + meta, + ); + + return result; +}); + +export const publicProcedure = loggedProcedure; + +const authnProcedure = loggedProcedure.use(({ ctx, next }) => { if (!ctx.session?.user) throw new TRPCError({ code: "UNAUTHORIZED" }); return next({ ctx: { @@ -65,6 +91,7 @@ const authnProcedure = t.procedure.use(({ ctx, next }) => { }, }); }); + const authzProcedure = authnProcedure.use( async ({ ctx, meta, path, getRawInput, next }) => { const { authorizationCheck } = meta ?? {}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8298cbd7..34d0b274 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -613,6 +613,9 @@ importers: '@ctrlplane/job-dispatch': specifier: workspace:* version: link:../job-dispatch + '@ctrlplane/logger': + specifier: workspace:* + version: link:../logger '@ctrlplane/validators': specifier: workspace:* version: link:../validators