Skip to content
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

Fix exception import Az.Accounts caused by env var #18312

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
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->

## Upcoming Release
* Fixed an issue that Az.Accounts failed to be imported if multiple environment variables, which only differ by case, are set. [#18304]

## Version 2.8.0
* Added a preview feature allowing user to control the following configurations by using `Get-AzConfig`, `Update-AzConfig` and `Clear-AzConfig`:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Authentication.Config;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Xunit;
using System;

namespace Microsoft.Azure.Authentication.Test.UnitTests
{
public class DefaultEnvironmentVariableProviderTests
{
[Fact]
[Trait(TestTraits.AcceptanceType, TestTraits.CheckIn)]
public void ShouldNotThrowWhen2EnvVarDifferOnlyByCase()
{
const string varName = "NameOfAnEnvVar";
Environment.SetEnvironmentVariable(varName, "a");
string varNameInUpper = varName.ToUpperInvariant();
Environment.SetEnvironmentVariable(varNameInUpper, "b");

try
{
DefaultEnvironmentVariableProvider provider = new DefaultEnvironmentVariableProvider();
var dict = provider.List();
Assert.True(dict.ContainsKey(varName));
Assert.True(dict.ContainsKey(varNameInUpper));
Assert.Equal(dict[varName], dict[varNameInUpper]);
}
finally
{
Environment.SetEnvironmentVariable(varName, null);
Environment.SetEnvironmentVariable(varNameInUpper, null);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Microsoft.Azure.Commands.Common.Authentication.Config
Expand All @@ -32,9 +33,15 @@ public string Get(string variableName, EnvironmentVariableTarget target = Enviro

public IReadOnlyDictionary<string, string> List(EnvironmentVariableTarget target = EnvironmentVariableTarget.Process)
{
return Environment.GetEnvironmentVariables()
.Cast<DictionaryEntry>()
.ToDictionary(pair => pair.Key.ToString(), pair => pair.Value.ToString(), StringComparer.OrdinalIgnoreCase);
IDictionary source = Environment.GetEnvironmentVariables();
IDictionary<string, string> results = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// cannot use ToDictionary() because keys (names of env var) may duplicate on Linux,
// with case being the only difference
foreach (var item in source.Cast<DictionaryEntry>())
{
results[item.Key.ToString()] = item.Value.ToString();
}
return new ReadOnlyDictionary<string, string>(results);
}

public void Set(string variableName, string value, EnvironmentVariableTarget target = EnvironmentVariableTarget.Process)
Expand Down