diff --git a/__tests__/gpg.test.ts b/__tests__/gpg.test.ts index 46b96be..b3e9477 100644 --- a/__tests__/gpg.test.ts +++ b/__tests__/gpg.test.ts @@ -72,7 +72,7 @@ describe('getDirs', () => { describe('configureAgent', () => { // eslint-disable-next-line jest/expect-expect it('configures GnuPG agent', async () => { - await gpg.configureAgent(gpg.agentConfig); + await gpg.configureAgent(await gpg.getHome(), gpg.agentConfig); }); }); @@ -119,7 +119,7 @@ for (const userInfo of userInfos) { describe('presetPassphrase', () => { it('presets passphrase', async () => { await gpg.importKey(userInfo.pgp); - await gpg.configureAgent(gpg.agentConfig); + await gpg.configureAgent(await gpg.getHome(), gpg.agentConfig); for (const keygrip of await gpg.getKeygrips(userInfo.fingerprint)) { await gpg.presetPassphrase(keygrip, userInfo.passphrase).then(output => { expect(output).not.toEqual(''); @@ -131,7 +131,7 @@ for (const userInfo of userInfos) { describe('setTrustLevel', () => { it('set trust level', async () => { await gpg.importKey(userInfo.pgp); - await gpg.configureAgent(gpg.agentConfig); + await gpg.configureAgent(await gpg.getHome(), gpg.agentConfig); expect(() => { gpg.setTrustLevel(userInfo.keyID, '5'); }).not.toThrow(); diff --git a/src/gpg.ts b/src/gpg.ts index 7b4145b..0d692f9 100644 --- a/src/gpg.ts +++ b/src/gpg.ts @@ -20,17 +20,6 @@ export interface Dirs { homedir: string; } -const getGnupgHome = async (): Promise => { - if (process.env.GNUPGHOME) { - return process.env.GNUPGHOME; - } - let homedir: string = path.join(process.env.HOME || '', '.gnupg'); - if (os.platform() == 'win32' && !process.env.HOME) { - homedir = path.join(process.env.USERPROFILE || '', '.gnupg'); - } - return homedir; -}; - const gpgConnectAgent = async (command: string): Promise => { return await exec .getExecOutput(`gpg-connect-agent "${command}" /bye`, [], { @@ -50,6 +39,26 @@ const gpgConnectAgent = async (command: string): Promise => { }); }; +export const getHome = async (): Promise => { + let homedir = ''; + if (process.env.GNUPGHOME) { + homedir = process.env.GNUPGHOME; + } else if (os.platform() == 'win32' && !process.env.HOME && process.env.USERPROFILE) { + homedir = path.join(process.env.USERPROFILE, '.gnupg'); + } else if (process.env.HOME) { + homedir = path.join(process.env.HOME, '.gnupg'); + } else { + homedir = (await getDirs()).homedir; + } + if (homedir.length == 0) { + throw new Error('Unable to determine GnuPG home directory'); + } + if (!fs.existsSync(homedir)) { + fs.mkdirSync(homedir, {recursive: true}); + } + return homedir; +}; + export const getVersion = async (): Promise => { return await exec .getExecOutput('gpg', ['--version'], { @@ -192,12 +201,8 @@ export const getKeygrip = async (fingerprint: string): Promise => { }); }; -export const configureAgent = async (config: string): Promise => { - const gnupgHomeDir = await getGnupgHome(); - if (!fs.existsSync(gnupgHomeDir)) { - fs.mkdirSync(gnupgHomeDir, {recursive: true}); - } - const gpgAgentConf = path.join(gnupgHomeDir, 'gpg-agent.conf'); +export const configureAgent = async (homedir: string, config: string): Promise => { + const gpgAgentConf = path.join(homedir, 'gpg-agent.conf'); await fs.writeFile(gpgAgentConf, config, function (err) { if (err) throw err; }); diff --git a/src/main.ts b/src/main.ts index 8e04296..591f23b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -50,35 +50,32 @@ async function run(): Promise { }); }); - if (inputs.passphrase && !inputs.fingerprint) { - // Set the passphrase for all subkeys - - core.info('Configuring GnuPG agent'); - await gpg.configureAgent(gpg.agentConfig); - - await core.group(`Getting keygrips`, async () => { - for (const keygrip of await gpg.getKeygrips(fingerprint)) { - core.info(`Presetting passphrase for ${keygrip}`); + if (inputs.passphrase) { + await core.group(`Configuring GnuPG agent`, async () => { + const gpgHome = await gpg.getHome(); + core.info(`GnuPG home: ${gpgHome}`); + await gpg.configureAgent(gpgHome, gpg.agentConfig); + }); + if (!inputs.fingerprint) { + // Set the passphrase for all subkeys + await core.group(`Getting keygrips`, async () => { + for (const keygrip of await gpg.getKeygrips(fingerprint)) { + core.info(`Presetting passphrase for ${keygrip}`); + await gpg.presetPassphrase(keygrip, inputs.passphrase).then(stdout => { + core.debug(stdout); + }); + } + }); + } else { + // Set the passphrase only for the subkey specified in the input `fingerprint` + await core.group(`Getting keygrip for fingerprint`, async () => { + const keygrip = await gpg.getKeygrip(fingerprint); + core.info(`Presetting passphrase for key ${fingerprint} with keygrip ${keygrip}`); await gpg.presetPassphrase(keygrip, inputs.passphrase).then(stdout => { core.debug(stdout); }); - } - }); - } - - if (inputs.passphrase && inputs.fingerprint) { - // Set the passphrase only for the subkey specified in the input `fingerprint` - - core.info('Configuring GnuPG agent'); - await gpg.configureAgent(gpg.agentConfig); - - await core.group(`Getting keygrip for fingerprint`, async () => { - const keygrip = await gpg.getKeygrip(fingerprint); - core.info(`Presetting passphrase for key ${fingerprint} with keygrip ${keygrip}`); - await gpg.presetPassphrase(keygrip, inputs.passphrase).then(stdout => { - core.debug(stdout); }); - }); + } } if (inputs.trustLevel) {