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