2018-08-06 15:38:17 +02:00
|
|
|
import * as program from "commander";
|
2022-02-23 22:47:32 +01:00
|
|
|
import * as inquirer from "inquirer";
|
|
|
|
|
2023-01-30 20:03:12 +01:00
|
|
|
import { ImportService } from "@bitwarden/common/abstractions/import/import.service.abstraction";
|
2022-09-27 22:25:19 +02:00
|
|
|
import { OrganizationService } from "@bitwarden/common/abstractions/organization/organization.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ImportType } from "@bitwarden/common/enums/importOptions";
|
|
|
|
import { Importer } from "@bitwarden/common/importers/importer";
|
2018-08-06 15:38:17 +02:00
|
|
|
|
2022-11-18 13:20:19 +01:00
|
|
|
import { Response } from "../models/response";
|
|
|
|
import { MessageResponse } from "../models/response/message.response";
|
2018-08-06 15:38:17 +02:00
|
|
|
import { CliUtils } from "../utils";
|
|
|
|
|
|
|
|
export class ImportCommand {
|
2021-12-28 21:38:51 +01:00
|
|
|
constructor(
|
|
|
|
private importService: ImportService,
|
|
|
|
private organizationService: OrganizationService
|
|
|
|
) {}
|
2018-08-06 15:38:17 +02:00
|
|
|
|
2022-02-08 00:31:36 +01:00
|
|
|
async run(
|
|
|
|
format: ImportType,
|
|
|
|
filepath: string,
|
|
|
|
options: program.OptionValues
|
|
|
|
): Promise<Response> {
|
2021-06-16 15:50:29 +02:00
|
|
|
const organizationId = options.organizationid;
|
|
|
|
if (organizationId != null) {
|
2022-11-08 22:25:19 +01:00
|
|
|
const organization = await this.organizationService.getFromState(organizationId);
|
2021-06-16 15:50:29 +02:00
|
|
|
|
|
|
|
if (organization == null) {
|
|
|
|
return Response.badRequest(
|
|
|
|
`You do not belong to an organization with the ID of ${organizationId}. Check the organization ID and sync your vault.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!organization.canAccessImportExport) {
|
|
|
|
return Response.badRequest(
|
|
|
|
"You are not authorized to import into the provided organization."
|
|
|
|
);
|
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2021-06-16 15:50:29 +02:00
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
if (options.formats || false) {
|
2021-06-16 15:50:29 +02:00
|
|
|
return await this.list();
|
2018-08-06 16:38:32 +02:00
|
|
|
} else {
|
2021-06-16 15:50:29 +02:00
|
|
|
return await this.import(format, filepath, organizationId);
|
2018-08-06 15:38:17 +02:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2018-08-06 15:38:17 +02:00
|
|
|
|
2022-02-08 00:31:36 +01:00
|
|
|
private async import(format: ImportType, filepath: string, organizationId: string) {
|
|
|
|
if (format == null) {
|
2018-08-06 16:38:32 +02:00
|
|
|
return Response.badRequest("`format` was not provided.");
|
|
|
|
}
|
|
|
|
if (filepath == null || filepath === "") {
|
|
|
|
return Response.badRequest("`filepath` was not provided.");
|
|
|
|
}
|
|
|
|
|
2021-06-16 15:50:29 +02:00
|
|
|
const importer = await this.importService.getImporter(format, organizationId);
|
2018-08-06 15:38:17 +02:00
|
|
|
if (importer === null) {
|
|
|
|
return Response.badRequest("Proper importer type required.");
|
|
|
|
}
|
2018-08-06 16:38:32 +02:00
|
|
|
|
|
|
|
try {
|
2022-03-03 15:32:49 +01:00
|
|
|
let contents;
|
|
|
|
if (format === "1password1pux") {
|
|
|
|
contents = await CliUtils.extract1PuxContent(filepath);
|
|
|
|
} else {
|
|
|
|
contents = await CliUtils.readFile(filepath);
|
|
|
|
}
|
|
|
|
|
2018-08-06 16:38:32 +02:00
|
|
|
if (contents === null || contents === "") {
|
|
|
|
return Response.badRequest("Import file was empty.");
|
|
|
|
}
|
|
|
|
|
2022-02-23 22:47:32 +01:00
|
|
|
const response = await this.doImport(importer, contents, organizationId);
|
|
|
|
if (response.success) {
|
|
|
|
response.data = new MessageResponse("Imported " + filepath, null);
|
2018-08-06 16:38:32 +02:00
|
|
|
}
|
2022-02-23 22:47:32 +01:00
|
|
|
return response;
|
2018-08-06 16:38:32 +02:00
|
|
|
} catch (err) {
|
|
|
|
return Response.badRequest(err);
|
2018-08-06 15:38:17 +02:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2018-08-06 16:38:32 +02:00
|
|
|
|
|
|
|
private async list() {
|
2018-08-06 17:43:07 +02:00
|
|
|
const options = this.importService
|
|
|
|
.getImportOptions()
|
|
|
|
.sort((a, b) => {
|
2018-08-06 16:38:32 +02:00
|
|
|
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
2021-02-04 05:51:59 +01:00
|
|
|
})
|
|
|
|
.map((option) => option.id)
|
|
|
|
.join("\n");
|
2018-08-06 16:38:32 +02:00
|
|
|
const res = new MessageResponse("Supported input formats:", options);
|
|
|
|
res.raw = options;
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
2022-02-23 22:47:32 +01:00
|
|
|
|
|
|
|
private async doImport(
|
|
|
|
importer: Importer,
|
|
|
|
contents: string,
|
|
|
|
organizationId?: string
|
|
|
|
): Promise<Response> {
|
|
|
|
const err = await this.importService.import(importer, contents, organizationId);
|
|
|
|
if (err != null) {
|
|
|
|
if (err.passwordRequired) {
|
|
|
|
importer = this.importService.getImporter(
|
|
|
|
"bitwardenpasswordprotected",
|
|
|
|
organizationId,
|
|
|
|
await this.promptPassword()
|
|
|
|
);
|
|
|
|
return this.doImport(importer, contents, organizationId);
|
|
|
|
}
|
|
|
|
return Response.badRequest(err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response.success();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async promptPassword() {
|
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({
|
|
|
|
output: process.stderr,
|
|
|
|
})({
|
|
|
|
type: "password",
|
|
|
|
name: "password",
|
|
|
|
message: "Import file password:",
|
|
|
|
});
|
|
|
|
return answer.password;
|
|
|
|
}
|
2018-08-06 15:38:17 +02:00
|
|
|
}
|