This repository has been archived by the owner on Oct 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a startup filter which initializes the key ring before the server…
… starts
- Loading branch information
Nate McMaster
committed
Jun 2, 2017
1 parent
988fb80
commit b17acab
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Microsoft.AspNetCore.DataProtection/Internal/DataProtectionStartupFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNetCore.DataProtection.Internal | ||
{ | ||
internal class DataProtectionStartupFilter : IStartupFilter | ||
{ | ||
private readonly IKeyRingProvider _keyRingProvider; | ||
private readonly ILogger<DataProtectionStartupFilter> _logger; | ||
|
||
public DataProtectionStartupFilter(IKeyRingProvider keyRingProvider, ILoggerFactory loggerFactory) | ||
{ | ||
_keyRingProvider = keyRingProvider; | ||
_logger = loggerFactory.CreateLogger<DataProtectionStartupFilter>(); | ||
} | ||
|
||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||
{ | ||
try | ||
{ | ||
// It doesn't look like much, but this preloads the key ring, | ||
// which in turn may load data from remote stores like Redis or Azure. | ||
var keyRing = _keyRingProvider.GetCurrentKeyRing(); | ||
|
||
_logger.KeyRingWasLoadedOnStartup(keyRing.DefaultKeyId); | ||
} | ||
catch (Exception ex) | ||
{ | ||
// This should be non-fatal, so swallow, log, and allow server startup to continue. | ||
// The KeyRingProvider may be able to try again on the first request. | ||
_logger.KeyRingFailedToLoadOnStartup(ex); | ||
} | ||
|
||
return next; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
test/Microsoft.AspNetCore.DataProtection.Test/HostingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.Testing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.DataProtection.Test | ||
{ | ||
public class HostingTests | ||
{ | ||
[Fact] | ||
public async Task LoadsKeyRingBeforeServerStarts() | ||
{ | ||
var tcs = new TaskCompletionSource<object>(); | ||
var mockKeyRing = new Mock<IKeyRingProvider>(); | ||
mockKeyRing.Setup(m => m.GetCurrentKeyRing()) | ||
.Returns(Mock.Of<IKeyRing>()) | ||
.Callback(() => tcs.TrySetResult(null)); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseStartup<TestStartup>() | ||
.ConfigureServices(s => | ||
s.AddDataProtection() | ||
.Services | ||
.Replace(ServiceDescriptor.Singleton(mockKeyRing.Object)) | ||
.AddSingleton<IServer>( | ||
new FakeServer(onStart: () => tcs.TrySetException(new InvalidOperationException("Server was started before key ring was initialized"))))); | ||
|
||
using (var host = builder.Build()) | ||
{ | ||
await host.StartAsync(); | ||
} | ||
|
||
await tcs.Task.TimeoutAfter(TimeSpan.FromSeconds(10)); | ||
mockKeyRing.VerifyAll(); | ||
} | ||
|
||
[Fact] | ||
public async Task StartupContinuesOnFailureToLoadKey() | ||
{ | ||
var mockKeyRing = new Mock<IKeyRingProvider>(); | ||
mockKeyRing.Setup(m => m.GetCurrentKeyRing()) | ||
.Throws(new NotSupportedException("This mock doesn't actually work, but shouldn't kill the server")) | ||
.Verifiable(); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseStartup<TestStartup>() | ||
.ConfigureServices(s => | ||
s.AddDataProtection() | ||
.Services | ||
.Replace(ServiceDescriptor.Singleton(mockKeyRing.Object)) | ||
.AddSingleton(Mock.Of<IServer>())); | ||
|
||
using (var host = builder.Build()) | ||
{ | ||
await host.StartAsync(); | ||
} | ||
|
||
mockKeyRing.VerifyAll(); | ||
} | ||
|
||
private class TestStartup | ||
{ | ||
public void Configure(IApplicationBuilder app) | ||
{ | ||
} | ||
} | ||
|
||
public class FakeServer : IServer | ||
{ | ||
private readonly Action _onStart; | ||
|
||
public FakeServer(Action onStart) | ||
{ | ||
_onStart = onStart; | ||
} | ||
|
||
public IFeatureCollection Features => new FeatureCollection(); | ||
|
||
public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken) | ||
{ | ||
_onStart(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters