-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCodeQLInstallation.cs
More file actions
444 lines (355 loc) · 17.2 KB
/
Copy pathCodeQLInstallation.cs
File metadata and controls
444 lines (355 loc) · 17.2 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
using System.Net;
using CodeQLToolkit.Shared.Utils;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using CodeQLToolkit.Shared.Logging;
using LibGit2Sharp;
using System.IO.Compression;
using System.Diagnostics;
using System;
namespace CodeQLToolkit.Shared.CodeQL
{
public class CodeQLInstallation
{
public string CLIVersion { get; set; }
public string StandardLibraryVersion { get; set; }
public string CodeScanningConfig { get; set; }
public string QLTConfigFilePath { get; private set; }
public string CLIBundle { get; set; }
public string StandardLibraryIdent { get; set; }
public bool EnableCustomCodeQLBundles { get; set; }
public CodeQLPackConfiguration[] CodeQLPackConfiguration { get; set; }
public bool QuickBundle { get; set; }
public string Base { get; set; }
// Optional directory passed to the bundle tool via `--cache-dir`, used
// for downloaded bundles and compilation caches.
public string CacheDir { get; set; }
public static CodeQLInstallation LoadFromConfig(string Base)
{
var c = new QLTConfig()
{
Base = Base
};
return LoadFromConfig(c.FromFile());
}
public static CodeQLInstallation LoadFromConfig(QLTConfig c)
{
var config = c.FromFile();
return new CodeQLInstallation
{
CLIVersion = config.CodeQLCLI,
CLIBundle = config.CodeQLCLIBundle,
StandardLibraryIdent = config.CodeQLStandardLibraryIdent,
StandardLibraryVersion = config.CodeQLStandardLibrary,
CodeQLPackConfiguration = config.CodeQLPackConfiguration,
Base = config.Base,
CodeScanningConfig = config.CodeQLConfiguration,
CacheDir = config.CacheDir,
QLTConfigFilePath = config.QLTConfigFilePath
};
}
public void LogPacksToBeBuilt()
{
if (CodeQLPackConfiguration != null)
{
foreach (var p in CodeQLPackConfiguration)
{
Log<CodeQLInstallation>.G().LogInformation($"Pack: {p}");
}
}
}
public ArtifactKind Kind
{
get
{
if (EnableCustomCodeQLBundles)
{
return ArtifactKind.CUSTOM_BUNDLE;
}
// TODO - here we only support the kind
// we package together however this can be extended
// to support the precompiled packages.
return ArtifactKind.PACKAGE;
}
}
public string PlatformID
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return "win64";
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return RuntimeInformation.OSArchitecture switch
{
Architecture.X64 => "linux64",
Architecture.Arm64 => "linux-arm64",
_ => throw new PlatformNotSupportedException($"Unsupported Linux architecture: {RuntimeInformation.OSArchitecture}.")
};
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return "osx64";
}
throw new Exception("Unknown platform.");
}
}
public string PlatformExtension => "zip";
public void Install()
{
// each time we download the file; however,
// .qlt/repo/packages/ident
// .qlt/repo/custom-bundle/ident
// .qlt/repo/bundle/ident
// https://github.com/github/codeql-cli-binaries/releases/download/v2.16.0/codeql-linux64.zip
// workout a destination directory for this
if (Kind == ArtifactKind.CUSTOM_BUNDLE)
{
CustomBundleInstall();
}
else if (Kind == ArtifactKind.BUNDLE)
{
BundleInstall();
}
else
{
PackageInstall();
}
}
public string CustomBundleOutputBundleCurrentPlatform => Path.Combine(CustomBundleOutputDirectory, $"codeql-bundle-{PlatformID}.tar.gz");
public string CustomBundleOutputBundleLinux => Path.Combine(CustomBundleOutputDirectory, "codeql-bundle-linux64.tar.gz");
public string CustomBundleOutputBundleWindows => Path.Combine(CustomBundleOutputDirectory, "codeql-bundle-win64.tar.gz");
public string CustomBundleOutputBundleOSX => Path.Combine(CustomBundleOutputDirectory, "codeql-bundle-osx64.tar.gz");
public string CustomBundleOutputDirectory => Path.Combine(InstallationDirectory, "out");
public string StdLibDirectory => Path.Combine(InstallationDirectory, "codeql-stdlib");
public string CodeQLDirectory => Path.Combine(InstallationDirectory, "codeql");
private void PackageInstall()
{
Log<CodeQLInstallation>.G().LogInformation($"Begin Installation ");
Log<CodeQLInstallation>.G().LogInformation($"Requested CLI Version {CLIVersion}, Standard Library Ident: {StandardLibraryIdent}");
Log<CodeQLInstallation>.G().LogInformation($"Create installation directory {InstallationDirectory}");
Directory.CreateDirectory(InstallationDirectory);
Log<CodeQLInstallation>.G().LogInformation($"Download CodeQL CLI...");
var downloadFile = $"codeql-{PlatformID}.{PlatformExtension}";
// first, download the cli.
using (var client = new WebClient())
{
string uri = $"https://github.com/github/codeql-cli-binaries/releases/download/v{CLIVersion}/{downloadFile}";
Log<CodeQLInstallation>.G().LogInformation($"Remote URL: {uri}...");
client.DownloadFile(uri, Path.Combine(InstallationDirectory, downloadFile));
}
// unpack
Log<CodeQLInstallation>.G().LogInformation($"Unpacking distribution...");
ZipFile.ExtractToDirectory(Path.Combine(InstallationDirectory, downloadFile), InstallationDirectory);
Log<CodeQLInstallation>.G().LogInformation($"Done.");
Log<CodeQLInstallation>.G().LogInformation($"Checkout standard library into.. {StdLibDirectory}");
var repoPath = Repository.Clone("https://github.com/github/codeql.git", StdLibDirectory);
Log<CodeQLInstallation>.G().LogInformation($"Getting standard library version.. {StandardLibraryVersion}");
using (var repo = new Repository(repoPath))
{
var tag = repo.Tags[$"refs/tags/{StandardLibraryVersion}"];
if (tag == null)
{
Log<CodeQLInstallation>.G().LogInformation($"Unknown standard library version: {StandardLibraryVersion}");
throw new Exception($"Unknown standard library version: {StandardLibraryVersion}");
}
Branch b = Commands.Checkout(repo, $"refs/tags/{StandardLibraryVersion}");
}
}
private void CustomBundleInstall()
{
Log<CodeQLInstallation>.G().LogInformation($"Begin Installation");
Log<CodeQLInstallation>.G().LogInformation($"Requested Base Bundle Version {CLIBundle}");
Log<CodeQLInstallation>.G().LogInformation($"Create installation directory {InstallationDirectory}");
if (!Directory.Exists(InstallationDirectory))
{
Directory.CreateDirectory(InstallationDirectory);
}
Log<CodeQLInstallation>.G().LogInformation($"Downloading CodeQL base bundle...");
var downloadFile = $"codeql-bundle-{PlatformID}.tar.gz";
var customBundlePath = Path.Combine(InstallationDirectory, downloadFile);
Log<CodeQLInstallation>.G().LogInformation($"Checking if existing source bundle {downloadFile} is present...");
if (File.Exists(customBundlePath))
{
Log<CodeQLInstallation>.G().LogInformation($"Bundle exists, will skip download");
}
else
{
using var client = new WebClient();
string uri = $"https://github.com/github/codeql-action/releases/download/{CLIBundle}/{downloadFile}";
Log<CodeQLInstallation>.G().LogInformation($"Downloading platform-specific bundle from remote URL: {uri}...");
client.DownloadFile(uri, customBundlePath);
}
// Create the output directory that will contain the custom bundles.
// If it already exists, delete it and recreate it.
Log<CodeQLInstallation>.G().LogInformation($"Checking for custom bundle output directory...");
if (Directory.Exists(CustomBundleOutputDirectory))
{
Log<CodeQLInstallation>.G().LogInformation($"Exists. Removing...");
Directory.Delete(CustomBundleOutputDirectory, true);
Directory.CreateDirectory(CustomBundleOutputDirectory);
}
else
{
Directory.CreateDirectory(CustomBundleOutputDirectory);
}
var workingDirectory = Path.GetFullPath(Base);
if (CodeQLPackConfiguration == null || CodeQLPackConfiguration.Length == 0)
{
throw new Exception("No packs are set to be exported. Please add at least one pack to export in your `qlt.conf.json` file under the property `ExportedCustomizationPacks`.");
}
Log<CodeQLInstallation>.G().LogInformation($"Building custom bundles. This may take a while...");
var packsToExport = CodeQLPackConfiguration.Where(p => p.Bundle == true).Select(p => p.Name).ToArray();
var packs = string.Join(" ", packsToExport);
var bundleArgs = $"--log DEBUG -a qlt.conf.json -p {PlatformID} -b {customBundlePath} -o {CustomBundleOutputDirectory} -w {workingDirectory} {packs}";
if (QuickBundle)
{
Log<CodeQLInstallation>.G().LogInformation($"Note: Quick Bundles enabled and pre-compilation will be disabled...");
bundleArgs = $"-nc {bundleArgs}";
}
if (CodeScanningConfig != null && CodeScanningConfig.Length > 0)
{
Log<CodeQLInstallation>.G().LogInformation($"Note: Attempting to include default code scanning configuration ...");
if (File.Exists(Path.Combine(Base, CodeScanningConfig)))
{
bundleArgs = $"-c \"{Path.Combine(Base, CodeScanningConfig)}\" {bundleArgs}";
}
}
if (CacheDir != null && CacheDir.Length > 0)
{
Log<CodeQLInstallation>.G().LogInformation($"Note: Using bundle cache directory `{CacheDir}` ...");
bundleArgs = $"--cache-dir \"{Path.GetFullPath(CacheDir)}\" {bundleArgs}";
}
Log<CodeQLInstallation>.G().LogInformation($"Executing Bundle Tool with Working Directory: `{workingDirectory}`");
Log<CodeQLInstallation>.G().LogInformation($"Executing Bundle Tool with Arguments: `{bundleArgs}`");
using (Process process = new Process())
{
process.StartInfo.FileName = ToolUtil.GetTool("codeql_bundle");
process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.RedirectStandardError = false;
if (QuickBundle)
{
Log<CodeQLInstallation>.G().LogInformation($"Note: Quick Bundles enabled and pre-compilation will be disabled...");
process.StartInfo.Arguments = bundleArgs;
}
else
{
process.StartInfo.Arguments = bundleArgs;
}
process.Start();
process.WaitForExit();
if (process.ExitCode != 0)
{
Log<CodeQLInstallation>.G().LogInformation($"Failure running bundle command.");
throw new Exception("Failure running bundle command.");
}
}
// Expand the correct platform-specific archive that is generated by the bundle command to `InstallationDirectory`
Log<CodeQLInstallation>.G().LogInformation($"Checking for existence of {CodeQLDirectory}");
if (Directory.Exists(CodeQLDirectory))
{
Log<CodeQLInstallation>.G().LogInformation($"Exists. Will delete {CodeQLDirectory}");
Directory.Delete(CodeQLDirectory, true);
}
Log<CodeQLInstallation>.G().LogInformation($"Extracting bundle to {InstallationDirectory}...");
using (Process process = new Process())
{
process.StartInfo.FileName = ToolUtil.GetCommand("tar");
process.StartInfo.WorkingDirectory = InstallationDirectory;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.RedirectStandardError = false;
process.StartInfo.Arguments = $"-zxf {CustomBundleOutputBundleCurrentPlatform} ";
process.Start();
process.WaitForExit();
if (process.ExitCode != 0)
{
Log<CodeQLInstallation>.G().LogInformation($"Failure running extract bundle command.");
throw new Exception("Failure running extract bundle command.");
}
}
Log<CodeQLInstallation>.G().LogInformation($"Done.");
}
private void BundleInstall()
{
throw new NotImplementedException();
}
private string GetIdentForPackage(ArtifactKind k)
{
if (k == ArtifactKind.PACKAGE)
{
var ident = String.Join("", "codeql-cli-" + CLIVersion, "#standard-library-ident-", StandardLibraryIdent);
return StringUtils.CreateMD5(FileUtils.SanitizeFilename(ident)).ToLower();
}
if (k == ArtifactKind.CUSTOM_BUNDLE)
{
var ident = String.Join("", "codeql-bundle-" + CLIBundle);
return StringUtils.CreateMD5(FileUtils.SanitizeFilename(ident)).ToLower();
}
throw new NotImplementedException();
}
public bool IsInstalledOrDie()
{
var installed = IsInstalled();
if (!installed)
{
ProcessUtils.DieWithError("CodeQL not installed. Please run `qlt codeql run install --help` for more info.");
}
return true;
}
public bool IsInstalled()
{
Log<CodeQLInstallation>.G().LogInformation($"Checking if codeql is installed...");
if (EnableCustomCodeQLBundles)
{
Log<CodeQLInstallation>.G().LogInformation($"Requested Custom Bundle Based on Bundle {CLIBundle}");
}
else
{
Log<CodeQLInstallation>.G().LogInformation($"Requested CLI Version {CLIVersion} with Standard Library Ident: {StandardLibraryIdent}");
}
Log<CodeQLInstallation>.G().LogInformation($"Checking for existance of required directories...");
if (!Directory.Exists(InstallationDirectory))
{
Log<CodeQLInstallation>.G().LogInformation($"Installation directory {InstallationDirectory} is missing.");
return false;
}
if (!Directory.Exists(CodeQLDirectory))
{
Log<CodeQLInstallation>.G().LogInformation($"CodeQL Directory Missing: {CodeQLDirectory}");
return false;
}
// custom bundles don't have a standard library directory.
if (!EnableCustomCodeQLBundles)
{
if (!Directory.Exists(StdLibDirectory))
{
Log<CodeQLInstallation>.G().LogInformation($"Standard Library Directory Missing: {StdLibDirectory}");
return false;
}
}
return true;
}
public string InstallationDirectory => GetInstallationDirectory(Kind);
public string GetInstallationDirectory(ArtifactKind k)
{
return InstallationRepository.DirectoryForVersion(k, GetIdentForPackage(k));
}
public string CodeQLHome => CodeQLDirectory;
public string CodeQLToolBinary
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Path.Combine(CodeQLDirectory, "codeql.exe");
}
return Path.Combine(CodeQLDirectory, "codeql");
}
}
}
}