Skip to content

Commit

Permalink
refactor(core): Decouple credentials from internal hooks (no-changelo…
Browse files Browse the repository at this point in the history
…g) (#10162)
  • Loading branch information
ivov authored and cstuncsik committed Jul 31, 2024
1 parent eb0f18f commit 06a1afd
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 135 deletions.
71 changes: 0 additions & 71 deletions packages/cli/src/InternalHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,77 +547,6 @@ export class InternalHooks {
});
}

/**
* Credentials
*/

async onUserCreatedCredentials(userCreatedCredentialsData: {
user: User;
credential_name: string;
credential_type: string;
credential_id: string;
public_api: boolean;
}): Promise<void> {
const project = await this.sharedCredentialsRepository.findCredentialOwningProject(
userCreatedCredentialsData.credential_id,
);
void this.telemetry.track('User created credentials', {
user_id: userCreatedCredentialsData.user.id,
credential_type: userCreatedCredentialsData.credential_type,
credential_id: userCreatedCredentialsData.credential_id,
instance_id: this.instanceSettings.instanceId,
project_id: project?.id,
project_type: project?.type,
});
}

async onUserSharedCredentials(userSharedCredentialsData: {
user: User;
credential_name: string;
credential_type: string;
credential_id: string;
user_id_sharer: string;
user_ids_sharees_added: string[];
sharees_removed: number | null;
}): Promise<void> {
void this.telemetry.track('User updated cred sharing', {
user_id: userSharedCredentialsData.user.id,
credential_type: userSharedCredentialsData.credential_type,
credential_id: userSharedCredentialsData.credential_id,
user_id_sharer: userSharedCredentialsData.user_id_sharer,
user_ids_sharees_added: userSharedCredentialsData.user_ids_sharees_added,
sharees_removed: userSharedCredentialsData.sharees_removed,
instance_id: this.instanceSettings.instanceId,
});
}

async onUserUpdatedCredentials(userUpdatedCredentialsData: {
user: User;
credential_name: string;
credential_type: string;
credential_id: string;
}): Promise<void> {
void this.telemetry.track('User updated credentials', {
user_id: userUpdatedCredentialsData.user.id,
credential_type: userUpdatedCredentialsData.credential_type,
credential_id: userUpdatedCredentialsData.credential_id,
});
}

async onUserDeletedCredentials(userUpdatedCredentialsData: {
user: User;
credential_name: string;
credential_type: string;
credential_id: string;
}): Promise<void> {
void this.telemetry.track('User deleted credentials', {
user_id: userUpdatedCredentialsData.user.id,
credential_type: userUpdatedCredentialsData.credential_type,
credential_id: userUpdatedCredentialsData.credential_id,
instance_id: this.instanceSettings.instanceId,
});
}

/*
* Execution Statistics
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { Container } from 'typedi';
import { CredentialsRepository } from '@db/repositories/credentials.repository';
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
import { ProjectRepository } from '@/databases/repositories/project.repository';
import { InternalHooks } from '@/InternalHooks';
import { EventService } from '@/eventbus/event.service';

export async function getCredentials(credentialId: string): Promise<ICredentialsDb | null> {
Expand Down Expand Up @@ -52,22 +51,7 @@ export async function saveCredential(
user: User,
encryptedData: ICredentialsDb,
): Promise<CredentialsEntity> {
await Container.get(ExternalHooks).run('credentials.create', [encryptedData]);
void Container.get(InternalHooks).onUserCreatedCredentials({
user,
credential_name: credential.name,
credential_type: credential.type,
credential_id: credential.id,
public_api: true,
});
Container.get(EventService).emit('credentials-created', {
user,
credentialName: credential.name,
credentialType: credential.type,
credentialId: credential.id,
});

return await Db.transaction(async (transactionManager) => {
const result = await Db.transaction(async (transactionManager) => {
const savedCredential = await transactionManager.save<CredentialsEntity>(credential);

savedCredential.data = credential.data;
Expand All @@ -89,22 +73,32 @@ export async function saveCredential(

return savedCredential;
});

await Container.get(ExternalHooks).run('credentials.create', [encryptedData]);

const project = await Container.get(SharedCredentialsRepository).findCredentialOwningProject(
credential.id,
);

Container.get(EventService).emit('credentials-created', {
user,
credentialType: credential.type,
credentialId: credential.id,
projectId: project?.id,
projectType: project?.type,
publicApi: true,
});

return result;
}

export async function removeCredential(
user: User,
credentials: CredentialsEntity,
): Promise<ICredentialsDb> {
await Container.get(ExternalHooks).run('credentials.delete', [credentials.id]);
void Container.get(InternalHooks).onUserDeletedCredentials({
user,
credential_name: credentials.name,
credential_type: credentials.type,
credential_id: credentials.id,
});
Container.get(EventService).emit('credentials-deleted', {
user,
credentialName: credentials.name,
credentialType: credentials.type,
credentialId: credentials.id,
});
Expand Down
43 changes: 8 additions & 35 deletions packages/cli/src/credentials/credentials.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { In } from '@n8n/typeorm';

import { CredentialsService } from './credentials.service';
import { CredentialRequest } from '@/requests';
import { InternalHooks } from '@/InternalHooks';
import { Logger } from '@/Logger';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { ForbiddenError } from '@/errors/response-errors/forbidden.error';
Expand Down Expand Up @@ -42,7 +41,6 @@ export class CredentialsController {
private readonly namingService: NamingService,
private readonly license: License,
private readonly logger: Logger,
private readonly internalHooks: InternalHooks,
private readonly userManagementMailer: UserManagementMailer,
private readonly sharedCredentialsRepository: SharedCredentialsRepository,
private readonly projectRelationRepository: ProjectRelationRepository,
Expand Down Expand Up @@ -162,18 +160,17 @@ export class CredentialsController {
req.body.projectId,
);

void this.internalHooks.onUserCreatedCredentials({
user: req.user,
credential_name: newCredential.name,
credential_type: credential.type,
credential_id: credential.id,
public_api: false,
});
const project = await this.sharedCredentialsRepository.findCredentialOwningProject(
credential.id,
);

this.eventService.emit('credentials-created', {
user: req.user,
credentialName: newCredential.name,
credentialType: credential.type,
credentialId: credential.id,
publicApi: false,
projectId: project?.id,
projectType: project?.type,
});

const scopes = await this.credentialsService.getCredentialScopes(req.user, credential.id);
Expand Down Expand Up @@ -223,15 +220,8 @@ export class CredentialsController {

this.logger.verbose('Credential updated', { credentialId });

void this.internalHooks.onUserUpdatedCredentials({
user: req.user,
credential_name: credential.name,
credential_type: credential.type,
credential_id: credential.id,
});
this.eventService.emit('credentials-updated', {
user: req.user,
credentialName: credential.name,
credentialType: credential.type,
credentialId: credential.id,
});
Expand Down Expand Up @@ -264,15 +254,8 @@ export class CredentialsController {

await this.credentialsService.delete(credential);

void this.internalHooks.onUserDeletedCredentials({
user: req.user,
credential_name: credential.name,
credential_type: credential.type,
credential_id: credential.id,
});
this.eventService.emit('credentials-deleted', {
user: req.user,
credentialName: credential.name,
credentialType: credential.type,
credentialId: credential.id,
});
Expand Down Expand Up @@ -332,22 +315,12 @@ export class CredentialsController {
newShareeIds = toShare;
});

void this.internalHooks.onUserSharedCredentials({
user: req.user,
credential_name: credential.name,
credential_type: credential.type,
credential_id: credential.id,
user_id_sharer: req.user.id,
user_ids_sharees_added: newShareeIds,
sharees_removed: amountRemoved,
});
this.eventService.emit('credentials-shared', {
user: req.user,
credentialName: credential.name,
credentialType: credential.type,
credentialId: credential.id,
userIdSharer: req.user.id,
userIdsShareesRemoved: newShareeIds,
userIdsShareesAdded: newShareeIds,
shareesRemoved: amountRemoved,
});

Expand Down
9 changes: 4 additions & 5 deletions packages/cli/src/eventbus/event.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,30 @@ export type Event = {

'credentials-created': {
user: UserLike;
credentialName: string;
credentialType: string;
credentialId: string;
publicApi: boolean;
projectId?: string;
projectType?: string;
};

'credentials-shared': {
user: UserLike;
credentialName: string;
credentialType: string;
credentialId: string;
userIdSharer: string;
userIdsShareesRemoved: string[];
userIdsShareesAdded: string[];
shareesRemoved: number | null;
};

'credentials-updated': {
user: UserLike;
credentialName: string;
credentialType: string;
credentialId: string;
};

'credentials-deleted': {
user: UserLike;
credentialName: string;
credentialType: string;
credentialId: string;
};
Expand Down
63 changes: 63 additions & 0 deletions packages/cli/src/telemetry/telemetry-event-relay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ export class TelemetryEventRelay {
this.eventService.on('community-package-deleted', (event) => {
this.communityPackageDeleted(event);
});

this.eventService.on('credentials-created', (event) => {
this.credentialsCreated(event);
});
this.eventService.on('credentials-shared', (event) => {
this.credentialsShared(event);
});
this.eventService.on('credentials-updated', (event) => {
this.credentialsUpdated(event);
});
this.eventService.on('credentials-deleted', (event) => {
this.credentialsDeleted(event);
});
this.eventService.on('ldap-general-sync-finished', (event) => {
this.ldapGeneralSyncFinished(event);
});
Expand Down Expand Up @@ -306,6 +319,56 @@ export class TelemetryEventRelay {
});
}

private credentialsCreated({
user,
credentialType,
credentialId,
projectId,
projectType,
}: Event['credentials-created']) {
void this.telemetry.track('User created credentials', {
user_id: user.id,
credential_type: credentialType,
credential_id: credentialId,
project_id: projectId,
project_type: projectType,
});
}

private credentialsShared({
user,
credentialType,
credentialId,
userIdSharer,
userIdsShareesAdded,
shareesRemoved,
}: Event['credentials-shared']) {
void this.telemetry.track('User updated cred sharing', {
user_id: user.id,
credential_type: credentialType,
credential_id: credentialId,
user_id_sharer: userIdSharer,
user_ids_sharees_added: userIdsShareesAdded,
sharees_removed: shareesRemoved,
});
}

private credentialsUpdated({ user, credentialId, credentialType }: Event['credentials-updated']) {
void this.telemetry.track('User updated credentials', {
user_id: user.id,
credential_type: credentialType,
credential_id: credentialId,
});
}

private credentialsDeleted({ user, credentialId, credentialType }: Event['credentials-deleted']) {
void this.telemetry.track('User deleted credentials', {
user_id: user.id,
credential_type: credentialType,
credential_id: credentialId,
});
}

private ldapGeneralSyncFinished({
type,
succeeded,
Expand Down

0 comments on commit 06a1afd

Please sign in to comment.