-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakeGoDaddyClient.cs
More file actions
331 lines (274 loc) · 11.4 KB
/
FakeGoDaddyClient.cs
File metadata and controls
331 lines (274 loc) · 11.4 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
// Copyright 2026 Keyfactor
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Collections.Concurrent;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Keyfactor.Extensions.CAPlugin.GoDaddy.Client;
using Keyfactor.AnyGateway.Extensions;
using Keyfactor.Logging;
using Keyfactor.PKI.Enums.EJBCA;
using Microsoft.Extensions.Logging;
namespace Keyfactor.Extensions.CAPlugin.GoDaddyTests;
public class FakeGoDaddyClient : IGoDaddyClient
{
public class Builder : IGoDaddyClientBuilder
{
private string? _baseUrl { get; set; }
private string? _apiKey { get; set; }
private string? _apiSecret { get; set; }
private string? _shopperId { get; set; }
public IGoDaddyClientBuilder WithApiKey(string apiToken)
{
_apiKey = apiToken;
return this;
}
public IGoDaddyClientBuilder WithApiSecret(string apiSecret)
{
_apiSecret = apiSecret;
return this;
}
public IGoDaddyClientBuilder WithBaseUrl(string baseUrl)
{
_baseUrl = baseUrl;
return this;
}
public IGoDaddyClientBuilder WithShopperId(string shopperId) {
_shopperId = shopperId;
return this;
}
public IGoDaddyClient Build()
{
IGoDaddyClient client = new FakeGoDaddyClient();
return client;
}
}
public FakeGoDaddyClient()
{
EnrollmentNotBefore = DateTime.UtcNow;
EnrollmentNotAfter = DateTime.UtcNow.AddYears(1);
}
ILogger _logger = LogHandler.GetClassLogger<FakeGoDaddyClient>();
public Dictionary<string, AnyCAPluginCertificate>? CertificatesIssuedByFakeGoDaddy { get; set; }
public DateTime EnrollmentNotBefore;
public DateTime EnrollmentNotAfter;
bool _fakeClientIsEnabled = true;
public Task Enable()
{
_fakeClientIsEnabled = true;
return Task.CompletedTask;
}
public Task Disable()
{
_fakeClientIsEnabled = false;
return Task.CompletedTask;
}
public bool IsEnabled()
{
return _fakeClientIsEnabled;
}
public Task Ping()
{
return Task.CompletedTask;
}
public Task<string> DownloadCertificate(string certificateId)
{
throw new NotImplementedException();
}
public Task<int> DownloadAllIssuedCertificates(BlockingCollection<AnyCAPluginCertificate> certificatesBuffer, CancellationToken cancelToken)
{
_logger.LogDebug("Getting all issued certificates from Fake GoDaddy");
if (CertificatesIssuedByFakeGoDaddy == null)
{
throw new Exception("No certificates have been issued by Fake GoDaddy - no items set");
}
foreach (var cert in CertificatesIssuedByFakeGoDaddy)
{
certificatesBuffer.Add(cert.Value);
}
return Task.FromResult(CertificatesIssuedByFakeGoDaddy.Count);
}
public Task<CertificateDetailsRestResponse> GetCertificateDetails(string certificateId)
{
throw new NotImplementedException();
}
Task<AnyCAPluginCertificate> IGoDaddyClient.DownloadCertificate(string certificateId)
{
if (CertificatesIssuedByFakeGoDaddy == null)
{
throw new Exception("No certificates have been issued by Fake GoDaddy - no items set");
}
if (CertificatesIssuedByFakeGoDaddy.ContainsKey(certificateId))
return Task.FromResult(CertificatesIssuedByFakeGoDaddy[certificateId]);
throw new Exception($"Certificate with ID {certificateId} not found");
}
public Task<string> DownloadCertificatePem(string certificateId)
{
if (CertificatesIssuedByFakeGoDaddy == null)
{
throw new Exception("No certificates have been issued by Fake GoDaddy - no items set");
}
if (CertificatesIssuedByFakeGoDaddy.ContainsKey(certificateId))
return Task.FromResult(CertificatesIssuedByFakeGoDaddy[certificateId].Certificate);
throw new Exception($"Certificate with ID {certificateId} not found");
}
public Task RevokeCertificate(string certificateId, RevokeReason reason)
{
return Task.CompletedTask;
}
public Task CancelCertificateOrder(string certificateId)
{
throw new NotImplementedException();
}
public Task<EnrollmentResult> Enroll(CertificateOrderRestRequest request, CancellationToken cancelToken)
{
// Validate that request has the correct fields for the product type
switch (request.ProductType)
{
case "DV_SSL":
case "DV_WILDCARD_SSL":
case "UCC_DV_SSL":
Assert.NotNull(request.Contact);
Assert.NotNull(request.Contact.Email);
Assert.NotNull(request.Contact.NameFirst);
Assert.NotNull(request.Contact.NameLast);
Assert.NotNull(request.Contact.Phone);
Assert.NotNull(request.Csr);
Assert.NotNull(request.Period);
Assert.NotNull(request.SlotSize);
break;
case "OV_SSL":
case "OV_CS":
case "OV_DS":
case "OV_WILDCARD_SSL":
case "UCC_OV_SSL":
Assert.NotNull(request.Contact);
Assert.NotNull(request.Contact.Email);
Assert.NotNull(request.Contact.NameFirst);
Assert.NotNull(request.Contact.NameLast);
Assert.NotNull(request.Contact.Phone);
Assert.NotNull(request.Organization);
Assert.NotNull(request.Organization.Name);
Assert.NotNull(request.Organization.Address);
Assert.NotNull(request.Organization.Address.City);
Assert.NotNull(request.Organization.Address.State);
Assert.NotNull(request.Organization.Address.Country);
Assert.NotNull(request.Organization.Phone);
Assert.NotNull(request.Csr);
Assert.NotNull(request.Period);
Assert.NotNull(request.SlotSize);
break;
case "EV_SSL":
case "UCC_EV_SSL":
Assert.NotNull(request.Contact);
Assert.NotNull(request.Contact.Email);
Assert.NotNull(request.Contact.NameFirst);
Assert.NotNull(request.Contact.NameLast);
Assert.NotNull(request.Contact.Phone);
Assert.NotNull(request.Organization);
Assert.NotNull(request.Organization.Name);
Assert.NotNull(request.Organization.Address);
Assert.NotNull(request.Organization.Address.City);
Assert.NotNull(request.Organization.Address.State);
Assert.NotNull(request.Organization.Address.Country);
Assert.NotNull(request.Organization.Phone);
Assert.NotNull(request.Organization.RegistrationAgent);
Assert.NotNull(request.Organization.RegistrationNumber);
Assert.NotNull(request.Contact.JobTitle);
Assert.NotNull(request.Csr);
Assert.NotNull(request.Period);
Assert.NotNull(request.SlotSize);
break;
default:
_logger.LogError($"Invalid product type: {request.ProductType}");
throw new ArgumentException($"Invalid product type: {request.ProductType}");
}
_logger.LogInformation("Enrolling certificate with Fake GoDaddy");
X509Certificate2 cert = SignFakeCsr(request.Csr);
_logger.LogDebug("Preparing EnrollmentResult object");
EnrollmentResult result = new EnrollmentResult
{
CARequestID = Guid.NewGuid().ToString(),
Status = (int)EndEntityStatus.GENERATED,
StatusMessage = "Certificate generated successfully",
Certificate = cert.ExportCertificatePem()
};
return Task.FromResult(result);
}
public Task<EnrollmentResult> Reissue(string certificateId, ReissueCertificateRestRequest request, CancellationToken cancelToken)
{
_logger.LogInformation("Reissuing certificate with Fake GoDaddy");
X509Certificate2 cert = SignFakeCsr(request.Csr);
_logger.LogDebug("Preparing EnrollmentResult object");
EnrollmentResult result = new EnrollmentResult
{
CARequestID = certificateId,
Status = (int)EndEntityStatus.GENERATED,
StatusMessage = $"Certificate with ID {certificateId} has been reissued",
Certificate = cert.ExportCertificatePem()
};
return Task.FromResult(result);
}
public Task<EnrollmentResult> Renew(string certificateId, RenewCertificateRestRequest request, CancellationToken cancelToken)
{
_logger.LogInformation("Renewing certificate with Fake GoDaddy");
X509Certificate2 cert = SignFakeCsr(request.Csr);
_logger.LogDebug("Preparing EnrollmentResult object");
EnrollmentResult result = new EnrollmentResult
{
CARequestID = certificateId,
Status = (int)EndEntityStatus.GENERATED,
StatusMessage = $"Certificate with ID {certificateId} has been renewed",
Certificate = cert.ExportCertificatePem()
};
return Task.FromResult(result);
}
private X509Certificate2 SignFakeCsr(string csrString)
{
_logger.LogDebug("Serializing csr string to CertificateRequest object");
CertificateRequest csr = CertificateRequest.LoadSigningRequestPem(
csrString.AsSpan(),
HashAlgorithmName.SHA256,
CertificateRequestLoadOptions.Default,
RSASignaturePadding.Pkcs1
);
_logger.LogDebug("Generating self-signed CA certificate");
using var caKeyPair = RSA.Create(2048);
var caCertificate = GenerateSelfSignedCertificate(caKeyPair, "CN=Test CA", DateTime.UtcNow, DateTime.UtcNow.AddYears(1));
var serialNumber = new byte[8];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(serialNumber);
}
_logger.LogDebug($"Creating certificate from CSR valid from {EnrollmentNotBefore} to {EnrollmentNotAfter}");
X509Certificate2 cert = csr.Create(
caCertificate.SubjectName,
X509SignatureGenerator.CreateForRSA(caKeyPair, RSASignaturePadding.Pkcs1),
EnrollmentNotBefore,
EnrollmentNotAfter,
serialNumber
);
return cert;
}
public static X509Certificate2 GenerateSelfSignedCertificate(RSA keyPair, string subjectName, DateTimeOffset notBefore, DateTimeOffset notAfter)
{
var request = new CertificateRequest(
new X500DistinguishedName(subjectName),
keyPair,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1
);
var cert = request.CreateSelfSigned(notBefore, notAfter);
return cert;
}
}