Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pem export #119

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public ExportCommand()
{
AddCommand(new ExportKeyCommand());
AddCommand(new ExportMaskinportenCommand());
AddCommand(new ExportPemCommand());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Altinn.Cli.Jwks.Stores;
using System.Buffers.Text;
using System.CommandLine;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
Expand Down Expand Up @@ -27,7 +26,7 @@
this.SetHandler(ExecuteAsync, Console, StoreOption, NameArg, ProdOption, CancellationToken);
}

private async Task<int> ExecuteAsync(

Check warning on line 29 in src/Altinn.Cli/src/Altinn.Cli.Jwks/Commands/ExportMaskinportenCommand.cs

View workflow job for this annotation

GitHub Actions / Analyze

Make 'ExecuteAsync' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)

Check warning on line 29 in src/Altinn.Cli/src/Altinn.Cli.Jwks/Commands/ExportMaskinportenCommand.cs

View workflow job for this annotation

GitHub Actions / Analyze

Make 'ExecuteAsync' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
IConsole console,
JsonWebKeySetStore store,
string name,
Expand Down
62 changes: 62 additions & 0 deletions src/Altinn.Cli/src/Altinn.Cli.Jwks/Commands/ExportPemCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Altinn.Cli.Jwks.Stores;
using Microsoft.IdentityModel.Tokens;
using System.CommandLine;
using System.CommandLine.IO;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;

namespace Altinn.Cli.Jwks.Commands;

[ExcludeFromCodeCoverage]
internal class ExportPemCommand
: BaseCommand
{
public static Argument<string> NameArg { get; }
= new Argument<string>("name", "Name of the integration to generate JWKs for.");

public static Option<bool> ProdOption { get; }
= new Option<bool>(
aliases: ["--prod", "-p"],
description: "Generate PROD keys. Defaults to true unless --test is specified.");

public ExportPemCommand()
: base("pem", "Export a public key in pem format")
{
AddArgument(NameArg);
AddOption(ProdOption);

this.SetHandler(ExecuteAsync, Console, StoreOption, NameArg, ProdOption, CancellationToken);
}

private async Task<int> ExecuteAsync(
IConsole console,
JsonWebKeySetStore store,
string name,
bool prod,
CancellationToken cancellationToken)
{
var environment = prod ? JsonWebKeySetEnvironment.Prod : JsonWebKeySetEnvironment.Test;
var keySet = await store.GetKeySet(name, environment, JsonWebKeySetVariant.Public, cancellationToken);

var signingKey = keySet.GetSigningKeys()[^1];
switch (signingKey)
{
case RsaSecurityKey rsa:
WriteRsa(console, rsa);
break;

default:
console.Error.WriteLine("Unsupported key type.");
return 1;
}

return 0;
}

private void WriteRsa(IConsole console, RsaSecurityKey key)

Check warning on line 56 in src/Altinn.Cli/src/Altinn.Cli.Jwks/Commands/ExportPemCommand.cs

View workflow job for this annotation

GitHub Actions / Analyze

Make 'WriteRsa' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)

Check warning on line 56 in src/Altinn.Cli/src/Altinn.Cli.Jwks/Commands/ExportPemCommand.cs

View workflow job for this annotation

GitHub Actions / Analyze

Make 'WriteRsa' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
var rsa = key.Rsa ?? RSA.Create(key.Parameters);
var pem = rsa.ExportSubjectPublicKeyInfoPem();
console.Out.WriteLine(pem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
"commandLineArgs": "--store ./test-keys export key br --base64",
"workingDirectory": "obj",
"hotReloadEnabled": false
},
"export key pem": {
"commandName": "Project",
"commandLineArgs": "--store ./test-keys export pem br",
"workingDirectory": "obj",
"hotReloadEnabled": false
}
}
}