Skip to content

Commit

Permalink
refactored the sha256 transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 21, 2023
1 parent 4f31fd7 commit a8f9c64
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 4 deletions.
3 changes: 3 additions & 0 deletions worker/internal/benthos/transformers/random_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func GenerateRandomFloat(value float64, preserveLength bool, digitsAfterDecimal,
res, err := GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal)
return res, err
}

Check warning on line 87 in worker/internal/benthos/transformers/random_float.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_float.go#L85-L87

Added lines #L85 - L87 were not covered by tests
} else if digitsBeforeDecimal != 0 && digitsAfterDecimal != 0 {
res, err := GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal)
return res, err
} else {
res, err := GenerateRandomFloatWithRandomLength()
return res, err
Expand Down
13 changes: 13 additions & 0 deletions worker/internal/benthos/transformers/random_float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ func TestRandomFloatTransformerWithNoValue(t *testing.T) {
assert.Equal(t, 6, actual, "The length of the output float needs to match the digits before + the digits after")
}

func TestRandomFloatTransformerWithNoValueAndLength(t *testing.T) {
mapping := `root = randomfloattransformer(digits_before_decimal: 2, digits_after_decimal: 2)`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random float transformer")

res, err := ex.Query(nil)
assert.NoError(t, err)

actual := GetFloatLength(res.(float64)).DigitsAfterDecimalLength + GetFloatLength(res.(float64)).DigitsBeforeDecimalLength
assert.IsType(t, res, float64(1))
assert.Equal(t, 4, actual, "The length of the output float needs to match the digits before + the digits after")
}

func Test_GetFloatLength(t *testing.T) {
val := float64(3.14)
res := GetFloatLength(val)
Expand Down
12 changes: 10 additions & 2 deletions worker/internal/benthos/transformers/random_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,24 @@ func GenerateRandomInt(value int64, preserveLength bool, intLength int64) (int64
returnValue = val

}
} else {
} else if intLength != 0 {

val, err := transformer_utils.GenerateRandomInt(defaultIntLength)
val, err := transformer_utils.GenerateRandomInt(intLength)

if err != nil {
return 0, fmt.Errorf("unable to generate a random string with length")
}

returnValue = val

} else {
val, err := transformer_utils.GenerateRandomInt(defaultIntLength)

if err != nil {
return 0, fmt.Errorf("unable to generate a random string with length")
}

returnValue = val
}

return returnValue, nil
Expand Down
12 changes: 12 additions & 0 deletions worker/internal/benthos/transformers/random_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ func TestRandomIntTransformerWithNoValue(t *testing.T) {
assert.Equal(t, int64(4), transformer_utils.GetIntLength(res.(int64))) // Generated int must be the same length as the input int"
assert.IsType(t, res, int64(2))
}

func TestRandomIntTransformerWithNoValueAndLength(t *testing.T) {
mapping := `root = randominttransformer(int_length: 4)`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random int transformer")

res, err := ex.Query(nil)
assert.NoError(t, err)

assert.Equal(t, int64(4), transformer_utils.GetIntLength(res.(int64))) // Generated int must be the same length as the input int"
assert.IsType(t, res, int64(2))
}
10 changes: 10 additions & 0 deletions worker/internal/benthos/transformers/random_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func GenerateRandomString(value string, preserveLength bool, strLength int64) (s
returnValue = val

}
} else if strLength != 0 {

val, err := transformer_utils.GenerateRandomStringWithLength(strLength)

if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
}

returnValue = val

} else {

val, err := transformer_utils.GenerateRandomStringWithLength(defaultStrLength)
Expand Down
12 changes: 12 additions & 0 deletions worker/internal/benthos/transformers/random_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ func TestRandomStringTransformerWithNoValue(t *testing.T) {
assert.Equal(t, 10, len(res.(string)), "Generated string must be the same length as the input string")
assert.IsType(t, res, "")
}

func TestRandomStringTransformerWithNoValueAndLength(t *testing.T) {
mapping := `root = randomstringtransformer(str_length:6)`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random string transformer")

res, err := ex.Query(nil)
assert.NoError(t, err)

assert.Equal(t, 6, len(res.(string)), "Generated string must be the same length as the input string")
assert.IsType(t, res, "")
}
65 changes: 65 additions & 0 deletions worker/internal/benthos/transformers/sha256hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package neosync_transformers

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"

"github.com/benthosdev/benthos/v4/public/bloblang"
_ "github.com/benthosdev/benthos/v4/public/components/io"
transformer_utils "github.com/nucleuscloud/neosync/worker/internal/benthos/transformers/utils"
)

func init() {

spec := bloblang.NewPluginSpec()

err := bloblang.RegisterFunctionV2("sha256hash", spec, func(args *bloblang.ParsedParams) (bloblang.Function, error) {

return func() (any, error) {

val, err := GenerateRandomSHA256Hash()

if err != nil {
return false, fmt.Errorf("unable to generate sha256 hash")
}
return val, nil

Check warning on line 27 in worker/internal/benthos/transformers/sha256hash.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/sha256hash.go#L19-L27

Added lines #L19 - L27 were not covered by tests
}, nil
})
if err != nil {
panic(err)

Check warning on line 31 in worker/internal/benthos/transformers/sha256hash.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/sha256hash.go#L31

Added line #L31 was not covered by tests
}
}

func GenerateRandomSHA256Hash() (string, error) {

length, err := transformer_utils.GenerateRandomInt(1)
if err != nil {
return "", err
}

Check warning on line 40 in worker/internal/benthos/transformers/sha256hash.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/sha256hash.go#L39-L40

Added lines #L39 - L40 were not covered by tests

str, err := transformer_utils.GenerateRandomStringWithLength(length)
if err != nil {
return "", err
}

Check warning on line 45 in worker/internal/benthos/transformers/sha256hash.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/sha256hash.go#L44-L45

Added lines #L44 - L45 were not covered by tests

// hash the value
bites := []byte(str)
hasher := sha256.New()
_, err = hasher.Write(bites)
if err != nil {
return "", err
}

Check warning on line 53 in worker/internal/benthos/transformers/sha256hash.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/sha256hash.go#L52-L53

Added lines #L52 - L53 were not covered by tests

// compute sha256 checksum and encode it into a hex string
hashed := hasher.Sum(nil)
var buf bytes.Buffer
e := hex.NewEncoder(&buf)
_, err = e.Write(hashed)
if err != nil {
return "", err
}

Check warning on line 62 in worker/internal/benthos/transformers/sha256hash.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/sha256hash.go#L61-L62

Added lines #L61 - L62 were not covered by tests

return buf.String(), nil
}
15 changes: 15 additions & 0 deletions worker/internal/benthos/transformers/sha256hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package neosync_transformers

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_GenerateSHA256Hash(t *testing.T) {

res, err := GenerateRandomSHA256Hash()
assert.NoError(t, err)

assert.IsType(t, "", res)
}
8 changes: 6 additions & 2 deletions worker/pkg/workflows/datasync/activities/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func computeMutationFunction(col *mgmtv1alpha1.JobMapping) (string, error) {
case "random_string":
pl := col.Transformer.Config.GetRandomStringConfig().PreserveLength
sl := col.Transformer.Config.GetRandomStringConfig().StrLength
return fmt.Sprintf(`this.%s.randomstringtransformer(%t, %d)`, col.Column, pl, sl), nil
return fmt.Sprintf(`randomstringtransformer(this.%s,%t, %d)`, col.Column, pl, sl), nil

Check warning on line 601 in worker/pkg/workflows/datasync/activities/activities.go

View check run for this annotation

Codecov / codecov/patch

worker/pkg/workflows/datasync/activities/activities.go#L601

Added line #L601 was not covered by tests
case "random_int":
pl := col.Transformer.Config.GetRandomIntConfig().PreserveLength
sl := col.Transformer.Config.GetRandomIntConfig().IntLength
Expand Down Expand Up @@ -629,7 +629,11 @@ func computeMutationFunction(col *mgmtv1alpha1.JobMapping) (string, error) {
luhn := col.Transformer.Config.GetCardNumberConfig().ValidLuhn
return fmt.Sprintf(`cardnumbertransformer(%t)`, luhn), nil
case "sha256":
return fmt.Sprintf(`this.%s.bytes().hash("sha256").encode("hex")`, col.Column), nil
if col.Column != "" {
return fmt.Sprintf(`root = this.%s.bytes().hash("sha256").encode("hex")`, col.Column), nil
} else {
return `sha256hash()`, nil
}

Check warning on line 636 in worker/pkg/workflows/datasync/activities/activities.go

View check run for this annotation

Codecov / codecov/patch

worker/pkg/workflows/datasync/activities/activities.go#L632-L636

Added lines #L632 - L636 were not covered by tests
case "social_security_number":
return "ssntransformer()", nil
default:
Expand Down

0 comments on commit a8f9c64

Please sign in to comment.