Skip to content

Commit 6f6aa32

Browse files
erich-wangcormacpaynexiaohuyyyy-msisra-felmsJinLei
authored
Add support for Azure.Identity integration (#218)
* Add method needed to get TokenCache for MSAL * Change name of cache to "msal.cache" * Add event to allow writing to various streams * Update Dependencies.Test.targets * Update Dependencies.Test.targets * change the deprecated licenseUrl element to license file instead * change the deprecated licenseUrl element to using license file link instead * Resolve review feedback * fix no default context * add a property 'environment' to AzureSession * Disable context auto saving when AzureRmContext.json not available Fix the issue Azure/azure-powershell#8963 Enable warning messages printout in module importing. * Add EnqueueDebugMessage * Add az version, time, and subscription id to client telemetry * Catch exception if host is not available * change require default context method * add support for Azure.Identity integration * use right default tenant Co-authored-by: cormacpayne <[email protected]> Co-authored-by: xiaohuyyyy-ms <[email protected]> Co-authored-by: Yeming Liu <[email protected]> Co-authored-by: msJinLei <[email protected]> Co-authored-by: Dingmeng Xue <[email protected]>
1 parent c091c90 commit 6f6aa32

30 files changed

+299
-41
lines changed

Azure.PowerShell.Common.sln

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29613.14
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.1231
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common", "src\Common\Common.csproj", "{5EE72C53-1720-4309-B54B-5FB79703195F}"
77
EndProject

src/Aks/Aks.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PackageTags>azure;powershell;clients;aks</PackageTags>
1818
<Authors>Microsoft Corporation</Authors>
1919
<Copyright>Copyright © Microsoft Corporation</Copyright>
20-
<PackageLicenseUrl>https://aka.ms/azps-common-license</PackageLicenseUrl>
20+
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2121
<PackageProjectUrl>https://github.com/Azure/azure-powershell-common</PackageProjectUrl>
2222
<PackageOutputPath>$(ProjectDir)..\..\artifacts\Package\$(Configuration)</PackageOutputPath>
2323
</PropertyGroup>
@@ -49,4 +49,8 @@
4949
</EmbeddedResource>
5050
</ItemGroup>
5151

52+
<ItemGroup>
53+
<None Include="..\..\LICENSE.txt" Link="LICENSE.txt" Pack="true" PackagePath="" />
54+
</ItemGroup>
55+
5256
</Project>

src/Authentication.Abstractions/Authentication.Abstractions.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<PackageTags>azure;powershell;authentication;abstractions</PackageTags>
2121
<Authors>Microsoft Corporation</Authors>
2222
<Copyright>Copyright © Microsoft Corporation</Copyright>
23-
<PackageLicenseUrl>https://aka.ms/azps-common-license</PackageLicenseUrl>
23+
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2424
<PackageProjectUrl>https://github.com/Azure/azure-powershell-common</PackageProjectUrl>
2525
<PackageOutputPath>$(ProjectDir)..\..\artifacts\Package\$(Configuration)</PackageOutputPath>
2626
</PropertyGroup>
@@ -53,6 +53,7 @@
5353
</ItemGroup>
5454

5555
<ItemGroup>
56+
<None Include="..\..\LICENSE.txt" Link="LICENSE.txt" Pack="true" PackagePath="" />
5657
<Folder Include="Models\" />
5758
</ItemGroup>
5859

src/Authentication.Abstractions/AuthenticationStore.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@ namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
2020
/// State storage for token cache
2121
/// </summary>
2222
[Serializable]
23-
public class AzureTokenCache : IAzureTokenCache
23+
public class AzureTokenCache : IAzureTokenCache, IAzureMsalTokenCache
2424
{
2525
/// <summary>
2626
/// Token Cache serialization
2727
/// </summary>
2828
public virtual byte[] CacheData {get; set;}
2929

30+
/// <summary>
31+
/// Get the user token cache.
32+
/// </summary>
33+
public object GetUserCache()
34+
{
35+
return null;
36+
}
37+
3038
/// <summary>
3139
/// Remove all token cache state
3240
/// </summary>

src/Authentication.Abstractions/AzureAccount.cs

+10
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ public static class Property
105105
/// </summary>
106106
AccessToken = "AccessToken",
107107

108+
/// <summary>
109+
/// Account object id + home tenant id.
110+
/// </summary>
111+
HomeAccountId = "HomeAccountId",
112+
113+
/// <summary>
114+
/// Indicate whether to use user name and password for authentication.
115+
/// </summary>
116+
UsePasswordAuth = "UsePasswordAuth",
117+
108118
/// <summary>
109119
/// Access token for AD Graph service.
110120
/// </summary>

src/Authentication.Abstractions/AzureSession.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public void ClearComponents()
254254

255255
void ChangeRegistry(Action changeAction)
256256
{
257-
changeAction();
257+
changeAction();
258258
}
259259

260260
private class ComponentKey : IComparable<ComponentKey>, IEquatable<ComponentKey>
@@ -341,5 +341,13 @@ public int GetHashCode(ComponentKey obj)
341341
return obj.GetHashCode();
342342
}
343343
}
344+
345+
public static class Property
346+
{
347+
/// <summary>
348+
/// Name of the current environment
349+
/// </summary>
350+
public const string Environment = "Environment";
351+
}
344352
}
345353
}

src/Authentication.Abstractions/Extensions/AzureAccountExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public static void Update(this IAzureAccount account, IAzureAccount source)
231231
/// <param name="account">The account to get tenant from</param>
232232
public static string GetCommonTenant(this IAzureAccount account)
233233
{
234-
string result = AzureEnvironmentConstants.CommonAdTenant;
234+
string result = "organizations";
235235
if (account.IsPropertySet(AzureAccount.Property.Tenants))
236236
{
237237
var candidate = account.GetTenants().FirstOrDefault();

src/Authentication.Abstractions/Interfaces/IAccessToken.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
using System;
1616

17+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
18+
1719
namespace Microsoft.Azure.Commands.Common.Authentication
1820
{
1921
/// <summary>
2022
/// Canonical representation of a renewable access token
2123
/// </summary>
22-
public interface IAccessToken
24+
public interface IAccessToken : IExtensibleModel
2325
{
2426
/// <summary>
2527
/// Authorize the given request, using the given function for setting the token
@@ -37,6 +39,11 @@ public interface IAccessToken
3739
/// </summary>
3840
string UserId { get; }
3941

42+
/// <summary>
43+
/// The object id + home tenant id
44+
/// </summary>
45+
string HomeAccountId { get; }
46+
4047
/// <summary>
4148
/// The Active Directory tenant for this token
4249
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
18+
{
19+
public interface IAzureEventListener : IDisposable
20+
{
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
18+
{
19+
public interface IAzureEventListenerFactory
20+
{
21+
IAzureEventListener GetAzureEventListener(Action<string> action);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
16+
{
17+
/// <summary>
18+
/// State storage for an MSAL token cache
19+
/// </summary>
20+
public interface IAzureMsalTokenCache
21+
{
22+
/// <summary>
23+
/// Get the user token cache.
24+
/// </summary>
25+
/// <returns>The user token cache.</returns>
26+
object GetUserCache();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
18+
{
19+
public class StreamEventArgs : EventArgs
20+
{
21+
/// <summary>
22+
/// The message to write to the corresponding stream.
23+
/// </summary>
24+
public string Message { get; set; }
25+
}
26+
}

src/Authorization/Authorization.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PackageTags>azure;powershell;clients;authorization</PackageTags>
1818
<Authors>Microsoft Corporation</Authors>
1919
<Copyright>Copyright © Microsoft Corporation</Copyright>
20-
<PackageLicenseUrl>https://aka.ms/azps-common-license</PackageLicenseUrl>
20+
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2121
<PackageProjectUrl>https://github.com/Azure/azure-powershell-common</PackageProjectUrl>
2222
<PackageOutputPath>$(ProjectDir)..\..\artifacts\Package\$(Configuration)</PackageOutputPath>
2323
</PropertyGroup>
@@ -49,4 +49,8 @@
4949
</EmbeddedResource>
5050
</ItemGroup>
5151

52+
<ItemGroup>
53+
<None Include="..\..\LICENSE.txt" Link="LICENSE.txt" Pack="true" PackagePath="" />
54+
</ItemGroup>
55+
5256
</Project>

src/Common/AzurePSCmdlet.cs

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Globalization;
2626
using System.IO;
2727
using System.Linq;
28+
using System.Linq.Expressions;
2829
using System.Management.Automation;
2930
using System.Net.Http.Headers;
3031
using System.Text;
@@ -42,6 +43,7 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
4243

4344
public ConcurrentQueue<string> DebugMessages { get; private set; }
4445

46+
IAzureEventListener _azureEventListener;
4547
protected static ConcurrentQueue<string> InitializationWarnings { get; set; } = new ConcurrentQueue<string>();
4648

4749
private RecordingTracingInterceptor _httpTracingInterceptor;
@@ -328,12 +330,23 @@ protected virtual void SetupDebuggingTraces()
328330
_adalListener = _adalListener ?? new DebugStreamTraceListener(DebugMessages);
329331
RecordingTracingInterceptor.AddToContext(_httpTracingInterceptor);
330332
DebugStreamTraceListener.AddAdalTracing(_adalListener);
333+
334+
if (AzureSession.Instance.TryGetComponent(nameof(IAzureEventListenerFactory), out IAzureEventListenerFactory factory))
335+
{
336+
_azureEventListener = factory.GetAzureEventListener(
337+
(message) =>
338+
{
339+
DebugMessages.Enqueue(message);
340+
});
341+
}
331342
}
332343

333344
protected virtual void TearDownDebuggingTraces()
334345
{
335346
RecordingTracingInterceptor.RemoveFromContext(_httpTracingInterceptor);
336347
DebugStreamTraceListener.RemoveAdalTracing(_adalListener);
348+
_azureEventListener?.Dispose();
349+
_azureEventListener = null;
337350
FlushDebugMessages();
338351
}
339352

src/Common/AzurePowerShell.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class AzurePowerShell
3636

3737
public const string OldProfileFileBackup = "WindowsAzureProfile.xml.bak";
3838

39-
public const string TokenCacheFile = "TokenCache.dat";
39+
public const string TokenCacheFile = "msal.cache";
4040

4141
public static ProductInfoHeaderValue UserAgentValue = new ProductInfoHeaderValue(
4242
"AzurePowershell",

src/Common/Common.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<PackageTags>azure;powershell;common</PackageTags>
2323
<Authors>Microsoft Corporation</Authors>
2424
<Copyright>Copyright © Microsoft Corporation</Copyright>
25-
<PackageLicenseUrl>https://aka.ms/azps-common-license</PackageLicenseUrl>
25+
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2626
<PackageProjectUrl>https://github.com/Azure/azure-powershell-common</PackageProjectUrl>
2727
<PackageOutputPath>$(ProjectDir)..\..\artifacts\Package\$(Configuration)</PackageOutputPath>
2828
</PropertyGroup>
@@ -62,4 +62,8 @@
6262
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.4.0" />
6363
</ItemGroup>
6464

65+
<ItemGroup>
66+
<None Include="..\..\LICENSE.txt" Link="LICENSE.txt" Pack="true" PackagePath="" />
67+
</ItemGroup>
68+
6569
</Project>

src/Compute/Compute.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PackageTags>azure;powershell;clients;compute</PackageTags>
1818
<Authors>Microsoft Corporation</Authors>
1919
<Copyright>Copyright © Microsoft Corporation</Copyright>
20-
<PackageLicenseUrl>https://aka.ms/azps-common-license</PackageLicenseUrl>
20+
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2121
<PackageProjectUrl>https://github.com/Azure/azure-powershell-common</PackageProjectUrl>
2222
<PackageOutputPath>$(ProjectDir)..\..\artifacts\Package\$(Configuration)</PackageOutputPath>
2323
</PropertyGroup>
@@ -34,4 +34,8 @@
3434
<DefineConstants>TRACE;RELEASE;NETSTANDARD;SIGN</DefineConstants>
3535
</PropertyGroup>
3636

37+
<ItemGroup>
38+
<None Include="..\..\LICENSE.txt" Link="LICENSE.txt" Pack="true" PackagePath="" />
39+
</ItemGroup>
40+
3741
</Project>

src/Dependencies.Test.targets

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="1.9.0-preview" />
99
<PackageReference Include="Microsoft.Azure.Test.HttpRecorder" Version="1.13.3" />
10-
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure.Authentication" Version="2.3.7" />
11-
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.14.0" />
10+
<PackageReference Include="Microsoft.Identity.Client" Version="3.0.8" />
1211
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure.TestFramework" Version="1.7.6" />
1312
<PackageReference Include="xunit" Version="2.4.0" />
1413
</ItemGroup>
@@ -20,4 +19,4 @@
2019
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
2120
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.2.4" IncludeAssets="All" />
2221
</ItemGroup>
23-
</Project>
22+
</Project>

src/Graph.Rbac/Graph.Rbac.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<PackageTags>azure;powershell;clients;graph;rbac</PackageTags>
2121
<Authors>Microsoft Corporation</Authors>
2222
<Copyright>Copyright © Microsoft Corporation</Copyright>
23-
<PackageLicenseUrl>https://aka.ms/azps-common-license</PackageLicenseUrl>
23+
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2424
<PackageProjectUrl>https://github.com/Azure/azure-powershell-common</PackageProjectUrl>
2525
<PackageOutputPath>$(ProjectDir)..\..\artifacts\Package\$(Configuration)</PackageOutputPath>
2626
</PropertyGroup>
@@ -58,4 +58,8 @@
5858
</EmbeddedResource>
5959
</ItemGroup>
6060

61+
<ItemGroup>
62+
<None Include="..\..\LICENSE.txt" Link="LICENSE.txt" Pack="true" PackagePath="" />
63+
</ItemGroup>
64+
6165
</Project>

src/Graph.Rbac/Graph.Rbac.nuspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<authors>Microsoft Corporation</authors>
88
<owners>Microsoft Corporation</owners>
99
<requireLicenseAcceptance>false</requireLicenseAcceptance>
10-
<licenseUrl>https://aka.ms/azps-common-license</licenseUrl>
10+
<license type="file">LICENSE.txt</license>
1111
<projectUrl>https://github.com/Azure/azure-powershell-common</projectUrl>
1212
<description>Microsoft Azure PowerShell Clients Graph Rbac library. Only for use with the Azure PowerShell runtime. Not intended for general development use.</description>
1313
<copyright>Copyright © Microsoft Corporation</copyright>

0 commit comments

Comments
 (0)