This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from streamdal/blinktag/dynamic_transforms
ENG-1458 - Dynamic Transforms
- Loading branch information
Showing
4 changed files
with
136 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/json" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
. "github.com/onsi/ginkgo/v2" | ||
|
@@ -13,6 +14,10 @@ import ( | |
|
||
"github.com/streamdal/streamdal/libs/protos/build/go/protos" | ||
"github.com/streamdal/streamdal/libs/protos/build/go/protos/steps" | ||
|
||
"github.com/streamdal/go-sdk/logger" | ||
"github.com/streamdal/go-sdk/metrics/metricsfakes" | ||
"github.com/streamdal/go-sdk/server/serverfakes" | ||
) | ||
|
||
var _ = Describe("WASM Modules", func() { | ||
|
@@ -396,7 +401,7 @@ var _ = Describe("WASM Modules", func() { | |
Expect(wasmResp.OutputPayload).Should(MatchJSON(`{"object": {"type": "str", "cc_num": "1234"}}`)) | ||
}) | ||
|
||
It("truncates a field by total length", func() { | ||
It("truncates a field by percent length", func() { | ||
wasmData, err := os.ReadFile("test-assets/wasm/transform.wasm") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
|
@@ -408,8 +413,8 @@ var _ = Describe("WASM Modules", func() { | |
Options: &steps.TransformStep_TruncateOptions{ | ||
TruncateOptions: &steps.TransformTruncateOptions{ | ||
Path: "object.type", | ||
Type: steps.TransformTruncateType_TRANSFORM_TRUNCATE_TYPE_LENGTH, | ||
Value: 3, | ||
Type: steps.TransformTruncateType_TRANSFORM_TRUNCATE_TYPE_PERCENTAGE, | ||
Value: 50, | ||
}, | ||
}, | ||
}, | ||
|
@@ -432,7 +437,7 @@ var _ = Describe("WASM Modules", func() { | |
Expect(err).ToNot(HaveOccurred()) | ||
Expect(wasmResp).ToNot(BeNil()) | ||
Expect(wasmResp.ExitCode).To(Equal(protos.WASMExitCode_WASM_EXIT_CODE_SUCCESS)) | ||
Expect(wasmResp.OutputPayload).Should(MatchJSON(`{"object": {"type": "str", "cc_num": "1234"}}`)) | ||
Expect(wasmResp.OutputPayload).Should(MatchJSON(`{"object": {"type": "stre", "cc_num": "1234"}}`)) | ||
}) | ||
}) | ||
|
||
|
@@ -619,4 +624,116 @@ var _ = Describe("WASM Modules", func() { | |
|
||
// TODO: additional tests for each transform type | ||
}) | ||
|
||
Context("inter step result", func() { | ||
It("finds and transforms PII in a payload without a path", func() { | ||
payload := []byte(`{ | ||
"users": [ | ||
{ | ||
"name": "Bob", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Mary", | ||
"email": "[email protected]" | ||
} | ||
] | ||
}`) | ||
detectiveWASM, err := os.ReadFile("test-assets/wasm/detective.wasm") | ||
Expect(err).ToNot(HaveOccurred()) | ||
transformWASM, err := os.ReadFile("test-assets/wasm/transform.wasm") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
pipeline := &protos.Pipeline{ | ||
Id: uuid.New().String(), | ||
Name: "Test Pipeline", | ||
Steps: []*protos.PipelineStep{ | ||
{ | ||
Name: "Find Email", | ||
XWasmId: stringPtr(uuid.New().String()), | ||
XWasmBytes: detectiveWASM, | ||
XWasmFunction: stringPtr("f"), | ||
OnSuccess: make([]protos.PipelineStepCondition, 0), | ||
OnFailure: []protos.PipelineStepCondition{protos.PipelineStepCondition_PIPELINE_STEP_CONDITION_ABORT_ALL}, | ||
Step: &protos.PipelineStep_Detective{ | ||
Detective: &steps.DetectiveStep{ | ||
Path: stringPtr(""), // No path, we're searching the entire payload | ||
Negate: boolPtr(false), | ||
Type: steps.DetectiveType_DETECTIVE_TYPE_PII_EMAIL, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Dynamic: true, | ||
Name: "Transform Email", | ||
XWasmId: stringPtr(uuid.New().String()), | ||
XWasmBytes: transformWASM, | ||
XWasmFunction: stringPtr("f"), | ||
OnSuccess: make([]protos.PipelineStepCondition, 0), | ||
OnFailure: []protos.PipelineStepCondition{protos.PipelineStepCondition_PIPELINE_STEP_CONDITION_ABORT_ALL}, | ||
Step: &protos.PipelineStep_Transform{ | ||
Transform: &steps.TransformStep{ | ||
Type: steps.TransformType_TRANSFORM_TYPE_REPLACE_VALUE, | ||
Options: &steps.TransformStep_ReplaceValueOptions{ | ||
ReplaceValueOptions: &steps.TransformReplaceValueOptions{ | ||
Path: "", // No path, we're getting the result from the detective step | ||
Value: `"REDACTED"`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
aud := &protos.Audience{ | ||
ServiceName: "mysvc1", | ||
ComponentName: "kafka", | ||
OperationType: protos.OperationType_OPERATION_TYPE_PRODUCER, | ||
OperationName: "mytopic", | ||
} | ||
|
||
s := &Streamdal{ | ||
serverClient: &serverfakes.FakeIServerClient{}, | ||
functionsMtx: &sync.RWMutex{}, | ||
functions: map[string]*function{}, | ||
audiencesMtx: &sync.RWMutex{}, | ||
audiences: map[string]struct{}{}, | ||
tails: map[string]map[string]*Tail{}, | ||
tailsMtx: &sync.RWMutex{}, | ||
config: &Config{ | ||
ServiceName: "mysvc1", | ||
Logger: &logger.TinyLogger{}, | ||
StepTimeout: time.Millisecond * 1000, | ||
PipelineTimeout: time.Millisecond * 1000, | ||
}, | ||
metrics: &metricsfakes.FakeIMetrics{}, | ||
pipelinesMtx: &sync.RWMutex{}, | ||
pipelines: map[string]map[string]*protos.Command{ | ||
audToStr(aud): { | ||
pipeline.Id: { | ||
Audience: aud, | ||
Command: &protos.Command_AttachPipeline{ | ||
AttachPipeline: &protos.AttachPipelineCommand{ | ||
Pipeline: pipeline, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
resp := s.Process(context.Background(), &ProcessRequest{ | ||
ComponentName: aud.ComponentName, | ||
OperationType: OperationType(aud.OperationType), | ||
OperationName: aud.OperationName, | ||
Data: payload, | ||
}) | ||
|
||
Expect(resp.Error).To(BeFalse()) | ||
Expect(resp.ErrorMessage).To(Equal("")) | ||
Expect(resp.Data).To(MatchJSON(`{"users": [{"name":"Bob","email":"REDACTED"},{"name":"Mary","email":"REDACTED"}]}`)) | ||
}) | ||
|
||
}) | ||
}) |