From 75ae99e9d45deec60b7168c79ec221b3d41c4173 Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Tue, 22 Jun 2021 14:08:29 +0200 Subject: [PATCH] refactor: move scrubber into its own package (#393) Also part of https://github.com/ooni/probe/issues/1687 --- internal/engine/experiment/tor/tor.go | 9 +++++---- internal/engine/netx/errorx/errorx.go | 4 +++- .../netx/errorx/sanitizer.go => scrubber/scrubber.go} | 4 ++-- .../sanitizer_test.go => scrubber/scrubber_test.go} | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) rename internal/{engine/netx/errorx/sanitizer.go => scrubber/scrubber.go} (98%) rename internal/{engine/netx/errorx/sanitizer_test.go => scrubber/scrubber_test.go} (99%) diff --git a/internal/engine/experiment/tor/tor.go b/internal/engine/experiment/tor/tor.go index cbe9f32..3d3dffa 100644 --- a/internal/engine/experiment/tor/tor.go +++ b/internal/engine/experiment/tor/tor.go @@ -20,6 +20,7 @@ import ( "github.com/ooni/probe-cli/v3/internal/engine/model" "github.com/ooni/probe-cli/v3/internal/engine/netx/errorx" "github.com/ooni/probe-cli/v3/internal/runtimex" + "github.com/ooni/probe-cli/v3/internal/scrubber" ) const ( @@ -282,7 +283,7 @@ func maybeSanitize(input TargetResults, kt keytarget) TargetResults { // Implementation note: here we are using a strict scrubbing policy where // we remove all IP _endpoints_, mainly for convenience, because we already // have a well tested implementation that does that. - data = []byte(errorx.Scrub(string(data))) + data = []byte(scrubber.Scrub(string(data))) var out TargetResults err = json.Unmarshal(data, &out) runtimex.PanicOnError(err, "json.Unmarshal should not fail here") @@ -332,7 +333,7 @@ type scrubbingLogger struct { } func (sl scrubbingLogger) Debug(message string) { - sl.Logger.Debug(errorx.Scrub(message)) + sl.Logger.Debug(scrubber.Scrub(message)) } func (sl scrubbingLogger) Debugf(format string, v ...interface{}) { @@ -340,7 +341,7 @@ func (sl scrubbingLogger) Debugf(format string, v ...interface{}) { } func (sl scrubbingLogger) Info(message string) { - sl.Logger.Info(errorx.Scrub(message)) + sl.Logger.Info(scrubber.Scrub(message)) } func (sl scrubbingLogger) Infof(format string, v ...interface{}) { @@ -348,7 +349,7 @@ func (sl scrubbingLogger) Infof(format string, v ...interface{}) { } func (sl scrubbingLogger) Warn(message string) { - sl.Logger.Warn(errorx.Scrub(message)) + sl.Logger.Warn(scrubber.Scrub(message)) } func (sl scrubbingLogger) Warnf(format string, v ...interface{}) { diff --git a/internal/engine/netx/errorx/errorx.go b/internal/engine/netx/errorx/errorx.go index fc72510..1d968e9 100644 --- a/internal/engine/netx/errorx/errorx.go +++ b/internal/engine/netx/errorx/errorx.go @@ -8,6 +8,8 @@ import ( "fmt" "regexp" "strings" + + "github.com/ooni/probe-cli/v3/internal/scrubber" ) const ( @@ -287,7 +289,7 @@ func toFailureString(err error) string { return FailureGenericTimeoutError } formatted := fmt.Sprintf("unknown_failure: %s", s) - return Scrub(formatted) // scrub IP addresses in the error + return scrubber.Scrub(formatted) // scrub IP addresses in the error } func toOperationString(err error, operation string) string { diff --git a/internal/engine/netx/errorx/sanitizer.go b/internal/scrubber/scrubber.go similarity index 98% rename from internal/engine/netx/errorx/sanitizer.go rename to internal/scrubber/scrubber.go index d181f31..341b829 100644 --- a/internal/engine/netx/errorx/sanitizer.go +++ b/internal/scrubber/scrubber.go @@ -1,4 +1,4 @@ -package errorx +package scrubber import "regexp" @@ -64,7 +64,7 @@ func scrub(b []byte) []byte { } // Scrub sanitizes a string containing an error such that -// any occurrence of IP endpoints is scrubbed +// any occurrence of IP endpoints is scrubbed. func Scrub(s string) string { return string(scrub([]byte(s))) } diff --git a/internal/engine/netx/errorx/sanitizer_test.go b/internal/scrubber/scrubber_test.go similarity index 99% rename from internal/engine/netx/errorx/sanitizer_test.go rename to internal/scrubber/scrubber_test.go index 0b7692f..4c3c5c0 100644 --- a/internal/engine/netx/errorx/sanitizer_test.go +++ b/internal/scrubber/scrubber_test.go @@ -1,4 +1,4 @@ -package errorx +package scrubber import ( "testing"