feat: update ssh-verify.ts

This commit is contained in:
2025-01-19 16:26:34 +08:00
parent 4d61625252
commit 27111b421f
2 changed files with 22 additions and 19 deletions

View File

@@ -1,6 +1,9 @@
#!/usr/bin/env -S deno run --allow-env --allow-import --allow-read --allow-net
import { parseArgs } from "jsr:@std/cli/parse-args";
import {
log,
} from "https://hatter.ink/script/fetch/library/deno-commons-mod.ts?202401191623";
import {
fetchKeys,
SshKey,
@@ -15,7 +18,7 @@ const flags = parseArgs(Deno.args, {
});
if (Deno.args.length === 0) {
console.log("ssh-verify.ts --help for help");
log.info("ssh-verify.ts --help for help");
Deno.exit(1);
}
@@ -29,7 +32,7 @@ ssh-verify.ts --username <github-username> <filename>
}
if (flags._.length === 0) {
console.log("Requires filename.");
log.error("Requires filename.");
Deno.exit(1);
}
@@ -37,15 +40,15 @@ const filename = flags._[0] as string;
const sshSigFilename = `${filename}.sshsig`;
if (flags.debug) {
console.log(`SSH signature file: ${sshSigFilename}`);
log.debug(`SSH signature file: ${sshSigFilename}`);
}
const sshSignature = await SshSignature.parseFile(sshSigFilename);
const verifyResult = await sshSignature.verifyFile(filename);
if (!verifyResult) {
console.log(
`[ERROR] Verify ${filename}, signature: ${sshSigFilename} failed.`,
log.error(
`Verify ${filename}, signature: ${sshSigFilename} failed.`,
);
Deno.exit(1);
}
@@ -54,19 +57,19 @@ const sshPublicKey = sshSignature.publicKey;
const sshPublicKeyRawBase64 = sshSignature.publicKey.asRawBase64();
const sshPublicKeySshFormat = sshPublicKey.toSshKeyFormat();
console.log(`[SUCCESS] File ${filename} verify success.`);
console.log(`[SUCCESS] Signing SSH key: ${sshPublicKeySshFormat}`);
log.success(`File ${filename} verify success.`);
log.success(`Signing SSH key: ${sshPublicKeySshFormat}`);
if (flags.username) {
let matches = false;
try {
console.log(`[INFO] Fetch SSH keys for user: ${flags.username}`);
log.info(`Fetch SSH keys for user: ${flags.username}`);
const keys: Array<SshKey> = await fetchKeys(flags.username);
if (flags.debug) {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
console.log(
log.debug(
`[DEBUG] Found SSH key: ${key.algorithm} ${key.material}`,
);
}
@@ -79,19 +82,19 @@ if (flags.username) {
}
}
} catch (e) {
console.error(
`[ERROR] Fetch SSH keys for ${flags.username} failed: ${e}`,
log.error(
`Fetch SSH keys for ${flags.username} failed: ${e}`,
);
}
if (matches) {
console.log(
`[SUCCESS] SSH key matches for GitHub user: ${flags.username} success.`,
log.success(
`SSH key matches for GitHub user: ${flags.username} success.`,
);
} else {
console.log(
`[ERROR] SSH key NOT matched for GitHub user: ${flags.username}.`,
log.error(
`SSH key NOT matched for GitHub user: ${flags.username}.`,
);
}
} else {
console.log("[WARN] GitHub user is not assigned.");
log.warn("GitHub user is not assigned.");
}