Skip to content

Commit ec483fa

Browse files
committed
Support KeyVault secrets in functional test configuration (#7865)
Move to PackageReference and .NET 4.7.2 Progress on NuGet/Engineering#3060
1 parent 99784ab commit ec483fa

13 files changed

Lines changed: 109 additions & 273 deletions

File tree

tests/NuGetGallery.FunctionalTests.Core/GalleryConfiguration.cs

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
6-
using System.IO;
7-
using Newtonsoft.Json;
5+
using System.Net;
6+
using Microsoft.Extensions.Configuration;
7+
using NuGet.Services.Configuration;
88

99
namespace NuGetGallery.FunctionalTests
1010
{
@@ -14,38 +14,43 @@ public class GalleryConfiguration
1414

1515
public string GalleryBaseUrl => "staging".Equals(Slot, StringComparison.OrdinalIgnoreCase) ? StagingBaseUrl : ProductionBaseUrl;
1616

17-
[JsonProperty]
18-
private string Slot { get; set; }
19-
[JsonProperty]
20-
private string ProductionBaseUrl { get; set; }
21-
[JsonProperty]
22-
private string StagingBaseUrl { get; set; }
23-
[JsonProperty]
24-
public string SearchServiceBaseUrl { get; private set; }
25-
[JsonProperty]
26-
public string EmailServerHost { get; private set; }
27-
[JsonProperty]
28-
public bool DefaultSecurityPoliciesEnforced { get; private set; }
29-
[JsonProperty]
30-
public bool TestPackageLock { get; private set; }
31-
[JsonProperty]
32-
public AccountConfiguration Account { get; private set; }
33-
[JsonProperty]
34-
public OrganizationConfiguration AdminOrganization { get; private set; }
35-
[JsonProperty]
36-
public OrganizationConfiguration CollaboratorOrganization { get; private set; }
37-
[JsonProperty]
38-
public BrandingConfiguration Branding { get; private set; }
39-
[JsonProperty]
40-
public bool TyposquattingCheckAndBlockUsers { get; private set; }
17+
public string Slot { get; set; }
18+
public string ProductionBaseUrl { get; set; }
19+
public string StagingBaseUrl { get; set; }
20+
public string SearchServiceBaseUrl { get; set; }
21+
public string EmailServerHost { get; set; }
22+
public bool DefaultSecurityPoliciesEnforced { get; set; }
23+
public bool TestPackageLock { get; set; }
24+
public AccountConfiguration Account { get; set; }
25+
public OrganizationConfiguration AdminOrganization { get; set; }
26+
public OrganizationConfiguration CollaboratorOrganization { get; set; }
27+
public BrandingConfiguration Branding { get; set; }
28+
public bool TyposquattingCheckAndBlockUsers { get; set; }
4129

4230
static GalleryConfiguration()
4331
{
4432
try
4533
{
46-
var configurationFilePath = EnvironmentSettings.ConfigurationFilePath;
47-
var configurationString = File.ReadAllText(configurationFilePath);
48-
Instance = JsonConvert.DeserializeObject<GalleryConfiguration>(configurationString);
34+
// This test suite hits the gallery which requires TLS 1.2 (at least in some environments).
35+
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
36+
37+
// Load the configuration without injection. This allows us to read KeyVault configuration.
38+
var uninjectedBuilder = new ConfigurationBuilder()
39+
.AddJsonFile(EnvironmentSettings.ConfigurationFilePath, optional: false);
40+
var uninjectedConfiguration = uninjectedBuilder.Build();
41+
42+
// Initialize KeyVault integration.
43+
var secretReaderFactory = new ConfigurationRootSecretReaderFactory(uninjectedConfiguration);
44+
var secretInjector = secretReaderFactory.CreateSecretInjector(secretReaderFactory.CreateSecretReader());
45+
46+
// Initialize the configuration with KeyVault secrets injected.
47+
var builder = new ConfigurationBuilder()
48+
.SetBasePath(Environment.CurrentDirectory)
49+
.AddInjectedJsonFile(EnvironmentSettings.ConfigurationFilePath, secretInjector);
50+
var instance = new GalleryConfiguration();
51+
builder.Build().Bind(instance);
52+
53+
Instance = instance;
4954
}
5055
catch (ArgumentException ae)
5156
{
@@ -65,40 +70,27 @@ static GalleryConfiguration()
6570

6671
public class AccountConfiguration : OrganizationConfiguration
6772
{
68-
[JsonProperty]
69-
public string Email { get; private set; }
70-
[JsonProperty]
71-
public string Password { get; private set; }
72-
[JsonProperty]
73-
public string ApiKeyPush { get; private set; }
74-
[JsonProperty]
75-
public string ApiKeyPushVersion { get; private set; }
76-
[JsonProperty]
77-
public string ApiKeyUnlist { get; private set; }
73+
public string Email { get; set; }
74+
public string Password { get; set; }
75+
public string ApiKeyPush { get; set; }
76+
public string ApiKeyPushVersion { get; set; }
77+
public string ApiKeyUnlist { get; set; }
7878
}
7979

8080
public class OrganizationConfiguration
8181
{
82-
[JsonProperty]
83-
public string Name { get; private set; }
84-
[JsonProperty]
85-
public string ApiKey { get; private set; }
82+
public string Name { get; set; }
83+
public string ApiKey { get; set; }
8684
}
8785

8886
public class BrandingConfiguration
8987
{
90-
[JsonProperty]
91-
public string Message { get; private set; }
92-
[JsonProperty]
93-
public string Url { get; private set; }
94-
[JsonProperty]
95-
public string AboutUrl { get; private set; }
96-
[JsonProperty]
97-
public string PrivacyPolicyUrl { get; private set; }
98-
[JsonProperty]
99-
public string TermsOfUseUrl { get; private set; }
100-
[JsonProperty]
101-
public string TrademarksUrl { get; private set; }
88+
public string Message { get; set; }
89+
public string Url { get; set; }
90+
public string AboutUrl { get; set; }
91+
public string PrivacyPolicyUrl { get; set; }
92+
public string TermsOfUseUrl { get; set; }
93+
public string TrademarksUrl { get; set; }
10294
}
10395
}
10496
}
Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="..\..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props')" />
4-
<Import Project="..\..\packages\xunit.core.2.3.1\build\xunit.core.props" Condition="Exists('..\..\packages\xunit.core.2.3.1\build\xunit.core.props')" />
53
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
64
<PropertyGroup>
75
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -11,7 +9,7 @@
119
<AppDesignerFolder>Properties</AppDesignerFolder>
1210
<RootNamespace>NuGetGallery.FunctionalTests</RootNamespace>
1311
<AssemblyName>NuGetGallery.FunctionalTests.Core</AssemblyName>
14-
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1513
<FileAlignment>512</FileAlignment>
1614
<NuGetPackageImportStamp>
1715
</NuGetPackageImportStamp>
@@ -36,39 +34,12 @@
3634
</PropertyGroup>
3735
<ItemGroup>
3836
<Reference Include="Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
39-
<Reference Include="Microsoft.Web.XmlTransform">
40-
<HintPath>..\..\packages\Microsoft.Web.Xdt.2.1.1\lib\net40\Microsoft.Web.XmlTransform.dll</HintPath>
41-
</Reference>
42-
<Reference Include="Newtonsoft.Json">
43-
<HintPath>..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
44-
</Reference>
45-
<Reference Include="NuGet.Core, Version=2.8.60717.93, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46-
<SpecificVersion>False</SpecificVersion>
47-
<HintPath>..\..\packages\NuGet.Core.2.8.6\lib\net40-Client\NuGet.Core.dll</HintPath>
48-
</Reference>
49-
<Reference Include="NuGet.Versioning">
50-
<HintPath>..\..\packages\NuGet.Versioning.4.4.0\lib\net45\NuGet.Versioning.dll</HintPath>
51-
<Private>True</Private>
52-
</Reference>
5337
<Reference Include="System" />
5438
<Reference Include="System.Core" />
5539
<Reference Include="Microsoft.CSharp" />
5640
<Reference Include="System.IO.Compression" />
5741
<Reference Include="System.IO.Compression.FileSystem" />
5842
<Reference Include="System.Net.Http" />
59-
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
60-
<HintPath>..\..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll</HintPath>
61-
<Private>True</Private>
62-
</Reference>
63-
<Reference Include="xunit.assert, Version=2.3.1.3858, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
64-
<HintPath>..\..\packages\xunit.assert.2.3.1\lib\netstandard1.1\xunit.assert.dll</HintPath>
65-
</Reference>
66-
<Reference Include="xunit.core, Version=2.3.1.3858, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
67-
<HintPath>..\..\packages\xunit.extensibility.core.2.3.1\lib\netstandard1.1\xunit.core.dll</HintPath>
68-
</Reference>
69-
<Reference Include="xunit.execution.desktop, Version=2.3.1.3858, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
70-
<HintPath>..\..\packages\xunit.extensibility.execution.2.3.1\lib\net452\xunit.execution.desktop.dll</HintPath>
71-
</Reference>
7243
</ItemGroup>
7344
<ItemGroup>
7445
<Compile Include="GalleryConfiguration.cs" />
@@ -100,28 +71,29 @@
10071
<Compile Include="Helpers\UrlHelper.cs" />
10172
</ItemGroup>
10273
<ItemGroup>
103-
<None Include="packages.config">
104-
<SubType>Designer</SubType>
105-
</None>
106-
</ItemGroup>
107-
<ItemGroup>
108-
<Analyzer Include="..\..\packages\xunit.analyzers.0.7.0\analyzers\dotnet\cs\xunit.analyzers.dll" />
74+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder">
75+
<Version>2.2.0</Version>
76+
</PackageReference>
77+
<PackageReference Include="Microsoft.Web.Xdt">
78+
<Version>2.1.1</Version>
79+
</PackageReference>
80+
<PackageReference Include="NuGet.Core">
81+
<Version>2.8.6</Version>
82+
</PackageReference>
83+
<PackageReference Include="NuGet.Services.Configuration">
84+
<Version>2.69.0</Version>
85+
</PackageReference>
86+
<PackageReference Include="NuGet.Versioning">
87+
<Version>4.4.0</Version>
88+
</PackageReference>
89+
<PackageReference Include="xunit">
90+
<Version>2.3.1</Version>
91+
</PackageReference>
92+
<PackageReference Include="xunit.runner.visualstudio">
93+
<Version>2.3.1</Version>
94+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
95+
<PrivateAssets>all</PrivateAssets>
96+
</PackageReference>
10997
</ItemGroup>
11098
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
111-
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
112-
<PropertyGroup>
113-
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
114-
</PropertyGroup>
115-
<Error Condition="!Exists('..\..\packages\xunit.core.2.3.1\build\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.core.2.3.1\build\xunit.core.props'))" />
116-
<Error Condition="!Exists('..\..\packages\xunit.core.2.3.1\build\xunit.core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.core.2.3.1\build\xunit.core.targets'))" />
117-
<Error Condition="!Exists('..\..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props'))" />
118-
</Target>
119-
<Import Project="..\..\packages\xunit.core.2.3.1\build\xunit.core.targets" Condition="Exists('..\..\packages\xunit.core.2.3.1\build\xunit.core.targets')" />
120-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
121-
Other similar extension points exist, see Microsoft.Common.targets.
122-
<Target Name="BeforeBuild">
123-
</Target>
124-
<Target Name="AfterBuild">
125-
</Target>
126-
-->
12799
</Project>

tests/NuGetGallery.FunctionalTests.Core/packages.config

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)