-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
145 lines (131 loc) · 4.54 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import * as core from '@actions/core';
import { promises as fs } from 'fs';
import { unlinkSync, existsSync, createWriteStream } from 'fs';
import path from 'path';
import util from 'util';
import { exec } from 'child_process';
import { env } from 'process';
const request = require('request');
const asyncExec = util.promisify(exec);
const certificateFileName = env['TEMP'] + '\\cert.pem';
const signtool = env['TEMP'] + '\\signtool.exe';
const credentialsFileName = env['TEMP'] + '\\creds.json';
const toSignFileName = env['TEMP'] + '\\tosign.txt';
const signtoolFileExtensions = [
'.dll', '.exe', '.sys', '.vxd',
'.msix', '.msixbundle', '.appx',
'.appxbundle', '.msi', '.msp',
'.msm', '.cab', '.ps1', '.psm1'
];
function sleep(seconds: number) {
if (seconds > 0)
console.log(`Waiting for ${seconds} seconds.`);
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
async function createCertificate() {
const base64Certificate = core.getInput('certificate');
const certificate = Buffer.from(base64Certificate, 'base64');
if (certificate.length == 0) {
console.log('The value for "certificate" is not set.');
return false;
}
console.log(`Writing ${certificate.length} bytes to ${certificateFileName}.`);
await fs.writeFile(certificateFileName, certificate);
return true;
}
async function createCredentials() {
const base64Certificate = core.getInput('credentials');
const credentials = Buffer.from(base64Certificate, 'base64');
if (credentials.length == 0) {
console.log('The value for "credentials" is not set.');
return false;
}
console.log(`Writing ${credentials.length} bytes to ${credentialsFileName}.`);
await fs.writeFile(credentialsFileName, credentials);
return true;
}
function downloadCloudSignTool() {
if (existsSync(signtool)) {
return;
}
console.log(`Downloading signtool.exe.`);
request('https://github.com/nextgens/CloudSignTool/releases/download/1.0.0/SignTool.exe').pipe(createWriteStream(signtool));
}
async function signWithCloudSigntool() {
try {
var options = "";
const timestampUrl = core.getInput('timestamp-url');
if(timestampUrl != "") {
options += ` -tr \"${timestampUrl}\"`
}
const description = core.getInput('description');
if(description != "") {
options += ` -d \"${description}\"`
}
const descriptionURL = core.getInput('description-url');
if(descriptionURL != "") {
options += ` -du \"${descriptionURL}\"`
}
if(core.getInput('page-hash') == "true") {
options += ` -ph`
} else {
options += ` -nph`
}
const cmd = `"${signtool}" sign -kac "${credentialsFileName}" -ac "${certificateFileName}" ${options} -k "${core.getInput('key-uri')}" -ifl "${toSignFileName}"`;
console.log(cmd);
const { stdout } = await asyncExec(cmd);
console.log(stdout);
return true;
} catch(err) {
console.log(err.stdout);
console.log(err.stderr);
return false;
}
}
async function* getFiles(folder: string, recursive: boolean): any {
const files = await fs.readdir(folder);
for (const file of files) {
const fullPath = `${folder}/${file}`;
const stat = await fs.stat(fullPath);
if (stat.isFile()) {
const extension = path.extname(file);
if (signtoolFileExtensions.includes(extension))
yield fullPath;
}
else if (stat.isDirectory() && recursive) {
yield* getFiles(fullPath, recursive);
}
}
}
async function signFiles() {
const folder = core.getInput('folder', { required: true });
const recursive = core.getInput('recursive') == 'true';
console.log(`Getting ready to sign the following files:`);
let buffer: string[] = [];
for await (const file of getFiles(folder, recursive)) {
console.log(` ${file}`);
buffer.push(file);
}
if(buffer.length > 0) {
await fs.writeFile(toSignFileName, buffer.join("\r\n"));
console.log(`Getting ready to talk to the cloud.`);
for (let i=0;i<10;i++) {
await sleep(i);
if(await signWithCloudSigntool()) { return; }
}
throw `Failed to sign`;
}
}
async function run() {
try {
await createCredentials();
downloadCloudSignTool();
if (await createCertificate())
await signFiles();
}
catch (err) {
core.setFailed(`Action failed with error: ${err}`);
}
unlinkSync(credentialsFileName);
}
run();