Skip to content

Commit 6cfb86a

Browse files
committed
refactor: streamline runCheckedTool and writeSecretBase64 function signatures
1 parent a96ed90 commit 6cfb86a

2 files changed

Lines changed: 39 additions & 40 deletions

File tree

packages/build/dist/index.mjs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19124,7 +19124,7 @@ function parseWindowsSignMode(raw) {
1912419124
`windows-sign-mode must be one of: none | signtool | trusted-signing. Got "${raw}".`
1912519125
);
1912619126
}
19127-
async function runCheckedTool(deps, command, args, label, opts = {}) {
19127+
async function runCheckedTool(command, args, label, deps, opts = {}) {
1912819128
deps.logger.info(`[pkg-action] ${label}: ${command} ${args.join(" ")}`);
1912919129
let result = await deps.exec(command, args, {
1913019130
ignoreReturnCode: !0,
@@ -19133,32 +19133,31 @@ async function runCheckedTool(deps, command, args, label, opts = {}) {
1913319133
if (result.exitCode !== 0)
1913419134
throw new SignError(`${label} failed (exit ${String(result.exitCode)}). See stderr above.`);
1913519135
}
19136-
async function writeSecretBase64(deps, tempDir, base64, extension) {
19137-
let path4 = join6(tempDir, `${randomBytes(8).toString("hex")}.${extension}`), bytes = Buffer.from(base64, "base64");
19136+
async function writeSecretBase64(base64, extension, deps) {
19137+
let path4 = join6(deps.tempDir, `${randomBytes(8).toString("hex")}.${extension}`), bytes = Buffer.from(base64, "base64");
1913819138
return await (deps.writeFile ?? ((p, d) => writeFile4(p, d, { mode: 384 })))(path4, bytes), path4;
1913919139
}
1914019140
async function signMacos(binaryPath, cfg, deps) {
1914119141
let keychainPath = join6(
1914219142
deps.tempDir,
1914319143
`pkg-action-${randomBytes(6).toString("hex")}.keychain-db`
19144-
), p12Path = await writeSecretBase64(deps, deps.tempDir, cfg.certificate, "p12");
19144+
), p12Path = await writeSecretBase64(cfg.certificate, "p12", deps);
1914519145
await runCheckedTool(
19146-
deps,
1914719146
"security",
1914819147
["create-keychain", "-p", cfg.keychainPassword, keychainPath],
19149-
"security create-keychain"
19148+
"security create-keychain",
19149+
deps
1915019150
), await runCheckedTool(
19151-
deps,
1915219151
"security",
1915319152
["set-keychain-settings", "-lut", "21600", keychainPath],
19154-
"security set-keychain-settings"
19153+
"security set-keychain-settings",
19154+
deps
1915519155
), await runCheckedTool(
19156-
deps,
1915719156
"security",
1915819157
["unlock-keychain", "-p", cfg.keychainPassword, keychainPath],
19159-
"security unlock-keychain"
19158+
"security unlock-keychain",
19159+
deps
1916019160
), await runCheckedTool(
19161-
deps,
1916219161
"security",
1916319162
[
1916419163
"import",
@@ -19172,9 +19171,9 @@ async function signMacos(binaryPath, cfg, deps) {
1917219171
"-T",
1917319172
"/usr/bin/security"
1917419173
],
19175-
"security import"
19174+
"security import",
19175+
deps
1917619176
), await runCheckedTool(
19177-
deps,
1917819177
"security",
1917919178
[
1918019179
"set-key-partition-list",
@@ -19185,7 +19184,8 @@ async function signMacos(binaryPath, cfg, deps) {
1918519184
cfg.keychainPassword,
1918619185
keychainPath
1918719186
],
19188-
"security set-key-partition-list"
19187+
"security set-key-partition-list",
19188+
deps
1918919189
);
1919019190
let codesignArgs = [
1919119191
"--force",
@@ -19197,13 +19197,12 @@ async function signMacos(binaryPath, cfg, deps) {
1919719197
"--sign",
1919819198
cfg.identity
1919919199
];
19200-
return cfg.entitlements !== void 0 && codesignArgs.push("--entitlements", cfg.entitlements), codesignArgs.push(binaryPath), await runCheckedTool(deps, "codesign", codesignArgs, "codesign"), await runCheckedTool(
19201-
deps,
19200+
return cfg.entitlements !== void 0 && codesignArgs.push("--entitlements", cfg.entitlements), codesignArgs.push(binaryPath), await runCheckedTool("codesign", codesignArgs, "codesign", deps), await runCheckedTool(
1920219201
"codesign",
1920319202
["--verify", "--strict", "--verbose=2", binaryPath],
19204-
"codesign --verify"
19203+
"codesign --verify",
19204+
deps
1920519205
), cfg.notarize && (await runCheckedTool(
19206-
deps,
1920719206
"xcrun",
1920819207
[
1920919208
"notarytool",
@@ -19217,13 +19216,14 @@ async function signMacos(binaryPath, cfg, deps) {
1921719216
cfg.appPassword,
1921819217
"--wait"
1921919218
],
19220-
"xcrun notarytool submit"
19219+
"xcrun notarytool submit",
19220+
deps
1922119221
), deps.logger.info(
1922219222
"[pkg-action] notarytool submit succeeded. Note: bare binaries cannot be stapled; Gatekeeper queries Apple at first launch."
1922319223
)), { keychainPath };
1922419224
}
1922519225
async function signWindowsSigntool(binaryPath, cfg, deps) {
19226-
let pfxPath = await writeSecretBase64(deps, deps.tempDir, cfg.certificate, "pfx"), args = [
19226+
let pfxPath = await writeSecretBase64(cfg.certificate, "pfx", deps), args = [
1922719227
"sign",
1922819228
"/fd",
1922919229
"sha256",
@@ -19236,7 +19236,7 @@ async function signWindowsSigntool(binaryPath, cfg, deps) {
1923619236
"/p",
1923719237
cfg.password
1923819238
];
19239-
cfg.description !== void 0 && args.push("/d", cfg.description), args.push(binaryPath), await runCheckedTool(deps, "signtool", args, "signtool sign"), await runCheckedTool(deps, "signtool", ["verify", "/pa", "/v", binaryPath], "signtool verify");
19239+
cfg.description !== void 0 && args.push("/d", cfg.description), args.push(binaryPath), await runCheckedTool("signtool", args, "signtool sign", deps), await runCheckedTool("signtool", ["verify", "/pa", "/v", binaryPath], "signtool verify", deps);
1924019240
}
1924119241
async function signWindowsTrustedSigning(binaryPath, cfg, deps) {
1924219242
let env = {
@@ -19256,7 +19256,7 @@ async function signWindowsTrustedSigning(binaryPath, cfg, deps) {
1925619256
"-fd",
1925719257
"sha256"
1925819258
];
19259-
cfg.description !== void 0 && args.push("-d", cfg.description), args.push(binaryPath), await runCheckedTool(deps, "azuresigntool", args, "azuresigntool sign", { env }), await runCheckedTool(deps, "signtool", ["verify", "/pa", "/v", binaryPath], "signtool verify");
19259+
cfg.description !== void 0 && args.push("-d", cfg.description), args.push(binaryPath), await runCheckedTool("azuresigntool", args, "azuresigntool sign", deps, { env }), await runCheckedTool("signtool", ["verify", "/pa", "/v", binaryPath], "signtool verify", deps);
1926019260
}
1926119261

1926219262
// packages/core/src/version.ts

packages/core/src/signing.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ export interface SigningDeps {
211211
}
212212

213213
async function runCheckedTool(
214-
deps: SigningDeps,
215214
command: string,
216215
args: readonly string[],
217216
label: string,
217+
deps: SigningDeps,
218218
opts: { env?: Readonly<Record<string, string>> } = {},
219219
): Promise<void> {
220220
deps.logger.info(`[pkg-action] ${label}: ${command} ${args.join(' ')}`);
@@ -231,12 +231,11 @@ async function runCheckedTool(
231231
* delete the path after use — we return it so the caller can plug into
232232
* saveState for post.ts cleanup. */
233233
async function writeSecretBase64(
234-
deps: SigningDeps,
235-
tempDir: string,
236234
base64: string,
237235
extension: string,
236+
deps: SigningDeps,
238237
): Promise<string> {
239-
const path = join(tempDir, `${randomBytes(8).toString('hex')}.${extension}`);
238+
const path = join(deps.tempDir, `${randomBytes(8).toString('hex')}.${extension}`);
240239
const bytes = Buffer.from(base64, 'base64');
241240
const writer = deps.writeFile ?? ((p: string, d: Uint8Array) => writeFile(p, d, { mode: 0o600 }));
242241
await writer(path, bytes);
@@ -260,29 +259,28 @@ export async function signMacos(
260259
deps.tempDir,
261260
`pkg-action-${randomBytes(6).toString('hex')}.keychain-db`,
262261
);
263-
const p12Path = await writeSecretBase64(deps, deps.tempDir, cfg.certificate, 'p12');
262+
const p12Path = await writeSecretBase64(cfg.certificate, 'p12', deps);
264263

265264
// Create + unlock ephemeral keychain, import cert, allow codesign access.
266265
await runCheckedTool(
267-
deps,
268266
'security',
269267
['create-keychain', '-p', cfg.keychainPassword, keychainPath],
270268
'security create-keychain',
269+
deps,
271270
);
272271
await runCheckedTool(
273-
deps,
274272
'security',
275273
['set-keychain-settings', '-lut', '21600', keychainPath],
276274
'security set-keychain-settings',
275+
deps,
277276
);
278277
await runCheckedTool(
279-
deps,
280278
'security',
281279
['unlock-keychain', '-p', cfg.keychainPassword, keychainPath],
282280
'security unlock-keychain',
281+
deps,
283282
);
284283
await runCheckedTool(
285-
deps,
286284
'security',
287285
[
288286
'import',
@@ -297,9 +295,9 @@ export async function signMacos(
297295
'/usr/bin/security',
298296
],
299297
'security import',
298+
deps,
300299
);
301300
await runCheckedTool(
302-
deps,
303301
'security',
304302
[
305303
'set-key-partition-list',
@@ -311,6 +309,7 @@ export async function signMacos(
311309
keychainPath,
312310
],
313311
'security set-key-partition-list',
312+
deps,
314313
);
315314

316315
// codesign. --force replaces existing sigs (pkg ships unsigned);
@@ -329,23 +328,22 @@ export async function signMacos(
329328
codesignArgs.push('--entitlements', cfg.entitlements);
330329
}
331330
codesignArgs.push(binaryPath);
332-
await runCheckedTool(deps, 'codesign', codesignArgs, 'codesign');
331+
await runCheckedTool('codesign', codesignArgs, 'codesign', deps);
333332

334333
// Post-sign sanity: re-invoke codesign in verify mode to confirm the
335334
// signature actually landed. Catches bad identities, revoked certs, and
336335
// silent signtool/codesign failures that still exit 0.
337336
await runCheckedTool(
338-
deps,
339337
'codesign',
340338
['--verify', '--strict', '--verbose=2', binaryPath],
341339
'codesign --verify',
340+
deps,
342341
);
343342

344343
if (cfg.notarize) {
345344
// notarytool only needs the three secrets — appleId/teamId/appPassword.
346345
// Validated up front in parseSigningInputs.
347346
await runCheckedTool(
348-
deps,
349347
'xcrun',
350348
[
351349
'notarytool',
@@ -360,6 +358,7 @@ export async function signMacos(
360358
'--wait',
361359
],
362360
'xcrun notarytool submit',
361+
deps,
363362
);
364363
// Binaries (unlike .app bundles) cannot be stapled — only container
365364
// formats accept the notarization ticket. We still submit, which
@@ -380,7 +379,7 @@ export async function signWindowsSigntool(
380379
cfg: WindowsSigntoolInputs,
381380
deps: SigningDeps,
382381
): Promise<void> {
383-
const pfxPath = await writeSecretBase64(deps, deps.tempDir, cfg.certificate, 'pfx');
382+
const pfxPath = await writeSecretBase64(cfg.certificate, 'pfx', deps);
384383
const args = [
385384
'sign',
386385
'/fd',
@@ -396,10 +395,10 @@ export async function signWindowsSigntool(
396395
];
397396
if (cfg.description !== undefined) args.push('/d', cfg.description);
398397
args.push(binaryPath);
399-
await runCheckedTool(deps, 'signtool', args, 'signtool sign');
398+
await runCheckedTool('signtool', args, 'signtool sign', deps);
400399
// Post-sign sanity: verify the signature embedded in the PE. `/pa` uses
401400
// the default Authenticode chain policy; `/v` is verbose.
402-
await runCheckedTool(deps, 'signtool', ['verify', '/pa', '/v', binaryPath], 'signtool verify');
401+
await runCheckedTool('signtool', ['verify', '/pa', '/v', binaryPath], 'signtool verify', deps);
403402
}
404403

405404
// ─── Azure Trusted Signing ────────────────────────────────────────────────
@@ -433,8 +432,8 @@ export async function signWindowsTrustedSigning(
433432
];
434433
if (cfg.description !== undefined) args.push('-d', cfg.description);
435434
args.push(binaryPath);
436-
await runCheckedTool(deps, 'azuresigntool', args, 'azuresigntool sign', { env });
435+
await runCheckedTool('azuresigntool', args, 'azuresigntool sign', deps, { env });
437436
// azuresigntool produces a standard Authenticode signature, so the same
438437
// signtool verify path applies. No azure creds required to verify.
439-
await runCheckedTool(deps, 'signtool', ['verify', '/pa', '/v', binaryPath], 'signtool verify');
438+
await runCheckedTool('signtool', ['verify', '/pa', '/v', binaryPath], 'signtool verify', deps);
440439
}

0 commit comments

Comments
 (0)