Skip to content

Backport ANCM environment variable additions #20006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ ShimOptions::ShimOptions(const ConfigurationSource &configurationSource) :
.value_or(environmentVariables[CS_ASPNETCORE_ENVIRONMENT]);
const auto dotnetEnvironment = Environment::GetEnvironmentVariableValue(CS_DOTNET_ENVIRONMENT)
.value_or(environmentVariables[CS_DOTNET_ENVIRONMENT]);
// We prefer the environment variables for LAUNCHER_PATH and LAUNCHER_ARGS
m_strProcessPath = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_PATH)
.value_or(m_strProcessPath);
m_strArguments = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_ARGS)
.value_or(m_strArguments);

auto detailedErrorsEnabled = equals_ignore_case(L"1", detailedErrors) || equals_ignore_case(L"true", detailedErrors);
auto aspnetCoreEnvironmentEnabled = equals_ignore_case(L"Development", aspnetCoreEnvironment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#define CS_ASPNETCORE_DETAILEDERRORS L"ASPNETCORE_DETAILEDERRORS"
#define CS_ASPNETCORE_ENVIRONMENT L"ASPNETCORE_ENVIRONMENT"
#define CS_DOTNET_ENVIRONMENT L"DOTNET_ENVIRONMENT"
#define CS_ANCM_LAUNCHER_PATH L"ANCM_LAUNCHER_PATH"
#define CS_ANCM_LAUNCHER_ARGS L"ANCM_LAUNCHER_ARGS"

class ConfigurationSection: NonCopyable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "InProcessOptions.h"
#include "InvalidOperationException.h"
#include "EventLog.h"
#include "Environment.h"

HRESULT InProcessOptions::Create(
IHttpServer& pServer,
Expand Down Expand Up @@ -51,6 +52,11 @@ InProcessOptions::InProcessOptions(const ConfigurationSource &configurationSourc
auto const aspNetCoreSection = configurationSource.GetRequiredSection(CS_ASPNETCORE_SECTION);
m_strArguments = aspNetCoreSection->GetString(CS_ASPNETCORE_PROCESS_ARGUMENTS).value_or(CS_ASPNETCORE_PROCESS_ARGUMENTS_DEFAULT);
m_strProcessPath = aspNetCoreSection->GetRequiredString(CS_ASPNETCORE_PROCESS_EXE_PATH);
// We prefer the environment variables for LAUNCHER_PATH and LAUNCHER_ARGS
m_strProcessPath = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_PATH)
.value_or(m_strProcessPath);
m_strArguments = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_ARGS)
.value_or(m_strArguments);
m_fStdoutLogEnabled = aspNetCoreSection->GetRequiredBool(CS_ASPNETCORE_STDOUT_LOG_ENABLED);
m_struStdoutLogFile = aspNetCoreSection->GetRequiredString(CS_ASPNETCORE_STDOUT_LOG_FILE);
m_fDisableStartUpErrorPage = aspNetCoreSection->GetRequiredBool(CS_ASPNETCORE_DISABLE_START_UP_ERROR_PAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define ASPNETCORE_IIS_AUTH_BASIC L"basic;"
#define ASPNETCORE_IIS_AUTH_ANONYMOUS L"anonymous;"
#define ASPNETCORE_IIS_AUTH_NONE L"none"
#define ANCM_PREFER_ENVIRONMENT_VARIABLES_ENV_STR L"ANCM_PREFER_ENVIRONMENT_VARIABLES"

//
// The key used for hash-table lookups, consists of the port on which the http process is created.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,27 @@ class ENVIRONMENT_VAR_HELPERS
environmentVariables.insert_or_assign(HOSTING_STARTUP_ASSEMBLIES_ENV_STR, hostingStartupValues);
}

auto preferEnvironmentVariablesSetting = Environment::GetEnvironmentVariableValue(ANCM_PREFER_ENVIRONMENT_VARIABLES_ENV_STR).value_or(L"false");
auto preferEnvironmentVariables = equals_ignore_case(L"1", preferEnvironmentVariablesSetting) || equals_ignore_case(L"true", preferEnvironmentVariablesSetting);

for (auto& environmentVariable : environmentVariables)
{
environmentVariable.second = Environment::ExpandEnvironmentVariables(environmentVariable.second);
if (preferEnvironmentVariables)
{
auto env = Environment::GetEnvironmentVariableValue(environmentVariable.first);
if (env.has_value())
{
environmentVariable.second = env.value();
}
else
{
environmentVariable.second = Environment::ExpandEnvironmentVariables(environmentVariable.second);
}
}
else
{
environmentVariable.second = Environment::ExpandEnvironmentVariables(environmentVariable.second);
}
}

return environmentVariables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "environmentvariablehash.h"
#include "exceptions.h"
#include "config_utility.h"
#include "Environment.h"

REQUESTHANDLER_CONFIG::~REQUESTHANDLER_CONFIG()
{
Expand Down Expand Up @@ -101,6 +102,8 @@ REQUESTHANDLER_CONFIG::Populate(
BSTR bstrBasicAuthSection = NULL;
BSTR bstrAnonymousAuthSection = NULL;
BSTR bstrAspNetCoreSection = NULL;
std::optional<std::wstring> launcherPathEnv;
std::optional<std::wstring> launcherArgsEnv;

pAdminManager = pHttpServer->GetAdminManager();
try
Expand Down Expand Up @@ -248,12 +251,47 @@ REQUESTHANDLER_CONFIG::Populate(
goto Finished;
}

hr = GetElementStringProperty(pAspNetCoreElement,
CS_ASPNETCORE_PROCESS_EXE_PATH,
&m_struProcessPath);
if (FAILED(hr))
// We prefer the environment variables for LAUNCHER_PATH and LAUNCHER_ARGS
try
{
goto Finished;
launcherPathEnv = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_PATH);
launcherArgsEnv = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_ARGS);
}
catch(...)
{
FINISHED_IF_FAILED(E_FAIL);
}

if (launcherPathEnv.has_value())
{
hr = m_struProcessPath.Copy(launcherPathEnv.value().c_str());
FINISHED_IF_FAILED(hr);
}
else
{
hr = GetElementStringProperty(pAspNetCoreElement,
CS_ASPNETCORE_PROCESS_EXE_PATH,
&m_struProcessPath);
if (FAILED(hr))
{
goto Finished;
}
}

if (launcherArgsEnv.has_value())
{
hr = m_struArguments.Copy(launcherArgsEnv.value().c_str());
FINISHED_IF_FAILED(hr);
}
else
{
hr = GetElementStringProperty(pAspNetCoreElement,
CS_ASPNETCORE_PROCESS_ARGUMENTS,
&m_struArguments);
if (FAILED(hr))
{
goto Finished;
}
}

hr = GetElementStringProperty(pAspNetCoreElement,
Expand Down Expand Up @@ -281,14 +319,6 @@ REQUESTHANDLER_CONFIG::Populate(
goto Finished;
}

hr = GetElementStringProperty(pAspNetCoreElement,
CS_ASPNETCORE_PROCESS_ARGUMENTS,
&m_struArguments);
if (FAILED(hr))
{
goto Finished;
}

hr = GetElementDWORDProperty(pAspNetCoreElement,
CS_ASPNETCORE_RAPID_FAILS_PER_MINUTE,
&m_dwRapidFailsPerMinute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,22 @@ private async Task WebConfigExpandsVariables(HostingModel hostingModel)
deploymentParameters.WebConfigBasedEnvironmentVariables["OtherVariable"] = "%TestVariable%;Hello";
Assert.Equal("World;Hello", await GetStringAsync(deploymentParameters, "/GetEnvironmentVariable?name=OtherVariable"));
}

[ConditionalTheory]
[RequiresIIS(IISCapability.PoolEnvironmentVariables)]
[RequiresNewHandler]
[RequiresNewShim]
[InlineData(HostingModel.InProcess)]
[InlineData(HostingModel.OutOfProcess)]
public async Task PreferEnvironmentVariablesOverWebConfigWhenConfigured(HostingModel hostingModel)
{
var deploymentParameters = Fixture.GetBaseDeploymentParameters(hostingModel);

var environment = "Development";
deploymentParameters.EnvironmentVariables["ANCM_PREFER_ENVIRONMENT_VARIABLES"] = "true";
deploymentParameters.EnvironmentVariables["ASPNETCORE_ENVIRONMENT"] = environment;
deploymentParameters.WebConfigBasedEnvironmentVariables.Add("ASPNETCORE_ENVIRONMENT", "Debug");
Assert.Equal(environment, await GetStringAsync(deploymentParameters, "/GetEnvironmentVariable?name=ASPNETCORE_ENVIRONMENT"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,39 @@ public async Task StackOverflowCanBeSetBySettingLargerStackViaHandlerSetting()
Assert.True(result.IsSuccessStatusCode);
}

[ConditionalTheory]
[RequiresIIS(IISCapability.PoolEnvironmentVariables)]
[RequiresNewShim]
[RequiresNewHandler]
[InlineData(HostingModel.InProcess)]
[InlineData(HostingModel.OutOfProcess)]
public async Task EnvironmentVariableForLauncherPathIsPreferred(HostingModel hostingModel)
{
var deploymentParameters = Fixture.GetBaseDeploymentParameters(hostingModel);

deploymentParameters.EnvironmentVariables["ANCM_LAUNCHER_PATH"] = _dotnetLocation;
deploymentParameters.WebConfigActionList.Add(WebConfigHelpers.AddOrModifyAspNetCoreSection("processPath", "nope"));

await StartAsync(deploymentParameters);
}

[ConditionalTheory]
[RequiresIIS(IISCapability.PoolEnvironmentVariables)]
[RequiresNewShim]
[RequiresNewHandler]
[InlineData(HostingModel.InProcess)]
[InlineData(HostingModel.OutOfProcess)]
public async Task EnvironmentVariableForLauncherArgsIsPreferred(HostingModel hostingModel)
{
var deploymentParameters = Fixture.GetBaseDeploymentParameters(hostingModel);
using var publishedApp = await deploymentParameters.ApplicationPublisher.Publish(deploymentParameters, LoggerFactory.CreateLogger("test"));

deploymentParameters.EnvironmentVariables["ANCM_LAUNCHER_ARGS"] = Path.ChangeExtension(Path.Combine(publishedApp.Path, deploymentParameters.ApplicationPublisher.ApplicationPath), ".dll");
deploymentParameters.WebConfigActionList.Add(WebConfigHelpers.AddOrModifyAspNetCoreSection("arguments", "nope"));

await StartAsync(deploymentParameters);
}

private static void VerifyDotnetRuntimeEventLog(IISDeploymentResult deploymentResult)
{
var entries = GetEventLogsFromDotnetRuntime(deploymentResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ static RequiresIISAttribute()

var ancmConfigPath = Path.Combine(Environment.SystemDirectory, "inetsrv", "config", "schema", "aspnetcore_schema.xml");

if (!File.Exists(ancmConfigPath))
{
ancmConfigPath = Path.Combine(Environment.SystemDirectory, "inetsrv", "config", "schema", "aspnetcore_schema_v2.xml");
}

if (!File.Exists(ancmConfigPath) && !SkipInVSTSAttribute.RunningInVSTS)
{
_skipReasonStatic = "IIS Schema is not installed.";
Expand Down