From 4ea0873d7a88a791487b299064d73f2e8424f372 Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Wed, 24 Jan 2024 09:55:21 +0300 Subject: [PATCH] Copied redact function --- cmd/cmd.go | 8 ++++---- cmd/cmd_internal_test.go | 2 +- testutil/promrated/promrated.go | 14 ++++++++++++-- testutil/promrated/promrated_internal_test.go | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 testutil/promrated/promrated_internal_test.go diff --git a/cmd/cmd.go b/cmd/cmd.go index c292366fe..ad746787e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -166,12 +166,12 @@ func printFlags(ctx context.Context, flags *pflag.FlagSet) { func flagsToLogFields(flags *pflag.FlagSet) []z.Field { var fields []z.Field flags.VisitAll(func(flag *pflag.Flag) { - val := Redact(flag.Name, flag.Value.String()) + val := redact(flag.Name, flag.Value.String()) if sliceVal, ok := flag.Value.(pflag.SliceValue); ok { var vals []string for _, s := range sliceVal.GetSlice() { - vals = append(vals, Redact(flag.Name, s)) + vals = append(vals, redact(flag.Name, s)) } val = "[" + strings.Join(vals, ",") + "]" } @@ -182,9 +182,9 @@ func flagsToLogFields(flags *pflag.FlagSet) []z.Field { return fields } -// Redact returns a redacted version of the given flag value. It currently supports redacting +// redact returns a redacted version of the given flag value. It currently supports redacting // passwords in valid URLs provided in ".*address.*" flags and redacting auth tokens. -func Redact(flag, val string) string { +func redact(flag, val string) string { if strings.Contains(flag, "auth-token") { return "xxxxx" } diff --git a/cmd/cmd_internal_test.go b/cmd/cmd_internal_test.go index e715d483d..eb77576d5 100644 --- a/cmd/cmd_internal_test.go +++ b/cmd/cmd_internal_test.go @@ -243,7 +243,7 @@ func TestRedact(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := Redact(tt.flag, tt.value) + got := redact(tt.flag, tt.value) require.Equal(t, tt.expected, got) }) } diff --git a/testutil/promrated/promrated.go b/testutil/promrated/promrated.go index be2d1ec22..157918384 100644 --- a/testutil/promrated/promrated.go +++ b/testutil/promrated/promrated.go @@ -6,6 +6,7 @@ package promrated import ( "context" + "net/url" "time" "github.com/prometheus/client_golang/prometheus" @@ -13,7 +14,6 @@ import ( "github.com/obolnetwork/charon/app/log" "github.com/obolnetwork/charon/app/promauto" "github.com/obolnetwork/charon/app/z" - "github.com/obolnetwork/charon/cmd" ) type Config struct { @@ -28,7 +28,7 @@ type Config struct { // Run blocks running the promrated program until the context is canceled or a fatal error occurs. func Run(ctx context.Context, config Config) error { log.Info(ctx, "Promrated started", - z.Str("rated_endpoint", cmd.Redact("address", config.RatedEndpoint)), + z.Str("rated_endpoint", redactURL(config.RatedEndpoint)), z.Str("monitoring_addr", config.MonitoringAddr), ) @@ -131,3 +131,13 @@ func contains(arr []string, s string) bool { return result } + +// redactURL returns a redacted version of the given URL. +func redactURL(val string) string { + u, err := url.Parse(val) + if err != nil { + return val + } + + return u.Redacted() +} diff --git a/testutil/promrated/promrated_internal_test.go b/testutil/promrated/promrated_internal_test.go new file mode 100644 index 000000000..c324760bb --- /dev/null +++ b/testutil/promrated/promrated_internal_test.go @@ -0,0 +1,17 @@ +// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 + +package promrated + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestRedactURL(t *testing.T) { + url := "https://user:password@domain.com" + + redacted := redactURL(url) + + require.Equal(t, "https://user:xxxxx@domain.com", redacted) +}