Skip to content

Commit

Permalink
fix!: LDAP sync triggers multiple cron jobs in case an invalid sync i…
Browse files Browse the repository at this point in the history
…nterval is provided (#32285)

* fix: use settings' packageValue as a fallback to LDAP sync intervals

* fix: add migration to update the packageValue of LDAP sync interval settings
---------

Co-authored-by: Marcos Spessatto Defendi <[email protected]>
  • Loading branch information
2 people authored and sampaiodiego committed Oct 9, 2024
1 parent 82d1ac2 commit 374979f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sixty-vans-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": major
---

Fixed issue with LDAP sync triggering multiple cron jobs in case an invalid sync interval is provided
6 changes: 5 additions & 1 deletion apps/meteor/ee/server/configuration/ldap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { IImportUser, ILDAPEntry, IUser } from '@rocket.chat/core-typings';
import { cronJobs } from '@rocket.chat/cron';
import { License } from '@rocket.chat/license';
import { Settings } from '@rocket.chat/models';
import { isValidCron } from 'cron-validator';
import { Meteor } from 'meteor/meteor';

import { settings } from '../../../app/settings/server';
Expand Down Expand Up @@ -28,7 +30,9 @@ Meteor.startup(async () => {
}

const settingValue = settings.get<string>(intervalSetting);
const schedule = ldapIntervalValuesToCronMap[settingValue] ?? settingValue;
const schedule =
ldapIntervalValuesToCronMap[settingValue] ??
(isValidCron(settingValue) ? settingValue : ((await Settings.findOneById(intervalSetting))?.packageValue as string));
if (schedule) {
if (schedule !== lastSchedule && (await cronJobs.has(jobName))) {
await cronJobs.remove(jobName);
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"cron": "~1.8.2",
"cron-validator": "^1.3.1",
"css-vars-ponyfill": "^2.4.9",
"csv-parse": "^5.2.0",
"date-fns": "^2.28.0",
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/server/startup/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ import './v304';
import './v305';
import './v306';
import './v307';
import './v308';

export * from './xrun';
31 changes: 31 additions & 0 deletions apps/meteor/server/startup/migrations/v308.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { ISetting } from '@rocket.chat/core-typings';
import { Settings } from '@rocket.chat/models';
import { isValidCron } from 'cron-validator';

import { addMigration } from '../../lib/migrations';

addMigration({
version: 308,
name: 'Update packageValue from LDAP interval settings',
async up() {
const newAvatarSyncPackageValue = '0 0 * * *';
const newAutoLogoutPackageValue = '*/5 * * * *';
const ldapAvatarSyncInterval = await Settings.findOneById<Pick<ISetting, 'value'>>('LDAP_Background_Sync_Avatars_Interval', {
projection: { value: 1 },
});
const ldapAutoLogoutInterval = await Settings.findOneById<Pick<ISetting, 'value'>>('LDAP_Sync_AutoLogout_Interval', {
projection: { value: 1 },
});
const isValidAvatarSyncInterval = ldapAvatarSyncInterval && isValidCron(ldapAvatarSyncInterval.value as string);
const isValidAutoLogoutInterval = ldapAutoLogoutInterval && isValidCron(ldapAutoLogoutInterval.value as string);

await Settings.updateOne(
{ _id: 'LDAP_Background_Sync_Avatars_Interval' },
{ $set: { packageValue: newAvatarSyncPackageValue, ...(!isValidAvatarSyncInterval && { value: newAvatarSyncPackageValue }) } },
);
await Settings.updateOne(
{ _id: 'LDAP_Sync_AutoLogout_Interval' },
{ $set: { packageValue: newAutoLogoutPackageValue, ...(!isValidAutoLogoutInterval && { value: newAutoLogoutPackageValue }) } },
);
},
});
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9722,6 +9722,7 @@ __metadata:
cookie-parser: ^1.4.6
cors: ^2.8.5
cron: ~1.8.2
cron-validator: ^1.3.1
cross-env: ^7.0.3
css-vars-ponyfill: ^2.4.9
csv-parse: ^5.2.0
Expand Down Expand Up @@ -20504,6 +20505,13 @@ __metadata:
languageName: node
linkType: hard

"cron-validator@npm:^1.3.1":
version: 1.3.1
resolution: "cron-validator@npm:1.3.1"
checksum: 82895b417bc35a96c8ad8501d2f236492403ba6e35c1112762e9573939eb15cd80bd0693d5ae95e3a15e15fc6442b1a6e6cc3dd0f740f0a3320b202082aeabec
languageName: node
linkType: hard

"cron@npm:~1.8.2":
version: 1.8.2
resolution: "cron@npm:1.8.2"
Expand Down

0 comments on commit 374979f

Please sign in to comment.