Skip to content

Commit cf00704

Browse files
added additional logging for troubleshooting.
1 parent c9bc2bb commit cf00704

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

hashicorp-vault-cagateway/Client/HashicorpVaultClient.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ public async Task<CertResponse> GetCertificate(string certSerial)
132132
try
133133
{
134134
var response = await _vaultHttp.GetAsync<CertResponse>($"cert/{certSerial}");
135+
135136
logger.LogTrace($"successfully received a response for certificate with serial number: {certSerial}");
136137
logger.LogTrace($"--response data--");
137138
logger.LogTrace($"cert string: {response.Certificate}");
138139
logger.LogTrace($"revocation time: {response.RevocationTime}");
140+
139141

140142
return response;
141143
}
@@ -193,7 +195,7 @@ public async Task<bool> PingServer()
193195
}
194196

195197
/// <summary>
196-
/// Retreives all serial numbers for issued certificates
198+
/// Retrieves all serial numbers for issued certificates
197199
/// </summary>
198200
/// <returns>a list of the certificate serial number strings</returns>
199201
public async Task<List<string>> GetAllCertSerialNumbers()
@@ -251,7 +253,7 @@ public async Task<List<string>> GetRoleNamesAsync()
251253
}
252254

253255
/// <summary>
254-
/// Retreives the metadata for the certificate
256+
/// Retrieves the metadata for the certificate
255257
/// </summary>
256258
/// <param name="certSerial"></param>
257259
/// <returns></returns>
@@ -279,7 +281,7 @@ public async Task<MetadataResponse> GetCertMetadata(string certSerial)
279281
}
280282
catch (Exception ex)
281283
{
282-
logger.LogError($"an error occurred when attempting to retreive the certificate metadata: {ex.Message}");
284+
logger.LogError($"an error occurred when attempting to retrieve the certificate metadata: {ex.Message}");
283285
throw;
284286
}
285287
finally { logger.MethodExit(); }

hashicorp-vault-cagateway/Client/VaultHttp.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
using Keyfactor.Extensions.CAPlugin.HashicorpVault.APIProxy;
99
using Keyfactor.Logging;
1010
using Microsoft.Extensions.Logging;
11+
using Newtonsoft.Json;
1112
using RestSharp;
1213
using RestSharp.Serializers.Json;
1314
using System;
1415
using System.Collections.Generic;
1516
using System.Text.Json;
1617
using System.Text.Json.Serialization;
1718
using System.Threading.Tasks;
19+
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
1820

1921
namespace Keyfactor.Extensions.CAPlugin.HashicorpVault.Client
2022
{
@@ -35,13 +37,13 @@ public VaultHttp(string host, string mountPoint, string authToken, string nameSp
3537

3638
_serializerOptions = new()
3739
{
38-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
40+
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
3941
RespectNullableAnnotations = true,
4042
PropertyNameCaseInsensitive = true,
41-
PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace,
43+
PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace
4244
};
4345

44-
var restClientOptions = new RestClientOptions($"{host.TrimEnd('/')}/v1") { ThrowOnAnyError = true };
46+
var restClientOptions = new RestClientOptions($"{host.TrimEnd('/')}/v1") { ThrowOnAnyError = true };
4547
_restClient = new RestClient(restClientOptions, configureSerialization: s => s.UseSystemTextJson(_serializerOptions));
4648

4749
_mountPoint = mountPoint.TrimStart('/').TrimEnd('/'); // remove leading and trailing slashes
@@ -69,19 +71,28 @@ public VaultHttp(string host, string mountPoint, string authToken, string nameSp
6971
public async Task<T> GetAsync<T>(string path, Dictionary<string, string> parameters = null)
7072
{
7173
logger.MethodEntry();
72-
logger.LogTrace($"preparing to send GET request to {path} with parameters {JsonSerializer.Serialize(parameters)}");
73-
logger.LogTrace($"will attempt to deserialize the response into a {typeof(T)}");
74+
logger.LogTrace($"preparing to send GET request to {path} with parameters {JsonConvert.SerializeObject(parameters)}");
75+
7476
try
7577
{
7678
var request = new RestRequest($"{_mountPoint}/{path}", Method.Get);
77-
if (parameters != null) { request.AddJsonBody(parameters); }
78-
79-
var response = await _restClient.ExecuteGetAsync<T>(request);
79+
if (parameters != null && parameters.Keys.Count > 0) { request.AddJsonBody(parameters); }
80+
var response = await _restClient.ExecuteGetAsync(request);
81+
8082
logger.LogTrace($"raw response: {response.Content}");
8183

84+
logger.LogTrace($"response status: {response.StatusCode}");
85+
86+
logger.LogTrace($"response error msg: {response.ErrorMessage}");
87+
8288
response.ThrowIfError();
89+
if (string.IsNullOrEmpty(response.Content)) throw new Exception(response.ErrorMessage ?? "no content returned from Vault");
8390

84-
return response.Data;
91+
logger.LogTrace($"deserializing the response into a {typeof(T)}");
92+
var serialized = JsonConvert.DeserializeObject<T>(response.Content);
93+
94+
logger.LogTrace($"successfully deserialized the reponse");
95+
return serialized;
8596
}
8697
catch (Exception ex)
8798
{
@@ -108,7 +119,7 @@ public async Task<T> PostAsync<T>(string path, dynamic parameters = default)
108119
var request = new RestRequest(resourcePath, Method.Post);
109120
if (parameters != null)
110121
{
111-
string serializedParams = JsonSerializer.Serialize(parameters, _serializerOptions);
122+
string serializedParams = JsonConvert.SerializeObject(parameters);
112123
logger.LogTrace($"serialized parameters (from {parameters.GetType()?.Name}): {serializedParams}");
113124
request.AddJsonBody(serializedParams);
114125
}
@@ -127,7 +138,7 @@ public async Task<T> PostAsync<T>(string path, dynamic parameters = default)
127138

128139
if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
129140
{
130-
errorResponse = JsonSerializer.Deserialize<ErrorResponse>(response.Content!);
141+
errorResponse = JsonConvert.DeserializeObject<ErrorResponse>(response.Content ?? "no content");
131142
string allErrors = "(Bad Request)";
132143
if (errorResponse?.Errors.Count > 0)
133144
{

hashicorp-vault-cagateway/HashicorpVaultCAConnector.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
240240
}
241241
catch (Exception ex)
242242
{
243-
logger.LogError($"failed to retreive serial numbers: {LogHandler.FlattenException(ex)}");
243+
logger.LogError($"failed to retrieve serial numbers: {LogHandler.FlattenException(ex)}");
244244
throw;
245245
}
246246

@@ -251,15 +251,15 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
251251
CertResponse certFromVault = null;
252252
var dbStatus = -1;
253253

254-
// first, retreive the details from Vault
254+
// first, retrieve the details from Vault
255255
try
256256
{
257257
logger.LogTrace($"Calling GetCertificate on our client, passing serial number: {certSerial}");
258258
certFromVault = await _client.GetCertificate(certSerial);
259259
}
260260
catch (Exception ex)
261261
{
262-
logger.LogError($"Failed to retreive details for certificate with serial number {certSerial} from Vault. Errors: {LogHandler.FlattenException(ex)}");
262+
logger.LogError($"Failed to retrieve details for certificate with serial number {certSerial} from Vault. Errors: {LogHandler.FlattenException(ex)}");
263263
throw;
264264
}
265265
logger.LogTrace($"converting {certSerial} to database trackingId");
@@ -269,7 +269,7 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
269269
// then, check for an existing local entry
270270
try
271271
{
272-
logger.LogTrace($"attempting to retreive status of cert with tracking id {trackingId} from the database");
272+
logger.LogTrace($"attempting to retrieve status of cert with tracking id {trackingId} from the database");
273273
dbStatus = await _certificateDataReader.GetStatusByRequestID(trackingId);
274274
}
275275
catch
@@ -281,7 +281,7 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
281281
{
282282
logger.LogTrace($"adding cert with serial {trackingId} to the database. fullsync is {fullSync}, and the certificate {(dbStatus == -1 ? "does not yet exist" : "already exists")} in the database.");
283283

284-
logger.LogTrace("attempting to retreive the role name (productId) from the certificate metadata, if available");
284+
logger.LogTrace("attempting to retrieve the role name (productId) from the certificate metadata, if available");
285285

286286
var metaData = new MetadataResponse();
287287

@@ -291,7 +291,7 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
291291
}
292292
catch (Exception)
293293
{
294-
logger.LogTrace("an error occurred when attempting to retreive the metadata, continuing..");
294+
logger.LogTrace("an error occurred when attempting to retrieve the metadata, continuing..");
295295
}
296296

297297
var newCert = new AnyCAPluginCertificate
@@ -423,7 +423,7 @@ public async Task ValidateCAConnectionInfo(Dictionary<string, object> connection
423423

424424
_client = new HashicorpVaultClient(config);
425425

426-
// attempt an authenticated request to retreive role names
426+
// attempt an authenticated request to retrieve role names
427427
try
428428
{
429429
logger.LogTrace("making an authenticated request to the Vault server to verify credentials (listing role names)..");

hashicorp-vault-cagateway/hashicorp-vault-caplugin.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<PackageReference Include="Keyfactor.AnyGateway.IAnyCAPlugin" Version="3.0.0" />
3838
<PackageReference Include="Keyfactor.Logging" Version="1.1.2" />
3939
<PackageReference Include="Keyfactor.PKI" Version="5.5.0" />
40+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
4041
<PackageReference Include="RestSharp" Version="112.1.0" />
4142
<PackageReference Include="System.Formats.Asn1" Version="9.0.0" />
4243
<PackageReference Include="System.Net.Http.WinHttpHandler" Version="9.0.0" />

readme_source.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Certificates issued for the Hashicorp Vault CA from within the Keyfactor Command
9595
1. Create an entry for each of the PKI secrets engine roles you would like to use for issuing certificates from the Hashicorp Vault CA.
9696
1. Navigate to the "Certificate Authorities" tab and click "Edit"
9797
1. In the "Edit CA" window, navigate to the "Templates" tab.
98-
1. Create an association between each of the certificate profiles we just created with the PKI secrets engine roles retreived from Vault.
98+
1. Create an association between each of the certificate profiles we just created with the PKI secrets engine roles retrieved from Vault.
9999

100100
### Configure the CA in Keyfactor Command
101101

0 commit comments

Comments
 (0)