Skip to content

Commit

Permalink
Copied redact function
Browse files Browse the repository at this point in the history
  • Loading branch information
pinebit committed Jan 24, 2024
1 parent 4604842 commit 4ea0873
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ",") + "]"
}
Expand All @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
14 changes: 12 additions & 2 deletions testutil/promrated/promrated.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ package promrated

import (
"context"
"net/url"
"time"

"github.com/prometheus/client_golang/prometheus"

"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 {
Expand All @@ -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)),

Check warning on line 31 in testutil/promrated/promrated.go

View check run for this annotation

Codecov / codecov/patch

testutil/promrated/promrated.go#L31

Added line #L31 was not covered by tests
z.Str("monitoring_addr", config.MonitoringAddr),
)

Expand Down Expand Up @@ -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
}

Check warning on line 140 in testutil/promrated/promrated.go

View check run for this annotation

Codecov / codecov/patch

testutil/promrated/promrated.go#L139-L140

Added lines #L139 - L140 were not covered by tests

return u.Redacted()
}
17 changes: 17 additions & 0 deletions testutil/promrated/promrated_internal_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4ea0873

Please sign in to comment.