refactor: move scrubber into its own package (#393)

Also part of https://github.com/ooni/probe/issues/1687
This commit is contained in:
Simone Basso 2021-06-22 14:08:29 +02:00 committed by GitHub
parent 760ac905d6
commit 75ae99e9d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 8 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/model" "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/engine/netx/errorx"
"github.com/ooni/probe-cli/v3/internal/runtimex" "github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/scrubber"
) )
const ( const (
@ -282,7 +283,7 @@ func maybeSanitize(input TargetResults, kt keytarget) TargetResults {
// Implementation note: here we are using a strict scrubbing policy where // Implementation note: here we are using a strict scrubbing policy where
// we remove all IP _endpoints_, mainly for convenience, because we already // we remove all IP _endpoints_, mainly for convenience, because we already
// have a well tested implementation that does that. // have a well tested implementation that does that.
data = []byte(errorx.Scrub(string(data))) data = []byte(scrubber.Scrub(string(data)))
var out TargetResults var out TargetResults
err = json.Unmarshal(data, &out) err = json.Unmarshal(data, &out)
runtimex.PanicOnError(err, "json.Unmarshal should not fail here") runtimex.PanicOnError(err, "json.Unmarshal should not fail here")
@ -332,7 +333,7 @@ type scrubbingLogger struct {
} }
func (sl scrubbingLogger) Debug(message string) { 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{}) { 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) { 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{}) { 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) { 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{}) { func (sl scrubbingLogger) Warnf(format string, v ...interface{}) {

View File

@ -8,6 +8,8 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"strings" "strings"
"github.com/ooni/probe-cli/v3/internal/scrubber"
) )
const ( const (
@ -287,7 +289,7 @@ func toFailureString(err error) string {
return FailureGenericTimeoutError return FailureGenericTimeoutError
} }
formatted := fmt.Sprintf("unknown_failure: %s", s) 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 { func toOperationString(err error, operation string) string {

View File

@ -1,4 +1,4 @@
package errorx package scrubber
import "regexp" import "regexp"
@ -64,7 +64,7 @@ func scrub(b []byte) []byte {
} }
// Scrub sanitizes a string containing an error such that // 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 { func Scrub(s string) string {
return string(scrub([]byte(s))) return string(scrub([]byte(s)))
} }

View File

@ -1,4 +1,4 @@
package errorx package scrubber
import ( import (
"testing" "testing"