Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add secret rotation tool #5564

Merged
merged 3 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<ToolName>secrets</ToolName>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Azure.Sdk.Tools.SecretRotation.Configuration\Azure.Sdk.Tools.SecretRotation.Configuration.csproj" />
<ProjectReference Include="..\Azure.Sdk.Tools.SecretRotation.Core\Azure.Sdk.Tools.SecretRotation.Core.csproj" />
<ProjectReference Include="..\Azure.Sdk.Tools.SecretRotation.Stores.AzureActiveDirectory\Azure.Sdk.Tools.SecretRotation.Stores.AzureActiveDirectory.csproj" />
<ProjectReference Include="..\Azure.Sdk.Tools.SecretRotation.Stores.AzureDevOps\Azure.Sdk.Tools.SecretRotation.Stores.AzureDevOps.csproj" />
<ProjectReference Include="..\Azure.Sdk.Tools.SecretRotation.Stores.Generic\Azure.Sdk.Tools.SecretRotation.Stores.Generic.csproj" />
<ProjectReference Include="..\Azure.Sdk.Tools.SecretRotation.Stores.KeyVault\Azure.Sdk.Tools.SecretRotation.Stores.KeyVault.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using Azure.Sdk.Tools.SecretRotation.Configuration;
using Azure.Sdk.Tools.SecretRotation.Core;

namespace Azure.Sdk.Tools.SecretRotation.Cli.Commands;

public class RotateCommand : RotationCommandBase
{
private readonly Option<bool> allOption = new(new[] { "--all", "-a" }, "Rotate all secrets");
private readonly Option<string[]> secretsOption = new(new[] { "--secrets", "-s" }, "Rotate only the specified secrets");
private readonly Option<bool> expiringOption = new(new[] { "--expiring", "-e" }, "Only rotate expiring secrets");
private readonly Option<bool> whatIfOption = new(new[] { "--dry-run", "-d" }, "Preview the changes that will be made without submitting them.");

public RotateCommand() : base("rotate", "Rotate one, expiring or all secrets")
{
AddOption(this.expiringOption);
AddOption(this.whatIfOption);
AddOption(this.allOption);
AddOption(this.secretsOption);
AddValidator(ValidateOptions);
}

protected override async Task HandleCommandAsync(ILogger logger, RotationConfiguration rotationConfiguration,
InvocationContext invocationContext)
{
bool onlyRotateExpiring = invocationContext.ParseResult.GetValueForOption(this.expiringOption);
bool all = invocationContext.ParseResult.GetValueForOption(this.allOption);
bool whatIf = invocationContext.ParseResult.GetValueForOption(this.whatIfOption);

var timeProvider = new TimeProvider();

IEnumerable<RotationPlan> plans;

if (all)
{
plans = rotationConfiguration.GetAllRotationPlans(logger, timeProvider);
}
else
{
string[] secretNames = invocationContext.ParseResult.GetValueForOption(this.secretsOption)!;

plans = rotationConfiguration.GetRotationPlans(logger, secretNames, timeProvider);
}

foreach (RotationPlan plan in plans)
{
await plan.ExecuteAsync(onlyRotateExpiring, whatIf);
}
}

private void ValidateOptions(CommandResult commandResult)
{
bool secretsUsed = commandResult.FindResultFor(this.secretsOption) is not null;
bool allUsed = commandResult.FindResultFor(this.allOption) is not null;

if (!(secretsUsed || allUsed))
{
commandResult.ErrorMessage = "Either the --secrets or the --all option must be provided.";
}

if (secretsUsed && allUsed)
{
commandResult.ErrorMessage = "The --secrets and --all options cannot both be provided.";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using Azure.Identity;
using Azure.Sdk.Tools.SecretRotation.Configuration;
using Azure.Sdk.Tools.SecretRotation.Core;
using Azure.Sdk.Tools.SecretRotation.Stores.AzureActiveDirectory;
using Azure.Sdk.Tools.SecretRotation.Stores.AzureDevOps;
using Azure.Sdk.Tools.SecretRotation.Stores.Generic;
using Azure.Sdk.Tools.SecretRotation.Stores.KeyVault;
using Microsoft.Extensions.Logging.Console;

namespace Azure.Sdk.Tools.SecretRotation.Cli.Commands;

public abstract class RotationCommandBase : Command
{
private readonly Option<string> configOption = new(new[] { "--config", "-c" }, "Configuration path")
{
IsRequired = true,
Arity = ArgumentArity.ExactlyOne
};

private readonly Option<bool> verboseOption = new(new[] { "--verbose", "-v" }, "Verbose output");

protected RotationCommandBase(string name, string description) : base(name, description)
{
AddOption(this.configOption);
AddOption(this.verboseOption);
this.SetHandler(ParseAndHandleCommandAsync);
}

protected abstract Task HandleCommandAsync(ILogger logger, RotationConfiguration rotationConfiguration,
InvocationContext invocationContext);

private async Task ParseAndHandleCommandAsync(InvocationContext invocationContext)
{
string configPath = invocationContext.ParseResult.GetValueForOption(this.configOption)!;
bool verbose = invocationContext.ParseResult.GetValueForOption(this.verboseOption);

LogLevel logLevel = verbose ? LogLevel.Trace : LogLevel.Information;

ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder
.AddConsoleFormatter<SimplerConsoleFormatter, ConsoleFormatterOptions>()
.AddConsole(options => options.FormatterName = SimplerConsoleFormatter.FormatterName)
.SetMinimumLevel(logLevel));

ILogger logger = loggerFactory.CreateLogger(string.Empty);

logger.LogDebug("Parsing configuration");

// TODO: Pass a logger to the token so it can verbose log getting tokens.
var tokenCredential = new AzureCliCredential();

IDictionary<string, Func<StoreConfiguration, SecretStore>> secretStoreFactories =
GetDefaultSecretStoreFactories(tokenCredential, logger);

// TODO: Pass a logger to RotationConfiguration so it can verbose log when reading from files.
RotationConfiguration rotationConfiguration = RotationConfiguration.From(configPath, secretStoreFactories);

await HandleCommandAsync(logger, rotationConfiguration, invocationContext);
}

private static IDictionary<string, Func<StoreConfiguration, SecretStore>> GetDefaultSecretStoreFactories(
AzureCliCredential tokenCredential, ILogger logger)
{
return new Dictionary<string, Func<StoreConfiguration, SecretStore>>
{
[RandomStringGenerator.MappingKey] = RandomStringGenerator.GetSecretStoreFactory(logger),
[KeyVaultSecretStore.MappingKey] = KeyVaultSecretStore.GetSecretStoreFactory(tokenCredential, logger),
[KeyVaultCertificateStore.MappingKey] = KeyVaultCertificateStore.GetSecretStoreFactory(tokenCredential, logger),
[ManualActionStore.MappingKey] = ManualActionStore.GetSecretStoreFactory(logger, new ConsoleValueProvider()),
[ServiceConnectionParameterStore.MappingKey] = ServiceConnectionParameterStore.GetSecretStoreFactory(tokenCredential, logger),
[AadApplicationSecretStore.MappingKey] = AadApplicationSecretStore.GetSecretStoreFactory(tokenCredential, logger)
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.CommandLine.Invocation;
using System.Text.Json;
using Azure.Sdk.Tools.SecretRotation.Configuration;
using Azure.Sdk.Tools.SecretRotation.Core;

namespace Azure.Sdk.Tools.SecretRotation.Cli.Commands;

public class StatusCommand : RotationCommandBase
{
public StatusCommand() : base("status", "Show secret rotation status")
{
}

protected override async Task HandleCommandAsync(ILogger logger, RotationConfiguration rotationConfiguration,
InvocationContext invocationContext)
{
var timeProvider = new TimeProvider();
IEnumerable<RotationPlan> plans = rotationConfiguration.GetAllRotationPlans(logger, timeProvider);

foreach (RotationPlan plan in plans)
{
Console.WriteLine();
Console.WriteLine($"{plan.Name}:");

RotationPlanStatus status = await plan.GetStatusAsync();
Console.WriteLine(JsonSerializer.Serialize(status, new JsonSerializerOptions { WriteIndented = true }));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Text;
using System.Windows.Forms;
using Azure.Sdk.Tools.SecretRotation.Stores.Generic;

namespace Azure.Sdk.Tools.SecretRotation.Cli;

internal class ConsoleValueProvider : IUserValueProvider
{
public string GetValue(string prompt, bool secret)
{
Console.Write($"{prompt}: ");
StringBuilder keysPressed = new();
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(secret);

switch (key.Key)
{
case ConsoleKey.Enter:
Console.WriteLine();
return keysPressed.ToString();
case ConsoleKey.Backspace:
{
if (keysPressed.Length > 0)
{
Console.Write(' ');
Console.Write(key.KeyChar);
keysPressed.Length -= 1;
}

break;
}
default:
keysPressed.Append(key.KeyChar);
break;
}
}
}

public void PromptUser(string prompt, string? oldValue, string? newValue)
{
Console.WriteLine();
Console.WriteLine(prompt);

bool promptNew = !string.IsNullOrEmpty(newValue);
bool promptOld = !string.IsNullOrEmpty(oldValue);

if (promptOld)
{
Console.WriteLine("Press Ctrl-O to copy the old value to the clipboard.");
}

if (promptNew)
{
Console.WriteLine("Press Ctrl-N to copy the new value to the clipboard.");
}

Console.WriteLine("Press Enter to continue.");

while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);

if (key is { Key: ConsoleKey.Enter })
{
break;
}

if (promptOld && key is { Modifiers: ConsoleModifiers.Control, Key: ConsoleKey.O })
{
SetClipboard(oldValue!);
}

if (promptNew && key is { Modifiers: ConsoleModifiers.Control, Key: ConsoleKey.N })
{
SetClipboard(newValue!);
}
}
}

private static void SetClipboard(string value)
{
Thread thread = new(() => Clipboard.SetText(value));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Global using directives

global using Microsoft.Extensions.Logging;
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.Text;
using Azure.Sdk.Tools.SecretRotation.Cli.Commands;
using Azure.Sdk.Tools.SecretRotation.Configuration;
using Azure.Sdk.Tools.SecretRotation.Core;

namespace Azure.Sdk.Tools.SecretRotation.Cli;

public class Program
{
public static async Task<int> Main(string[] args)
{
var rootCommand = new RootCommand("Secrets rotation tool");
rootCommand.AddCommand(new StatusCommand());
rootCommand.AddCommand(new RotateCommand());

CommandLineBuilder cliBuilder = new CommandLineBuilder(rootCommand)
.UseDefaults()
.UseExceptionHandler(HandleExceptions);

return await cliBuilder.Build().InvokeAsync(args);
}

private static void HandleExceptions(Exception exception, InvocationContext invocationContext)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(BuildErrorMessage(exception).TrimEnd());
Console.ResetColor();
}

private static string BuildErrorMessage(Exception exception)
{
var builder = new StringBuilder();

if (exception is RotationConfigurationException)
{
builder.AppendLine("Configuration error:");
AppendKnownExceptionMessage(builder, exception);
}
else if (exception is RotationException)
{
builder.AppendLine("Rotation error:");
AppendKnownExceptionMessage(builder, exception);
}
else if (exception is RotationCliException)
{
builder.AppendLine("Command invocation error:");
AppendKnownExceptionMessage(builder, exception);
}
else
{
AppendUnknownExceptionMessage(builder, exception);
}

return builder.ToString();
}

private static void AppendKnownExceptionMessage(StringBuilder builder, Exception exception)
{
builder.Append(" ");
builder.AppendJoin("\n ", exception.Message.Split("\n"));

if (exception.InnerException != null)
{
builder.Append("\n ");
builder.AppendJoin("\n ", exception.InnerException.Message.Split("\n"));
}
}

private static void AppendUnknownExceptionMessage(StringBuilder builder, Exception exception, int indent = 0,
bool isInnerException = false)
{
string indentString = new string(' ', indent);
string prefix = isInnerException ? "----> " : "";
string[] messageStrings = $"{prefix}{exception.GetType().Name}: {exception.Message}".Split('\n');

foreach (string messageString in messageStrings)
{
builder.Append(indentString);
builder.AppendLine(messageString);
}

if (exception is AggregateException aggregateException)
{
foreach (Exception innerException in aggregateException.InnerExceptions)
{
AppendUnknownExceptionMessage(builder, innerException, indent + 2, true);
}
}
else if (exception.InnerException != null)
{
AppendUnknownExceptionMessage(builder, exception.InnerException, indent + 2, true);
}
}
}
Loading