Skip to content

Commit c38b4f7

Browse files
committed
better API logging
1 parent 8440ea7 commit c38b4f7

2 files changed

Lines changed: 24 additions & 54 deletions

File tree

sectigo-scm-caplugin/Client/SectigoClient.cs

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Newtonsoft.Json;
99
using Newtonsoft.Json.Linq;
1010

11+
using Org.BouncyCastle.Asn1.Ocsp;
12+
1113
using System;
1214
using System.Collections.Concurrent;
1315
using System.Collections.Generic;
@@ -19,6 +21,8 @@
1921
using System.Threading;
2022
using System.Threading.Tasks;
2123

24+
using Error = Keyfactor.Extensions.CAPlugin.Sectigo.API.Error;
25+
2226
namespace Keyfactor.Extensions.CAPlugin.Sectigo.Client
2327
{
2428
public class SectigoClient
@@ -34,7 +38,9 @@ public SectigoClient(HttpClient client)
3438

3539
public async Task<Certificate> GetCertificate(int sslId)
3640
{
37-
var response = await RestClient.GetAsync($"api/ssl/v1/{sslId}");
41+
string url = $"api/ssl/v1/{sslId}";
42+
Logger.LogTrace($"API Request: GET {url}");
43+
var response = await RestClient.GetAsync(url);
3844
return await ProcessResponse<Certificate>(response);
3945
}
4046

@@ -139,7 +145,7 @@ public async Task CertificateListProducer(BlockingCollection<Certificate> certs,
139145
public async Task<List<Certificate>> PageCertificates(int position = 0, int size = 25, string filter = "")
140146
{
141147
string filterQueryString = string.IsNullOrEmpty(filter) ? string.Empty : $"&{filter}";
142-
Logger.LogTrace($"API Request: api/ssl/v1?position={position}&size={size}{filterQueryString}".TrimEnd());
148+
Logger.LogTrace($"API Request: GET api/ssl/v1?position={position}&size={size}{filterQueryString}".TrimEnd());
143149
var response = await RestClient.GetAsync($"api/ssl/v1?position={position}&size={size}{filterQueryString}".TrimEnd());
144150
return await ProcessResponse<List<Certificate>>(response);
145151
}
@@ -151,37 +157,26 @@ public async Task<bool> RevokeSslCertificateById(int sslId, int revcode, string
151157
reasonCode = revcode,
152158
reason = revreason
153159
};
160+
Logger.LogTrace($"API Request: POST api/ssl/v1/revoke/{sslId}\nParameters: {JsonConvert.SerializeObject(data, Formatting.Indented)}");
154161
var response = await RestClient.PostAsJsonAsync($"api/ssl/v1/revoke/{sslId}", data);
155-
if (response.IsSuccessStatusCode)
156-
{
157-
return true;
158-
}
159-
var failedResp = ProcessResponse<RevocationResponse>(response).Result;
160-
return failedResp.IsSuccess;//Should throw an exception with error message from API
162+
var resp = ProcessResponse<RevocationResponse>(response).Result;
163+
164+
return true;//Should throw an exception with error message from API, should only hit this if success
161165
}
162166

163167
public async Task<ListOrganizationsResponse> ListOrganizations()
164168
{
169+
Logger.LogTrace($"API Request: GET api/organization/v1");
165170
var response = await RestClient.GetAsync("api/organization/v1");
166-
if (response.IsSuccessStatusCode)
167-
{
168-
string responseContent = await response.Content.ReadAsStringAsync();
169-
Logger.LogTrace($"Raw Response: {responseContent}");
170-
}
171171
var orgsResponse = await ProcessResponse<List<Organization>>(response);
172172

173173
return new ListOrganizationsResponse { Organizations = orgsResponse };
174174
}
175175

176176
public async Task<OrganizationDetailsResponse> GetOrganizationDetails(int orgId)
177177
{
178+
Logger.LogTrace($"API Request: GET api/organization/v1/{orgId}");
178179
var response = await RestClient.GetAsync($"api/organization/v1/{orgId}");
179-
if (response.IsSuccessStatusCode)
180-
{
181-
string responseContent = await response.Content.ReadAsStringAsync();
182-
Logger.LogTrace($"Raw Response: {responseContent}");
183-
}
184-
185180
var orgDetailsResponse = await ProcessResponse<OrganizationDetailsResponse>(response);
186181
return orgDetailsResponse;
187182
}
@@ -203,6 +198,7 @@ public async Task<ListPersonsResponse> ListPersons(int orgId)
203198

204199
public async Task<ListCustomFieldsResponse> ListCustomFields()
205200
{
201+
Logger.LogTrace($"API Request: GET api/ssl/v1/customFields");
206202
var response = await RestClient.GetAsync("api/ssl/v1/customFields");
207203
return new ListCustomFieldsResponse { CustomFields = await ProcessResponse<List<CustomField>>(response) };
208204
}
@@ -214,13 +210,14 @@ public async Task<ListSslProfilesResponse> ListSslProfiles(int? orgId = null)
214210
{
215211
urlSuffix = $"?organizationId={orgId}";
216212
}
217-
213+
Logger.LogTrace($"API Request: GET api/ssl/v1/types{urlSuffix}");
218214
var response = await RestClient.GetAsync($"api/ssl/v1/types{urlSuffix}");
219215
return new ListSslProfilesResponse { SslProfiles = await ProcessResponse<List<Profile>>(response) };
220216
}
221217

222218
public async Task<List<Person>> PagePersons(int orgId, int position = 0, int size = 25)
223219
{
220+
Logger.LogTrace($"API Request: GET api/person/v1?position={position}&size={size}&organizationId={orgId}");
224221
var response = await RestClient.GetAsync($"api/person/v1?position={position}&size={size}&organizationId={orgId}");
225222
return await ProcessResponse<List<Person>>(response);
226223
}
@@ -229,6 +226,7 @@ public async Task<int> Enroll(EnrollRequest request)
229226
{
230227
try
231228
{
229+
Logger.LogTrace($"API Request: POST api/ssl/v1/enroll\nParameters: {JsonConvert.SerializeObject(request, Formatting.Indented)}");
232230
var response = await RestClient.PostAsJsonAsync("api/ssl/v1/enroll", request);
233231
var enrollResponse = await ProcessResponse<EnrollResponse>(response);
234232

@@ -248,35 +246,14 @@ public async Task<int> Enroll(EnrollRequest request)
248246
}
249247
}
250248

251-
public async Task<int> Renew(int sslId)
252-
{
253-
try
254-
{
255-
var response = await RestClient.PostAsJsonAsync($"api/ssl/v1/renewById/{sslId}", "");
256-
var renewResponse = await ProcessResponse<EnrollResponse>(response);
257-
258-
return renewResponse.sslId;
259-
}
260-
catch (InvalidOperationException invalidOp)
261-
{
262-
throw new Exception($"Invalid Operation. {invalidOp.Message}|{invalidOp.StackTrace}");
263-
}
264-
catch (HttpRequestException httpEx)
265-
{
266-
throw new Exception($"HttpRequestException. {httpEx.Message}|{httpEx.StackTrace}");
267-
}
268-
catch (Exception)
269-
{
270-
throw;
271-
}
272-
}
273-
274249
public async Task<X509Certificate2> PickupCertificate(int sslId, string subject)
275250
{
251+
Logger.LogTrace($"API Request: GET api/ssl/v1/collect/{sslId}/x509C0");
276252
var response = await RestClient.GetAsync($"api/ssl/v1/collect/{sslId}/x509CO");
277-
253+
278254
if (response.IsSuccessStatusCode && response.Content.Headers.ContentLength > 0)
279255
{
256+
Logger.LogTrace($"Raw response: {response.Content.ReadAsStringAsync()}");
280257
string pemChain = await response.Content.ReadAsStringAsync();
281258

282259
string[] splitChain = pemChain.Replace("\r\n", string.Empty).Split(new string[] { "-----" }, StringSplitOptions.RemoveEmptyEntries);
@@ -287,24 +264,19 @@ public async Task<X509Certificate2> PickupCertificate(int sslId, string subject)
287264
//return new X509Certificate2();
288265
}
289266

290-
public async Task Reissue(ReissueRequest request, int sslId)
291-
{
292-
var response = await RestClient.PostAsJsonAsync($"api/ssl/v1/replace/{sslId}", request);
293-
response.EnsureSuccessStatusCode();
294-
}
295-
296267
#region Static Methods
297268

298269
private static async Task<T> ProcessResponse<T>(HttpResponseMessage response)
299270
{
271+
string responseContent = await response.Content.ReadAsStringAsync();
272+
Logger.LogDebug($"Raw API response: {responseContent}");
300273
if (response.IsSuccessStatusCode)
301274
{
302-
string responseContent = await response.Content.ReadAsStringAsync();
303275
return JsonConvert.DeserializeObject<T>(responseContent);
304276
}
305277
else
306278
{
307-
var error = JsonConvert.DeserializeObject<Error>(await response.Content.ReadAsStringAsync());
279+
var error = JsonConvert.DeserializeObject<Error>(responseContent);
308280
throw new Exception($"{error.Code} | {error.Description}");
309281
}
310282
}

sectigo-scm-caplugin/SectigoCAPlugin.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ public async Task<EnrollmentResult> Enroll(string csr, string subject, Dictionar
249249
};
250250

251251
_logger.LogDebug($"Submit {enrollmentType} request");
252-
var jsonReq = JsonConvert.SerializeObject(request, Formatting.Indented);
253-
_logger.LogDebug($"Request object: {jsonReq}");
254252
sslId = Task.Run(async () => await client.Enroll(request)).Result;
255253
newCert = Task.Run(async () => await client.GetCertificate(sslId)).Result;
256254
_logger.LogDebug($"Enrolled for Certificate {newCert.CommonName} (ID: {newCert.Id}) | Status: {newCert.status}. Attempt to Pickup Certificate.");

0 commit comments

Comments
 (0)