diff --git a/__tests__/gpg.test.ts b/__tests__/gpg.test.ts index 8630576..bba0bd5 100644 --- a/__tests__/gpg.test.ts +++ b/__tests__/gpg.test.ts @@ -6,6 +6,10 @@ const userInfo = { encoding: 'utf8', flag: 'r' }), + pgp_base64: fs.readFileSync('.github/test-key-base64.pgp', { + encoding: 'utf8', + flag: 'r' + }), passphrase: fs.readFileSync('.github/test-key.pass', { encoding: 'utf8', flag: 'r' @@ -47,7 +51,7 @@ describe('gpg', () => { }); }); it('imports key (as base64 string) to GnuPG', async () => { - await gpg.importKey(Buffer.from(userInfo.pgp).toString('base64')).then(output => { + await gpg.importKey(userInfo.pgp_base64).then(output => { console.log(output); expect(output).not.toEqual(''); }); diff --git a/__tests__/openpgp.test.ts b/__tests__/openpgp.test.ts index 9e13902..23eb506 100644 --- a/__tests__/openpgp.test.ts +++ b/__tests__/openpgp.test.ts @@ -6,6 +6,10 @@ const userInfo = { encoding: 'utf8', flag: 'r' }), + pgp_base64: fs.readFileSync('.github/test-key-base64.pgp', { + encoding: 'utf8', + flag: 'r' + }), passphrase: fs.readFileSync('.github/test-key.pass', { encoding: 'utf8', flag: 'r' @@ -28,7 +32,7 @@ describe('openpgp', () => { }); }); it('returns a PGP private key from a base64 armored string', async () => { - await openpgp.readPrivateKey(Buffer.from(userInfo.pgp).toString('base64')).then(privateKey => { + await openpgp.readPrivateKey(userInfo.pgp_base64).then(privateKey => { expect(privateKey.keyID).toEqual(userInfo.keyID); expect(privateKey.name).toEqual(userInfo.name); expect(privateKey.email).toEqual(userInfo.email); @@ -46,4 +50,17 @@ describe('openpgp', () => { }); }, 30000); }); + + describe('isArmored', () => { + it('returns true for armored key string', async () => { + await openpgp.isArmored(userInfo.pgp).then(armored => { + expect(armored).toEqual(true); + }); + }); + it('returns false for base64 key string', async () => { + await openpgp.isArmored(userInfo.pgp_base64).then(armored => { + expect(armored).toEqual(false); + }); + }); + }); }); diff --git a/dist/index.js b/dist/index.js index fb3ad5a..b4b1e08 100644 --- a/dist/index.js +++ b/dist/index.js @@ -967,27 +967,14 @@ module.exports = require("child_process"); "use strict"; // From https://github.com/actions/checkout/blob/master/src/state-helper.ts -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.IsPost = void 0; const coreCommand = __importStar(__webpack_require__(431)); /** * Indicates whether the POST action is running @@ -1007,25 +994,6 @@ if (!exports.IsPost) { "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -1035,6 +1003,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(__webpack_require__(470)); const git = __importStar(__webpack_require__(453)); @@ -1155,25 +1130,6 @@ else { "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -1183,12 +1139,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); -exports.killAgent = exports.deleteKey = exports.presetPassphrase = exports.configureAgent = exports.getKeygrip = exports.importKey = exports.getDirs = exports.getVersion = exports.agentConfig = void 0; const fs = __importStar(__webpack_require__(747)); const path = __importStar(__webpack_require__(622)); const os = __importStar(__webpack_require__(87)); const exec = __importStar(__webpack_require__(807)); +const openpgp = __importStar(__webpack_require__(781)); exports.agentConfig = `default-cache-ttl 7200 max-cache-ttl 31536000 allow-preset-passphrase`; @@ -1273,8 +1236,7 @@ exports.getDirs = () => __awaiter(void 0, void 0, void 0, function* () { exports.importKey = (key) => __awaiter(void 0, void 0, void 0, function* () { const keyFolder = fs.mkdtempSync(path.join(os.tmpdir(), 'ghaction-import-gpg-')); const keyPath = `${keyFolder}/key.pgp`; - const armored = key.trimLeft().startsWith('---') ? key : Buffer.from(key, 'base64').toString(); - fs.writeFileSync(keyPath, armored, { mode: 0o600 }); + fs.writeFileSync(keyPath, (yield openpgp.isArmored(key)) ? key : Buffer.from(key, 'base64').toString(), { mode: 0o600 }); return yield exec .exec('gpg', ['--import', '--batch', '--yes', keyPath], true) .then(res => { @@ -1448,25 +1410,6 @@ function escapeProperty(s) { "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -1476,8 +1419,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); -exports.setConfig = void 0; const exec = __importStar(__webpack_require__(807)); const git = (args = []) => __awaiter(void 0, void 0, void 0, function* () { return yield exec.exec(`git`, args, true).then(res => { @@ -45690,25 +45639,6 @@ module.exports = require("fs"); "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -45718,15 +45648,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.generateKeyPair = exports.readPrivateKey = void 0; const openpgp = __importStar(__webpack_require__(724)); const addressparser_1 = __importDefault(__webpack_require__(977)); -exports.readPrivateKey = (armoredText) => __awaiter(void 0, void 0, void 0, function* () { - const { keys: [privateKey], err: err } = yield openpgp.key.readArmored(isArmored(armoredText) ? armoredText : Buffer.from(armoredText, 'base64').toString()); +exports.readPrivateKey = (key) => __awaiter(void 0, void 0, void 0, function* () { + const { keys: [privateKey], err: err } = yield openpgp.key.readArmored((yield exports.isArmored(key)) ? key : Buffer.from(key, 'base64').toString()); if (err === null || err === void 0 ? void 0 : err.length) { throw err[0]; } @@ -45755,7 +45691,9 @@ exports.generateKeyPair = (name, email, passphrase, numBits = 4096) => __awaiter privateKey: keyPair.privateKeyArmored.replace(/\r\n/g, '\n').trim() }; }); -const isArmored = (text) => text.trimLeft().startsWith('---'); +exports.isArmored = (text) => __awaiter(void 0, void 0, void 0, function* () { + return text.trimLeft().startsWith('---'); +}); //# sourceMappingURL=openpgp.js.map /***/ }), @@ -45765,25 +45703,6 @@ const isArmored = (text) => text.trimLeft().startsWith('---'); "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -45793,8 +45712,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); -exports.exec = void 0; const actionsExec = __importStar(__webpack_require__(986)); exports.exec = (command, args = [], silent) => __awaiter(void 0, void 0, void 0, function* () { let stdout = ''; diff --git a/src/gpg.ts b/src/gpg.ts index 665ecf2..4b91a19 100644 --- a/src/gpg.ts +++ b/src/gpg.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; import * as exec from './exec'; +import * as openpgp from './openpgp'; export const agentConfig = `default-cache-ttl 7200 max-cache-ttl 31536000 @@ -105,10 +106,7 @@ export const getDirs = async (): Promise => { export const importKey = async (key: string): Promise => { const keyFolder: string = fs.mkdtempSync(path.join(os.tmpdir(), 'ghaction-import-gpg-')); const keyPath: string = `${keyFolder}/key.pgp`; - - const armored: string = key.trimLeft().startsWith('---') ? key : Buffer.from(key, 'base64').toString(); - - fs.writeFileSync(keyPath, armored, {mode: 0o600}); + fs.writeFileSync(keyPath, (await openpgp.isArmored(key)) ? key : Buffer.from(key, 'base64').toString(), {mode: 0o600}); return await exec .exec('gpg', ['--import', '--batch', '--yes', keyPath], true) diff --git a/src/openpgp.ts b/src/openpgp.ts index 85b9ea9..1373b98 100644 --- a/src/openpgp.ts +++ b/src/openpgp.ts @@ -14,11 +14,11 @@ export interface KeyPair { privateKey: string; } -export const readPrivateKey = async (armoredText: string): Promise => { +export const readPrivateKey = async (key: string): Promise => { const { keys: [privateKey], err: err - } = await openpgp.key.readArmored(isArmored(armoredText) ? armoredText : Buffer.from(armoredText, 'base64').toString()); + } = await openpgp.key.readArmored((await isArmored(key)) ? key : Buffer.from(key, 'base64').toString()); if (err?.length) { throw err[0]; @@ -53,4 +53,6 @@ export const generateKeyPair = async (name: string, email: string, passphrase: s }; }; -const isArmored = (text: string) => text.trimLeft().startsWith('---'); +export const isArmored = async (text: string): Promise => { + return text.trimLeft().startsWith('---'); +};