mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 23:27:35 +00:00
selectively get user email from service toke/jwt
This commit is contained in:
@@ -7,7 +7,7 @@ const { ValidationError } = mongoose.Error;
|
|||||||
import { BadRequestError, InternalServerError, UnauthorizedRequestError, ValidationError as RouteValidationError } from '../../utils/errors';
|
import { BadRequestError, InternalServerError, UnauthorizedRequestError, ValidationError as RouteValidationError } from '../../utils/errors';
|
||||||
import { AnyBulkWriteOperation } from 'mongodb';
|
import { AnyBulkWriteOperation } from 'mongodb';
|
||||||
import { SECRET_PERSONAL, SECRET_SHARED } from "../../variables";
|
import { SECRET_PERSONAL, SECRET_SHARED } from "../../variables";
|
||||||
// import { postHogClient } from '../../services';
|
import { postHogClient } from '../../services';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create secret for workspace with id [workspaceId] and environment [environment]
|
* Create secret for workspace with id [workspaceId] and environment [environment]
|
||||||
@@ -42,19 +42,19 @@ export const createSecret = async (req: Request, res: Response) => {
|
|||||||
throw RouteValidationError({ message: error.message, stack: error.stack })
|
throw RouteValidationError({ message: error.message, stack: error.stack })
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (postHogClient) {
|
if (postHogClient) {
|
||||||
// postHogClient.capture({
|
postHogClient.capture({
|
||||||
// event: 'secrets added',
|
event: 'secrets added',
|
||||||
// distinctId: req.user.email,
|
distinctId: req.user.email,
|
||||||
// properties: {
|
properties: {
|
||||||
// numberOfSecrets: 1,
|
numberOfSecrets: 1,
|
||||||
// workspaceId,
|
workspaceId,
|
||||||
// environment,
|
environment,
|
||||||
// channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
||||||
// userAgent: req.headers?.['user-agent']
|
userAgent: req.headers?.['user-agent']
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
res.status(200).send({
|
res.status(200).send({
|
||||||
secret
|
secret
|
||||||
@@ -103,19 +103,19 @@ export const createSecrets = async (req: Request, res: Response) => {
|
|||||||
throw InternalServerError({ message: "Unable to process your batch create request. Please try again", stack: bulkCreateError.stack })
|
throw InternalServerError({ message: "Unable to process your batch create request. Please try again", stack: bulkCreateError.stack })
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (postHogClient) {
|
if (postHogClient) {
|
||||||
// postHogClient.capture({
|
postHogClient.capture({
|
||||||
// event: 'secrets added',
|
event: 'secrets added',
|
||||||
// distinctId: req.user.email,
|
distinctId: req.user.email,
|
||||||
// properties: {
|
properties: {
|
||||||
// numberOfSecrets: (secretsToCreate ?? []).length,
|
numberOfSecrets: (secretsToCreate ?? []).length,
|
||||||
// workspaceId,
|
workspaceId,
|
||||||
// environment,
|
environment,
|
||||||
// channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
||||||
// userAgent: req.headers?.['user-agent']
|
userAgent: req.headers?.['user-agent']
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
res.status(200).send({
|
res.status(200).send({
|
||||||
secrets
|
secrets
|
||||||
@@ -158,19 +158,19 @@ export const deleteSecrets = async (req: Request, res: Response) => {
|
|||||||
throw InternalServerError()
|
throw InternalServerError()
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (postHogClient) {
|
if (postHogClient) {
|
||||||
// postHogClient.capture({
|
postHogClient.capture({
|
||||||
// event: 'secrets deleted',
|
event: 'secrets deleted',
|
||||||
// distinctId: req.user.email,
|
distinctId: req.user.email,
|
||||||
// properties: {
|
properties: {
|
||||||
// numberOfSecrets: numSecretsDeleted,
|
numberOfSecrets: numSecretsDeleted,
|
||||||
// environment: environmentName,
|
environment: environmentName,
|
||||||
// workspaceId,
|
workspaceId,
|
||||||
// channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
||||||
// userAgent: req.headers?.['user-agent']
|
userAgent: req.headers?.['user-agent']
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
res.status(200).send()
|
res.status(200).send()
|
||||||
}
|
}
|
||||||
@@ -183,19 +183,19 @@ export const deleteSecrets = async (req: Request, res: Response) => {
|
|||||||
export const deleteSecret = async (req: Request, res: Response) => {
|
export const deleteSecret = async (req: Request, res: Response) => {
|
||||||
await Secret.findByIdAndDelete(req._secret._id)
|
await Secret.findByIdAndDelete(req._secret._id)
|
||||||
|
|
||||||
// if (postHogClient) {
|
if (postHogClient) {
|
||||||
// postHogClient.capture({
|
postHogClient.capture({
|
||||||
// event: 'secrets deleted',
|
event: 'secrets deleted',
|
||||||
// distinctId: req.user.email,
|
distinctId: req.user.email,
|
||||||
// properties: {
|
properties: {
|
||||||
// numberOfSecrets: 1,
|
numberOfSecrets: 1,
|
||||||
// workspaceId: req._secret.workspace.toString(),
|
workspaceId: req._secret.workspace.toString(),
|
||||||
// environment: req._secret.environment,
|
environment: req._secret.environment,
|
||||||
// channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
||||||
// userAgent: req.headers?.['user-agent']
|
userAgent: req.headers?.['user-agent']
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
res.status(200).send({
|
res.status(200).send({
|
||||||
secret: req._secret
|
secret: req._secret
|
||||||
@@ -252,19 +252,19 @@ export const updateSecrets = async (req: Request, res: Response) => {
|
|||||||
throw InternalServerError()
|
throw InternalServerError()
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (postHogClient) {
|
if (postHogClient) {
|
||||||
// postHogClient.capture({
|
postHogClient.capture({
|
||||||
// event: 'secrets modified',
|
event: 'secrets modified',
|
||||||
// distinctId: req.user.email,
|
distinctId: req.user.email,
|
||||||
// properties: {
|
properties: {
|
||||||
// numberOfSecrets: (secretsModificationsRequested ?? []).length,
|
numberOfSecrets: (secretsModificationsRequested ?? []).length,
|
||||||
// environment: environmentName,
|
environment: environmentName,
|
||||||
// workspaceId,
|
workspaceId,
|
||||||
// channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
||||||
// userAgent: req.headers?.['user-agent']
|
userAgent: req.headers?.['user-agent']
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
return res.status(200).send()
|
return res.status(200).send()
|
||||||
}
|
}
|
||||||
@@ -304,19 +304,19 @@ export const updateSecret = async (req: Request, res: Response) => {
|
|||||||
throw RouteValidationError({ message: "Unable to apply modifications, please try again", stack: error.stack })
|
throw RouteValidationError({ message: "Unable to apply modifications, please try again", stack: error.stack })
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (postHogClient) {
|
if (postHogClient) {
|
||||||
// postHogClient.capture({
|
postHogClient.capture({
|
||||||
// event: 'secrets modified',
|
event: 'secrets modified',
|
||||||
// distinctId: req.user.email,
|
distinctId: req.user.email,
|
||||||
// properties: {
|
properties: {
|
||||||
// numberOfSecrets: 1,
|
numberOfSecrets: 1,
|
||||||
// environment: environmentName,
|
environment: environmentName,
|
||||||
// workspaceId,
|
workspaceId,
|
||||||
// channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
||||||
// userAgent: req.headers?.['user-agent']
|
userAgent: req.headers?.['user-agent']
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
return res.status(200).send(singleModificationUpdate)
|
return res.status(200).send(singleModificationUpdate)
|
||||||
}
|
}
|
||||||
@@ -332,13 +332,16 @@ export const getSecrets = async (req: Request, res: Response) => {
|
|||||||
const { environment } = req.query;
|
const { environment } = req.query;
|
||||||
const { workspaceId } = req.params;
|
const { workspaceId } = req.params;
|
||||||
|
|
||||||
let userId: string | undefined = undefined // used for getting personal secrets for user
|
let userId: Types.ObjectId | undefined = undefined // used for getting personal secrets for user
|
||||||
|
let userEmail: Types.ObjectId | undefined = undefined // used for posthog
|
||||||
if (req.user) {
|
if (req.user) {
|
||||||
userId = req.user._id.toString();
|
userId = req.user._id;
|
||||||
|
userEmail = req.user.email;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.serviceTokenData) {
|
if (req.serviceTokenData) {
|
||||||
userId = req.serviceTokenData.user._id
|
userId = req.serviceTokenData.user._id
|
||||||
|
userEmail = req.serviceTokenData.user.email;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [err, secrets] = await to(Secret.find(
|
const [err, secrets] = await to(Secret.find(
|
||||||
@@ -354,19 +357,19 @@ export const getSecrets = async (req: Request, res: Response) => {
|
|||||||
throw RouteValidationError({ message: "Failed to get secrets, please try again", stack: err.stack })
|
throw RouteValidationError({ message: "Failed to get secrets, please try again", stack: err.stack })
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (postHogClient) {
|
if (postHogClient) {
|
||||||
// postHogClient.capture({
|
postHogClient.capture({
|
||||||
// event: 'secrets pulled',
|
event: 'secrets pulled',
|
||||||
// distinctId: req.user.email,
|
distinctId: userEmail,
|
||||||
// properties: {
|
properties: {
|
||||||
// numberOfSecrets: (secrets ?? []).length,
|
numberOfSecrets: (secrets ?? []).length,
|
||||||
// environment,
|
environment,
|
||||||
// workspaceId,
|
workspaceId,
|
||||||
// channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
|
||||||
// userAgent: req.headers?.['user-agent']
|
userAgent: req.headers?.['user-agent']
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
return res.json(secrets)
|
return res.json(secrets)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,12 +162,15 @@ export const getSecrets = async (req: Request, res: Response) => {
|
|||||||
const { workspaceId, environment } = req.query;
|
const { workspaceId, environment } = req.query;
|
||||||
|
|
||||||
let userId: Types.ObjectId | undefined = undefined // used for getting personal secrets for user
|
let userId: Types.ObjectId | undefined = undefined // used for getting personal secrets for user
|
||||||
|
let userEmail: Types.ObjectId | undefined = undefined // used for posthog
|
||||||
if (req.user) {
|
if (req.user) {
|
||||||
userId = req.user._id;
|
userId = req.user._id;
|
||||||
|
userEmail = req.user.email;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.serviceTokenData) {
|
if (req.serviceTokenData) {
|
||||||
userId = req.serviceTokenData.user._id
|
userId = req.serviceTokenData.user._id
|
||||||
|
userEmail = req.serviceTokenData.user.email;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [err, secrets] = await to(Secret.find(
|
const [err, secrets] = await to(Secret.find(
|
||||||
@@ -204,7 +207,7 @@ export const getSecrets = async (req: Request, res: Response) => {
|
|||||||
if (postHogClient) {
|
if (postHogClient) {
|
||||||
postHogClient.capture({
|
postHogClient.capture({
|
||||||
event: 'secrets added',
|
event: 'secrets added',
|
||||||
distinctId: req.user.email,
|
distinctId: userEmail,
|
||||||
properties: {
|
properties: {
|
||||||
numberOfSecrets: secrets.length,
|
numberOfSecrets: secrets.length,
|
||||||
environment,
|
environment,
|
||||||
|
|||||||
Reference in New Issue
Block a user