Skip to content

Commit 801ab5c

Browse files
author
Douglas Camata
authored
Redact URLs when recording target metadata (#47)
* Redact URLs when recording target metadata * Make the linter happy
1 parent c767734 commit 801ab5c

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

internal/integration/rules_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package integration
55

66
import (
77
"fmt"
8+
"net/url"
89
"strings"
910
"testing"
1011

@@ -360,9 +361,11 @@ some_undecorated_stuff{addr="ohai-playground-redis-slave:6379",alias="ohai-playg
360361
}
361362

362363
func TestDecorate(t *testing.T) {
364+
targetURL, _ := url.Parse("https://user:[email protected]")
363365
se := []TargetMetrics{{
364366
Target: endpoints.Target{
365367
Name: "a_simple_target",
368+
URL: *targetURL,
366369
Object: endpoints.Object{
367370
Labels: labels.Set{
368371
"hello": "friend",
@@ -378,8 +381,8 @@ func TestDecorate(t *testing.T) {
378381

379382
Decorate(&se[0], []DecorateRule{})
380383

381-
assert.Equal(t, se[0].Metrics[0].attributes, labels.Set{"hello": "friend", "bye": "boy", "md1": "v1", "md2": "v2", "attr1": "val1"})
382-
assert.Equal(t, se[0].Metrics[1].attributes, labels.Set{"hello": "friend", "bye": "boy", "md3": "v3", "md4": "v4", "attr2": "val2"})
384+
assert.Equal(t, se[0].Metrics[0].attributes, labels.Set{"hello": "friend", "bye": "boy", "md1": "v1", "md2": "v2", "attr1": "val1", "scrapedTargetURL": "https://user:[email protected]"})
385+
assert.Equal(t, se[0].Metrics[1].attributes, labels.Set{"hello": "friend", "bye": "boy", "md3": "v3", "md4": "v4", "attr2": "val2", "scrapedTargetURL": "https://user:[email protected]"})
383386

384387
}
385388

internal/pkg/endpoints/endpoints.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ type Target struct {
4040
func (t *Target) Metadata() labels.Set {
4141
if t.metadata == nil {
4242
metadata := labels.Set{}
43-
if t.URL.String() != "" {
44-
metadata["scrapedTargetURL"] = t.URL.String()
43+
if targetURL := redactedURLString(&t.URL); targetURL != "" {
44+
metadata["scrapedTargetURL"] = targetURL
4545
}
4646
if t.Object.Name != "" {
4747
metadata["scrapedTargetName"] = t.Object.Name
@@ -54,6 +54,20 @@ func (t *Target) Metadata() labels.Set {
5454
return t.metadata
5555
}
5656

57+
// redactedURLString returns the string representation of the URL object while redacting the password that could be present.
58+
// This code is copied from this commit https://github.com/golang/go/commit/e3323f57df1f4a44093a2d25fee33513325cbb86.
59+
// The feature is supposed to be added to the net/url.URL type in Golang 1.15.
60+
func redactedURLString(u *url.URL) string {
61+
if u == nil {
62+
return ""
63+
}
64+
ru := *u
65+
if _, has := ru.User.Password(); has {
66+
ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
67+
}
68+
return ru.String()
69+
}
70+
5771
// New returns a Target from the discovered information
5872
func New(name string, addr url.URL, object Object) Target {
5973
return Target{

0 commit comments

Comments
 (0)