Skip to content

Commit 85adce9

Browse files
authored
Merge pull request Azure#4390 from cormacpayne/adal-fix
Fix error when authenticating with ADAL
2 parents 17104f3 + 8a47a84 commit 85adce9

File tree

10 files changed

+140
-34
lines changed

10 files changed

+140
-34
lines changed

setup/azurecmdfiles.wxi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,9 @@
22282228
<Component Id="cmp1460DC2324E946B9F5F76360ED57C0FE" Guid="*">
22292229
<File Id="fil63597DE31C6B3211017C4B4EAD93F683" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.Profile\Microsoft.IdentityModel.Clients.ActiveDirectory.dll" />
22302230
</Component>
2231+
<Component Id="cmp95697C7A2E29A43F7195D799D68E138B" Guid="*">
2232+
<File Id="fil53FA53B6DEFBC23592C3C2A82969E769" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.Profile\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll" />
2233+
</Component>
22312234
<Component Id="cmp3BEF3C9459257F053CAAC956B7CFA10D" Guid="*">
22322235
<File Id="fil830F7BE66C1E1324FCE632956432C2C7" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.Profile\Microsoft.Rest.ClientRuntime.Azure.Authentication.dll" />
22332236
</Component>
@@ -6063,6 +6066,7 @@
60636066
<ComponentRef Id="cmpE2AF801EDF4F7F1C347F9D7052313074" />
60646067
<ComponentRef Id="cmp6004E0F8B6FA21BF5CAE3CFCA2CA9E67" />
60656068
<ComponentRef Id="cmp1460DC2324E946B9F5F76360ED57C0FE" />
6069+
<ComponentRef Id="cmp95697C7A2E29A43F7195D799D68E138B" />
60666070
<ComponentRef Id="cmp3BEF3C9459257F053CAAC956B7CFA10D" />
60676071
<ComponentRef Id="cmpB7D392D58F12D947C1D915326ABA98EA" />
60686072
<ComponentRef Id="cmp61D87C757ACDE206CC79A5B19CE403B2" />

src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMCmdlet.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,19 @@ protected override void Dispose(bool disposing)
311311
protected override void BeginProcessing()
312312
{
313313
AzureSession.Instance.ClientFactory.RemoveHandler(typeof(RPRegistrationDelegatingHandler));
314-
AzureSession.Instance.ClientFactory.AddHandler(new RPRegistrationDelegatingHandler(
315-
() => new ResourceManagementClient(
316-
DefaultContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
317-
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(DefaultContext, AzureEnvironment.Endpoint.ResourceManager)),
318-
s => DebugMessages.Enqueue(s)));
314+
if (DefaultContext != null && DefaultContext.Subscription != null)
315+
{
316+
AzureSession.Instance.ClientFactory.AddHandler(new RPRegistrationDelegatingHandler(
317+
() =>
318+
{
319+
var client = new ResourceManagementClient(
320+
DefaultContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
321+
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(DefaultContext, AzureEnvironment.Endpoint.ResourceManager));
322+
client.SubscriptionId = DefaultContext.Subscription.Id;
323+
return client;
324+
},
325+
s => DebugMessages.Enqueue(s)));
326+
}
319327

320328
base.BeginProcessing();
321329
}

src/ResourceManager/Profile/Commands.Profile.Test/ErrorResolutionTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,46 @@ public void HandlesExceptionError()
124124
Assert.Contains("Autorest message", autorestResult.ServerMessage);
125125
}
126126

127+
[Fact]
128+
[Trait(Category.AcceptanceType, Category.CheckIn)]
129+
public void HandlesNullValuesInArmExceptions()
130+
{
131+
var runtime = new MockCommandRuntime();
132+
var hyakException = new TestHyakException(null, null, null);
133+
134+
var autorestException = new Microsoft.Rest.Azure.CloudException();
135+
136+
var cmdlet = new ResolveError
137+
{
138+
Error = new[]
139+
{
140+
new ErrorRecord(new Exception(), null, ErrorCategory.AuthenticationError, null),
141+
new ErrorRecord(hyakException, null, ErrorCategory.ConnectionError, null),
142+
new ErrorRecord(autorestException , null, ErrorCategory.InvalidOperation, null),
143+
},
144+
CommandRuntime = runtime
145+
};
146+
147+
cmdlet.ExecuteCmdlet();
148+
Assert.NotNull(runtime.OutputPipeline);
149+
Assert.Equal(3, runtime.OutputPipeline.Count);
150+
var errorResult = runtime.OutputPipeline[0] as AzureExceptionRecord;
151+
Assert.NotNull(errorResult);
152+
Assert.Equal(ErrorCategory.AuthenticationError, errorResult.ErrorCategory.Category);
153+
Assert.NotNull(errorResult.Exception);
154+
Assert.Equal(errorResult.Exception.GetType(), typeof(Exception));
155+
var hyakResult = runtime.OutputPipeline[1] as AzureRestExceptionRecord;
156+
Assert.NotNull(hyakResult);
157+
Assert.Equal(ErrorCategory.ConnectionError, hyakResult.ErrorCategory.Category);
158+
Assert.NotNull(errorResult.Exception);
159+
Assert.Equal(hyakResult.Exception.GetType(), typeof(TestHyakException));
160+
var autorestResult = runtime.OutputPipeline[2] as AzureRestExceptionRecord;
161+
Assert.NotNull(autorestResult);
162+
Assert.Equal(ErrorCategory.InvalidOperation, autorestResult.ErrorCategory.Category);
163+
Assert.NotNull(autorestResult.Exception);
164+
Assert.Equal(autorestResult.Exception.GetType(), typeof(Microsoft.Rest.Azure.CloudException));
165+
}
166+
127167
[Fact]
128168
[Trait(Category.AcceptanceType, Category.CheckIn)]
129169
public void LastParameterFindsLastError()

src/ResourceManager/Profile/Commands.Profile/Commands.Profile.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949
<ItemGroup>
5050
<Reference Include="Microsoft.Build" />
5151
<Reference Include="Microsoft.Build.Framework" />
52+
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.28.3.860, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
53+
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.28.3\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
54+
<Private>True</Private>
55+
</Reference>
56+
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms, Version=2.28.3.860, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
57+
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.28.3\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
58+
<Private>True</Private>
59+
</Reference>
5260
<Reference Include="System.ComponentModel.DataAnnotations" />
5361
<Reference Include="System.Configuration.Install" />
5462
<Reference Include="System.Data.Services.Client" />

src/ResourceManager/Profile/Commands.Profile/Errors/AzureErrorRecord.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ public class AzureErrorRecord
2525
{
2626
public AzureErrorRecord(ErrorRecord record)
2727
{
28-
InvocationInfo = record.InvocationInfo;
29-
ScriptStackTrace = record.ScriptStackTrace;
30-
ErrorCategory = record.CategoryInfo;
31-
ErrorDetails = record.ErrorDetails;
28+
if (record != null)
29+
{
30+
InvocationInfo = record.InvocationInfo;
31+
ScriptStackTrace = record.ScriptStackTrace;
32+
ErrorCategory = record.CategoryInfo;
33+
ErrorDetails = record.ErrorDetails;
34+
}
3235
}
3336

3437
public ErrorDetails ErrorDetails { get; set; }

src/ResourceManager/Profile/Commands.Profile/Errors/AzureExceptionRecord.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ public class AzureExceptionRecord : AzureErrorRecord
2121
{
2222
public AzureExceptionRecord(Exception exception, ErrorRecord record, bool inner = false) : base(record)
2323
{
24-
Message = exception.Message;
25-
HelpLink = exception.HelpLink;
26-
StackTrace = exception.StackTrace;
24+
if (exception != null)
25+
{
26+
Message = exception.Message;
27+
HelpLink = exception.HelpLink;
28+
StackTrace = exception.StackTrace;
29+
}
30+
2731
Exception = exception;
2832
}
2933

src/ResourceManager/Profile/Commands.Profile/Errors/AzureRestExceptionRecord.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,46 @@ public class AzureRestExceptionRecord : AzureExceptionRecord
2525
{
2626
public AzureRestExceptionRecord(Hyak.Common.CloudException exception, ErrorRecord record, bool inner = false) : base(exception, record, inner)
2727
{
28-
ServerMessage = string.Format($"{exception.Error.Code}: {exception.Error.Message} ({exception.Error.OriginalMessage})");
29-
ServerResponse = new HttpResponseInfo(exception.Response);
30-
RequestMessage = new HttpRequestInfo(exception.Request);
28+
if (exception != null)
29+
{
30+
if (exception.Error != null)
31+
{
32+
ServerMessage = string.Format($"{exception.Error.Code}: {exception.Error.Message} ({exception.Error.OriginalMessage})");
33+
}
34+
35+
if (exception.Response != null)
36+
{
37+
ServerResponse = new HttpResponseInfo(exception.Response);
38+
}
39+
40+
if (exception.Request != null)
41+
{
42+
RequestMessage = new HttpRequestInfo(exception.Request);
43+
}
44+
}
3145
}
3246

3347
public AzureRestExceptionRecord(Microsoft.Rest.Azure.CloudException exception, ErrorRecord record, bool inner = false) : base(exception, record, inner)
3448
{
35-
ServerMessage = string.Format($"{exception.Body.Code}: {exception.Body.Message} ({exception.Body.Details})");
36-
ServerResponse = new HttpResponseInfo(exception.Response);
37-
RequestMessage = new HttpRequestInfo(exception.Request);
38-
RequestId = exception.RequestId;
49+
if (exception != null)
50+
{
51+
if (exception.Body != null)
52+
{
53+
ServerMessage = string.Format($"{exception.Body.Code}: {exception.Body.Message} ({exception.Body.Details})");
54+
}
55+
56+
if (exception.Response != null)
57+
{
58+
ServerResponse = new HttpResponseInfo(exception.Response);
59+
}
60+
61+
if (exception.Request != null)
62+
{
63+
RequestMessage = new HttpRequestInfo(exception.Request);
64+
}
65+
66+
RequestId = exception.RequestId;
67+
}
3968
}
4069
public string ServerMessage { get; set; }
4170

src/ResourceManager/Profile/Commands.Profile/Errors/HttpMessageInfo.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,28 @@ public class HttpMessageInfo
2626
{
2727
public HttpMessageInfo(CloudHttpErrorInfo message)
2828
{
29-
foreach (var header in message.Headers)
29+
if (message != null)
3030
{
31-
Headers[header.Key] = header.Value;
32-
}
31+
foreach (var header in message.Headers)
32+
{
33+
Headers[header.Key] = header.Value;
34+
}
3335

34-
Body = message.Content;
36+
Body = message.Content;
37+
}
3538
}
3639

3740
public HttpMessageInfo(HttpMessageWrapper message)
3841
{
39-
foreach (var header in message.Headers)
42+
if (message != null)
4043
{
41-
Headers[header.Key] = header.Value;
42-
}
44+
foreach (var header in message.Headers)
45+
{
46+
Headers[header.Key] = header.Value;
47+
}
4348

44-
Body = message.Content;
49+
Body = message.Content;
50+
}
4551
}
4652
public IDictionary<string, IEnumerable<string>> Headers { get; } = new Dictionary<string, IEnumerable<string>>(StringComparer.OrdinalIgnoreCase);
4753

src/ResourceManager/Profile/Commands.Profile/Errors/ResolveError.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,16 @@ private IEnumerable<ErrorRecord> GetErrorVariable()
9393

9494
private void HandleError(ErrorRecord record)
9595
{
96-
if (record.Exception != null)
96+
if (record != null)
9797
{
98-
HandleException(record.Exception, record);
99-
}
100-
else
101-
{
102-
WriteObject(new AzureErrorRecord(record));
98+
if (record.Exception != null)
99+
{
100+
HandleException(record.Exception, record);
101+
}
102+
else
103+
{
104+
WriteObject(new AzureErrorRecord(record));
105+
}
103106
}
104107
}
105108

@@ -110,7 +113,7 @@ private void HandleException(Exception exception, ErrorRecord record, bool inner
110113
var restException = exception as Microsoft.Rest.Azure.CloudException;
111114
if (aggregate != null)
112115
{
113-
foreach (var innerException in aggregate.InnerExceptions)
116+
foreach (var innerException in aggregate.InnerExceptions.Where(e => e!=null))
114117
{
115118
HandleException(innerException, record, true);
116119
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net452" />
34
</packages>

0 commit comments

Comments
 (0)