1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-23 11:43:46 +00:00

[PM-328] Move exporter to tools (#5070)

* Create and register new libs/exporter

Create package.json
Create tsconfig
Create jest.config
Extend shared and root tsconfig and jest.configs
Register with eslint

* Migrate exportService to libs/exporter

Move exportService (abstraction and impl) into libs/exporter
Refactored exportService to be split into vault-export and event-export
Created barrel-files for both exports
Moved export.service.spec.ts into vault-export
Created an export-helper, which helps build the filename (extract method refactor from ExportService)

* Move components in libs/angular into tools-subfolder

Moved components
Updated imports in jslib-services.module and jslib.module

* Register libs/exporter with browser and fix imports

Move export.component into tools-subfolder

* Register libs/exporter with cli and fix imports

Move export.command into tools-subfolder

* Register libs/exporter with desktop and fix imports

Move export.component into tools-subfolder

* Move export models to libs/exporter

* Update web imports

* Update package-lock.json

* Move export models back as it would create circular dependency

Reponse models in common rely on export models which are in libs/exporter, which relies on common

* Fix up web for event-export

* Update CODEOWNERS

* Add export-models to team-tools-dev

* Simplify domain import

* Moving EventExport into web
This commit is contained in:
Daniel James Smith
2023-04-19 11:30:46 +02:00
committed by GitHub
parent 830af7b06d
commit 192bb5a7b3
53 changed files with 266 additions and 133 deletions

View File

@@ -3,7 +3,6 @@ import { NgModule } from "@angular/core";
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { CalloutComponent } from "./components/callout.component";
import { ExportScopeCalloutComponent } from "./components/export-scope-callout.component";
import { BitwardenToastModule } from "./components/toastr.component";
import { A11yInvalidDirective } from "./directives/a11y-invalid.directive";
import { A11yTitleDirective } from "./directives/a11y-title.directive";
@@ -28,6 +27,7 @@ import { SearchPipe } from "./pipes/search.pipe";
import { UserNamePipe } from "./pipes/user-name.pipe";
import { UserTypePipe } from "./pipes/user-type.pipe";
import { PasswordStrengthComponent } from "./shared/components/password-strength/password-strength.component";
import { ExportScopeCalloutComponent } from "./tools/export/components/export-scope-callout.component";
import { IconComponent } from "./vault/components/icon.component";
@NgModule({

View File

@@ -14,7 +14,6 @@ import { EncryptService } from "@bitwarden/common/abstractions/encrypt.service";
import { EnvironmentService as EnvironmentServiceAbstraction } from "@bitwarden/common/abstractions/environment.service";
import { EventCollectionService as EventCollectionServiceAbstraction } from "@bitwarden/common/abstractions/event/event-collection.service";
import { EventUploadService as EventUploadServiceAbstraction } from "@bitwarden/common/abstractions/event/event-upload.service";
import { ExportService as ExportServiceAbstraction } from "@bitwarden/common/abstractions/export.service";
import { FileUploadService as FileUploadServiceAbstraction } from "@bitwarden/common/abstractions/file-upload/file-upload.service";
import { FormValidationErrorsService as FormValidationErrorsServiceAbstraction } from "@bitwarden/common/abstractions/formValidationErrors.service";
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/abstractions/i18n.service";
@@ -94,7 +93,6 @@ import { MultithreadEncryptServiceImplementation } from "@bitwarden/common/servi
import { EnvironmentService } from "@bitwarden/common/services/environment.service";
import { EventCollectionService } from "@bitwarden/common/services/event/event-collection.service";
import { EventUploadService } from "@bitwarden/common/services/event/event-upload.service";
import { ExportService } from "@bitwarden/common/services/export.service";
import { FileUploadService } from "@bitwarden/common/services/file-upload/file-upload.service";
import { FormValidationErrorsService } from "@bitwarden/common/services/formValidationErrors.service";
import { NotificationsService } from "@bitwarden/common/services/notifications.service";
@@ -138,6 +136,10 @@ import { FolderApiService } from "@bitwarden/common/vault/services/folder/folder
import { FolderService } from "@bitwarden/common/vault/services/folder/folder.service";
import { SyncNotifierService } from "@bitwarden/common/vault/services/sync/sync-notifier.service";
import { SyncService } from "@bitwarden/common/vault/services/sync/sync.service";
import {
VaultExportService,
VaultExportServiceAbstraction,
} from "@bitwarden/exporter/vault-export";
import { AuthGuard } from "../auth/guards/auth.guard";
import { LockGuard } from "../auth/guards/lock.guard";
@@ -464,8 +466,8 @@ import { AbstractThemingService } from "./theming/theming.service.abstraction";
deps: [AbstractStorageService, SECURE_STORAGE, STATE_FACTORY],
},
{
provide: ExportServiceAbstraction,
useClass: ExportService,
provide: VaultExportServiceAbstraction,
useClass: VaultExportService,
deps: [
FolderServiceAbstraction,
CipherServiceAbstraction,

View File

@@ -4,7 +4,6 @@ import { merge, takeUntil, Subject, startWith } from "rxjs";
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service";
import { ExportService } from "@bitwarden/common/abstractions/export.service";
import { FileDownloadService } from "@bitwarden/common/abstractions/fileDownload/fileDownload.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
@@ -13,6 +12,7 @@ import { UserVerificationService } from "@bitwarden/common/abstractions/userVeri
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { EncryptedExportType, EventType } from "@bitwarden/common/enums";
import { VaultExportServiceAbstraction } from "@bitwarden/exporter/vault-export";
@Directive()
export class ExportComponent implements OnInit, OnDestroy {
@@ -41,7 +41,7 @@ export class ExportComponent implements OnInit, OnDestroy {
protected cryptoService: CryptoService,
protected i18nService: I18nService,
protected platformUtilsService: PlatformUtilsService,
protected exportService: ExportService,
protected exportService: VaultExportServiceAbstraction,
protected eventCollectionService: EventCollectionService,
private policyService: PolicyService,
protected win: Window,

View File

@@ -1,28 +0,0 @@
import { EventType } from "../../enums";
import { EventView } from "../view/event.view";
export class EventExport {
message: string;
appIcon: string;
appName: string;
userId: string;
userName: string;
userEmail: string;
date: string;
ip: string;
type: string;
installationId: string;
constructor(event: EventView) {
this.message = event.humanReadableMessage;
this.appIcon = event.appIcon;
this.appName = event.appName;
this.userId = event.userId;
this.userName = event.userName;
this.userEmail = event.userEmail;
this.date = event.date;
this.ip = event.ip;
this.type = EventType[event.type];
this.installationId = event.installationId;
}
}

View File

@@ -0,0 +1,11 @@
export { CardExport } from "./card.export";
export { CipherWithIdExport } from "./cipher-with-ids.export";
export { CipherExport } from "./cipher.export";
export { CollectionWithIdExport } from "./collection-with-id.export";
export { CollectionExport } from "./collection.export";
export { FieldExport } from "./field.export";
export { FolderWithIdExport } from "./folder-with-id.export";
export { FolderExport } from "./folder.export";
export { IdentityExport } from "./identity.export";
export { LoginUriExport } from "./login-uri.export";
export { SecureNoteExport } from "./secure-note.export";

View File

@@ -0,0 +1,14 @@
const { pathsToModuleNameMapper } = require("ts-jest");
const { compilerOptions } = require("../shared/tsconfig.libs");
const sharedConfig = require("../shared/jest.config.base");
module.exports = {
...sharedConfig,
preset: "ts-jest",
testEnvironment: "jsdom",
moduleNameMapper: pathsToModuleNameMapper(compilerOptions?.paths || {}, {
prefix: "<rootDir>/",
}),
};

View File

@@ -0,0 +1,23 @@
{
"name": "@bitwarden/exporter",
"version": "0.0.0",
"description": "Home for all Bitwarden exporters.",
"keywords": [
"bitwarden"
],
"author": "Bitwarden Inc.",
"homepage": "https://bitwarden.com",
"repository": {
"type": "git",
"url": "https://github.com/bitwarden/clients"
},
"license": "GPL-3.0",
"scripts": {
"clean": "rimraf dist/**/*",
"build": "npm run clean && tsc",
"build:watch": "npm run clean && tsc -watch"
},
"dependencies": {
"@bitwarden/common": "file:../common"
}
}

View File

@@ -0,0 +1,24 @@
export class ExportHelper {
static getFileName(prefix: string = null, extension = "csv"): string {
const now = new Date();
const dateString =
now.getFullYear() +
"" +
this.padNumber(now.getMonth() + 1, 2) +
"" +
this.padNumber(now.getDate(), 2) +
this.padNumber(now.getHours(), 2) +
"" +
this.padNumber(now.getMinutes(), 2) +
this.padNumber(now.getSeconds(), 2);
return "bitwarden" + (prefix ? "_" + prefix : "") + "_export_" + dateString + "." + extension;
}
private static padNumber(num: number, width: number, padCharacter = "0"): string {
const numString = num.toString();
return numString.length >= width
? numString
: new Array(width - numString.length + 1).join(padCharacter) + numString;
}
}

View File

@@ -0,0 +1,2 @@
export * from "./services/vault-export.service.abstraction";
export * from "./services/vault-export.service";

View File

@@ -1,11 +1,9 @@
import { EventView } from "../models/view/event.view";
export const EXPORT_FORMATS = ["csv", "json", "encrypted_json"] as const;
export type ExportFormat = (typeof EXPORT_FORMATS)[number];
export abstract class ExportService {
export abstract class VaultExportServiceAbstraction {
getExport: (format?: ExportFormat, organizationId?: string) => Promise<string>;
getPasswordProtectedExport: (password: string, organizationId?: string) => Promise<string>;
getOrganizationExport: (organizationId: string, format?: ExportFormat) => Promise<string>;
getEventExport: (events: EventView[]) => Promise<string>;
getFileName: (prefix?: string, extension?: string) => string;
}

View File

@@ -8,8 +8,7 @@ import { KdfConfig } from "@bitwarden/common/auth/models/domain/kdf-config";
import { KdfType, DEFAULT_PBKDF2_ITERATIONS } from "@bitwarden/common/enums";
import { Utils } from "@bitwarden/common/misc/utils";
import { EncString } from "@bitwarden/common/models/domain/enc-string";
import { CipherWithIdExport as CipherExport } from "@bitwarden/common/models/export/cipher-with-ids.export";
import { ExportService } from "@bitwarden/common/services/export.service";
import { CipherWithIdExport } from "@bitwarden/common/models/export/cipher-with-ids.export";
import { StateService } from "@bitwarden/common/services/state.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
@@ -21,7 +20,9 @@ import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
import { LoginView } from "@bitwarden/common/vault/models/view/login.view";
import { BuildTestObject, GetUniqueString } from "../utils";
import { BuildTestObject, GetUniqueString } from "../../../../common/spec/utils";
import { VaultExportService } from "./vault-export.service";
const UserCipherViews = [
generateCipherView(false),
@@ -101,9 +102,9 @@ function generateFolder() {
function expectEqualCiphers(ciphers: CipherView[] | Cipher[], jsonResult: string) {
const actual = JSON.stringify(JSON.parse(jsonResult).items);
const items: CipherExport[] = [];
const items: CipherWithIdExport[] = [];
ciphers.forEach((c: CipherView | Cipher) => {
const item = new CipherExport();
const item = new CipherWithIdExport();
item.build(c);
items.push(item);
});
@@ -139,8 +140,8 @@ function expectEqualFolders(folders: Folder[], jsonResult: string) {
expect(actual).toEqual(JSON.stringify(items));
}
describe("ExportService", () => {
let exportService: ExportService;
describe("VaultExportService", () => {
let exportService: VaultExportService;
let apiService: SubstituteOf<ApiService>;
let cryptoFunctionService: SubstituteOf<CryptoFunctionService>;
let cipherService: SubstituteOf<CipherService>;
@@ -161,7 +162,7 @@ describe("ExportService", () => {
stateService.getKdfType().resolves(KdfType.PBKDF2_SHA256);
stateService.getKdfConfig().resolves(new KdfConfig(DEFAULT_PBKDF2_ITERATIONS));
exportService = new ExportService(
exportService = new VaultExportService(
folderService,
cipherService,
apiService,

View File

@@ -1,37 +1,36 @@
import * as papa from "papaparse";
import { BitwardenPasswordProtectedFileFormat } from "@bitwarden/importer/src/importers/bitwarden/bitwarden-password-protected-types";
import { ApiService } from "../abstractions/api.service";
import { CryptoService } from "../abstractions/crypto.service";
import { CryptoFunctionService } from "../abstractions/cryptoFunction.service";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { CryptoFunctionService } from "@bitwarden/common/abstractions/cryptoFunction.service";
import { StateService } from "@bitwarden/common/abstractions/state.service";
import { CollectionData } from "@bitwarden/common/admin-console/models/data/collection.data";
import { Collection } from "@bitwarden/common/admin-console/models/domain/collection";
import { CollectionDetailsResponse } from "@bitwarden/common/admin-console/models/response/collection.response";
import { CollectionView } from "@bitwarden/common/admin-console/models/view/collection.view";
import { KdfConfig } from "@bitwarden/common/auth/models/domain/kdf-config";
import { KdfType } from "@bitwarden/common/enums";
import { Utils } from "@bitwarden/common/misc/utils";
import {
ExportFormat,
ExportService as ExportServiceAbstraction,
} from "../abstractions/export.service";
import { StateService } from "../abstractions/state.service";
import { CollectionData } from "../admin-console/models/data/collection.data";
import { Collection } from "../admin-console/models/domain/collection";
import { CollectionDetailsResponse } from "../admin-console/models/response/collection.response";
import { CollectionView } from "../admin-console/models/view/collection.view";
import { KdfConfig } from "../auth/models/domain/kdf-config";
import { KdfType } from "../enums";
import { Utils } from "../misc/utils";
import { CipherWithIdExport as CipherExport } from "../models/export/cipher-with-ids.export";
import { CollectionWithIdExport as CollectionExport } from "../models/export/collection-with-id.export";
import { EventExport } from "../models/export/event.export";
import { FolderWithIdExport as FolderExport } from "../models/export/folder-with-id.export";
import { EventView } from "../models/view/event.view";
import { CipherService } from "../vault/abstractions/cipher.service";
import { FolderService } from "../vault/abstractions/folder/folder.service.abstraction";
import { CipherType } from "../vault/enums/cipher-type";
import { CipherData } from "../vault/models/data/cipher.data";
import { Cipher } from "../vault/models/domain/cipher";
import { Folder } from "../vault/models/domain/folder";
import { CipherView } from "../vault/models/view/cipher.view";
import { FolderView } from "../vault/models/view/folder.view";
CipherWithIdExport,
CollectionWithIdExport,
FolderWithIdExport,
} from "@bitwarden/common/models/export";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { CipherType } from "@bitwarden/common/vault/enums/cipher-type";
import { CipherData } from "@bitwarden/common/vault/models/data/cipher.data";
import { Cipher } from "@bitwarden/common/vault/models/domain/cipher";
import { Folder } from "@bitwarden/common/vault/models/domain/folder";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
export class ExportService implements ExportServiceAbstraction {
import { ExportHelper } from "../../export-helper";
import { BitwardenPasswordProtectedFileFormat } from "../bitwarden-password-protected-types";
import { ExportFormat, VaultExportServiceAbstraction } from "./vault-export.service.abstraction";
export class VaultExportService implements VaultExportServiceAbstraction {
constructor(
private folderService: FolderService,
private cipherService: CipherService,
@@ -93,24 +92,8 @@ export class ExportService implements ExportServiceAbstraction {
}
}
async getEventExport(events: EventView[]): Promise<string> {
return papa.unparse(events.map((e) => new EventExport(e)));
}
getFileName(prefix: string = null, extension = "csv"): string {
const now = new Date();
const dateString =
now.getFullYear() +
"" +
this.padNumber(now.getMonth() + 1, 2) +
"" +
this.padNumber(now.getDate(), 2) +
this.padNumber(now.getHours(), 2) +
"" +
this.padNumber(now.getMinutes(), 2) +
this.padNumber(now.getSeconds(), 2);
return "bitwarden" + (prefix ? "_" + prefix : "") + "_export_" + dateString + "." + extension;
return ExportHelper.getFileName(prefix, extension);
}
private async getDecryptedExport(format: "json" | "csv"): Promise<string> {
@@ -170,7 +153,7 @@ export class ExportService implements ExportServiceAbstraction {
if (f.id == null) {
return;
}
const folder = new FolderExport();
const folder = new FolderWithIdExport();
folder.build(f);
jsonDoc.folders.push(folder);
});
@@ -179,7 +162,7 @@ export class ExportService implements ExportServiceAbstraction {
if (c.organizationId != null) {
return;
}
const cipher = new CipherExport();
const cipher = new CipherWithIdExport();
cipher.build(c);
cipher.collectionIds = null;
jsonDoc.items.push(cipher);
@@ -221,7 +204,7 @@ export class ExportService implements ExportServiceAbstraction {
if (f.id == null) {
return;
}
const folder = new FolderExport();
const folder = new FolderWithIdExport();
folder.build(f);
jsonDoc.folders.push(folder);
});
@@ -230,7 +213,7 @@ export class ExportService implements ExportServiceAbstraction {
if (c.organizationId != null) {
return;
}
const cipher = new CipherExport();
const cipher = new CipherWithIdExport();
cipher.build(c);
cipher.collectionIds = null;
jsonDoc.items.push(cipher);
@@ -313,13 +296,13 @@ export class ExportService implements ExportServiceAbstraction {
};
decCollections.forEach((c) => {
const collection = new CollectionExport();
const collection = new CollectionWithIdExport();
collection.build(c);
jsonDoc.collections.push(collection);
});
decCiphers.forEach((c) => {
const cipher = new CipherExport();
const cipher = new CipherWithIdExport();
cipher.build(c);
jsonDoc.items.push(cipher);
});
@@ -373,26 +356,19 @@ export class ExportService implements ExportServiceAbstraction {
};
collections.forEach((c) => {
const collection = new CollectionExport();
const collection = new CollectionWithIdExport();
collection.build(c);
jsonDoc.collections.push(collection);
});
ciphers.forEach((c) => {
const cipher = new CipherExport();
const cipher = new CipherWithIdExport();
cipher.build(c);
jsonDoc.items.push(cipher);
});
return JSON.stringify(jsonDoc, null, " ");
}
private padNumber(num: number, width: number, padCharacter = "0"): string {
const numString = num.toString();
return numString.length >= width
? numString
: new Array(width - numString.length + 1).join(padCharacter) + numString;
}
private buildCommonCipher(cipher: any, c: CipherView) {
cipher.type = null;
cipher.name = c.name;

View File

@@ -0,0 +1,5 @@
{
"extends": "../shared/tsconfig.libs",
"include": ["src"],
"exclude": ["node_modules", "dist"]
}

View File

@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}

View File

@@ -18,6 +18,7 @@
"build:watch": "npm run clean && tsc -watch"
},
"dependencies": {
"@bitwarden/common": "file:../common"
"@bitwarden/common": "file:../common",
"@bitwarden/exporter": "file:../exporter"
}
}

View File

@@ -1,9 +1,11 @@
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { EncString } from "@bitwarden/common/models/domain/enc-string";
import { CipherWithIdExport } from "@bitwarden/common/models/export/cipher-with-ids.export";
import { CollectionWithIdExport } from "@bitwarden/common/models/export/collection-with-id.export";
import { FolderWithIdExport } from "@bitwarden/common/models/export/folder-with-id.export";
import {
CipherWithIdExport,
CollectionWithIdExport,
FolderWithIdExport,
} from "@bitwarden/common/models/export";
import { ImportResult } from "../../models/import-result";
import { BaseImporter } from "../base-importer";

View File

@@ -4,12 +4,12 @@ import { KdfConfig } from "@bitwarden/common/auth/models/domain/kdf-config";
import { KdfType } from "@bitwarden/common/enums";
import { EncString } from "@bitwarden/common/models/domain/enc-string";
import { SymmetricCryptoKey } from "@bitwarden/common/models/domain/symmetric-crypto-key";
import { BitwardenPasswordProtectedFileFormat } from "@bitwarden/exporter/vault-export/bitwarden-password-protected-types";
import { ImportResult } from "../../models/import-result";
import { Importer } from "../importer";
import { BitwardenJsonImporter } from "./bitwarden-json-importer";
import { BitwardenPasswordProtectedFileFormat } from "./bitwarden-password-protected-types";
export class BitwardenPasswordProtectedImporter extends BitwardenJsonImporter implements Importer {
private key: SymmetricCryptoKey;

View File

@@ -5,7 +5,8 @@
"@bitwarden/common/*": ["../common/src/*"],
"@bitwarden/angular/*": ["../angular/src/*"],
"@bitwarden/node/*": ["../node/src/*"],
"@bitwarden/importer": ["../importer/src"]
"@bitwarden/importer": ["../importer/src"],
"@bitwarden/exporter/*": ["../exporter/src/*"]
}
}
}