This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPackageSignatureToCsvDriver.cs
More file actions
187 lines (162 loc) · 8.27 KB
/
PackageSignatureToCsvDriver.cs
File metadata and controls
187 lines (162 loc) · 8.27 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
// 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.
using System.Security.Cryptography;
using NuGet.Packaging.Signing;
namespace NuGet.Insights.Worker.PackageSignatureToCsv
{
public class PackageSignatureToCsvDriver : ICatalogLeafToCsvDriver<PackageSignature>, ICsvResultStorage<PackageSignature>
{
private readonly CatalogClient _catalogClient;
private readonly PackageFileService _packageFileService;
private readonly IOptions<NuGetInsightsWorkerSettings> _options;
public PackageSignatureToCsvDriver(
CatalogClient catalogClient,
PackageFileService packageFileService,
IOptions<NuGetInsightsWorkerSettings> options)
{
_catalogClient = catalogClient;
_packageFileService = packageFileService;
_options = options;
}
public string ResultContainerName => _options.Value.PackageSignatureContainerName;
public bool SingleMessagePerId => false;
public async Task InitializeAsync()
{
await _packageFileService.InitializeAsync();
}
public Task DestroyAsync()
{
return Task.CompletedTask;
}
public async Task<DriverResult<IReadOnlyList<PackageSignature>>> ProcessLeafAsync(CatalogLeafScan leafScan)
{
var records = await ProcessLeafInternalAsync(leafScan);
return DriverResult.Success(records);
}
private async Task<IReadOnlyList<PackageSignature>> ProcessLeafInternalAsync(CatalogLeafScan leafScan)
{
var scanId = Guid.NewGuid();
var scanTimestamp = DateTimeOffset.UtcNow;
if (leafScan.LeafType == CatalogLeafType.PackageDelete)
{
var leaf = (PackageDeleteCatalogLeaf)await _catalogClient.GetCatalogLeafAsync(leafScan.LeafType, leafScan.Url);
return new List<PackageSignature> { new PackageSignature(scanId, scanTimestamp, leaf) };
}
else
{
var leaf = (PackageDetailsCatalogLeaf)await _catalogClient.GetCatalogLeafAsync(leafScan.LeafType, leafScan.Url);
var primarySignature = await _packageFileService.GetPrimarySignatureAsync(leafScan.ToPackageIdentityCommit());
if (primarySignature == null)
{
// Ignore packages where the .nupkg is missing. A subsequent scan will produce a deleted record.
return new List<PackageSignature>();
}
var output = new PackageSignature(scanId, scanTimestamp, leaf)
{
HashAlgorithm = primarySignature.SignatureContent.HashAlgorithm,
HashValue = primarySignature.SignatureContent.HashValue,
};
if (primarySignature.Type == SignatureType.Author)
{
ApplyAuthorSignature(output, primarySignature);
ApplyRepositorySignature(output, RepositoryCountersignature.GetRepositoryCountersignature(primarySignature));
}
else if (primarySignature.Type == SignatureType.Repository)
{
ApplyRepositorySignature(output, (RepositoryPrimarySignature)primarySignature);
}
else
{
throw new NotSupportedException();
}
return new List<PackageSignature> { output };
}
}
private void ApplyAuthorSignature(PackageSignature output, Signature signature)
{
var info = GetInfo(signature);
output.AuthorSHA1 = info.SHA1;
output.AuthorSHA256 = info.SHA256;
output.AuthorSubject = info.Subject;
output.AuthorNotBefore = info.NotBefore;
output.AuthorNotAfter = info.NotAfter;
output.AuthorIssuer = info.Issuer;
output.AuthorTimestampSHA1 = info.TimestampSHA1;
output.AuthorTimestampSHA256 = info.TimestampSHA256;
output.AuthorTimestampSubject = info.TimestampSubject;
output.AuthorTimestampNotBefore = info.TimestampNotBefore;
output.AuthorTimestampNotAfter = info.TimestampNotAfter;
output.AuthorTimestampIssuer = info.TimestampIssuer;
output.AuthorTimestampValue = info.TimestampValue;
output.AuthorTimestampHasASN1Error = info.TimestampHasASN1Error;
}
private void ApplyRepositorySignature<T>(PackageSignature output, T signature) where T : Signature, IRepositorySignature
{
var info = GetInfo(signature);
output.RepositorySHA1 = info.SHA1;
output.RepositorySHA256 = info.SHA256;
output.RepositorySubject = info.Subject;
output.RepositoryNotBefore = info.NotBefore;
output.RepositoryNotAfter = info.NotAfter;
output.RepositoryIssuer = info.Issuer;
output.RepositoryTimestampSHA1 = info.TimestampSHA1;
output.RepositoryTimestampSHA256 = info.TimestampSHA256;
output.RepositoryTimestampSubject = info.TimestampSubject;
output.RepositoryTimestampNotBefore = info.TimestampNotBefore;
output.RepositoryTimestampNotAfter = info.TimestampNotAfter;
output.RepositoryTimestampIssuer = info.TimestampIssuer;
output.RepositoryTimestampValue = info.TimestampValue;
output.RepositoryTimestampHasASN1Error = info.TimestampHasASN1Error;
output.PackageOwners = KustoDynamicSerializer.Serialize(signature.PackageOwners);
}
private SignatureInfo GetInfo(Signature signature)
{
Timestamp timestamp;
bool timestampHasASN1Error;
try
{
timestamp = signature.Timestamps.Single();
timestampHasASN1Error = false;
}
catch (CryptographicException ex) when (ex.IsInvalidDataException())
{
timestamp = null;
timestampHasASN1Error = true;
}
return new SignatureInfo
{
TimestampHasASN1Error = timestampHasASN1Error,
SHA1 = signature.SignerInfo.Certificate.GetSHA1HexFingerprint(),
SHA256 = signature.SignerInfo.Certificate.GetSHA256HexFingerprint(),
Subject = signature.SignerInfo.Certificate.GetSubjectXplat(),
NotBefore = signature.SignerInfo.Certificate.NotBefore.ToUniversalTime(),
NotAfter = signature.SignerInfo.Certificate.NotAfter.ToUniversalTime(),
Issuer = signature.SignerInfo.Certificate.GetIssuerXplat(),
TimestampSHA1 = timestamp?.SignerInfo.Certificate.GetSHA1HexFingerprint(),
TimestampSHA256 = timestamp != null ? timestamp.SignerInfo.Certificate.GetSHA256HexFingerprint() : null,
TimestampSubject = timestamp?.SignerInfo.Certificate.GetSubjectXplat(),
TimestampNotBefore = timestamp?.SignerInfo.Certificate.NotBefore.ToUniversalTime(),
TimestampNotAfter = timestamp?.SignerInfo.Certificate.NotAfter.ToUniversalTime(),
TimestampIssuer = timestamp?.SignerInfo.Certificate.GetIssuerXplat(),
TimestampValue = timestamp?.GeneralizedTime.ToUniversalTime(),
};
}
private record SignatureInfo
{
public bool TimestampHasASN1Error { get; init; }
public string SHA1 { get; init; }
public string SHA256 { get; init; }
public string Subject { get; init; }
public DateTimeOffset NotBefore { get; init; }
public DateTimeOffset NotAfter { get; init; }
public string Issuer { get; init; }
public string TimestampSHA1 { get; init; }
public string TimestampSHA256 { get; init; }
public string TimestampSubject { get; init; }
public DateTimeOffset? TimestampNotBefore { get; init; }
public DateTimeOffset? TimestampNotAfter { get; init; }
public string TimestampIssuer { get; init; }
public DateTimeOffset? TimestampValue { get; init; }
}
}
}