Skip to content

Commit b522c9c

Browse files
committed
Add config command
1 parent 6c8e98f commit b522c9c

File tree

6 files changed

+147
-9
lines changed

6 files changed

+147
-9
lines changed

src/EasySign.Cli/BundleCommandProvider.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,19 @@ public override RootCommand GetRootCommand()
3030
Info,
3131
Sign,
3232
Verify,
33-
SelfSign,
34-
Trust,
33+
Config,
3534
};
3635

36+
if (Configuration.Settings["selfsign.enable"])
37+
{
38+
root.Add(SelfSign);
39+
}
40+
41+
if (Configuration.Settings["trust.enable"])
42+
{
43+
root.Add(Trust);
44+
}
45+
3746
return root;
3847
}
3948
}

src/EasySign.Cli/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ private static int Main(string[] args)
5656
appLogger.Information("Shutting down EasySign CLI at {DateTime} with exit code {ExitCode}", DateTime.Now, exitCode);
5757

5858
string data = JsonSerializer.Serialize(config, config.GetType(), SourceGenerationConfigurationContext.Default);
59-
using (FileStream fs = File.OpenWrite(ConfigPath))
59+
60+
if(File.Exists(ConfigPath))
61+
{
62+
File.Delete(ConfigPath);
63+
}
64+
65+
using (FileStream fs = File.Create(ConfigPath))
6066
{
6167
fs.Write(Encoding.UTF8.GetBytes(data));
6268
}

src/EasySign.CommandLine/BundleWorker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ protected bool VerifyCertificate(X509Certificate2 certificate, bool ignoreTime)
577577
verificationResults.Add(defaultVerification);
578578
verificationStatuses.Add(defaultChainStatuses);
579579

580-
if (!verificationResults.Any(x => x) && Configuration.TrustedRootCA.Count > 0)
580+
if (Configuration.Settings["trust.enable"] && !verificationResults.Any(x => x) && Configuration.TrustedRootCA.Count > 0)
581581
{
582582
policy.TrustMode = X509ChainTrustMode.CustomRootTrust;
583583
policy.CustomTrustStore.AddRange(Configuration.LoadCertificates(CertificateStore.TrustedRootCA));

src/EasySign.CommandLine/CommandProvider.cs

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,16 @@ public Command Sign
169169

170170
if (selfSign)
171171
{
172+
if (!Configuration.Settings["selfsign.enable"])
173+
{
174+
AnsiConsole.MarkupLine("[red]Self-Signing feature is disabled[/]");
175+
return;
176+
}
177+
172178
X509Certificate2? rootCA = GetSelfSigningRootCA();
173179
if (rootCA == null)
174180
{
175-
AnsiConsole.MarkupLine("[red]Self-Signing Root CA not found![/]");
181+
AnsiConsole.MarkupLine("[red]Self-Signing Root CA not found[/]");
176182
return;
177183
}
178184

@@ -432,13 +438,91 @@ public Command Trust
432438
}
433439
}, idArg, interOpt);
434440

435-
Command command = new Command("trust", "Manage trusted root CAs and intermediate CAs")
441+
Command command = new Command("trust", "Manage trusted root CAs and intermediate CAs");
442+
443+
if (Configuration.Settings["trust.enable"])
444+
{
445+
command.AddCommand(addCmd);
446+
command.AddCommand(listCmd);
447+
command.AddCommand(removeCmd);
448+
}
449+
else
450+
{
451+
command.SetHandler(() =>
452+
{
453+
AnsiConsole.MarkupLine("[red]Custom trust store feature is disabled[/]");
454+
return;
455+
});
456+
}
457+
458+
return command;
459+
}
460+
}
461+
462+
/// <summary>
463+
/// Gets the command for managing configuration settings.
464+
/// </summary>
465+
public Command Config
466+
{
467+
get
468+
{
469+
var keyArg = new Argument<string>("key", "Key to set or get\n" +
470+
"if not specified, will list all keys")
471+
{
472+
Arity = ArgumentArity.ZeroOrOne,
473+
};
474+
475+
var valueArg = new Argument<string>("value", "Value to set\n" +
476+
"if not specified, will get the value of the key")
477+
{
478+
Arity = ArgumentArity.ZeroOrOne,
479+
};
480+
481+
var forceOpt = new Option<bool>("--force", "Set value even if it is not existing");
482+
forceOpt.AddAlias("-f");
483+
484+
var command = new Command("config", "Get or set configuration values")
436485
{
437-
addCmd,
438-
listCmd,
439-
removeCmd,
486+
keyArg,
487+
valueArg,
488+
forceOpt,
440489
};
441490

491+
command.SetHandler((key, value, force) =>
492+
{
493+
if (string.IsNullOrEmpty(value))
494+
{
495+
var items = string.IsNullOrEmpty(key) ? Configuration.Settings : Configuration.Settings.Where(x => x.Key.StartsWith(key));
496+
497+
foreach (var item in items)
498+
{
499+
AnsiConsole.WriteLine($"{item.Key} = {item.Value}");
500+
}
501+
}
502+
else
503+
{
504+
if (!force && !Configuration.Settings.ContainsKey(key))
505+
{
506+
AnsiConsole.MarkupLine($"[red]Invalid key: {key}[/]");
507+
return;
508+
}
509+
510+
bool bValue;
511+
try
512+
{
513+
bValue = Utilities.ParseToBool(value);
514+
}
515+
catch
516+
{
517+
AnsiConsole.MarkupLine($"[red]Invalid value: {value}[/]");
518+
return;
519+
}
520+
521+
Configuration.Settings[key] = bValue;
522+
AnsiConsole.MarkupLine($"[green]{key} set to {Configuration.Settings[key]}[/]");
523+
}
524+
}, keyArg, valueArg, forceOpt);
525+
442526
return command;
443527
}
444528
}
@@ -456,6 +540,12 @@ public Command Trust
456540
/// <param name="country">Country (C) - optional.</param>
457541
public virtual void RunSelfSign(bool force, string? commonName, string? email, string? organization, string? organizationalUnit, string? locality, string? state, string? country)
458542
{
543+
if (!Configuration.Settings["selfsign.enable"])
544+
{
545+
AnsiConsole.MarkupLine("[red]Self-Signing feature is disabled[/]");
546+
return;
547+
}
548+
459549
Logger.LogInformation("Running self-sign command");
460550

461551
if (force || Configuration.SelfSignedRootCA != null)

src/EasySign.CommandLine/CommandProviderConfiguration.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ namespace SAPTeam.EasySign.CommandLine
1313
/// </summary>
1414
public class CommandProviderConfiguration
1515
{
16+
/// <summary>
17+
/// Gets or sets the settings for the command provider.
18+
/// </summary>
19+
public Dictionary<string, bool> Settings { get; set; } = new Dictionary<string, bool>
20+
{
21+
["trust.enable"] = true,
22+
["selfsign.enable"] = true,
23+
};
24+
1625
/// <summary>
1726
/// Gets or sets the list of prefixes that should be protected from modification.
1827
/// </summary>

src/EasySign.CommandLine/Utilities.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,29 @@ public static void EnumerateStatuses(X509ChainStatus[] statuses)
124124
AnsiConsole.MarkupLine($"[{Color.IndianRed}] {status.StatusInformation}[/]");
125125
}
126126
}
127+
128+
/// <summary>
129+
/// Parses a string to a boolean value.
130+
/// </summary>
131+
/// <param name="input">
132+
/// The string to parse.
133+
/// </param>
134+
/// <returns>
135+
/// <see cref="bool"/> representation of the input string.
136+
/// </returns>
137+
/// <exception cref="FormatException"></exception>
138+
public static bool ParseToBool(string input)
139+
{
140+
if (int.TryParse(input, out int number))
141+
{
142+
return number == 1;
143+
}
144+
if (bool.TryParse(input, out bool boolean))
145+
{
146+
return boolean;
147+
}
148+
149+
throw new FormatException($"Cannot convert '{input}' to a boolean.");
150+
}
127151
}
128152
}

0 commit comments

Comments
 (0)