Skip to content

Commit a83a8eb

Browse files
authored
Revert "Use protected configuration provider instead of reflection (#8098)" (#8124)
This reverts commit e6d6dc8.
1 parent 964b2ac commit a83a8eb

4 files changed

Lines changed: 24 additions & 31 deletions

File tree

src/NuGetGallery/App_Start/OwinStartup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ 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);
6769
// Refresh the content for the ContentObjectService to guarantee it has loaded the latest configuration on startup.
6870
var contentObjectService = dependencyResolver.GetService<IContentObjectService>();
6971
HostingEnvironment.QueueBackgroundWorkItem(async token =>

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
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;
54
using System.Configuration;
6-
using System.Web.Mvc;
7-
using System.Xml;
5+
using System.Reflection;
6+
using System.Web.Configuration;
87
using NuGetGallery.Configuration;
98

109
namespace NuGetGallery
1110
{
12-
public class GalleryMachineKeyConfigurationProvider : ProtectedConfigurationProvider
11+
public static class SessionPersistence
1312
{
14-
public override XmlNode Decrypt(XmlNode encryptedNode)
13+
public static void Setup(IGalleryConfigurationService config)
1514
{
16-
var xmlDoc = new XmlDocument();
17-
xmlDoc.XmlResolver = null;
18-
xmlDoc.AppendChild(xmlDoc.CreateElement(string.Empty, "machineKey", string.Empty));
19-
2015
// The machine keys are used for encrypting/decrypting cookies used by ASP.NET, these are usually set by IIS in 'Auto' mode.
2116
// During a deployment to Azure cloud service the same machine key values are set on all the instances of a given cloud service,
2217
// thereby providing session persistence across different instances in the same deployment slot. However, across different slots(staging vs production)
2318
// these session keys are different. Thereby causing the loss of session upon a slot swap. Manually setting these values on role start ensures same
2419
// 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>();
2620
if (config.Current.EnableMachineKeyConfiguration
2721
&& !string.IsNullOrWhiteSpace(config.Current.MachineKeyDecryption)
2822
&& !string.IsNullOrWhiteSpace(config.Current.MachineKeyDecryptionKey)
2923
&& !string.IsNullOrWhiteSpace(config.Current.MachineKeyValidationAlgorithm)
3024
&& !string.IsNullOrWhiteSpace(config.Current.MachineKeyValidationKey))
3125
{
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-
}
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);
3729

38-
return xmlDoc.DocumentElement;
39-
}
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;
4035

41-
public override XmlNode Encrypt(XmlNode node)
42-
{
43-
throw new NotImplementedException();
36+
machineKeyConfig.DecryptionKey = config.Current.MachineKeyDecryptionKey;
37+
machineKeyConfig.Decryption = config.Current.MachineKeyDecryption;
38+
machineKeyConfig.ValidationKey = config.Current.MachineKeyValidationKey;
39+
machineKeyConfig.ValidationAlgorithm = config.Current.MachineKeyValidationAlgorithm;
40+
41+
resetMethod.Invoke(mksSection, new object[] { machineKeyConfig });
42+
}
4443
}
4544
}
46-
}
45+
}

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" />
129128
<Compile Include="App_Start\LatestVersionRouteConstraint.cs" />
130129
<Compile Include="App_Start\NuGetODataV2FeedConfig.cs" />
131130
<Compile Include="App_Start\NuGetODataV1FeedConfig.cs" />
132131
<Compile Include="App_Start\NuGetODataConfig.cs" />
133132
<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: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
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>
2621
<appSettings>
2722
<!-- If you're running in Azure, we suggest you set these in your .cscfg file. -->
2823
<!-- ******************* -->
@@ -359,9 +354,6 @@
359354
<error statusCode="500" redirect="~/App_500.aspx"/>
360355
</customErrors>
361356
<sessionState mode="Off"/>
362-
<machineKey configProtectionProvider="GalleryMachineKeyConfigurationProvider">
363-
<EncryptedData/>
364-
</machineKey>
365357
</system.web>
366358
<system.webServer>
367359
<tracing>

0 commit comments

Comments
 (0)