From 654b8ab5cac36d42f2a7b9bd0976ffa7649786d7 Mon Sep 17 00:00:00 2001 From: Vladyslav Matsiiako Date: Sat, 3 Feb 2024 11:09:49 -0800 Subject: [PATCH 1/2] fix posthog events --- backend/src/server/routes/v3/secret-router.ts | 56 ++++++++++++------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/backend/src/server/routes/v3/secret-router.ts b/backend/src/server/routes/v3/secret-router.ts index 8df8533b8..c17d03b06 100644 --- a/backend/src/server/routes/v3/secret-router.ts +++ b/backend/src/server/routes/v3/secret-router.ts @@ -18,6 +18,7 @@ import { ActorType, AuthMode } from "@app/services/auth/auth-type"; import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types"; import { secretRawSchema } from "../sanitizedSchemas"; +import { getUserAgentType } from "@app/server/plugins/audit-log"; const getDistinctId = (req: FastifyRequest) => { if (req.auth.actor === ActorType.USER) { @@ -107,6 +108,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId, environment, secretPath: req.query.secretPath, + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -188,6 +190,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId, environment, secretPath: req.query.secretPath, + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -255,7 +258,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.body.workspaceId, environment: req.body.environment, secretPath: req.body.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -322,7 +325,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.body.workspaceId, environment: req.body.environment, secretPath: req.body.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -384,7 +387,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.body.workspaceId, environment: req.body.environment, secretPath: req.body.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -467,18 +470,29 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { } }); - server.services.telemetry.sendPostHogEvents({ - event: PostHogEventTypes.SecretPulled, - distinctId: getDistinctId(req), - properties: { - numberOfSecrets: secrets.length, - workspaceId: req.query.workspaceId, - environment: req.query.environment, - secretPath: req.query.secretPath, - - ...req.auditLogInfo + let shouldRecordK8Event = false; + if (req.authData.userAgent == "k8-operator") { + const randomNumber = Math.random(); + if (randomNumber > 0.95) { + shouldRecordK8Event = true; } - }); + } + const shouldCapture = req.authData.userAgent !== "k8-operator" || shouldRecordK8Event; + const approximateNumberTotalSecrets = secrets.length * 20; + if (shouldCapture) { + server.services.telemetry.sendPostHogEvents({ + event: PostHogEventTypes.SecretPulled, + distinctId: getDistinctId(req), + properties: { + numberOfSecrets: shouldRecordK8Event ? approximateNumberTotalSecrets : secrets.length, + workspaceId: req.query.workspaceId, + environment: req.query.environment, + secretPath: req.query.secretPath, + channel: getUserAgentType(req.headers["user-agent"]), + ...req.auditLogInfo + } + }); + } return { secrets, imports }; } @@ -550,7 +564,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.query.workspaceId, environment: req.query.environment, secretPath: req.query.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -711,7 +725,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.body.workspaceId, environment: req.body.environment, secretPath: req.body.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -890,7 +904,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.body.workspaceId, environment: req.body.environment, secretPath: req.body.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -1005,7 +1019,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.body.workspaceId, environment: req.body.environment, secretPath: req.body.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -1123,7 +1137,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.body.workspaceId, environment: req.body.environment, secretPath: req.body.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -1240,7 +1254,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.body.workspaceId, environment: req.body.environment, secretPath: req.body.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -1345,7 +1359,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId: req.body.workspaceId, environment: req.body.environment, secretPath: req.body.secretPath, - + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); From 4a566cf83f6572dfe143ef449898e5cf6f0b098b Mon Sep 17 00:00:00 2001 From: Maidul Islam Date: Sat, 3 Feb 2024 14:24:28 -0500 Subject: [PATCH 2/2] remove existent authData --- backend/src/server/routes/v3/secret-router.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/src/server/routes/v3/secret-router.ts b/backend/src/server/routes/v3/secret-router.ts index c17d03b06..5c5890a6b 100644 --- a/backend/src/server/routes/v3/secret-router.ts +++ b/backend/src/server/routes/v3/secret-router.ts @@ -13,12 +13,12 @@ import { EventType } from "@app/ee/services/audit-log/audit-log-types"; import { CommitType } from "@app/ee/services/secret-approval-request/secret-approval-request-types"; import { BadRequestError } from "@app/lib/errors"; import { removeTrailingSlash } from "@app/lib/fn"; +import { getUserAgentType } from "@app/server/plugins/audit-log"; import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; import { ActorType, AuthMode } from "@app/services/auth/auth-type"; import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types"; import { secretRawSchema } from "../sanitizedSchemas"; -import { getUserAgentType } from "@app/server/plugins/audit-log"; const getDistinctId = (req: FastifyRequest) => { if (req.auth.actor === ActorType.USER) { @@ -108,7 +108,7 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { workspaceId, environment, secretPath: req.query.secretPath, - channel: getUserAgentType(req.headers["user-agent"]), + channel: getUserAgentType(req.headers["user-agent"]), ...req.auditLogInfo } }); @@ -470,14 +470,16 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => { } }); + // TODO: Move to telemetry plugin let shouldRecordK8Event = false; - if (req.authData.userAgent == "k8-operator") { + if (req.headers["user-agent"] === "k8-operator") { const randomNumber = Math.random(); if (randomNumber > 0.95) { shouldRecordK8Event = true; } } - const shouldCapture = req.authData.userAgent !== "k8-operator" || shouldRecordK8Event; + + const shouldCapture = req.headers["user-agent"] !== "k8-operator" || shouldRecordK8Event; const approximateNumberTotalSecrets = secrets.length * 20; if (shouldCapture) { server.services.telemetry.sendPostHogEvents({