Skip to content

Commit

Permalink
fix: Supported Versions misbehaving (#32631)
Browse files Browse the repository at this point in the history
Co-authored-by: Guilherme Gazzo <[email protected]>
  • Loading branch information
dionisio-bot[bot] and ggazzo authored Jun 19, 2024
1 parent 65cc99b commit 1f0705e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-shoes-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes the supported versions problem, where in most cases the data chosen was the oldest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SignedSupportedVersions } from '@rocket.chat/server-cloud-communic

export const supportedVersionsChooseLatest = async (...tokens: (SignedSupportedVersions | undefined)[]) => {
const [token] = (tokens.filter((r) => r?.timestamp != null) as SignedSupportedVersions[]).sort((a, b) => {
return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime();
return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();
});

return token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const cacheValueInSettings = <T extends SettingValue>(
reset: () => Promise<T>;
} => {
const reset = async () => {
SystemLogger.debug(`Resetting cached value ${key} in settings`);
const value = await fn();

await Settings.updateValueById(key, value);
Expand Down Expand Up @@ -134,6 +135,31 @@ const getSupportedVersionsToken = async () => {
(response.success && response.result) || undefined,
);

SystemLogger.debug({
msg: 'Supported versions',
supportedVersionsFromBuild: supportedVersionsFromBuild.timestamp,
versionsFromLicense: versionsFromLicense?.supportedVersions?.timestamp,
response: response.success && response.result?.timestamp,
});

switch (supportedVersions) {
case supportedVersionsFromBuild:
SystemLogger.info({
msg: 'Using supported versions from build',
});
break;
case versionsFromLicense?.supportedVersions:
SystemLogger.info({
msg: 'Using supported versions from license',
});
break;
case response.success && response.result:
SystemLogger.info({
msg: 'Using supported versions from cloud',
});
break;
}

await buildVersionUpdateMessage(supportedVersions?.versions);

return supportedVersions?.signed;
Expand Down
5 changes: 4 additions & 1 deletion apps/meteor/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const config: Config = {
{
displayName: 'server',
testEnvironment: 'node',
testMatch: ['<rootDir>/ee/app/authorization/server/validateUserRoles.spec.ts'],
testMatch: [
'<rootDir>/ee/app/authorization/server/validateUserRoles.spec.ts',
'<rootDir>/app/cloud/server/functions/supportedVersionsToken/**.spec.ts',
],
transformIgnorePatterns: ['!/node_modules/jose'],
errorOnDeprecated: true,

Expand Down
23 changes: 18 additions & 5 deletions apps/meteor/packages/rocketchat-version/plugin/compile-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class VersionCompiler {
function handleError(err) {
console.error(err);
// TODO remove this when we are ready to fail
// if (process.env.NODE_ENV !== 'development') {
// reject(err);
// return;
// }
if (process.env.NODE_ENV !== 'development') {
reject(err);
return;
}
resolve({});
}

Expand All @@ -34,11 +34,24 @@ class VersionCompiler {
response.on('data', function (chunk) {
data += chunk;
});
response.on('end', function () {
response.on('end', async function () {
const supportedVersions = JSON.parse(data);
if (!supportedVersions?.signed) {
return handleError(new Error(`Invalid supportedVersions result:\n URL: ${url} \n RESULT: ${data}`));
}

// check if timestamp is inside 1 hour within build
if (Math.abs(new Date().getTime() - new Date(supportedVersions.timestamp).getTime()) > 1000 * 60 * 60) {
return handleError(new Error(`Invalid supportedVersions timestamp:\n URL: ${url} \n RESULT: ${data}`));
}

for await (const version of supportedVersions.versions) {
// check if expiration is after the first rocket.chat release
if (new Date(version.expiration) < new Date('2019-04-01T00:00:00.000Z')) {
return handleError(new Error(`Invalid supportedVersions expiration:\n URL: ${url} \n RESULT: ${data}`));
}
}

resolve(supportedVersions);
});
response.on('error', function (err) {
Expand Down

0 comments on commit 1f0705e

Please sign in to comment.