Skip to content

Commit

Permalink
[PM-6199] VaultFilterService keeps decrypted collections in memory af…
Browse files Browse the repository at this point in the history
…ter logout (#8120)

* updated vault filter service to make use of collection service

* removed fix me

* reverted to use reload collections on org vault as collection admin service does not support state management yet

* fixed statement
  • Loading branch information
gbubemismith authored Mar 21, 2024
1 parent d6fa7a4 commit 6dc6aec
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CollectionView } from "@bitwarden/common/src/vault/models/view/collecti
import { FolderView } from "@bitwarden/common/src/vault/models/view/folder.view";
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";

import { CollectionAdminView } from "../../../../core/views/collection-admin.view";
import {
CipherTypeFilter,
CollectionFilter,
Expand All @@ -20,7 +21,6 @@ export abstract class VaultFilterService {
folderTree$: Observable<TreeNode<FolderFilter>>;
collectionTree$: Observable<TreeNode<CollectionFilter>>;
cipherTypeTree$: Observable<TreeNode<CipherTypeFilter>>;
reloadCollections: (collections: CollectionView[]) => void;
getCollectionNodeFromTree: (id: string) => Promise<TreeNode<CollectionFilter>>;
setCollapsedFilterNodes: (collapsedFilterNodes: Set<string>) => Promise<void>;
expandOrgFilter: () => Promise<void>;
Expand All @@ -30,4 +30,6 @@ export abstract class VaultFilterService {
head: CipherTypeFilter,
array: CipherTypeFilter[],
) => Observable<TreeNode<CipherTypeFilter>>;
// TODO: Remove this from org vault when collection admin service adopts state management
reloadCollections?: (collections: CollectionAdminView[]) => void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.servic
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CollectionService } from "@bitwarden/common/vault/abstractions/collection.service";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { CollectionView } from "@bitwarden/common/vault/models/view/collection.view";
Expand All @@ -31,8 +32,10 @@ describe("vault filter service", () => {
let cipherService: MockProxy<CipherService>;
let policyService: MockProxy<PolicyService>;
let i18nService: MockProxy<I18nService>;
let collectionService: MockProxy<CollectionService>;
let organizations: ReplaySubject<Organization[]>;
let folderViews: ReplaySubject<FolderView[]>;
let collectionViews: ReplaySubject<CollectionView[]>;
let stateProvider: FakeStateProvider;

const mockUserId = Utils.newGuid() as UserId;
Expand All @@ -48,12 +51,15 @@ describe("vault filter service", () => {
accountService = mockAccountServiceWith(mockUserId);
stateProvider = new FakeStateProvider(accountService);
i18nService.collator = new Intl.Collator("en-US");
collectionService = mock<CollectionService>();

organizations = new ReplaySubject<Organization[]>(1);
folderViews = new ReplaySubject<FolderView[]>(1);
collectionViews = new ReplaySubject<CollectionView[]>(1);

organizationService.memberOrganizations$ = organizations;
folderService.folderViews$ = folderViews;
collectionService.decryptedCollections$ = collectionViews;

vaultFilterService = new VaultFilterService(
organizationService,
Expand All @@ -62,6 +68,7 @@ describe("vault filter service", () => {
policyService,
i18nService,
stateProvider,
collectionService,
);
collapsedGroupingsState = stateProvider.activeUser.getFake(COLLAPSED_GROUPINGS);
});
Expand Down Expand Up @@ -196,9 +203,7 @@ describe("vault filter service", () => {
createCollectionView("1", "collection 1", "org test id"),
createCollectionView("2", "collection 2", "non matching org id"),
];
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vaultFilterService.reloadCollections(storedCollections);
collectionViews.next(storedCollections);

await expect(firstValueFrom(vaultFilterService.filteredCollections$)).resolves.toEqual([
createCollectionView("1", "collection 1", "org test id"),
Expand All @@ -213,9 +218,7 @@ describe("vault filter service", () => {
createCollectionView("id-2", "Collection 1/Collection 2", "org test id"),
createCollectionView("id-3", "Collection 1/Collection 3", "org test id"),
];
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vaultFilterService.reloadCollections(storedCollections);
collectionViews.next(storedCollections);

const result = await firstValueFrom(vaultFilterService.collectionTree$);

Expand All @@ -228,9 +231,7 @@ describe("vault filter service", () => {
createCollectionView("id-1", "Collection 1", "org test id"),
createCollectionView("id-3", "Collection 1/Collection 2/Collection 3", "org test id"),
];
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vaultFilterService.reloadCollections(storedCollections);
collectionViews.next(storedCollections);

const result = await firstValueFrom(vaultFilterService.collectionTree$);

Expand All @@ -246,9 +247,7 @@ describe("vault filter service", () => {
createCollectionView("id-3", "Collection 1/Collection 2/Collection 3", "org test id"),
createCollectionView("id-4", "Collection 1/Collection 4", "org test id"),
];
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vaultFilterService.reloadCollections(storedCollections);
collectionViews.next(storedCollections);

const result = await firstValueFrom(vaultFilterService.collectionTree$);

Expand All @@ -266,9 +265,7 @@ describe("vault filter service", () => {
createCollectionView("id-1", "Collection 1", "org test id"),
createCollectionView("id-3", "Collection 1/Collection 2/Collection 3", "org test id"),
];
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vaultFilterService.reloadCollections(storedCollections);
collectionViews.next(storedCollections);

const result = await firstValueFrom(vaultFilterService.collectionTree$);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Injectable } from "@angular/core";
import {
BehaviorSubject,
combineLatest,
combineLatestWith,
firstValueFrom,
map,
Observable,
of,
ReplaySubject,
switchMap,
} from "rxjs";

Expand All @@ -18,6 +16,7 @@ import { Organization } from "@bitwarden/common/admin-console/models/domain/orga
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { ActiveUserState, StateProvider } from "@bitwarden/common/platform/state";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CollectionService } from "@bitwarden/common/vault/abstractions/collection.service";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { CipherType } from "@bitwarden/common/vault/enums";
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
Expand Down Expand Up @@ -57,17 +56,14 @@ export class VaultFilterService implements VaultFilterServiceAbstraction {
map((folders) => this.buildFolderTree(folders)),
);

// TODO: Remove once collections is refactored with observables
// replace with collection service observable
private collectionViews$ = new ReplaySubject<CollectionView[]>(1);
filteredCollections$: Observable<CollectionView[]> = combineLatest([
this.collectionViews$,
this._organizationFilter,
]).pipe(
switchMap(([collections, org]) => {
return this.filterCollections(collections, org);
}),
);
filteredCollections$: Observable<CollectionView[]> =
this.collectionService.decryptedCollections$.pipe(
combineLatestWith(this._organizationFilter),
switchMap(([collections, org]) => {
return this.filterCollections(collections, org);
}),
);

collectionTree$: Observable<TreeNode<CollectionFilter>> = this.filteredCollections$.pipe(
map((collections) => this.buildCollectionTree(collections)),
);
Expand All @@ -87,12 +83,9 @@ export class VaultFilterService implements VaultFilterServiceAbstraction {
protected policyService: PolicyService,
protected i18nService: I18nService,
protected stateProvider: StateProvider,
protected collectionService: CollectionService,
) {}

async reloadCollections(collections: CollectionView[]) {
this.collectionViews$.next(collections);
}

async getCollectionNodeFromTree(id: string) {
const collections = await firstValueFrom(this.collectionTree$);
return ServiceUtils.getTreeNodeObject(collections, id) as TreeNode<CollectionFilter>;
Expand Down
4 changes: 0 additions & 4 deletions apps/web/src/app/vault/individual-vault/vault.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,6 @@ export class VaultComponent implements OnInit, OnDestroy {
(filter.organizationId === undefined || filter.organizationId === Unassigned);
this.isEmpty = collections?.length === 0 && ciphers?.length === 0;

// This is a temporary fix to avoid double fetching collections.
// TODO: Remove when implementing new VVR menu
this.vaultFilterService.reloadCollections(allCollections);

this.performingInitialLoad = false;
this.refreshing = false;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { PolicyService } from "@bitwarden/common/admin-console/abstractions/poli
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { StateProvider } from "@bitwarden/common/platform/state";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CollectionService } from "@bitwarden/common/vault/abstractions/collection.service";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";

import { CollectionAdminView } from "../../../vault/core/views/collection-admin.view";
import { CollectionAdminService } from "../../core/collection-admin.service";
import { VaultFilterService as BaseVaultFilterService } from "../../individual-vault/vault-filter/services/vault-filter.service";
import { CollectionFilter } from "../../individual-vault/vault-filter/shared/models/vault-filter.type";

Expand All @@ -32,7 +32,7 @@ export class VaultFilterService extends BaseVaultFilterService implements OnDest
policyService: PolicyService,
i18nService: I18nService,
stateProvider: StateProvider,
protected collectionAdminService: CollectionAdminService,
collectionService: CollectionService,
) {
super(
organizationService,
Expand All @@ -41,6 +41,7 @@ export class VaultFilterService extends BaseVaultFilterService implements OnDest
policyService,
i18nService,
stateProvider,
collectionService,
);
}

Expand Down
2 changes: 2 additions & 0 deletions libs/common/src/vault/abstractions/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { TreeNode } from "../models/domain/tree-node";
import { CollectionView } from "../models/view/collection.view";

export abstract class CollectionService {
decryptedCollections$: Observable<CollectionView[]>;

clearActiveUserCache: () => Promise<void>;
encrypt: (model: CollectionView) => Promise<Collection>;
decryptedCollectionViews$: (ids: CollectionId[]) => Observable<CollectionView[]>;
Expand Down

0 comments on commit 6dc6aec

Please sign in to comment.