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
19 changes: 14 additions & 5 deletions src/Confix.Tool/src/Confix.Library/ThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text;
using Azure;
using Confix.Tool.Commands.Logging;

namespace Confix.Tool;
Expand Down Expand Up @@ -42,15 +44,22 @@ public static Exception CouldNotParseJsonFile(FileInfo file)
public static Exception SecretNotFound(Exception innerException) =>
new ExitException("Secret does not exist in this provider.", innerException)
{
Help =
$"try running {"confix variable list".AsHighlighted()} to list all available variables"
Help = $"try running {"confix variable list".AsHighlighted()} to list all available variables"
};

public static Exception AccessToKeyVaultFailed(Exception innerException) =>
new ExitException("Access to Key Vault failed", innerException)
public static Exception AccessToKeyVaultFailed(RequestFailedException innerException)
{
var details = new StringBuilder();
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"
Help = "check if you have the required permissions to access the Key Vault",
Details = details.ToString()
};
}

public static Exception AuthenticationFailedForVault(Exception innerException) =>
new ExitException("Authentication for Key Vault failed", innerException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public ExitException(string? message, Exception? innerException)
}

public string? Help { get; init; }
public string? Details { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.CommandLine.Invocation;
using Confix.Tool.Commands.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Spectre.Console;

namespace Confix.Tool;
Expand Down Expand Up @@ -55,6 +54,7 @@ public static void HandleException(this InvocationContext context, Exception exc
case ExitException exitException:
logger.ExitException(exitException);
console.PrintHelp(exitException);
console.PrintDetails(exitException);
break;

case ValidationException validationException:
Expand Down Expand Up @@ -88,7 +88,15 @@ public static void PrintHelp(this IAnsiConsole console, ExitException exception)
console.Write(panel);
}
}


public static void PrintDetails(this IAnsiConsole console, ExitException exception)
{
if (exception.Details is not null)
{
console.MarkupLine($"[yellow]{exception.Details.EscapeMarkup()}[/]");
}
}

public static void ValidationException(
this IConsoleLogger logger,
ValidationException exception)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json.Nodes;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Confix.Tool.Commands.Logging;
using Confix.Utilities.Azure;
using Json.Schema;

Expand Down Expand Up @@ -36,6 +37,8 @@ public AzureKeyVaultProvider(SecretClient client)
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 Down Expand Up @@ -83,3 +86,11 @@ file static class Extensions

public static string ToKeyVaultCompatiblePath(this string path) => path.Replace('.', '-');
}

file static class LogExtensions
{
public static void ListSecrets(this IConsoleLogger log, Uri vaultUri)
{
log.Information($"List all secrets from Azure Kev Vault '{vaultUri}'");
}
}
Loading