-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joaquim Rocha <joaquim.rocha@microsoft.com>
- Loading branch information
1 parent
e098a11
commit 6821ca0
Showing
7 changed files
with
342 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require('dotenv').config(); | ||
const { execSync } = require('child_process'); | ||
const path = require('path'); | ||
|
||
exports.default = async function codeSign(config) { | ||
const teamID = process.env.APPLE_TEAM_ID; | ||
|
||
if (!teamID) { | ||
console.log('Mac codesign: No Apple Team ID found, skipping codesign'); | ||
return; | ||
} | ||
|
||
const entitlementsPath = path.resolve(path.join(__dirname, '..', 'entitlements.mac.plist')); | ||
|
||
let exitCode = 0; | ||
try { | ||
execSync( | ||
`codesign -s ${teamID} --deep --force --options runtime --entitlements ${entitlementsPath} ${config.app}` | ||
); | ||
} catch (e) { | ||
exitCode = e.status !== null ? e.status : 1; | ||
} | ||
|
||
console.log('Mac codesign:', exitCode === 0 ? 'Success' : `Failed (${exitCode})`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/** | ||
* This script is used to sign and notarize the Headlamp app for MacOS | ||
* using a tool from ESRP (Windows only). It is mainly called from CI. | ||
* | ||
* Usage: node esrp-notarize.js SIGN|NOTARIZE path-to-sign | ||
**/ | ||
|
||
const crypto = require('crypto'); | ||
const { execSync } = require('child_process'); | ||
const path = require('path'); | ||
const os = require('os'); | ||
const fs = require('fs'); | ||
|
||
const SIGN_JSON_TEMPLATE = { | ||
Version: '1.0.0', | ||
DriEmail: [`${process.env.HEADLAMP_WINDOWS_SIGN_EMAIL}`], | ||
GroupId: null, | ||
CorrelationVector: null, | ||
SignBatches: [], | ||
}; | ||
|
||
const POLICY_JSON = { | ||
Version: '1.0.0', | ||
Intent: '', | ||
ContentType: '', | ||
ContentOrigin: '', | ||
ProductState: '', | ||
Audience: '', | ||
}; | ||
|
||
const AUTH_JSON = { | ||
Version: '1.0.0', | ||
AuthenticationType: 'AAD_CERT', | ||
ClientId: `${process.env.HEADLAMP_WINDOWS_CLIENT_ID}`, | ||
AuthCert: { | ||
SubjectName: `CN=${process.env.HEADLAMP_WINDOWS_CLIENT_ID}.microsoft.com`, | ||
StoreLocation: 'LocalMachine', | ||
StoreName: 'My', | ||
SendX5c: 'true', | ||
}, | ||
RequestSigningCert: { | ||
SubjectName: `CN=${process.env.HEADLAMP_WINDOWS_CLIENT_ID}`, | ||
StoreLocation: 'LocalMachine', | ||
StoreName: 'My', | ||
}, | ||
}; | ||
|
||
function getFileList(rootDir) { | ||
let files = {}; | ||
let dirs = ['.']; | ||
while (dirs.length > 0) { | ||
const dirName = dirs.shift(); | ||
const curDir = path.join(rootDir, dirName); | ||
|
||
fs.readdirSync(curDir).forEach(file => { | ||
if (['node_modules', '.git'].includes(file)) { | ||
return; | ||
} | ||
const filepath = path.resolve(rootDir, dirName, file); | ||
const stat = fs.statSync(filepath); | ||
if (stat.isDirectory() && !files[file]) { | ||
dirs.push(path.join(dirName, file)); | ||
files[file] = []; | ||
} else { | ||
if (!files[dirName]) { | ||
files[dirName] = []; | ||
} | ||
|
||
files[dirName].push({ | ||
path: file, | ||
hash: getSHA256(filepath), | ||
}); | ||
} | ||
}); | ||
} | ||
return files; | ||
} | ||
|
||
function getSHA256(filePath) { | ||
const hash = crypto.createHash('sha256'); | ||
const data = fs.readFileSync(filePath); | ||
|
||
hash.update(data); | ||
return hash.digest('hex'); | ||
} | ||
|
||
const signOp = { | ||
KeyCode: 'CP-401337-Apple', | ||
|
||
OperationCode: 'MacAppDeveloperSign', | ||
Parameters: { | ||
Hardening: '--options=runtime', | ||
}, | ||
ToolName: 'sign', | ||
|
||
ToolVersion: '1.0', | ||
}; | ||
const notarizeOp = { | ||
KeyCode: 'CP-401337-Apple', | ||
OperationCode: 'MacAppNotarize', | ||
Parameters: { | ||
BundleId: 'com.microsoft.Headlamp', | ||
}, | ||
ToolName: 'sign', | ||
ToolVersion: '1.0', | ||
}; | ||
|
||
function createSignJson(pathToSign, fileName = 'test_SignInput.json') { | ||
return createJson(pathToSign, signOp, fileName); | ||
} | ||
|
||
function createNotarizeJson(pathToSign, fileName = 'test_SignInput.json') { | ||
return createJson(pathToSign, notarizeOp, fileName); | ||
} | ||
|
||
function createJson(pathToSign, op, fileName = 'test_SignInput.json') { | ||
let rootDir = pathToSign; | ||
let files = {}; | ||
|
||
// Check if we are signing one single file or all files in a directory | ||
const stat = fs.statSync(pathToSign); | ||
if (stat.isFile()) { | ||
rootDir = path.dirname(pathToSign); | ||
files = { | ||
'.': [ | ||
{ | ||
path: path.basename(pathToSign), | ||
hash: getSHA256(pathToSign), | ||
}, | ||
], | ||
}; | ||
} else { | ||
files = getFileList(pathToSign); | ||
} | ||
|
||
const filesJson = (dir, files) => { | ||
return { | ||
SourceLocationType: 'UNC', | ||
SourceRootDirectory: path.resolve(rootDir, dir), | ||
SignRequestFiles: files.map(f => ({ | ||
SourceLocation: f.path, | ||
SourceHash: f.hash ?? '', | ||
HashType: (f.hash && 'SHA256') || null, | ||
Name: f.path, | ||
})), | ||
SigningInfo: { | ||
Operations: [op], | ||
}, | ||
}; | ||
}; | ||
|
||
SIGN_JSON_TEMPLATE.SignBatches = Object.keys(files) | ||
.map(dir => filesJson(dir, files[dir])) | ||
.filter(f => f.SignRequestFiles.length > 0); | ||
const filePath = path.join(os.tmpdir(), fileName); | ||
fs.writeFileSync(filePath, JSON.stringify(SIGN_JSON_TEMPLATE, undefined, 2)); | ||
|
||
return filePath; | ||
} | ||
|
||
/** | ||
* Signs the given file, or all files in a given directory if that's what's passed to it. | ||
* @param esrpTool - The path to the ESRP tool. | ||
* @param op - The operation to perform. Either 'SIGN' or 'NOTARIZE'. | ||
* @param pathToSign - A path to a file or directory. | ||
*/ | ||
function sign(esrpTool, op, pathToSign) { | ||
const absPathToSign = path.resolve(pathToSign); | ||
const signJsonBase = path.basename(absPathToSign).split('.')[0]; | ||
let signInputJson = ''; | ||
if (op === 'SIGN') { | ||
signInputJson = createSignJson(absPathToSign, `${signJsonBase}-SignInput.json`); | ||
} else if (op === 'NOTARIZE') { | ||
signInputJson = createNotarizeJson(absPathToSign, `${signJsonBase}-SignInput.json`); | ||
} else { | ||
throw new Error('Invalid operation'); | ||
} | ||
|
||
const policyJson = path.resolve(os.tmpdir(), 'Policy.json'); | ||
fs.writeFileSync(policyJson, JSON.stringify(POLICY_JSON, undefined, 2)); | ||
|
||
const authJson = path.resolve(os.tmpdir(), 'Auth.json'); | ||
fs.writeFileSync(authJson, JSON.stringify(AUTH_JSON, undefined, 2)); | ||
|
||
try { | ||
execSync(`${esrpTool} Sign -l Verbose -a ${authJson} -p ${policyJson} -i ${signInputJson}`); | ||
} catch (e) { | ||
console.error('Failed to sign:', e); | ||
process.exit(e.status !== null ? e.status ?? 1 : 1); | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
const wantedOp = process.argv[2]; | ||
const pathToSign = process.argv[3]; | ||
sign(process.env.ESRP_PATH, wantedOp, pathToSign); | ||
process.exit(0); | ||
} |
Oops, something went wrong.