keeweb/build/tasks/grunt-sign-exe.js

71 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-01-07 18:33:21 +01:00
const fs = require('fs');
2020-04-03 20:26:27 +02:00
const path = require('path');
const { spawnSync } = require('child_process');
2020-04-05 21:43:32 +02:00
const AdmZip = require('adm-zip');
const { runRemoteTask } = require('run-remote-task');
2019-01-07 18:33:21 +01:00
2019-08-16 23:05:39 +02:00
module.exports = function(grunt) {
2020-04-05 21:43:32 +02:00
grunt.registerMultiTask(
'sign-exe',
'Signs exe file with authenticode certificate',
async function() {
const done = this.async();
const opt = this.options();
for (const [file, name] of Object.entries(opt.files)) {
await signFile(file, name, opt);
}
done();
2019-01-07 18:33:21 +01:00
}
2020-04-05 21:43:32 +02:00
);
async function signFile(file, name, opt) {
grunt.log.writeln(`Signing ${file}...`);
2020-04-03 20:26:27 +02:00
const fileNameWithoutFolder = path.basename(file);
2020-04-05 21:43:32 +02:00
const actionConfig = {
exe: fileNameWithoutFolder,
name: name || fileNameWithoutFolder,
url: opt.url
};
const zip = new AdmZip();
zip.addFile('action.json', Buffer.from(JSON.stringify(actionConfig)));
zip.addLocalFile(file);
const zipContents = zip.toBuffer();
fs.writeFileSync('data.zip', zipContents);
try {
const taskResult = await runRemoteTask(opt.windows, zipContents);
const signedFile = taskResult.file;
2020-04-05 22:45:46 +02:00
const signtool =
'C:\\Program Files (x86)\\Windows Kits\\10\\App Certification Kit\\signtool.exe';
2020-04-05 23:39:36 +02:00
const res = spawnSync(signtool, ['verify', '/pa', '/v', signedFile]);
2020-04-05 22:13:06 +02:00
2020-04-05 23:39:36 +02:00
if (res.status) {
2020-04-05 22:13:06 +02:00
grunt.warn(
`Verify error ${file}: exit code ${res.status}.\n${res.stdout.toString()}`
);
2020-04-03 20:26:27 +02:00
}
2020-04-05 22:13:06 +02:00
2020-04-05 23:39:36 +02:00
if (!res.stdout.includes('Successfully verified')) {
grunt.warn(`Verify error ${file}:\n${res.stdout.toString()}`);
}
if (!res.stdout.includes(opt.certHash)) {
grunt.warn(`Verify error ${file}: expected hash was not found`);
}
2020-04-05 22:13:06 +02:00
fs.unlinkSync(signedFile, file);
fs.writeFileSync(file, taskResult.data);
2020-04-05 21:43:32 +02:00
grunt.log.writeln(`Signed ${file}: ${name}`);
} catch (e) {
grunt.warn(`Sign error ${file}: ${e}`);
2020-04-03 20:26:27 +02:00
}
2019-01-07 18:33:21 +01:00
}
};