Skip to content

Commit

Permalink
feat: add input validation for key import
Browse files Browse the repository at this point in the history
  • Loading branch information
phukon committed Jan 14, 2025
1 parent 8857fde commit e6aa7a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
7 changes: 2 additions & 5 deletions bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import arg, { ArgError } from "arg";
import arg from "arg";
import chalk from "chalk";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
Expand All @@ -13,9 +13,6 @@ import createLogger from "../src/utils/logger";

const logger = createLogger("bin");

process.on("SIGINT", () => process.exit(0));
process.on("SIGTERM", () => process.exit(0));

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageJson = JSON.parse(readFileSync(join(__dirname, "../package.json"), "utf8"));
Expand Down Expand Up @@ -104,7 +101,7 @@ async function main(): Promise<void> {

usage();
} catch (error) {
if (error instanceof ArgError && error?.code === "ARG_UNKNOWN_OPTION") {
if (error instanceof arg.ArgError && error?.code === "ARG_UNKNOWN_OPTION") {
logger.error(`Invalid argument: ${error.message}`);
console.log("------");
usage();
Expand Down
44 changes: 38 additions & 6 deletions src/commands/import.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
import { readFileSync } from "fs";
import { execSync } from "child_process";
import { GitKeyKitCodes, GitKeyKitError } from "../gitkeykitCodes";
import createLogger from "../utils/logger";

export function importKey(key: string): boolean {
const logger = createLogger("commands:import");

/**
* Imports a GPG key from a file
* @param keyPath Path to the key file
* @throws {GitKeyKitError} If key import fails
*/
export async function importKey(keyPath: string): Promise<void> {
try {
execSync(`gpg --import ${key}`, { stdio: "inherit" });
return true; // Indicate success
} catch (error: any) {
throw new Error(`Error importing key: ${(error as Error).message}`);
let keyContent: string;
try {
keyContent = readFileSync(keyPath, "utf-8");
} catch (error) {
throw new GitKeyKitError(`Failed to read key file: ${keyPath}`, GitKeyKitCodes.KEY_IMPORT_ERROR, error);
}

if (!keyContent.includes("-----BEGIN PGP PRIVATE KEY BLOCK-----")) {
throw new GitKeyKitError("Invalid key file format: Missing PGP private key block", GitKeyKitCodes.KEY_IMPORT_ERROR);
}

try {
execSync("gpg --import", {
input: keyContent,
stdio: ["pipe", "inherit", "inherit"],
});

logger.green("GPG key imported successfully");
} catch (error) {
throw new GitKeyKitError("Failed to import GPG key", GitKeyKitCodes.KEY_IMPORT_ERROR, error);
}
} catch (error) {
if (error instanceof GitKeyKitError) {
throw error;
}
throw new GitKeyKitError("Unexpected error during key import", GitKeyKitCodes.KEY_IMPORT_ERROR, error);
}
}
}

0 comments on commit e6aa7a9

Please sign in to comment.