Skip to content

Commit 7b9032a

Browse files
k0st1xMrZoidberg
andauthored
Hotfix - vault-proxy support (#63)
* authMethod should be null for vault-agent * customization on application side * test * Success_TokenNoAuthMethod - skip --------- Co-authored-by: Mikhail Merkulov <[email protected]>
1 parent 3cd2f0d commit 7b9032a

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

Source/VaultSharp.Extensions.Configuration/VaultConfigurationProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override void Load()
5151
{
5252
if (this.vaultClient == null)
5353
{
54-
IAuthMethodInfo authMethod;
54+
IAuthMethodInfo? authMethod = null;
5555
if (this.ConfigurationSource.Options.AuthMethod != null)
5656
{
5757
authMethod = this.ConfigurationSource.Options.AuthMethod;
@@ -64,7 +64,7 @@ public override void Load()
6464
this.ConfigurationSource.Options.VaultRoleId,
6565
this.ConfigurationSource.Options.VaultSecret);
6666
}
67-
else
67+
else if (!string.IsNullOrEmpty(this.ConfigurationSource.Options.VaultToken))
6868
{
6969
this.logger?.LogDebug("VaultConfigurationProvider: using Token authentication");
7070
authMethod = new TokenAuthMethodInfo(this.ConfigurationSource.Options.VaultToken);

Source/VaultSharp.Extensions.Configuration/VaultConfigurationSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace VaultSharp.Extensions.Configuration
77
/// <summary>
88
/// Vault configuration source.
99
/// </summary>
10-
public sealed class VaultConfigurationSource : IConfigurationSource
10+
public class VaultConfigurationSource : IConfigurationSource
1111
{
1212
/// <summary>
1313
/// Default Vault URL.
@@ -56,6 +56,6 @@ public VaultConfigurationSource(VaultOptions options, string basePath, string? m
5656
/// </summary>
5757
/// <param name="builder">Configuration builder.</param>
5858
/// <returns>Instance of <see cref="IConfigurationProvider"/>.</returns>
59-
public IConfigurationProvider Build(IConfigurationBuilder builder) => new VaultConfigurationProvider(this, this.logger);
59+
public virtual IConfigurationProvider Build(IConfigurationBuilder builder) => new VaultConfigurationProvider(this, this.logger);
6060
}
6161
}

Tests/VaultSharp.Extensions.Configuration.Test/IntegrationTests.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace VaultSharp.Extensions.Configuration.Test
1616
using Serilog;
1717
using Serilog.Extensions.Logging;
1818
using VaultSharp.Core;
19+
using VaultSharp.V1.AuthMethods;
1920
using VaultSharp.V1.AuthMethods.AppRole;
2021
using VaultSharp.V1.AuthMethods.Token;
2122
using Xunit;
@@ -35,15 +36,15 @@ public IntegrationTests()
3536
this.logger = new SerilogLoggerProvider(Log.Logger).CreateLogger(nameof(IntegrationTests));
3637
}
3738

38-
private IContainer PrepareVaultContainer(bool enableSSL = false, string? script = null)
39+
private IContainer PrepareVaultContainer(bool enableSSL = false, string? script = null, string tokenId = "root")
3940
{
4041
var builder = new ContainerBuilder()
4142
.WithImage("vault:1.13.3")
4243
.WithName("vaultsharptest_"+Guid.NewGuid().ToString().Substring(0,8))
4344
.WithPortBinding(8200, 8200)
4445
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8200))
4546
.WithEnvironment("VAULT_UI", "true")
46-
.WithEnvironment("VAULT_DEV_ROOT_TOKEN_ID", "root")
47+
.WithEnvironment("VAULT_DEV_ROOT_TOKEN_ID", tokenId)
4748
.WithEnvironment("VAULT_DEV_LISTEN_ADDRESS", "0.0.0.0:8200");
4849

4950
if (enableSSL)
@@ -822,6 +823,56 @@ public async Task Success_Proxy_Verify_Custom_Hook_Invoked()
822823
await container.DisposeAsync();
823824
}
824825
}
826+
827+
[Fact(Skip = "vault-proxy required")]
828+
public async Task Success_TokenNoAuthMethod()
829+
{
830+
// arrange
831+
using CancellationTokenSource cts = new CancellationTokenSource();
832+
var values =
833+
new Dictionary<string, IEnumerable<KeyValuePair<string, object>>>
834+
{
835+
{
836+
"myservice-config", new[]
837+
{
838+
new KeyValuePair<string, object>("option1", "value1")
839+
}
840+
},
841+
};
842+
843+
var container = this.PrepareVaultContainer(tokenId: "");
844+
try
845+
{
846+
await container.StartAsync(cts.Token).ConfigureAwait(false);
847+
await this.LoadDataAsync("http://localhost:8200", values).ConfigureAwait(false);
848+
849+
// Moq mock of PostProcessHttpClientHandlerAction implementation:
850+
var mockConfigureProxyAction = new Mock<Action<HttpMessageHandler>>();
851+
852+
// act
853+
ConfigurationBuilder builder = new ConfigurationBuilder();
854+
builder.AddVaultConfiguration(
855+
() => new VaultOptions("http://localhost:8200", (IAuthMethodInfo)null!)
856+
{
857+
PostProcessHttpClientHandlerAction = mockConfigureProxyAction.Object
858+
},
859+
"myservice-config",
860+
"secret",
861+
this.logger);
862+
var configurationRoot = builder.Build();
863+
864+
// assert secrets were loaded successfully:
865+
configurationRoot.GetValue<string>("option1").Should().Be("value1");
866+
867+
// assert that PostProcessHttpClientHandlerAction was actually invoked, and a HttpMessageHandler was passed:
868+
mockConfigureProxyAction.Verify(x => x(It.IsAny<HttpMessageHandler>()), Times.Once);
869+
}
870+
finally
871+
{
872+
cts.Cancel();
873+
await container.DisposeAsync().ConfigureAwait(false);
874+
}
875+
}
825876
}
826877

827878
public class TestConfigObject

0 commit comments

Comments
 (0)