Skip to content

Commit

Permalink
fix: apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustrb authored and ggazzo committed Oct 17, 2024
1 parent a4303a7 commit ee560a5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .changeset/plenty-hairs-camp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/v1/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions apps/meteor/ee/server/models/WorkspaceCredentials.ts
Original file line number Diff line number Diff line change
@@ -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));
14 changes: 7 additions & 7 deletions apps/meteor/ee/server/models/raw/WorkspaceCredentials.ts
Original file line number Diff line number Diff line change
@@ -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<IWorkspaceCredentials> implements IWorkspaceCredentialsModel {
constructor(db: Db, trash?: Collection<RocketChatRecordDeleted<IWorkspaceCredentials>>) {
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<IWorkspaceCredentials | null> {
getCredentialByScope(scope = ''): Promise<IWorkspaceCredentials | null> {
const query: Filter<IWorkspaceCredentials> = {
scopes: {
$all: [scope],
Expand All @@ -24,7 +24,7 @@ export class WorkspaceCredentialsRaw extends BaseRaw<IWorkspaceCredentials> impl
return this.findOne(query);
}

unsetCredentialByScope(scope: string): Promise<DeleteResult> {
unsetCredentialByScope(scope = ''): Promise<DeleteResult> {
const query: Filter<IWorkspaceCredentials> = {
scopes: {
$all: [scope],
Expand Down
8 changes: 4 additions & 4 deletions apps/meteor/server/startup/migrations/v316.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { DeleteResult, UpdateResult } from 'mongodb';
import type { IBaseModel } from './IBaseModel';

export interface IWorkspaceCredentialsModel extends IBaseModel<IWorkspaceCredentials> {
getCredentialByScope(scope: string): Promise<IWorkspaceCredentials | null>;
unsetCredentialByScope(scope: string): Promise<DeleteResult>;
getCredentialByScope(scope?: string): Promise<IWorkspaceCredentials | null>;
unsetCredentialByScope(scope?: string): Promise<DeleteResult>;
updateCredentialByScope(credentials: { scope: string; accessToken: string; expirationDate: Date }): Promise<UpdateResult>;
removeAllCredentials(): Promise<DeleteResult>;
}

0 comments on commit ee560a5

Please sign in to comment.