Remove keyvault hosting startup (#159)

This commit is contained in:
Pavel Krymets 2018-03-02 14:59:28 -08:00 committed by GitHub
parent dfcb0bf3e0
commit 850ce33edf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 5 additions and 298 deletions

View File

@ -54,8 +54,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Applic
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Runtime.SiteExtension", "extensions\Microsoft.AspNetCore.Runtime.SiteExtension\Microsoft.AspNetCore.Runtime.SiteExtension.csproj", "{E1E9BC7A-6951-4B60-8DFB-DBB9AC3CDEB0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.AzureKeyVault.HostingStartup", "src\Microsoft.AspNetCore.AzureKeyVault.HostingStartup\Microsoft.AspNetCore.AzureKeyVault.HostingStartup.csproj", "{4B471CD7-4A15-4C42-B7B7-0A1C8BA2FDBA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -126,10 +124,6 @@ Global
{E1E9BC7A-6951-4B60-8DFB-DBB9AC3CDEB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1E9BC7A-6951-4B60-8DFB-DBB9AC3CDEB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E1E9BC7A-6951-4B60-8DFB-DBB9AC3CDEB0}.Release|Any CPU.Build.0 = Release|Any CPU
{4B471CD7-4A15-4C42-B7B7-0A1C8BA2FDBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B471CD7-4A15-4C42-B7B7-0A1C8BA2FDBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B471CD7-4A15-4C42-B7B7-0A1C8BA2FDBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B471CD7-4A15-4C42-B7B7-0A1C8BA2FDBA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -151,7 +145,6 @@ Global
{F0CABFE8-A5B1-487B-A451-A486D26742D3} = {FF9B744E-6C59-40CC-9E41-9D2EBD292435}
{15664836-2B94-4D2D-AC18-6DED01FCCCBD} = {CD650B4B-81C2-4A44-AEF2-A251A877C1F0}
{E1E9BC7A-6951-4B60-8DFB-DBB9AC3CDEB0} = {FF9B744E-6C59-40CC-9E41-9D2EBD292435}
{4B471CD7-4A15-4C42-B7B7-0A1C8BA2FDBA} = {FF9B744E-6C59-40CC-9E41-9D2EBD292435}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5743DFE7-1AA5-439D-84AE-A480EA389927}

View File

@ -22,7 +22,6 @@
<ItemGroup>
<HostingStartupPackageReference Include="Microsoft.AspNetCore.AzureAppServices.HostingStartup" Version="$(MicrosoftAspNetCoreAzureAppServicesHostingStartupPackageVersion)" />
<HostingStartupPackageReference Include="Microsoft.AspNetCore.AzureKeyVault.HostingStartup" Version="$(MicrosoftAspNetCoreAzureKeyVaultHostingStartupPackageVersion)" />
<PackageReference Include="Internal.AspNetCore.SiteExtension.Sdk" Version="$(InternalAspNetCoreSiteExtensionSdkPackageVersion)" PrivateAssets="All" />
</ItemGroup>

View File

@ -7,9 +7,9 @@
<system.webServer xdt:Transform="InsertIfMissing">
<runtime xdt:Transform="InsertIfMissing" >
<environmentVariables xdt:Transform="InsertIfMissing">
<add name="DOTNET_ADDITIONAL_DEPS" value="%XDT_EXTENSIONPATH%\additionalDeps\Microsoft.AspNetCore.AzureAppServices.HostingStartup\;%XDT_EXTENSIONPATH%\additionalDeps\Microsoft.AspNetCore.AzureKeyVault.HostingStartup\;%ProgramFiles%\dotnet\additionalDeps\Microsoft.AspNetCore.AzureAppServices.HostingStartup\" xdt:Locator="Match(name)" xdt:Transform="InsertOrAppendAttribute(Attribute='value')" />
<add name="DOTNET_ADDITIONAL_DEPS" value="%XDT_EXTENSIONPATH%\additionalDeps\Microsoft.AspNetCore.AzureAppServices.HostingStartup\;%ProgramFiles%\dotnet\additionalDeps\Microsoft.AspNetCore.AzureAppServices.HostingStartup\" xdt:Locator="Match(name)" xdt:Transform="InsertOrAppendAttribute(Attribute='value')" />
<add name="DOTNET_SHARED_STORE" value="%XDT_EXTENSIONPATH%\store" xdt:Locator="Match(name)" xdt:Transform="InsertOrAppendAttribute(Attribute='value')" />
<add name="ASPNETCORE_HOSTINGSTARTUPASSEMBLIES" value="Microsoft.AspNetCore.AzureAppServices.HostingStartup;Microsoft.AspNetCore.AzureKeyVault.HostingStartup" xdt:Locator="Match(name)" xdt:Transform="InsertOrAppendAttribute(Attribute='value')" />
<add name="ASPNETCORE_HOSTINGSTARTUPASSEMBLIES" value="Microsoft.AspNetCore.AzureAppServices.HostingStartup" xdt:Locator="Match(name)" xdt:Transform="InsertOrAppendAttribute(Attribute='value')" />
</environmentVariables>
</runtime>
</system.webServer>

View File

@ -13,7 +13,6 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.AzureAppServices.HostingStartup\Microsoft.AspNetCore.AzureAppServices.HostingStartup.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.AzureKeyVault.HostingStartup\Microsoft.AspNetCore.AzureKeyVault.HostingStartup.csproj" />
</ItemGroup>
</Project>

View File

@ -1,21 +1,14 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace IISSample
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
{
var logger = loggerfactory.CreateLogger("Requests");
@ -62,13 +55,6 @@ namespace IISSample
await context.Response.WriteAsync(key + ": " + value + Environment.NewLine);
}
await context.Response.WriteAsync(Environment.NewLine);
var protectorProvider = context.RequestServices.GetService<IDataProtectionProvider>();
var protector = protectorProvider.CreateProtector("Purpose");
await context.Response.WriteAsync("Protected Query: " + protector.Protect(context.Request.QueryString.Value) + Environment.NewLine);
await context.Response.WriteAsync(Environment.NewLine);
});
}

View File

@ -3,7 +3,7 @@
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:22071/",
"applicationUrl": "http://localhost:64358/",
"sslPort": 0
}
},

View File

@ -1,6 +0,0 @@
// 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.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.AzureAppServicesIntegration.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]

View File

@ -1,93 +0,0 @@
// 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.IO;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;
using Microsoft.Extensions.DependencyInjection;
[assembly: HostingStartup(typeof(Microsoft.AspNetCore.AzureKeyVault.HostingStartup.AzureKeyVaultHostingStartup))]
namespace Microsoft.AspNetCore.AzureKeyVault.HostingStartup
{
/// <summary>
/// A dynamic KeyVault lightup experience
/// </summary>
public class AzureKeyVaultHostingStartup : IHostingStartup
{
private const string HostingStartupName = "KeyVault";
private const string ConfigurationFeatureName = "ConfigurationEnabled";
private const string ConfigurationVaultName = "ConfigurationVault";
private const string DataProtectionFeatureName = "DataProtectionEnabled";
private const string DataProtectionKeyName = "DataProtectionKey";
/// <inheritdoc />
public void Configure(IWebHostBuilder builder)
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var authenticationCallback = new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback);
var keyVaultClient = new KeyVaultClient(authenticationCallback);
var baseConfiguration = HostingStartupConfigurationExtensions.GetBaseConfiguration();
builder.ConfigureServices((context, collection) =>
{
var configuration = new ConfigurationBuilder()
.AddConfiguration(baseConfiguration)
.AddConfiguration(context.Configuration)
.Build();
if (configuration.IsEnabled(HostingStartupName, DataProtectionFeatureName) &&
configuration.TryGetOption(HostingStartupName, DataProtectionKeyName, out var protectionKey))
{
AddDataProtection(collection, keyVaultClient, protectionKey);
}
});
if (baseConfiguration.IsEnabled(HostingStartupName, ConfigurationFeatureName) &&
baseConfiguration.TryGetOption(HostingStartupName, ConfigurationVaultName, out var vault))
{
builder.ConfigureAppConfiguration((context, configurationBuilder) =>
{
AddConfiguration(configurationBuilder, keyVaultClient, vault);
});
}
}
internal virtual void AddDataProtection(IServiceCollection serviceCollection, KeyVaultClient client, string protectionKey)
{
// Duplicates functionality from GetKeyStorageDirectoryForAzureWebSites in DataProtection
// to detect key storage location when running on Azure
// because you are not alowed to set IXmlEncryptor without setting IXmlRepository
// Check that we are running in Azure AppServices
var siteId = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
if (string.IsNullOrWhiteSpace(siteId))
{
return;
}
var home = Environment.GetEnvironmentVariable("HOME");
if (string.IsNullOrWhiteSpace(home))
{
return;
}
var keyLocation = new DirectoryInfo(Path.Combine(home, "ASP.NET", "DataProtection-Keys"));
serviceCollection.AddDataProtection()
.ProtectKeysWithAzureKeyVault(client, protectionKey)
.PersistKeysToFileSystem(keyLocation);
}
internal virtual void AddConfiguration(IConfigurationBuilder configurationBuilder, KeyVaultClient client, string keyVault)
{
configurationBuilder.AddAzureKeyVault(keyVault, client, new DefaultKeyVaultSecretManager());
}
}
}

View File

@ -1,24 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\hostingstartup.targets" />
<PropertyGroup>
<Description>ASP.NET Core lightup integration with Azure KeyVault.</Description>
<TargetFrameworks>netcoreapp2.1;netcoreapp2.0;net461</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;azure;appservices</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(MicrosoftAspNetCoreHostingAbstractionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="$(MicrosoftExtensionsConfigurationAzureKeyVaultPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.AzureKeyVault" Version="$(MicrosoftAspNetCoreDataProtectionAzureKeyVaultPackageVersion)" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="$(MicrosoftAzureServicesAppAuthenticationPackageVersion)" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Microsoft.AspNetCore.AzureAppServices.HostingStartup\HostingStartupConfigurationExtensions.cs" />
</ItemGroup>
</Project>

View File

@ -32,7 +32,6 @@ namespace Microsoft.AspNetCore.AzureAppServices.SiteExtension
Assert.Equal("add", depsElement.Name);
Assert.Equal("DOTNET_ADDITIONAL_DEPS", depsElement.Attributes["name"].Value);
Assert.Equal($@"{XdtExtensionPath}\additionalDeps\Microsoft.AspNetCore.AzureAppServices.HostingStartup\;" +
$@"{XdtExtensionPath}\additionalDeps\Microsoft.AspNetCore.AzureKeyVault.HostingStartup\;" +
@"%ProgramFiles%\dotnet\additionalDeps\Microsoft.AspNetCore.AzureAppServices.HostingStartup\",
depsElement.Attributes["value"].Value);
@ -44,7 +43,7 @@ namespace Microsoft.AspNetCore.AzureAppServices.SiteExtension
var startupAssembliesElement = sharedStoreElement.NextSibling;
Assert.Equal("add", startupAssembliesElement.Name);
Assert.Equal("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES", startupAssembliesElement.Attributes["name"].Value);
Assert.Equal("Microsoft.AspNetCore.AzureAppServices.HostingStartup;Microsoft.AspNetCore.AzureKeyVault.HostingStartup", startupAssembliesElement.Attributes["value"].Value);
Assert.Equal("Microsoft.AspNetCore.AzureAppServices.HostingStartup", startupAssembliesElement.Attributes["value"].Value);
}
[Fact]
@ -64,7 +63,6 @@ namespace Microsoft.AspNetCore.AzureAppServices.SiteExtension
Assert.Equal("DOTNET_ADDITIONAL_DEPS", depsElement.Attributes["name"].Value);
Assert.Equal(@"ExistingValue1;"+
$@"{XdtExtensionPath}\additionalDeps\Microsoft.AspNetCore.AzureAppServices.HostingStartup\;" +
$@"{XdtExtensionPath}\additionalDeps\Microsoft.AspNetCore.AzureKeyVault.HostingStartup\;" +
@"%ProgramFiles%\dotnet\additionalDeps\Microsoft.AspNetCore.AzureAppServices.HostingStartup\",
depsElement.Attributes["value"].Value);
@ -76,7 +74,7 @@ namespace Microsoft.AspNetCore.AzureAppServices.SiteExtension
var startupAssembliesElement = sharedStoreElement.NextSibling;
Assert.Equal("add", startupAssembliesElement.Name);
Assert.Equal("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES", startupAssembliesElement.Attributes["name"].Value);
Assert.Equal("ExistingValue2;Microsoft.AspNetCore.AzureAppServices.HostingStartup;Microsoft.AspNetCore.AzureKeyVault.HostingStartup", startupAssembliesElement.Attributes["value"].Value);
Assert.Equal("ExistingValue2;Microsoft.AspNetCore.AzureAppServices.HostingStartup", startupAssembliesElement.Attributes["value"].Value);
}
private static XmlDocument LoadDocAndRunTransform(string docName)

View File

@ -1,144 +0,0 @@
// 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.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Azure.KeyVault;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Microsoft.AspNetCore.AzureKeyVault.HostingStartup.Tests
{
public class HostinStartupTests
{
[Fact]
public void Configure_AddsDataProtection()
{
Environment.SetEnvironmentVariable("ASPNETCORE_HostingStartup__KeyVault__DataProtectionEnabled", null);
Environment.SetEnvironmentVariable("ASPNETCORE_HostingStartup__KeyVault__DataProtectionKey", "http://vault");
var callbackCalled = false;
var builder = new WebHostBuilder().Configure(app => { });
var mockHostingStartup = new MockAzureKeyVaultHostingStartup(
(collection, client, key) =>
{
callbackCalled = true;
Assert.NotNull(collection);
Assert.NotNull(client);
Assert.Equal("http://vault", key);
},
(configurationBuilder, client, vault) => {}
);
mockHostingStartup.Configure(builder);
var _ = new TestServer(builder);
Assert.True(callbackCalled);
}
[Theory]
[InlineData("0")]
[InlineData("FALSE")]
[InlineData("false")]
public void Configure_SkipsAddsDataProtection_IfDisabled(string value)
{
Environment.SetEnvironmentVariable("ASPNETCORE_HostingStartup__KeyVault__DataProtectionEnabled", value);
Environment.SetEnvironmentVariable("ASPNETCORE_HostingStartup__KeyVault__DataProtectionKey", "http://vault");
var callbackCalled = false;
var builder = new WebHostBuilder().Configure(app => { });
var mockHostingStartup = new MockAzureKeyVaultHostingStartup(
(collection, client, key) =>
{
callbackCalled = true;
},
(configurationBuilder, client, vault) => {}
);
mockHostingStartup.Configure(builder);
var _ = new TestServer(builder);
Assert.False(callbackCalled);
}
[Fact]
public void Configure_AddsConfiguration()
{
Environment.SetEnvironmentVariable("ASPNETCORE_HostingStartup__KeyVault__ConfigurationEnabled", null);
Environment.SetEnvironmentVariable("ASPNETCORE_HostingStartup__KeyVault__ConfigurationVault", "http://vault");
var callbackCalled = false;
var builder = new WebHostBuilder().Configure(app => { });
var mockHostingStartup = new MockAzureKeyVaultHostingStartup(
(collection, client, key) => { },
(configurationBuilder, client, vault) =>
{
callbackCalled = true;
Assert.NotNull(configurationBuilder);
Assert.NotNull(client);
Assert.Equal("http://vault", vault);
}
);
mockHostingStartup.Configure(builder);
var _ = new TestServer(builder);
Assert.True(callbackCalled);
}
[Theory]
[InlineData("0")]
[InlineData("FALSE")]
[InlineData("false")]
public void Configure_SkipsConfiguration_IfDisabled(string value)
{
Environment.SetEnvironmentVariable("ASPNETCORE_HostingStartup__KeyVault__ConfigurationEnabled", value);
Environment.SetEnvironmentVariable("ASPNETCORE_HostingStartup__KeyVault__ConfigurationVault", "http://vault");
var callbackCalled = false;
var builder = new WebHostBuilder().Configure(app => { });
var mockHostingStartup = new MockAzureKeyVaultHostingStartup(
(collection, client, key) => { },
(configurationBuilder, client, vault) =>
{
callbackCalled = true;
}
);
mockHostingStartup.Configure(builder);
var _ = new TestServer(builder);
Assert.False(callbackCalled);
}
private class MockAzureKeyVaultHostingStartup : AzureKeyVaultHostingStartup
{
private readonly Action<IServiceCollection, KeyVaultClient, string> _dataProtectionCallback;
private readonly Action<IConfigurationBuilder, KeyVaultClient, string> _configurationCallback;
public MockAzureKeyVaultHostingStartup(
Action<IServiceCollection, KeyVaultClient, string> dataProtectionCallback,
Action<IConfigurationBuilder, KeyVaultClient, string> configurationCallback)
{
_dataProtectionCallback = dataProtectionCallback;
_configurationCallback = configurationCallback;
}
internal override void AddDataProtection(IServiceCollection serviceCollection, KeyVaultClient client, string protectionKey)
{
_dataProtectionCallback(serviceCollection, client, protectionKey);
}
internal override void AddConfiguration(IConfigurationBuilder configurationBuilder, KeyVaultClient client, string keyVault)
{
_configurationCallback(configurationBuilder, client, keyVault);
}
}
}
}

View File

@ -7,7 +7,6 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.AzureAppServicesIntegration\Microsoft.AspNetCore.AzureAppServicesIntegration.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.AzureAppServices.HostingStartup\Microsoft.AspNetCore.AzureAppServices.HostingStartup.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.AzureKeyVault.HostingStartup\Microsoft.AspNetCore.AzureKeyVault.HostingStartup.csproj" />
</ItemGroup>
<ItemGroup>