Skip to content

Commit 7c40e7c

Browse files
committed
feat: Add verbose logging to buddypass calls.
1 parent 363c939 commit 7c40e7c

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

kubernetes-orchestrator-extension/Clients/KubeClient.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,13 +1124,23 @@ private V1Secret CreateOrUpdatePKCS12Secret(string secretName, string namespaceN
11241124
public V1Secret ReadBuddyPass(string secretName, string passwordSecretPath)
11251125
{
11261126
// Lookup password secret path on cluster to see if it exists
1127+
_logger.MethodEntry();
11271128
_logger.LogDebug("Attempting to lookup password secret path on cluster...");
11281129
var splitPasswordPath = passwordSecretPath.Split("/");
1130+
_logger.LogDebug("Split password secret path: {Join}", string.Join(", ", splitPasswordPath));
11291131
// Assume secret pattern is namespace/secretName
1130-
var passwordSecretName = splitPasswordPath[splitPasswordPath.Length - 1];
1132+
var passwordSecretName = splitPasswordPath[^1];
1133+
_logger.LogDebug("Password secret name: {PasswordSecretName}", passwordSecretName);
11311134
var passwordSecretNamespace = splitPasswordPath[0];
1132-
_logger.LogDebug($"Attempting to lookup secret {passwordSecretName} in namespace {passwordSecretNamespace}");
1135+
_logger.LogDebug("Attempting to lookup secret {PasswordSecretName} in namespace {PasswordSecretNamespace}", passwordSecretName, passwordSecretNamespace);
1136+
11331137
var passwordSecretResponse = Client.CoreV1.ReadNamespacedSecret(secretName, passwordSecretNamespace);
1138+
if (passwordSecretResponse == null)
1139+
{
1140+
_logger.LogError("Unable to find secret {PasswordSecretName} in namespace {PasswordSecretNamespace}", passwordSecretName, passwordSecretNamespace);
1141+
throw new InvalidK8SSecretException($"Unable to find secret {passwordSecretName} in namespace {passwordSecretNamespace}");
1142+
}
1143+
_logger.MethodExit();
11341144
return passwordSecretResponse;
11351145
}
11361146

kubernetes-orchestrator-extension/Jobs/Inventory.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,17 @@ private Dictionary<string, List<string>> HandleJKSSecret(JobConfiguration config
319319
Logger.LogDebug("Iterating through keys in K8S secret " + KubeSecretName + " in namespace " + KubeNamespace);
320320
foreach (var (keyName, keyBytes) in k8sData.Inventory)
321321
{
322-
Logger.LogDebug("Fetching store password for K8S secret " + KubeSecretName + " in namespace " +
323-
KubeNamespace + " and key " + keyName);
322+
Logger.LogTrace("Key name: {KeyName}", keyName);
323+
if (keyBytes == null || keyBytes.Length == 0)
324+
{
325+
Logger.LogWarning("Key '{KeyName}' in secret {Secret} is empty or null, skipping", keyName, KubeSecretName);
326+
continue;
327+
}
328+
Logger.LogDebug("Attempting to get password for key '{Key}' in k8s namesapce {Namespace} in secret '{Secret}'", keyName,KubeNamespace, KubeSecretName);
324329
var keyPassword = getK8SStorePassword(k8sData.Secret);
325330
var passwordHash = GetSHA256Hash(keyPassword);
326-
// Logger.LogTrace("Password hash for '{Secret}/{Key}': {Hash}", KubeSecretName, keyName, passwordHash);
331+
Logger.LogTrace("Password hash for '{Secret}/{Key}': {Hash}", KubeSecretName, keyName, passwordHash); //TODO: Remove this line, it is for debugging purposes only
332+
Logger.LogTrace("Password for '{Secret}/{Key}': {Hash}", KubeSecretName, keyName, keyPassword); //TODO: Remove this line, it is for debugging purposes only
327333
var keyAlias = keyName;
328334
Logger.LogTrace("Key alias: {Alias}", keyAlias);
329335
Logger.LogDebug("Attempting to deserialize JKS store '{Secret}/{Key}'", KubeSecretName, keyName);
@@ -953,7 +959,19 @@ private Dictionary<string, List<string>> HandlePkcs12Secret(JobConfiguration con
953959
// iterate through the keys in the secret and add them to the pkcs12 store
954960
foreach (var (keyName, keyBytes) in k8sData.Inventory)
955961
{
962+
Logger.LogTrace("Key name: {KeyName}", keyName);
963+
if (keyBytes == null || keyBytes.Length == 0)
964+
{
965+
Logger.LogWarning("Key '{KeyName}' in secret {Secret} is empty or null, skipping", keyName, KubeSecretName);
966+
continue;
967+
}
968+
Logger.LogDebug("Attempting to get password for key '{Key}' in k8s namespace {Namespace} in secret '{Secret}'", keyName,KubeNamespace, KubeSecretName);
956969
var keyPassword = getK8SStorePassword(k8sData.Secret);
970+
Logger.LogTrace("Password for '{Secret}/{Key}': {Hash}", KubeSecretName, keyName, keyPassword); //TODO: Remove this line, it is for debugging purposes only
971+
var keyAlias = keyName;
972+
Logger.LogTrace("Key alias: {Alias}", keyAlias);
973+
Logger.LogDebug("Attempting to deserialize PKCS12 store '{Secret}/{Key}'", KubeSecretName, keyName);
974+
957975
var pStoreDs = pkcs12Store.DeserializeRemoteCertificateStore(keyBytes, keyName, keyPassword);
958976
// create a list of certificate chains in PEM format
959977
foreach (var certAlias in pStoreDs.Aliases)

kubernetes-orchestrator-extension/Keyfactor.Orchestrators.K8S.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<PackageReference Include="KubernetesClient" Version="14.0.2"/>
3030
<!-- <PackageReference Include="KubernetesClient.Classic" Version="10.0.31" />-->
3131
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="8.0.0"/>
32+
<!-- <PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />-->
33+
<ProjectReference Include="../../bc-csharp/crypto/src/BouncyCastle.Crypto.csproj" />
34+
35+
3236
</ItemGroup>
3337

3438
</Project>

kubernetes-orchestrator-extension/StoreTypes/K8SJKS/Store.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Pkcs12Store DeserializeRemoteCertificateStore(byte[] storeContents, strin
4848
try
4949
{
5050
_logger.LogTrace("Attempting to load JKS store w/ password");
51-
// _logger.LogTrace("Attempting to load JKS store w/ password ${Pass}", storePassword); //TODO: Remove this line, it is for debugging purposes only
51+
_logger.LogTrace("Attempting to load JKS store w/ password ${Pass}", storePassword); //TODO: Remove this line, it is for debugging purposes only
5252
using (var ms = new MemoryStream(storeContents))
5353
{
5454
jksStore.Load(ms, string.IsNullOrEmpty(storePassword) ? Array.Empty<char>() : storePassword.ToCharArray());

0 commit comments

Comments
 (0)