|
1 | 1 | using System.CommandLine; |
| 2 | +using System.Diagnostics.Metrics; |
| 3 | +using System.Security.AccessControl; |
2 | 4 | using System.Security.Cryptography.X509Certificates; |
3 | 5 | using System.Text; |
4 | 6 | using System.Text.Json; |
@@ -334,45 +336,73 @@ public Command SelfSign |
334 | 336 | countryOption, |
335 | 337 | }; |
336 | 338 |
|
337 | | - command.SetHandler((string commonName, string email, string organization, string organizationalUnit, string locality, string state, string country) => |
338 | | - { |
339 | | - if (GetSelfSigningRootCA() != null) |
340 | | - { |
341 | | - AnsiConsole.MarkupLine("[red]Root CA already exists![/]"); |
342 | | - return; |
343 | | - } |
| 339 | + command.SetHandler(RunSelfSign, cnOption, emailOption, orgOption, ouOption, locOption, stateOption, countryOption); |
344 | 340 |
|
345 | | - string subject; |
| 341 | + return command; |
| 342 | + } |
| 343 | + } |
346 | 344 |
|
347 | | - if (string.IsNullOrEmpty(commonName)) |
348 | | - { |
349 | | - subject = CertificateUtilities.GetSubjectNameFromUser(); |
350 | | - } |
351 | | - else |
352 | | - { |
353 | | - subject = new CertificateSubject(commonName: commonName, |
354 | | - email: email, |
355 | | - organization: organization, |
356 | | - organizationalUnit: organizationalUnit, |
357 | | - locality: locality, |
358 | | - state: state, |
359 | | - country: country).ToString(); |
360 | | - } |
| 345 | + /// <summary> |
| 346 | + /// Runs the self-sign command to create a self-signed root CA certificate. |
| 347 | + /// </summary> |
| 348 | + /// <param name="commonName">Common Name (CN) - required. if not specified, will prompt for user input.</param> |
| 349 | + /// <param name="email">Email (E) - optional.</param> |
| 350 | + /// <param name="organization">Organization (O) - optional.</param> |
| 351 | + /// <param name="organizationalUnit">Organizational Unit (OU) - optional.</param> |
| 352 | + /// <param name="locality">Locality (L) - optional.</param> |
| 353 | + /// <param name="state">State or Province (ST) - optional.</param> |
| 354 | + /// <param name="country">Country (C) - optional.</param> |
| 355 | + public virtual void RunSelfSign(string? commonName, string? email, string? organization, string? organizationalUnit, string? locality, string? state, string? country) |
| 356 | + { |
| 357 | + Logger.LogInformation("Running self-sign command"); |
| 358 | + |
| 359 | + if (GetSelfSigningRootCA() != null) |
| 360 | + { |
| 361 | + Logger.LogWarning("Root CA already exists"); |
| 362 | + AnsiConsole.MarkupLine("[red]Root CA already exists![/]"); |
| 363 | + return; |
| 364 | + } |
361 | 365 |
|
362 | | - var rootCA = CertificateUtilities.CreateSelfSignedCACertificate(subject); |
| 366 | + string subject; |
363 | 367 |
|
364 | | - using (FileStream fs = File.Create(Path.Combine(AppDirectory, $"rootCA.pfx"))) |
365 | | - { |
366 | | - fs.Write(rootCA.Export(X509ContentType.Pfx)); |
367 | | - } |
| 368 | + if (string.IsNullOrEmpty(commonName)) |
| 369 | + { |
| 370 | + Logger.LogDebug("Getting subject name from user"); |
| 371 | + subject = CertificateUtilities.GetSubjectNameFromUser(); |
| 372 | + } |
| 373 | + else |
| 374 | + { |
| 375 | + subject = new CertificateSubject(commonName: commonName, |
| 376 | + email: email, |
| 377 | + organization: organization, |
| 378 | + organizationalUnit: organizationalUnit, |
| 379 | + locality: locality, |
| 380 | + state: state, |
| 381 | + country: country).ToString(); |
| 382 | + } |
368 | 383 |
|
369 | | - CertificateUtilities.DisplayCertificate(rootCA); |
| 384 | + Logger.LogInformation("Creating self-signed root CA certificate with subject: {subject}", subject); |
| 385 | + var rootCA = CertificateUtilities.CreateSelfSignedCACertificate(subject); |
| 386 | + Logger.LogDebug("Root CA certificate issued with subject: {subject}", rootCA.Subject); |
370 | 387 |
|
371 | | - AnsiConsole.MarkupLine($"[green]Root CA created successfully![/]"); |
372 | | - }, cnOption, emailOption, orgOption, ouOption, locOption, stateOption, countryOption); |
| 388 | + Logger.LogDebug("Exporting root CA certificate to PFX file"); |
| 389 | + using (FileStream fs = File.Create(Path.Combine(AppDirectory, $"rootCA.pfx"))) |
| 390 | + { |
| 391 | + fs.Write(rootCA.Export(X509ContentType.Pfx)); |
| 392 | + } |
373 | 393 |
|
374 | | - return command; |
| 394 | + Logger.LogDebug("Clearing issued certificates"); |
| 395 | + |
| 396 | + Configuration.IssuedCertificates.Clear(); |
| 397 | + if (Directory.Exists(Path.Combine(AppDirectory, "certs"))) |
| 398 | + { |
| 399 | + Directory.Delete(Path.Combine(AppDirectory, "certs"), true); |
375 | 400 | } |
| 401 | + |
| 402 | + CertificateUtilities.DisplayCertificate(rootCA); |
| 403 | + |
| 404 | + Logger.LogInformation("Root CA created successfully"); |
| 405 | + AnsiConsole.MarkupLine($"[green]Root CA created successfully![/]"); |
376 | 406 | } |
377 | 407 |
|
378 | 408 | /// <summary> |
|
0 commit comments