diff --git a/.changeset/sixty-vans-grab.md b/.changeset/sixty-vans-grab.md new file mode 100644 index 000000000000..97b33b207a7f --- /dev/null +++ b/.changeset/sixty-vans-grab.md @@ -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 diff --git a/apps/meteor/ee/server/configuration/ldap.ts b/apps/meteor/ee/server/configuration/ldap.ts index 27c47fdec408..055401f40b05 100644 --- a/apps/meteor/ee/server/configuration/ldap.ts +++ b/apps/meteor/ee/server/configuration/ldap.ts @@ -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'; @@ -28,7 +30,9 @@ Meteor.startup(async () => { } const settingValue = settings.get(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); diff --git a/apps/meteor/package.json b/apps/meteor/package.json index 14786cc0bd07..4ef81ecb6d5c 100644 --- a/apps/meteor/package.json +++ b/apps/meteor/package.json @@ -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", diff --git a/apps/meteor/server/startup/migrations/index.ts b/apps/meteor/server/startup/migrations/index.ts index ca749c3c2086..71ef63582e5b 100644 --- a/apps/meteor/server/startup/migrations/index.ts +++ b/apps/meteor/server/startup/migrations/index.ts @@ -40,5 +40,6 @@ import './v304'; import './v305'; import './v306'; import './v307'; +import './v308'; export * from './xrun'; diff --git a/apps/meteor/server/startup/migrations/v308.ts b/apps/meteor/server/startup/migrations/v308.ts new file mode 100644 index 000000000000..fc27fdd31868 --- /dev/null +++ b/apps/meteor/server/startup/migrations/v308.ts @@ -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>('LDAP_Background_Sync_Avatars_Interval', { + projection: { value: 1 }, + }); + const ldapAutoLogoutInterval = await Settings.findOneById>('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 }) } }, + ); + }, +}); diff --git a/yarn.lock b/yarn.lock index 7f876adeb1d7..7f181db83005 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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"