-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathCargoProjectManager.cs
More file actions
462 lines (408 loc) · 20.2 KB
/
Copy pathCargoProjectManager.cs
File metadata and controls
462 lines (408 loc) · 20.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
namespace Microsoft.CST.OpenSource.PackageManagers
{
using Extensions;
using Helpers;
using Microsoft.CST.OpenSource.Contracts;
using Microsoft.CST.OpenSource.Model;
using Microsoft.CST.OpenSource.PackageActions;
using Microsoft.CST.OpenSource.Utilities;
using Model.Enums;
using Octokit;
using PackageUrl;
using Polly.Retry;
using Polly;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml.Linq;
public class CargoProjectManager : TypedManager<IManagerPackageVersionMetadata, CargoProjectManager.CargoArtifactType>
{
/// <summary>
/// The type of the project manager from the package-url type specifications.
/// </summary>
/// <seealso href="https://www.github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst"/>
public const string Type = "cargo";
public override string ManagerType => Type;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
public string ENV_CARGO_ENDPOINT { get; set; } = "https://crates.io";
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
public string ENV_CARGO_ENDPOINT_STATIC { get; set; } = "https://static.crates.io";
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Modified through reflection.")]
public string ENV_CARGO_INDEX_ENDPOINT { get; set; } = "https://raw.githubusercontent.com/rust-lang/crates.io-index/master";
public bool allowUseOfRateLimitedRegistryAPIs = true;
private static int InternalServerErrorMaxRetries = 3;
private AsyncRetryPolicy retryPolicy = Policy
.Handle<HttpRequestException>(ex => ex.StatusCode == HttpStatusCode.InternalServerError)
.RetryAsync(InternalServerErrorMaxRetries, (exception, retryCount) =>
{
Logger.Debug(exception, "Internal Server error 500, retrying... {0}/{1} tries", retryCount, InternalServerErrorMaxRetries);
});
public CargoProjectManager(
string directory,
IManagerPackageActions<IManagerPackageVersionMetadata>? actions = null,
IHttpClientFactory? httpClientFactory = null,
TimeSpan? timeout = null,
bool allowUseOfRateLimitedRegistryAPIs = true)
: base(actions ?? new NoOpPackageActions(), httpClientFactory ?? new DefaultHttpClientFactory(), directory, timeout)
{
this.allowUseOfRateLimitedRegistryAPIs = allowUseOfRateLimitedRegistryAPIs;
}
/// <inheritdoc />
public override async IAsyncEnumerable<ArtifactUri<CargoArtifactType>> GetArtifactDownloadUrisAsync(PackageURL purl, bool useCache = true)
{
Check.NotNull(nameof(purl.Version), purl.Version);
string? packageName = purl?.Name;
string? packageVersion = purl?.Version;
string artifactUri = $"{ENV_CARGO_ENDPOINT_STATIC}/crates/{packageName}/{packageVersion}/download";
yield return new ArtifactUri<CargoArtifactType>(CargoArtifactType.Tarball, artifactUri);
}
/// <inheritdoc />
public override async IAsyncEnumerable<PackageURL> GetPackagesFromOwnerAsync(string owner, bool useCache = true)
{
// Packages by owner is not currently supported for Cargo, so an empty list is returned.
yield break;
}
/// <summary>
/// Download one Cargo package and extract it to the target directory.
/// </summary>
/// <param name="purl"> Package URL of the package to download. </param>
/// <returns> Path to the downloaded package </returns>
public override async Task<IEnumerable<string>> DownloadVersionAsync(PackageURL purl, bool doExtract, bool cached = false)
{
Logger.Trace("DownloadVersion {0}", purl?.ToString());
string? packageName = purl?.Name;
string? packageVersion = purl?.Version;
string? fileName = purl?.ToStringFilename();
List<string> downloadedPaths = new();
if (string.IsNullOrWhiteSpace(packageName) || string.IsNullOrWhiteSpace(packageVersion) || string.IsNullOrWhiteSpace(fileName))
{
Logger.Debug("Error with 'purl' argument. Unable to download [{0} {1}] @ {2}. Both must be defined.", packageName, packageVersion, fileName);
return downloadedPaths;
}
Uri url = (await GetArtifactDownloadUrisAsync(purl, cached).ToListAsync()).Single().Uri;
try
{
await retryPolicy.ExecuteAsync(async () =>
{
string targetName = $"cargo-{fileName}";
string extractionPath = Path.Combine(TopLevelExtractionDirectory, targetName);
// if the cache is already present, no need to extract
if (doExtract && cached && Directory.Exists(extractionPath))
{
downloadedPaths.Add(extractionPath);
return;
}
Logger.Debug("Downloading {0}", url);
HttpClient httpClient = CreateHttpClient();
System.Net.Http.HttpResponseMessage result = await httpClient.GetAsync(url);
result.EnsureSuccessStatusCode();
if (doExtract)
{
downloadedPaths.Add(await ArchiveHelper.ExtractArchiveAsync(TopLevelExtractionDirectory, targetName, await result.Content.ReadAsStreamAsync(), cached));
}
else
{
extractionPath += Path.GetExtension(url.ToString()) ?? "";
await File.WriteAllBytesAsync(extractionPath, await result.Content.ReadAsByteArrayAsync());
downloadedPaths.Add(extractionPath);
}
});
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.InternalServerError)
{
Logger.Debug($"Max retries reached. Unable to download the package: {purl}, error: {ex.Message}");
}
catch (Exception ex)
{
Logger.Debug(ex, "Error downloading Cargo package: {0}", ex.Message);
}
return downloadedPaths;
}
/// <inheritdoc />
public override async Task<bool> PackageExistsAsync(PackageURL purl, bool useCache = true)
{
Logger.Trace("PackageExists {0}", purl?.ToString());
if (string.IsNullOrEmpty(purl?.Name))
{
Logger.Trace("Provided PackageURL was null.");
return false;
}
string packageName = purl.Name;
HttpClient httpClient = CreateHttpClient();
// NOTE: The file isn't valid json, so use the custom rule.
return await CheckJsonCacheForPackage(httpClient, $"{ENV_CARGO_INDEX_ENDPOINT}/{CreatePath(packageName)}", useCache: useCache, jsonParsingOption: JsonParsingOption.NotInArrayNotCsv);
}
/// <inheritdoc />
public override async Task<IEnumerable<string>> EnumerateVersionsAsync(PackageURL purl, bool useCache = true, bool includePrerelease = true)
{
Logger.Trace("EnumerateVersions {0}", purl?.ToString());
if (purl == null || purl.Name is null)
{
return new List<string>();
}
try
{
return await retryPolicy.ExecuteAsync(async () =>
{
string? packageName = purl.Name;
HttpClient httpClient = CreateHttpClient();
// NOTE: The file isn't valid json, so use the custom rule.
string cargoUrl = $"{ENV_CARGO_INDEX_ENDPOINT}/{CreatePath(packageName)}";
JsonDocument doc = await GetJsonCache(httpClient, cargoUrl, useCache: useCache, jsonParsingOption: JsonParsingOption.NotInArrayNotCsv);
List<string> versionList = new();
foreach (JsonElement versionObject in doc.RootElement.EnumerateArray())
{
if (versionObject.TryGetProperty("vers", out JsonElement version))
{
Logger.Debug("Identified {0} version {1}.", packageName, version.ToString());
if (version.ToString() is string s)
{
versionList.Add(s);
}
}
}
return SortVersions(versionList.Distinct());
});
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.InternalServerError)
{
Logger.Debug($"Max retries reached. Unable to enumerate versions for package: {purl}, error: {ex.Message}");
throw;
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
Logger.Debug("Unable to enumerate versions (404): {0}", ex.Message);
return Array.Empty<string>();
}
catch (Exception ex)
{
Logger.Debug("Unable to enumerate versions: {0}", ex.Message);
throw;
}
}
/// <inheritdoc />
public override async Task<bool> PackageVersionExistsAsync(PackageURL purl, bool useCache = true)
{
Logger.Trace("PackageExists {0}", purl?.ToString());
if (purl is null)
{
Logger.Trace("Provided PackageURL was null.");
return false;
}
if (purl.Version.IsBlank())
{
Logger.Trace("Provided PackageURL version was null or blank.");
return false;
}
bool existsInStaticEndpoint = await CheckVersionExistsInStaticEndpoint(purl, useCache);
if (existsInStaticEndpoint)
{
return true;
}
return (await EnumerateVersionsAsync(purl, useCache)).Contains(purl.Version);
}
private async Task<bool> CheckVersionExistsInStaticEndpoint(PackageURL purl, bool useCache = true)
{
if (purl == null || purl.Name is null || purl.Version.IsBlank())
{
return false;
}
string packageName = purl.Name;
string requestedVersion = purl.Version;
HttpClient httpClient = CreateHttpClient();
string rssFeedUrl = $"{ENV_CARGO_ENDPOINT_STATIC}/rss/crates/{packageName}.xml";
try
{
string? rssContent = await GetHttpStringCache(httpClient, rssFeedUrl, useCache);
if (string.IsNullOrEmpty(rssContent))
{
return false;
}
XDocument doc = XDocument.Parse(rssContent);
XNamespace cratesNs = "https://crates.io/";
return doc.Descendants("item").Any(item =>
item.Element(cratesNs + "name")?.Value == packageName &&
item.Element(cratesNs + "version")?.Value == requestedVersion);
}
catch (Exception ex)
{
Logger.Debug($"Error checking version in RSS feed: {ex.Message}. Falling back to raw github endpoint.");
return false;
}
}
/// <summary>
/// This method uses the Crates.io rate-limited API to get the metadata for a package. It should be used when the request volume is low (1 request per second).
/// </summary>
public override async Task<string?> GetMetadataAsync(PackageURL purl, bool useCache = true)
{
if (!allowUseOfRateLimitedRegistryAPIs)
{
throw new InvalidOperationException("Rate-limited API is disabled. Crates.io does not have a non-rate-limited API defined to fetch metadata. See https://crates.io/data-access.");
}
try
{
return await retryPolicy.ExecuteAsync(async () =>
{
string? packageName = purl.Name;
HttpClient httpClient = CreateHttpClient();
string? content = await GetHttpStringCache(httpClient, $"{ENV_CARGO_ENDPOINT}/api/v1/crates/{packageName}", useCache);
return content;
});
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.InternalServerError)
{
Logger.Debug($"Max retries reached. Unable to get metadata for package: {purl}, error: {ex.Message}");
throw;
}
catch (Exception ex)
{
Logger.Debug(ex, "Error fetching Cargo metadata: {0}", ex.Message);
throw;
}
}
/// <summary>
/// This method uses the Crates.io rate-limited API to get the metadata for a package. It should be used when the request volume is low (1 request per second).
/// </summary>
public override async Task<PackageMetadata?> GetPackageMetadataAsync(PackageURL purl, bool includePrerelease = false, bool useCache = true, bool includeRepositoryMetadata = true)
{
if (!allowUseOfRateLimitedRegistryAPIs)
{
throw new InvalidOperationException("Rate-limited API is disabled. Crates.io does not have a non-rate-limited API defined to fetch metadata. See https://crates.io/data-access.");
}
string? content = await GetMetadataAsync(purl, useCache);
if (string.IsNullOrEmpty(content)) { return null; }
PackageMetadata metadata = new();
metadata.Name = purl?.Name;
metadata.PackageVersion = purl?.Version;
metadata.PackageManagerUri = ENV_CARGO_ENDPOINT.EnsureTrailingSlash();
metadata.Platform = "Cargo";
metadata.Language = "Rust";
JsonDocument contentJSON = JsonDocument.Parse(content);
JsonElement root = contentJSON.RootElement;
JsonElement? crateElement = OssUtilities.GetJSONPropertyIfExists(root, "crate");
if (crateElement != null)
{
if (OssUtilities.GetJSONPropertyStringIfExists(crateElement, "description") is string description &&
!string.IsNullOrWhiteSpace(description))
{
metadata.Description = description;
}
if (OssUtilities.GetJSONPropertyStringIfExists(crateElement, "newest_version") is string newestVersion &&
!string.IsNullOrWhiteSpace(newestVersion))
{
metadata.LatestPackageVersion = newestVersion;
}
}
JsonElement.ArrayEnumerator? versionsListElement = OssUtilities.GetJSONEnumerator(OssUtilities.GetJSONPropertyIfExists(root, "versions"));
if (versionsListElement != null)
{
JsonElement targetVersionElement = versionsListElement.Value
.Where(versionElement =>
{
return OssUtilities.GetJSONPropertyStringIfExists(versionElement, "num") is string versionNumber && versionNumber == purl?.Version;
}).SingleOrDefault();
// The specified version does not exist. Return null for the method.
if (targetVersionElement.Equals(default(JsonElement)))
{
return null;
}
if (OssUtilities.GetJSONPropertyStringIfExists(targetVersionElement, "created_at") is string createdAtString &&
!string.IsNullOrWhiteSpace(createdAtString) && DateTime.TryParse(createdAtString, out DateTime createdAtDate))
{
metadata.UploadTime = createdAtDate;
}
}
return metadata;
}
public override Uri GetPackageAbsoluteUri(PackageURL purl)
{
string? packageName = purl?.Name;
string? packageVersion = purl?.Version;
string url = $"{ENV_CARGO_ENDPOINT}/crates/{packageName}";
if (packageVersion.IsNotBlank())
{
url += $"/{packageVersion}";
}
return new Uri(url);
}
public override async Task<DateTime?> GetPublishedAtUtcAsync(PackageURL purl, bool useCache = true)
{
DateTime? uploadTime = await GetPackageVersionPublishedTimestampFromRssFeed(purl, useCache);
if (uploadTime == null && allowUseOfRateLimitedRegistryAPIs)
{
uploadTime = (await GetPackageMetadataAsync(purl, useCache, includeRepositoryMetadata: false))?.UploadTime;
}
return uploadTime?.ToUniversalTime();
}
private async Task<DateTime?> GetPackageVersionPublishedTimestampFromRssFeed(PackageURL purl, bool useCache = true)
{
try
{
string? packageName = purl.Name;
HttpClient httpClient = CreateHttpClient();
// The Rss feed fetches package versions published in the last 24 hours.
string? rssFeedContent = await GetHttpStringCache(httpClient, $"{ENV_CARGO_ENDPOINT_STATIC}/rss/crates/{packageName}.xml", useCache);
if (rssFeedContent == null)
{
return null;
}
XDocument doc = XDocument.Parse(rssFeedContent);
List<XElement> items = doc.Descendants("item").ToList();
foreach (XElement item in items)
{
string? pubDate = item.Elements().FirstOrDefault(e => e.Name == "pubDate")?.Value;
string? crateName = item.Elements().FirstOrDefault(e => e.Name.LocalName == "name" && e.Name.NamespaceName == $"{ ENV_CARGO_ENDPOINT}/")?.Value;
string? crateVersion = item.Elements().FirstOrDefault(e => e.Name.NamespaceName == $"{ENV_CARGO_ENDPOINT}/" && e.Name.LocalName == "version")?.Value;
if (crateName == purl.Name && crateVersion == purl.Version)
{
return pubDate != null ? DateTime.Parse(pubDate) : null;
}
}
return null;
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
Logger.Debug($"RSS feed not found for package: {purl}, error: {ex.Message}");
return null;
}
}
/// <summary>
/// Helper method to create the path for the crates.io index for this package name.
/// </summary>
/// <param name="crateName">The name of this package.</param>
/// <returns>The path to the package.</returns>
/// <example>
/// rand -> ra/nd/rand<br/>
/// go -> 2/go<br/>
/// who -> 3/w/who<br/>
/// spotify-retro -> sp/ot/spotify-retro
/// </example>
public static string CreatePath(string crateName)
{
switch (crateName.Length)
{
case 0:
return string.Empty;
case 1:
return $"1/{crateName}";
case 2:
return $"2/{crateName}";
case 3:
return $"3/{crateName[0]}/{crateName}";
default:
return $"{crateName[..2]}/{crateName[2..4]}/{crateName}";
}
}
public enum CargoArtifactType
{
Unknown = 0,
Tarball,
}
}
}