Skip to content

Commit a39a9b9

Browse files
Merge 1f0ae06 into 33d52cc
2 parents 33d52cc + 1f0ae06 commit a39a9b9

File tree

9 files changed

+126
-85
lines changed

9 files changed

+126
-85
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.0.2
2+
* bug fix: _certDataReader is now initialized in the Initialize method
3+
14
## 1.0.1
25
* added retrieval of roles associated with enrolled certificates via metadata for Vault Enterprise users
36

hashicorp-vault-cagateway/APIProxy/CertResponse.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public class CertResponse
1616
public string Certificate { get; set; }
1717

1818
[JsonPropertyName("revocation_time_rfc3339")]
19-
public DateTime? RevocationTime { get; set; }
19+
public string RevocationTime { get; set; }
20+
21+
[JsonPropertyName("revocation_time")]
22+
public int? RevocationTimestamp { get; set; }
2023

2124
[JsonPropertyName("issuer_id")]
2225
public string IssuerId { get; set; }

hashicorp-vault-cagateway/APIProxy/WrappedResponse.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace Keyfactor.Extensions.CAPlugin.HashicorpVault.APIProxy
1212
{
1313
public class WrappedResponse<T>
1414
{
15+
[JsonPropertyName("request_id")]
16+
public string RequestId { get; set; }
17+
1518
[JsonPropertyName("lease_id")]
1619
public string LeaseId { get; set; }
1720

@@ -30,6 +33,9 @@ public class WrappedResponse<T>
3033
[JsonPropertyName("mount_point")]
3134
public string MountPoint { get; set; }
3235

36+
[JsonPropertyName("mount_type")]
37+
public string MountType { get; set; }
38+
3339
[JsonPropertyName("mount_running_plugin_version")]
3440
public string PluginVersion { get; set; }
3541

hashicorp-vault-cagateway/Client/HashicorpVaultClient.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,15 @@ public async Task<CertResponse> GetCertificate(string certSerial)
131131

132132
try
133133
{
134-
var response = await _vaultHttp.GetAsync<CertResponse>($"cert/{certSerial}");
134+
var response = await _vaultHttp.GetAsync<WrappedResponse<CertResponse>>($"cert/{certSerial}");
135+
135136
logger.LogTrace($"successfully received a response for certificate with serial number: {certSerial}");
136-
return response;
137+
logger.LogTrace($"--response data--");
138+
logger.LogTrace($"cert string: {response.Data?.Certificate}");
139+
logger.LogTrace($"revocation time: {response.Data?.RevocationTime}");
140+
141+
142+
return response.Data;
137143
}
138144
catch (Exception ex)
139145
{
@@ -152,9 +158,9 @@ public async Task<RevokeResponse> RevokeCertificate(string serial)
152158
logger.LogTrace($"making request to revoke cert with serial: {serial}");
153159
try
154160
{
155-
var response = await _vaultHttp.PostAsync<RevokeResponse>("revoke", new RevokeRequest(serial));
156-
logger.LogTrace($"successfully revoked cert with serial {serial}, revocation time: {response.RevocationTime}");
157-
return response;
161+
var response = await _vaultHttp.PostAsync<WrappedResponse<RevokeResponse>>("revoke", new RevokeRequest(serial));
162+
logger.LogTrace($"successfully revoked cert with serial {serial}, revocation time: {response.Data.RevocationTime}");
163+
return response.Data;
158164
}
159165
catch (Exception ex)
160166
{
@@ -189,7 +195,7 @@ public async Task<bool> PingServer()
189195
}
190196

191197
/// <summary>
192-
/// Retreives all serial numbers for issued certificates
198+
/// Retrieves all serial numbers for issued certificates
193199
/// </summary>
194200
/// <returns>a list of the certificate serial number strings</returns>
195201
public async Task<List<string>> GetAllCertSerialNumbers()
@@ -199,7 +205,7 @@ public async Task<List<string>> GetAllCertSerialNumbers()
199205
try
200206
{
201207
var res = await _vaultHttp.GetAsync<WrappedResponse<KeyedList>>("certs/?list=true");
202-
return res.Data.Entries;
208+
return res.Data?.Entries;
203209
}
204210
catch (Exception ex)
205211
{
@@ -215,8 +221,8 @@ private async Task<List<string>> GetRevokedSerialNumbers()
215221
var keys = new List<string>();
216222
try
217223
{
218-
var res = await _vaultHttp.GetAsync<KeyedList>("certs/revoked");
219-
keys = res.Entries;
224+
var res = await _vaultHttp.GetAsync<WrappedResponse<KeyedList>>("certs/revoked");
225+
keys = res.Data?.Entries;
220226
}
221227
catch (Exception ex)
222228
{
@@ -247,7 +253,7 @@ public async Task<List<string>> GetRoleNamesAsync()
247253
}
248254

249255
/// <summary>
250-
/// Retreives the metadata for the certificate
256+
/// Retrieves the metadata for the certificate
251257
/// </summary>
252258
/// <param name="certSerial"></param>
253259
/// <returns></returns>
@@ -275,7 +281,7 @@ public async Task<MetadataResponse> GetCertMetadata(string certSerial)
275281
}
276282
catch (Exception ex)
277283
{
278-
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}");
279285
throw;
280286
}
281287
finally { logger.MethodExit(); }
@@ -317,5 +323,7 @@ private static string ConvertSerialToTrackingId(string serialNumber)
317323

318324
return serialNumber.Replace(":", "-");
319325
}
326+
327+
320328
}
321329
}

hashicorp-vault-cagateway/Client/VaultHttp.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Collections.Generic;
1515
using System.Text.Json;
1616
using System.Text.Json.Serialization;
17+
using System.Threading;
1718
using System.Threading.Tasks;
1819

1920
namespace Keyfactor.Extensions.CAPlugin.HashicorpVault.Client
@@ -36,12 +37,12 @@ public VaultHttp(string host, string mountPoint, string authToken, string nameSp
3637
_serializerOptions = new()
3738
{
3839
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
39-
RespectNullableAnnotations = true,
4040
PropertyNameCaseInsensitive = true,
41-
PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace,
41+
RespectNullableAnnotations = true,
42+
PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace
4243
};
4344

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

4748
_mountPoint = mountPoint.TrimStart('/').TrimEnd('/'); // remove leading and trailing slashes
@@ -69,19 +70,32 @@ public VaultHttp(string host, string mountPoint, string authToken, string nameSp
6970
public async Task<T> GetAsync<T>(string path, Dictionary<string, string> parameters = null)
7071
{
7172
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)}");
73+
logger.LogTrace($"preparing to send GET request to {_mountPoint}/{path} with parameters {JsonSerializer.Serialize(parameters)}");
74+
7475
try
7576
{
7677
var request = new RestRequest($"{_mountPoint}/{path}", Method.Get);
77-
if (parameters != null) { request.AddJsonBody(parameters); }
78+
if (parameters != null && parameters.Keys.Count > 0) { request.AddJsonBody(parameters); }
79+
var response = await _restClient.ExecuteGetAsync(request);
80+
81+
logger.LogTrace($"raw response: {JsonSerializer.Serialize(response)}");
82+
83+
logger.LogTrace($"response content: {response.Content}");
84+
85+
logger.LogTrace($"response status: {response.StatusCode}");
7886

79-
var response = await _restClient.ExecuteGetAsync<T>(request);
80-
logger.LogTrace($"raw response: {response.Content}");
87+
logger.LogTrace($"response error msg: {response.ErrorMessage}");
8188

8289
response.ThrowIfError();
90+
if (string.IsNullOrEmpty(response.Content)) throw new Exception(response.ErrorMessage ?? "no content returned from Vault");
8391

84-
return response.Data;
92+
logger.LogTrace($"deserializing the response into a {typeof(T)}");
93+
94+
var deserialized = JsonSerializer.Deserialize<T>(response.Content, _serializerOptions);
95+
96+
logger.LogTrace($"successfully deserialized the response");
97+
98+
return deserialized;
8599
}
86100
catch (Exception ex)
87101
{
@@ -108,8 +122,8 @@ public async Task<T> PostAsync<T>(string path, dynamic parameters = default)
108122
var request = new RestRequest(resourcePath, Method.Post);
109123
if (parameters != null)
110124
{
111-
string serializedParams = JsonSerializer.Serialize(parameters, _serializerOptions);
112-
logger.LogTrace($"serialized parameters (from {parameters.GetType()?.Name}): {serializedParams}");
125+
string serializedParams = JsonSerializer.Serialize(parameters);
126+
logger.LogTrace($"deserialized parameters (from {parameters.GetType()?.Name}): {serializedParams}");
113127
request.AddJsonBody(serializedParams);
114128
}
115129

@@ -127,7 +141,7 @@ public async Task<T> PostAsync<T>(string path, dynamic parameters = default)
127141

128142
if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
129143
{
130-
errorResponse = JsonSerializer.Deserialize<ErrorResponse>(response.Content!);
144+
errorResponse = JsonSerializer.Deserialize<ErrorResponse>(response.Content ?? "no content");
131145
string allErrors = "(Bad Request)";
132146
if (errorResponse?.Errors.Count > 0)
133147
{

0 commit comments

Comments
 (0)