Skip to content

Commit

Permalink
fix(gpg): extract only primary key fingerprint
Browse files Browse the repository at this point in the history
  • Loading branch information
phukon committed Jan 14, 2025
1 parent 1030c2d commit 6ba7ff4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ jobs:
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
6 changes: 5 additions & 1 deletion src/utils/createKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import confirm from "@inquirer/confirm";
import chalk from "chalk";
import { GitKeyKitCodes } from "../gitkeykitCodes";


export async function createPgpKey(): Promise<GitKeyKitCodes> {
try {
const shouldCreate = await confirm({
Expand All @@ -23,6 +22,11 @@ export async function createPgpKey(): Promise<GitKeyKitCodes> {
stdio: "inherit",
});

gpg.on("error", (error) => {
console.error(chalk.red(`Failed to start GPG process: ${error.message}`));
resolve(GitKeyKitCodes.ERR_KEY_GENERATION);
});

gpg.on("close", (code) => {
if (code === 0) {
console.log(chalk.green("GPG key has been generated successfully."));
Expand Down
46 changes: 33 additions & 13 deletions src/utils/setGitConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,43 @@ const execFileAsync = promisify(execFile);

async function getGpgKeyFingerprint(): Promise<string> {
try {
const { stdout } = await execFileAsync('gpg --list-secret-keys');
/*
output of `gpg --list-secret-keys --with-colons`
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ sec:u:4096:1:A1B2C3D4E5F6G7H8:1700848217:::u:::scESC:::#:::23::0: β”‚
β”‚ fpr:::::::::1234567890ABCDEF1234567890ABCDEF12345678: β”‚<-- we want this one
β”‚ grp:::::::::ABCDEF1234567890ABCDEF1234567890ABCDEF12: β”‚
β”‚ uid:u::::1700850983::0123456789ABCDEF0123456789ABCDEF01234567::Username β”‚
β”‚ <email>::::::::::0: β”‚
β”‚ ssb:u:4096:1:FEDCBA9876543210:1732404248:1858548248:::::e:::+:::23: β”‚
β”‚ fpr:::::::::FEDCBA9876543210FEDCBA9876543210FEDCBA98: β”‚
β”‚ grp:::::::::9876543210FEDCBA9876543210FEDCBA98765432: β”‚
β”‚ ssb:u:4096:1:1A2B3C4D5E6F7G8H:1732404191:1858548191:::::s:::+:::23: β”‚
β”‚ fpr:::::::::ABCD1234EFGH5678IJKL9012MNOP3456QRST7890: β”‚
β”‚ grp:::::::::WXYZ7890ABCD1234EFGH5678IJKL9012MNOP3456: β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
*/
const { stdout } = await execFileAsync('gpg', ['--list-secret-keys', '--with-colons']);

// Find the longest string that could be a fingerprint
const lines = stdout.split('\n');
let maxLength = 0;
let isPrimaryKey = false;
let keyFingerprint = '';

for (const line of lines) {
const tokens = line.trim().split(/\s+/);
for (const token of tokens) {
if (token.length > maxLength) {
keyFingerprint = token;
maxLength = token.length;
}
const parts = line.split(':');
// Mark when we find a primary key (sec)
if (parts[0] === 'sec') {
isPrimaryKey = true;
continue;
}
// Get the fingerprint only if it's for the primary key
if (isPrimaryKey && parts[0] === 'fpr') {
keyFingerprint = parts[9];
break;
}

if (parts[0] === 'ssb') {
isPrimaryKey = false;
}
}

Expand All @@ -47,7 +70,6 @@ async function setGitConfigValue(key: string, value: string): Promise<void> {

export async function setGitConfig(gpgPath: string): Promise<GitKeyKitCodes> {
try {
// Get user input
const username = await input({
message: 'Enter your name:',
validate: (value) => value.length > 0 || 'Name cannot be empty'
Expand All @@ -63,10 +85,8 @@ export async function setGitConfig(gpgPath: string): Promise<GitKeyKitCodes> {

console.log(chalk.blue('Setting git config...'));

// Get GPG key fingerprint
const keyFingerprint = await getGpgKeyFingerprint();

// Configure git settings
const configs = [
['user.name', username],
['user.email', email],
Expand Down

0 comments on commit 6ba7ff4

Please sign in to comment.