-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathRepositorySignatureResourceProvider.cs
More file actions
127 lines (111 loc) · 5.31 KB
/
RepositorySignatureResourceProvider.cs
File metadata and controls
127 lines (111 loc) · 5.31 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable disable
using System;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.Model;
using NuGet.Protocol.Utility;
namespace NuGet.Protocol
{
public class RepositorySignatureResourceProvider : ResourceProvider
{
public RepositorySignatureResourceProvider()
: base(typeof(RepositorySignatureResource),
nameof(RepositorySignatureResource),
NuGetResourceProviderPositions.Last)
{
}
public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceRepository source, CancellationToken token)
{
RepositorySignatureResource resource = null;
var serviceIndex = await source.GetResourceAsync<ServiceIndexResourceV3>(token);
if (serviceIndex != null)
{
var serviceEntry = serviceIndex.GetServiceEntries(ServiceTypes.RepositorySignatures).FirstOrDefault();
if (serviceEntry != null)
{
resource = await GetRepositorySignatureResourceAsync(source, serviceEntry, NullLogger.Instance, token);
}
}
return new Tuple<bool, INuGetResource>(resource != null, resource);
}
private async Task<RepositorySignatureResource> GetRepositorySignatureResourceAsync(
SourceRepository source,
ServiceIndexEntry serviceEntry,
ILogger log,
CancellationToken token)
{
var repositorySignaturesResourceUri = serviceEntry.Uri;
if (repositorySignaturesResourceUri == null
|| !string.Equals(repositorySignaturesResourceUri.Scheme, "https", StringComparison.OrdinalIgnoreCase)
&& !string.Equals(repositorySignaturesResourceUri.Scheme, "http", StringComparison.OrdinalIgnoreCase))
{
throw new FatalProtocolException(string.Format(CultureInfo.CurrentCulture, Strings.RepositorySignaturesResourceMustBeHttps, source.PackageSource.Source));
}
var httpSourceResource = await source.GetResourceAsync<HttpSourceResource>(token);
var client = httpSourceResource.HttpSource;
var cacheKey = GenerateCacheKey(serviceEntry);
const int maxRetries = 3;
for (var retry = 1; retry <= maxRetries; retry++)
{
using (var sourceCacheContext = new SourceCacheContext())
{
var cacheContext = HttpSourceCacheContext.Create(sourceCacheContext, isFirstAttempt: retry == 1);
try
{
return await client.GetAsync(
new HttpSourceCachedRequest(
serviceEntry.Uri.AbsoluteUri,
cacheKey,
cacheContext)
{
EnsureValidContents = stream => HttpStreamValidation.ValidateJObject(repositorySignaturesResourceUri.AbsoluteUri, stream),
MaxTries = 1,
IsRetry = retry > 1,
IsLastAttempt = retry == maxRetries
},
async httpSourceResult =>
{
RepositorySignatureModel model = await JsonSerializer.DeserializeAsync(
httpSourceResult.Stream,
JsonContext.Default.RepositorySignatureModel,
token);
return new RepositorySignatureResource(model, source);
},
log,
token);
}
catch (Exception ex) when (retry < maxRetries)
{
var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_RetryingRepositorySignature, repositorySignaturesResourceUri.AbsoluteUri)
+ Environment.NewLine
+ ExceptionUtilities.DisplayMessage(ex);
log.LogMinimal(message);
}
catch (Exception ex) when (retry == maxRetries)
{
var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToReadRepositorySignature, repositorySignaturesResourceUri.AbsoluteUri);
throw new FatalProtocolException(message, ex);
}
}
}
return null;
}
private static string GenerateCacheKey(ServiceIndexEntry serviceEntry)
{
#if NETCOREAPP
var index = serviceEntry.Type.IndexOf('/', StringComparison.Ordinal);
#else
var index = serviceEntry.Type.IndexOf('/');
#endif
var version = serviceEntry.Type.Substring(index + 1).Trim();
return $"repository_signatures_{version}";
}
}
}