2018-05-17 16:58:30 +02:00
|
|
|
import * as program from "commander";
|
2018-05-23 17:16:23 +02:00
|
|
|
import * as inquirer from "inquirer";
|
2018-05-17 16:58:30 +02:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ExportFormat, ExportService } from "@bitwarden/common/abstractions/export.service";
|
|
|
|
import { PolicyService } from "@bitwarden/common/abstractions/policy.service";
|
|
|
|
import { PolicyType } from "@bitwarden/common/enums/policyType";
|
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
|
|
|
import { Response } from "@bitwarden/node/cli/models/response";
|
2019-10-07 17:01:30 +02:00
|
|
|
|
2021-09-15 15:57:43 +02:00
|
|
|
import { CliUtils } from "../utils";
|
|
|
|
|
2018-05-17 16:58:30 +02:00
|
|
|
export class ExportCommand {
|
2022-02-23 22:47:32 +01:00
|
|
|
constructor(private exportService: ExportService, private policyService: PolicyService) {}
|
2021-12-20 18:04:00 +01:00
|
|
|
|
2022-02-23 22:47:32 +01:00
|
|
|
async run(options: program.OptionValues): Promise<Response> {
|
2021-11-16 10:42:30 +01:00
|
|
|
if (
|
|
|
|
options.organizationid == null &&
|
|
|
|
(await this.policyService.policyAppliesToUser(PolicyType.DisablePersonalVaultExport))
|
2021-12-20 18:04:00 +01:00
|
|
|
) {
|
2021-11-16 10:42:30 +01:00
|
|
|
return Response.badRequest(
|
|
|
|
"One or more organization policies prevents you from exporting your personal vault."
|
|
|
|
);
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
|
|
|
|
2022-02-23 22:47:32 +01:00
|
|
|
const format = options.format ?? "csv";
|
2021-05-27 22:47:39 +02:00
|
|
|
|
2021-11-16 10:42:30 +01:00
|
|
|
if (options.organizationid != null && !Utils.isGuid(options.organizationid)) {
|
|
|
|
return Response.error("`" + options.organizationid + "` is not a GUID.");
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2022-02-23 22:47:32 +01:00
|
|
|
|
2021-11-16 10:42:30 +01:00
|
|
|
let exportContent: string = null;
|
2021-12-20 18:04:00 +01:00
|
|
|
try {
|
2021-11-16 10:42:30 +01:00
|
|
|
exportContent =
|
2022-02-23 22:47:32 +01:00
|
|
|
format === "encrypted_json"
|
|
|
|
? await this.getProtectedExport(options.password, options.organizationid)
|
|
|
|
: await this.getUnprotectedExport(format, options.organizationid);
|
2021-11-16 10:42:30 +01:00
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e);
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2021-11-16 10:42:30 +01:00
|
|
|
return await this.saveFile(exportContent, options, format);
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 22:47:32 +01:00
|
|
|
private async getProtectedExport(passwordOption: string | boolean, organizationId?: string) {
|
|
|
|
const password = await this.promptPassword(passwordOption);
|
|
|
|
return password == null
|
|
|
|
? await this.exportService.getExport("encrypted_json", organizationId)
|
|
|
|
: await this.exportService.getPasswordProtectedExport(password, organizationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getUnprotectedExport(format: ExportFormat, organizationId?: string) {
|
|
|
|
return this.exportService.getExport(format, organizationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async saveFile(
|
2021-11-16 10:42:30 +01:00
|
|
|
exportContent: string,
|
|
|
|
options: program.OptionValues,
|
2022-02-23 22:47:32 +01:00
|
|
|
format: ExportFormat
|
2021-11-16 10:42:30 +01:00
|
|
|
): Promise<Response> {
|
2021-12-20 18:04:00 +01:00
|
|
|
try {
|
2021-11-16 10:42:30 +01:00
|
|
|
const fileName = this.getFileName(format, options.organizationid != null ? "org" : null);
|
|
|
|
return await CliUtils.saveResultToFile(exportContent, options.output, fileName);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e.toString());
|
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 22:47:32 +01:00
|
|
|
private getFileName(format: ExportFormat, prefix?: string) {
|
2021-05-27 22:47:39 +02:00
|
|
|
if (format === "encrypted_json") {
|
|
|
|
if (prefix == null) {
|
|
|
|
prefix = "encrypted";
|
2021-12-20 18:04:00 +01:00
|
|
|
} else {
|
2021-05-27 22:47:39 +02:00
|
|
|
prefix = "encrypted_" + prefix;
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2021-05-27 22:47:39 +02:00
|
|
|
format = "json";
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2021-11-16 10:42:30 +01:00
|
|
|
return this.exportService.getFileName(prefix, format);
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 22:47:32 +01:00
|
|
|
private async promptPassword(password: string | boolean) {
|
|
|
|
// boolean => flag set with no value, we need to prompt for password
|
|
|
|
// string => flag set with value, use this value for password
|
|
|
|
// undefined/null/false => account protect, not password, no password needed
|
|
|
|
if (typeof password === "string") {
|
|
|
|
return password;
|
|
|
|
} else if (password) {
|
2021-11-16 10:42:30 +01:00
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({
|
|
|
|
output: process.stderr,
|
2021-12-20 18:04:00 +01:00
|
|
|
})({
|
2021-11-16 10:42:30 +01:00
|
|
|
type: "password",
|
|
|
|
name: "password",
|
2022-02-23 22:47:32 +01:00
|
|
|
message: "Export file password:",
|
2021-12-20 18:04:00 +01:00
|
|
|
});
|
2022-02-23 22:47:32 +01:00
|
|
|
return answer.password as string;
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2022-02-23 22:47:32 +01:00
|
|
|
return null;
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|