1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00

[PM-5269] Key Connector state migration (#8327)

* key connector migration initial

* migrator complete

* fix dependencies

* finalized tests

* fix deps and sync main

* clean up definition file

* fixing tests

* fixed tests

* fixing CLI, Browser, Desktop builds

* fixed factory options

* reverting exports

* implemented UserKeyDefinition clearOn

* Update KeyConnector MIgration

* updated migrator and tests to match profile object

* removed unused service and updated clear

* dep fix

* dep fixes

* clear usesKeyConnector on logout
This commit is contained in:
Ike
2024-03-28 09:50:24 -07:00
committed by GitHub
parent df058ba399
commit 3d19e3489c
17 changed files with 691 additions and 72 deletions

View File

@@ -46,6 +46,7 @@ import { MoveDesktopSettingsMigrator } from "./migrations/47-move-desktop-settin
import { MoveDdgToStateProviderMigrator } from "./migrations/48-move-ddg-to-state-provider";
import { AccountServerConfigMigrator } from "./migrations/49-move-account-server-configs";
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
import { KeyConnectorMigrator } from "./migrations/50-move-key-connector-to-state-provider";
import { RemoveLegacyEtmKeyMigrator } from "./migrations/6-remove-legacy-etm-key";
import { MoveBiometricAutoPromptToAccount } from "./migrations/7-move-biometric-auto-prompt-to-account";
import { MoveStateVersionMigrator } from "./migrations/8-move-state-version";
@@ -53,7 +54,8 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting
import { MinVersionMigrator } from "./migrations/min-version";
export const MIN_VERSION = 3;
export const CURRENT_VERSION = 49;
export const CURRENT_VERSION = 50;
export type MinVersion = typeof MIN_VERSION;
export function createMigrationBuilder() {
@@ -104,7 +106,8 @@ export function createMigrationBuilder() {
.with(DeleteBiometricPromptCancelledData, 45, 46)
.with(MoveDesktopSettingsMigrator, 46, 47)
.with(MoveDdgToStateProviderMigrator, 47, 48)
.with(AccountServerConfigMigrator, 48, CURRENT_VERSION);
.with(AccountServerConfigMigrator, 48, 49)
.with(KeyConnectorMigrator, 49, CURRENT_VERSION);
}
export async function currentVersion(

View File

@@ -0,0 +1,174 @@
import { MockProxy } from "jest-mock-extended";
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { mockMigrationHelper } from "../migration-helper.spec";
import { KeyConnectorMigrator } from "./50-move-key-connector-to-state-provider";
function exampleJSON() {
return {
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["FirstAccount", "SecondAccount", "ThirdAccount"],
FirstAccount: {
profile: {
usesKeyConnector: true,
convertAccountToKeyConnector: false,
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
},
SecondAccount: {
profile: {
usesKeyConnector: true,
convertAccountToKeyConnector: true,
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
function rollbackJSON() {
return {
user_FirstAccount_keyConnector_usesKeyConnector: true,
user_FirstAccount_keyConnector_convertAccountToKeyConnector: false,
user_SecondAccount_keyConnector_usesKeyConnector: true,
user_SecondAccount_keyConnector_convertAccountToKeyConnector: true,
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["FirstAccount", "SecondAccount", "ThirdAccount"],
FirstAccount: {
profile: {
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
},
SecondAccount: {
profile: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
const usesKeyConnectorKeyDefinition: KeyDefinitionLike = {
key: "usesKeyConnector",
stateDefinition: {
name: "keyConnector",
},
};
const convertAccountToKeyConnectorKeyDefinition: KeyDefinitionLike = {
key: "convertAccountToKeyConnector",
stateDefinition: {
name: "keyConnector",
},
};
describe("KeyConnectorMigrator", () => {
let helper: MockProxy<MigrationHelper>;
let sut: KeyConnectorMigrator;
describe("migrate", () => {
beforeEach(() => {
helper = mockMigrationHelper(exampleJSON(), 50);
sut = new KeyConnectorMigrator(49, 50);
});
it("should remove usesKeyConnector and convertAccountToKeyConnector from Profile", async () => {
await sut.migrate(helper);
// Set is called 2 times even though there are 3 accounts. Since the target properties don't exist in ThirdAccount, they are not set.
expect(helper.set).toHaveBeenCalledTimes(2);
expect(helper.set).toHaveBeenCalledWith("FirstAccount", {
profile: {
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
});
expect(helper.setToUser).toHaveBeenCalledWith(
"FirstAccount",
usesKeyConnectorKeyDefinition,
true,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"FirstAccount",
convertAccountToKeyConnectorKeyDefinition,
false,
);
expect(helper.set).toHaveBeenCalledWith("SecondAccount", {
profile: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
});
expect(helper.setToUser).toHaveBeenCalledWith(
"SecondAccount",
usesKeyConnectorKeyDefinition,
true,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"SecondAccount",
convertAccountToKeyConnectorKeyDefinition,
true,
);
expect(helper.setToUser).not.toHaveBeenCalledWith("ThirdAccount");
});
});
describe("rollback", () => {
beforeEach(() => {
helper = mockMigrationHelper(rollbackJSON(), 50);
sut = new KeyConnectorMigrator(49, 50);
});
it("should null out new usesKeyConnector global value", async () => {
await sut.rollback(helper);
expect(helper.setToUser).toHaveBeenCalledTimes(4);
expect(helper.set).toHaveBeenCalledTimes(2);
expect(helper.setToUser).toHaveBeenCalledWith(
"FirstAccount",
usesKeyConnectorKeyDefinition,
null,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"FirstAccount",
convertAccountToKeyConnectorKeyDefinition,
null,
);
expect(helper.set).toHaveBeenCalledWith("FirstAccount", {
profile: {
usesKeyConnector: true,
convertAccountToKeyConnector: false,
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
});
expect(helper.setToUser).toHaveBeenCalledWith(
"SecondAccount",
usesKeyConnectorKeyDefinition,
null,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"SecondAccount",
convertAccountToKeyConnectorKeyDefinition,
null,
);
expect(helper.set).toHaveBeenCalledWith("SecondAccount", {
profile: {
usesKeyConnector: true,
convertAccountToKeyConnector: true,
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
});
expect(helper.setToUser).not.toHaveBeenCalledWith("ThirdAccount");
expect(helper.set).not.toHaveBeenCalledWith("ThirdAccount");
});
});
});

View File

@@ -0,0 +1,78 @@
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { Migrator } from "../migrator";
type ExpectedAccountType = {
profile?: {
usesKeyConnector?: boolean;
convertAccountToKeyConnector?: boolean;
};
};
const usesKeyConnectorKeyDefinition: KeyDefinitionLike = {
key: "usesKeyConnector",
stateDefinition: {
name: "keyConnector",
},
};
const convertAccountToKeyConnectorKeyDefinition: KeyDefinitionLike = {
key: "convertAccountToKeyConnector",
stateDefinition: {
name: "keyConnector",
},
};
export class KeyConnectorMigrator extends Migrator<49, 50> {
async migrate(helper: MigrationHelper): Promise<void> {
const accounts = await helper.getAccounts<ExpectedAccountType>();
async function migrateAccount(userId: string, account: ExpectedAccountType): Promise<void> {
const usesKeyConnector = account?.profile?.usesKeyConnector;
const convertAccountToKeyConnector = account?.profile?.convertAccountToKeyConnector;
if (usesKeyConnector == null && convertAccountToKeyConnector == null) {
return;
}
if (usesKeyConnector != null) {
await helper.setToUser(userId, usesKeyConnectorKeyDefinition, usesKeyConnector);
delete account.profile.usesKeyConnector;
}
if (convertAccountToKeyConnector != null) {
await helper.setToUser(
userId,
convertAccountToKeyConnectorKeyDefinition,
convertAccountToKeyConnector,
);
delete account.profile.convertAccountToKeyConnector;
}
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 usesKeyConnector: boolean = await helper.getFromUser(
userId,
usesKeyConnectorKeyDefinition,
);
const convertAccountToKeyConnector: boolean = await helper.getFromUser(
userId,
convertAccountToKeyConnectorKeyDefinition,
);
if (usesKeyConnector == null && convertAccountToKeyConnector == null) {
return;
}
if (usesKeyConnector != null) {
account.profile.usesKeyConnector = usesKeyConnector;
await helper.setToUser(userId, usesKeyConnectorKeyDefinition, null);
}
if (convertAccountToKeyConnector != null) {
account.profile.convertAccountToKeyConnector = convertAccountToKeyConnector;
await helper.setToUser(userId, convertAccountToKeyConnectorKeyDefinition, null);
}
await helper.set(userId, account);
}
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
}
}