-
-
Notifications
You must be signed in to change notification settings - Fork 74
feat(core): Add certificates to Operator.Web #756
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
30befc5
feat(web): Add local development webhooks
ian-buse ce7a7b5
feat(test): Add tests for certs and cert service
ian-buse 3bb58fb
chore(cli): Replace BouncyCastle with built-in libs
ian-buse ea1439a
fix(web): Set EnhancedKeyUsage.Critical to false
ian-buse cd29c5d
Fixes for PR
ian-buse 6f14d4b
Supress obsolete warning
ian-buse e0ec28d
Merge branch 'main' into x509lib
ian-buse 5175ed7
Use Lazy<T> for delayed creation of certs/keys
ian-buse f3f8c2e
Merge branch 'x509lib' of https://github.com/dh2i-devs/dotnet-operato…
ian-buse 3d7d2e1
Update Operator.Web README with feature
ian-buse d841413
Merge branch 'main' into x509lib
buehler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
using KubeOps.Cli.Certificates; | ||
using KubeOps.Cli.Output; | ||
using KubeOps.Operator.Web.Certificates; | ||
|
||
namespace KubeOps.Cli.Generators; | ||
|
||
internal class CertificateGenerator(string serverName, string namespaceName) : IConfigGenerator | ||
{ | ||
public void Generate(ResultOutput output) | ||
{ | ||
var (caCert, caKey) = Certificates.CertificateGenerator.CreateCaCertificate(); | ||
using Operator.Web.CertificateGenerator generator = new(serverName, namespaceName); | ||
|
||
output.Add("ca.pem", caCert.ToPem(), OutputFormat.Plain); | ||
output.Add("ca-key.pem", caKey.ToPem(), OutputFormat.Plain); | ||
|
||
var (srvCert, srvKey) = Certificates.CertificateGenerator.CreateServerCertificate( | ||
(caCert, caKey), | ||
serverName, | ||
namespaceName); | ||
|
||
output.Add("svc.pem", srvCert.ToPem(), OutputFormat.Plain); | ||
output.Add("svc-key.pem", srvKey.ToPem(), OutputFormat.Plain); | ||
output.Add("ca.pem", generator.Root.Certificate.EncodeToPem(), OutputFormat.Plain); | ||
output.Add("ca-key.pem", generator.Root.Key.EncodeToPem(), OutputFormat.Plain); | ||
output.Add("svc.pem", generator.Server.Certificate.EncodeToPem(), OutputFormat.Plain); | ||
output.Add("svc-key.pem", generator.Server.Key.EncodeToPem(), OutputFormat.Plain); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/KubeOps.Operator.Web/Certificates/CertificateExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Security.Cryptography; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Text; | ||
|
||
namespace KubeOps.Operator.Web.Certificates | ||
{ | ||
public static class CertificateExtensions | ||
{ | ||
/// <summary> | ||
/// Encodes the certificate in PEM format for use in Kubernetes. | ||
/// </summary> | ||
/// <param name="certificate">The certificate to encode.</param> | ||
/// <returns>The byte representation of the PEM-encoded certificate.</returns> | ||
public static byte[] EncodeToPemBytes(this X509Certificate2 certificate) => Encoding.ASCII.GetBytes(certificate.EncodeToPem()); | ||
|
||
/// <summary> | ||
/// Encodes the certificate in PEM format. | ||
/// </summary> | ||
/// <param name="certificate">The certificate to encode.</param> | ||
/// <returns>The string representation of the PEM-encoded certificate.</returns> | ||
public static string EncodeToPem(this X509Certificate2 certificate) => new(PemEncoding.Write("CERTIFICATE", certificate.RawData)); | ||
|
||
/// <summary> | ||
/// Encodes the key in PEM format. | ||
/// </summary> | ||
/// <param name="key">The key to encode.</param> | ||
/// <returns>The string representation of the PEM-encoded key.</returns> | ||
public static string EncodeToPem(this AsymmetricAlgorithm key) => new(PemEncoding.Write("PRIVATE KEY", key.ExportPkcs8PrivateKey())); | ||
|
||
/// <summary> | ||
/// Generates a new server certificate with its private key attached, and sets <see cref="X509KeyStorageFlags.PersistKeySet"/>. | ||
/// For example, this certificate can be used in development environments to configure <see cref="Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions"/>. | ||
/// </summary> | ||
/// <param name="server">The cert/key tuple to attach.</param> | ||
/// <returns>An <see cref="X509Certificate2"/> with the private key attached.</returns> | ||
/// <exception cref="NotImplementedException">The <see cref="AsymmetricAlgorithm"/> not have a CopyWithPrivateKey method, or the | ||
/// method has not been implemented in this extension.</exception> | ||
public static X509Certificate2 CopyServerCertWithPrivateKey(this (X509Certificate2 Certificate, AsymmetricAlgorithm Key) server) | ||
{ | ||
const string? password = null; | ||
using X509Certificate2 temp = server.Key switch | ||
{ | ||
ECDsa ecdsa => server.Certificate.CopyWithPrivateKey(ecdsa), | ||
RSA rsa => server.Certificate.CopyWithPrivateKey(rsa), | ||
ECDiffieHellman ecdh => server.Certificate.CopyWithPrivateKey(ecdh), | ||
DSA dsa => server.Certificate.CopyWithPrivateKey(dsa), | ||
_ => throw new NotImplementedException($"{server.Key} is not implemented for {nameof(CopyServerCertWithPrivateKey)}"), | ||
}; | ||
|
||
return new X509Certificate2( | ||
temp.Export(X509ContentType.Pfx, password), | ||
password, | ||
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.