Skip to content

Commit

Permalink
adding a unit test for the HideSensitive
Browse files Browse the repository at this point in the history
Signed-off-by: Fokion Sotiropoulos <[email protected]>
  • Loading branch information
fokion committed Jun 5, 2023
1 parent d774ec6 commit c28e105
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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__")
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}
19 changes: 13 additions & 6 deletions venom_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit c28e105

Please sign in to comment.