Repository: prometheus/common
Pull request #328 (2021-09-27 15:09:54)

Do not marshal secrets in URL's
Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>

SuperQ: I think we could update to require 1.15.
roidelapluie: They is a question for client_golang.

Le mar. 28 sept. 2021 à 07:38, Ben Kochie ***@***.***> a
écrit :

> I think we could update to require 1.15.
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/prometheus/common/pull/328#issuecomment-928858613>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AACHHJWKFHOXITCWVI7CDCDUEFIHDANCNFSM5E2ZPVPA>
> .
>


Patch:
diff --git a/config/http_config.go b/config/http_config.go
index 6b3609a..f47892d 100644
--- a/config/http_config.go
+++ b/config/http_config.go
@@ -110,11 +110,25 @@ func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
 // MarshalYAML implements the yaml.Marshaler interface for URLs.
 func (u URL) MarshalYAML() (interface{}, error) {
 	if u.URL != nil {
-		return u.String(), nil
+		return u.Redacted(), nil
 	}
 	return nil, nil
 }
 
+// Redacted returns the URL but replaces any password with "xxxxx".
+func (u URL) Redacted() string {
+	if u.URL == nil {
+		return ""
+	}
+
+	ru := *u.URL
+	if _, ok := ru.User.Password(); ok {
+		// We can not use secretToken because it would be escaped.
+		ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
+	}
+	return ru.String()
+}
+
 // UnmarshalJSON implements the json.Marshaler interface for URL.
 func (u *URL) UnmarshalJSON(data []byte) error {
 	var s string
@@ -380,18 +394,17 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
 			ExpectContinueTimeout: 1 * time.Second,
 			DialContext:           dialContext,
 		}
-		if opts.http2Enabled && os.Getenv("PROMETHEUS_COMMON_DISABLE_HTTP2") == "" {
-			// HTTP/2 support is golang had many problematic cornercases where
+		if opts.http2Enabled || os.Getenv("PROMETHEUS_COMMON_ENABLE_HTTP2") != "" {
+			// HTTP/2 support is golang has many problematic cornercases where
 			// dead connections would be kept and used in connection pools.
 			// https://github.com/golang/go/issues/32388
 			// https://github.com/golang/go/issues/39337
 			// https://github.com/golang/go/issues/39750
 
-			// Do not enable HTTP2 if the environment variable
-			// PROMETHEUS_COMMON_DISABLE_HTTP2 is set to a non-empty value.
-			// This allows users to easily disable HTTP2 in case they run into
-			// issues again, but will be removed once we are confident that
-			// things work as expected.
+			// Enable HTTP2 if the environment variable
+			// PROMETHEUS_COMMON_ENABLE_HTTP2 is set.
+			// This is a temporary workaround so that users can safely test this
+			// and validate that HTTP2 can be enabled Prometheus-Wide again.
 
 			http2t, err := http2.ConfigureTransports(rt.(*http.Transport))
 			if err != nil {
diff --git a/promlog/log.go b/promlog/log.go
index b7c7608..adb0599 100644
--- a/promlog/log.go
+++ b/promlog/log.go
@@ -118,11 +118,10 @@ func New(config *Config) log.Logger {
 		l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
 	}
 
-	l = log.With(l, "ts", timestampFormat, "caller", log.DefaultCaller)
-
 	if config.Level != nil {
 		l = level.NewFilter(l, config.Level.o)
 	}
+	l = log.With(l, "ts", timestampFormat, "caller", log.DefaultCaller)
 	return l
 }
 


Test patch:
diff --git a/config/http_config_test.go b/config/http_config_test.go
index 5ec3229..689bbde 100644
--- a/config/http_config_test.go
+++ b/config/http_config_test.go
@@ -1418,3 +1418,19 @@ func TestUnmarshalURL(t *testing.T) {
 		t.Fatalf("URL not properly unmarshaled in YAML, got '%s'", u.String())
 	}
 }
+
+func TestMarshalURLWithSecret(t *testing.T) {
+	var u URL
+	err := yaml.Unmarshal([]byte("http://foo:bar@example.com"), &u)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	b, err := yaml.Marshal(u)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if strings.TrimSpace(string(b)) != "http://foo:xxxxx@example.com" {
+		t.Fatalf("URL not properly marshaled in YAML, got '%s'", string(b))
+	}
+}