Skip to content

Commit 08581f7

Browse files
authored
Log troubleshooting headers for AzurePipelinesCredential (Azure#46376)
1 parent c5bc53a commit 08581f7

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

sdk/identity/Azure.Identity.Broker/tests/PopTestClient.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public PopTestClient(TokenCredential credential, ClientOptions options = null)
2020
var pipelineOptions = new HttpPipelineOptions(options);
2121
pipelineOptions.PerRetryPolicies.Add(new PopTokenAuthenticationPolicy(credential, "https://graph.microsoft.com/.default"));
2222
_pipeline = HttpPipelineBuilder.Build(
23-
pipelineOptions,
24-
new HttpPipelineTransportOptions { ServerCertificateCustomValidationCallback = (_) => true });
23+
pipelineOptions);
2524
}
2625

2726
[ForwardsClientCalls(true)]

sdk/identity/Azure.Identity/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
### Other Changes
1515

16+
- Improved error logging for `AzurePipelinesCredential`.
17+
1618
## 1.13.0-beta.2 (2024-09-17)
1719

1820
### Features Added

sdk/identity/Azure.Identity/src/Credentials/AzurePipelinesCredential.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Azure.Identity
1616
/// </summary>
1717
public class AzurePipelinesCredential : TokenCredential
1818
{
19-
private const string Troubleshooting = "See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/azurepipelinescredential/troubleshoot";
19+
internal const string Troubleshooting = "See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/azurepipelinescredential/troubleshoot";
2020
internal readonly string[] AdditionallyAllowedTenantIds;
2121
internal string SystemAccessToken { get; }
2222
internal string TenantId { get; }
@@ -151,7 +151,10 @@ internal string GetOidcTokenResponse(HttpMessage message)
151151
string error = $"OIDC token not found in response. " + Troubleshooting;
152152
if (message.Response.Status != 200)
153153
{
154-
error = error + $"\n\nResponse= {message.Response.Content}";
154+
var is_x_vss_e2eidValueFound = message.Response.Headers.TryGetValue("x-vss-e2eid", out var x_vss_e2eidValue);
155+
var is_x_msedge_refValueFound = message.Response.Headers.TryGetValue("x-msedge-ref", out var x_msedge_refValue);
156+
157+
error = error + $"\n\nResponse= {message.Response.Content}\n\nx-vss-e2eid= {(is_x_vss_e2eidValueFound ? x_vss_e2eidValue : "Not Found")}\n\nx-msedge-ref= {(is_x_msedge_refValueFound ? x_msedge_refValue : "Not Found")}";
155158
}
156159
throw new AuthenticationFailedException(error);
157160
}

sdk/identity/Azure.Identity/tests/AzurePipelinesCredentialTests.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ public void AzurePipelineCredentialReturnsErrorInformation()
114114
var mockTransport = new MockTransport(req => new MockResponse(200).WithContent(
115115
$"{{\"token_type\": \"Bearer\",\"expires_in\": 9999,\"ext_expires_in\": 9999,\"access_token\": \"mytoken\" }}"));
116116

117-
var options = new AzurePipelinesCredentialOptions { Transport = mockTransport };
117+
var options = new AzurePipelinesCredentialOptions { Transport = mockTransport, OidcRequestUri = "https://mockCollectionUri" };
118118
var cred = new AzurePipelinesCredential(tenantId, clientId, serviceConnectionId, systemAccessToken, options);
119119

120-
Assert.ThrowsAsync<AuthenticationFailedException>(async () => await cred.GetTokenAsync(new TokenRequestContext(new[] { "scope" }), CancellationToken.None));
120+
var ex = Assert.ThrowsAsync<AuthenticationFailedException>(async () => await cred.GetTokenAsync(new TokenRequestContext(new[] { "scope" }), CancellationToken.None));
121+
Assert.That(ex.Message, Does.Contain(AzurePipelinesCredential.Troubleshooting));
121122
}
122123
}
123124

@@ -140,7 +141,10 @@ public void AzurePipelineCredentialReturnsNonJsonErrorInformation(int responseSt
140141
{
141142
if (req.Uri.Host == "mockcollectionuri")
142143
{
143-
return new MockResponse(responseStatusCode).WithContent($"< not json, but an error >");
144+
return new MockResponse(responseStatusCode)
145+
.WithContent($"< not json, but an error >")
146+
.WithHeader("x-vss-e2eid", "myE2EId")
147+
.WithHeader("x-msedge-ref", "myRef");
144148
}
145149
return new MockResponse(200).WithContent(
146150
$"{{\"token_type\": \"Bearer\",\"expires_in\": 9999,\"ext_expires_in\": 9999,\"access_token\": \"mytoken\" }}");
@@ -158,6 +162,9 @@ public void AzurePipelineCredentialReturnsNonJsonErrorInformation(int responseSt
158162
else
159163
{
160164
Assert.That(ex.Message, Does.Contain("< not json, but an error >"));
165+
Assert.That(ex.Message, Does.Contain(AzurePipelinesCredential.Troubleshooting));
166+
Assert.That(ex.Message, Does.Contain("myE2EId"));
167+
Assert.That(ex.Message, Does.Contain("myRef"));
161168
}
162169
}
163170
}

0 commit comments

Comments
 (0)