refactor(core): Decouple credentials from internal hooks (no-changelog) (#10162)
This commit is contained in:
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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> {
|
||||
@@ -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;
|
||||
@@ -89,6 +73,23 @@ 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(
|
||||
@@ -96,15 +97,8 @@ export async function removeCredential(
|
||||
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,
|
||||
});
|
||||
|
||||
@@ -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';
|
||||
@@ -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,
|
||||
@@ -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);
|
||||
@@ -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,
|
||||
});
|
||||
@@ -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,
|
||||
});
|
||||
@@ -335,22 +318,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,
|
||||
});
|
||||
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user