Skip to content

Commit 5f4ecc0

Browse files
authored
Improve log when secret is not found in keyVault (#173)
This pull request introduces improvements to error handling and logging when interacting with Azure Key Vault. The changes primarily focus on providing more detailed error messages by including the path of the secret that caused the error. ### Improvements to error handling: * [`src/Confix.Tool/src/Confix.Library/ThrowHelper.cs`](diffhunk://#diff-de2d44cf88807e7118a43465a75745e67e659f7268cad9c28496735aac87ad67L44-R48): Modified the `SecretNotFound` method to accept an optional `path` parameter and include it in the error message if provided. ### Updates to method signatures: * [`src/Confix.Tool/src/Confix.Library/Utilities/Azure/KeyVaultExtension.cs`](diffhunk://#diff-520f5459080dfd82d45c6e5bf94a9cc9a0ecda7e85b403aa953d50421aee0ebcL4-R17): Updated the `HandleKeyVaultException` method to accept an optional `path` parameter and pass it to the `SecretNotFound` method. ### Enhancements to Azure Key Vault provider: * [`src/Confix.Tool/src/Confix.Library/Variables/Providers/AzureKeyVault/AzureKeyVaultProvider.cs`](diffhunk://#diff-bcce2fb5047575d77b3d9a54ee0f9df0688c86e01fb6afd1665a8ae18b20f135L57-R57): Updated the `ResolveAsync` and `SetAsync` methods to pass the `path` parameter to the `HandleKeyVaultException` method. [[1]](diffhunk://#diff-bcce2fb5047575d77b3d9a54ee0f9df0688c86e01fb6afd1665a8ae18b20f135L57-R57) [[2]](diffhunk://#diff-bcce2fb5047575d77b3d9a54ee0f9df0688c86e01fb6afd1665a8ae18b20f135L75-R75)- mention secret path in error message when available
1 parent da7cdbe commit 5f4ecc0

3 files changed

Lines changed: 15 additions & 13 deletions

File tree

src/Confix.Tool/src/Confix.Library/ThrowHelper.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ public static Exception VariablesNotFound(string[] names) =>
4141
public static Exception CouldNotParseJsonFile(FileInfo file)
4242
=> throw new ExitException($"File {file.FullName} has invalid content.");
4343

44-
public static Exception SecretNotFound(Exception innerException) =>
45-
new ExitException("Secret does not exist in this provider.", innerException)
44+
public static Exception SecretNotFound(Exception innerException, string? path = null) =>
45+
new ExitException(
46+
path is null
47+
? "Secret does not exist in this provider."
48+
: $"Secret {path.AsHighlighted()} does not exist in this provider.", innerException)
4649
{
4750
Help = $"try running {"confix variable list".AsHighlighted()} to list all available variables"
4851
};
@@ -53,7 +56,7 @@ public static Exception AccessToKeyVaultFailed(RequestFailedException innerExcep
5356
details.AppendLine($"Message: {innerException.Message}");
5457
details.AppendLine($"Error code: {innerException.ErrorCode}");
5558
details.AppendLine($"Status code: {innerException.Status}");
56-
59+
5760
return new ExitException("Access to Key Vault failed", innerException)
5861
{
5962
Help = "check if you have the required permissions to access the Key Vault",
@@ -66,4 +69,4 @@ public static Exception AuthenticationFailedForVault(Exception innerException) =
6669
{
6770
Help = $"try running {"az login".AsHighlighted()} to authenticate with Azure"
6871
};
69-
}
72+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
using Azure;
22
using Azure.Identity;
33
using Confix.Tool;
4-
using Confix.Tool.Commands.Logging;
54

65
namespace Confix.Utilities.Azure;
76

87
public static class KeyVaultExtension
98
{
10-
public static async Task<T> HandleKeyVaultException<T>(Func<Task<T>> action)
9+
public static async Task<T> HandleKeyVaultException<T>(Func<Task<T>> action, string? path = null)
1110
{
1211
try
1312
{
1413
return await action();
1514
}
1615
catch (RequestFailedException ex) when (ex.ErrorCode == "SecretNotFound")
1716
{
18-
throw ThrowHelper.SecretNotFound(ex);
17+
throw ThrowHelper.SecretNotFound(ex, path);
1918
}
2019
catch (RequestFailedException ex)
2120
{
@@ -26,4 +25,4 @@ public static async Task<T> HandleKeyVaultException<T>(Func<Task<T>> action)
2625
throw ThrowHelper.AuthenticationFailedForVault(ex);
2726
}
2827
}
29-
}
28+
}

src/Confix.Tool/src/Confix.Library/Variables/Providers/AzureKeyVault/AzureKeyVaultProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Task<IReadOnlyList<string>> ListAsync(CancellationToken cancellationToken
3838
=> KeyVaultExtension.HandleKeyVaultException<IReadOnlyList<string>>(async () =>
3939
{
4040
App.Log.ListSecrets(_client.VaultUri);
41-
41+
4242
var secrets = new List<string>();
4343
await foreach (var secret in _client.GetPropertiesOfSecretsAsync(cancellationToken))
4444
{
@@ -54,7 +54,7 @@ public Task<JsonNode> ResolveAsync(string path, CancellationToken cancellationTo
5454
KeyVaultSecret result = await _client.GetSecretAsync(path.ToKeyVaultCompatiblePath(),
5555
cancellationToken: cancellationToken);
5656
return JsonValue.Create(result.Value)!;
57-
});
57+
}, path);
5858

5959
public Task<IReadOnlyDictionary<string, JsonNode>> ResolveManyAsync(
6060
IReadOnlyList<string> paths,
@@ -70,9 +70,9 @@ public Task<string> SetAsync(string path, JsonNode value, CancellationToken ct)
7070
}
7171

7272
KeyVaultSecret result = await _client
73-
.SetSecretAsync(path.ToKeyVaultCompatiblePath(), (string) value!, ct);
73+
.SetSecretAsync(path.ToKeyVaultCompatiblePath(), (string)value!, ct);
7474
return result.Name.ToConfixPath();
75-
});
75+
}, path);
7676

7777
public ValueTask DisposeAsync()
7878
{
@@ -93,4 +93,4 @@ public static void ListSecrets(this IConsoleLogger log, Uri vaultUri)
9393
{
9494
log.Information($"List all secrets from Azure Kev Vault '{vaultUri}'");
9595
}
96-
}
96+
}

0 commit comments

Comments
 (0)