Open History to see the last commit
1// Azure Trusted Signing from a non-Windows host. The Windows-only path is
2// electron-builder's azureSignOptions (PowerShell + signtool.exe); this one is
3// electron-builder's `win.signtoolOptions.sign` hook and the CLI archive's
4// signer. jsign reads AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET
5// itself when no --storepass is given.
6
7import * as NodeServices from "@effect/platform-node/NodeServices";
8import * as Config from "effect/Config";
9import * as Effect from "effect/Effect";
10import * as Schema from "effect/Schema";
11import { ChildProcess, ChildProcessSpawner } from "effect/process";
12
13export class JsignFailedError extends Schema.TaggedError<JsignFailedError>()("JsignFailedError", {
14 file: Schema.String,
15 exitCode: Schema.Number,
16}) {
17 override get message() {
18 return `jsign exited with code ${String(this.exitCode)} signing ${this.file}.`;
19 }
20}
21
22export const signWithJsign = Effect.fn("signWithJsign")(function* (file: string) {
23 const { jsign, endpoint, account, profile } = yield* Config.all({
24 jsign: Config.String("T3CODE_JSIGN").pipe(Config.withDefault("jsign")),
25 endpoint: Config.String("AZURE_TRUSTED_SIGNING_ENDPOINT"),
26 account: Config.String("AZURE_TRUSTED_SIGNING_ACCOUNT_NAME"),
27 profile: Config.String("AZURE_TRUSTED_SIGNING_CERTIFICATE_PROFILE_NAME"),
28 });
29 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
30 const child = yield* spawner.spawn(
31 ChildProcess.make(
32 jsign,
33 [
34 "--storetype",
35 "TRUSTEDSIGNING",
36 "--keystore",
37 new URL(endpoint).host,
38 "--alias",
39 `${account}/${profile}`,
40 "--alg",
41 "SHA-256",
42 "--tsaurl",
43 "http://timestamp.acs.microsoft.com",
44 "--tsmode",
45 "RFC3161",
46 // jsign appends a nested signature by default; signtool replaces.
47 "--replace",
48 file,
49 ],
50 { stdout: "inherit", stderr: "inherit" },
51 ),
52 );
53 const exitCode = Number(yield* child.exitCode);
54 if (exitCode !== 0) {
55 return yield* new JsignFailedError({ file, exitCode });
56 }
57});
58
59export default function sign(configuration: { readonly path: string }): Promise<void> {
60 return signWithJsign(configuration.path).pipe(
61 Effect.scoped,
62 Effect.provide(NodeServices.layer),
63 Effect.runPromise,
64 );
65}