Skip to content

Commit d6f3f49

Browse files
authored
Merge pull request #181 from crazy-max/fix-gpg-home
gpg: fallback to gpg homedir if HOME not set
2 parents 72f7de7 + e07c757 commit d6f3f49

File tree

6 files changed

+50
-49
lines changed

6 files changed

+50
-49
lines changed

Diff for: __tests__/gpg.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('getDirs', () => {
7272
describe('configureAgent', () => {
7373
// eslint-disable-next-line jest/expect-expect
7474
it('configures GnuPG agent', async () => {
75-
await gpg.configureAgent(gpg.agentConfig);
75+
await gpg.configureAgent(await gpg.getHome(), gpg.agentConfig);
7676
});
7777
});
7878

@@ -119,7 +119,7 @@ for (const userInfo of userInfos) {
119119
describe('presetPassphrase', () => {
120120
it('presets passphrase', async () => {
121121
await gpg.importKey(userInfo.pgp);
122-
await gpg.configureAgent(gpg.agentConfig);
122+
await gpg.configureAgent(await gpg.getHome(), gpg.agentConfig);
123123
for (const keygrip of await gpg.getKeygrips(userInfo.fingerprint)) {
124124
await gpg.presetPassphrase(keygrip, userInfo.passphrase).then(output => {
125125
expect(output).not.toEqual('');
@@ -131,7 +131,7 @@ for (const userInfo of userInfos) {
131131
describe('setTrustLevel', () => {
132132
it('set trust level', async () => {
133133
await gpg.importKey(userInfo.pgp);
134-
await gpg.configureAgent(gpg.agentConfig);
134+
await gpg.configureAgent(await gpg.getHome(), gpg.agentConfig);
135135
expect(() => {
136136
gpg.setTrustLevel(userInfo.keyID, '5');
137137
}).not.toThrow();

Diff for: codecov.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
comment: false
2-
github_checks:
3-
annotations: false
2+
github_checks: false

Diff for: dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/gpg.ts

+22-17
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ export interface Dirs {
2020
homedir: string;
2121
}
2222

23-
const getGnupgHome = async (): Promise<string> => {
24-
if (process.env.GNUPGHOME) {
25-
return process.env.GNUPGHOME;
26-
}
27-
let homedir: string = path.join(process.env.HOME || '', '.gnupg');
28-
if (os.platform() == 'win32' && !process.env.HOME) {
29-
homedir = path.join(process.env.USERPROFILE || '', '.gnupg');
30-
}
31-
return homedir;
32-
};
33-
3423
const gpgConnectAgent = async (command: string): Promise<string> => {
3524
return await exec
3625
.getExecOutput(`gpg-connect-agent "${command}" /bye`, [], {
@@ -50,6 +39,26 @@ const gpgConnectAgent = async (command: string): Promise<string> => {
5039
});
5140
};
5241

42+
export const getHome = async (): Promise<string> => {
43+
let homedir = '';
44+
if (process.env.GNUPGHOME) {
45+
homedir = process.env.GNUPGHOME;
46+
} else if (os.platform() == 'win32' && !process.env.HOME && process.env.USERPROFILE) {
47+
homedir = path.join(process.env.USERPROFILE, '.gnupg');
48+
} else if (process.env.HOME) {
49+
homedir = path.join(process.env.HOME, '.gnupg');
50+
} else {
51+
homedir = (await getDirs()).homedir;
52+
}
53+
if (homedir.length == 0) {
54+
throw new Error('Unable to determine GnuPG home directory');
55+
}
56+
if (!fs.existsSync(homedir)) {
57+
fs.mkdirSync(homedir, {recursive: true});
58+
}
59+
return homedir;
60+
};
61+
5362
export const getVersion = async (): Promise<Version> => {
5463
return await exec
5564
.getExecOutput('gpg', ['--version'], {
@@ -192,12 +201,8 @@ export const getKeygrip = async (fingerprint: string): Promise<string> => {
192201
});
193202
};
194203

195-
export const configureAgent = async (config: string): Promise<void> => {
196-
const gnupgHomeDir = await getGnupgHome();
197-
if (!fs.existsSync(gnupgHomeDir)) {
198-
fs.mkdirSync(gnupgHomeDir, {recursive: true});
199-
}
200-
const gpgAgentConf = path.join(gnupgHomeDir, 'gpg-agent.conf');
204+
export const configureAgent = async (homedir: string, config: string): Promise<void> => {
205+
const gpgAgentConf = path.join(homedir, 'gpg-agent.conf');
201206
await fs.writeFile(gpgAgentConf, config, function (err) {
202207
if (err) throw err;
203208
});

Diff for: src/main.ts

+22-25
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,32 @@ async function run(): Promise<void> {
5050
});
5151
});
5252

53-
if (inputs.passphrase && !inputs.fingerprint) {
54-
// Set the passphrase for all subkeys
55-
56-
core.info('Configuring GnuPG agent');
57-
await gpg.configureAgent(gpg.agentConfig);
58-
59-
await core.group(`Getting keygrips`, async () => {
60-
for (const keygrip of await gpg.getKeygrips(fingerprint)) {
61-
core.info(`Presetting passphrase for ${keygrip}`);
53+
if (inputs.passphrase) {
54+
await core.group(`Configuring GnuPG agent`, async () => {
55+
const gpgHome = await gpg.getHome();
56+
core.info(`GnuPG home: ${gpgHome}`);
57+
await gpg.configureAgent(gpgHome, gpg.agentConfig);
58+
});
59+
if (!inputs.fingerprint) {
60+
// Set the passphrase for all subkeys
61+
await core.group(`Getting keygrips`, async () => {
62+
for (const keygrip of await gpg.getKeygrips(fingerprint)) {
63+
core.info(`Presetting passphrase for ${keygrip}`);
64+
await gpg.presetPassphrase(keygrip, inputs.passphrase).then(stdout => {
65+
core.debug(stdout);
66+
});
67+
}
68+
});
69+
} else {
70+
// Set the passphrase only for the subkey specified in the input `fingerprint`
71+
await core.group(`Getting keygrip for fingerprint`, async () => {
72+
const keygrip = await gpg.getKeygrip(fingerprint);
73+
core.info(`Presetting passphrase for key ${fingerprint} with keygrip ${keygrip}`);
6274
await gpg.presetPassphrase(keygrip, inputs.passphrase).then(stdout => {
6375
core.debug(stdout);
6476
});
65-
}
66-
});
67-
}
68-
69-
if (inputs.passphrase && inputs.fingerprint) {
70-
// Set the passphrase only for the subkey specified in the input `fingerprint`
71-
72-
core.info('Configuring GnuPG agent');
73-
await gpg.configureAgent(gpg.agentConfig);
74-
75-
await core.group(`Getting keygrip for fingerprint`, async () => {
76-
const keygrip = await gpg.getKeygrip(fingerprint);
77-
core.info(`Presetting passphrase for key ${fingerprint} with keygrip ${keygrip}`);
78-
await gpg.presetPassphrase(keygrip, inputs.passphrase).then(stdout => {
79-
core.debug(stdout);
8077
});
81-
});
78+
}
8279
}
8380

8481
if (inputs.trustLevel) {

0 commit comments

Comments
 (0)