Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gpg: fallback to gpg homedir if HOME not set #181

Merged
merged 3 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions __tests__/gpg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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('');
Expand All @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
comment: false
github_checks:
annotations: false
github_checks: false
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

39 changes: 22 additions & 17 deletions src/gpg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ export interface Dirs {
homedir: string;
}

const getGnupgHome = async (): Promise<string> => {
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<string> => {
return await exec
.getExecOutput(`gpg-connect-agent "${command}" /bye`, [], {
Expand All @@ -50,6 +39,26 @@ const gpgConnectAgent = async (command: string): Promise<string> => {
});
};

export const getHome = async (): Promise<string> => {
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<Version> => {
return await exec
.getExecOutput('gpg', ['--version'], {
Expand Down Expand Up @@ -192,12 +201,8 @@ export const getKeygrip = async (fingerprint: string): Promise<string> => {
});
};

export const configureAgent = async (config: string): Promise<void> => {
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<void> => {
const gpgAgentConf = path.join(homedir, 'gpg-agent.conf');
await fs.writeFile(gpgAgentConf, config, function (err) {
if (err) throw err;
});
Expand Down
47 changes: 22 additions & 25 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,32 @@ async function run(): Promise<void> {
});
});

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) {
Expand Down