And workspace-environment specific integrations syncs to secrets v3 endpoints, add PostHog

This commit is contained in:
Tuan Dang
2023-04-17 14:23:56 +03:00
parent 338d287d35
commit 763ec1aa0f
18 changed files with 281 additions and 294 deletions
+4 -3
View File
@@ -1,3 +1,4 @@
import { Types } from 'mongoose';
import {
getSecretsHelper,
encryptSymmetricHelper,
@@ -21,7 +22,7 @@ class BotService {
workspaceId,
environment
}: {
workspaceId: string;
workspaceId: Types.ObjectId;
environment: string;
}) {
return await getSecretsHelper({
@@ -41,7 +42,7 @@ class BotService {
workspaceId,
plaintext
}: {
workspaceId: string;
workspaceId: Types.ObjectId;
plaintext: string;
}) {
return await encryptSymmetricHelper({
@@ -65,7 +66,7 @@ class BotService {
iv,
tag
}: {
workspaceId: string;
workspaceId: Types.ObjectId;
ciphertext: string;
iv: string;
tag: string;
+3 -1
View File
@@ -1,8 +1,10 @@
import { Types } from 'mongoose';
import { handleEventHelper } from '../helpers/event';
interface Event {
name: string;
workspaceId: string;
workspaceId: Types.ObjectId;
environment?: string;
payload: any;
}
+4 -2
View File
@@ -52,9 +52,11 @@ class IntegrationService {
* @param {Object} obj.workspaceId - id of workspace
*/
static async syncIntegrations({
workspaceId
workspaceId,
environment
}: {
workspaceId: string;
workspaceId: Types.ObjectId;
environment?: string;
}) {
return await syncIntegrationsHelper({
workspaceId
+29 -86
View File
@@ -11,6 +11,7 @@ import {
DeleteSecretParams
} from '../interfaces/services/SecretService';
import {
createSecretBlindIndexDataHelper,
generateSecretBlindIndexHelper,
createSecretHelper,
getSecretsHelper,
@@ -20,12 +21,28 @@ import {
} from '../helpers/secrets';
class SecretService {
/**
* Create secret blind index data containing encrypted blind index salt
* for workspace with id [workspaceId]
* @param {Object} obj
* @param {Types.ObjectId} obj.workspaceId
*/
static async createSecretBlindIndexData({
workspaceId
}: {
workspaceId: Types.ObjectId;
}) {
return await createSecretBlindIndexDataHelper({
workspaceId
});
}
/**
* Create and return blind index for secret with
* name [secretName] part of workspace with id [workspaceId]
* @param {Object} obj
* @param {Object} obj.secretName - name of secret to generate blind index for
* @param {Object} obj.workspaceId - id of workspace that secret belongs to
* @param {String} obj.secretName - name of secret to generate blind index for
* @param {Types.ObjectId} obj.workspaceId - id of workspace that secret belongs to
*/
static async generateSecretBlindIndex({
secretName,
@@ -50,32 +67,8 @@ class SecretService {
* @param {AuthData} obj.authData - authentication data on request
* @returns
*/
static async createSecret({
secretName,
workspaceId,
environment,
type,
authData,
secretKeyCiphertext,
secretKeyIV,
secretKeyTag,
secretValueCiphertext,
secretValueIV,
secretValueTag
}: CreateSecretParams) {
return await createSecretHelper({
secretName,
workspaceId,
environment,
type,
authData,
secretKeyCiphertext,
secretKeyIV,
secretKeyTag,
secretValueCiphertext,
secretValueIV,
secretValueTag
});
static async createSecret(createSecretParams: CreateSecretParams) {
return await createSecretHelper(createSecretParams);
}
/**
@@ -86,16 +79,8 @@ class SecretService {
* @param {AuthData} obj.authData - authentication data on request
* @returns
*/
static async getSecrets({
workspaceId,
environment,
authData
}: GetSecretsParams) {
return await getSecretsHelper({
workspaceId,
environment,
authData
});
static async getSecrets(getSecretsParams: GetSecretsParams) {
return await getSecretsHelper(getSecretsParams);
}
/**
@@ -108,20 +93,8 @@ class SecretService {
* @param {AuthData} obj.authData - authentication data on request
* @returns
*/
static async getSecret({
secretName,
workspaceId,
environment,
type,
authData
}: GetSecretParams) {
return await getSecretHelper({
secretName,
workspaceId,
environment,
type,
authData
});
static async getSecret(getSecretParams: GetSecretParams) {
return await getSecretHelper(getSecretParams);
}
/**
@@ -137,26 +110,8 @@ class SecretService {
* @param {AuthData} obj.authData - authentication data on request
* @returns
*/
static async updateSecret({
secretName,
workspaceId,
environment,
type,
secretValueCiphertext,
secretValueIV,
secretValueTag,
authData
}: UpdateSecretParams) {
return await updateSecretHelper({
secretName,
workspaceId,
environment,
type,
authData,
secretValueCiphertext,
secretValueIV,
secretValueTag
});
static async updateSecret(updateSecretParams: UpdateSecretParams) {
return await updateSecretHelper(updateSecretParams);
}
/**
@@ -169,20 +124,8 @@ class SecretService {
* @param {AuthData} obj.authData - authentication data on request
* @returns
*/
static async deleteSecret({
secretName,
workspaceId,
environment,
type,
authData
}: DeleteSecretParams) {
return await deleteSecretHelper({
secretName,
workspaceId,
environment,
type,
authData
});
static async deleteSecret(deleteSecretParams: DeleteSecretParams) {
return await deleteSecretHelper(deleteSecretParams);
}
}
+21 -34
View File
@@ -1,5 +1,6 @@
import { PostHog } from 'posthog-node';
import { getLogger } from '../utils/logger';
import { AuthData } from '../interfaces/middleware';
import {
getNodeEnv,
getTelemetryEnabled,
@@ -11,7 +12,8 @@ import {
User,
IServiceAccount,
ServiceAccount,
IServiceTokenData
IServiceTokenData,
ServiceTokenData
} from '../models';
import {
BadRequestError
@@ -48,42 +50,27 @@ class Telemetry {
return postHogClient;
}
/**
* Return a distinct id for client to be used for logging telemetry
*/
static getDistinctId = ({
user,
serviceAccount,
serviceTokenData
static getDistinctId ({
authData
}: {
user?: IUser;
serviceAccount?: IServiceAccount;
serviceTokenData?: any; // TODO: fix (it's ServiceTokenData with user populated)
}) => {
// TODO: modify to accept authData instead
let distinctId = '';
if (user) {
distinctId = user.email;
authData: AuthData;
}) {
let distinctId: any = '';
if (authData.authPayload instanceof User) {
distinctId = authData.authPayload.email;
} else if (authData.authPayload instanceof ServiceAccount) {
distinctId = `sa.${authData.authPayload._id.toString()}`;
} else if (authData.authPayload instanceof ServiceTokenData) {
if (authData.authPayload.user instanceof User) {
distinctId = authData.authPayload?.user.email;
} else if (authData.authPayload?.serviceAccount) {
distinctId = distinctId = `sa.${authData.authPayload.serviceAccount.toString()}`;
}
}
if (serviceAccount) {
distinctId = `sa.${serviceAccount._id.toString()}`;
}
if (serviceTokenData?.user && serviceTokenData?.user instanceof User) {
distinctId = serviceTokenData.user.email;
} else if (serviceTokenData?.serviceAccount && serviceTokenData?.serviceAccount instanceof ServiceAccount) {
distinctId = `sa.${serviceTokenData.serviceAccount._id.toString()}`;
}
if (distinctId === '') {
throw BadRequestError({
message: 'Failed to obtain distinct id for logging telemetry'
});
}
if (distinctId === '') throw BadRequestError({
message: 'Failed to obtain distinct id for logging telemetry'
});
return distinctId;
}