From 05c17e4c200a0a9381cb4d85da49907018ce8268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Grzywacz?= Date: Fri, 8 Sep 2023 09:29:32 +0200 Subject: [PATCH] fix(manifest sign): --private-key option is required and validated JST-386 --- src/lib/file.ts | 10 ++++++++++ src/manifest/manifest-sign.action.ts | 2 ++ src/manifest/manifest-sign.command.ts | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib/file.ts b/src/lib/file.ts index c5d5f4f..8627852 100644 --- a/src/lib/file.ts +++ b/src/lib/file.ts @@ -21,3 +21,13 @@ export async function checkFileOverwrite( // File does not exist, that's fine. } } + +export async function assertFileExists(name: string, path: string): Promise { + try { + await stat(path); + } catch (e) { + // File does not exist, that's fine. + console.error(`Error: ${name} "${path}" not found.`); + process.exit(1); + } +} diff --git a/src/manifest/manifest-sign.action.ts b/src/manifest/manifest-sign.action.ts index 2825551..f364ecf 100644 --- a/src/manifest/manifest-sign.action.ts +++ b/src/manifest/manifest-sign.action.ts @@ -2,10 +2,12 @@ import { ManifestSignOptions } from "./manifest-sign.options"; import { readManifest } from "./manifest-utils"; import { readFile, writeFile } from "fs/promises"; import { createSign } from "crypto"; +import { assertFileExists } from "../lib/file"; export async function manifestSignAction(options: ManifestSignOptions): Promise { // Read and validate the manifest. await readManifest(options.manifest); + await assertFileExists("Private key file", options.keyFile); // Read manifest buffer. const manifestBuffer = await readFile(options.manifest); diff --git a/src/manifest/manifest-sign.command.ts b/src/manifest/manifest-sign.command.ts index e310f2d..1b187e3 100644 --- a/src/manifest/manifest-sign.command.ts +++ b/src/manifest/manifest-sign.command.ts @@ -6,7 +6,7 @@ export const manifestSignCommand = new Command("sign"); manifestSignCommand .description("Sign Golem manifest file.") .addOption(createManifestOption()) - .option("-k, --key-file ", "Private key file.") + .requiredOption("-k, --key-file ", "Private key file.") .option("-p, --passphrase ", "Passphrase for the private key.") .option("-s, --signature-file ", "Signature file.", "manifest.sig") .action(async (options: ManifestSignOptions) => {