Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): Decouple LDAP from internal hooks (no-changelog) #10157

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions packages/cli/src/InternalHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,40 +618,6 @@ export class InternalHooks {
});
}

async onLdapSyncFinished(data: {
type: string;
succeeded: boolean;
users_synced: number;
error: string;
}): Promise<void> {
return await this.telemetry.track('Ldap general sync finished', data);
}

async onUserUpdatedLdapSettings(data: {
user_id: string;
loginIdAttribute: string;
firstNameAttribute: string;
lastNameAttribute: string;
emailAttribute: string;
ldapIdAttribute: string;
searchPageSize: number;
searchTimeout: number;
synchronizationEnabled: boolean;
synchronizationInterval: number;
loginLabel: string;
loginEnabled: boolean;
}): Promise<void> {
return await this.telemetry.track('Ldap general sync finished', data);
}

async onLdapLoginSyncFailed(data: { error: string }): Promise<void> {
return await this.telemetry.track('Ldap login sync failed', data);
}

async userLoginFailedDueToLdapDisabled(data: { user_id: string }): Promise<void> {
return await this.telemetry.track('User login failed since ldap disabled', data);
}

/*
* Execution Statistics
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/Ldap/ldap.controller.ee.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import pick from 'lodash/pick';
import { Get, Post, Put, RestController, GlobalScope } from '@/decorators';
import { InternalHooks } from '@/InternalHooks';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';

import { NON_SENSIBLE_LDAP_CONFIG_PROPERTIES } from './constants';
import { getLdapSynchronizations } from './helpers.ee';
import { LdapConfiguration } from './types';
import { LdapService } from './ldap.service.ee';
import { EventService } from '@/eventbus/event.service';

@RestController('/ldap')
export class LdapController {
constructor(
private readonly internalHooks: InternalHooks,
private readonly ldapService: LdapService,
private readonly eventService: EventService,
) {}

@Get('/config')
Expand Down Expand Up @@ -42,8 +42,8 @@ export class LdapController {

const data = await this.ldapService.loadConfig();

void this.internalHooks.onUserUpdatedLdapSettings({
user_id: req.user.id,
this.eventService.emit('ldap-settings-updated', {
userId: req.user.id,
...pick(data, NON_SENSIBLE_LDAP_CONFIG_PROPERTIES),
});

Expand Down
12 changes: 5 additions & 7 deletions packages/cli/src/Ldap/ldap.service.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import config from '@/config';
import type { User } from '@db/entities/User';
import type { RunningMode, SyncStatus } from '@db/entities/AuthProviderSyncHistory';
import { SettingsRepository } from '@db/repositories/settings.repository';
import { InternalHooks } from '@/InternalHooks';
import { Logger } from '@/Logger';

import { BadRequestError } from '@/errors/response-errors/bad-request.error';
Expand Down Expand Up @@ -45,6 +44,7 @@ import {
LDAP_LOGIN_ENABLED,
LDAP_LOGIN_LABEL,
} from './constants';
import { EventService } from '@/eventbus/event.service';

@Service()
export class LdapService {
Expand All @@ -56,9 +56,9 @@ export class LdapService {

constructor(
private readonly logger: Logger,
private readonly internalHooks: InternalHooks,
private readonly settingsRepository: SettingsRepository,
private readonly cipher: Cipher,
private readonly eventService: EventService,
) {}

async init() {
Expand Down Expand Up @@ -257,9 +257,7 @@ export class LdapService {
);
} catch (e) {
if (e instanceof Error) {
void this.internalHooks.onLdapLoginSyncFailed({
error: e.message,
});
this.eventService.emit('ldap-login-sync-failed', { error: e.message });
this.logger.error('LDAP - Error during search', { message: e.message });
}
return undefined;
Expand Down Expand Up @@ -383,10 +381,10 @@ export class LdapService {
error: errorMessage,
});

void this.internalHooks.onLdapSyncFinished({
this.eventService.emit('ldap-general-sync-finished', {
type: !this.syncTimer ? 'scheduled' : `manual_${mode}`,
succeeded: true,
users_synced: usersToCreate.length + usersToUpdate.length + usersToDisable.length,
usersSynced: usersToCreate.length + usersToUpdate.length + usersToDisable.length,
error: errorMessage,
});

Expand Down
6 changes: 2 additions & 4 deletions packages/cli/src/auth/methods/email.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { User } from '@db/entities/User';
import { PasswordUtility } from '@/services/password.utility';
import { Container } from 'typedi';
import { InternalHooks } from '@/InternalHooks';
import { isLdapLoginEnabled } from '@/Ldap/helpers.ee';
import { UserRepository } from '@db/repositories/user.repository';
import { AuthError } from '@/errors/response-errors/auth.error';
import { EventService } from '@/eventbus/event.service';

export const handleEmailLogin = async (
email: string,
Expand All @@ -23,9 +23,7 @@ export const handleEmailLogin = async (
// so suggest to reset the password to gain access to the instance.
const ldapIdentity = user?.authIdentities?.find((i) => i.providerType === 'ldap');
if (user && ldapIdentity && !isLdapLoginEnabled()) {
void Container.get(InternalHooks).userLoginFailedDueToLdapDisabled({
user_id: user.id,
});
Container.get(EventService).emit('login-failed-due-to-ldap-disabled', { userId: user.id });

throw new AuthError('Reset your password to gain access to the instance.');
}
Expand Down
30 changes: 30 additions & 0 deletions packages/cli/src/eventbus/event.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,36 @@ export type Event = {
errorMessage?: string;
};

'ldap-general-sync-finished': {
type: string;
succeeded: boolean;
usersSynced: number;
error: string;
};

'ldap-settings-updated': {
userId: string;
loginIdAttribute: string;
firstNameAttribute: string;
lastNameAttribute: string;
emailAttribute: string;
ldapIdAttribute: string;
searchPageSize: number;
searchTimeout: number;
synchronizationEnabled: boolean;
synchronizationInterval: number;
loginLabel: string;
loginEnabled: boolean;
};

'ldap-login-sync-failed': {
error: string;
};

'login-failed-due-to-ldap-disabled': {
userId: string;
};

/**
* Events listened to by more than one relay
*/
Expand Down
64 changes: 64 additions & 0 deletions packages/cli/src/telemetry/telemetry-event-relay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ export class TelemetryEventRelay {
this.eventService.on('community-package-deleted', (event) => {
this.communityPackageDeleted(event);
});
this.eventService.on('ldap-general-sync-finished', (event) => {
this.ldapGeneralSyncFinished(event);
});
this.eventService.on('ldap-settings-updated', (event) => {
this.ldapSettingsUpdated(event);
});
this.eventService.on('ldap-login-sync-failed', (event) => {
this.ldapLoginSyncFailed(event);
});
this.eventService.on('login-failed-due-to-ldap-disabled', (event) => {
this.loginFailedDueToLdapDisabled(event);
});
}

private teamProjectUpdated({ userId, role, members, projectId }: Event['team-project-updated']) {
Expand Down Expand Up @@ -297,4 +309,56 @@ export class TelemetryEventRelay {
package_author_email: packageAuthorEmail,
});
}

private ldapGeneralSyncFinished({
type,
succeeded,
usersSynced,
error,
}: Event['ldap-general-sync-finished']) {
void this.telemetry.track('Ldap general sync finished', {
type,
succeeded,
users_synced: usersSynced,
error,
});
}

private ldapSettingsUpdated({
userId,
loginIdAttribute,
firstNameAttribute,
lastNameAttribute,
emailAttribute,
ldapIdAttribute,
searchPageSize,
searchTimeout,
synchronizationEnabled,
synchronizationInterval,
loginLabel,
loginEnabled,
}: Event['ldap-settings-updated']) {
void this.telemetry.track('User Updated Ldap settings', {
user_id: userId,
loginIdAttribute,
firstNameAttribute,
lastNameAttribute,
emailAttribute,
ldapIdAttribute,
searchPageSize,
searchTimeout,
synchronizationEnabled,
synchronizationInterval,
loginLabel,
loginEnabled,
});
}

private ldapLoginSyncFailed({ error }: Event['ldap-login-sync-failed']) {
void this.telemetry.track('Ldap login sync failed', { error });
}

private loginFailedDueToLdapDisabled({ userId }: Event['login-failed-due-to-ldap-disabled']) {
void this.telemetry.track('User login failed since ldap disabled', { user_ud: userId });
}
}
Loading