Skip to content

Commit e6d6dc8

Browse files
committed
Use protected configuration provider instead of reflection (#8098)
Address https://github.com/NuGet/Engineering/issues/3206
1 parent f8c3780 commit e6d6dc8

4 files changed

Lines changed: 31 additions & 24 deletions

File tree

src/NuGetGallery/App_Start/SessionPersistence.cs renamed to src/NuGetGallery/App_Start/GalleryMachineKeyConfigurationProvider.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Configuration;
5-
using System.Reflection;
6-
using System.Web.Configuration;
6+
using System.Web.Mvc;
7+
using System.Xml;
78
using NuGetGallery.Configuration;
89

910
namespace NuGetGallery
1011
{
11-
public static class SessionPersistence
12+
public class GalleryMachineKeyConfigurationProvider : ProtectedConfigurationProvider
1213
{
13-
public static void Setup(IGalleryConfigurationService config)
14+
public override XmlNode Decrypt(XmlNode encryptedNode)
1415
{
16+
var xmlDoc = new XmlDocument();
17+
xmlDoc.XmlResolver = null;
18+
xmlDoc.AppendChild(xmlDoc.CreateElement(string.Empty, "machineKey", string.Empty));
19+
1520
// The machine keys are used for encrypting/decrypting cookies used by ASP.NET, these are usually set by IIS in 'Auto' mode.
1621
// During a deployment to Azure cloud service the same machine key values are set on all the instances of a given cloud service,
1722
// thereby providing session persistence across different instances in the same deployment slot. However, across different slots(staging vs production)
1823
// these session keys are different. Thereby causing the loss of session upon a slot swap. Manually setting these values on role start ensures same
1924
// keys are used by all the instances across all the slots of a Azure cloud service. See more analysis here: https://github.com/NuGet/Engineering/issues/1329
25+
var config = DependencyResolver.Current.GetService<IGalleryConfigurationService>();
2026
if (config.Current.EnableMachineKeyConfiguration
2127
&& !string.IsNullOrWhiteSpace(config.Current.MachineKeyDecryption)
2228
&& !string.IsNullOrWhiteSpace(config.Current.MachineKeyDecryptionKey)
2329
&& !string.IsNullOrWhiteSpace(config.Current.MachineKeyValidationAlgorithm)
2430
&& !string.IsNullOrWhiteSpace(config.Current.MachineKeyValidationKey))
2531
{
26-
var mksType = typeof(MachineKeySection);
27-
var mksSection = ConfigurationManager.GetSection("system.web/machineKey") as MachineKeySection;
28-
var resetMethod = mksType.GetMethod("Reset", BindingFlags.NonPublic | BindingFlags.Instance);
29-
30-
var machineKeyConfig = new MachineKeySection();
31-
machineKeyConfig.ApplicationName = mksSection.ApplicationName;
32-
machineKeyConfig.CompatibilityMode = mksSection.CompatibilityMode;
33-
machineKeyConfig.DataProtectorType = mksSection.DataProtectorType;
34-
machineKeyConfig.Validation = mksSection.Validation;
32+
xmlDoc.DocumentElement.SetAttribute("decryptionKey", config.Current.MachineKeyDecryptionKey);
33+
xmlDoc.DocumentElement.SetAttribute("decryption", config.Current.MachineKeyDecryption);
34+
xmlDoc.DocumentElement.SetAttribute("validationKey", config.Current.MachineKeyValidationKey);
35+
xmlDoc.DocumentElement.SetAttribute("validation", config.Current.MachineKeyValidationAlgorithm);
36+
}
3537

36-
machineKeyConfig.DecryptionKey = config.Current.MachineKeyDecryptionKey;
37-
machineKeyConfig.Decryption = config.Current.MachineKeyDecryption;
38-
machineKeyConfig.ValidationKey = config.Current.MachineKeyValidationKey;
39-
machineKeyConfig.ValidationAlgorithm = config.Current.MachineKeyValidationAlgorithm;
38+
return xmlDoc.DocumentElement;
39+
}
4040

41-
resetMethod.Invoke(mksSection, new object[] { machineKeyConfig });
42-
}
41+
public override XmlNode Encrypt(XmlNode node)
42+
{
43+
throw new NotImplementedException();
4344
}
4445
}
45-
}
46+
}

src/NuGetGallery/App_Start/OwinStartup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public static void Configuration(IAppBuilder app)
6464
var config = dependencyResolver.GetService<IGalleryConfigurationService>();
6565
var auth = dependencyResolver.GetService<AuthenticationService>();
6666

67-
// Configure machine key for session persistence across slots
68-
SessionPersistence.Setup(config);
6967
// Refresh the content for the ContentObjectService to guarantee it has loaded the latest configuration on startup.
7068
var contentObjectService = dependencyResolver.GetService<IContentObjectService>();
7169
HostingEnvironment.QueueBackgroundWorkItem(async token =>

src/NuGetGallery/NuGetGallery.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@
125125
</ItemGroup>
126126
<ItemGroup>
127127
<Compile Include="ActionName.cs" />
128+
<Compile Include="App_Start\GalleryMachineKeyConfigurationProvider.cs" />
128129
<Compile Include="App_Start\LatestVersionRouteConstraint.cs" />
129130
<Compile Include="App_Start\NuGetODataV2FeedConfig.cs" />
130131
<Compile Include="App_Start\NuGetODataV1FeedConfig.cs" />
131132
<Compile Include="App_Start\NuGetODataConfig.cs" />
132133
<Compile Include="App_Start\StorageDependent.cs" />
133-
<Compile Include="App_Start\SessionPersistence.cs" />
134134
<Compile Include="App_Start\WebApiConfig.cs" />
135135
<Compile Include="App_Start\AutofacConfig.cs" />
136136
<Compile Include="Areas\Admin\Controllers\ApiKeysController.cs" />

src/NuGetGallery/Web.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere"/>
1919
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
2020
</configSections>
21+
<configProtectedData>
22+
<providers>
23+
<add name="GalleryMachineKeyConfigurationProvider" type="NuGetGallery.GalleryMachineKeyConfigurationProvider, NuGetGallery"/>
24+
</providers>
25+
</configProtectedData>
2126
<appSettings>
2227
<!-- If you're running in Azure, we suggest you set these in your .cscfg file. -->
2328
<!-- ******************* -->
@@ -354,6 +359,9 @@
354359
<error statusCode="500" redirect="~/App_500.aspx"/>
355360
</customErrors>
356361
<sessionState mode="Off"/>
362+
<machineKey configProtectionProvider="GalleryMachineKeyConfigurationProvider">
363+
<EncryptedData />
364+
</machineKey>
357365
</system.web>
358366
<system.webServer>
359367
<tracing>

0 commit comments

Comments
 (0)