Skip to content

Fix error when authenticating with ADAL #4390

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 3 commits into from
Aug 3, 2017
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
4 changes: 4 additions & 0 deletions setup/azurecmdfiles.wxi
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,9 @@
<Component Id="cmp1460DC2324E946B9F5F76360ED57C0FE" Guid="*">
<File Id="fil63597DE31C6B3211017C4B4EAD93F683" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.Profile\Microsoft.IdentityModel.Clients.ActiveDirectory.dll" />
</Component>
<Component Id="cmp95697C7A2E29A43F7195D799D68E138B" Guid="*">
<File Id="fil53FA53B6DEFBC23592C3C2A82969E769" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.Profile\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll" />
</Component>
<Component Id="cmp3BEF3C9459257F053CAAC956B7CFA10D" Guid="*">
<File Id="fil830F7BE66C1E1324FCE632956432C2C7" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.Profile\Microsoft.Rest.ClientRuntime.Azure.Authentication.dll" />
</Component>
Expand Down Expand Up @@ -6063,6 +6066,7 @@
<ComponentRef Id="cmpE2AF801EDF4F7F1C347F9D7052313074" />
<ComponentRef Id="cmp6004E0F8B6FA21BF5CAE3CFCA2CA9E67" />
<ComponentRef Id="cmp1460DC2324E946B9F5F76360ED57C0FE" />
<ComponentRef Id="cmp95697C7A2E29A43F7195D799D68E138B" />
<ComponentRef Id="cmp3BEF3C9459257F053CAAC956B7CFA10D" />
<ComponentRef Id="cmpB7D392D58F12D947C1D915326ABA98EA" />
<ComponentRef Id="cmp61D87C757ACDE206CC79A5B19CE403B2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,19 @@ protected override void Dispose(bool disposing)
protected override void BeginProcessing()
{
AzureSession.Instance.ClientFactory.RemoveHandler(typeof(RPRegistrationDelegatingHandler));
AzureSession.Instance.ClientFactory.AddHandler(new RPRegistrationDelegatingHandler(
() => new ResourceManagementClient(
DefaultContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(DefaultContext, AzureEnvironment.Endpoint.ResourceManager)),
s => DebugMessages.Enqueue(s)));
if (DefaultContext != null && DefaultContext.Subscription != null)
{
AzureSession.Instance.ClientFactory.AddHandler(new RPRegistrationDelegatingHandler(
() =>
{
var client = new ResourceManagementClient(
DefaultContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(DefaultContext, AzureEnvironment.Endpoint.ResourceManager));
client.SubscriptionId = DefaultContext.Subscription.Id;
return client;
},
s => DebugMessages.Enqueue(s)));
}

base.BeginProcessing();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,46 @@ public void HandlesExceptionError()
Assert.Contains("Autorest message", autorestResult.ServerMessage);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void HandlesNullValuesInArmExceptions()
{
var runtime = new MockCommandRuntime();
var hyakException = new TestHyakException(null, null, null);

var autorestException = new Microsoft.Rest.Azure.CloudException();

var cmdlet = new ResolveError
{
Error = new[]
{
new ErrorRecord(new Exception(), null, ErrorCategory.AuthenticationError, null),
new ErrorRecord(hyakException, null, ErrorCategory.ConnectionError, null),
new ErrorRecord(autorestException , null, ErrorCategory.InvalidOperation, null),
},
CommandRuntime = runtime
};

cmdlet.ExecuteCmdlet();
Assert.NotNull(runtime.OutputPipeline);
Assert.Equal(3, runtime.OutputPipeline.Count);
var errorResult = runtime.OutputPipeline[0] as AzureExceptionRecord;
Assert.NotNull(errorResult);
Assert.Equal(ErrorCategory.AuthenticationError, errorResult.ErrorCategory.Category);
Assert.NotNull(errorResult.Exception);
Assert.Equal(errorResult.Exception.GetType(), typeof(Exception));
var hyakResult = runtime.OutputPipeline[1] as AzureRestExceptionRecord;
Assert.NotNull(hyakResult);
Assert.Equal(ErrorCategory.ConnectionError, hyakResult.ErrorCategory.Category);
Assert.NotNull(errorResult.Exception);
Assert.Equal(hyakResult.Exception.GetType(), typeof(TestHyakException));
var autorestResult = runtime.OutputPipeline[2] as AzureRestExceptionRecord;
Assert.NotNull(autorestResult);
Assert.Equal(ErrorCategory.InvalidOperation, autorestResult.ErrorCategory.Category);
Assert.NotNull(autorestResult.Exception);
Assert.Equal(autorestResult.Exception.GetType(), typeof(Microsoft.Rest.Azure.CloudException));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void LastParameterFindsLastError()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
<ItemGroup>
<Reference Include="Microsoft.Build" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.28.3.860, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.28.3\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms, Version=2.28.3.860, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.28.3\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Data.Services.Client" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ public class AzureErrorRecord
{
public AzureErrorRecord(ErrorRecord record)
{
InvocationInfo = record.InvocationInfo;
ScriptStackTrace = record.ScriptStackTrace;
ErrorCategory = record.CategoryInfo;
ErrorDetails = record.ErrorDetails;
if (record != null)
{
InvocationInfo = record.InvocationInfo;
ScriptStackTrace = record.ScriptStackTrace;
ErrorCategory = record.CategoryInfo;
ErrorDetails = record.ErrorDetails;
}
}

public ErrorDetails ErrorDetails { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ public class AzureExceptionRecord : AzureErrorRecord
{
public AzureExceptionRecord(Exception exception, ErrorRecord record, bool inner = false) : base(record)
{
Message = exception.Message;
HelpLink = exception.HelpLink;
StackTrace = exception.StackTrace;
if (exception != null)
{
Message = exception.Message;
HelpLink = exception.HelpLink;
StackTrace = exception.StackTrace;
}

Exception = exception;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,46 @@ public class AzureRestExceptionRecord : AzureExceptionRecord
{
public AzureRestExceptionRecord(Hyak.Common.CloudException exception, ErrorRecord record, bool inner = false) : base(exception, record, inner)
{
ServerMessage = string.Format($"{exception.Error.Code}: {exception.Error.Message} ({exception.Error.OriginalMessage})");
ServerResponse = new HttpResponseInfo(exception.Response);
RequestMessage = new HttpRequestInfo(exception.Request);
if (exception != null)
{
if (exception.Error != null)
{
ServerMessage = string.Format($"{exception.Error.Code}: {exception.Error.Message} ({exception.Error.OriginalMessage})");
}

if (exception.Response != null)
{
ServerResponse = new HttpResponseInfo(exception.Response);
}

if (exception.Request != null)
{
RequestMessage = new HttpRequestInfo(exception.Request);
}
}
}

public AzureRestExceptionRecord(Microsoft.Rest.Azure.CloudException exception, ErrorRecord record, bool inner = false) : base(exception, record, inner)
{
ServerMessage = string.Format($"{exception.Body.Code}: {exception.Body.Message} ({exception.Body.Details})");
ServerResponse = new HttpResponseInfo(exception.Response);
RequestMessage = new HttpRequestInfo(exception.Request);
RequestId = exception.RequestId;
if (exception != null)
{
if (exception.Body != null)
{
ServerMessage = string.Format($"{exception.Body.Code}: {exception.Body.Message} ({exception.Body.Details})");
}

if (exception.Response != null)
{
ServerResponse = new HttpResponseInfo(exception.Response);
}

if (exception.Request != null)
{
RequestMessage = new HttpRequestInfo(exception.Request);
}

RequestId = exception.RequestId;
}
}
public string ServerMessage { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,28 @@ public class HttpMessageInfo
{
public HttpMessageInfo(CloudHttpErrorInfo message)
{
foreach (var header in message.Headers)
if (message != null)
{
Headers[header.Key] = header.Value;
}
foreach (var header in message.Headers)
{
Headers[header.Key] = header.Value;
}

Body = message.Content;
Body = message.Content;
}
}

public HttpMessageInfo(HttpMessageWrapper message)
{
foreach (var header in message.Headers)
if (message != null)
{
Headers[header.Key] = header.Value;
}
foreach (var header in message.Headers)
{
Headers[header.Key] = header.Value;
}

Body = message.Content;
Body = message.Content;
}
}
public IDictionary<string, IEnumerable<string>> Headers { get; } = new Dictionary<string, IEnumerable<string>>(StringComparer.OrdinalIgnoreCase);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,16 @@ private IEnumerable<ErrorRecord> GetErrorVariable()

private void HandleError(ErrorRecord record)
{
if (record.Exception != null)
if (record != null)
{
HandleException(record.Exception, record);
}
else
{
WriteObject(new AzureErrorRecord(record));
if (record.Exception != null)
{
HandleException(record.Exception, record);
}
else
{
WriteObject(new AzureErrorRecord(record));
}
}
}

Expand All @@ -110,7 +113,7 @@ private void HandleException(Exception exception, ErrorRecord record, bool inner
var restException = exception as Microsoft.Rest.Azure.CloudException;
if (aggregate != null)
{
foreach (var innerException in aggregate.InnerExceptions)
foreach (var innerException in aggregate.InnerExceptions.Where(e => e!=null))
{
HandleException(innerException, record, true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net452" />
</packages>