Skip to content
Permalink
Newer
Older
100644 178 lines (174 sloc) 7.79 KB
August 25, 2020 16:19
1
"use strict";
2
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
if (k2 === undefined) k2 = k;
4
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
}) : (function(o, m, k, k2) {
6
if (k2 === undefined) k2 = k;
7
o[k2] = m[k];
8
}));
9
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
Object.defineProperty(o, "default", { enumerable: true, value: v });
11
}) : function(o, v) {
12
o["default"] = v;
13
});
August 25, 2020 16:19
14
var __importStar = (this && this.__importStar) || function (mod) {
15
if (mod && mod.__esModule) return mod;
16
var result = {};
17
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
__setModuleDefault(result, mod);
August 25, 2020 16:19
19
return result;
20
};
21
Object.defineProperty(exports, "__esModule", { value: true });
22
exports.installPythonDeps = exports.injectWindowsTracer = exports.runInit = exports.initConfig = exports.initCodeQL = void 0;
August 25, 2020 16:19
23
const fs = __importStar(require("fs"));
24
const path = __importStar(require("path"));
November 20, 2020 11:35
25
const toolrunner = __importStar(require("@actions/exec/lib/toolrunner"));
26
const safeWhich = __importStar(require("@chrisgavin/safe-which"));
August 25, 2020 16:19
27
const analysisPaths = __importStar(require("./analysis-paths"));
28
const codeql_1 = require("./codeql");
29
const configUtils = __importStar(require("./config-utils"));
30
const tracer_config_1 = require("./tracer-config");
31
const util = __importStar(require("./util"));
32
async function initCodeQL(codeqlURL, apiDetails, tempDir, toolCacheDir, variant, logger) {
September 14, 2020 10:44
33
logger.startGroup("Setup CodeQL tools");
34
const { codeql, toolsVersion } = await codeql_1.setupCodeQL(codeqlURL, apiDetails, tempDir, toolCacheDir, variant, logger, true);
August 25, 2020 16:19
35
await codeql.printVersion();
36
logger.endGroup();
37
return { codeql, toolsVersion };
August 25, 2020 16:19
38
}
39
exports.initCodeQL = initCodeQL;
40
async function initConfig(languagesInput, queriesInput, packsInput, configFile, dbLocation, repository, tempDir, toolCacheDir, codeQL, workspacePath, gitHubVersion, apiDetails, logger) {
September 14, 2020 10:44
41
logger.startGroup("Load language configuration");
42
const config = await configUtils.initConfig(languagesInput, queriesInput, packsInput, configFile, dbLocation, repository, tempDir, toolCacheDir, codeQL, workspacePath, gitHubVersion, apiDetails, logger);
August 25, 2020 16:19
43
analysisPaths.printPathFiltersWarning(config, logger);
44
logger.endGroup();
45
return config;
46
}
47
exports.initConfig = initConfig;
48
async function runInit(codeql, config, sourceRoot) {
49
fs.mkdirSync(config.dbLocation, { recursive: true });
August 25, 2020 16:19
50
// TODO: replace this code once CodeQL supports multi-language tracing
September 14, 2020 10:44
51
for (const language of config.languages) {
August 25, 2020 16:19
52
// Init language database
53
await codeql.databaseInit(util.getCodeQLDatabasePath(config, language), language, sourceRoot);
August 25, 2020 16:19
54
}
55
return await tracer_config_1.getCombinedTracerConfig(config, codeql);
56
}
57
exports.runInit = runInit;
58
// Runs a powershell script to inject the tracer into a parent process
59
// so it can tracer future processes, hopefully including the build process.
60
// If processName is given then injects into the nearest parent process with
61
// this name, otherwise uses the processLevel-th parent if defined, otherwise
62
// defaults to the 3rd parent as a rough guess.
63
async function injectWindowsTracer(processName, processLevel, config, codeql, tracerConfig) {
64
let script;
65
if (processName !== undefined) {
66
script = `
67
Param(
68
[Parameter(Position=0)]
69
[String]
70
$tracer
71
)
72
73
$id = $PID
74
while ($true) {
75
$p = Get-CimInstance -Class Win32_Process -Filter "ProcessId = $id"
76
Write-Host "Found process: $p"
77
if ($p -eq $null) {
78
throw "Could not determine ${processName} process"
79
}
80
if ($p[0].Name -eq "${processName}") {
81
Break
82
} else {
83
$id = $p[0].ParentProcessId
84
}
85
}
September 7, 2020 17:10
86
Write-Host "Final process: $p"
87
88
Invoke-Expression "&$tracer --inject=$id"`;
89
}
90
else {
91
// If the level is not defined then guess at the 3rd parent process.
92
// This won't be correct in every setting but it should be enough in most settings,
93
// and overestimating is likely better in this situation so we definitely trace
94
// what we want, though this does run the risk of interfering with future CI jobs.
95
// Note that the default of 3 doesn't work on github actions, so we include a
96
// special case in the script that checks for Runner.Worker.exe so we can still work
97
// on actions if the runner is invoked there.
98
processLevel = processLevel || 3;
99
script = `
100
Param(
101
[Parameter(Position=0)]
102
[String]
103
$tracer
104
)
105
106
$id = $PID
September 7, 2020 17:10
107
for ($i = 0; $i -le ${processLevel}; $i++) {
108
$p = Get-CimInstance -Class Win32_Process -Filter "ProcessId = $id"
September 7, 2020 17:10
109
Write-Host "Parent process \${i}: $p"
110
if ($p -eq $null) {
111
throw "Process tree ended before reaching required level"
112
}
113
# Special case just in case the runner is used on actions
114
if ($p[0].Name -eq "Runner.Worker.exe") {
115
Write-Host "Found Runner.Worker.exe process which means we are running on GitHub Actions"
116
Write-Host "Aborting search early and using process: $p"
117
Break
November 10, 2020 16:16
118
} elseif ($p[0].Name -eq "Agent.Worker.exe") {
119
Write-Host "Found Agent.Worker.exe process which means we are running on Azure Pipelines"
120
Write-Host "Aborting search early and using process: $p"
121
Break
122
} else {
123
$id = $p[0].ParentProcessId
124
}
125
}
September 7, 2020 17:10
126
Write-Host "Final process: $p"
127
128
Invoke-Expression "&$tracer --inject=$id"`;
August 25, 2020 16:19
129
}
September 14, 2020 10:44
130
const injectTracerPath = path.join(config.tempDir, "inject-tracer.ps1");
131
fs.writeFileSync(injectTracerPath, script);
November 20, 2020 11:35
132
await new toolrunner.ToolRunner(await safeWhich.safeWhich("powershell"), [
September 14, 2020 10:44
133
"-ExecutionPolicy",
134
"Bypass",
135
"-file",
136
injectTracerPath,
137
path.resolve(path.dirname(codeql.getPath()), "tools", "win64", "tracer.exe"),
138
], { env: { ODASA_TRACER_CONFIGURATION: tracerConfig.spec } }).exec();
August 25, 2020 16:19
139
}
140
exports.injectWindowsTracer = injectWindowsTracer;
September 11, 2020 10:53
141
async function installPythonDeps(codeql, logger) {
142
logger.startGroup("Setup Python dependencies");
143
const scriptsFolder = path.resolve(__dirname, "../python-setup");
144
try {
145
if (process.platform === "win32") {
146
await new toolrunner.ToolRunner(await safeWhich.safeWhich("powershell"), [
147
path.join(scriptsFolder, "install_tools.ps1"),
148
]).exec();
149
}
150
else {
151
await new toolrunner.ToolRunner(path.join(scriptsFolder, "install_tools.sh")).exec();
152
}
October 8, 2020 12:14
153
const script = "auto_install_packages.py";
154
if (process.platform === "win32") {
November 20, 2020 11:35
155
await new toolrunner.ToolRunner(await safeWhich.safeWhich("py"), [
October 8, 2020 12:14
156
"-3",
157
path.join(scriptsFolder, script),
158
path.dirname(codeql.getPath()),
159
]).exec();
160
}
161
else {
November 20, 2020 11:35
162
await new toolrunner.ToolRunner(path.join(scriptsFolder, script), [
October 8, 2020 12:14
163
path.dirname(codeql.getPath()),
164
]).exec();
165
}
September 11, 2020 10:53
166
}
167
catch (e) {
168
logger.endGroup();
169
logger.warning(`An error occurred while trying to automatically install Python dependencies: ${e}\n` +
170
"Please make sure any necessary dependencies are installed before calling the codeql-action/analyze " +
171
"step, and add a 'setup-python-dependencies: false' argument to this step to disable our automatic " +
172
"dependency installation and avoid this warning.");
October 8, 2020 12:06
173
return;
September 11, 2020 10:53
174
}
175
logger.endGroup();
176
}
177
exports.installPythonDeps = installPythonDeps;
August 25, 2020 16:19
178
//# sourceMappingURL=init.js.map