internal void SetVaultTokenDelegate()
{
if (_authMethodLoginProvider != null)
{
_lazyVaultToken = new Lazy<Task<string>>(_authMethodLoginProvider.GetVaultTokenAsync, LazyThreadSafetyMode.PublicationOnly);
}
}
The problem here is using Lazy with Task object. In that case if any errors occur inside GetVaultTokenAsync (e.g network exceptions, VaultApiException) then Task will be created anyway, but with Exception as result.
Why it's bad?
It's bad because we can't just retry request (Task result with exception is cached), so the only way is to recreate whole client.
Sample Code Snippet
It's easy to reproduce with custom HttpMessageHandler (we checked with Mock)
public interface IHttpMessageHandler
{
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken);
}
var mockMessageHandler = new Mock<HttpMessageHandler>();
mockMessageHandler.Protected().As<IHttpMessageHandler>()
.SetupSequence(p => p.SendAsync(It.IsAny<HttpRequestMessage>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(new HttpResponseMessage((HttpStatusCode)500))
.ReturnsAsync(new HttpResponseMessage((HttpStatusCode)501))
.ReturnsAsync(new HttpResponseMessage((HttpStatusCode)503));
var settings = new VaultClientSettings(connectionOptions.Url, connectionOptions.Authentication.GetMethod())
{
MyHttpClientProviderFunc = handler => new HttpClient(customHandler)
};
var client = new VaultSharp.VaultClient(settings);
client.V1.Secrets.KeyValue.V2.ReadSecretAsync<TSecret>(...)
client.V1.Secrets.KeyValue.V2.ReadSecretAsync<TSecret>(...)
client.V1.Secrets.KeyValue.V2.ReadSecretAsync<TSecret>(...)
Code block from Polymath
The problem here is using Lazy with Task object. In that case if any errors occur inside GetVaultTokenAsync (e.g network exceptions, VaultApiException) then Task will be created anyway, but with Exception as result. Why it's bad? It's bad because we can't just retry request (Task result with exception is cached), so the only way is to recreate whole client.
Sample Code Snippet It's easy to reproduce with custom HttpMessageHandler (we checked with Mock)
The first exception (500) is cached.
VaultSharp Version 1.13.0.1