Skip to content

Commit b786db7

Browse files
Not catching all exceptions during reading. Now "permission denied" is logged. (#30)
Co-authored-by: Dariusz Dobosz <[email protected]>
1 parent f9a5fa6 commit b786db7

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

Source/VaultSharp.Extensions.Configuration/VaultConfigurationProvider.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace VaultSharp.Extensions.Configuration
44
using System.Collections.Generic;
55
using System.Globalization;
66
using System.Linq;
7+
using System.Net;
78
using System.Text;
89
using System.Threading.Tasks;
910
using Microsoft.Extensions.Configuration;
@@ -112,7 +113,7 @@ private async Task<bool> LoadVaultDataAsync(IVaultClient vaultClient)
112113
if (this._versionsCache.TryGetValue(key, out var currentVersion))
113114
{
114115
shouldSetValue = secretData.SecretData.Metadata.Version > currentVersion;
115-
string keyMsg = shouldSetValue ? "has new version" : "is outdated";
116+
var keyMsg = shouldSetValue ? "has new version" : "is outdated";
116117
this._logger?.LogDebug($"VaultConfigurationProvider: Data for key `{secretData.Key}` {keyMsg}");
117118
}
118119

@@ -224,7 +225,7 @@ private async IAsyncEnumerable<KeyedSecretData> ReadKeysAsync(IVaultClient vault
224225
{
225226
keys = await vaultClient.V1.Secrets.KeyValue.V2.ReadSecretPathsAsync(folderPath, this._source.MountPoint).ConfigureAwait(false);
226227
}
227-
catch (VaultApiException)
228+
catch (VaultApiException ex) when (ex.HttpStatusCode == HttpStatusCode.NotFound)
228229
{
229230
// this is key, not a folder
230231
}
@@ -254,7 +255,7 @@ private async IAsyncEnumerable<KeyedSecretData> ReadKeysAsync(IVaultClient vault
254255
.ConfigureAwait(false);
255256
keyedSecretData = new KeyedSecretData(valuePath, secretData.Data);
256257
}
257-
catch (VaultApiException)
258+
catch (VaultApiException ex) when (ex.HttpStatusCode == HttpStatusCode.NotFound)
258259
{
259260
// this is folder, not a key
260261
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ namespace VaultSharp.Extensions.Configuration.Test
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Net;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using DotNet.Testcontainers.Builders;
89
using DotNet.Testcontainers.Containers;
910
using FluentAssertions;
1011
using Microsoft.Extensions.Configuration;
12+
using Microsoft.Extensions.Logging;
13+
using Moq;
1114
using Newtonsoft.Json;
1215
using Serilog;
1316
using Serilog.Extensions.Logging;
17+
using VaultSharp.Core;
1418
using VaultSharp.V1.AuthMethods.Token;
1519
using Xunit;
1620
using ILogger = Microsoft.Extensions.Logging.ILogger;
@@ -391,6 +395,45 @@ public async Task Success_AuthMethod()
391395
await container.DisposeAsync().ConfigureAwait(false);
392396
}
393397
}
398+
399+
400+
[Fact]
401+
public async Task Failure_PermissionDenied()
402+
{
403+
// arrange
404+
using var cts = new CancellationTokenSource();
405+
var jsonData = @"{""option1"": ""value1"",""subsection"":{""option2"": ""value2""}}";
406+
var loggerMock = new Mock<ILogger<IntegrationTests>>();
407+
var container = this.PrepareVaultContainer();
408+
try
409+
{
410+
await container.StartAsync(cts.Token).ConfigureAwait(false);
411+
await this.LoadDataAsync("myservice-config", jsonData).ConfigureAwait(false);
412+
413+
// act
414+
var builder = new ConfigurationBuilder();
415+
builder.AddVaultConfiguration(
416+
() => new VaultOptions("http://localhost:8200", new TokenAuthMethodInfo("NON VALID TOKEN"), reloadOnChange: true, reloadCheckIntervalSeconds: 10, omitVaultKeyName: true),
417+
"myservice-config",
418+
"secret",
419+
loggerMock.Object);
420+
var configurationRoot = builder.Build();
421+
422+
// assert
423+
loggerMock.Verify(
424+
x => x.Log(
425+
It.Is<LogLevel>(l => l == LogLevel.Error),
426+
It.IsAny<EventId>(),
427+
It.Is<It.IsAnyType>((v, t) => v.ToString() == "Cannot load configuration from Vault"),
428+
It.Is<VaultApiException>(exception => exception.HttpStatusCode == HttpStatusCode.Forbidden),
429+
It.Is<Func<It.IsAnyType, Exception?, string>>((v, t) => true)), Times.Once);
430+
}
431+
finally
432+
{
433+
cts.Cancel();
434+
await container.DisposeAsync().ConfigureAwait(false);
435+
}
436+
}
394437
}
395438

396439
public class TestConfigObject

0 commit comments

Comments
 (0)