-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use --tags and --name for rotation config filtering (#5577)
* Add --tags and --name options to secret rotation * Add json schema for plan configuration files
- Loading branch information
Showing
21 changed files
with
378 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tools/secret-rotation/Azure.Sdk.Tools.SecretRotation.Cli/Commands/ListCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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 ListCommand : RotationCommandBase | ||
{ | ||
public ListCommand() : base("list", "List secret rotation plans") | ||
{ | ||
} | ||
|
||
protected override Task HandleCommandAsync(ILogger logger, RotationConfiguration rotationConfiguration, | ||
InvocationContext invocationContext) | ||
{ | ||
foreach (PlanConfiguration plan in rotationConfiguration.PlanConfigurations) | ||
{ | ||
Console.WriteLine($"name: {plan.Name} - tags: {string.Join(", ", plan.Tags)}"); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
36 changes: 1 addition & 35 deletions
36
tools/secret-rotation/Azure.Sdk.Tools.SecretRotation.Cli/Commands/RotateCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,34 @@ | ||
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); | ||
} | ||
IEnumerable<RotationPlan> plans = rotationConfiguration.GetAllRotationPlans(logger, 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."; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.