Skip to content

Commit 5f1f347

Browse files
authored
Change the behavior to throw exception if credentials/token are invalid or there are some issues connecting to vault. (#48)
1 parent 1e67d16 commit 5f1f347

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

Source/VaultSharp.Extensions.Configuration/VaultConfigurationProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public override void Load()
112112
catch (Exception e) when (e is VaultApiException || e is System.Net.Http.HttpRequestException)
113113
{
114114
this.logger?.Log(LogLevel.Error, e, "Cannot load configuration from Vault");
115+
throw;
115116
}
116117
}
117118

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace VaultSharp.Extensions.Configuration.Test
55
using System.IO;
66
using System.Net;
77
using System.Net.Http;
8-
using System.Text.Json;
98
using System.Threading;
109
using System.Threading.Tasks;
1110
using DotNet.Testcontainers.Builders;
@@ -14,7 +13,6 @@ namespace VaultSharp.Extensions.Configuration.Test
1413
using Microsoft.Extensions.Configuration;
1514
using Microsoft.Extensions.Logging;
1615
using Moq;
17-
using Newtonsoft.Json.Linq;
1816
using Serilog;
1917
using Serilog.Extensions.Logging;
2018
using VaultSharp.Core;
@@ -598,22 +596,25 @@ public async Task Failure_PermissionDenied()
598596
"myservice-config",
599597
"secret",
600598
loggerMock.Object);
601-
var configurationRoot = builder.Build();
599+
Action act = () => builder.Build();
600+
act.Should().Throw<VaultApiException>().And.HttpStatusCode.Should().Be(HttpStatusCode.Forbidden);
602601

603-
// assert
604-
loggerMock.Verify(
605-
x => x.Log(
606-
It.Is<LogLevel>(l => l == LogLevel.Error),
607-
It.IsAny<EventId>(),
608-
It.Is<It.IsAnyType>((v, t) => v.ToString() == "Cannot load configuration from Vault"),
609-
It.Is<VaultApiException>(exception => exception.HttpStatusCode == HttpStatusCode.Forbidden),
610-
It.Is<Func<It.IsAnyType, Exception?, string>>((v, t) => true)), Times.Once);
611-
}
602+
}
612603
finally
613604
{
614605
cts.Cancel();
615606
await container.DisposeAsync().ConfigureAwait(false);
616607
}
608+
609+
// assert
610+
loggerMock.Verify(
611+
x => x.Log(
612+
It.Is<LogLevel>(l => l == LogLevel.Error),
613+
It.IsAny<EventId>(),
614+
It.Is<It.IsAnyType>((v, t) => v.ToString() == "Cannot load configuration from Vault"),
615+
It.Is<VaultApiException>(exception => exception.HttpStatusCode == HttpStatusCode.Forbidden),
616+
It.Is<Func<It.IsAnyType, Exception?, string>>((v, t) => true)), Times.Once);
617+
617618
}
618619
}
619620

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ public async Task SSL_Enabled_CannotVerifyCert()
8282
"test",
8383
"secret",
8484
this._logger);
85-
var configurationRoot = builder.Build();
85+
Action act = () => builder.Build();
86+
8687

8788
// assert
88-
configurationRoot.GetValue<string>("option1").Should().BeNull();
89+
act.Should().Throw<System.Net.Http.HttpRequestException>("The SSL connection could not be established, see inner exception.");
8990
}
9091
finally
9192
{

0 commit comments

Comments
 (0)