Skip to content

Commit 4d60f8a

Browse files
committed
Add info command
1 parent 2b599ba commit 4d60f8a

File tree

4 files changed

+132
-16
lines changed

4 files changed

+132
-16
lines changed

src/EasySign.Cli/BundleCommandProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public override RootCommand GetRootCommand()
2626
RootCommand root = new RootCommand("Easy Digital Signing Tool")
2727
{
2828
Add,
29+
Info,
2930
Sign,
3031
Verify,
3132
SelfSign,

src/EasySign.CommandLine/BundleWorker.cs

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,67 @@ protected virtual void RunAdd(StatusContext statusContext, string[] files, bool
201201
}
202202
}
203203

204+
protected virtual void RunInfo(StatusContext statusContext)
205+
{
206+
Logger.LogInformation("Running info command");
207+
208+
if (Bundle == null)
209+
{
210+
throw new ApplicationException("Bundle is not initialized");
211+
}
212+
213+
Logger.LogDebug("Loading bundle");
214+
statusContext.Status("[yellow]Loading Bundle[/]");
215+
if (!LoadBundle()) return;
216+
217+
Grid bundleGrid = new Grid();
218+
bundleGrid.AddColumn(new GridColumn().NoWrap());
219+
bundleGrid.AddColumn(new GridColumn().PadLeft(2));
220+
221+
bundleGrid.AddRow("Bundle Info:");
222+
bundleGrid.AddRow(" Full Path:", Bundle.BundlePath);
223+
bundleGrid.AddRow(" Updated By:", Bundle.Manifest.UpdatedBy ?? "N/A");
224+
bundleGrid.AddRow(" Protected Entry Names:", Bundle.Manifest.ProtectedEntryNames.Count.ToString());
225+
bundleGrid.AddRow(" Store Files In Bundle:", Bundle.Manifest.StoreOriginalFiles ? "Yes" : "No");
226+
bundleGrid.AddRow(" Manifest Entries:", Bundle.Manifest.Entries.Count.ToString());
227+
bundleGrid.AddRow(" Manifest Is Signed:", Bundle.Signatures.Entries.Count > 0 ? "Yes" : "No");
228+
bundleGrid.AddRow(" Signature Count:", Bundle.Signatures.Entries.Count.ToString());
229+
230+
AnsiConsole.Write(bundleGrid);
231+
AnsiConsole.WriteLine();
232+
233+
Grid protectedEntries = new Grid();
234+
protectedEntries.AddColumn(new GridColumn().NoWrap());
235+
236+
protectedEntries.AddRow("Protected Entry Names:");
237+
238+
foreach (var entryName in Bundle.Manifest.ProtectedEntryNames)
239+
{
240+
protectedEntries.AddRow($" {entryName}");
241+
}
242+
243+
AnsiConsole.Write(protectedEntries);
244+
AnsiConsole.WriteLine();
245+
246+
Grid manifestEntries = new Grid();
247+
manifestEntries.AddColumn(new GridColumn());
248+
manifestEntries.AddColumn(new GridColumn().PadLeft(2).Width(18));
249+
250+
manifestEntries.AddRow("Manifest Entries:");
251+
manifestEntries.AddRow(" Entry Name", "Hash");
252+
253+
foreach (var entry in Bundle.Manifest.Entries)
254+
{
255+
var entryHash = BitConverter.ToString(entry.Value).Replace("-", "");
256+
manifestEntries.AddRow($" {entry.Key}", $"{entryHash[0..8]}..{entryHash.Substring(entryHash.Length - 8)}");
257+
}
258+
259+
AnsiConsole.Write(manifestEntries);
260+
AnsiConsole.WriteLine();
261+
262+
CertificateUtilities.DisplayCertificate(Bundle.Signatures.Entries.Keys.Select(Bundle.GetCertificate).ToArray());
263+
}
264+
204265
/// <summary>
205266
/// Runs the sign command.
206267
/// </summary>
@@ -236,19 +297,7 @@ protected virtual void RunSign(StatusContext statusContext, X509Certificate2Coll
236297
Logger.LogDebug("Loading certificate information for {Cert}", cert);
237298
statusContext.Status("[yellow]Loading certificate informations[/]");
238299

239-
Grid grid = new Grid();
240-
grid.AddColumn(new GridColumn().NoWrap());
241-
grid.AddColumn(new GridColumn().PadLeft(2));
242-
grid.AddRow("Certificate Info:");
243-
grid.AddRow(" Common Name", cert.GetNameInfo(X509NameType.SimpleName, false));
244-
grid.AddRow(" Issuer Name", cert.GetNameInfo(X509NameType.SimpleName, true));
245-
grid.AddRow(" Holder Email", cert.GetNameInfo(X509NameType.EmailName, false));
246-
grid.AddRow(" Valid From", cert.GetEffectiveDateString());
247-
grid.AddRow(" Valid To", cert.GetExpirationDateString());
248-
grid.AddRow(" Thumbprint", cert.Thumbprint);
249-
250-
AnsiConsole.Write(grid);
251-
AnsiConsole.WriteLine();
300+
CertificateUtilities.DisplayCertificate(cert);
252301

253302
Logger.LogDebug("Verifying certificate {cert}", cert);
254303
statusContext.Status("[yellow]Verifying Certificate[/]");

src/EasySign.CommandLine/CertificateUtilities.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text;
77
using System.Threading.Tasks;
88
using EnsureThat;
9+
using Spectre.Console;
910

1011
namespace SAPTeam.EasySign.CommandLine
1112
{
@@ -14,6 +15,49 @@ namespace SAPTeam.EasySign.CommandLine
1415
/// </summary>
1516
public static class CertificateUtilities
1617
{
18+
/// <summary>
19+
/// Prints the details of given X.509 certificates to the console.
20+
/// </summary>
21+
/// <param name="certificates">
22+
/// The X.509 certificates to display.
23+
/// </param>
24+
public static void DisplayCertificate(params X509Certificate2[] certificates)
25+
{
26+
Grid grid = new Grid();
27+
grid.AddColumn(new GridColumn().NoWrap());
28+
grid.AddColumn(new GridColumn().PadLeft(2));
29+
30+
int index = 1;
31+
foreach (var certificate in certificates)
32+
{
33+
if (index > 1)
34+
{
35+
grid.AddRow();
36+
}
37+
38+
if (certificates.Length > 1)
39+
{
40+
grid.AddRow($"Certificate #{index}:");
41+
}
42+
else
43+
{
44+
grid.AddRow("Certificate Info:");
45+
}
46+
47+
grid.AddRow(" Common Name", certificate.GetNameInfo(X509NameType.SimpleName, false));
48+
grid.AddRow(" Issuer Name", certificate.GetNameInfo(X509NameType.SimpleName, true));
49+
grid.AddRow(" Holder Email", certificate.GetNameInfo(X509NameType.EmailName, false));
50+
grid.AddRow(" Valid From", certificate.GetEffectiveDateString());
51+
grid.AddRow(" Valid To", certificate.GetExpirationDateString());
52+
grid.AddRow(" Thumbprint", certificate.Thumbprint);
53+
54+
index++;
55+
}
56+
57+
AnsiConsole.Write(grid);
58+
AnsiConsole.WriteLine();
59+
}
60+
1761
/// <summary>
1862
/// Prompts the user for certificate subject information and generates a standardized subject name.
1963
/// </summary>

src/EasySign.CommandLine/CommandProvider.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,28 @@ public Command Add
124124
}
125125
}
126126

127+
/// <summary>
128+
/// Gets the command for Showing bundle information
129+
/// </summary>
130+
public Command Info
131+
{
132+
get
133+
{
134+
Command command = new Command("info", "Show bundle information")
135+
{
136+
BundlePath,
137+
};
138+
139+
command.SetHandler((bundlePath) =>
140+
{
141+
InitializeBundle(bundlePath);
142+
Utilities.RunInStatusContext("[yellow]Preparing[/]", ctx => RunInfo(ctx));
143+
}, BundlePath);
144+
145+
return command;
146+
}
147+
}
148+
127149
/// <summary>
128150
/// Gets the command for signing bundle with one or more certificate.
129151
/// </summary>
@@ -252,9 +274,9 @@ public Command Verify
252274
get
253275
{
254276
Command command = new Command("verify", "Verify bundle")
255-
{
256-
BundlePath,
257-
};
277+
{
278+
BundlePath,
279+
};
258280

259281
command.SetHandler((bundlePath) =>
260282
{

0 commit comments

Comments
 (0)