Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Confix.Tool/src/Confix.Library/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public static Exception VariablesNotFound(string[] names) =>
public static Exception CouldNotParseJsonFile(FileInfo file)
=> throw new ExitException($"File {file.FullName} has invalid content.");

public static Exception SecretNotFound(Exception innerException) =>
new ExitException("Secret does not exist in this provider.", innerException)
public static Exception SecretNotFound(Exception innerException, string? path = null) =>
new ExitException(
path is null
? "Secret does not exist in this provider."
: $"Secret {path.AsHighlighted()} does not exist in this provider.", innerException)
{
Help = $"try running {"confix variable list".AsHighlighted()} to list all available variables"
};
Expand All @@ -53,7 +56,7 @@ public static Exception AccessToKeyVaultFailed(RequestFailedException innerExcep
details.AppendLine($"Message: {innerException.Message}");
details.AppendLine($"Error code: {innerException.ErrorCode}");
details.AppendLine($"Status code: {innerException.Status}");

return new ExitException("Access to Key Vault failed", innerException)
{
Help = "check if you have the required permissions to access the Key Vault",
Expand All @@ -66,4 +69,4 @@ public static Exception AuthenticationFailedForVault(Exception innerException) =
{
Help = $"try running {"az login".AsHighlighted()} to authenticate with Azure"
};
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using Azure;
using Azure.Identity;
using Confix.Tool;
using Confix.Tool.Commands.Logging;

namespace Confix.Utilities.Azure;

public static class KeyVaultExtension
{
public static async Task<T> HandleKeyVaultException<T>(Func<Task<T>> action)
public static async Task<T> HandleKeyVaultException<T>(Func<Task<T>> action, string? path = null)
{
try
{
return await action();
}
catch (RequestFailedException ex) when (ex.ErrorCode == "SecretNotFound")
{
throw ThrowHelper.SecretNotFound(ex);
throw ThrowHelper.SecretNotFound(ex, path);
}
catch (RequestFailedException ex)
{
Expand All @@ -26,4 +25,4 @@ public static async Task<T> HandleKeyVaultException<T>(Func<Task<T>> action)
throw ThrowHelper.AuthenticationFailedForVault(ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Task<IReadOnlyList<string>> ListAsync(CancellationToken cancellationToken
=> KeyVaultExtension.HandleKeyVaultException<IReadOnlyList<string>>(async () =>
{
App.Log.ListSecrets(_client.VaultUri);

var secrets = new List<string>();
await foreach (var secret in _client.GetPropertiesOfSecretsAsync(cancellationToken))
{
Expand All @@ -54,7 +54,7 @@ public Task<JsonNode> ResolveAsync(string path, CancellationToken cancellationTo
KeyVaultSecret result = await _client.GetSecretAsync(path.ToKeyVaultCompatiblePath(),
cancellationToken: cancellationToken);
return JsonValue.Create(result.Value)!;
});
}, path);

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

KeyVaultSecret result = await _client
.SetSecretAsync(path.ToKeyVaultCompatiblePath(), (string) value!, ct);
.SetSecretAsync(path.ToKeyVaultCompatiblePath(), (string)value!, ct);
return result.Name.ToConfixPath();
});
}, path);

public ValueTask DisposeAsync()
{
Expand All @@ -93,4 +93,4 @@ public static void ListSecrets(this IConsoleLogger log, Uri vaultUri)
{
log.Information($"List all secrets from Azure Kev Vault '{vaultUri}'");
}
}
}
Loading