Skip to content

Commit df2abba

Browse files
committed
Manual backport of 7fc9ca0
1 parent c3d8d91 commit df2abba

File tree

3 files changed

+107
-53
lines changed

3 files changed

+107
-53
lines changed

src/Elasticsearch.Net/Responses/ElasticsearchResponse.cs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,6 @@
55

66
namespace Elasticsearch.Net
77
{
8-
public static class ResponseStatics
9-
{
10-
private static readonly string RequestAlreadyCaptured =
11-
"<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>";
12-
13-
private static readonly string ResponseAlreadyCaptured =
14-
"<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>";
15-
16-
public static string DebugInformationBuilder(IApiCallDetails r, StringBuilder sb)
17-
{
18-
if (r.DeprecationWarnings.HasAny())
19-
{
20-
sb.AppendLine($"# Server indicated deprecations:");
21-
foreach (var deprecation in r.DeprecationWarnings)
22-
sb.AppendLine($"- {deprecation}");
23-
}
24-
sb.AppendLine($"# Audit trail of this API call:");
25-
var auditTrail = (r.AuditTrail ?? Enumerable.Empty<Audit>()).ToList();
26-
DebugAuditTrail(auditTrail, sb);
27-
if (r.ServerError != null) sb.AppendLine($"# ServerError: {r.ServerError}");
28-
if (r.OriginalException != null) sb.AppendLine($"# OriginalException: {r.OriginalException}");
29-
DebugAuditTrailExceptions(auditTrail, sb);
30-
31-
var response = r.ResponseBodyInBytes?.Utf8String() ?? ResponseAlreadyCaptured;
32-
var request = r.RequestBodyInBytes?.Utf8String() ?? RequestAlreadyCaptured;
33-
sb.AppendLine($"# Request:\r\n{request}");
34-
sb.AppendLine($"# Response:\r\n{response}");
35-
36-
return sb.ToString();
37-
}
38-
39-
public static void DebugAuditTrailExceptions(List<Audit> auditTrail, StringBuilder sb)
40-
{
41-
var auditExceptions = auditTrail.Select((audit, i) => new { audit, i }).Where(a => a.audit.Exception != null);
42-
foreach (var a in auditExceptions)
43-
sb.AppendLine($"# Audit exception in step {a.i + 1} {a.audit.Event.GetStringValue()}:\r\n{a.audit.Exception}");
44-
}
45-
46-
public static void DebugAuditTrail(List<Audit> auditTrail, StringBuilder sb)
47-
{
48-
if (auditTrail == null) return;
49-
50-
foreach (var a in auditTrail.Select((a, i) => new { a, i }))
51-
{
52-
var audit = a.a;
53-
sb.Append($" - [{a.i + 1}] {audit.Event.GetStringValue()}:");
54-
if (audit.Node?.Uri != null) sb.Append($" Node: {audit.Node.Uri}");
55-
if (audit.Exception != null) sb.Append($" Exception: {audit.Exception.GetType().Name}");
56-
sb.AppendLine($" Took: {(audit.Ended - audit.Started).ToString()}");
57-
}
58-
}
59-
}
60-
618
public class ElasticsearchResponse<T> : IApiCallDetails
629
{
6310
public ElasticsearchResponse(Exception e)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Elasticsearch.Net
7+
{
8+
public static class ResponseStatics
9+
{
10+
private static readonly string RequestAlreadyCaptured =
11+
"<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>";
12+
13+
private static readonly string ResponseAlreadyCaptured =
14+
"<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>";
15+
16+
public static string DebugInformationBuilder(IApiCallDetails r, StringBuilder sb)
17+
{
18+
if (r.DeprecationWarnings.HasAny())
19+
{
20+
sb.AppendLine($"# Server indicated deprecations:");
21+
foreach (var deprecation in r.DeprecationWarnings)
22+
sb.AppendLine($"- {deprecation}");
23+
}
24+
sb.AppendLine($"# Audit trail of this API call:");
25+
var auditTrail = (r.AuditTrail ?? Enumerable.Empty<Audit>()).ToList();
26+
DebugAuditTrail(auditTrail, sb);
27+
if (r.ServerError != null) sb.AppendLine($"# ServerError: {r.ServerError}");
28+
if (r.OriginalException != null) sb.AppendLine($"# OriginalException: {r.OriginalException}");
29+
DebugAuditTrailExceptions(auditTrail, sb);
30+
31+
var response = r.ResponseBodyInBytes?.Utf8String() ?? ResponseAlreadyCaptured;
32+
var request = r.RequestBodyInBytes?.Utf8String() ?? RequestAlreadyCaptured;
33+
sb.AppendLine($"# Request:\r\n{request}");
34+
sb.AppendLine($"# Response:\r\n{response}");
35+
36+
return sb.ToString();
37+
}
38+
39+
public static void DebugAuditTrailExceptions(List<Audit> auditTrail, StringBuilder sb)
40+
{
41+
var auditExceptions = auditTrail.Select((audit, i) => new { audit, i }).Where(a => a.audit.Exception != null);
42+
foreach (var a in auditExceptions)
43+
sb.AppendLine($"# Audit exception in step {a.i + 1} {a.audit.Event.GetStringValue()}:\r\n{a.audit.Exception}");
44+
}
45+
46+
public static void DebugAuditTrail(List<Audit> auditTrail, StringBuilder sb)
47+
{
48+
if (auditTrail == null) return;
49+
50+
foreach (var a in auditTrail.Select((a, i) => new { a, i }))
51+
{
52+
var audit = a.a;
53+
sb.Append($" - [{a.i + 1}] {audit.Event.GetStringValue()}:");
54+
55+
AuditNodeUrl(sb, audit);
56+
57+
if (audit.Exception != null) sb.Append($" Exception: {audit.Exception.GetType().Name}");
58+
sb.AppendLine($" Took: {(audit.Ended - audit.Started).ToString()}");
59+
}
60+
}
61+
62+
private static void AuditNodeUrl(StringBuilder sb, Audit audit)
63+
{
64+
var uri = audit.Node?.Uri;
65+
if (uri == null) return;
66+
67+
if (!string.IsNullOrEmpty(uri.UserInfo))
68+
{
69+
var builder = new UriBuilder(uri);
70+
builder.Password = "redacted";
71+
uri = builder.Uri;
72+
}
73+
sb.Append($" Node: {uri}");
74+
}
75+
}
76+
}

src/Tests/Tests/ClientConcepts/Troubleshooting/DebugInformation.doc.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using FluentAssertions;
99
using Nest;
1010
using Tests.Core.Client;
11+
using Tests.Core.Client.Settings;
1112
using Tests.Core.ManagedElasticsearch.Clusters;
1213
using Tests.Domain;
1314
using Tests.Framework;
@@ -41,6 +42,36 @@ public void DefaultDebug()
4142

4243
response.DebugInformation.Should().Contain("Valid NEST response");
4344
}
45+
//hide
46+
[U] public void PasswordIsNotExposedInDebugInformation()
47+
{
48+
// hide
49+
var client = new ElasticClient(new AlwaysInMemoryConnectionSettings()
50+
.DefaultIndex("index")
51+
.BasicAuthentication("user1", "pass2")
52+
);
53+
var response = client.Search<Project>(s => s
54+
.Query(q => q
55+
.MatchAll()
56+
)
57+
);
58+
response.DebugInformation.Should().NotContain("pass2");
59+
}
60+
//hide
61+
[U] public void PasswordIsNotExposedInDebugInformationWhenPartOfUrl()
62+
{
63+
// hide
64+
var pool = new SingleNodeConnectionPool(new Uri("http://user1:pass2@localhost:9200"));
65+
var client = new ElasticClient(new ConnectionSettings(pool, new InMemoryConnection())
66+
.DefaultIndex("index")
67+
);
68+
var response = client.Search<Project>(s => s
69+
.Query(q => q
70+
.MatchAll()
71+
)
72+
);
73+
response.DebugInformation.Should().NotContain("pass2");
74+
}
4475
/**
4576
* This can be useful in tracking down numerous problems and can also be useful when filing an
4677
* {github}/issues[issue] on our github repository.

0 commit comments

Comments
 (0)