From d5a444c6cf320e2078ee34645287cab1f909efd7 Mon Sep 17 00:00:00 2001 From: Fokion Sotiropoulos Date: Mon, 5 Jun 2023 21:44:24 +0100 Subject: [PATCH] adding a unit test for the HideSensitive --- log.go | 4 ++-- log_test.go | 18 ++++++++++++++++++ venom_output.go | 19 +++++++++++++------ 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 log_test.go diff --git a/log.go b/log.go index 72bf6748..c3a80b72 100644 --- a/log.go +++ b/log.go @@ -35,6 +35,7 @@ func fieldsFromContext(ctx context.Context, keys ...string) logrus.Fields { return fields } +// HideSensitive replace the value with __hidden__ func HideSensitive(ctx context.Context, arg interface{}) string { s := ctx.Value(ContextKey("secrets")) cleanVars := fmt.Sprint(arg) @@ -44,8 +45,7 @@ func HideSensitive(ctx context.Context, arg interface{}) string { secrets := reflect.ValueOf(s) for i := 0; i < secrets.Len(); i++ { secret := fmt.Sprint(secrets.Index(i).Interface()) - stringArg := fmt.Sprint(arg) - cleanVars = strings.ReplaceAll(stringArg, secret, "__hidden__") + cleanVars = strings.ReplaceAll(cleanVars, secret, "__hidden__") } } } diff --git a/log_test.go b/log_test.go new file mode 100644 index 00000000..bec96ce9 --- /dev/null +++ b/log_test.go @@ -0,0 +1,18 @@ +package venom + +import ( + "context" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestHideSensitive(t *testing.T) { + ctx := context.Background() + ctx = context.WithValue(ctx, ContextKey("secrets"), []string{"Joe", "Doe"}) + assert.Equal(t, "__hidden__", HideSensitive(ctx, "Joe")) + assert.Equal(t, "__hidden__ tests something", HideSensitive(ctx, "Joe tests something")) + assert.Equal(t, "Dave tests something", HideSensitive(ctx, "Dave tests something")) + assert.Equal(t, "1234", HideSensitive(ctx, 1234)) + assert.Equal(t, "__hidden__!", HideSensitive(ctx, "Doe!")) + assert.Equal(t, "__hidden__ __hidden__", HideSensitive(ctx, "Joe Doe")) +} diff --git a/venom_output.go b/venom_output.go index 516ddbc4..ffa1a545 100644 --- a/venom_output.go +++ b/venom_output.go @@ -24,18 +24,25 @@ func init() { } } +// CleanUpSecrets This method tries to hide all the sensitive variables func (v *Venom) CleanUpSecrets(testSuite TestSuite) TestSuite { for _, testCase := range testSuite.TestCases { ctx := v.processSecrets(context.Background(), &testSuite, &testCase) for _, result := range testCase.TestStepResults { - for i, v := range result.ComputedVars { - result.ComputedVars[i] = HideSensitive(ctx, v) + for k, v := range result.ComputedVars { + if !strings.HasPrefix(k, "venom.") { + result.ComputedVars[k] = HideSensitive(ctx, v) + } } - for i, v := range result.InputVars { - result.InputVars[i] = HideSensitive(ctx, v) + for k, v := range result.InputVars { + if !strings.HasPrefix(k, "venom.") { + result.InputVars[k] = HideSensitive(ctx, v) + } } - for i, v := range testCase.TestCaseInput.Vars { - testCase.TestCaseInput.Vars[i] = HideSensitive(ctx, v) + for k, v := range testCase.TestCaseInput.Vars { + if !strings.HasPrefix(k, "venom.") { + testCase.TestCaseInput.Vars[k] = HideSensitive(ctx, v) + } } result.Raw = HideSensitive(ctx, fmt.Sprint(result.Raw)) result.Interpolated = HideSensitive(ctx, fmt.Sprint(result.Interpolated))