Skip to content

Commit b130b33

Browse files
committed
Redact URL in opentracing transport
This commit hopefully, finally, fixes the credentials leakage described in #1459.
1 parent 932871c commit b130b33

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

trace/opentracing/transport.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
4646
defer span.Finish()
4747

4848
ext.Component.Set(span, "github.com/olivere/elastic/v7")
49-
ext.HTTPUrl.Set(span, req.URL.String())
49+
ext.HTTPUrl.Set(span, req.URL.Redacted())
5050
ext.HTTPMethod.Set(span, req.Method)
5151
ext.PeerHostname.Set(span, req.URL.Hostname())
5252
ext.PeerPort.Set(span, atouint16(req.URL.Port()))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2012-present Oliver Eilhard. All rights reserved.
2+
// Use of this source code is governed by a MIT-license.
3+
// See http://olivere.mit-license.org/license.txt for details.
4+
5+
package opentracing
6+
7+
import (
8+
"context"
9+
"net/http"
10+
"strings"
11+
"testing"
12+
13+
"github.com/opentracing/opentracing-go"
14+
"github.com/opentracing/opentracing-go/mocktracer"
15+
16+
"github.com/olivere/elastic/v7"
17+
)
18+
19+
func TestTransportIntegration(t *testing.T) {
20+
// Mock tracer
21+
tracer := mocktracer.New()
22+
opentracing.InitGlobalTracer(tracer)
23+
24+
// Setup a simple transport
25+
tr := NewTransport()
26+
httpClient := &http.Client{
27+
Transport: tr,
28+
}
29+
30+
// Create a simple Ping request via Elastic
31+
client, err := elastic.NewClient(
32+
elastic.SetURL("http://127.0.0.1:9210"),
33+
elastic.SetHealthcheck(false),
34+
elastic.SetSniff(false),
35+
elastic.SetBasicAuth("elastic", "elastic"),
36+
elastic.SetHttpClient(httpClient),
37+
)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
_, err = client.Search("_all").Query(elastic.NewMatchAllQuery()).Do(context.Background())
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
46+
// Check the data written into tracer
47+
spans := tracer.FinishedSpans()
48+
if want, have := 1, len(spans); want != have {
49+
t.Fatalf("want %d finished spans, have %d", want, have)
50+
}
51+
span := spans[0]
52+
53+
if want, have := "PerformRequest", span.OperationName; want != have {
54+
t.Fatalf("want Span.OperationName=%q, have %q", want, have)
55+
}
56+
if want, have := "github.com/olivere/elastic/v7", span.Tag("component"); want != have {
57+
t.Fatalf("want component tag=%q, have %q", want, have)
58+
}
59+
httpURL, ok := span.Tag("http.url").(string)
60+
if !ok || httpURL == "" {
61+
t.Fatalf("want http.url tag=%q to be a non-empty string (found type %T)", "http.url", span.Tag("http.url"))
62+
}
63+
if want, have := "http://127.0.0.1:9210/_all/_search", httpURL; want != have {
64+
t.Fatalf("want http.url tag=%q, have %q", want, have)
65+
}
66+
if strings.Contains(httpURL, "elastic") {
67+
t.Fatalf("want http.url tag %q to not contain username and/or password: %s", "URL", span.Tag("http.url"))
68+
}
69+
if want, have := "POST", span.Tag("http.method"); want != have {
70+
t.Fatalf("want http.method tag=%q, have %q", want, have)
71+
}
72+
if want, have := uint16(http.StatusOK), span.Tag("http.status_code"); want != have {
73+
t.Fatalf("want http.status_code tag=%v (%T), have %v (%T)", want, want, have, have)
74+
}
75+
}

trace/opentracing/transport_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func TestTransport(t *testing.T) {
106106
if want, have := ts.URL+"/", httpURL; want != have {
107107
t.Fatalf("want http.url tag=%q, have %q", want, have)
108108
}
109+
t.Logf("http.url = %q", httpURL)
109110
if strings.Contains(httpURL, "alice") || strings.Contains(httpURL, "password") {
110111
t.Fatalf("want http.url tag %q to not contain username and/or password: %s", "URL", span.Tag("http.url"))
111112
}

0 commit comments

Comments
 (0)