-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from Infisical/daniel/configuration-provider
feat: dotnet configuration provider
- Loading branch information
Showing
13 changed files
with
714 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Release .NET SDK | ||
run-name: Release .NET SDK | ||
|
||
on: | ||
push: | ||
tags: | ||
- "*.*.*" # version, e.g. 1.0.0 | ||
|
||
jobs: | ||
build_dotnet: | ||
name: Build .NET | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 | ||
|
||
- name: Set up .NET Core | ||
uses: actions/setup-dotnet@3447fd6a9f9e57506b15f895c5b76d3b197dc7c2 # v3.2.0 | ||
with: | ||
global-json-file: languages/csharp/global.json | ||
|
||
- name: Build .NET 8 Project | ||
working-directory: InfisicalConfiguration | ||
run: | | ||
dotnet restore | ||
dotnet build --configuration Release | ||
- name: Pack NuGet Package | ||
env: | ||
VERSION: ${{ github.ref_name }} | ||
run: dotnet pack --configuration Release -p:PackageID=Infisical.IConfigurationProvider -p:Version=${{env.VERSION}} --output ./nuget-output /nologo /v:n | ||
working-directory: InfisicalConfiguration | ||
|
||
- name: Publish NuGet Package | ||
run: dotnet nuget push ./InfisicalConfiguration/nuget-output/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json |
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,45 @@ | ||
*.swp | ||
*.*~ | ||
project.lock.json | ||
.DS_Store | ||
*.pyc | ||
nupkg/ | ||
|
||
# Visual Studio Code | ||
.vscode/ | ||
|
||
# Rider | ||
.idea/ | ||
|
||
# Visual Studio | ||
.vs/ | ||
|
||
# Fleet | ||
.fleet/ | ||
|
||
# Code Rush | ||
.cr/ | ||
|
||
# User-specific files | ||
*.suo | ||
*.user | ||
*.userosscache | ||
*.sln.docstates | ||
|
||
# Build results | ||
[Dd]ebug/ | ||
[Dd]ebugPublic/ | ||
[Rr]elease/ | ||
[Rr]eleases/ | ||
x64/ | ||
x86/ | ||
build/ | ||
bld/ | ||
[Bb]in/ | ||
[Oo]bj/ | ||
[Oo]ut/ | ||
msbuild.log | ||
msbuild.err | ||
msbuild.wrn | ||
|
||
nuget-output |
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,43 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace InfisicalConfiguration.Tests; | ||
|
||
[TestClass] | ||
public class SystemTests | ||
{ | ||
|
||
public TestContext TestContext { get; set; } | ||
|
||
|
||
[TestMethod] | ||
public void ShouldGetSecretsFromTestAccount() | ||
{ | ||
|
||
|
||
var configuration = new ConfigurationBuilder() | ||
.AddInfisical( | ||
new InfisicalConfigBuilder() | ||
.SetProjectId("<project-id>") | ||
.SetEnvironment("dev") | ||
.SetSecretPath("/") | ||
.SetInfisicalUrl("http://localhost:8080") | ||
.SetAuth( | ||
new InfisicalAuthBuilder() | ||
.SetUniversalAuth( | ||
"<machine-identity-client-id>", | ||
"<machine-identity-client-secret>" | ||
) | ||
.Build() | ||
) | ||
.Build() | ||
) | ||
.Build(); | ||
|
||
|
||
foreach (var kvp in configuration.AsEnumerable()) | ||
{ | ||
TestContext.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}"); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
InfisicalConfiguration.Tests/InfisicalConfiguration.Tests.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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="3.7.1" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="3.7.1" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\InfisicalConfiguration\InfisicalConfiguration.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,45 @@ | ||
using System.Text.Json; | ||
|
||
namespace InfisicalConfiguration; | ||
|
||
public class MachineIdentityLogin | ||
{ | ||
|
||
public string AccessToken { get; set; } | ||
|
||
public static MachineIdentityLogin Deserialize(string content) | ||
{ | ||
var result = JsonSerializer.Deserialize<MachineIdentityLogin>(content, new JsonSerializerOptions() | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
}); | ||
|
||
if (result == null) | ||
{ | ||
throw new InvalidOperationException("Failed to deserialize MachineIdentityLogin"); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
public class SecretsList | ||
{ | ||
public List<Secret> Secrets { get; set; } | ||
|
||
public static SecretsList Deserialize(string content) | ||
{ | ||
var result = JsonSerializer.Deserialize<SecretsList>(content, new JsonSerializerOptions() | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
}); | ||
return result; | ||
} | ||
} | ||
|
||
public class Secret | ||
{ | ||
public string SecretKey { get; set; } | ||
public string SecretValue { get; set; } | ||
|
||
} |
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,87 @@ | ||
namespace InfisicalConfiguration; | ||
|
||
public class UniversalAuthCredentials | ||
{ | ||
public string ClientId { get; set; } | ||
public string ClientSecret { get; set; } | ||
|
||
public UniversalAuthCredentials(string clientId, string clientSecret) | ||
{ | ||
ClientId = clientId; | ||
ClientSecret = clientSecret; | ||
} | ||
} | ||
|
||
public enum InfisicalAuthType | ||
{ | ||
Universal, | ||
} | ||
|
||
public class InfisicalAuth | ||
{ | ||
private InfisicalAuthType AuthType { get; set; } | ||
private UniversalAuthCredentials? universalAuthCredentials; | ||
|
||
internal InfisicalAuth() { } | ||
|
||
public InfisicalAuthType GetAuthMethod() | ||
{ | ||
return AuthType; | ||
} | ||
|
||
public UniversalAuthCredentials GetUniversalAuth() | ||
{ | ||
if (universalAuthCredentials == null) | ||
{ | ||
throw new InvalidOperationException("UniversalAuth must be set"); | ||
} | ||
|
||
if (AuthType == InfisicalAuthType.Universal) | ||
{ | ||
return universalAuthCredentials; | ||
} | ||
|
||
throw new InvalidOperationException("AuthType must be set. Are you missing a call to SetUniversalAuth?"); | ||
} | ||
|
||
internal void SetUniversalAuthCredentials(UniversalAuthCredentials credentials) | ||
{ | ||
universalAuthCredentials = credentials; | ||
AuthType = InfisicalAuthType.Universal; | ||
} | ||
} | ||
|
||
public class InfisicalAuthBuilder | ||
{ | ||
private readonly InfisicalAuth _auth; | ||
|
||
public InfisicalAuthBuilder() | ||
{ | ||
_auth = new InfisicalAuth(); | ||
} | ||
|
||
public InfisicalAuthBuilder SetUniversalAuth(string clientId, string clientSecret) | ||
{ | ||
_auth.SetUniversalAuthCredentials(new UniversalAuthCredentials(clientId, clientSecret)); | ||
return this; | ||
} | ||
|
||
public InfisicalAuth Build() | ||
{ | ||
var auth = _auth; | ||
if (auth.GetAuthMethod() == InfisicalAuthType.Universal) | ||
{ | ||
var universalAuth = auth.GetUniversalAuth(); | ||
if (string.IsNullOrEmpty(universalAuth.ClientId) || string.IsNullOrEmpty(universalAuth.ClientSecret)) | ||
{ | ||
throw new InvalidOperationException("ClientId and ClientSecret must be set"); | ||
} | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException("AuthType must be set. Are you missing a call to SetUniversalAuth?"); | ||
} | ||
|
||
return auth; | ||
} | ||
} |
Oops, something went wrong.