mirror of
https://github.com/crazy-max/ghaction-import-gpg.git
synced 2024-11-22 04:50:56 -05:00
Merge pull request #181 from crazy-max/fix-gpg-home
gpg: fallback to gpg homedir if HOME not set
This commit is contained in:
commit
d6f3f49f33
6 changed files with 50 additions and 49 deletions
|
@ -72,7 +72,7 @@ describe('getDirs', () => {
|
||||||
describe('configureAgent', () => {
|
describe('configureAgent', () => {
|
||||||
// eslint-disable-next-line jest/expect-expect
|
// eslint-disable-next-line jest/expect-expect
|
||||||
it('configures GnuPG agent', async () => {
|
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', () => {
|
describe('presetPassphrase', () => {
|
||||||
it('presets passphrase', async () => {
|
it('presets passphrase', async () => {
|
||||||
await gpg.importKey(userInfo.pgp);
|
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)) {
|
for (const keygrip of await gpg.getKeygrips(userInfo.fingerprint)) {
|
||||||
await gpg.presetPassphrase(keygrip, userInfo.passphrase).then(output => {
|
await gpg.presetPassphrase(keygrip, userInfo.passphrase).then(output => {
|
||||||
expect(output).not.toEqual('');
|
expect(output).not.toEqual('');
|
||||||
|
@ -131,7 +131,7 @@ for (const userInfo of userInfos) {
|
||||||
describe('setTrustLevel', () => {
|
describe('setTrustLevel', () => {
|
||||||
it('set trust level', async () => {
|
it('set trust level', async () => {
|
||||||
await gpg.importKey(userInfo.pgp);
|
await gpg.importKey(userInfo.pgp);
|
||||||
await gpg.configureAgent(gpg.agentConfig);
|
await gpg.configureAgent(await gpg.getHome(), gpg.agentConfig);
|
||||||
expect(() => {
|
expect(() => {
|
||||||
gpg.setTrustLevel(userInfo.keyID, '5');
|
gpg.setTrustLevel(userInfo.keyID, '5');
|
||||||
}).not.toThrow();
|
}).not.toThrow();
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
comment: false
|
comment: false
|
||||||
github_checks:
|
github_checks: false
|
||||||
annotations: false
|
|
||||||
|
|
2
dist/index.js
generated
vendored
2
dist/index.js
generated
vendored
File diff suppressed because one or more lines are too long
2
dist/index.js.map
generated
vendored
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
39
src/gpg.ts
39
src/gpg.ts
|
@ -20,17 +20,6 @@ export interface Dirs {
|
||||||
homedir: string;
|
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> => {
|
const gpgConnectAgent = async (command: string): Promise<string> => {
|
||||||
return await exec
|
return await exec
|
||||||
.getExecOutput(`gpg-connect-agent "${command}" /bye`, [], {
|
.getExecOutput(`gpg-connect-agent "${command}" /bye`, [], {
|
||||||
|
@ -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> => {
|
export const getVersion = async (): Promise<Version> => {
|
||||||
return await exec
|
return await exec
|
||||||
.getExecOutput('gpg', ['--version'], {
|
.getExecOutput('gpg', ['--version'], {
|
||||||
|
@ -192,12 +201,8 @@ export const getKeygrip = async (fingerprint: string): Promise<string> => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const configureAgent = async (config: string): Promise<void> => {
|
export const configureAgent = async (homedir: string, config: string): Promise<void> => {
|
||||||
const gnupgHomeDir = await getGnupgHome();
|
const gpgAgentConf = path.join(homedir, 'gpg-agent.conf');
|
||||||
if (!fs.existsSync(gnupgHomeDir)) {
|
|
||||||
fs.mkdirSync(gnupgHomeDir, {recursive: true});
|
|
||||||
}
|
|
||||||
const gpgAgentConf = path.join(gnupgHomeDir, 'gpg-agent.conf');
|
|
||||||
await fs.writeFile(gpgAgentConf, config, function (err) {
|
await fs.writeFile(gpgAgentConf, config, function (err) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
});
|
});
|
||||||
|
|
21
src/main.ts
21
src/main.ts
|
@ -50,12 +50,14 @@ async function run(): Promise<void> {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if (inputs.passphrase && !inputs.fingerprint) {
|
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
|
// Set the passphrase for all subkeys
|
||||||
|
|
||||||
core.info('Configuring GnuPG agent');
|
|
||||||
await gpg.configureAgent(gpg.agentConfig);
|
|
||||||
|
|
||||||
await core.group(`Getting keygrips`, async () => {
|
await core.group(`Getting keygrips`, async () => {
|
||||||
for (const keygrip of await gpg.getKeygrips(fingerprint)) {
|
for (const keygrip of await gpg.getKeygrips(fingerprint)) {
|
||||||
core.info(`Presetting passphrase for ${keygrip}`);
|
core.info(`Presetting passphrase for ${keygrip}`);
|
||||||
|
@ -64,14 +66,8 @@ async function run(): Promise<void> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
} else {
|
||||||
|
|
||||||
if (inputs.passphrase && inputs.fingerprint) {
|
|
||||||
// Set the passphrase only for the subkey specified in the input `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 () => {
|
await core.group(`Getting keygrip for fingerprint`, async () => {
|
||||||
const keygrip = await gpg.getKeygrip(fingerprint);
|
const keygrip = await gpg.getKeygrip(fingerprint);
|
||||||
core.info(`Presetting passphrase for key ${fingerprint} with keygrip ${keygrip}`);
|
core.info(`Presetting passphrase for key ${fingerprint} with keygrip ${keygrip}`);
|
||||||
|
@ -80,6 +76,7 @@ async function run(): Promise<void> {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (inputs.trustLevel) {
|
if (inputs.trustLevel) {
|
||||||
await core.group(`Setting key's trust level`, async () => {
|
await core.group(`Setting key's trust level`, async () => {
|
||||||
|
|
Loading…
Reference in a new issue