Skip to content

Commit

Permalink
fix(dependencies): upgrade dependencies and fix namespace in certs (#764
Browse files Browse the repository at this point in the history
)
  • Loading branch information
buehler authored May 22, 2024
1 parent f2e31e8 commit 7cc782e
Show file tree
Hide file tree
Showing 34 changed files with 491 additions and 481 deletions.
2 changes: 2 additions & 0 deletions examples/ConversionWebhookOperator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using KubeOps.Operator;
using KubeOps.Operator.Web.Builder;

#pragma warning disable CS0618 // Type or member is obsolete

var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddKubernetesOperator()
Expand Down
2 changes: 2 additions & 0 deletions examples/WebhookOperator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using KubeOps.Operator;
using KubeOps.Operator.Web.Builder;

#pragma warning disable CS0618 // Type or member is obsolete

var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddKubernetesOperator()
Expand Down
6 changes: 3 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
<ItemGroup>
<PackageReference
Include="StyleCop.Analyzers"
Version="1.2.0-beta.507"
Version="1.2.0-beta.556"
PrivateAssets="all"
Condition="$(MSBuildProjectExtension) == '.csproj'" />
<PackageReference
Include="SonarAnalyzer.CSharp"
Version="9.20.0.85982"
Version="9.25.0.90414"
PrivateAssets="all"
Condition="$(MSBuildProjectExtension) == '.csproj'" />
<PackageReference Include="Roslynator.Analyzers" Version="4.11.0" PrivateAssets="All" />
<PackageReference Include="Roslynator.Analyzers" Version="4.12.3" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 3 additions & 4 deletions src/KubeOps.Abstractions/Certificates/CertificatePair.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace KubeOps.Abstractions.Certificates
{
public record CertificatePair(X509Certificate2 Certificate, AsymmetricAlgorithm Key);
}
namespace KubeOps.Abstractions.Certificates;

public record CertificatePair(X509Certificate2 Certificate, AsymmetricAlgorithm Key);
28 changes: 12 additions & 16 deletions src/KubeOps.Abstractions/Certificates/ICertificateProvider.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace KubeOps.Abstractions.Certificates;

namespace KubeOps.Abstractions.Certificates
/// <summary>
/// Defines properties for certificate/key pair so a custom certificate/key provider may be implemented.
/// The provider is used by the CertificateWebhookService to provide a caBundle to the webhooks.
/// </summary>
public interface ICertificateProvider : IDisposable
{
/// <summary>
/// Defines properties for certificate/key pair so a custom certificate/key provider may be implemented.
/// The provider is used by the CertificateWebhookService to provide a caBundle to the webhooks.
/// The server certificate and key.
/// </summary>
public interface ICertificateProvider : IDisposable
{
/// <summary>
/// The server certificate and key.
/// </summary>
CertificatePair Server { get; }
CertificatePair Server { get; }

/// <summary>
/// The root certificate and key.
/// </summary>
CertificatePair Root { get; }
}
/// <summary>
/// The root certificate and key.
/// </summary>
CertificatePair Root { get; }
}
2 changes: 1 addition & 1 deletion src/KubeOps.Abstractions/KubeOps.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="KubernetesClient" Version="12.1.1"/>
<PackageReference Include="KubernetesClient" Version="14.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0"/>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/KubeOps.Cli/Generators/CertificateGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class CertificateGenerator(string serverName, string namespaceName) : I
{
public void Generate(ResultOutput output)
{
using Operator.Web.CertificateGenerator generator = new(serverName, namespaceName);
using Operator.Web.Certificates.CertificateGenerator generator = new(serverName, namespaceName);

output.Add("ca.pem", generator.Root.Certificate.EncodeToPem(), OutputFormat.Plain);
output.Add("ca-key.pem", generator.Root.Key.EncodeToPem(), OutputFormat.Plain);
Expand Down
2 changes: 1 addition & 1 deletion src/KubeOps.Cli/Generators/RbacGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Generate(ResultOutput output)

var roleBinding = new V1ClusterRoleBinding(
roleRef: new V1RoleRef(V1ClusterRole.KubeGroup, V1ClusterRole.KubeKind, "operator-role"),
subjects: new List<V1Subject>
subjects: new List<Rbacv1Subject>
{
new(V1ServiceAccount.KubeKind, "default", namespaceProperty: "system"),
})
Expand Down
16 changes: 8 additions & 8 deletions src/KubeOps.Cli/KubeOps.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.7.1" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.8.0" />
<PackageReference Include="Spectre.Console" Version="0.48.0" />
<PackageReference Include="Spectre.Console.Analyzer" Version="0.48.0">
<PackageReference Include="Microsoft.Build.Locator" Version="1.7.8" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.9.2" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.9.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.9.2" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.9.2" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.Analyzer" Version="0.49.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions src/KubeOps.Cli/Transpilation/BaseWebhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace KubeOps.Cli.Transpilation;

internal abstract record BaseWebhook(TypeInfo Webhook, EntityMetadata Metadata)
{
public abstract string WebhookPath { get; }

private bool HasCreate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Create"));

private bool HasUpdate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Update"));

private bool HasDelete => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Delete"));

public abstract string WebhookPath { get; }

public string[] GetOperations() =>
new[] { HasCreate ? "CREATE" : null, HasUpdate ? "UPDATE" : null, HasDelete ? "DELETE" : null, }
.Where(o => o is not null).ToArray()!;
Expand Down
9 changes: 7 additions & 2 deletions src/KubeOps.Generator/KubeOps.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="[4.0.0,)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp"
Version="[4.0.0,)"
PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)\$(AssemblyName).dll"
Pack="true"
PackagePath="analyzers/dotnet/cs"
Visible="false" />
</ItemGroup>

</Project>
87 changes: 43 additions & 44 deletions src/KubeOps.Operator.Web/Certificates/CertificateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,54 @@

using KubeOps.Abstractions.Certificates;

namespace KubeOps.Operator.Web.Certificates
namespace KubeOps.Operator.Web.Certificates;

public static class CertificateExtensions
{
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.UTF8.GetBytes(certificate.EncodeToPem());
/// <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.UTF8.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 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>
/// 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="serverPair">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 CertificatePair serverPair)
/// <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="serverPair">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"/> does not have a CopyWithPrivateKey method, or the
/// method has not been implemented in this extension.</exception>
public static X509Certificate2 CopyServerCertWithPrivateKey(this CertificatePair serverPair)
{
const string? password = null;
using X509Certificate2 temp = serverPair.Key switch
{
const string? password = null;
using X509Certificate2 temp = serverPair.Key switch
{
ECDsa ecdsa => serverPair.Certificate.CopyWithPrivateKey(ecdsa),
RSA rsa => serverPair.Certificate.CopyWithPrivateKey(rsa),
ECDiffieHellman ecdh => serverPair.Certificate.CopyWithPrivateKey(ecdh),
DSA dsa => serverPair.Certificate.CopyWithPrivateKey(dsa),
_ => throw new NotImplementedException($"{serverPair.Key} is not implemented for {nameof(CopyServerCertWithPrivateKey)}"),
};
ECDsa ecdsa => serverPair.Certificate.CopyWithPrivateKey(ecdsa),
RSA rsa => serverPair.Certificate.CopyWithPrivateKey(rsa),
ECDiffieHellman ecdh => serverPair.Certificate.CopyWithPrivateKey(ecdh),
DSA dsa => serverPair.Certificate.CopyWithPrivateKey(dsa),
_ => throw new NotImplementedException($"{serverPair.Key} is not implemented for {nameof(CopyServerCertWithPrivateKey)}"),
};

return new X509Certificate2(
temp.Export(X509ContentType.Pfx, password),
password,
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
}
return new X509Certificate2(
temp.Export(X509ContentType.Pfx, password),
password,
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
}
}
Loading

0 comments on commit 7cc782e

Please sign in to comment.