Skip to content

Commit 7cd93d6

Browse files
added additional logging and null checking on response data.
1 parent 0985e03 commit 7cd93d6

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

hashicorp-vault-orchestrator/HcvKeyValueClient.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Keyfactor.Orchestrators.Common.Enums;
1818
using Keyfactor.Orchestrators.Extensions;
1919
using Microsoft.Extensions.Logging;
20+
using Newtonsoft.Json;
2021
using Org.BouncyCastle.Crypto;
2122
using Org.BouncyCastle.OpenSsl;
2223
using Org.BouncyCastle.Pkcs;
@@ -63,7 +64,6 @@ public HcvKeyValueClient(string vaultToken, string serverUrl, string mountPoint,
6364
_passphrasePropName = passphrasePropName;
6465
_subfolderInventory = SubfolderInventory;
6566
_storeType = storeType?.Split('.')[1];
66-
6767
}
6868

6969
public async Task CreateCertStore()
@@ -179,7 +179,7 @@ private async Task CreateFileStore()
179179
// write the passphrase secret
180180
var req = new PatchSecretDataRequest();
181181
req.Data = passphraseSecretContent;
182-
182+
183183
logger.LogTrace($"sending request to write new cert store passphrase");
184184
res = await VaultClient.V1.Secrets.KeyValue.V2.PatchSecretAsync(pathToWritePassphrase, req, _mountPoint);
185185
logger.LogTrace($"request to write passphrase secret was successful. secret created time: {res.Data?.CreatedTime}");
@@ -960,15 +960,22 @@ private async Task<List<string>> GetSubPaths(string storagePath)
960960
// first get cert contents
961961
try
962962
{
963-
logger.LogTrace("retreiving the certificate store secret..");
963+
logger.LogTrace($"retreiving the certificate store secret at {_certPath} from the Key-Value secrets engine mounted at {_mountPoint}..");
964964

965965
res = await VaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync(_certPath, mountPoint: _mountPoint);
966966

967-
certFileObj = (Dictionary<string, object>)res.Data.Data;
967+
logger.LogTrace($"received a response: {JsonConvert.SerializeObject(res)}");
968+
969+
if (res.Warnings.Any())
970+
{
971+
logger.LogTrace($"response warnings: {res.Warnings}");
972+
}
973+
974+
certFileObj = (Dictionary<string, object>)res?.Data?.Data;
968975

969976
logger.LogTrace($"got cert secret data.. contents: ");
970977

971-
if (certFileObj == null || certFileObj.Keys.Count == 0)
978+
if (certFileObj == null || certFileObj?.Keys?.Count == 0)
972979
{
973980
logger.LogError($"no secret content was found at path {_certPath}");
974981
throw new DirectoryNotFoundException($"entry named {certSecretName} not found at {certParentPath} or is empty.");
@@ -985,7 +992,7 @@ private async Task<List<string>> GetSubPaths(string storagePath)
985992
if (certSecretIsJSON)
986993
{
987994
// if the cert data is stored as a property in a JSON secret object, we get the value from the property
988-
certContent = certFileObj[_certPropName].ToString();
995+
certContent = certFileObj[_certPropName].ToString();
989996
}
990997
else
991998
{
@@ -997,7 +1004,7 @@ private async Task<List<string>> GetSubPaths(string storagePath)
9971004

9981005
logger.LogTrace($"now we retrieve the passphrase from {passphraseParentPath + passphraseSecretName}");
9991006
res = await VaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync(_passphrasePath, mountPoint: _mountPoint);
1000-
var passphraseObj = (Dictionary<string, object>)res.Data.Data;
1007+
var passphraseObj = (Dictionary<string, object>)res?.Data?.Data;
10011008

10021009
foreach (var key in passphraseObj.Keys)
10031010
{

0 commit comments

Comments
 (0)