-
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.
Merge pull request #95 from tintoy/feature/data-protection
Initial port of KubeClient.Extensions.DataProtection
- Loading branch information
Showing
24 changed files
with
3,563 additions
and
57 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
93 changes: 93 additions & 0 deletions
93
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,93 @@ | ||
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)); | ||
|
||
builder.Services.Configure<KeyManagementOptions>(options => | ||
{ | ||
KubeApiClient client = KubeApiClient.Create(clientOptions); | ||
|
||
// Persist secret data in the target K8s secret. | ||
options.XmlRepository = new KubeSecretXmlRepository(client, secretName, kubeNamespace ?? client.DefaultNamespace); | ||
}); | ||
|
||
return builder; | ||
} | ||
|
||
/// <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 => | ||
{ | ||
// Persist secret data in the target K8s secret. | ||
options.XmlRepository = new KubeSecretXmlRepository(client, secretName, kubeNamespace ?? client.DefaultNamespace); | ||
}); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
<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" /> | ||
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.0.0" /> | ||
</ItemGroup> | ||
|
||
<Import Project="..\Common.props" /> | ||
</Project> |
Oops, something went wrong.