-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial port of KubeClient.Extensions.DataProtection implementation
This code has been ported from https://github.com/rriverak/KubeClient.Extensions.DataProtection (thanks, @rriverak!). Note that, at this, stage, it is a work in progress (we need to decide on strategies for thread-safety and handling of common error conditions (such as the underlying Secret being deleted). Relates to #94.
- Loading branch information
Showing
6 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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
114 changes: 114 additions & 0 deletions
114
src/KubeClient.Extensions.DataProtection/DataProtectionExtensions.cs
This file contains 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,114 @@ | ||
using Microsoft.AspNetCore.DataProtection; | ||
using Microsoft.AspNetCore.DataProtection.KeyManagement; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
|
||
namespace KubeClient | ||
{ | ||
using Extensions.DataProtection; | ||
|
||
/// <summary> | ||
/// <see cref="IDataProtectionBuilder"/> extension methods to persist Keys in a Kubernetes Secret. | ||
/// </summary> | ||
public static class DataProtectionExtensions | ||
{ | ||
/// <summary> | ||
/// Add or Create a Kubernetes Secret as a Repository. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// The <see cref="IDataProtectionBuilder"/> to Configure. | ||
/// </param> | ||
/// <param name="clientOptions"> | ||
/// <see cref="KubeClientOptions"/> for the <see cref="KubeApiClient"/> used to communicate with the Kubernetes API. | ||
/// </param> | ||
/// <param name="secretName"> | ||
/// The name of the target Secret. | ||
/// </param> | ||
/// <param name="kubeNamespace"> | ||
/// The namespace of the target Secret. | ||
/// </param> | ||
/// <returns> | ||
/// The configured <see cref="IDataProtectionBuilder"/>. | ||
/// </returns> | ||
public static IDataProtectionBuilder PersistKeysToKubeSecret(this IDataProtectionBuilder builder, KubeClientOptions clientOptions, string secretName, string kubeNamespace = null) | ||
{ | ||
if (builder == null) | ||
throw new ArgumentNullException(nameof(builder)); | ||
|
||
if (clientOptions == null) | ||
throw new ArgumentNullException(nameof(clientOptions)); | ||
|
||
if (String.IsNullOrWhiteSpace(secretName)) | ||
throw new ArgumentException($"Argument cannot be null, empty, or entirely composed of whitespace: {nameof(secretName)}.", nameof(secretName)); | ||
|
||
KubeApiClient client = KubeApiClient.Create(clientOptions); | ||
|
||
return builder.PersistKeysToKubeSecret(client, secretName, kubeNamespace); | ||
} | ||
|
||
/// <summary> | ||
/// Add or Create a Kubernetes Secret as a Repository. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// The <see cref="IDataProtectionBuilder"/> to Configure. | ||
/// </param> | ||
/// <param name="client"> | ||
/// The <see cref="IKubeApiClient"/> used to communicate with the Kubernetes API. | ||
/// </param> | ||
/// <param name="secretName"> | ||
/// The name of the target Secret. | ||
/// </param> | ||
/// <param name="kubeNamespace"> | ||
/// The namespace of the target Secret. | ||
/// </param> | ||
/// <returns> | ||
/// The configured <see cref="IDataProtectionBuilder"/>. | ||
/// </returns> | ||
public static IDataProtectionBuilder PersistKeysToKubeSecret(this IDataProtectionBuilder builder, IKubeApiClient client, string secretName, string kubeNamespace = null) | ||
{ | ||
if (builder == null) | ||
throw new ArgumentNullException(nameof(builder)); | ||
|
||
if (client == null) | ||
throw new ArgumentNullException(nameof(client)); | ||
|
||
if (String.IsNullOrWhiteSpace(secretName)) | ||
throw new ArgumentException($"Argument cannot be null, empty, or entirely composed of whitespace: {nameof(secretName)}.", nameof(secretName)); | ||
|
||
builder.Services.Configure<KeyManagementOptions>(options => | ||
{ | ||
// Add KubeClientXmlRepository as KeyStore | ||
options.XmlRepository = new KubeSecretXmlRepository(client, secretName, kubeNamespace ?? client.DefaultNamespace); | ||
}); | ||
|
||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Internal Implementation | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// The <see cref="IDataProtectionBuilder"/> to Configure. | ||
/// </param> | ||
/// <param name="client"> | ||
/// <see cref="KubeApiClient"/> used to communicate with the Kubernetes API. | ||
/// </param> | ||
/// <param name="secretName"> | ||
/// The name of the target Secret. | ||
/// </param> | ||
/// <param name="kubeNamespace"> | ||
/// The namespace of the target Secret. | ||
/// </param> | ||
/// <returns>The <see cref="IDataProtectionBuilder"/> (enables method-chaining).</returns> | ||
private static IDataProtectionBuilder PersistKeysToKubeSecretInternal(IDataProtectionBuilder builder, IKubeApiClient client, string secretName, string kubeNamespace = null) | ||
{ | ||
builder.Services.Configure<KeyManagementOptions>(options => | ||
{ | ||
// Add KubeClientXmlRepository as KeyStore | ||
options.XmlRepository = new KubeSecretXmlRepository(client, secretName, kubeNamespace); | ||
}); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/KubeClient.Extensions.DataProtection/KubeClient.Extensions.DataProtection.csproj
This file contains 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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
|
||
<Description>KubeClient extensions for ASP.NET Core data-protection</Description> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\KubeClient\KubeClient.csproj" /> | ||
<ProjectReference Include="..\KubeClient.Extensions.Configuration\KubeClient.Extensions.Configuration.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
<Import Project="..\Common.props" /> | ||
</Project> |
Oops, something went wrong.