From ee560a5e48cca2eac9b23aeaa04342365c3d9e74 Mon Sep 17 00:00:00 2001 From: gustrb Date: Wed, 16 Oct 2024 16:15:47 -0300 Subject: [PATCH] fix: apply suggestions --- .changeset/plenty-hairs-camp.md | 3 ++- apps/meteor/app/api/server/v1/misc.ts | 2 +- .../ee/server/models/WorkspaceCredentials.ts | 3 +-- .../ee/server/models/raw/WorkspaceCredentials.ts | 14 +++++++------- apps/meteor/server/startup/migrations/v316.ts | 8 ++++---- .../src/models/IWorkspaceCredentialsModel.ts | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.changeset/plenty-hairs-camp.md b/.changeset/plenty-hairs-camp.md index 3e26790fe9fd..e75743f4a863 100644 --- a/.changeset/plenty-hairs-camp.md +++ b/.changeset/plenty-hairs-camp.md @@ -5,4 +5,5 @@ "@rocket.chat/models": major --- -Now, we are not fetching cloud credentials from a cache, by doing so, we are avoiding the use of stale cloud credentials when comunicating with cloud services. We are, also, using a new collection to store the credentials and their scopes. +Adds a new collection to store all the workspace cloud tokens to defer the race condition management to MongoDB instead of having to handle it within the settings cache. +Removes the Cloud_Workspace_Access_Token & Cloud_Workspace_Access_Token_Expires_At settings since they are not going to be used anymore. \ No newline at end of file diff --git a/apps/meteor/app/api/server/v1/misc.ts b/apps/meteor/app/api/server/v1/misc.ts index cf8ece115d36..5cd522d20533 100644 --- a/apps/meteor/app/api/server/v1/misc.ts +++ b/apps/meteor/app/api/server/v1/misc.ts @@ -664,7 +664,7 @@ API.v1.addRoute( const settingsIds: string[] = []; if (this.bodyParams.setDeploymentAs === 'new-workspace') { - await WorkspaceCredentials.unsetCredentialByScope(''); + await WorkspaceCredentials.unsetCredentialByScope(); settingsIds.push( 'Cloud_Service_Agree_PrivacyTerms', 'Cloud_Workspace_Id', diff --git a/apps/meteor/ee/server/models/WorkspaceCredentials.ts b/apps/meteor/ee/server/models/WorkspaceCredentials.ts index d4f5cd2d912d..26b1f015f067 100644 --- a/apps/meteor/ee/server/models/WorkspaceCredentials.ts +++ b/apps/meteor/ee/server/models/WorkspaceCredentials.ts @@ -1,7 +1,6 @@ import { registerModel } from '@rocket.chat/models'; -import { trashCollection } from '../../../server/database/trash'; import { db } from '../../../server/database/utils'; import { WorkspaceCredentialsRaw } from './raw/WorkspaceCredentials'; -registerModel('IWorkspaceCredentialsModel', new WorkspaceCredentialsRaw(db, trashCollection)); +registerModel('IWorkspaceCredentialsModel', new WorkspaceCredentialsRaw(db)); diff --git a/apps/meteor/ee/server/models/raw/WorkspaceCredentials.ts b/apps/meteor/ee/server/models/raw/WorkspaceCredentials.ts index 814bc365a338..f4141967814d 100644 --- a/apps/meteor/ee/server/models/raw/WorkspaceCredentials.ts +++ b/apps/meteor/ee/server/models/raw/WorkspaceCredentials.ts @@ -1,19 +1,19 @@ -import type { RocketChatRecordDeleted, IWorkspaceCredentials } from '@rocket.chat/core-typings'; +import type { IWorkspaceCredentials } from '@rocket.chat/core-typings'; import type { IWorkspaceCredentialsModel } from '@rocket.chat/model-typings'; -import type { Collection, Db, DeleteResult, Filter, IndexDescription, UpdateResult } from 'mongodb'; +import type { Db, DeleteResult, Filter, IndexDescription, UpdateResult } from 'mongodb'; import { BaseRaw } from '../../../../server/models/raw/BaseRaw'; export class WorkspaceCredentialsRaw extends BaseRaw implements IWorkspaceCredentialsModel { - constructor(db: Db, trash?: Collection>) { - super(db, 'workspace_credentials', trash); + constructor(db: Db) { + super(db, 'workspace_credentials'); } protected modelIndexes(): IndexDescription[] { - return [{ key: { _id: 1, scopes: 1, expirationDate: 1, accessToken: 1 }, unique: true }]; + return [{ key: { scopes: 1, expirationDate: 1, accessToken: 1 }, unique: true }]; } - getCredentialByScope(scope: string): Promise { + getCredentialByScope(scope = ''): Promise { const query: Filter = { scopes: { $all: [scope], @@ -24,7 +24,7 @@ export class WorkspaceCredentialsRaw extends BaseRaw impl return this.findOne(query); } - unsetCredentialByScope(scope: string): Promise { + unsetCredentialByScope(scope = ''): Promise { const query: Filter = { scopes: { $all: [scope], diff --git a/apps/meteor/server/startup/migrations/v316.ts b/apps/meteor/server/startup/migrations/v316.ts index 7e69fc80d4a0..c8641b896e77 100644 --- a/apps/meteor/server/startup/migrations/v316.ts +++ b/apps/meteor/server/startup/migrations/v316.ts @@ -6,26 +6,26 @@ addMigration({ version: 316, name: 'Remove Cloud_Workspace_Access_Token and Cloud_Workspace_Access_Token_Expires_At from the settings collection and add to the WorkspaceCredentials collection', async up() { - const workspaceCredentials = await WorkspaceCredentials.getCredentialByScope(''); + const workspaceCredentials = await WorkspaceCredentials.getCredentialByScope(); if (workspaceCredentials) { return; } const accessToken = ((await Settings.getValueById('Cloud_Workspace_Access_Token')) as string) || ''; - const accessTokenExpiresAt = ((await Settings.getValueById('Cloud_Workspace_Access_Token_Expires_At')) as Date) || new Date(0); + const expirationDate = ((await Settings.getValueById('Cloud_Workspace_Access_Token_Expires_At')) as Date) || new Date(0); if (accessToken) { await Settings.removeById('Cloud_Workspace_Access_Token'); } - if (accessTokenExpiresAt) { + if (expirationDate) { await Settings.removeById('Cloud_Workspace_Access_Token_Expires_At'); } await WorkspaceCredentials.updateCredentialByScope({ scope: '', accessToken, - expirationDate: accessTokenExpiresAt, + expirationDate, }); }, }); diff --git a/packages/model-typings/src/models/IWorkspaceCredentialsModel.ts b/packages/model-typings/src/models/IWorkspaceCredentialsModel.ts index 303da0ff1cf9..58b9a8a5049d 100644 --- a/packages/model-typings/src/models/IWorkspaceCredentialsModel.ts +++ b/packages/model-typings/src/models/IWorkspaceCredentialsModel.ts @@ -4,8 +4,8 @@ import type { DeleteResult, UpdateResult } from 'mongodb'; import type { IBaseModel } from './IBaseModel'; export interface IWorkspaceCredentialsModel extends IBaseModel { - getCredentialByScope(scope: string): Promise; - unsetCredentialByScope(scope: string): Promise; + getCredentialByScope(scope?: string): Promise; + unsetCredentialByScope(scope?: string): Promise; updateCredentialByScope(credentials: { scope: string; accessToken: string; expirationDate: Date }): Promise; removeAllCredentials(): Promise; }