Skip to content

Commit f9a5fa6

Browse files
authored
Add possibility to pass IAuthMethodInfo implementation to VaultOptions (#29)
1 parent 845e22a commit f9a5fa6

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,30 @@ config.AddVaultConfiguration(
104104
"secret");
105105
```
106106
new VaultOptions("http://localhost:8200", "root", null, null, false, 300, false, new []{'.'}),
107-
## Configuration using environmnt variables
107+
## Configuration using environment variables
108108

109-
Alternatively, you can configure Vault connection using next environmnt variables:
109+
Alternatively, you can configure Vault connection using next environment variables:
110110

111111
- `VAULT_ADDR` : Address of the Vault instance. Default value is `"http://locahost:8200`.
112112
- `VAULT_TOKEN` : Vault token. Used for token-based authentication. Default value is `root`.
113113
- `VAULT_ROLEID` : Vault AppRole ID. Used for AppRole-based authentication.
114114
- `VAULT_SECRET` : Vault AppRole secret. Used for AppRole-based authentication.
115115

116+
## Configuration using IAuthMethodInfo
117+
118+
You can configure Vault connection using any supported auth method (look at https://github.com/rajanadar/VaultSharp#auth-methods):
119+
120+
```csharp
121+
config.AddVaultConfiguration(
122+
() => new VaultOptions(
123+
"htpp://localhost:8200",
124+
new KerberosAuthMethodInfo(),
125+
reloadOnChange: true,
126+
reloadCheckIntervalSeconds: 60),
127+
"sampleapp",
128+
"secret");
129+
```
130+
116131
## Preparing secrets in Vault
117132

118133
You need to store your secrets with special naming rules.

Source/VaultSharp.Extensions.Configuration/VaultConfigurationProvider.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public override void Load()
5353
if (this._vaultClient == null)
5454
{
5555
IAuthMethodInfo authMethod;
56-
if (!string.IsNullOrEmpty(this._source.Options.VaultRoleId) &&
56+
if (this._source.Options.AuthMethod != null)
57+
{
58+
authMethod = this._source.Options.AuthMethod;
59+
}
60+
else if (!string.IsNullOrEmpty(this._source.Options.VaultRoleId) &&
5761
!string.IsNullOrEmpty(this._source.Options.VaultSecret))
5862
{
5963
this._logger?.LogDebug("VaultConfigurationProvider: using AppRole authentication");

Source/VaultSharp.Extensions.Configuration/VaultOptions.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace VaultSharp.Extensions.Configuration
22
{
33
using System;
44
using System.Collections.Generic;
5+
using VaultSharp.V1.AuthMethods;
56

67
/// <summary>
78
/// Vault options class.
@@ -42,6 +43,39 @@ public VaultOptions(
4243
this.Namespace = @namespace;
4344
}
4445

46+
/// <summary>
47+
/// Initializes a new instance of the <see cref="VaultOptions"/> class.
48+
/// </summary>
49+
/// <param name="vaultAddress">Vault address.</param>
50+
/// <param name="authMethod">Vault auth method.</param>
51+
/// <param name="reloadOnChange">Reload secrets if changed in Vault.</param>
52+
/// <param name="reloadCheckIntervalSeconds">Interval in seconds to check Vault for any changes.</param>
53+
/// <param name="omitVaultKeyName">Omit Vault Key Name in Configuration Keys.</param>
54+
/// <param name="additionalCharactersForConfigurationPath">Additional characters for the Configuration path.</param>
55+
/// <param name="namespace">Vault namespace.</param>
56+
public VaultOptions(
57+
string vaultAddress,
58+
IAuthMethodInfo authMethod,
59+
bool reloadOnChange = false,
60+
int reloadCheckIntervalSeconds = 300,
61+
bool omitVaultKeyName = false,
62+
IEnumerable<char>? additionalCharactersForConfigurationPath = null,
63+
string? @namespace = null)
64+
{
65+
this.VaultAddress = vaultAddress;
66+
this.AuthMethod = authMethod;
67+
this.ReloadOnChange = reloadOnChange;
68+
this.ReloadCheckIntervalSeconds = reloadCheckIntervalSeconds;
69+
this.OmitVaultKeyName = omitVaultKeyName;
70+
this.AdditionalCharactersForConfigurationPath = additionalCharactersForConfigurationPath ?? Array.Empty<char>();
71+
this.Namespace = @namespace;
72+
}
73+
74+
/// <summary>
75+
/// Gets Vault Auth method
76+
/// </summary>
77+
public IAuthMethodInfo? AuthMethod { get; }
78+
4579
/// <summary>
4680
/// Gets Vault URL address.
4781
/// </summary>

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,39 @@ public async Task Success_WatcherTest_OmitVaultKey_TokenAuth()
358358
await container.DisposeAsync().ConfigureAwait(false);
359359
}
360360
}
361+
362+
[Fact]
363+
public async Task Success_AuthMethod()
364+
{
365+
// arrange
366+
using CancellationTokenSource cts = new CancellationTokenSource();
367+
string jsonData = @"{""option1"": ""value1"",""subsection"":{""option2"": ""value2""}}";
368+
369+
var container = this.PrepareVaultContainer();
370+
try
371+
{
372+
await container.StartAsync(cts.Token).ConfigureAwait(false);
373+
await this.LoadDataAsync("myservice-config", jsonData).ConfigureAwait(false);
374+
375+
// act
376+
ConfigurationBuilder builder = new ConfigurationBuilder();
377+
builder.AddVaultConfiguration(
378+
() => new VaultOptions("http://localhost:8200", new TokenAuthMethodInfo("root"), reloadOnChange: true, reloadCheckIntervalSeconds: 10, omitVaultKeyName: true),
379+
"myservice-config",
380+
"secret",
381+
this._logger);
382+
var configurationRoot = builder.Build();
383+
384+
// assert
385+
configurationRoot.GetValue<string>("option1").Should().Be("value1");
386+
configurationRoot.GetSection("subsection").GetValue<string>("option2").Should().Be("value2");
387+
}
388+
finally
389+
{
390+
cts.Cancel();
391+
await container.DisposeAsync().ConfigureAwait(false);
392+
}
393+
}
361394
}
362395

363396
public class TestConfigObject

0 commit comments

Comments
 (0)