1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

[PM-5276] Migrate FolderService to state providers (#7682)

* added state definitionand key definition for folder service

* added data migrations

* created folder to house key definitions

* deleted browser-folder-service and added state provider to the browser

* exposed decrypt function so it can be used by the key definition, updated folder service to use state provider

* removed memory since derived state is now used

* updated test cases

* updated test cases

* updated migrations after merge conflict fix

* added state provider to the folder service constructor

* renamed migration file

* updated comments

* updated comments

* removed service registartion from browser service module and removed unused set and get encrypted folders from state service

* renamed files

* added storage location overides and removed extra methods
This commit is contained in:
SmithThe4th
2024-02-06 14:51:02 -05:00
committed by GitHub
parent f64092cc90
commit 7e00ece092
19 changed files with 473 additions and 241 deletions

View File

@@ -10,6 +10,7 @@ import { OrganizationKeyMigrator } from "./migrations/11-move-org-keys-to-state-
import { MoveEnvironmentStateToProviders } from "./migrations/12-move-environment-state-to-providers";
import { ProviderKeyMigrator } from "./migrations/13-move-provider-keys-to-state-providers";
import { MoveBiometricClientKeyHalfToStateProviders } from "./migrations/14-move-biometric-client-key-half-state-to-providers";
import { FolderMigrator } from "./migrations/15-move-folder-state-to-state-provider";
import { FixPremiumMigrator } from "./migrations/3-fix-premium";
import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked";
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
@@ -20,7 +21,7 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting
import { MinVersionMigrator } from "./migrations/min-version";
export const MIN_VERSION = 2;
export const CURRENT_VERSION = 14;
export const CURRENT_VERSION = 15;
export type MinVersion = typeof MIN_VERSION;
export async function migrate(
@@ -50,7 +51,8 @@ export async function migrate(
.with(OrganizationKeyMigrator, 10, 11)
.with(MoveEnvironmentStateToProviders, 11, 12)
.with(ProviderKeyMigrator, 12, 13)
.with(MoveBiometricClientKeyHalfToStateProviders, 13, CURRENT_VERSION)
.with(MoveBiometricClientKeyHalfToStateProviders, 13, 14)
.with(FolderMigrator, 14, CURRENT_VERSION)
.migrate(migrationHelper);
}

View File

@@ -0,0 +1,163 @@
import { MockProxy, any } from "jest-mock-extended";
import { MigrationHelper } from "../migration-helper";
import { mockMigrationHelper } from "../migration-helper.spec";
import { FolderMigrator } from "./15-move-folder-state-to-state-provider";
function exampleJSON() {
return {
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2"],
"user-1": {
data: {
folders: {
encrypted: {
"folder-id-1": {
id: "folder-id-1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
"folder-id-2": {
id: "folder-id-2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
},
otherStuff: "overStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
data: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
function rollbackJSON() {
return {
"user_user-1_folder_folders": {
"folder-id-1": {
id: "folder-id-1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
"folder-id-2": {
id: "folder-id-2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
"user_user-2_folder_folders": null as any,
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2"],
"user-1": {
data: {
otherStuff: "overStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
data: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
describe("FolderMigrator", () => {
let helper: MockProxy<MigrationHelper>;
let sut: FolderMigrator;
const keyDefinitionLike = {
key: "folders",
stateDefinition: {
name: "folder",
},
};
describe("migrate", () => {
beforeEach(() => {
helper = mockMigrationHelper(exampleJSON(), 14);
sut = new FolderMigrator(14, 15);
});
it("should remove folders from all accounts", async () => {
await sut.migrate(helper);
expect(helper.set).toHaveBeenCalledWith("user-1", {
data: {
otherStuff: "overStuff2",
},
otherStuff: "otherStuff3",
});
});
it("should set folders value for each account", async () => {
await sut.migrate(helper);
expect(helper.setToUser).toHaveBeenCalledWith("user-1", keyDefinitionLike, {
"folder-id-1": {
id: "folder-id-1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
"folder-id-2": {
id: "folder-id-2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
});
});
});
describe("rollback", () => {
beforeEach(() => {
helper = mockMigrationHelper(rollbackJSON(), 14);
sut = new FolderMigrator(14, 15);
});
it.each(["user-1", "user-2"])("should null out new values", async (userId) => {
await sut.rollback(helper);
expect(helper.setToUser).toHaveBeenCalledWith(userId, keyDefinitionLike, null);
});
it("should add explicit value back to accounts", async () => {
await sut.rollback(helper);
expect(helper.set).toHaveBeenCalledWith("user-1", {
data: {
folders: {
encrypted: {
"folder-id-1": {
id: "folder-id-1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
"folder-id-2": {
id: "folder-id-2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
},
otherStuff: "overStuff2",
},
otherStuff: "otherStuff3",
});
});
it("should not try to restore values to missing accounts", async () => {
await sut.rollback(helper);
expect(helper.set).not.toHaveBeenCalledWith("user-3", any());
});
});
});

View File

@@ -0,0 +1,57 @@
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { Migrator } from "../migrator";
type FolderDataType = {
id: string;
name: string;
revisionDate: string;
};
type ExpectedAccountType = {
data?: {
folders?: {
encrypted?: Record<string, FolderDataType>;
};
};
};
const USER_ENCRYPTED_FOLDERS: KeyDefinitionLike = {
key: "folders",
stateDefinition: {
name: "folder",
},
};
export class FolderMigrator extends Migrator<14, 15> {
async migrate(helper: MigrationHelper): Promise<void> {
const accounts = await helper.getAccounts<ExpectedAccountType>();
async function migrateAccount(userId: string, account: ExpectedAccountType): Promise<void> {
const value = account?.data?.folders?.encrypted;
if (value != null) {
await helper.setToUser(userId, USER_ENCRYPTED_FOLDERS, value);
delete account.data.folders;
await helper.set(userId, account);
}
}
await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]);
}
async rollback(helper: MigrationHelper): Promise<void> {
const accounts = await helper.getAccounts<ExpectedAccountType>();
async function rollbackAccount(userId: string, account: ExpectedAccountType): Promise<void> {
const value = await helper.getFromUser(userId, USER_ENCRYPTED_FOLDERS);
if (account) {
account.data = Object.assign(account.data ?? {}, {
folders: {
encrypted: value,
},
});
await helper.set(userId, account);
}
await helper.setToUser(userId, USER_ENCRYPTED_FOLDERS, null);
}
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
}
}