2023-05-19 22:55:29 +03:00
|
|
|
/** @file This script signs the content of all archives that we have for macOS.
|
|
|
|
* For this to work this needs to run on macOS with `codesign`, and a JDK installed.
|
|
|
|
* `codesign` is needed to sign the files, while the JDK is needed for correct packing
|
|
|
|
* and unpacking of java archives.
|
2023-03-15 06:42:14 +03:00
|
|
|
*
|
2023-05-19 22:55:29 +03:00
|
|
|
* We require this extra step as our dependencies contain files that require us
|
|
|
|
* to re-sign jar contents that cannot be opened as pure zip archives,
|
|
|
|
* but require a java toolchain to extract and re-assemble to preserve manifest information.
|
|
|
|
* This functionality is not provided by `electron-osx-sign` out of the box.
|
2023-03-15 06:42:14 +03:00
|
|
|
*
|
2023-05-19 22:55:29 +03:00
|
|
|
* This code is based on https://github.com/electron/electron-osx-sign/pull/231
|
|
|
|
* but our use-case is unlikely to be supported by `electron-osx-sign`
|
|
|
|
* as it adds a java toolchain as additional dependency.
|
2023-03-15 06:42:14 +03:00
|
|
|
* This script should be removed once the engine is signed. */
|
|
|
|
|
|
|
|
import * as childProcess from 'node:child_process'
|
|
|
|
import * as fs from 'node:fs/promises'
|
|
|
|
import * as os from 'node:os'
|
|
|
|
import * as pathModule from 'node:path'
|
|
|
|
|
2022-09-19 22:02:18 +03:00
|
|
|
import glob from 'fast-glob'
|
|
|
|
|
|
|
|
// ===============================================
|
|
|
|
// === Patterns of entities that need signing. ===
|
|
|
|
// ===============================================
|
|
|
|
|
|
|
|
/** Parts of the GraalVM distribution that need to be signed by us in an extra step. */
|
|
|
|
async function graalSignables(resourcesDir: string): Promise<Signable[]> {
|
|
|
|
const archivePatterns: ArchivePattern[] = [
|
2023-11-27 18:05:09 +03:00
|
|
|
[`Contents/Home/jmods/java.base.jmod`, ['bin/java', 'bin/keytool', 'lib/jspawnhelper']],
|
|
|
|
[`Contents/Home/jmods/java.rmi.jmod`, ['bin/rmiregistry']],
|
|
|
|
[`Contents/Home/jmods/java.scripting.jmod`, ['bin/jrunscript']],
|
|
|
|
[`Contents/Home/jmods/jdk.compiler.jmod`, ['bin/javac', 'bin/serialver']],
|
|
|
|
[`Contents/Home/jmods/jdk.hotspot.agent.jmod`, ['bin/jhsdb']],
|
|
|
|
[`Contents/Home/jmods/jdk.httpserver.jmod`, ['bin/jwebserver']],
|
2022-09-19 22:02:18 +03:00
|
|
|
[`Contents/Home/jmods/jdk.jartool.jmod`, ['bin/jarsigner', 'bin/jar']],
|
2023-11-27 18:05:09 +03:00
|
|
|
[`Contents/Home/jmods/jdk.javadoc.jmod`, ['bin/javadoc']],
|
|
|
|
[`Contents/Home/jmods/jdk.javadoc.jmod`, ['bin/javadoc']],
|
|
|
|
[`Contents/Home/jmods/jdk.jconsole.jmod`, ['bin/jconsole']],
|
2022-09-19 22:02:18 +03:00
|
|
|
[`Contents/Home/jmods/jdk.jdeps.jmod`, ['bin/javap', 'bin/jdeprscan', 'bin/jdeps']],
|
2023-11-27 18:05:09 +03:00
|
|
|
[`Contents/Home/jmods/jdk.jdi.jmod`, ['bin/jdb']],
|
2022-09-19 22:02:18 +03:00
|
|
|
[`Contents/Home/jmods/jdk.jfr.jmod`, ['bin/jfr']],
|
|
|
|
[`Contents/Home/jmods/jdk.jlink.jmod`, ['bin/jmod', 'bin/jlink', 'bin/jimage']],
|
|
|
|
[`Contents/Home/jmods/jdk.jshell.jmod`, ['bin/jshell']],
|
2023-05-24 11:39:08 +03:00
|
|
|
[
|
|
|
|
`Contents/Home/jmods/jdk.jpackage.jmod`,
|
|
|
|
['bin/jpackage', 'classes/jdk/jpackage/internal/resources/jpackageapplauncher'],
|
|
|
|
],
|
2023-11-27 18:05:09 +03:00
|
|
|
[`Contents/Home/jmods/jdk.jstatd.jmod`, ['bin/jstatd']],
|
|
|
|
[
|
|
|
|
`Contents/Home/jmods/jdk.jcmd.jmod`,
|
|
|
|
['bin/jstack', 'bin/jcmd', 'bin/jps', 'bin/jmap', 'bin/jstat', 'bin/jinfo'],
|
|
|
|
],
|
2022-09-19 22:02:18 +03:00
|
|
|
]
|
|
|
|
|
2023-11-27 18:05:09 +03:00
|
|
|
const binariesPatterns = [`Contents/MacOS/libjli.dylib`]
|
2022-09-19 22:02:18 +03:00
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
// We use `*` for Graal versioned directory to not have to update this script on every GraalVM
|
|
|
|
// update. Updates might still be needed when the list of binaries to sign changes.
|
2023-03-15 06:42:14 +03:00
|
|
|
const graalDir = pathModule.join(resourcesDir, 'enso', 'runtime', '*')
|
2022-09-19 22:02:18 +03:00
|
|
|
const archives = await ArchiveToSign.lookupMany(graalDir, archivePatterns)
|
|
|
|
const binaries = await BinaryToSign.lookupMany(graalDir, binariesPatterns)
|
|
|
|
return [...archives, ...binaries]
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parts of the Enso Engine distribution that need to be signed by us in an extra step. */
|
|
|
|
async function ensoPackageSignables(resourcesDir: string): Promise<Signable[]> {
|
2023-05-19 22:55:29 +03:00
|
|
|
// Archives, and their content that need to be signed in an extra step. If a new archive is
|
|
|
|
// added to the engine dependencies this also needs to be added here. If an archive is not added
|
|
|
|
// here, it will show up as a failure to notarise the IDE. The offending archive will be named
|
|
|
|
// in the error message provided by Apple and can then be added here.
|
2022-09-19 22:02:18 +03:00
|
|
|
const engineDir = `${resourcesDir}/enso/dist/*`
|
|
|
|
const archivePatterns: ArchivePattern[] = [
|
|
|
|
[
|
2023-11-27 18:05:09 +03:00
|
|
|
`/component/runner/runner.jar`,
|
2022-09-19 22:02:18 +03:00
|
|
|
[
|
|
|
|
'org/sqlite/native/Mac/x86_64/libsqlitejdbc.jnilib',
|
2023-11-27 18:05:09 +03:00
|
|
|
'org/sqlite/native/Mac/aarch64/libsqlitejdbc.jnilib',
|
|
|
|
'com/sun/jna/darwin-aarch64/libjnidispatch.jnilib',
|
|
|
|
'com/sun/jna/darwin-x86-64/libjnidispatch.jnilib',
|
|
|
|
],
|
|
|
|
],
|
2023-12-04 14:50:59 +03:00
|
|
|
[
|
|
|
|
'component/python-resources-23.1.0.jar',
|
|
|
|
[
|
|
|
|
'META-INF/resources/darwin/*/lib/graalpy23.1/*.dylib',
|
|
|
|
'META-INF/resources/darwin/*/lib/graalpy23.1/modules/*.so',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
`component/truffle-nfi-libffi-23.1.0.jar`,
|
|
|
|
['META-INF/resources/nfi-native/libnfi/darwin/*/bin/libtrufflenfi.dylib'],
|
|
|
|
],
|
2023-11-27 18:05:09 +03:00
|
|
|
[
|
|
|
|
`component/truffle-runtime-23.1.0.jar`,
|
|
|
|
[
|
|
|
|
'META-INF/resources/engine/libtruffleattach/darwin/amd64/bin/libtruffleattach.dylib',
|
|
|
|
'META-INF/resources/engine/libtruffleattach/darwin/aarch64/bin/libtruffleattach.dylib',
|
2022-09-19 22:02:18 +03:00
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
2023-11-27 18:05:09 +03:00
|
|
|
`lib/Standard/Database/*/polyglot/java/sqlite-jdbc-*.jar`,
|
2022-09-19 22:02:18 +03:00
|
|
|
[
|
2023-11-27 18:05:09 +03:00
|
|
|
'org/sqlite/native/Mac/aarch64/libsqlitejdbc.jnilib',
|
2022-09-19 22:02:18 +03:00
|
|
|
'org/sqlite/native/Mac/x86_64/libsqlitejdbc.jnilib',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
return ArchiveToSign.lookupMany(engineDir, archivePatterns)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ================
|
|
|
|
// === Signing. ===
|
|
|
|
// ================
|
|
|
|
|
|
|
|
/** Information we need to sign a given binary. */
|
|
|
|
interface SigningContext {
|
|
|
|
/** A digital identity that is stored in a keychain that is on the calling user's keychain
|
2023-05-19 22:55:29 +03:00
|
|
|
* search list. We rely on this already being set up by the Electron Builder. */
|
2024-02-07 14:26:59 +03:00
|
|
|
readonly identity: string
|
2022-09-19 22:02:18 +03:00
|
|
|
/** Path to the entitlements file. */
|
2024-02-07 14:26:59 +03:00
|
|
|
readonly entitlements: string
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** An entity that we want to sign. */
|
|
|
|
interface Signable {
|
|
|
|
/** Sign this entity. */
|
2024-02-07 14:26:59 +03:00
|
|
|
readonly sign: (context: SigningContext) => Promise<void>
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Placeholder name for temporary archives. */
|
2023-03-15 06:42:14 +03:00
|
|
|
const TEMPORARY_ARCHIVE_PATH = 'temporary_archive.zip'
|
2022-09-19 22:02:18 +03:00
|
|
|
|
|
|
|
/** Helper to execute a program in a given directory and return the output. */
|
2023-03-15 06:42:14 +03:00
|
|
|
function run(cmd: string, args: string[], cwd?: string) {
|
2022-09-19 22:02:18 +03:00
|
|
|
console.log('Running', cmd, args, cwd)
|
2023-03-15 06:42:14 +03:00
|
|
|
return childProcess.execFileSync(cmd, args, { cwd }).toString()
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Archive with some binaries that we want to sign.
|
|
|
|
*
|
2023-05-19 22:55:29 +03:00
|
|
|
* Can be either a zip or a jar file. */
|
2022-09-19 22:02:18 +03:00
|
|
|
class ArchiveToSign implements Signable {
|
2023-03-15 06:42:14 +03:00
|
|
|
/** Looks up for archives to sign using the given path patterns. */
|
|
|
|
static lookupMany = lookupManyHelper(ArchiveToSign.lookup.bind(this))
|
2022-09-19 22:02:18 +03:00
|
|
|
|
|
|
|
/** Create a new instance. */
|
2023-03-15 06:42:14 +03:00
|
|
|
constructor(
|
|
|
|
/** An absolute path to the archive. */
|
|
|
|
public path: string,
|
|
|
|
/** A list of patterns for files to sign inside the archive.
|
|
|
|
* Relative to the root of the archive. */
|
|
|
|
public binaries: glob.Pattern[]
|
|
|
|
) {}
|
|
|
|
|
|
|
|
/** Looks up for archives to sign using the given path pattern. */
|
|
|
|
static async lookup(base: string, [pattern, binaries]: ArchivePattern) {
|
|
|
|
return lookupHelper(path => new ArchiveToSign(path, binaries))(base, pattern)
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 06:42:14 +03:00
|
|
|
/** Sign content of an archive. This function extracts the archive, signs the required files,
|
|
|
|
* re-packages the archive and replaces the original. */
|
2022-09-19 22:02:18 +03:00
|
|
|
async sign(context: SigningContext) {
|
|
|
|
console.log(`Signing archive ${this.path}`)
|
2023-03-15 06:42:14 +03:00
|
|
|
const archiveName = pathModule.basename(this.path)
|
2022-09-19 22:02:18 +03:00
|
|
|
const workingDir = await getTmpDir()
|
|
|
|
try {
|
|
|
|
const isJar = archiveName.endsWith(`jar`)
|
|
|
|
|
|
|
|
if (isJar) {
|
|
|
|
run(`jar`, ['xf', this.path], workingDir)
|
|
|
|
} else {
|
2022-11-27 05:43:18 +03:00
|
|
|
// We cannot use `unzip` here because of the following issue:
|
2023-05-19 22:55:29 +03:00
|
|
|
// https://unix.stackexchange.com/questions/115825/
|
2022-11-27 05:43:18 +03:00
|
|
|
// This started to be an issue with GraalVM 22.3.0 release.
|
|
|
|
run(`7za`, ['X', `-o${workingDir}`, this.path])
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const binariesToSign = await BinaryToSign.lookupMany(workingDir, this.binaries)
|
|
|
|
for (const binaryToSign of binariesToSign) {
|
2023-03-15 06:42:14 +03:00
|
|
|
void binaryToSign.sign(context)
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isJar) {
|
|
|
|
if (archiveName.includes(`runner`)) {
|
2023-03-15 06:42:14 +03:00
|
|
|
run(
|
|
|
|
`jar`,
|
|
|
|
['-cfm', TEMPORARY_ARCHIVE_PATH, 'META-INF/MANIFEST.MF', '.'],
|
|
|
|
workingDir
|
|
|
|
)
|
2022-09-19 22:02:18 +03:00
|
|
|
} else {
|
2023-03-15 06:42:14 +03:00
|
|
|
run(`jar`, ['-cf', TEMPORARY_ARCHIVE_PATH, '.'], workingDir)
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-15 06:42:14 +03:00
|
|
|
run(`zip`, ['-rm', TEMPORARY_ARCHIVE_PATH, '.'], workingDir)
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot use fs.rename because temp and target might be on different volumes.
|
2023-03-15 06:42:14 +03:00
|
|
|
console.log(
|
|
|
|
run(`/bin/mv`, [pathModule.join(workingDir, TEMPORARY_ARCHIVE_PATH), this.path])
|
|
|
|
)
|
2022-09-19 22:02:18 +03:00
|
|
|
console.log(
|
|
|
|
`Successfully repacked ${this.path} to handle signing inner native dependency.`
|
|
|
|
)
|
2023-04-24 15:56:26 +03:00
|
|
|
return
|
2022-09-19 22:02:18 +03:00
|
|
|
} catch (error) {
|
|
|
|
console.error(
|
|
|
|
`Could not repackage ${archiveName}. Please check the ${import.meta.url} task to ` +
|
|
|
|
`ensure that it's working. This jar has to be treated specially` +
|
|
|
|
` because it has a native library and Apple's codesign does not sign inner ` +
|
|
|
|
`native libraries correctly for jar files.`
|
|
|
|
)
|
|
|
|
throw error
|
|
|
|
} finally {
|
|
|
|
await rmRf(workingDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A single code binary file to be signed. */
|
|
|
|
class BinaryToSign implements Signable {
|
2023-03-15 06:42:14 +03:00
|
|
|
/** Looks up for binaries to sign using the given path pattern. */
|
|
|
|
static lookup = lookupHelper(path => new BinaryToSign(path))
|
|
|
|
|
|
|
|
/** Looks up for binaries to sign using the given path patterns. */
|
|
|
|
static lookupMany = lookupManyHelper(BinaryToSign.lookup)
|
2022-09-19 22:02:18 +03:00
|
|
|
|
|
|
|
/** Create a new instance. */
|
2023-03-15 06:42:14 +03:00
|
|
|
constructor(
|
|
|
|
/** An absolute path to the binary. */
|
|
|
|
public path: string
|
|
|
|
) {}
|
2022-09-19 22:02:18 +03:00
|
|
|
|
|
|
|
/** Sign this binary. */
|
|
|
|
async sign({ entitlements, identity }: SigningContext) {
|
|
|
|
console.log(`Signing ${this.path}`)
|
|
|
|
run(`codesign`, [
|
|
|
|
'-vvv',
|
|
|
|
'--entitlements',
|
|
|
|
entitlements,
|
|
|
|
'--force',
|
|
|
|
'--options=runtime',
|
|
|
|
'--sign',
|
|
|
|
identity,
|
|
|
|
this.path,
|
|
|
|
])
|
2023-03-15 06:42:14 +03:00
|
|
|
// Async functions should contain await.
|
|
|
|
await Promise.resolve()
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==============================
|
|
|
|
// === Discovering Signables. ===
|
|
|
|
// ==============================
|
|
|
|
|
|
|
|
/** Helper used to concisely define patterns for an archive to sign.
|
|
|
|
*
|
2023-03-15 06:42:14 +03:00
|
|
|
* Consists of pattern of the archive path
|
2023-05-19 22:55:29 +03:00
|
|
|
* and set of patterns for files to sign inside the archive. */
|
2022-09-19 22:02:18 +03:00
|
|
|
type ArchivePattern = [glob.Pattern, glob.Pattern[]]
|
|
|
|
|
|
|
|
/** Like `glob` but returns absolute paths by default. */
|
2023-05-19 22:55:29 +03:00
|
|
|
async function globAbsolute(pattern: glob.Pattern, options?: glob.Options): Promise<string[]> {
|
2022-09-19 22:02:18 +03:00
|
|
|
const paths = await glob(pattern, { absolute: true, ...options })
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
/** Glob patterns relative to a given base directory. The base directory is allowed to be a pattern
|
|
|
|
* as well. */
|
|
|
|
async function globAbsoluteIn(
|
2022-09-19 22:02:18 +03:00
|
|
|
base: glob.Pattern,
|
|
|
|
pattern: glob.Pattern,
|
|
|
|
options?: glob.Options
|
|
|
|
): Promise<string[]> {
|
2023-05-19 22:55:29 +03:00
|
|
|
return globAbsolute(pathModule.join(base, pattern), options)
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Generate a lookup function for a given Signable type. */
|
|
|
|
function lookupHelper<R extends Signable>(mapper: (path: string) => R) {
|
|
|
|
return async (base: string, pattern: glob.Pattern) => {
|
2023-05-19 22:55:29 +03:00
|
|
|
const paths = await globAbsoluteIn(base, pattern)
|
2022-09-19 22:02:18 +03:00
|
|
|
return paths.map(mapper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Generate a lookup function for a given Signable type. */
|
|
|
|
function lookupManyHelper<T, R extends Signable>(
|
|
|
|
lookup: (base: string, pattern: T) => Promise<R[]>
|
|
|
|
) {
|
|
|
|
return async function (base: string, patterns: T[]) {
|
2023-11-27 18:05:09 +03:00
|
|
|
const results = await Promise.all(
|
|
|
|
patterns.map(async pattern => {
|
|
|
|
const ret = await lookup(base, pattern)
|
|
|
|
if (ret.length === 0) {
|
|
|
|
console.warn(`No files found for pattern ${String(pattern)} in ${base}`)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
})
|
|
|
|
)
|
2022-09-19 22:02:18 +03:00
|
|
|
return results.flat()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==================
|
|
|
|
// === Utilities. ===
|
|
|
|
// ==================
|
|
|
|
|
|
|
|
/** Remove file recursively. */
|
|
|
|
async function rmRf(path: string) {
|
|
|
|
await fs.rm(path, { recursive: true, force: true })
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
/** Get a new temporary directory. Caller is responsible for cleaning up the directory. */
|
2022-09-19 22:02:18 +03:00
|
|
|
async function getTmpDir(prefix?: string) {
|
2023-03-15 06:42:14 +03:00
|
|
|
return await fs.mkdtemp(pathModule.join(os.tmpdir(), prefix ?? 'enso-signing-'))
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ====================
|
|
|
|
// === Entry point. ===
|
|
|
|
// ====================
|
|
|
|
|
|
|
|
/** Input for this script. */
|
|
|
|
interface Input extends SigningContext {
|
2024-02-07 14:26:59 +03:00
|
|
|
readonly appOutDir: string
|
|
|
|
readonly productFilename: string
|
2022-09-19 22:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Entry point, meant to be used from an afterSign Electron Builder's hook. */
|
|
|
|
export default async function (context: Input) {
|
|
|
|
console.log('Environment: ', process.env)
|
2023-03-15 06:42:14 +03:00
|
|
|
const { appOutDir, productFilename } = context
|
|
|
|
const appDir = pathModule.join(appOutDir, `${productFilename}.app`)
|
|
|
|
const contentsDir = pathModule.join(appDir, 'Contents')
|
|
|
|
const resourcesDir = pathModule.join(contentsDir, 'Resources')
|
2022-09-19 22:02:18 +03:00
|
|
|
|
|
|
|
// Sign archives.
|
|
|
|
console.log('Signing GraalVM elemenets...')
|
|
|
|
for (const signable of await graalSignables(resourcesDir)) await signable.sign(context)
|
|
|
|
|
|
|
|
console.log('Signing Engine elements...')
|
|
|
|
for (const signable of await ensoPackageSignables(resourcesDir)) await signable.sign(context)
|
|
|
|
|
|
|
|
// Finally re-sign the top-level enso.
|
2023-03-15 06:42:14 +03:00
|
|
|
const topLevelExecutable = new BinaryToSign(
|
|
|
|
pathModule.join(contentsDir, 'MacOS', productFilename)
|
|
|
|
)
|
2022-09-19 22:02:18 +03:00
|
|
|
await topLevelExecutable.sign(context)
|
|
|
|
}
|