2020-05-03 15:42:55 -04:00
|
|
|
import * as child_process from 'child_process';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as os from 'os';
|
2020-05-03 14:46:05 -04:00
|
|
|
|
|
|
|
export interface Version {
|
|
|
|
gnupg: string;
|
|
|
|
libgcrypt: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gpg = async (args: string[] = []): Promise<string> => {
|
|
|
|
return child_process
|
|
|
|
.execSync(`gpg ${args.join(' ')}`, {
|
|
|
|
encoding: 'utf8'
|
|
|
|
})
|
|
|
|
.trim();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getVersion = async (): Promise<Version> => {
|
|
|
|
let gnupgVersion: string = '';
|
|
|
|
let libgcryptVersion: string = '';
|
|
|
|
|
|
|
|
await gpg(['--version']).then(stdout => {
|
|
|
|
for (let line of stdout.replace(/\r/g, '').trim().split(/\n/g)) {
|
|
|
|
if (line.startsWith('gpg (GnuPG) ')) {
|
|
|
|
gnupgVersion = line.substr('gpg (GnuPG) '.length).trim();
|
|
|
|
} else if (line.startsWith('gpg (GnuPG/MacGPG2) ')) {
|
|
|
|
gnupgVersion = line.substr('gpg (GnuPG/MacGPG2) '.length).trim();
|
|
|
|
} else if (line.startsWith('libgcrypt ')) {
|
|
|
|
libgcryptVersion = line.substr('libgcrypt '.length).trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
gnupg: gnupgVersion,
|
|
|
|
libgcrypt: libgcryptVersion
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const importKey = async (armoredText: string): Promise<void> => {
|
2020-05-03 15:42:55 -04:00
|
|
|
const keyFolder: string = fs.mkdtempSync(path.join(os.tmpdir(), 'ghaction-import-gpg-'));
|
|
|
|
const keyPath: string = `${keyFolder}/key.pgp`;
|
|
|
|
fs.writeFileSync(keyPath, armoredText, {mode: 0o600});
|
2020-05-03 14:46:05 -04:00
|
|
|
|
|
|
|
await gpg(['--import', '--batch', '--yes', keyPath]).finally(() => {
|
2020-05-03 15:42:55 -04:00
|
|
|
fs.unlinkSync(keyPath);
|
2020-05-03 14:46:05 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteKey = async (fingerprint: string): Promise<void> => {
|
|
|
|
await gpg(['--batch', '--yes', ' --delete-secret-keys', fingerprint]);
|
|
|
|
await gpg(['--batch', '--yes', ' --delete-keys', fingerprint]);
|
|
|
|
};
|