Skip to content

Commit

Permalink
Add CLI command for generating a certificate for webhook signatures (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Nov 20, 2024
1 parent f0dea49 commit 06f663e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace TeachingRecordSystem.Cli;

public static partial class Commands
{
public static Command CreateGenerateWebhookSignatureCertificateCommand(IConfiguration configuration)
{
var command = new Command(
"generate-webhook-signature-certificate",
$"Generates a new self-signed certificate and outputs its private key and certificate to key.pem and certificate.pem, respectively.");

command.SetHandler(
() =>
{
var key = ECDsa.Create(ECCurve.NamedCurves.nistP384);
var certRequest = new CertificateRequest("CN=Teaching Record System", key, HashAlgorithmName.SHA384);
var cert = certRequest.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(1));
var certPem = cert.ExportCertificatePem();
var keyPem = key.ExportECPrivateKeyPem();

File.WriteAllText("key.pem", keyPem);
File.WriteAllText("certificate.pem", certPem);
});

return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Commands.CreateSyncPersonCommand(configuration),
Commands.CreateGenerateKeyCommand(configuration),
Commands.CreateDropDqtReportingReplicationSlotCommand(configuration),
Commands.CreateGenerateWebhookSignatureCertificateCommand(configuration),
};

return await rootCommand.InvokeAsync(args);
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ restore:

# Run the trscli
cli *ARGS:
@cd {{solution-root / "src" / "TeachingRecordSystem.Cli"}} && dotnet {{"bin" / "Debug" / "net8.0" / "trscli.dll"}} {{ARGS}}
@dotnet {{solution-root / "src" / "TeachingRecordSystem.Cli" / "bin" / "Debug" / "net8.0" / "trscli.dll"}} {{ARGS}}

# Build the .NET solution
build:
Expand Down

0 comments on commit 06f663e

Please sign in to comment.